rpc: optimize GetNetworkInfo by using the channel graph cache

This commit is contained in:
Olaoluwa Osuntokun 2021-10-19 16:04:51 -07:00
parent fae470293f
commit 79406696ad
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306

View File

@ -5708,7 +5708,9 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
// network, tallying up the total number of nodes, and also gathering
// each node so we can measure the graph diameter and degree stats
// below.
if err := graph.ForEachNode(func(tx kvdb.RTx, node *channeldb.LightningNode) error {
err := graph.ForEachNodeCached(func(node route.Vertex,
edges map[uint64]*channeldb.DirectedChannel) error {
// Increment the total number of nodes with each iteration.
numNodes++
@ -5718,9 +5720,7 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
// through the db transaction from the outer view so we can
// re-use it within this inner view.
var outDegree uint32
if err := node.ForEachChannel(tx, func(_ kvdb.RTx,
edge *channeldb.ChannelEdgeInfo, _, _ *channeldb.ChannelEdgePolicy) error {
for _, edge := range edges {
// Bump up the out degree for this node for each
// channel encountered.
outDegree++
@ -5751,9 +5751,6 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
seenChans[edge.ChannelID] = struct{}{}
allChans = append(allChans, edge.Capacity)
return nil
}); err != nil {
return err
}
// Finally, if the out degree of this node is greater than what
@ -5763,7 +5760,8 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
}
return nil
}); err != nil {
})
if err != nil {
return nil, err
}