From 28fe3585acd39e7695703e7bfab161aa2bd1e839 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 3 Feb 2026 19:12:34 +0200 Subject: [PATCH] graph/db: version ChannelID lookup Add a gossip version parameter to the ChannelID method (outpoint to short channel ID lookup) in the Store interface and both KV/SQL implementations. Update the VersionedGraph wrapper and tests. --- graph/db/graph.go | 16 ++++++++++++---- graph/db/graph_test.go | 2 +- graph/db/interfaces.go | 3 ++- graph/db/kv_store.go | 8 +++++++- graph/db/sql_store.go | 6 ++++-- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/graph/db/graph.go b/graph/db/graph.go index 91286b75f..3e6b4e730 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -177,8 +177,9 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error { return nil }, func() {}, ) - if err != nil && - !errors.Is(err, ErrVersionNotSupportedForKVDB) { + if err != nil && !errors.Is( + err, ErrVersionNotSupportedForKVDB, + ) { return err } @@ -685,8 +686,10 @@ func (c *ChannelGraph) AddEdgeProof(chanID lnwire.ShortChannelID, } // ChannelID attempts to lookup the 8-byte compact channel ID. -func (c *ChannelGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) { - return c.db.ChannelID(chanPoint) +func (c *ChannelGraph) ChannelID(v lnwire.GossipVersion, + chanPoint *wire.OutPoint) (uint64, error) { + + return c.db.ChannelID(v, chanPoint) } // HighestChanID returns the "highest" known channel ID in the channel graph. @@ -947,6 +950,11 @@ func (c *VersionedGraph) ForEachChannelCacheable( return c.db.ForEachChannelCacheable(c.v, cb, reset) } +// ChannelID attempts to lookup the 8-byte compact channel ID. +func (c *VersionedGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) { + return c.db.ChannelID(c.v, chanPoint) +} + // IsPublicNode determines whether the node is seen as public in the graph. func (c *VersionedGraph) IsPublicNode(pubKey [33]byte) (bool, error) { return c.db.IsPublicNode(c.v, pubKey) diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index f7776af16..dbdfb1376 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -1238,7 +1238,7 @@ func TestEdgeInfoUpdates(t *testing.T) { // We should also be able to retrieve the channelID only knowing the // channel point of the channel. - dbChanID, err := graph.ChannelID(&outpoint) + dbChanID, err := graph.ChannelID(lnwire.GossipVersion1, &outpoint) require.NoError(t, err, "unable to retrieve channel ID") if dbChanID != chanID { t.Fatalf("chan ID's mismatch, expected %v got %v", dbChanID, diff --git a/graph/db/interfaces.go b/graph/db/interfaces.go index 5a6d27d55..95d8bb3b7 100644 --- a/graph/db/interfaces.go +++ b/graph/db/interfaces.go @@ -239,7 +239,8 @@ type Store interface { //nolint:interfacebloat // ChannelID attempt to lookup the 8-byte compact channel ID which maps // to the passed channel point (outpoint). If the passed channel doesn't // exist within the database, then ErrEdgeNotFound is returned. - ChannelID(chanPoint *wire.OutPoint) (uint64, error) + ChannelID(v lnwire.GossipVersion, + 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 diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 3a143ee7e..bdb1a780a 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -1998,7 +1998,13 @@ func (c *KVStore) DeleteChannelEdges(v lnwire.GossipVersion, // ChannelID attempt to lookup the 8-byte compact channel ID which maps to the // passed channel point (outpoint). If the passed channel doesn't exist within // the database, then ErrEdgeNotFound is returned. -func (c *KVStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) { +func (c *KVStore) ChannelID(v lnwire.GossipVersion, + chanPoint *wire.OutPoint) (uint64, error) { + + if v != lnwire.GossipVersion1 { + return 0, ErrVersionNotSupportedForKVDB + } + var chanID uint64 if err := kvdb.View(c.db, func(tx kvdb.RTx) error { var err error diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 75f06f667..e2b8477ea 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -2507,7 +2507,9 @@ func (s *SQLStore) HasChannelEdge(v lnwire.GossipVersion, // the database, then ErrEdgeNotFound is returned. // // NOTE: part of the Store interface. -func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) { +func (s *SQLStore) ChannelID(v lnwire.GossipVersion, + chanPoint *wire.OutPoint) (uint64, error) { + var ( ctx = context.TODO() channelID uint64 @@ -2516,7 +2518,7 @@ func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) { chanID, err := db.GetSCIDByOutpoint( ctx, sqlc.GetSCIDByOutpointParams{ Outpoint: chanPoint.String(), - Version: int16(lnwire.GossipVersion1), + Version: int16(v), }, ) if errors.Is(err, sql.ErrNoRows) {