graph/db: thread context through ForEachNodeDirectedChannel

This commit is contained in:
Elle Mouton committed 2026-02-25 15:33:34 +02:00
1 parent 2f0d2962dd
commit 6ccf93c2c9
10 files changed
+36 -31

No files matched your search

+6 -3
View File
@@ -217,8 +217,9 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error {
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
// //
// NOTE: this is part of the graphdb.NodeTraverser interface. // NOTE: this is part of the graphdb.NodeTraverser interface.
func (c *ChannelGraph) ForEachNodeDirectedChannel(node route.Vertex, func (c *ChannelGraph) ForEachNodeDirectedChannel(ctx context.Context,
cb func(channel *DirectedChannel) error, reset func()) error { node route.Vertex, cb func(channel *DirectedChannel) error,
reset func()) error {
if c.graphCache != nil { if c.graphCache != nil {
return c.graphCache.ForEachChannel(node, cb) return c.graphCache.ForEachChannel(node, cb)
@@ -227,7 +228,9 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(node route.Vertex,
// TODO(elle): once the no-cache path needs to support // TODO(elle): once the no-cache path needs to support
// pathfinding across gossip versions, this should iterate // pathfinding across gossip versions, this should iterate
// across all versions rather than defaulting to v1. // across all versions rather than defaulting to v1.
return c.db.ForEachNodeDirectedChannel(gossipV1, node, cb, reset) return c.db.ForEachNodeDirectedChannel(
ctx, gossipV1, node, cb, reset,
)
} }
// FetchNodeFeatures returns the features of the given node. If no features are // FetchNodeFeatures returns the features of the given node. If no features are
+3 -3
View File
@@ -1875,7 +1875,7 @@ func testGraphTraversalCacheable(t *testing.T, v lnwire.GossipVersion) {
// Query the ChannelGraph which uses the cache to iterate // Query the ChannelGraph which uses the cache to iterate
// through the channels for each node. // through the channels for each node.
err = graph.ChannelGraph.ForEachNodeDirectedChannel( err = graph.ChannelGraph.ForEachNodeDirectedChannel(
node, func(d *DirectedChannel) error { ctx, node, func(d *DirectedChannel) error {
delete(chanIndex, d.ChannelID) delete(chanIndex, d.ChannelID)
return nil return nil
}, func() {}, }, func() {},
@@ -1884,7 +1884,7 @@ func testGraphTraversalCacheable(t *testing.T, v lnwire.GossipVersion) {
// Now skip the cache and query the DB directly. // Now skip the cache and query the DB directly.
err = graph.db.ForEachNodeDirectedChannel( err = graph.db.ForEachNodeDirectedChannel(
v, node, func(d *DirectedChannel) error { ctx, v, node, func(d *DirectedChannel) error {
delete(chanIndex2, d.ChannelID) delete(chanIndex2, d.ChannelID)
return nil return nil
}, func() {}, }, func() {},
@@ -4932,7 +4932,7 @@ func testGraphCacheForEachNodeChannel(t *testing.T,
getSingleChannel := func() *DirectedChannel { getSingleChannel := func() *DirectedChannel {
var ch *DirectedChannel var ch *DirectedChannel
err := graph.db.ForEachNodeDirectedChannel( err := graph.db.ForEachNodeDirectedChannel(
v, node1.PubKeyBytes, ctx, v, node1.PubKeyBytes,
func(c *DirectedChannel) error { func(c *DirectedChannel) error {
require.Nil(t, ch) require.Nil(t, ch)
ch = c ch = c
+4 -3
View File
@@ -21,7 +21,7 @@ import (
type NodeTraverser interface { type NodeTraverser interface {
// ForEachNodeDirectedChannel calls the callback for every channel of // ForEachNodeDirectedChannel calls the callback for every channel of
// the given node. // the given node.
ForEachNodeDirectedChannel(nodePub route.Vertex, ForEachNodeDirectedChannel(ctx context.Context, nodePub route.Vertex,
cb func(channel *DirectedChannel) error, reset func()) error cb func(channel *DirectedChannel) error, reset func()) error
// FetchNodeFeatures returns the features of the given node. // FetchNodeFeatures returns the features of the given node.
@@ -34,8 +34,9 @@ type NodeTraverser interface {
type Store interface { //nolint:interfacebloat type Store interface { //nolint:interfacebloat
// ForEachNodeDirectedChannel calls the callback for every channel of // ForEachNodeDirectedChannel calls the callback for every channel of
// the given node. // the given node.
ForEachNodeDirectedChannel(v lnwire.GossipVersion, nodePub route.Vertex, ForEachNodeDirectedChannel(ctx context.Context, v lnwire.GossipVersion,
cb func(channel *DirectedChannel) error, reset func()) error nodePub route.Vertex, cb func(channel *DirectedChannel) error,
reset func()) error
// FetchNodeFeatures returns the features of the given node. // FetchNodeFeatures returns the features of the given node.
FetchNodeFeatures(ctx context.Context, v lnwire.GossipVersion, FetchNodeFeatures(ctx context.Context, v lnwire.GossipVersion,
+5 -5
View File
@@ -669,9 +669,9 @@ func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx,
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
// //
// NOTE: this is part of the graphdb.NodeTraverser interface. // NOTE: this is part of the graphdb.NodeTraverser interface.
func (c *KVStore) ForEachNodeDirectedChannel(v lnwire.GossipVersion, func (c *KVStore) ForEachNodeDirectedChannel(_ context.Context,
nodePub route.Vertex, cb func(channel *DirectedChannel) error, v lnwire.GossipVersion, nodePub route.Vertex,
reset func()) error { cb func(channel *DirectedChannel) error, reset func()) error {
if v != lnwire.GossipVersion1 { if v != lnwire.GossipVersion1 {
return ErrVersionNotSupportedForKVDB return ErrVersionNotSupportedForKVDB
@@ -4501,8 +4501,8 @@ type nodeTraverserSession struct {
// //
// NOTE: Part of the NodeTraverser interface. // NOTE: Part of the NodeTraverser interface.
func (c *nodeTraverserSession) ForEachNodeDirectedChannel( func (c *nodeTraverserSession) ForEachNodeDirectedChannel(
nodePub route.Vertex, cb func(channel *DirectedChannel) error, _ context.Context, nodePub route.Vertex,
_ func()) error { cb func(channel *DirectedChannel) error, _ func()) error {
return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb, func() {}) return c.db.forEachNodeDirectedChannel(c.tx, nodePub, cb, func() {})
} }
+5 -9
View File
@@ -1006,11 +1006,9 @@ func (s *SQLStore) ForEachNode(ctx context.Context,
// Unknown policies are passed into the callback as nil values. // Unknown policies are passed into the callback as nil values.
// //
// NOTE: this is part of the graphdb.NodeTraverser interface. // NOTE: this is part of the graphdb.NodeTraverser interface.
func (s *SQLStore) ForEachNodeDirectedChannel(v lnwire.GossipVersion, func (s *SQLStore) ForEachNodeDirectedChannel(ctx context.Context,
nodePub route.Vertex, cb func(channel *DirectedChannel) error, v lnwire.GossipVersion, nodePub route.Vertex,
reset func()) error { cb func(channel *DirectedChannel) error, reset func()) error {
var ctx = context.TODO()
return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
return forEachNodeDirectedChannel(ctx, db, v, nodePub, cb) return forEachNodeDirectedChannel(ctx, db, v, nodePub, cb)
@@ -3375,10 +3373,8 @@ func newSQLNodeTraverser(db SQLQueries,
// //
// NOTE: Part of the NodeTraverser interface. // NOTE: Part of the NodeTraverser interface.
func (s *sqlNodeTraverser) ForEachNodeDirectedChannel( func (s *sqlNodeTraverser) ForEachNodeDirectedChannel(
nodePub route.Vertex, cb func(channel *DirectedChannel) error, ctx context.Context, nodePub route.Vertex,
_ func()) error { cb func(channel *DirectedChannel) error, _ func()) error {
ctx := context.TODO()
return forEachNodeDirectedChannel( return forEachNodeDirectedChannel(
ctx, s.db, lnwire.GossipVersion1, nodePub, cb, ctx, s.db, lnwire.GossipVersion1, nodePub, cb,
+3 -1
View File
@@ -1,6 +1,7 @@
package routing package routing
import ( import (
"context"
"fmt" "fmt"
"github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/fn/v2"
@@ -64,7 +65,8 @@ func newBandwidthManager(graph Graph, sourceNode route.Vertex,
// First, we'll collect the set of outbound edges from the target // First, we'll collect the set of outbound edges from the target
// source node and add them to our bandwidth manager's map of channels. // source node and add them to our bandwidth manager's map of channels.
err := graph.ForEachNodeDirectedChannel( err := graph.ForEachNodeDirectedChannel(
sourceNode, func(channel *graphdb.DirectedChannel) error { context.TODO(), sourceNode,
func(channel *graphdb.DirectedChannel) error {
shortID := lnwire.NewShortChanIDFromInt( shortID := lnwire.NewShortChanIDFromInt(
channel.ChannelID, channel.ChannelID,
) )
+1 -1
View File
@@ -15,7 +15,7 @@ import (
type Graph interface { type Graph interface {
// ForEachNodeDirectedChannel calls the callback for every channel of // ForEachNodeDirectedChannel calls the callback for every channel of
// the given node. // the given node.
ForEachNodeDirectedChannel(nodePub route.Vertex, ForEachNodeDirectedChannel(ctx context.Context, nodePub route.Vertex,
cb func(channel *graphdb.DirectedChannel) error, cb func(channel *graphdb.DirectedChannel) error,
reset func()) error reset func()) error
+3 -2
View File
@@ -166,8 +166,9 @@ func (m *mockGraph) addChannel(id uint64, node1id, node2id byte,
// forEachNodeChannel calls the callback for every channel of the given node. // forEachNodeChannel calls the callback for every channel of the given node.
// //
// NOTE: Part of the Graph interface. // NOTE: Part of the Graph interface.
func (m *mockGraph) ForEachNodeDirectedChannel(nodePub route.Vertex, func (m *mockGraph) ForEachNodeDirectedChannel(_ context.Context,
cb func(channel *graphdb.DirectedChannel) error, _ func()) error { nodePub route.Vertex, cb func(channel *graphdb.DirectedChannel) error,
_ func()) error {
// Look up the mock node. // Look up the mock node.
node, ok := m.nodes[nodePub] node, ok := m.nodes[nodePub]
+4 -3
View File
@@ -578,7 +578,7 @@ func getOutgoingBalance(node route.Vertex, outgoingChans map[uint64]struct{},
// Iterate over all channels of the to node. // Iterate over all channels of the to node.
err := g.ForEachNodeDirectedChannel( err := g.ForEachNodeDirectedChannel(
node, cb, func() { context.TODO(), node, cb, func() {
max = 0 max = 0
total = 0 total = 0
}, },
@@ -1291,7 +1291,7 @@ func findBlindedPaths(g Graph, target route.Vertex,
nextTargetReset = nextTarget nextTargetReset = nextTarget
) )
err := g.ForEachNodeDirectedChannel( err := g.ForEachNodeDirectedChannel(
nextTarget, context.TODO(), nextTarget,
func(channel *graphdb.DirectedChannel) error { func(channel *graphdb.DirectedChannel) error {
// This is not the right channel, continue to // This is not the right channel, continue to
// the node's other channels. // the node's other channels.
@@ -1466,7 +1466,8 @@ func processNodeForBlindedPath(g Graph, node route.Vertex,
// Now, iterate over the node's channels in search for paths to this // Now, iterate over the node's channels in search for paths to this
// node that can be used for blinded paths // node that can be used for blinded paths
err = g.ForEachNodeDirectedChannel( err = g.ForEachNodeDirectedChannel(
node, func(channel *graphdb.DirectedChannel) error { context.TODO(), node,
func(channel *graphdb.DirectedChannel) error {
// Keep track of how many incoming channels this node // Keep track of how many incoming channels this node
// has. We only use a node as an introduction node if it // has. We only use a node as an introduction node if it
// has channels other than the one that lead us to it. // has channels other than the one that lead us to it.
+2 -1
View File
@@ -1,6 +1,7 @@
package routing package routing
import ( import (
"context"
"math" "math"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
@@ -116,7 +117,7 @@ func (u *nodeEdgeUnifier) addGraphPolicies(g Graph) error {
// Iterate over all channels of the to node. // Iterate over all channels of the to node.
err := g.ForEachNodeDirectedChannel( err := g.ForEachNodeDirectedChannel(
u.toNode, cb, func() { context.TODO(), u.toNode, cb, func() {
channels = nil channels = nil
}, },
) )