Skip to content

Commit 66c5a97

Browse files
committed
graph/db: thread context through to FetchLightingNode
1 parent d1cfb47 commit 66c5a97

File tree

12 files changed

+47
-39
lines changed

12 files changed

+47
-39
lines changed

autopilot/prefattach_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ func TestPrefAttachmentSelectSkipNodes(t *testing.T) {
395395
func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
396396
capacity btcutil.Amount) (*ChannelEdge, *ChannelEdge, error) {
397397

398+
ctx := context.Background()
399+
398400
fetchNode := func(pub *btcec.PublicKey) (*models.LightningNode, error) {
399401
if pub != nil {
400402
vertex, err := route.NewVertexFromBytes(
@@ -404,7 +406,7 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
404406
return nil, err
405407
}
406408

407-
dbNode, err := d.db.FetchLightningNode(vertex)
409+
dbNode, err := d.db.FetchLightningNode(ctx, vertex)
408410
switch {
409411
case errors.Is(err, graphdb.ErrGraphNodeNotFound):
410412
fallthrough
@@ -737,7 +739,7 @@ func (t *testNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
737739
}
738740

739741
func (t *testNodeTx) FetchNode(pub route.Vertex) (graphdb.NodeRTx, error) {
740-
node, err := t.db.db.FetchLightningNode(pub)
742+
node, err := t.db.db.FetchLightningNode(context.Background(), pub)
741743
if err != nil {
742744
return nil, err
743745
}

discovery/gossiper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,10 +2206,10 @@ func (d *AuthenticatedGossiper) processZombieUpdate(_ context.Context,
22062206

22072207
// fetchNodeAnn fetches the latest signed node announcement from our point of
22082208
// view for the node with the given public key.
2209-
func (d *AuthenticatedGossiper) fetchNodeAnn(_ context.Context,
2209+
func (d *AuthenticatedGossiper) fetchNodeAnn(ctx context.Context,
22102210
pubKey [33]byte) (*lnwire.NodeAnnouncement, error) {
22112211

2212-
node, err := d.cfg.Graph.FetchLightningNode(pubKey)
2212+
node, err := d.cfg.Graph.FetchLightningNode(ctx, pubKey)
22132213
if err != nil {
22142214
return nil, err
22152215
}

discovery/gossiper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (r *mockGraphSource) GetChannelByID(chanID lnwire.ShortChannelID) (
294294
return &chanInfo, edge1, edge2, nil
295295
}
296296

297-
func (r *mockGraphSource) FetchLightningNode(
297+
func (r *mockGraphSource) FetchLightningNode(_ context.Context,
298298
nodePub route.Vertex) (*models.LightningNode, error) {
299299

300300
for _, node := range r.nodes {

graph/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,10 +1265,10 @@ func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
12651265
// within the graph.
12661266
//
12671267
// NOTE: This method is part of the ChannelGraphSource interface.
1268-
func (b *Builder) FetchLightningNode(
1268+
func (b *Builder) FetchLightningNode(ctx context.Context,
12691269
node route.Vertex) (*models.LightningNode, error) {
12701270

1271-
return b.cfg.Graph.FetchLightningNode(node)
1271+
return b.cfg.Graph.FetchLightningNode(ctx, node)
12721272
}
12731273

12741274
// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by

graph/db/graph_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
132132

133133
// Next, fetch the node from the database to ensure everything was
134134
// serialized properly.
135-
dbNode, err := graph.FetchLightningNode(testPub)
135+
dbNode, err := graph.FetchLightningNode(ctx, testPub)
136136
require.NoError(t, err, "unable to locate node")
137137

138138
_, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes)
@@ -170,7 +170,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
170170

171171
// Finally, attempt to fetch the node again. This should fail as the
172172
// node should have been deleted from the database.
173-
_, err = graph.FetchLightningNode(testPub)
173+
_, err = graph.FetchLightningNode(ctx, testPub)
174174
require.ErrorIs(t, err, ErrGraphNodeNotFound)
175175

176176
// Now, we'll specifically test the updating of addresses of a node
@@ -192,7 +192,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
192192
require.NoError(t, graph.AddLightningNode(ctx, node))
193193

194194
// Fetch the node and assert the empty addresses.
195-
dbNode, err = graph.FetchLightningNode(testPub)
195+
dbNode, err = graph.FetchLightningNode(ctx, testPub)
196196
require.NoError(t, err)
197197
require.Empty(t, dbNode.Addresses)
198198

@@ -219,7 +219,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
219219
require.NoError(t, graph.AddLightningNode(ctx, node))
220220

221221
// Fetch the node and assert the updated addresses.
222-
dbNode, err = graph.FetchLightningNode(testPub)
222+
dbNode, err = graph.FetchLightningNode(ctx, testPub)
223223
require.NoError(t, err)
224224
require.Equal(t, expAddrs, dbNode.Addresses)
225225

@@ -240,7 +240,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
240240
require.NoError(t, graph.AddLightningNode(ctx, node))
241241

242242
// Fetch the node and assert the updated addresses.
243-
dbNode, err = graph.FetchLightningNode(testPub)
243+
dbNode, err = graph.FetchLightningNode(ctx, testPub)
244244
require.NoError(t, err)
245245
require.Equal(t, expAddrs, dbNode.Addresses)
246246

@@ -253,7 +253,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
253253
require.NoError(t, graph.AddLightningNode(ctx, node))
254254

255255
// Fetch the node and assert the updated addresses.
256-
dbNode, err = graph.FetchLightningNode(testPub)
256+
dbNode, err = graph.FetchLightningNode(ctx, testPub)
257257
require.NoError(t, err)
258258
require.Equal(t, expAddrs, dbNode.Addresses)
259259
}
@@ -262,6 +262,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
262262
// only the pubkey is known to the database.
263263
func TestPartialNode(t *testing.T) {
264264
t.Parallel()
265+
ctx := context.Background()
265266

266267
graph := MakeTestGraphNew(t)
267268

@@ -282,9 +283,9 @@ func TestPartialNode(t *testing.T) {
282283

283284
// Next, fetch the node2 from the database to ensure everything was
284285
// serialized properly.
285-
dbNode1, err := graph.FetchLightningNode(pubKey1)
286+
dbNode1, err := graph.FetchLightningNode(ctx, pubKey1)
286287
require.NoError(t, err)
287-
dbNode2, err := graph.FetchLightningNode(pubKey2)
288+
dbNode2, err := graph.FetchLightningNode(ctx, pubKey2)
288289
require.NoError(t, err)
289290

290291
_, exists, err := graph.HasLightningNode(dbNode1.PubKeyBytes)
@@ -322,7 +323,7 @@ func TestPartialNode(t *testing.T) {
322323

323324
// Finally, attempt to fetch the node again. This should fail as the
324325
// node should have been deleted from the database.
325-
_, err = graph.FetchLightningNode(testPub)
326+
_, err = graph.FetchLightningNode(ctx, testPub)
326327
require.ErrorIs(t, err, ErrGraphNodeNotFound)
327328
}
328329

@@ -3338,7 +3339,7 @@ func TestPruneGraphNodes(t *testing.T) {
33383339

33393340
// Finally, we'll ensure that node3, the only fully unconnected node as
33403341
// properly deleted from the graph and not another node in its place.
3341-
_, err := graph.FetchLightningNode(node3.PubKeyBytes)
3342+
_, err := graph.FetchLightningNode(ctx, node3.PubKeyBytes)
33423343
require.NotNil(t, err)
33433344
}
33443345

@@ -3364,11 +3365,11 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
33643365

33653366
// Ensure that node1 was inserted as a full node, while node2 only has
33663367
// a shell node present.
3367-
node1, err := graph.FetchLightningNode(node1.PubKeyBytes)
3368+
node1, err := graph.FetchLightningNode(ctx, node1.PubKeyBytes)
33683369
require.NoError(t, err, "unable to fetch node1")
33693370
require.True(t, node1.HaveNodeAnnouncement)
33703371

3371-
node2, err = graph.FetchLightningNode(node2.PubKeyBytes)
3372+
node2, err = graph.FetchLightningNode(ctx, node2.PubKeyBytes)
33723373
require.NoError(t, err, "unable to fetch node2")
33733374
require.False(t, node2.HaveNodeAnnouncement)
33743375

@@ -4350,7 +4351,7 @@ func TestLightningNodePersistence(t *testing.T) {
43504351
require.NoError(t, err)
43514352

43524353
// Read the node from disk.
4353-
diskNode, err := graph.FetchLightningNode(node.PubKeyBytes)
4354+
diskNode, err := graph.FetchLightningNode(ctx, node.PubKeyBytes)
43544355
require.NoError(t, err)
43554356

43564357
// Convert it back to a wire message.

graph/db/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ type V1Store interface { //nolint:interfacebloat
126126
// FetchLightningNode attempts to look up a target node by its identity
127127
// public key. If the node isn't found in the database, then
128128
// ErrGraphNodeNotFound is returned.
129-
FetchLightningNode(nodePub route.Vertex) (
130-
*models.LightningNode, error)
129+
FetchLightningNode(ctx context.Context,
130+
nodePub route.Vertex) (*models.LightningNode, error)
131131

132132
// HasLightningNode determines if the graph has a vertex identified by
133133
// the target node identity public key. If the node exists in the

graph/db/kv_store.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,14 @@ func initKVStore(db kvdb.Backend) error {
379379
func (c *KVStore) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr,
380380
error) {
381381

382+
ctx := context.TODO()
383+
382384
pubKey, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
383385
if err != nil {
384386
return false, nil, err
385387
}
386388

387-
node, err := c.FetchLightningNode(pubKey)
389+
node, err := c.FetchLightningNode(ctx, pubKey)
388390
// We don't consider it an error if the graph is unaware of the node.
389391
switch {
390392
case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
@@ -2995,8 +2997,8 @@ func (c *KVStore) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
29952997
// FetchLightningNode attempts to look up a target node by its identity public
29962998
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
29972999
// returned.
2998-
func (c *KVStore) FetchLightningNode(nodePub route.Vertex) (
2999-
*models.LightningNode, error) {
3000+
func (c *KVStore) FetchLightningNode(_ context.Context,
3001+
nodePub route.Vertex) (*models.LightningNode, error) {
30003002

30013003
return c.fetchLightningNode(nil, nodePub)
30023004
}

graph/db/sql_store.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,8 @@ func (s *SQLStore) AddLightningNode(ctx context.Context,
193193
// returned.
194194
//
195195
// NOTE: part of the V1Store interface.
196-
func (s *SQLStore) FetchLightningNode(pubKey route.Vertex) (
197-
*models.LightningNode, error) {
198-
199-
ctx := context.TODO()
196+
func (s *SQLStore) FetchLightningNode(ctx context.Context,
197+
pubKey route.Vertex) (*models.LightningNode, error) {
200198

201199
var node *models.LightningNode
202200
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {

graph/interfaces.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ type ChannelGraphSource interface {
8484
// FetchLightningNode attempts to look up a target node by its identity
8585
// public key. channeldb.ErrGraphNodeNotFound is returned if the node
8686
// doesn't exist within the graph.
87-
FetchLightningNode(route.Vertex) (*models.LightningNode, error)
87+
FetchLightningNode(context.Context,
88+
route.Vertex) (*models.LightningNode, error)
8889

8990
// MarkZombieEdge marks the channel with the given ID as a zombie edge.
9091
MarkZombieEdge(chanID uint64) error
@@ -242,8 +243,8 @@ type DB interface {
242243
// FetchLightningNode attempts to look up a target node by its identity
243244
// public key. If the node isn't found in the database, then
244245
// ErrGraphNodeNotFound is returned.
245-
FetchLightningNode(nodePub route.Vertex) (*models.LightningNode,
246-
error)
246+
FetchLightningNode(ctx context.Context,
247+
nodePub route.Vertex) (*models.LightningNode, error)
247248

248249
// ForEachNodeChannel iterates through all channels of the given node,
249250
// executing the passed callback with an edge info structure and the

routing/router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,12 +2907,12 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
29072907
_, _, err = ctx.router.FindRoute(req)
29082908
require.NoError(t, err, "unable to find any routes")
29092909

2910-
copy1, err := ctx.graph.FetchLightningNode(pub1)
2910+
copy1, err := ctx.graph.FetchLightningNode(ctxb, pub1)
29112911
require.NoError(t, err, "unable to fetch node")
29122912

29132913
require.Equal(t, n1.Alias, copy1.Alias)
29142914

2915-
copy2, err := ctx.graph.FetchLightningNode(pub2)
2915+
copy2, err := ctx.graph.FetchLightningNode(ctxb, pub2)
29162916
require.NoError(t, err, "unable to fetch node")
29172917

29182918
require.Equal(t, n2.Alias, copy2.Alias)

0 commit comments

Comments
 (0)