mirror of
https://github.com/aljazceru/lspd.git
synced 2026-01-04 14:44:30 +01:00
Add forwarding history sync from lnd to db
This commit is contained in:
62
db.go
62
db.go
@@ -69,3 +69,65 @@ func registerPayment(destination, paymentHash, paymentSecret []byte, incomingAmo
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertChannel(chanID uint64, channelPoint string, nodeID []byte) error {
|
||||
_, err := pgxPool.Exec(context.Background(),
|
||||
`INSERT INTO
|
||||
channels (chanis, channel_point, nodeid)
|
||||
VALUES ($1, $2, $3)`,
|
||||
chanID, channelPoint, nodeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insertChannel(%v, %s, %x) error: %w",
|
||||
chanID, channelPoint, nodeID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func lastForwardingEvent() (int64, error) {
|
||||
var last int64
|
||||
err := pgxPool.QueryRow(context.Background(),
|
||||
`SELECT coalesce(MAX("timestamp"), 0) AS last FROM forwarding_history`).Scan(&last)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return last, nil
|
||||
}
|
||||
|
||||
func insertForwardingEvents(rowSrc pgx.CopyFromSource) error {
|
||||
|
||||
tx, err := pgxPool.Begin(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("pgxPool.Begin() error: %w", err)
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
_, err = tx.Exec(context.Background(), `
|
||||
CREATE TEMP TABLE tmp_table ON COMMIT DROP AS
|
||||
SELECT *
|
||||
FROM forwarding_history
|
||||
WITH NO DATA;
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CREATE TEMP TABLE error: %w", err)
|
||||
}
|
||||
|
||||
count, err := tx.CopyFrom(context.Background(),
|
||||
pgx.Identifier{"tmp_table"},
|
||||
[]string{"timestamp", "chanid_in", "chanid_out", "amt_msat_in", "amt_msat_out"}, rowSrc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CopyFrom() error: %w", err)
|
||||
}
|
||||
log.Printf("count1: %v", count)
|
||||
|
||||
cmdTag, err := tx.Exec(context.Background(), `
|
||||
INSERT INTO forwarding_history
|
||||
SELECT *
|
||||
FROM tmp_table
|
||||
ON CONFLICT DO NOTHING
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("INSERT INTO forwarding_history error: %w", err)
|
||||
}
|
||||
log.Printf("count2: %v", cmdTag.RowsAffected())
|
||||
return tx.Commit(context.Background())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user