autopilot: remove the ForEachChannel method from Node interface

And instead make use of the new ForEachNodesChannels method which
uses a much more efficient method for iterating through nodes&channels.
This commit is contained in:
Elle Mouton
2025-08-03 17:22:56 +02:00
parent ce7fe84da7
commit 699e335954
5 changed files with 82 additions and 168 deletions

View File

@@ -89,26 +89,26 @@ func (p *PrefAttachment) NodeScores(ctx context.Context, g ChannelGraph,
allChans []btcutil.Amount
seenChans = make(map[uint64]struct{})
)
if err := g.ForEachNode(ctx, func(ctx context.Context, n Node) error {
err := n.ForEachChannel(ctx, func(_ context.Context,
e ChannelEdge) error {
err := g.ForEachNodesChannels(
ctx, func(_ context.Context, node Node,
channels []*ChannelEdge) error {
if _, ok := seenChans[e.ChanID.ToUint64()]; ok {
return nil
for _, e := range channels {
if _, ok := seenChans[e.ChanID.ToUint64()]; ok {
continue
}
seenChans[e.ChanID.ToUint64()] = struct{}{}
allChans = append(allChans, e.Capacity)
}
seenChans[e.ChanID.ToUint64()] = struct{}{}
allChans = append(allChans, e.Capacity)
return nil
})
if err != nil {
return err
}
return nil
}, func() {
allChans = nil
clear(seenChans)
}); err != nil {
return nil
},
func() {
allChans = nil
clear(seenChans)
},
)
if err != nil {
return nil, err
}
@@ -120,55 +120,59 @@ func (p *PrefAttachment) NodeScores(ctx context.Context, g ChannelGraph,
// the graph.
var maxChans int
nodeChanNum := make(map[NodeID]int)
if err := g.ForEachNode(ctx, func(ctx context.Context, n Node) error {
var nodeChans int
err := n.ForEachChannel(ctx, func(_ context.Context,
e ChannelEdge) error {
err = g.ForEachNodesChannels(
ctx, func(ctx context.Context, node Node,
edges []*ChannelEdge) error {
// Since connecting to nodes with a lot of small
// channels actually worsens our connectivity in the
// graph (we will potentially waste time trying to use
// these useless channels in path finding), we decrease
// the counter for such channels.
if e.Capacity <
medianChanSize/minMedianChanSizeFraction {
var nodeChans int
for _, e := range edges {
// Since connecting to nodes with a lot of small
// channels actually worsens our connectivity in
// the graph (we will potentially waste time
// trying to use these useless channels in path
// finding), we decrease the counter for such
// channels.
//
//nolint:ll
if e.Capacity <
medianChanSize/minMedianChanSizeFraction {
nodeChans--
nodeChans--
continue
}
// Larger channels we count.
nodeChans++
}
// We keep track of the highest-degree node we've seen,
// as this will be given the max score.
if nodeChans > maxChans {
maxChans = nodeChans
}
// If this node is not among our nodes to score, we can
// return early.
nID := NodeID(node.PubKey())
if _, ok := nodes[nID]; !ok {
log.Tracef("Node %x not among nodes to score, "+
"ignoring", nID[:])
return nil
}
// Larger channels we count.
nodeChans++
// Otherwise we'll record the number of channels.
nodeChanNum[nID] = nodeChans
log.Tracef("Counted %v channels for node %x", nodeChans,
nID[:])
return nil
})
if err != nil {
return err
}
// We keep track of the highest-degree node we've seen, as this
// will be given the max score.
if nodeChans > maxChans {
maxChans = nodeChans
}
// If this node is not among our nodes to score, we can return
// early.
nID := NodeID(n.PubKey())
if _, ok := nodes[nID]; !ok {
log.Tracef("Node %x not among nodes to score, "+
"ignoring", nID[:])
return nil
}
// Otherwise we'll record the number of channels.
nodeChanNum[nID] = nodeChans
log.Tracef("Counted %v channels for node %x", nodeChans, nID[:])
return nil
}, func() {
maxChans = 0
clear(nodeChanNum)
}); err != nil {
}, func() {
maxChans = 0
clear(nodeChanNum)
},
)
if err != nil {
return nil, err
}