Add forwarding history sync from lnd to db

This commit is contained in:
Yaacov Akiba Slama
2021-02-05 07:11:53 +02:00
parent 22b2b365ed
commit aca8c4f2c0
9 changed files with 173 additions and 1 deletions

62
db.go
View File

@@ -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())
}