diff --git a/autopilot/prefattach_test.go b/autopilot/prefattach_test.go index e0baf4971..fd94b86cc 100644 --- a/autopilot/prefattach_test.go +++ b/autopilot/prefattach_test.go @@ -422,7 +422,9 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey, AuthSigBytes: testSig.Serialize(), } graphNode.AddPubKey(pub) - err := d.db.AddLightningNode(graphNode) + err := d.db.AddLightningNode( + context.Background(), graphNode, + ) if err != nil { return nil, err } @@ -450,7 +452,9 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey, AuthSigBytes: testSig.Serialize(), } dbNode.AddPubKey(nodeKey) - if err := d.db.AddLightningNode(dbNode); err != nil { + if err := d.db.AddLightningNode( + context.Background(), dbNode, + ); err != nil { return nil, err } @@ -554,7 +558,8 @@ func (d *testDBGraph) addRandNode() (*btcec.PublicKey, error) { AuthSigBytes: testSig.Serialize(), } dbNode.AddPubKey(nodeKey) - if err := d.db.AddLightningNode(dbNode); err != nil { + err = d.db.AddLightningNode(context.Background(), dbNode) + if err != nil { return nil, err } diff --git a/discovery/gossiper.go b/discovery/gossiper.go index d6855ae45..3cd3bafaf 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -2022,7 +2022,7 @@ func (d *AuthenticatedGossiper) fetchPKScript(chanID *lnwire.ShortChannelID) ( // addNode processes the given node announcement, and adds it to our channel // graph. -func (d *AuthenticatedGossiper) addNode(_ context.Context, +func (d *AuthenticatedGossiper) addNode(ctx context.Context, msg *lnwire.NodeAnnouncement, op ...batch.SchedulerOption) error { if err := netann.ValidateNodeAnn(msg); err != nil { @@ -2030,7 +2030,9 @@ func (d *AuthenticatedGossiper) addNode(_ context.Context, err) } - return d.cfg.Graph.AddNode(models.NodeFromWireAnnouncement(msg), op...) + return d.cfg.Graph.AddNode( + ctx, models.NodeFromWireAnnouncement(msg), op..., + ) } // isPremature decides whether a given network message has a block height+delta diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index 399010991..d337ac4c3 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -109,7 +109,7 @@ func newMockRouter(t *testing.T, height uint32) *mockGraphSource { var _ graph.ChannelGraphSource = (*mockGraphSource)(nil) -func (r *mockGraphSource) AddNode(node *models.LightningNode, +func (r *mockGraphSource) AddNode(_ context.Context, node *models.LightningNode, _ ...batch.SchedulerOption) error { r.mu.Lock() diff --git a/graph/builder.go b/graph/builder.go index 99fc73c17..930091f65 100644 --- a/graph/builder.go +++ b/graph/builder.go @@ -1,6 +1,7 @@ package graph import ( + "context" "fmt" "sync" "sync/atomic" @@ -972,10 +973,10 @@ func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool { // be ignored. // // NOTE: This method is part of the ChannelGraphSource interface. -func (b *Builder) AddNode(node *models.LightningNode, +func (b *Builder) AddNode(ctx context.Context, node *models.LightningNode, op ...batch.SchedulerOption) error { - err := b.addNode(node, op...) + err := b.addNode(ctx, node, op...) if err != nil { logNetworkMsgProcessError(err) @@ -989,7 +990,7 @@ func (b *Builder) AddNode(node *models.LightningNode, // currently have persisted in the graph, and then adds it to the graph. If we // already know about the node, then we only update our DB if the new update // has a newer timestamp than the last one we received. -func (b *Builder) addNode(node *models.LightningNode, +func (b *Builder) addNode(ctx context.Context, node *models.LightningNode, op ...batch.SchedulerOption) error { // 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, return err } - if err := b.cfg.Graph.AddLightningNode(node, op...); err != nil { + if err := b.cfg.Graph.AddLightningNode(ctx, node, op...); err != nil { return errors.Errorf("unable to add node %x to the "+ "graph: %v", node.PubKeyBytes, err) } diff --git a/graph/builder_test.go b/graph/builder_test.go index fa7fc722d..ca46671ca 100644 --- a/graph/builder_test.go +++ b/graph/builder_test.go @@ -2,6 +2,7 @@ package graph import ( "bytes" + "context" "crypto/sha256" "encoding/hex" "encoding/json" @@ -106,7 +107,7 @@ func TestIgnoreNodeAnnouncement(t *testing.T) { } copy(node.PubKeyBytes[:], pub.SerializeCompressed()) - err := ctx.builder.AddNode(node) + err := ctx.builder.AddNode(context.Background(), node) if !IsError(err, ErrIgnored) { t.Fatalf("expected to get ErrIgnore, instead got: %v", err) } @@ -1068,7 +1069,7 @@ func TestIsStaleNode(t *testing.T) { Features: testFeatures, } copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed()) - if err := ctx.builder.AddNode(n1); err != nil { + if err := ctx.builder.AddNode(context.Background(), n1); err != nil { t.Fatalf("could not add node: %v", err) } @@ -1328,6 +1329,8 @@ func createTestCtxFromFile(t *testing.T, func parseTestGraph(t *testing.T, useCache bool, path string) ( *testGraphInstance, error) { + ctx := context.Background() + graphJSON, err := os.ReadFile(path) if err != nil { return nil, err @@ -1434,7 +1437,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) ( // With the node fully parsed, add it as a vertex within the // graph. - if err := graph.AddLightningNode(dbNode); err != nil { + if err := graph.AddLightningNode(ctx, dbNode); err != nil { return nil, err } } @@ -1715,6 +1718,8 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, testChannels []*testChannel, source string) (*testGraphInstance, error) { + ctx := context.Background() + // We'll use this fake address for the IP address of all the nodes in // our tests. This value isn't needed for path finding so it doesn't // need to be unique. @@ -1763,7 +1768,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, // With the node fully parsed, add it as a vertex within the // graph. - if err := graph.AddLightningNode(dbNode); err != nil { + if err := graph.AddLightningNode(ctx, dbNode); err != nil { return nil, err } diff --git a/graph/db/graph.go b/graph/db/graph.go index a68368e3d..a02e8193f 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -1,6 +1,7 @@ package graphdb import ( + "context" "errors" "fmt" "sync" @@ -260,10 +261,10 @@ func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex, // information. Note that this method is expected to only be called to update an // already present node from a node announcement, or to insert a node found in a // channel update. -func (c *ChannelGraph) AddLightningNode(node *models.LightningNode, - op ...batch.SchedulerOption) error { +func (c *ChannelGraph) AddLightningNode(ctx context.Context, + node *models.LightningNode, op ...batch.SchedulerOption) error { - err := c.V1Store.AddLightningNode(node, op...) + err := c.V1Store.AddLightningNode(ctx, node, op...) if err != nil { return err } diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 18d3e52a5..ee77c1376 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -2,6 +2,7 @@ package graphdb import ( "bytes" + "context" "crypto/sha256" "encoding/hex" "errors" @@ -99,6 +100,7 @@ func createTestVertex(t testing.TB) *models.LightningNode { func TestNodeInsertionAndDeletion(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) @@ -123,7 +125,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { // First, insert the node into the graph DB. This should succeed // without any errors. node := nodeWithAddrs(testAddrs) - if err := graph.AddLightningNode(node); err != nil { + if err := graph.AddLightningNode(ctx, node); err != nil { t.Fatalf("unable to add node: %v", err) } assertNodeInCache(t, graph, node, testFeatures) @@ -187,7 +189,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { // Add the node without any addresses. node = nodeWithAddrs(nil) - require.NoError(t, graph.AddLightningNode(node)) + require.NoError(t, graph.AddLightningNode(ctx, node)) // Fetch the node and assert the empty addresses. dbNode, err = graph.FetchLightningNode(testPub) @@ -214,7 +216,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { testOpaqueAddr, } node = nodeWithAddrs(expAddrs) - require.NoError(t, graph.AddLightningNode(node)) + require.NoError(t, graph.AddLightningNode(ctx, node)) // Fetch the node and assert the updated addresses. dbNode, err = graph.FetchLightningNode(testPub) @@ -235,7 +237,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { testIPV6Addr, } node = nodeWithAddrs(expAddrs) - require.NoError(t, graph.AddLightningNode(node)) + require.NoError(t, graph.AddLightningNode(ctx, node)) // Fetch the node and assert the updated addresses. dbNode, err = graph.FetchLightningNode(testPub) @@ -248,7 +250,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { testOnionV3Addr, } node = nodeWithAddrs(expAddrs) - require.NoError(t, graph.AddLightningNode(node)) + require.NoError(t, graph.AddLightningNode(ctx, node)) // Fetch the node and assert the updated addresses. dbNode, err = graph.FetchLightningNode(testPub) @@ -327,6 +329,7 @@ func TestPartialNode(t *testing.T) { // TestAliasLookup tests the alias lookup functionality of the graph store. func TestAliasLookup(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) @@ -336,7 +339,7 @@ func TestAliasLookup(t *testing.T) { // Add the node to the graph's database, this should also insert an // entry into the alias index for this node. - require.NoError(t, graph.AddLightningNode(testNode)) + require.NoError(t, graph.AddLightningNode(ctx, testNode)) // Next, attempt to lookup the alias. The alias should exactly match // the one which the test node was assigned. @@ -801,18 +804,19 @@ func createChannelEdge(node1, node2 *models.LightningNode, func TestEdgeInfoUpdates(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) // We'd like to test the update of edges inserted into the database, so // we create two vertexes to connect. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } assertNodeInCache(t, graph, node1, testFeatures) node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } assertNodeInCache(t, graph, node2, testFeatures) @@ -1483,6 +1487,8 @@ func TestGraphCacheTraversal(t *testing.T) { func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes, numChannels int) (map[uint64]struct{}, []*models.LightningNode) { + ctx := context.Background() + nodes := make([]*models.LightningNode, numNodes) nodeIndex := map[string]struct{}{} for i := 0; i < numNodes; i++ { @@ -1495,7 +1501,7 @@ func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes, // Add each of the nodes into the graph, they should be inserted // without error. for _, node := range nodes { - require.NoError(t, graph.AddLightningNode(node)) + require.NoError(t, graph.AddLightningNode(ctx, node)) } // Iterate over each node as returned by the graph, if all nodes are @@ -1671,6 +1677,7 @@ func assertChanViewEqualChanPoints(t *testing.T, a []EdgePoint, func TestGraphPruning(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -1687,7 +1694,7 @@ func TestGraphPruning(t *testing.T) { for i := 0; i < numNodes; i++ { node := createTestVertex(t) - if err := graph.AddLightningNode(node); err != nil { + if err := graph.AddLightningNode(ctx, node); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -1919,6 +1926,7 @@ func TestHighestChanID(t *testing.T) { // insertion of a new edge, the edge update index is updated properly. func TestChanUpdatesInHorizon(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -1935,11 +1943,11 @@ func TestChanUpdatesInHorizon(t *testing.T) { // We'll start by creating two nodes which will seed our test graph. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -2077,6 +2085,7 @@ func TestChanUpdatesInHorizon(t *testing.T) { // the most recent node updates within a particular time horizon. func TestNodeUpdatesInHorizon(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) @@ -2108,7 +2117,7 @@ func TestNodeUpdatesInHorizon(t *testing.T) { nodeAnns = append(nodeAnns, *nodeAnn) - require.NoError(t, graph.AddLightningNode(nodeAnn)) + require.NoError(t, graph.AddLightningNode(ctx, nodeAnn)) } queryCases := []struct { @@ -2245,6 +2254,7 @@ func TestFilterKnownChanIDsZombieRevival(t *testing.T) { // know of on disk. func TestFilterKnownChanIDs(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -2277,11 +2287,11 @@ func TestFilterKnownChanIDs(t *testing.T) { // We'll start by creating two nodes which will seed our test graph. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -2422,14 +2432,15 @@ func TestFilterKnownChanIDs(t *testing.T) { // methods that acquire the cache mutex along with the DB mutex. func TestStressTestChannelGraphAPI(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) node1 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node1)) + require.NoError(t, graph.AddLightningNode(ctx, node1)) node2 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node2)) + require.NoError(t, graph.AddLightningNode(ctx, node2)) require.NoError(t, graph.SetSourceNode(node1)) @@ -2706,16 +2717,17 @@ func TestStressTestChannelGraphAPI(t *testing.T) { // set of short channel ID's for a given block range. func TestFilterChannelRange(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) // We'll first populate our graph with two nodes. All channels created // below will be made between these two nodes. node1 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node1)) + require.NoError(t, graph.AddLightningNode(ctx, node1)) node2 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node2)) + require.NoError(t, graph.AddLightningNode(ctx, node2)) // If we try to filter a channel range before we have any channels // inserted, we should get an empty slice of results. @@ -2922,17 +2934,18 @@ func TestFilterChannelRange(t *testing.T) { // of ChannelEdge structs for a given set of short channel ID's. func TestFetchChanInfos(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) // We'll first populate our graph with two nodes. All channels created // below will be made between these two nodes. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3024,16 +3037,17 @@ func TestFetchChanInfos(t *testing.T) { // both sides. func TestIncompleteChannelPolicies(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) // Create two nodes. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3119,6 +3133,7 @@ func TestIncompleteChannelPolicies(t *testing.T) { // up. func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -3136,11 +3151,11 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) { // We'll first populate our graph with two nodes. All channels created // below will be made between these two nodes. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3266,6 +3281,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) { // PruneSyncState method. func TestPruneGraphNodes(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -3280,15 +3296,15 @@ func TestPruneGraphNodes(t *testing.T) { // channel graph, at the end of the scenario, only two of these nodes // should still be in the graph. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } node3 := createTestVertex(t) - if err := graph.AddLightningNode(node3); err != nil { + if err := graph.AddLightningNode(ctx, node3); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3331,6 +3347,7 @@ func TestPruneGraphNodes(t *testing.T) { // database, then shell edges are created for each node if needed. func TestAddChannelEdgeShellNodes(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) @@ -3361,7 +3378,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) { require.ErrorIs(t, err, ErrEdgeAlreadyExist) // Show that updating the shell node to a full node record works. - require.NoError(t, graph.AddLightningNode(node2)) + require.NoError(t, graph.AddLightningNode(ctx, node2)) } // TestNodePruningUpdateIndexDeletion tests that once a node has been removed @@ -3369,13 +3386,14 @@ func TestAddChannelEdgeShellNodes(t *testing.T) { // well. func TestNodePruningUpdateIndexDeletion(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) // We'll first populate our graph with a single node that will be // removed shortly. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3416,6 +3434,7 @@ func TestNodePruningUpdateIndexDeletion(t *testing.T) { // public within the network graph. func TestNodeIsPublic(t *testing.T) { t.Parallel() + ctx := context.Background() // We'll start off the test by creating a small network of 3 // participants with the following graph: @@ -3453,9 +3472,8 @@ func TestNodeIsPublic(t *testing.T) { graphs := []*ChannelGraph{aliceGraph, bobGraph, carolGraph} for _, graph := range graphs { for _, node := range nodes { - if err := graph.AddLightningNode(node); err != nil { - t.Fatalf("unable to add node: %v", err) - } + err := graph.AddLightningNode(ctx, node) + require.NoError(t, err) } for _, edge := range edges { if err := graph.AddChannelEdge(edge); err != nil { @@ -3551,24 +3569,25 @@ func TestNodeIsPublic(t *testing.T) { // DisabledChannelIDs is correct. func TestDisabledChannelIDs(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) // Create first node and add it to the graph. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } // Create second node and add it to the graph. node2 := createTestVertex(t) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } // Adding a new channel edge to the graph. edgeInfo, edge1, edge2 := createChannelEdge(node1, node2) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -3634,6 +3653,7 @@ func TestDisabledChannelIDs(t *testing.T) { // receive the proper update. func TestEdgePolicyMissingMaxHtcl(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -3646,13 +3666,13 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) { // We'd like to test the update of edges inserted into the database, so // we create two vertexes to connect. node1 := createTestVertex(t) - if err := graph.AddLightningNode(node1); err != nil { + if err := graph.AddLightningNode(ctx, node1); err != nil { t.Fatalf("unable to add node: %v", err) } node2 := createTestVertex(t) edgeInfo, edge1, edge2 := createChannelEdge(node1, node2) - if err := graph.AddLightningNode(node2); err != nil { + if err := graph.AddLightningNode(ctx, node2); err != nil { t.Fatalf("unable to add node: %v", err) } if err := graph.AddChannelEdge(edgeInfo); err != nil { @@ -4037,15 +4057,16 @@ func TestBatchedAddChannelEdge(t *testing.T) { // executes multiple UpdateEdgePolicy requests in a single txn. func TestBatchedUpdateEdgePolicy(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraphNew(t) // We'd like to test the update of edges inserted into the database, so // we create two vertexes to connect. node1 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node1)) + require.NoError(t, graph.AddLightningNode(ctx, node1)) node2 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node2)) + require.NoError(t, graph.AddLightningNode(ctx, node2)) // Create an edge and add it to the db. edgeInfo, edge1, edge2 := createChannelEdge(node1, node2) @@ -4139,6 +4160,7 @@ func BenchmarkForEachChannel(b *testing.B) { // method works as expected, and is able to handle nil self edges. func TestGraphCacheForEachNodeChannel(t *testing.T) { t.Parallel() + ctx := context.Background() graph := MakeTestGraph(t) @@ -4147,9 +4169,9 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) { graph.graphCache = nil node1 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node1)) + require.NoError(t, graph.AddLightningNode(ctx, node1)) node2 := createTestVertex(t) - require.NoError(t, graph.AddLightningNode(node2)) + require.NoError(t, graph.AddLightningNode(ctx, node2)) // Create an edge and add it to the db. edgeInfo, e1, e2 := createChannelEdge(node1, node2) @@ -4305,6 +4327,7 @@ var testNodeAnn = "01012674c2e7ef68c73a086b7de2603f4ef1567358df84bb4edaa06c" + // that the two messages are equal. func TestLightningNodePersistence(t *testing.T) { t.Parallel() + ctx := context.Background() // Create a new test graph instance. graph := MakeTestGraphNew(t) @@ -4323,7 +4346,7 @@ func TestLightningNodePersistence(t *testing.T) { node := models.NodeFromWireAnnouncement(na) // Persist the node to disk. - err = graph.AddLightningNode(node) + err = graph.AddLightningNode(ctx, node) require.NoError(t, err) // Read the node from disk. diff --git a/graph/db/interfaces.go b/graph/db/interfaces.go index 0f7d499fe..4e7f7eae1 100644 --- a/graph/db/interfaces.go +++ b/graph/db/interfaces.go @@ -1,6 +1,7 @@ package graphdb import ( + "context" "net" "time" @@ -56,7 +57,7 @@ type V1Store interface { //nolint:interfacebloat // update that node's information. Note that this method is expected to // only be called to update an already present node from a node // announcement, or to insert a node found in a channel update. - AddLightningNode(node *models.LightningNode, + AddLightningNode(ctx context.Context, node *models.LightningNode, op ...batch.SchedulerOption) error // AddrsForNode returns all known addresses for the target node public diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index bbac55f5d..bea8fb916 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -957,10 +957,8 @@ func (c *KVStore) SetSourceNode(node *models.LightningNode) error { // channel update. // // TODO(roasbeef): also need sig of announcement. -func (c *KVStore) AddLightningNode(node *models.LightningNode, - opts ...batch.SchedulerOption) error { - - ctx := context.TODO() +func (c *KVStore) AddLightningNode(ctx context.Context, + node *models.LightningNode, opts ...batch.SchedulerOption) error { r := &batch.Request[kvdb.RwTx]{ Opts: batch.NewSchedulerOptions(opts...), diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 2ae133480..148666cb8 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -174,10 +174,8 @@ func NewSQLStore(cfg *SQLStoreConfig, db BatchedSQLQueries, kvStore *KVStore, // information. // // NOTE: part of the V1Store interface. -func (s *SQLStore) AddLightningNode(node *models.LightningNode, - opts ...batch.SchedulerOption) error { - - ctx := context.TODO() +func (s *SQLStore) AddLightningNode(ctx context.Context, + node *models.LightningNode, opts ...batch.SchedulerOption) error { r := &batch.Request[SQLQueries]{ Opts: batch.NewSchedulerOptions(opts...), diff --git a/graph/interfaces.go b/graph/interfaces.go index 52897d8f5..a487c8af5 100644 --- a/graph/interfaces.go +++ b/graph/interfaces.go @@ -1,6 +1,7 @@ package graph import ( + "context" "time" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -22,7 +23,7 @@ type ChannelGraphSource interface { // AddNode is used to add information about a node to the router // database. If the node with this pubkey is not present in an existing // channel, it will be ignored. - AddNode(node *models.LightningNode, + AddNode(ctx context.Context, node *models.LightningNode, op ...batch.SchedulerOption) error // AddEdge is used to add edge/channel to the topology of the router, @@ -202,7 +203,7 @@ type DB interface { // update that node's information. Note that this method is expected to // only be called to update an already present node from a node // announcement, or to insert a node found in a channel update. - AddLightningNode(node *models.LightningNode, + AddLightningNode(ctx context.Context, node *models.LightningNode, op ...batch.SchedulerOption) error // AddChannelEdge adds a new (undirected, blank) edge to the graph diff --git a/graph/notifications_test.go b/graph/notifications_test.go index 2f2ec1b4b..099c2244b 100644 --- a/graph/notifications_test.go +++ b/graph/notifications_test.go @@ -2,6 +2,7 @@ package graph import ( "bytes" + "context" "encoding/hex" "fmt" "image/color" @@ -608,6 +609,7 @@ func TestEdgeUpdateNotification(t *testing.T) { // attributes with new data. func TestNodeUpdateNotification(t *testing.T) { t.Parallel() + ctxb := context.Background() const startingBlockHeight = 101 ctx := createTestCtxSingleNode(t, startingBlockHeight) @@ -665,10 +667,10 @@ func TestNodeUpdateNotification(t *testing.T) { // Change network topology by adding the updated info for the two nodes // to the channel router. - if err := ctx.builder.AddNode(node1); err != nil { + if err := ctx.builder.AddNode(ctxb, node1); err != nil { t.Fatalf("unable to add node: %v", err) } - if err := ctx.builder.AddNode(node2); err != nil { + if err := ctx.builder.AddNode(ctxb, node2); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -763,7 +765,7 @@ func TestNodeUpdateNotification(t *testing.T) { nodeUpdateAnn.LastUpdate = node1.LastUpdate.Add(300 * time.Millisecond) // Add new node topology update to the channel router. - if err := ctx.builder.AddNode(&nodeUpdateAnn); err != nil { + if err := ctx.builder.AddNode(ctxb, &nodeUpdateAnn); err != nil { t.Fatalf("unable to add node: %v", err) } @@ -790,6 +792,7 @@ func TestNodeUpdateNotification(t *testing.T) { // when the client wishes to exit. func TestNotificationCancellation(t *testing.T) { t.Parallel() + ctxb := context.Background() const startingBlockHeight = 101 ctx := createTestCtxSingleNode(t, startingBlockHeight) @@ -845,11 +848,11 @@ func TestNotificationCancellation(t *testing.T) { t.Fatalf("unable to add edge: %v", err) } - if err := ctx.builder.AddNode(node1); err != nil { + if err := ctx.builder.AddNode(ctxb, node1); err != nil { t.Fatalf("unable to add node: %v", err) } - if err := ctx.builder.AddNode(node2); err != nil { + if err := ctx.builder.AddNode(ctxb, node2); err != nil { t.Fatalf("unable to add node: %v", err) } diff --git a/lnrpc/devrpc/dev_server.go b/lnrpc/devrpc/dev_server.go index 71f51b085..a28a2500a 100644 --- a/lnrpc/devrpc/dev_server.go +++ b/lnrpc/devrpc/dev_server.go @@ -260,7 +260,7 @@ func (s *Server) ImportGraph(ctx context.Context, return nil, err } - if err := graphDB.AddLightningNode(node); err != nil { + if err := graphDB.AddLightningNode(ctx, node); err != nil { return nil, fmt.Errorf("unable to add node %v: %w", rpcNode.PubKey, err) } diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index 361fb83d4..0c483f0d9 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -2,6 +2,7 @@ package routing import ( "bytes" + "context" "crypto/sha256" "encoding/hex" "encoding/json" @@ -181,6 +182,8 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph, func parseTestGraph(t *testing.T, useCache bool, path string) ( *testGraphInstance, error) { + ctx := context.Background() + graphJSON, err := os.ReadFile(path) if err != nil { return nil, err @@ -290,7 +293,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) ( // With the node fully parsed, add it as a vertex within the // graph. - if err := graph.AddLightningNode(dbNode); err != nil { + if err := graph.AddLightningNode(ctx, dbNode); err != nil { return nil, err } } @@ -515,6 +518,8 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, testChannels []*testChannel, source string, sourceFeatureBits ...lnwire.FeatureBit) (*testGraphInstance, error) { + ctx := context.Background() + // We'll use this fake address for the IP address of all the nodes in // our tests. This value isn't needed for path finding so it doesn't // need to be unique. @@ -566,7 +571,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, // With the node fully parsed, add it as a vertex within the // graph. - if err := graph.AddLightningNode(dbNode); err != nil { + if err := graph.AddLightningNode(ctx, dbNode); err != nil { return nil, err } diff --git a/routing/router_test.go b/routing/router_test.go index 0f81bea37..a2647800d 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -2,6 +2,7 @@ package routing import ( "bytes" + "context" "fmt" "image/color" "math" @@ -2706,6 +2707,7 @@ func TestNewRouteRequest(t *testing.T) { // announcements for the channel vertexes to be able to use the channel. func TestAddEdgeUnknownVertexes(t *testing.T) { t.Parallel() + ctxb := context.Background() const startingBlockHeight = 101 ctx := createTestCtxFromFile(t, startingBlockHeight, basicGraphFilePath) @@ -2879,7 +2881,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) { } copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed()) - require.NoError(t, ctx.graph.AddLightningNode(n1)) + require.NoError(t, ctx.graph.AddLightningNode(ctxb, n1)) n2 := &models.LightningNode{ HaveNodeAnnouncement: true, @@ -2892,7 +2894,7 @@ func TestAddEdgeUnknownVertexes(t *testing.T) { } copy(n2.PubKeyBytes[:], priv2.PubKey().SerializeCompressed()) - require.NoError(t, ctx.graph.AddLightningNode(n2)) + require.NoError(t, ctx.graph.AddLightningNode(ctxb, n2)) // Should still be able to find the route, and the info should be // updated.