graph/db: thread context through to HasLightningNode

This commit is contained in:
Elle Mouton committed 2025-06-17 19:20:56 +02:00
1 parent 66c5a97202
commit dc6259fcc3
12 files changed
+31 -28

No files matched your search

+1 -1
View File
@@ -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 // 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. // 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) log.Debugf("Skipped processing stale node: %x", nodeAnn.NodeID)
nMsg.err <- nil nMsg.err <- nil
return nil, true return nil, true
+3 -1
View File
@@ -308,7 +308,9 @@ func (r *mockGraphSource) FetchLightningNode(_ context.Context,
// IsStaleNode returns true if the graph source has a node announcement for the // IsStaleNode returns true if the graph source has a node announcement for the
// target node with a more recent timestamp. // 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() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
+5 -5
View File
@@ -865,7 +865,7 @@ func (b *Builder) updateGraphWithClosedChannels(
// timestamp. ErrIgnored will be returned if we already have the node, and // 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 // ErrOutdated will be returned if we have a timestamp that's after the new
// timestamp. // timestamp.
func (b *Builder) assertNodeAnnFreshness(node route.Vertex, func (b *Builder) assertNodeAnnFreshness(ctx context.Context, node route.Vertex,
msgTimestamp time.Time) error { msgTimestamp time.Time) error {
// If we are not already aware of this node, it means that we don't // 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 // node announcements, we will ignore such nodes. If we do know about
// this node, check that this update brings info newer than what we // this node, check that this update brings info newer than what we
// already have. // already have.
lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(node) lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(ctx, node)
if err != nil { if err != nil {
return errors.Errorf("unable to query for the "+ return errors.Errorf("unable to query for the "+
"existence of node: %v", err) "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 // 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 // announcement is "fresh" or not. If it isn't, then we'll return an
// error. // error.
err := b.assertNodeAnnFreshness(node.PubKeyBytes, node.LastUpdate) err := b.assertNodeAnnFreshness(ctx, node.PubKeyBytes, node.LastUpdate)
if err != nil { if err != nil {
return err return err
} }
@@ -1306,12 +1306,12 @@ func (b *Builder) AddProof(chanID lnwire.ShortChannelID,
// target node with a more recent timestamp. // target node with a more recent timestamp.
// //
// NOTE: This method is part of the ChannelGraphSource interface. // 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 { timestamp time.Time) bool {
// If our attempt to assert that the node announcement is fresh fails, // If our attempt to assert that the node announcement is fresh fails,
// then we know that this is actually a stale announcement. // then we know that this is actually a stale announcement.
err := b.assertNodeAnnFreshness(node, timestamp) err := b.assertNodeAnnFreshness(ctx, node, timestamp)
if err != nil { if err != nil {
log.Debugf("Checking stale node %x got %v", node, err) log.Debugf("Checking stale node %x got %v", node, err)
return true return true
+4 -3
View File
@@ -1013,6 +1013,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
// node announcements. // node announcements.
func TestIsStaleNode(t *testing.T) { func TestIsStaleNode(t *testing.T) {
t.Parallel() t.Parallel()
ctxb := context.Background()
const startingBlockHeight = 101 const startingBlockHeight = 101
ctx := createTestCtxSingleNode(t, startingBlockHeight) 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 // Before we add the node, if we query for staleness, we should get
// false, as we haven't added the full node. // false, as we haven't added the full node.
updateTimeStamp := time.Unix(123, 0) 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") 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 // If we use the same timestamp and query for staleness, we should get
// true. // true.
if !ctx.builder.IsStaleNode(pub1, updateTimeStamp) { if !ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) {
t.Fatalf("failure to detect stale node update") t.Fatalf("failure to detect stale node update")
} }
// If we update the timestamp and once again query for staleness, it // If we update the timestamp and once again query for staleness, it
// should report false. // should report false.
newTimeStamp := time.Unix(1234, 0) 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") t.Fatalf("incorrectly detected node as stale")
} }
} }
+3 -3
View File
@@ -135,7 +135,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
dbNode, err := graph.FetchLightningNode(ctx, testPub) dbNode, err := graph.FetchLightningNode(ctx, testPub)
require.NoError(t, err, "unable to locate node") require.NoError(t, err, "unable to locate node")
_, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes) _, exists, err := graph.HasLightningNode(ctx, dbNode.PubKeyBytes)
if err != nil { if err != nil {
t.Fatalf("unable to query for node: %v", err) t.Fatalf("unable to query for node: %v", err)
} else if !exists { } else if !exists {
@@ -288,7 +288,7 @@ func TestPartialNode(t *testing.T) {
dbNode2, err := graph.FetchLightningNode(ctx, pubKey2) dbNode2, err := graph.FetchLightningNode(ctx, pubKey2)
require.NoError(t, err) require.NoError(t, err)
_, exists, err := graph.HasLightningNode(dbNode1.PubKeyBytes) _, exists, err := graph.HasLightningNode(ctx, dbNode1.PubKeyBytes)
require.NoError(t, err) require.NoError(t, err)
require.True(t, exists) require.True(t, exists)
@@ -302,7 +302,7 @@ func TestPartialNode(t *testing.T) {
} }
compareNodes(t, expectedNode1, dbNode1) compareNodes(t, expectedNode1, dbNode1)
_, exists, err = graph.HasLightningNode(dbNode2.PubKeyBytes) _, exists, err = graph.HasLightningNode(ctx, dbNode2.PubKeyBytes)
require.NoError(t, err) require.NoError(t, err)
require.True(t, exists) require.True(t, exists)
+2 -2
View File
@@ -134,8 +134,8 @@ type V1Store interface { //nolint:interfacebloat
// database, a timestamp of when the data for the node was lasted // database, a timestamp of when the data for the node was lasted
// updated is returned along with a true boolean. Otherwise, an empty // updated is returned along with a true boolean. Otherwise, an empty
// time.Time is returned with a false boolean. // time.Time is returned with a false boolean.
HasLightningNode(nodePub [33]byte) (time.Time, bool, HasLightningNode(ctx context.Context, nodePub [33]byte) (time.Time,
error) bool, error)
// IsPublicNode is a helper method that determines whether the node with // 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 // the given public key is seen as a public node in the graph from the
+2 -2
View File
@@ -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 // 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 // with a true boolean. Otherwise, an empty time.Time is returned with a false
// boolean. // boolean.
func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool, func (c *KVStore) HasLightningNode(_ context.Context,
error) { nodePub [33]byte) (time.Time, bool, error) {
var ( var (
updateTime time.Time updateTime time.Time
+2 -4
View File
@@ -217,10 +217,8 @@ func (s *SQLStore) FetchLightningNode(ctx context.Context,
// boolean. // boolean.
// //
// NOTE: part of the V1Store interface. // NOTE: part of the V1Store interface.
func (s *SQLStore) HasLightningNode(pubKey [33]byte) (time.Time, bool, func (s *SQLStore) HasLightningNode(ctx context.Context,
error) { pubKey [33]byte) (time.Time, bool, error) {
ctx := context.TODO()
var ( var (
exists bool exists bool
+4 -2
View File
@@ -46,7 +46,8 @@ type ChannelGraphSource interface {
// for the target node with a more recent timestamp. This method will // 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 // also return true if we don't have an active channel announcement for
// the target node. // 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 // 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. // 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 // database, a timestamp of when the data for the node was lasted
// updated is returned along with a true boolean. Otherwise, an empty // updated is returned along with a true boolean. Otherwise, an empty
// time.Time is returned with a false boolean. // 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 // FetchLightningNode attempts to look up a target node by its identity
// public key. If the node isn't found in the database, then // public key. If the node isn't found in the database, then
BIN
View File
Binary file not shown.
+4 -4
View File
@@ -2719,11 +2719,11 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
copy(pub2[:], priv2.PubKey().SerializeCompressed()) copy(pub2[:], priv2.PubKey().SerializeCompressed())
// The two nodes we are about to add should not exist yet. // 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.NoError(t, err, "unable to query graph")
require.False(t, exists1) 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.NoError(t, err, "unable to query graph")
require.False(t, exists2) require.False(t, exists2)
@@ -2779,11 +2779,11 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
// After adding the edge between the two previously unknown nodes, they // After adding the edge between the two previously unknown nodes, they
// should have been added to the graph. // 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.NoError(t, err, "unable to query graph")
require.True(t, exists1) 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.NoError(t, err, "unable to query graph")
require.True(t, exists2) require.True(t, exists2)
+1 -1
View File
@@ -1755,7 +1755,7 @@ func (r *rpcServer) VerifyMessage(ctx context.Context,
// //
// TODO(phlip9): Require valid nodes to have capital in active channels. // TODO(phlip9): Require valid nodes to have capital in active channels.
graph := r.server.graphDB graph := r.server.graphDB
_, active, err := graph.HasLightningNode(pub) _, active, err := graph.HasLightningNode(ctx, pub)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to query graph: %w", err) return nil, fmt.Errorf("failed to query graph: %w", err)
} }