From 90179b651e300afee0c76a771bb92ab32b3bb0c9 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 10 Feb 2025 16:29:00 +0200 Subject: [PATCH] graph/db: remove unnecessary AddNode method on GraphCache The AddNode method on the GraphCache calls `AddNodeFeatures` underneath and then iterates through all the node's persisted channels and adds them to the cache too via `AddChannel`. This is, however, not required since at the time the cache is populated in `NewChannelGraph`, the cache is populated will all persisted nodes and all persisted channels. Then, once any new channels come in, via `AddChannelEdge`, they are added to the cache via AddChannel. If any new nodes come in via `AddLightningNode`, then currently the cache's AddNode method is called which the both adds the node and again iterates through all persisted channels and re-adds them to the cache. This is definitely redundent since the initial cache population and updates via AddChannelEdge should keep the cache fresh in terms of channels. So we remove this for 2 reasons: 1) to remove the redundent DB calls and 2) this requires a kvdb.RTx to be passed in to the GraphCache calls which will make it hard to extract the cache out of the CRUD layer and be used more generally. The AddNode method made sense when the cache was first added in the code-base [here](https://github.com/lightningnetwork/lnd/commit/369c09be6152b76a24915050a8a5fe6bccf2b8f0#diff-ae36bdb6670644d20c4e43f3a0ed47f71886c2bcdf3cc2937de24315da5dc072R213) since then during graph cache population, nodes and channels would be added to the cache in a single DB transaction. This was, however, changed [later on](https://github.com/lightningnetwork/lnd/commit/352008a0c22ffd9e97b9481ffa44dbc50fca2ddc) to be done in 2 separate DB calls for efficiency reasons. --- graph/db/graph.go | 5 +---- graph/db/graph_cache.go | 17 ----------------- graph/db/graph_cache_test.go | 18 ++++++++---------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/graph/db/graph.go b/graph/db/graph.go index 06bd0257d..326c66ab0 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -904,10 +904,7 @@ func (c *ChannelGraph) AddLightningNode(node *models.LightningNode, cNode := newGraphCacheNode( node.PubKeyBytes, node.Features, ) - err := c.graphCache.AddNode(tx, cNode) - if err != nil { - return err - } + c.graphCache.AddNodeFeatures(cNode) } return addLightningNode(tx, node) diff --git a/graph/db/graph_cache.go b/graph/db/graph_cache.go index a034e23af..67f34e86e 100644 --- a/graph/db/graph_cache.go +++ b/graph/db/graph_cache.go @@ -136,23 +136,6 @@ func (c *GraphCache) AddNodeFeatures(node GraphCacheNode) { c.mtx.Unlock() } -// AddNode adds a graph node, including all the (directed) channels of that -// node. -func (c *GraphCache) AddNode(tx kvdb.RTx, node GraphCacheNode) error { - c.AddNodeFeatures(node) - - return node.ForEachChannel( - tx, func(tx kvdb.RTx, info *models.ChannelEdgeInfo, - outPolicy *models.ChannelEdgePolicy, - inPolicy *models.ChannelEdgePolicy) error { - - c.AddChannel(info, outPolicy, inPolicy) - - return nil - }, - ) -} - // AddChannel adds a non-directed channel, meaning that the order of policy 1 // and policy 2 does not matter, the directionality is extracted from the info // and policy flags automatically. The policy will be set as the outgoing policy diff --git a/graph/db/graph_cache_test.go b/graph/db/graph_cache_test.go index 69abb2597..35048e7cd 100644 --- a/graph/db/graph_cache_test.go +++ b/graph/db/graph_cache_test.go @@ -88,18 +88,16 @@ func TestGraphCacheAddNode(t *testing.T) { node := &node{ pubKey: nodeA, features: lnwire.EmptyFeatureVector(), - edgeInfos: []*models.ChannelEdgeInfo{{ - ChannelID: 1000, - // Those are direction independent! - NodeKey1Bytes: pubKey1, - NodeKey2Bytes: pubKey2, - Capacity: 500, - }}, - outPolicies: []*models.ChannelEdgePolicy{outPolicy1}, - inPolicies: []*models.ChannelEdgePolicy{inPolicy1}, } cache := NewGraphCache(10) - require.NoError(t, cache.AddNode(nil, node)) + cache.AddNodeFeatures(node) + cache.AddChannel(&models.ChannelEdgeInfo{ + ChannelID: 1000, + // Those are direction independent! + NodeKey1Bytes: pubKey1, + NodeKey2Bytes: pubKey2, + Capacity: 500, + }, outPolicy1, inPolicy1) var fromChannels, toChannels []*DirectedChannel _ = cache.ForEachChannel(nodeA, func(c *DirectedChannel) error {