From e58abbf0e540f284a0057654efe27947f9e9ae82 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 18 Feb 2025 11:04:27 -0300 Subject: [PATCH 1/2] graph/db: fix edges bucket check This commit fixes a bug that would check that the passed `edge` argument of UpdateChannelEdge is nil but it should actually be checking if the `edges` bucket is nil. --- graph/db/graph.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graph/db/graph.go b/graph/db/graph.go index 8004a2a5d..11943c7e9 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -1309,7 +1309,7 @@ func (c *ChannelGraph) UpdateChannelEdge(edge *models.ChannelEdgeInfo) error { return kvdb.Update(c.db, func(tx kvdb.RwTx) error { edges := tx.ReadWriteBucket(edgeBucket) - if edge == nil { + if edges == nil { return ErrEdgeNotFound } From 9df8773163c1786df7f5102df5eb76e0c793ab09 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Sat, 1 Feb 2025 13:26:48 +0200 Subject: [PATCH 2/2] graph: update proof by ID This commit restricts the graph CRUD interface such that one can only add a proof to a channel announcement and not update any other fields. This pattern is better suited for SQL land too. --- graph/builder.go | 9 +-------- graph/db/graph.go | 23 ++++++++++------------- graph/interfaces.go | 10 ++++------ 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/graph/builder.go b/graph/builder.go index 4c6557b93..c379fe93f 100644 --- a/graph/builder.go +++ b/graph/builder.go @@ -1441,14 +1441,7 @@ func (b *Builder) ForAllOutgoingChannels(cb func(*models.ChannelEdgeInfo, func (b *Builder) AddProof(chanID lnwire.ShortChannelID, proof *models.ChannelAuthProof) error { - info, _, _, err := b.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64()) - if err != nil { - return err - } - - info.AuthProof = proof - - return b.cfg.Graph.UpdateChannelEdge(info) + return b.cfg.Graph.AddEdgeProof(chanID, proof) } // IsStaleNode returns true if the graph source has a node announcement for the diff --git a/graph/db/graph.go b/graph/db/graph.go index 11943c7e9..b59476f99 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -1297,15 +1297,13 @@ func (c *ChannelGraph) HasChannelEdge( return upd1Time, upd2Time, exists, isZombie, nil } -// UpdateChannelEdge retrieves and update edge of the graph database. Method -// only reserved for updating an edge info after its already been created. -// In order to maintain this constraints, we return an error in the scenario -// that an edge info hasn't yet been created yet, but someone attempts to update -// it. -func (c *ChannelGraph) UpdateChannelEdge(edge *models.ChannelEdgeInfo) error { +// AddEdgeProof sets the proof of an existing edge in the graph database. +func (c *ChannelGraph) AddEdgeProof(chanID lnwire.ShortChannelID, + proof *models.ChannelAuthProof) error { + // Construct the channel's primary key which is the 8-byte channel ID. var chanKey [8]byte - binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID) + binary.BigEndian.PutUint64(chanKey[:], chanID.ToUint64()) return kvdb.Update(c.db, func(tx kvdb.RwTx) error { edges := tx.ReadWriteBucket(edgeBucket) @@ -1318,15 +1316,14 @@ func (c *ChannelGraph) UpdateChannelEdge(edge *models.ChannelEdgeInfo) error { return ErrEdgeNotFound } - if edgeInfo := edgeIndex.Get(chanKey[:]); edgeInfo == nil { - return ErrEdgeNotFound + edge, err := fetchChanEdgeInfo(edgeIndex, chanKey[:]) + if err != nil { + return err } - if c.graphCache != nil { - c.graphCache.UpdateChannel(edge) - } + edge.AuthProof = proof - return putChanEdgeInfo(edgeIndex, edge, chanKey) + return putChanEdgeInfo(edgeIndex, &edge, chanKey) }, func() {}) } diff --git a/graph/interfaces.go b/graph/interfaces.go index 87a004d25..e351514bb 100644 --- a/graph/interfaces.go +++ b/graph/interfaces.go @@ -259,12 +259,10 @@ type DB interface { *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error) error - // UpdateChannelEdge retrieves and update edge of the graph database. - // Method only reserved for updating an edge info after its already been - // created. In order to maintain this constraints, we return an error in - // the scenario that an edge info hasn't yet been created yet, but - // someone attempts to update it. - UpdateChannelEdge(edge *models.ChannelEdgeInfo) error + // AddEdgeProof sets the proof of an existing edge in the graph + // database. + AddEdgeProof(chanID lnwire.ShortChannelID, + proof *models.ChannelAuthProof) error // IsPublicNode is a helper method that determines whether the node with // the given public key is seen as a public node in the graph from the