Skip to content

Commit 90179b6

Browse files
committed
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](369c09b#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](352008a) to be done in 2 separate DB calls for efficiency reasons.
1 parent d10ab03 commit 90179b6

File tree

3 files changed

+9
-31
lines changed

3 files changed

+9
-31
lines changed

graph/db/graph.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,7 @@ func (c *ChannelGraph) AddLightningNode(node *models.LightningNode,
904904
cNode := newGraphCacheNode(
905905
node.PubKeyBytes, node.Features,
906906
)
907-
err := c.graphCache.AddNode(tx, cNode)
908-
if err != nil {
909-
return err
910-
}
907+
c.graphCache.AddNodeFeatures(cNode)
911908
}
912909

913910
return addLightningNode(tx, node)

graph/db/graph_cache.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,6 @@ func (c *GraphCache) AddNodeFeatures(node GraphCacheNode) {
136136
c.mtx.Unlock()
137137
}
138138

139-
// AddNode adds a graph node, including all the (directed) channels of that
140-
// node.
141-
func (c *GraphCache) AddNode(tx kvdb.RTx, node GraphCacheNode) error {
142-
c.AddNodeFeatures(node)
143-
144-
return node.ForEachChannel(
145-
tx, func(tx kvdb.RTx, info *models.ChannelEdgeInfo,
146-
outPolicy *models.ChannelEdgePolicy,
147-
inPolicy *models.ChannelEdgePolicy) error {
148-
149-
c.AddChannel(info, outPolicy, inPolicy)
150-
151-
return nil
152-
},
153-
)
154-
}
155-
156139
// AddChannel adds a non-directed channel, meaning that the order of policy 1
157140
// and policy 2 does not matter, the directionality is extracted from the info
158141
// and policy flags automatically. The policy will be set as the outgoing policy

graph/db/graph_cache_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,16 @@ func TestGraphCacheAddNode(t *testing.T) {
8888
node := &node{
8989
pubKey: nodeA,
9090
features: lnwire.EmptyFeatureVector(),
91-
edgeInfos: []*models.ChannelEdgeInfo{{
92-
ChannelID: 1000,
93-
// Those are direction independent!
94-
NodeKey1Bytes: pubKey1,
95-
NodeKey2Bytes: pubKey2,
96-
Capacity: 500,
97-
}},
98-
outPolicies: []*models.ChannelEdgePolicy{outPolicy1},
99-
inPolicies: []*models.ChannelEdgePolicy{inPolicy1},
10091
}
10192
cache := NewGraphCache(10)
102-
require.NoError(t, cache.AddNode(nil, node))
93+
cache.AddNodeFeatures(node)
94+
cache.AddChannel(&models.ChannelEdgeInfo{
95+
ChannelID: 1000,
96+
// Those are direction independent!
97+
NodeKey1Bytes: pubKey1,
98+
NodeKey2Bytes: pubKey2,
99+
Capacity: 500,
100+
}, outPolicy1, inPolicy1)
103101

104102
var fromChannels, toChannels []*DirectedChannel
105103
_ = cache.ForEachChannel(nodeA, func(c *DirectedChannel) error {

0 commit comments

Comments
 (0)