From 9597f01ce3659b8231908f67c4039d42b47483b5 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 18 Jun 2025 08:21:06 +0200 Subject: [PATCH] graph/db: thread context through to HighestChanID --- discovery/chan_series.go | 10 +++++++--- discovery/syncer.go | 6 ++++-- discovery/syncer_test.go | 5 ++++- graph/db/graph_test.go | 6 +++--- graph/db/interfaces.go | 2 +- graph/db/kv_store.go | 2 +- graph/db/sql_store.go | 4 +--- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/discovery/chan_series.go b/discovery/chan_series.go index 696a908c4..a6787edf9 100644 --- a/discovery/chan_series.go +++ b/discovery/chan_series.go @@ -1,6 +1,7 @@ package discovery import ( + "context" "time" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -22,7 +23,8 @@ type ChannelGraphTimeSeries interface { // height that's close to the current tip of the main chain as we // know it. We'll use this to start our QueryChannelRange dance with // the remote node. - HighestChanID(chain chainhash.Hash) (*lnwire.ShortChannelID, error) + HighestChanID(ctx context.Context, + chain chainhash.Hash) (*lnwire.ShortChannelID, error) // UpdatesInHorizon returns all known channel and node updates with an // update timestamp between the start time and end time. We'll use this @@ -87,8 +89,10 @@ func NewChanSeries(graph *graphdb.ChannelGraph) *ChanSeries { // this to start our QueryChannelRange dance with the remote node. // // NOTE: This is part of the ChannelGraphTimeSeries interface. -func (c *ChanSeries) HighestChanID(chain chainhash.Hash) (*lnwire.ShortChannelID, error) { - chanID, err := c.graph.HighestChanID() +func (c *ChanSeries) HighestChanID(ctx context.Context, + _ chainhash.Hash) (*lnwire.ShortChannelID, error) { + + chanID, err := c.graph.HighestChanID(ctx) if err != nil { return nil, err } diff --git a/discovery/syncer.go b/discovery/syncer.go index ccea2dc29..0ebfac4c2 100644 --- a/discovery/syncer.go +++ b/discovery/syncer.go @@ -965,12 +965,14 @@ func (g *GossipSyncer) processChanRangeReply(_ context.Context, // party when we're kicking off the channel graph synchronization upon // connection. The historicalQuery boolean can be used to generate a query from // the genesis block of the chain. -func (g *GossipSyncer) genChanRangeQuery(_ context.Context, +func (g *GossipSyncer) genChanRangeQuery(ctx context.Context, historicalQuery bool) (*lnwire.QueryChannelRange, error) { // First, we'll query our channel graph time series for its highest // known channel ID. - newestChan, err := g.cfg.channelSeries.HighestChanID(g.cfg.chainHash) + newestChan, err := g.cfg.channelSeries.HighestChanID( + ctx, g.cfg.chainHash, + ) if err != nil { return nil, err } diff --git a/discovery/syncer_test.go b/discovery/syncer_test.go index 44e8d6d70..5d5e82ef5 100644 --- a/discovery/syncer_test.go +++ b/discovery/syncer_test.go @@ -79,9 +79,12 @@ func newMockChannelGraphTimeSeries( } } -func (m *mockChannelGraphTimeSeries) HighestChanID(chain chainhash.Hash) (*lnwire.ShortChannelID, error) { +func (m *mockChannelGraphTimeSeries) HighestChanID(_ context.Context, + _ chainhash.Hash) (*lnwire.ShortChannelID, error) { + return &m.highestID, nil } + func (m *mockChannelGraphTimeSeries) UpdatesInHorizon(chain chainhash.Hash, startTime time.Time, endTime time.Time) ([]lnwire.Message, error) { diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index daec52526..dbd828527 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -1881,7 +1881,7 @@ func TestHighestChanID(t *testing.T) { // If we don't yet have any channels in the database, then we should // get a channel ID of zero if we ask for the highest channel ID. - bestID, err := graph.HighestChanID() + bestID, err := graph.HighestChanID(ctx) require.NoError(t, err, "unable to get highest ID") if bestID != 0 { t.Fatalf("best ID w/ no chan should be zero, is instead: %v", @@ -1907,7 +1907,7 @@ func TestHighestChanID(t *testing.T) { // Now that the edges has been inserted, we'll query for the highest // known channel ID in the database. - bestID, err = graph.HighestChanID() + bestID, err = graph.HighestChanID(ctx) require.NoError(t, err, "unable to get highest ID") if bestID != chanID2.ToUint64() { @@ -1921,7 +1921,7 @@ func TestHighestChanID(t *testing.T) { if err := graph.AddChannelEdge(ctx, &edge3); err != nil { t.Fatalf("unable to create channel edge: %v", err) } - bestID, err = graph.HighestChanID() + bestID, err = graph.HighestChanID(ctx) require.NoError(t, err, "unable to get highest ID") if bestID != chanID3.ToUint64() { diff --git a/graph/db/interfaces.go b/graph/db/interfaces.go index df629383e..0759637fd 100644 --- a/graph/db/interfaces.go +++ b/graph/db/interfaces.go @@ -227,7 +227,7 @@ type V1Store interface { //nolint:interfacebloat // graph. This represents the "newest" channel from the PoV of the // chain. This method can be used by peers to quickly determine if // they're graphs are in sync. - HighestChanID() (uint64, error) + HighestChanID(ctx context.Context) (uint64, error) // ChanUpdatesInHorizon returns all the known channel edges which have // at least one edge that has an update timestamp within the specified diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 4bd635737..bb5512750 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -1948,7 +1948,7 @@ func getChanID(tx kvdb.RTx, chanPoint *wire.OutPoint) (uint64, error) { // HighestChanID returns the "highest" known channel ID in the channel graph. // This represents the "newest" channel from the PoV of the chain. This method // can be used by peers to quickly determine if they're graphs are in sync. -func (c *KVStore) HighestChanID() (uint64, error) { +func (c *KVStore) HighestChanID(_ context.Context) (uint64, error) { var cid uint64 err := kvdb.View(c.db, func(tx kvdb.RTx) error { diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index ce44b2f78..564ce96c6 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -543,9 +543,7 @@ func (s *SQLStore) AddChannelEdge(ctx context.Context, // can be used by peers to quickly determine if their graphs are in sync. // // NOTE: This is part of the V1Store interface. -func (s *SQLStore) HighestChanID() (uint64, error) { - ctx := context.TODO() - +func (s *SQLStore) HighestChanID(ctx context.Context) (uint64, error) { var highestChanID uint64 err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { chanID, err := db.HighestSCID(ctx, int16(ProtocolV1))