multi: let ForEachNodeChannel take a reset param

This commit is contained in:
Elle Mouton committed 2025-07-15 11:23:27 +02:00
1 parent 16126d7703
commit e5fbca8299
13 files changed
+57 -43

No files matched your search

+2 -1
View File
@@ -736,7 +736,8 @@ func (t *testNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
policy2 *models.ChannelEdgePolicy) error { policy2 *models.ChannelEdgePolicy) error {
return f(edge, policy1, policy2) return f(edge, policy1, policy2)
}) }, func() {},
)
} }
func (t *testNodeTx) FetchNode(pub route.Vertex) (graphdb.NodeRTx, error) { func (t *testNodeTx) FetchNode(pub route.Vertex) (graphdb.NodeRTx, error) {
+3
View File
@@ -1758,6 +1758,9 @@ func (d *AuthenticatedGossiper) retransmitStaleAnns(ctx context.Context,
} }
return nil return nil
}, func() {
havePublicChannels = false
edgesToUpdate = nil
}) })
if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) { if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
return fmt.Errorf("unable to retrieve outgoing channels: %w", return fmt.Errorf("unable to retrieve outgoing channels: %w",
+3 -5
View File
@@ -213,7 +213,7 @@ func (r *mockGraphSource) ForEachNode(
func (r *mockGraphSource) ForAllOutgoingChannels(_ context.Context, func (r *mockGraphSource) ForAllOutgoingChannels(_ context.Context,
cb func(i *models.ChannelEdgeInfo, cb func(i *models.ChannelEdgeInfo,
c *models.ChannelEdgePolicy) error) error { c *models.ChannelEdgePolicy) error, _ func()) error {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
@@ -3728,10 +3728,8 @@ out:
}) })
return nil return nil
}) }, func() {})
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
err = ctx.gossiper.PropagateChanPolicyUpdate(edgesToUpdate) err = ctx.gossiper.PropagateChanPolicyUpdate(edgesToUpdate)
require.NoError(t, err, "unable to chan policies") require.NoError(t, err, "unable to chan policies")
+5 -4
View File
@@ -1276,10 +1276,11 @@ func (b *Builder) FetchLightningNode(ctx context.Context,
// //
// NOTE: This method is part of the ChannelGraphSource interface. // NOTE: This method is part of the ChannelGraphSource interface.
func (b *Builder) ForAllOutgoingChannels(ctx context.Context, func (b *Builder) ForAllOutgoingChannels(ctx context.Context,
cb func(*models.ChannelEdgeInfo, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy) error,
*models.ChannelEdgePolicy) error) error { reset func()) error {
return b.cfg.Graph.ForEachNodeChannel(ctx, b.cfg.SelfNode, return b.cfg.Graph.ForEachNodeChannel(
ctx, b.cfg.SelfNode,
func(c *models.ChannelEdgeInfo, e *models.ChannelEdgePolicy, func(c *models.ChannelEdgeInfo, e *models.ChannelEdgePolicy,
_ *models.ChannelEdgePolicy) error { _ *models.ChannelEdgePolicy) error {
@@ -1289,7 +1290,7 @@ func (b *Builder) ForAllOutgoingChannels(ctx context.Context,
} }
return cb(c, e) return cb(c, e)
}, }, reset,
) )
} }
+11 -12
View File
@@ -1394,7 +1394,8 @@ func TestGraphTraversal(t *testing.T) {
// outgoing channels for a particular node. // outgoing channels for a particular node.
numNodeChans := 0 numNodeChans := 0
firstNode, secondNode := nodeList[0], nodeList[1] firstNode, secondNode := nodeList[0], nodeList[1]
err = graph.ForEachNodeChannel(ctx, firstNode.PubKeyBytes, err = graph.ForEachNodeChannel(
ctx, firstNode.PubKeyBytes,
func(_ *models.ChannelEdgeInfo, outEdge, func(_ *models.ChannelEdgeInfo, outEdge,
inEdge *models.ChannelEdgePolicy) error { inEdge *models.ChannelEdgePolicy) error {
@@ -1425,7 +1426,8 @@ func TestGraphTraversal(t *testing.T) {
numNodeChans++ numNodeChans++
return nil return nil
}) }, func() {},
)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, numChannels, numNodeChans) require.Equal(t, numChannels, numNodeChans)
} }
@@ -3144,7 +3146,8 @@ func TestIncompleteChannelPolicies(t *testing.T) {
expectedOut bool) { expectedOut bool) {
calls := 0 calls := 0
err := graph.ForEachNodeChannel(ctx, node.PubKeyBytes, err := graph.ForEachNodeChannel(
ctx, node.PubKeyBytes,
func(_ *models.ChannelEdgeInfo, outEdge, func(_ *models.ChannelEdgeInfo, outEdge,
inEdge *models.ChannelEdgePolicy) error { inEdge *models.ChannelEdgePolicy) error {
@@ -3167,14 +3170,10 @@ func TestIncompleteChannelPolicies(t *testing.T) {
calls++ calls++
return nil return nil
}) }, func() {},
if err != nil { )
t.Fatalf("unable to scan channels: %v", err) require.NoError(t, err)
} require.Equal(t, 1, calls)
if calls != 1 {
t.Fatalf("Expected only one callback call")
}
} }
checkPolicies(node2, false, false) checkPolicies(node2, false, false)
@@ -4248,7 +4247,7 @@ func BenchmarkForEachChannel(b *testing.B) {
return nil return nil
} }
err := graph.ForEachNodeChannel(ctx, n, cb) err := graph.ForEachNodeChannel(ctx, n, cb, func() {})
require.NoError(b, err) require.NoError(b, err)
} }
} }
+1 -1
View File
@@ -86,7 +86,7 @@ type V1Store interface { //nolint:interfacebloat
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
ForEachNodeChannel(ctx context.Context, nodePub route.Vertex, ForEachNodeChannel(ctx context.Context, nodePub route.Vertex,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error *models.ChannelEdgePolicy) error, reset func()) error
// ForEachNodeCached is similar to forEachNode, but it returns // ForEachNodeCached is similar to forEachNode, but it returns
// DirectedChannel data to the call-back. // DirectedChannel data to the call-back.
+6 -8
View File
@@ -406,7 +406,7 @@ func (c *KVStore) AddrsForNode(ctx context.Context,
// callback. // callback.
func (c *KVStore) ForEachChannel(_ context.Context, func (c *KVStore) ForEachChannel(_ context.Context,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error, reset func()) error { *models.ChannelEdgePolicy) error, reset func()) error {
return forEachChannel(c.db, cb, reset) return forEachChannel(c.db, cb, reset)
} }
@@ -679,7 +679,7 @@ func (c *KVStore) FetchNodeFeatures(nodePub route.Vertex) (
// NOTE: The callback contents MUST not be modified. // NOTE: The callback contents MUST not be modified.
func (c *KVStore) ForEachNodeCached(_ context.Context, func (c *KVStore) ForEachNodeCached(_ context.Context,
cb func(node route.Vertex, cb func(node route.Vertex,
chans map[uint64]*DirectedChannel) error) error { chans map[uint64]*DirectedChannel) error) error {
reset := func() {} reset := func() {}
@@ -3182,7 +3182,7 @@ func (c *KVStore) HasLightningNode(_ context.Context,
// function's View/Update call which will then apply to the whole transaction. // function's View/Update call which will then apply to the whole transaction.
func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend, func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error, reset func()) error { *models.ChannelEdgePolicy) error, reset func()) error {
traversal := func(tx kvdb.RTx) error { traversal := func(tx kvdb.RTx) error {
edges := tx.ReadBucket(edgeBucket) edges := tx.ReadBucket(edgeBucket)
@@ -3271,9 +3271,7 @@ func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
func (c *KVStore) ForEachNodeChannel(_ context.Context, nodePub route.Vertex, func (c *KVStore) ForEachNodeChannel(_ context.Context, nodePub route.Vertex,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error { *models.ChannelEdgePolicy) error, reset func()) error {
reset := func() {}
return nodeTraversal( return nodeTraversal(
nil, nodePub[:], c.db, func(_ kvdb.RTx, nil, nodePub[:], c.db, func(_ kvdb.RTx,
@@ -3291,7 +3289,7 @@ func (c *KVStore) ForEachNodeChannel(_ context.Context, nodePub route.Vertex,
// peer's node information. // peer's node information.
func (c *KVStore) ForEachSourceNodeChannel(_ context.Context, func (c *KVStore) ForEachSourceNodeChannel(_ context.Context,
cb func(chanPoint wire.OutPoint, havePolicy bool, cb func(chanPoint wire.OutPoint, havePolicy bool,
otherNode *models.LightningNode) error, reset func()) error { otherNode *models.LightningNode) error, reset func()) error {
return kvdb.View(c.db, func(tx kvdb.RTx) error { return kvdb.View(c.db, func(tx kvdb.RTx) error {
nodes := tx.ReadBucket(nodeBucket) nodes := tx.ReadBucket(nodeBucket)
@@ -3341,7 +3339,7 @@ func (c *KVStore) ForEachSourceNodeChannel(_ context.Context,
// NOTE: the reset function is only meaningful if the tx param is nil. // NOTE: the reset function is only meaningful if the tx param is nil.
func (c *KVStore) forEachNodeChannelTx(tx kvdb.RTx, func (c *KVStore) forEachNodeChannelTx(tx kvdb.RTx,
nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo, nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error, *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error,
reset func()) error { reset func()) error {
return nodeTraversal(tx, nodePub[:], c.db, cb, reset) return nodeTraversal(tx, nodePub[:], c.db, cb, reset)
+1 -3
View File
@@ -962,9 +962,7 @@ func (s *SQLStore) ForEachNodeCacheable(ctx context.Context,
// NOTE: part of the V1Store interface. // NOTE: part of the V1Store interface.
func (s *SQLStore) ForEachNodeChannel(ctx context.Context, nodePub route.Vertex, func (s *SQLStore) ForEachNodeChannel(ctx context.Context, nodePub route.Vertex,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error { *models.ChannelEdgePolicy) error, reset func()) error {
reset := func() {}
return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
dbNode, err := db.GetNodeByPubKey( dbNode, err := db.GetNodeByPubKey(
+2 -2
View File
@@ -72,7 +72,7 @@ type ChannelGraphSource interface {
// star-graph. // star-graph.
ForAllOutgoingChannels(ctx context.Context, ForAllOutgoingChannels(ctx context.Context,
cb func(c *models.ChannelEdgeInfo, cb func(c *models.ChannelEdgeInfo,
e *models.ChannelEdgePolicy) error) error e *models.ChannelEdgePolicy) error, reset func()) error
// CurrentBlockHeight returns the block height from POV of the router // CurrentBlockHeight returns the block height from POV of the router
// subsystem. // subsystem.
@@ -260,7 +260,7 @@ type DB interface {
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
ForEachNodeChannel(ctx context.Context, nodePub route.Vertex, ForEachNodeChannel(ctx context.Context, nodePub route.Vertex,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error *models.ChannelEdgePolicy) error, reset func()) error
// AddEdgeProof sets the proof of an existing edge in the graph // AddEdgeProof sets the proof of an existing edge in the graph
// database. // database.
+9 -2
View File
@@ -42,7 +42,7 @@ type Manager struct {
// channels. The ChannelEdgePolicy parameter may be nil. // channels. The ChannelEdgePolicy parameter may be nil.
ForAllOutgoingChannels func(ctx context.Context, ForAllOutgoingChannels func(ctx context.Context,
cb func(*models.ChannelEdgeInfo, cb func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy) error) error *models.ChannelEdgePolicy) error, reset func()) error
// FetchChannel is used to query local channel parameters. Optionally an // FetchChannel is used to query local channel parameters. Optionally an
// existing db tx can be supplied. // existing db tx can be supplied.
@@ -152,7 +152,14 @@ func (r *Manager) UpdatePolicy(ctx context.Context,
// Next, we'll loop over all the outgoing channels the router knows of. // Next, we'll loop over all the outgoing channels the router knows of.
// If we have a filter then we'll only collect those channels, otherwise // If we have a filter then we'll only collect those channels, otherwise
// we'll collect them all. // we'll collect them all.
err := r.ForAllOutgoingChannels(ctx, processChan) err := r.ForAllOutgoingChannels(
ctx, processChan,
func() {
failedUpdates = nil
edgesToUpdate = nil
clear(policiesToUpdate)
},
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+1 -1
View File
@@ -125,7 +125,7 @@ func TestManager(t *testing.T) {
forAllOutgoingChannels := func(_ context.Context, forAllOutgoingChannels := func(_ context.Context,
cb func(*models.ChannelEdgeInfo, cb func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy) error) error { *models.ChannelEdgePolicy) error, _ func()) error {
for _, c := range channelSet { for _, c := range channelSet {
if err := cb(c.edgeInfo, &currentPolicy); err != nil { if err := cb(c.edgeInfo, &currentPolicy); err != nil {
+10 -2
View File
@@ -7048,7 +7048,8 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
channels []*lnrpc.ChannelEdge channels []*lnrpc.ChannelEdge
) )
err = graph.ForEachNodeChannel(ctx, node.PubKeyBytes, err = graph.ForEachNodeChannel(
ctx, node.PubKeyBytes,
func(edge *models.ChannelEdgeInfo, func(edge *models.ChannelEdgeInfo,
c1, c2 *models.ChannelEdgePolicy) error { c1, c2 *models.ChannelEdgePolicy) error {
@@ -7074,6 +7075,10 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
} }
return nil return nil
}, func() {
numChannels = 0
totalCapacity = 0
channels = nil
}, },
) )
if err != nil { if err != nil {
@@ -7730,7 +7735,8 @@ func (r *rpcServer) FeeReport(ctx context.Context,
} }
var feeReports []*lnrpc.ChannelFeeReport var feeReports []*lnrpc.ChannelFeeReport
err = channelGraph.ForEachNodeChannel(ctx, selfNode.PubKeyBytes, err = channelGraph.ForEachNodeChannel(
ctx, selfNode.PubKeyBytes,
func(chanInfo *models.ChannelEdgeInfo, func(chanInfo *models.ChannelEdgeInfo,
edgePolicy, _ *models.ChannelEdgePolicy) error { edgePolicy, _ *models.ChannelEdgePolicy) error {
@@ -7768,6 +7774,8 @@ func (r *rpcServer) FeeReport(ctx context.Context,
}) })
return nil return nil
}, func() {
feeReports = nil
}, },
) )
if err != nil { if err != nil {
+3 -2
View File
@@ -1254,7 +1254,8 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
DefaultRoutingPolicy: cc.RoutingPolicy, DefaultRoutingPolicy: cc.RoutingPolicy,
ForAllOutgoingChannels: func(ctx context.Context, ForAllOutgoingChannels: func(ctx context.Context,
cb func(*models.ChannelEdgeInfo, cb func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy) error) error { *models.ChannelEdgePolicy) error,
reset func()) error {
return s.graphDB.ForEachNodeChannel(ctx, selfVertex, return s.graphDB.ForEachNodeChannel(ctx, selfVertex,
func(c *models.ChannelEdgeInfo, func(c *models.ChannelEdgeInfo,
@@ -1264,7 +1265,7 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
// NOTE: The invoked callback here may // NOTE: The invoked callback here may
// receive a nil channel policy. // receive a nil channel policy.
return cb(c, e) return cb(c, e)
}, }, reset,
) )
}, },
PropagateChanPolicyUpdate: s.authGossiper.PropagateChanPolicyUpdate, PropagateChanPolicyUpdate: s.authGossiper.PropagateChanPolicyUpdate,