Skip to content

Commit d1cfb47

Browse files
committed
graph/db: thread context to AddLightningNode
1 parent c1740c1 commit d1cfb47

File tree

15 files changed

+126
-81
lines changed

15 files changed

+126
-81
lines changed

autopilot/prefattach_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
422422
AuthSigBytes: testSig.Serialize(),
423423
}
424424
graphNode.AddPubKey(pub)
425-
err := d.db.AddLightningNode(graphNode)
425+
err := d.db.AddLightningNode(
426+
context.Background(), graphNode,
427+
)
426428
if err != nil {
427429
return nil, err
428430
}
@@ -450,7 +452,9 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
450452
AuthSigBytes: testSig.Serialize(),
451453
}
452454
dbNode.AddPubKey(nodeKey)
453-
if err := d.db.AddLightningNode(dbNode); err != nil {
455+
if err := d.db.AddLightningNode(
456+
context.Background(), dbNode,
457+
); err != nil {
454458
return nil, err
455459
}
456460

@@ -554,7 +558,8 @@ func (d *testDBGraph) addRandNode() (*btcec.PublicKey, error) {
554558
AuthSigBytes: testSig.Serialize(),
555559
}
556560
dbNode.AddPubKey(nodeKey)
557-
if err := d.db.AddLightningNode(dbNode); err != nil {
561+
err = d.db.AddLightningNode(context.Background(), dbNode)
562+
if err != nil {
558563
return nil, err
559564
}
560565

discovery/gossiper.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,15 +2022,17 @@ func (d *AuthenticatedGossiper) fetchPKScript(chanID *lnwire.ShortChannelID) (
20222022

20232023
// addNode processes the given node announcement, and adds it to our channel
20242024
// graph.
2025-
func (d *AuthenticatedGossiper) addNode(_ context.Context,
2025+
func (d *AuthenticatedGossiper) addNode(ctx context.Context,
20262026
msg *lnwire.NodeAnnouncement, op ...batch.SchedulerOption) error {
20272027

20282028
if err := netann.ValidateNodeAnn(msg); err != nil {
20292029
return fmt.Errorf("unable to validate node announcement: %w",
20302030
err)
20312031
}
20322032

2033-
return d.cfg.Graph.AddNode(models.NodeFromWireAnnouncement(msg), op...)
2033+
return d.cfg.Graph.AddNode(
2034+
ctx, models.NodeFromWireAnnouncement(msg), op...,
2035+
)
20342036
}
20352037

20362038
// isPremature decides whether a given network message has a block height+delta

discovery/gossiper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func newMockRouter(t *testing.T, height uint32) *mockGraphSource {
109109

110110
var _ graph.ChannelGraphSource = (*mockGraphSource)(nil)
111111

112-
func (r *mockGraphSource) AddNode(node *models.LightningNode,
112+
func (r *mockGraphSource) AddNode(_ context.Context, node *models.LightningNode,
113113
_ ...batch.SchedulerOption) error {
114114

115115
r.mu.Lock()

graph/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graph
22

33
import (
4+
"context"
45
"fmt"
56
"sync"
67
"sync/atomic"
@@ -972,10 +973,10 @@ func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
972973
// be ignored.
973974
//
974975
// NOTE: This method is part of the ChannelGraphSource interface.
975-
func (b *Builder) AddNode(node *models.LightningNode,
976+
func (b *Builder) AddNode(ctx context.Context, node *models.LightningNode,
976977
op ...batch.SchedulerOption) error {
977978

978-
err := b.addNode(node, op...)
979+
err := b.addNode(ctx, node, op...)
979980
if err != nil {
980981
logNetworkMsgProcessError(err)
981982

@@ -989,7 +990,7 @@ func (b *Builder) AddNode(node *models.LightningNode,
989990
// currently have persisted in the graph, and then adds it to the graph. If we
990991
// already know about the node, then we only update our DB if the new update
991992
// has a newer timestamp than the last one we received.
992-
func (b *Builder) addNode(node *models.LightningNode,
993+
func (b *Builder) addNode(ctx context.Context, node *models.LightningNode,
993994
op ...batch.SchedulerOption) error {
994995

995996
// Before we add the node to the database, we'll check to see if the
@@ -1000,7 +1001,7 @@ func (b *Builder) addNode(node *models.LightningNode,
10001001
return err
10011002
}
10021003

1003-
if err := b.cfg.Graph.AddLightningNode(node, op...); err != nil {
1004+
if err := b.cfg.Graph.AddLightningNode(ctx, node, op...); err != nil {
10041005
return errors.Errorf("unable to add node %x to the "+
10051006
"graph: %v", node.PubKeyBytes, err)
10061007
}

graph/builder_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graph
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/sha256"
67
"encoding/hex"
78
"encoding/json"
@@ -106,7 +107,7 @@ func TestIgnoreNodeAnnouncement(t *testing.T) {
106107
}
107108
copy(node.PubKeyBytes[:], pub.SerializeCompressed())
108109

109-
err := ctx.builder.AddNode(node)
110+
err := ctx.builder.AddNode(context.Background(), node)
110111
if !IsError(err, ErrIgnored) {
111112
t.Fatalf("expected to get ErrIgnore, instead got: %v", err)
112113
}
@@ -1068,7 +1069,7 @@ func TestIsStaleNode(t *testing.T) {
10681069
Features: testFeatures,
10691070
}
10701071
copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed())
1071-
if err := ctx.builder.AddNode(n1); err != nil {
1072+
if err := ctx.builder.AddNode(context.Background(), n1); err != nil {
10721073
t.Fatalf("could not add node: %v", err)
10731074
}
10741075

@@ -1328,6 +1329,8 @@ func createTestCtxFromFile(t *testing.T,
13281329
func parseTestGraph(t *testing.T, useCache bool, path string) (
13291330
*testGraphInstance, error) {
13301331

1332+
ctx := context.Background()
1333+
13311334
graphJSON, err := os.ReadFile(path)
13321335
if err != nil {
13331336
return nil, err
@@ -1434,7 +1437,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
14341437

14351438
// With the node fully parsed, add it as a vertex within the
14361439
// graph.
1437-
if err := graph.AddLightningNode(dbNode); err != nil {
1440+
if err := graph.AddLightningNode(ctx, dbNode); err != nil {
14381441
return nil, err
14391442
}
14401443
}
@@ -1715,6 +1718,8 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17151718
testChannels []*testChannel, source string) (*testGraphInstance,
17161719
error) {
17171720

1721+
ctx := context.Background()
1722+
17181723
// We'll use this fake address for the IP address of all the nodes in
17191724
// our tests. This value isn't needed for path finding so it doesn't
17201725
// need to be unique.
@@ -1763,7 +1768,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17631768

17641769
// With the node fully parsed, add it as a vertex within the
17651770
// graph.
1766-
if err := graph.AddLightningNode(dbNode); err != nil {
1771+
if err := graph.AddLightningNode(ctx, dbNode); err != nil {
17671772
return nil, err
17681773
}
17691774

graph/db/graph.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphdb
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"sync"
@@ -260,10 +261,10 @@ func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
260261
// information. Note that this method is expected to only be called to update an
261262
// already present node from a node announcement, or to insert a node found in a
262263
// channel update.
263-
func (c *ChannelGraph) AddLightningNode(node *models.LightningNode,
264-
op ...batch.SchedulerOption) error {
264+
func (c *ChannelGraph) AddLightningNode(ctx context.Context,
265+
node *models.LightningNode, op ...batch.SchedulerOption) error {
265266

266-
err := c.V1Store.AddLightningNode(node, op...)
267+
err := c.V1Store.AddLightningNode(ctx, node, op...)
267268
if err != nil {
268269
return err
269270
}

0 commit comments

Comments
 (0)