From ee75d838a143c4970307abbac0093d91d00ee533 Mon Sep 17 00:00:00 2001 From: Yaacov Akiba Slama Date: Mon, 5 Apr 2021 18:31:02 +0300 Subject: [PATCH] Add field last_update to channels --- db.go | 11 ++++++----- forwarding_history.go | 3 ++- intercept.go | 2 +- .../migrations/000007_channels_last_update.down.sql | 1 + .../migrations/000007_channels_last_update.up.sql | 1 + 5 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 postgresql/migrations/000007_channels_last_update.down.sql create mode 100644 postgresql/migrations/000007_channels_last_update.up.sql diff --git a/db.go b/db.go index a2aeec5..42dd66c 100644 --- a/db.go +++ b/db.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "time" "github.com/jackc/pgtype" "github.com/jackc/pgx/v4" @@ -70,13 +71,13 @@ func registerPayment(destination, paymentHash, paymentSecret []byte, incomingAmo return nil } -func insertChannel(chanID uint64, channelPoint string, nodeID []byte) error { +func insertChannel(chanID uint64, channelPoint string, nodeID []byte, lastUpdate time.Time) error { _, err := pgxPool.Exec(context.Background(), `INSERT INTO - channels (chanid, channel_point, nodeid) - VALUES ($1, $2, $3) - ON CONFLICT (chanid) DO NOTHING`, - chanID, channelPoint, nodeID) + channels (chanid, channel_point, nodeid, last_update) + VALUES ($1, $2, $3, $4) + ON CONFLICT (chanid) DO UPDATE SET last_update=$4`, + chanID, channelPoint, nodeID, lastUpdate) if err != nil { return fmt.Errorf("insertChannel(%v, %s, %x) error: %w", chanID, channelPoint, nodeID, err) diff --git a/forwarding_history.go b/forwarding_history.go index 48bc35d..4310d8f 100644 --- a/forwarding_history.go +++ b/forwarding_history.go @@ -53,13 +53,14 @@ func channelsSynchronizeOnce() error { return fmt.Errorf("client.ListChannels() error: %w", err) } log.Printf("channelsSynchronizeOnce - received channels") + lastUpdate := time.Now() for _, c := range channels.Channels { nodeID, err := hex.DecodeString(c.RemotePubkey) if err != nil { log.Printf("hex.DecodeString in channelsSynchronizeOnce error: %v", err) continue } - err = insertChannel(c.ChanId, c.ChannelPoint, nodeID) + err = insertChannel(c.ChanId, c.ChannelPoint, nodeID, lastUpdate) if err != nil { log.Printf("insertChannel(%v, %v, %x) in channelsSynchronizeOnce error: %v", c.ChanId, c.ChannelPoint, nodeID, err) continue diff --git a/intercept.go b/intercept.go index 60d8afb..6cfa877 100644 --- a/intercept.go +++ b/intercept.go @@ -243,7 +243,7 @@ func resumeOrCancel( OutgoingRequestedChanId: chanID, OnionBlob: onionBlob, }) - err := insertChannel(chanID, channelPoint, destination) + err := insertChannel(chanID, channelPoint, destination, time.Now()) if err != nil { log.Printf("insertChannel error: %v", err) } diff --git a/postgresql/migrations/000007_channels_last_update.down.sql b/postgresql/migrations/000007_channels_last_update.down.sql new file mode 100644 index 0000000..4442e51 --- /dev/null +++ b/postgresql/migrations/000007_channels_last_update.down.sql @@ -0,0 +1 @@ +ALTER TABLE public.channels DROP COLUMN last_update; \ No newline at end of file diff --git a/postgresql/migrations/000007_channels_last_update.up.sql b/postgresql/migrations/000007_channels_last_update.up.sql new file mode 100644 index 0000000..83db788 --- /dev/null +++ b/postgresql/migrations/000007_channels_last_update.up.sql @@ -0,0 +1 @@ +ALTER TABLE public.channels ADD COLUMN last_update TIMESTAMP; \ No newline at end of file