graph/db: thread context through to HasLightningNode

This commit is contained in:
Elle Mouton 2025-06-17 11:49:23 +02:00
parent 66c5a97202
commit dc6259fcc3
No known key found for this signature in database
GPG key ID: D7D916376026F177
12 changed files with 31 additions and 28 deletions

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
// 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

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
// 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()

View file

@ -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

View file

@ -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")
}
}

View file

@ -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)

View file

@ -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

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
// 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

View file

@ -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

View file

@ -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

Binary file not shown.

View file

@ -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)

View file

@ -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)
}