Skip to content

Commit dc6259f

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

File tree

12 files changed

+31
-28
lines changed

12 files changed

+31
-28
lines changed

discovery/gossiper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2443,7 +2443,7 @@ func (d *AuthenticatedGossiper) handleNodeAnnouncement(ctx context.Context,
24432443

24442444
// We'll quickly ask the router if it already has a newer update for
24452445
// this node so we can skip validating signatures if not required.
2446-
if d.cfg.Graph.IsStaleNode(nodeAnn.NodeID, timestamp) {
2446+
if d.cfg.Graph.IsStaleNode(ctx, nodeAnn.NodeID, timestamp) {
24472447
log.Debugf("Skipped processing stale node: %x", nodeAnn.NodeID)
24482448
nMsg.err <- nil
24492449
return nil, true

discovery/gossiper_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ func (r *mockGraphSource) FetchLightningNode(_ context.Context,
308308

309309
// IsStaleNode returns true if the graph source has a node announcement for the
310310
// target node with a more recent timestamp.
311-
func (r *mockGraphSource) IsStaleNode(nodePub route.Vertex, timestamp time.Time) bool {
311+
func (r *mockGraphSource) IsStaleNode(_ context.Context,
312+
nodePub route.Vertex, timestamp time.Time) bool {
313+
312314
r.mu.Lock()
313315
defer r.mu.Unlock()
314316

graph/builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,15 @@ func (b *Builder) updateGraphWithClosedChannels(
865865
// timestamp. ErrIgnored will be returned if we already have the node, and
866866
// ErrOutdated will be returned if we have a timestamp that's after the new
867867
// timestamp.
868-
func (b *Builder) assertNodeAnnFreshness(node route.Vertex,
868+
func (b *Builder) assertNodeAnnFreshness(ctx context.Context, node route.Vertex,
869869
msgTimestamp time.Time) error {
870870

871871
// If we are not already aware of this node, it means that we don't
872872
// know about any channel using this node. To avoid a DoS attack by
873873
// node announcements, we will ignore such nodes. If we do know about
874874
// this node, check that this update brings info newer than what we
875875
// already have.
876-
lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(node)
876+
lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(ctx, node)
877877
if err != nil {
878878
return errors.Errorf("unable to query for the "+
879879
"existence of node: %v", err)
@@ -996,7 +996,7 @@ func (b *Builder) addNode(ctx context.Context, node *models.LightningNode,
996996
// Before we add the node to the database, we'll check to see if the
997997
// announcement is "fresh" or not. If it isn't, then we'll return an
998998
// error.
999-
err := b.assertNodeAnnFreshness(node.PubKeyBytes, node.LastUpdate)
999+
err := b.assertNodeAnnFreshness(ctx, node.PubKeyBytes, node.LastUpdate)
10001000
if err != nil {
10011001
return err
10021002
}
@@ -1306,12 +1306,12 @@ func (b *Builder) AddProof(chanID lnwire.ShortChannelID,
13061306
// target node with a more recent timestamp.
13071307
//
13081308
// NOTE: This method is part of the ChannelGraphSource interface.
1309-
func (b *Builder) IsStaleNode(node route.Vertex,
1309+
func (b *Builder) IsStaleNode(ctx context.Context, node route.Vertex,
13101310
timestamp time.Time) bool {
13111311

13121312
// If our attempt to assert that the node announcement is fresh fails,
13131313
// then we know that this is actually a stale announcement.
1314-
err := b.assertNodeAnnFreshness(node, timestamp)
1314+
err := b.assertNodeAnnFreshness(ctx, node, timestamp)
13151315
if err != nil {
13161316
log.Debugf("Checking stale node %x got %v", node, err)
13171317
return true

graph/builder_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
10131013
// node announcements.
10141014
func TestIsStaleNode(t *testing.T) {
10151015
t.Parallel()
1016+
ctxb := context.Background()
10161017

10171018
const startingBlockHeight = 101
10181019
ctx := createTestCtxSingleNode(t, startingBlockHeight)
@@ -1053,7 +1054,7 @@ func TestIsStaleNode(t *testing.T) {
10531054
// Before we add the node, if we query for staleness, we should get
10541055
// false, as we haven't added the full node.
10551056
updateTimeStamp := time.Unix(123, 0)
1056-
if ctx.builder.IsStaleNode(pub1, updateTimeStamp) {
1057+
if ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) {
10571058
t.Fatalf("incorrectly detected node as stale")
10581059
}
10591060

@@ -1075,14 +1076,14 @@ func TestIsStaleNode(t *testing.T) {
10751076

10761077
// If we use the same timestamp and query for staleness, we should get
10771078
// true.
1078-
if !ctx.builder.IsStaleNode(pub1, updateTimeStamp) {
1079+
if !ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) {
10791080
t.Fatalf("failure to detect stale node update")
10801081
}
10811082

10821083
// If we update the timestamp and once again query for staleness, it
10831084
// should report false.
10841085
newTimeStamp := time.Unix(1234, 0)
1085-
if ctx.builder.IsStaleNode(pub1, newTimeStamp) {
1086+
if ctx.builder.IsStaleNode(ctxb, pub1, newTimeStamp) {
10861087
t.Fatalf("incorrectly detected node as stale")
10871088
}
10881089
}

graph/db/graph_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
135135
dbNode, err := graph.FetchLightningNode(ctx, testPub)
136136
require.NoError(t, err, "unable to locate node")
137137

138-
_, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes)
138+
_, exists, err := graph.HasLightningNode(ctx, dbNode.PubKeyBytes)
139139
if err != nil {
140140
t.Fatalf("unable to query for node: %v", err)
141141
} else if !exists {
@@ -288,7 +288,7 @@ func TestPartialNode(t *testing.T) {
288288
dbNode2, err := graph.FetchLightningNode(ctx, pubKey2)
289289
require.NoError(t, err)
290290

291-
_, exists, err := graph.HasLightningNode(dbNode1.PubKeyBytes)
291+
_, exists, err := graph.HasLightningNode(ctx, dbNode1.PubKeyBytes)
292292
require.NoError(t, err)
293293
require.True(t, exists)
294294

@@ -302,7 +302,7 @@ func TestPartialNode(t *testing.T) {
302302
}
303303
compareNodes(t, expectedNode1, dbNode1)
304304

305-
_, exists, err = graph.HasLightningNode(dbNode2.PubKeyBytes)
305+
_, exists, err = graph.HasLightningNode(ctx, dbNode2.PubKeyBytes)
306306
require.NoError(t, err)
307307
require.True(t, exists)
308308

graph/db/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ type V1Store interface { //nolint:interfacebloat
134134
// database, a timestamp of when the data for the node was lasted
135135
// updated is returned along with a true boolean. Otherwise, an empty
136136
// time.Time is returned with a false boolean.
137-
HasLightningNode(nodePub [33]byte) (time.Time, bool,
138-
error)
137+
HasLightningNode(ctx context.Context, nodePub [33]byte) (time.Time,
138+
bool, error)
139139

140140
// IsPublicNode is a helper method that determines whether the node with
141141
// the given public key is seen as a public node in the graph from the

graph/db/kv_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,8 @@ func (c *KVStore) fetchLightningNode(tx kvdb.RTx,
30653065
// timestamp of when the data for the node was lasted updated is returned along
30663066
// with a true boolean. Otherwise, an empty time.Time is returned with a false
30673067
// boolean.
3068-
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool,
3069-
error) {
3068+
func (c *KVStore) HasLightningNode(_ context.Context,
3069+
nodePub [33]byte) (time.Time, bool, error) {
30703070

30713071
var (
30723072
updateTime time.Time

graph/db/sql_store.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,8 @@ func (s *SQLStore) FetchLightningNode(ctx context.Context,
217217
// boolean.
218218
//
219219
// NOTE: part of the V1Store interface.
220-
func (s *SQLStore) HasLightningNode(pubKey [33]byte) (time.Time, bool,
221-
error) {
222-
223-
ctx := context.TODO()
220+
func (s *SQLStore) HasLightningNode(ctx context.Context,
221+
pubKey [33]byte) (time.Time, bool, error) {
224222

225223
var (
226224
exists bool

graph/interfaces.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ type ChannelGraphSource interface {
4646
// for the target node with a more recent timestamp. This method will
4747
// also return true if we don't have an active channel announcement for
4848
// the target node.
49-
IsStaleNode(node route.Vertex, timestamp time.Time) bool
49+
IsStaleNode(ctx context.Context, node route.Vertex,
50+
timestamp time.Time) bool
5051

5152
// IsPublicNode determines whether the given vertex is seen as a public
5253
// node in the graph from the graph's source node's point of view.
@@ -238,7 +239,8 @@ type DB interface {
238239
// database, a timestamp of when the data for the node was lasted
239240
// updated is returned along with a true boolean. Otherwise, an empty
240241
// time.Time is returned with a false boolean.
241-
HasLightningNode(nodePub [33]byte) (time.Time, bool, error)
242+
HasLightningNode(ctx context.Context, nodePub [33]byte) (time.Time,
243+
bool, error)
242244

243245
// FetchLightningNode attempts to look up a target node by its identity
244246
// public key. If the node isn't found in the database, then

invoices/testdata/channel.db

-1 MB
Binary file not shown.

0 commit comments

Comments
 (0)