graph/db: thread context through FetchChannelEdgesByOutpoint
What changed, and why it matters
This change is a routine code cleanup: it threads a request-scoped cancellation context through a database lookup function called FetchChannelEdgesByOutpoint. It does not fix a security bug, change behavior, or introduce any new logic. The only effect is that callers can now pass a context, and the SQL backend can use that context instead of a placeholder. The KV backend ignores the context for now.
No security action required. Treat as normal refactoring/technical-debt cleanup.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors FetchChannelEdgesByOutpoint to accept a context.Context parameter through the call chain: Store interface, KVStore, SQLStore, ChannelGraph, VersionedGraph, and callers in netann/chan_status_manager.go, peer/brontide.go, and rpcserver.go. SQLStore replaces an internal context.TODO() with the supplied context; KVStore accepts but ignores the context (named _). Tests and mocks are updated accordingly. No functional, cryptographic, or authorization changes are present.
Changed components
graph/db/graph.gograph/db/interfaces.gograph/db/kv_store.gograph/db/sql_store.gonetann/chan_status_manager.gopeer/brontide.gorpcserver.goInspect captured patch +33 / −19
diff --git a/graph/db/graph.go b/graph/db/graph.go
index a192ae3..85e8785 100644
--- a/graph/db/graph.go
+++ b/graph/db/graph.go
@@ -729,12 +729,13 @@ func (c *ChannelGraph) FetchChanInfos(v lnwire.GossipVersion,
// FetchChannelEdgesByOutpoint attempts to lookup directed edges by funding
// outpoint.
-func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
+func (c *ChannelGraph) FetchChannelEdgesByOutpoint(ctx context.Context,
+ op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
return c.db.FetchChannelEdgesByOutpoint(
- lnwire.GossipVersion1, op,
+ ctx, lnwire.GossipVersion1, op,
)
}
@@ -823,11 +824,12 @@ func (c *VersionedGraph) FetchChannelEdgesByID(ctx context.Context,
// FetchChannelEdgesByOutpoint attempts to lookup directed edges by funding
// outpoint.
-func (c *VersionedGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
+func (c *VersionedGraph) FetchChannelEdgesByOutpoint(ctx context.Context,
+ op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
- return c.db.FetchChannelEdgesByOutpoint(c.v, op)
+ return c.db.FetchChannelEdgesByOutpoint(ctx, c.v, op)
}
// IsZombieEdge returns whether the edge is considered zombie for this version.
diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go
index b201e96..088b3dc 100644
--- a/graph/db/graph_test.go
+++ b/graph/db/graph_test.go
@@ -693,7 +693,9 @@ func testEdgeInsertionDeletion(t *testing.T, v lnwire.GossipVersion) {
}
// Also verify fetching by outpoint returns the same data.
- dbEdge2, _, _, err := graph.FetchChannelEdgesByOutpoint(&outpoint)
+ dbEdge2, _, _, err := graph.FetchChannelEdgesByOutpoint(
+ ctx, &outpoint,
+ )
require.NoError(t, err)
require.Equal(t, dbEdge.ChannelID, dbEdge2.ChannelID)
@@ -706,7 +708,7 @@ func testEdgeInsertionDeletion(t *testing.T, v lnwire.GossipVersion) {
// Ensure that any query attempts to lookup the delete channel edge are
// properly deleted.
- _, _, _, err = graph.FetchChannelEdgesByOutpoint(&outpoint)
+ _, _, _, err = graph.FetchChannelEdgesByOutpoint(ctx, &outpoint)
require.ErrorIs(t, err, ErrEdgeNotFound)
// Assert that if the edge is a zombie, then FetchChannelEdgesByID
@@ -1206,7 +1208,7 @@ func testEdgeInfoUpdates(t *testing.T, v lnwire.GossipVersion) {
// Next, attempt to query the channel edges according to the outpoint
// of the channel.
dbEdgeInfo, dbEdge1, dbEdge2, err = graph.FetchChannelEdgesByOutpoint(
- &outpoint,
+ ctx, &outpoint,
)
require.NoError(t, err, "unable to fetch channel by ID")
compareEdgePolicies(t, dbEdge1, edge1)
diff --git a/graph/db/interfaces.go b/graph/db/interfaces.go
index 550c78a..8db7e0e 100644
--- a/graph/db/interfaces.go
+++ b/graph/db/interfaces.go
@@ -296,7 +296,8 @@ type Store interface { //nolint:interfacebloat
// houses the general information for the channel itself is returned as
// well as two structs that contain the routing policies for the channel
// in either direction.
- FetchChannelEdgesByOutpoint(v lnwire.GossipVersion, op *wire.OutPoint) (
+ FetchChannelEdgesByOutpoint(ctx context.Context,
+ v lnwire.GossipVersion, op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error)
diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go
index f77484a..6fbd7df 100644
--- a/graph/db/kv_store.go
+++ b/graph/db/kv_store.go
@@ -3899,8 +3899,9 @@ func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
// found, then ErrEdgeNotFound is returned. A struct which houses the general
// information for the channel itself is returned as well as two structs that
// contain the routing policies for the channel in either direction.
-func (c *KVStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
- op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+func (c *KVStore) FetchChannelEdgesByOutpoint(_ context.Context,
+ v lnwire.GossipVersion, op *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
var (
diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go
index 9085e06..71f9d08 100644
--- a/graph/db/sql_store.go
+++ b/graph/db/sql_store.go
@@ -2167,12 +2167,12 @@ func (s *SQLStore) FetchChannelEdgesByID(ctx context.Context,
// contain the routing policies for the channel in either direction.
//
// NOTE: part of the Store interface.
-func (s *SQLStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
- op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+func (s *SQLStore) FetchChannelEdgesByOutpoint(ctx context.Context,
+ v lnwire.GossipVersion, op *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
var (
- ctx = context.TODO()
edge *models.ChannelEdgeInfo
policy1, policy2 *models.ChannelEdgePolicy
)
diff --git a/netann/chan_status_manager.go b/netann/chan_status_manager.go
index feb3a5d..b21aeb1 100644
--- a/netann/chan_status_manager.go
+++ b/netann/chan_status_manager.go
@@ -1,6 +1,7 @@
package netann
import (
+ "context"
"errors"
"sync"
"time"
@@ -654,7 +655,9 @@ func (m *ChanStatusManager) fetchLastChanUpdateByOutPoint(op wire.OutPoint) (
*lnwire.ChannelUpdate1, bool, error) {
// Get the edge info and policies for this channel from the graph.
- info, edge1, edge2, err := m.cfg.Graph.FetchChannelEdgesByOutpoint(&op)
+ info, edge1, edge2, err := m.cfg.Graph.FetchChannelEdgesByOutpoint(
+ context.TODO(), &op,
+ )
if err != nil {
return nil, false, err
}
diff --git a/netann/chan_status_manager_test.go b/netann/chan_status_manager_test.go
index 59ce324..02669ce 100644
--- a/netann/chan_status_manager_test.go
+++ b/netann/chan_status_manager_test.go
@@ -2,6 +2,7 @@ package netann_test
import (
"bytes"
+ "context"
"crypto/rand"
"encoding/binary"
"fmt"
@@ -173,7 +174,8 @@ func (g *mockGraph) FetchAllOpenChannels() ([]*channeldb.OpenChannel, error) {
}
func (g *mockGraph) FetchChannelEdgesByOutpoint(
- op *wire.OutPoint) (*models.ChannelEdgeInfo,
+ _ context.Context, op *wire.OutPoint) (
+ *models.ChannelEdgeInfo,
*models.ChannelEdgePolicy, *models.ChannelEdgePolicy, error) {
g.mu.Lock()
diff --git a/netann/interface.go b/netann/interface.go
index aa55943..78acc24 100644
--- a/netann/interface.go
+++ b/netann/interface.go
@@ -1,6 +1,8 @@
package netann
import (
+ "context"
+
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/graph/db/models"
@@ -19,6 +21,7 @@ type DB interface {
type ChannelGraph interface {
// FetchChannelEdgesByOutpoint returns the channel edge info and most
// recent channel edge policies for a given outpoint.
- FetchChannelEdgesByOutpoint(*wire.OutPoint) (*models.ChannelEdgeInfo,
- *models.ChannelEdgePolicy, *models.ChannelEdgePolicy, error)
+ FetchChannelEdgesByOutpoint(context.Context, *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error)
}
diff --git a/peer/brontide.go b/peer/brontide.go
index d92f9c4..0f4d6e3 100644
--- a/peer/brontide.go
+++ b/peer/brontide.go
@@ -1217,7 +1217,7 @@ func (p *Brontide) loadActiveChannels(chans []*channeldb.OpenChannel) (
// the database.
graph := p.cfg.ChannelGraph
info, p1, p2, err := graph.FetchChannelEdgesByOutpoint(
- &chanPoint,
+ context.TODO(), &chanPoint,
)
if err != nil && !errors.Is(err, graphdb.ErrEdgeNotFound) {
return nil, err
diff --git a/rpcserver.go b/rpcserver.go
index 4a9c8e7..bf01fc8 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -7132,7 +7132,7 @@ func (r *rpcServer) GetChanInfo(ctx context.Context,
return nil, err
}
edgeInfo, edge1, edge2, err = graph.FetchChannelEdgesByOutpoint(
- chanPoint,
+ ctx, chanPoint,
)
default:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.