diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 3b1d24b90..2f390da5b 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -2443,7 +2443,7 @@ func (d *AuthenticatedGossiper) handleNodeAnnouncement(ctx context.Context, // We'll quickly ask the router if it already has a newer update for // this node so we can skip validating signatures if not required. - if d.cfg.Graph.IsStaleNode(nodeAnn.NodeID, timestamp) { + if d.cfg.Graph.IsStaleNode(ctx, nodeAnn.NodeID, timestamp) { log.Debugf("Skipped processing stale node: %x", nodeAnn.NodeID) nMsg.err <- nil return nil, true diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index 4f8836286..fb219f41c 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -308,7 +308,9 @@ func (r *mockGraphSource) FetchLightningNode(_ context.Context, // IsStaleNode returns true if the graph source has a node announcement for the // target node with a more recent timestamp. -func (r *mockGraphSource) IsStaleNode(nodePub route.Vertex, timestamp time.Time) bool { +func (r *mockGraphSource) IsStaleNode(_ context.Context, + nodePub route.Vertex, timestamp time.Time) bool { + r.mu.Lock() defer r.mu.Unlock() diff --git a/graph/builder.go b/graph/builder.go index 0eab22033..ce8dff179 100644 --- a/graph/builder.go +++ b/graph/builder.go @@ -865,7 +865,7 @@ func (b *Builder) updateGraphWithClosedChannels( // timestamp. ErrIgnored will be returned if we already have the node, and // ErrOutdated will be returned if we have a timestamp that's after the new // timestamp. -func (b *Builder) assertNodeAnnFreshness(node route.Vertex, +func (b *Builder) assertNodeAnnFreshness(ctx context.Context, node route.Vertex, msgTimestamp time.Time) error { // If we are not already aware of this node, it means that we don't @@ -873,7 +873,7 @@ func (b *Builder) assertNodeAnnFreshness(node route.Vertex, // node announcements, we will ignore such nodes. If we do know about // this node, check that this update brings info newer than what we // already have. - lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(node) + lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(ctx, node) if err != nil { return errors.Errorf("unable to query for the "+ "existence of node: %v", err) @@ -996,7 +996,7 @@ func (b *Builder) addNode(ctx context.Context, node *models.LightningNode, // Before we add the node to the database, we'll check to see if the // announcement is "fresh" or not. If it isn't, then we'll return an // error. - err := b.assertNodeAnnFreshness(node.PubKeyBytes, node.LastUpdate) + err := b.assertNodeAnnFreshness(ctx, node.PubKeyBytes, node.LastUpdate) if err != nil { return err } @@ -1306,12 +1306,12 @@ func (b *Builder) AddProof(chanID lnwire.ShortChannelID, // target node with a more recent timestamp. // // NOTE: This method is part of the ChannelGraphSource interface. -func (b *Builder) IsStaleNode(node route.Vertex, +func (b *Builder) IsStaleNode(ctx context.Context, node route.Vertex, timestamp time.Time) bool { // If our attempt to assert that the node announcement is fresh fails, // then we know that this is actually a stale announcement. - err := b.assertNodeAnnFreshness(node, timestamp) + err := b.assertNodeAnnFreshness(ctx, node, timestamp) if err != nil { log.Debugf("Checking stale node %x got %v", node, err) return true diff --git a/graph/builder_test.go b/graph/builder_test.go index ca46671ca..8795fc857 100644 --- a/graph/builder_test.go +++ b/graph/builder_test.go @@ -1013,6 +1013,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) { // node announcements. func TestIsStaleNode(t *testing.T) { t.Parallel() + ctxb := context.Background() const startingBlockHeight = 101 ctx := createTestCtxSingleNode(t, startingBlockHeight) @@ -1053,7 +1054,7 @@ func TestIsStaleNode(t *testing.T) { // Before we add the node, if we query for staleness, we should get // false, as we haven't added the full node. updateTimeStamp := time.Unix(123, 0) - if ctx.builder.IsStaleNode(pub1, updateTimeStamp) { + if ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) { t.Fatalf("incorrectly detected node as stale") } @@ -1075,14 +1076,14 @@ func TestIsStaleNode(t *testing.T) { // If we use the same timestamp and query for staleness, we should get // true. - if !ctx.builder.IsStaleNode(pub1, updateTimeStamp) { + if !ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) { t.Fatalf("failure to detect stale node update") } // If we update the timestamp and once again query for staleness, it // should report false. newTimeStamp := time.Unix(1234, 0) - if ctx.builder.IsStaleNode(pub1, newTimeStamp) { + if ctx.builder.IsStaleNode(ctxb, pub1, newTimeStamp) { t.Fatalf("incorrectly detected node as stale") } } diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 7e2bb653b..25409df09 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -135,7 +135,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) { dbNode, err := graph.FetchLightningNode(ctx, testPub) require.NoError(t, err, "unable to locate node") - _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes) + _, exists, err := graph.HasLightningNode(ctx, dbNode.PubKeyBytes) if err != nil { t.Fatalf("unable to query for node: %v", err) } else if !exists { @@ -288,7 +288,7 @@ func TestPartialNode(t *testing.T) { dbNode2, err := graph.FetchLightningNode(ctx, pubKey2) require.NoError(t, err) - _, exists, err := graph.HasLightningNode(dbNode1.PubKeyBytes) + _, exists, err := graph.HasLightningNode(ctx, dbNode1.PubKeyBytes) require.NoError(t, err) require.True(t, exists) @@ -302,7 +302,7 @@ func TestPartialNode(t *testing.T) { } compareNodes(t, expectedNode1, dbNode1) - _, exists, err = graph.HasLightningNode(dbNode2.PubKeyBytes) + _, exists, err = graph.HasLightningNode(ctx, dbNode2.PubKeyBytes) require.NoError(t, err) require.True(t, exists) diff --git a/graph/db/interfaces.go b/graph/db/interfaces.go index a03e0a4bf..9f5eb6c8f 100644 --- a/graph/db/interfaces.go +++ b/graph/db/interfaces.go @@ -134,8 +134,8 @@ type V1Store interface { //nolint:interfacebloat // database, a timestamp of when the data for the node was lasted // updated is returned along with a true boolean. Otherwise, an empty // time.Time is returned with a false boolean. - HasLightningNode(nodePub [33]byte) (time.Time, bool, - error) + HasLightningNode(ctx context.Context, nodePub [33]byte) (time.Time, + bool, error) // IsPublicNode is a helper method that determines whether the node with // the given public key is seen as a public node in the graph from the diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 7212511f7..be8ab2d9d 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -3065,8 +3065,8 @@ func (c *KVStore) fetchLightningNode(tx kvdb.RTx, // timestamp of when the data for the node was lasted updated is returned along // with a true boolean. Otherwise, an empty time.Time is returned with a false // boolean. -func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool, - error) { +func (c *KVStore) HasLightningNode(_ context.Context, + nodePub [33]byte) (time.Time, bool, error) { var ( updateTime time.Time diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index a154fccbd..1fd68a7f9 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -217,10 +217,8 @@ func (s *SQLStore) FetchLightningNode(ctx context.Context, // boolean. // // NOTE: part of the V1Store interface. -func (s *SQLStore) HasLightningNode(pubKey [33]byte) (time.Time, bool, - error) { - - ctx := context.TODO() +func (s *SQLStore) HasLightningNode(ctx context.Context, + pubKey [33]byte) (time.Time, bool, error) { var ( exists bool diff --git a/graph/interfaces.go b/graph/interfaces.go index e54795a98..e9f894041 100644 --- a/graph/interfaces.go +++ b/graph/interfaces.go @@ -46,7 +46,8 @@ type ChannelGraphSource interface { // for the target node with a more recent timestamp. This method will // also return true if we don't have an active channel announcement for // the target node. - IsStaleNode(node route.Vertex, timestamp time.Time) bool + IsStaleNode(ctx context.Context, node route.Vertex, + timestamp time.Time) bool // IsPublicNode determines whether the given vertex is seen as a public // node in the graph from the graph's source node's point of view. @@ -238,7 +239,8 @@ type DB interface { // database, a timestamp of when the data for the node was lasted // updated is returned along with a true boolean. Otherwise, an empty // time.Time is returned with a false boolean. - HasLightningNode(nodePub [33]byte) (time.Time, bool, error) + HasLightningNode(ctx context.Context, nodePub [33]byte) (time.Time, + bool, error) // FetchLightningNode attempts to look up a target node by its identity // public key. If the node isn't found in the database, then diff --git a/invoices/testdata/channel.db b/invoices/testdata/channel.db deleted file mode 100644 index 69397f529..000000000 Binary files a/invoices/testdata/channel.db and /dev/null differ diff --git a/routing/router_test.go b/routing/router_test.go index 4a6bebdce..04f42c83d 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -2719,11 +2719,11 @@ func TestAddEdgeUnknownVertexes(t *testing.T) { copy(pub2[:], priv2.PubKey().SerializeCompressed()) // The two nodes we are about to add should not exist yet. - _, exists1, err := ctx.graph.HasLightningNode(pub1) + _, exists1, err := ctx.graph.HasLightningNode(ctxb, pub1) require.NoError(t, err, "unable to query graph") require.False(t, exists1) - _, exists2, err := ctx.graph.HasLightningNode(pub2) + _, exists2, err := ctx.graph.HasLightningNode(ctxb, pub2) require.NoError(t, err, "unable to query graph") require.False(t, exists2) @@ -2779,11 +2779,11 @@ func TestAddEdgeUnknownVertexes(t *testing.T) { // After adding the edge between the two previously unknown nodes, they // should have been added to the graph. - _, exists1, err = ctx.graph.HasLightningNode(pub1) + _, exists1, err = ctx.graph.HasLightningNode(ctxb, pub1) require.NoError(t, err, "unable to query graph") require.True(t, exists1) - _, exists2, err = ctx.graph.HasLightningNode(pub2) + _, exists2, err = ctx.graph.HasLightningNode(ctxb, pub2) require.NoError(t, err, "unable to query graph") require.True(t, exists2) diff --git a/rpcserver.go b/rpcserver.go index f3a95a77b..63ccfcbd9 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1755,7 +1755,7 @@ func (r *rpcServer) VerifyMessage(ctx context.Context, // // TODO(phlip9): Require valid nodes to have capital in active channels. graph := r.server.graphDB - _, active, err := graph.HasLightningNode(pub) + _, active, err := graph.HasLightningNode(ctx, pub) if err != nil { return nil, fmt.Errorf("failed to query graph: %w", err) }