graph/db: split HasNode into two methods

HasNode currently is very v1 specific since it returns a time.Time
timestamp which is specific to V1 node announcements. However, it is
mostly only used for the "exists" return value. So here we split it up
into HasNode which just checks existence and HasV1Node which retains the
same behavaiour as before.
This commit is contained in:
Elle Mouton committed 2026-02-13 07:21:20 +02:00
1 parent 5a3c013e91
commit 97143a74ab
8 files changed
+100 -25

No files matched your search

+1 -1
View File
@@ -874,7 +874,7 @@ func (b *Builder) assertNodeAnnFreshness(ctx context.Context, 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.HasNode(ctx, node) lastUpdate, exists, err := b.cfg.Graph.HasV1Node(ctx, node)
if err != nil { if err != nil {
return fmt.Errorf("unable to query for the "+ return fmt.Errorf("unable to query for the "+
"existence of node: %w", err) "existence of node: %w", err)
+11 -2
View File
@@ -664,10 +664,19 @@ func (c *ChannelGraph) FetchNode(ctx context.Context,
return c.db.FetchNode(ctx, nodePub) return c.db.FetchNode(ctx, nodePub)
} }
// HasNode determines if the graph has a vertex identified by the target node. // HasV1Node determines if the graph has a vertex identified by the target node
func (c *ChannelGraph) HasNode(ctx context.Context, // in the V1 graph.
func (c *ChannelGraph) HasV1Node(ctx context.Context,
nodePub [33]byte) (time.Time, bool, error) { nodePub [33]byte) (time.Time, bool, error) {
return c.db.HasV1Node(ctx, nodePub)
}
// HasNode determines if the graph has a vertex identified by the target node
// in the V1 graph.
func (c *ChannelGraph) HasNode(ctx context.Context, nodePub [33]byte) (bool,
error) {
return c.db.HasNode(ctx, nodePub) return c.db.HasNode(ctx, nodePub)
} }
+3 -3
View File
@@ -138,7 +138,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
dbNode, err := graph.FetchNode(ctx, testPub) dbNode, err := graph.FetchNode(ctx, testPub)
require.NoError(t, err, "unable to locate node") require.NoError(t, err, "unable to locate node")
_, exists, err := graph.HasNode(ctx, dbNode.PubKeyBytes) exists, err := graph.HasNode(ctx, dbNode.PubKeyBytes)
require.NoError(t, err) require.NoError(t, err)
require.True(t, exists) require.True(t, exists)
@@ -309,7 +309,7 @@ func TestPartialNode(t *testing.T) {
dbNode2, err := graph.FetchNode(ctx, pubKey2) dbNode2, err := graph.FetchNode(ctx, pubKey2)
require.NoError(t, err) require.NoError(t, err)
_, exists, err := graph.HasNode(ctx, dbNode1.PubKeyBytes) exists, err := graph.HasNode(ctx, dbNode1.PubKeyBytes)
require.NoError(t, err) require.NoError(t, err)
require.True(t, exists) require.True(t, exists)
@@ -318,7 +318,7 @@ func TestPartialNode(t *testing.T) {
expectedNode1 := models.NewV1ShellNode(pubKey1) expectedNode1 := models.NewV1ShellNode(pubKey1)
compareNodes(t, expectedNode1, dbNode1) compareNodes(t, expectedNode1, dbNode1)
_, exists, err = graph.HasNode(ctx, dbNode2.PubKeyBytes) exists, err = graph.HasNode(ctx, dbNode2.PubKeyBytes)
require.NoError(t, err) require.NoError(t, err)
require.True(t, exists) require.True(t, exists)
+12 -5
View File
@@ -119,12 +119,19 @@ type Store interface { //nolint:interfacebloat
FetchNode(ctx context.Context, nodePub route.Vertex) (*models.Node, FetchNode(ctx context.Context, nodePub route.Vertex) (*models.Node,
error) error)
// HasV1Node determines if the graph has a vertex identified by
// the target node identity public key in the V1 graph. If the node
// exists in the 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.
// This is specific to the V1 graph since only V1 node announcements
// use timestamps for their latest update timestamp.
HasV1Node(ctx context.Context, nodePub [33]byte) (time.Time, bool,
error)
// HasNode determines if the graph has a vertex identified by // HasNode determines if the graph has a vertex identified by
// the target node identity public key. If the node exists in the // the target node identity public key.
// database, a timestamp of when the data for the node was lasted HasNode(ctx context.Context, nodePub [33]byte) (bool, error)
// updated is returned along with a true boolean. Otherwise, an empty
// time.Time is returned with a false boolean.
HasNode(ctx context.Context, nodePub [33]byte) (time.Time, 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
+38 -6
View File
@@ -3433,11 +3433,12 @@ func (c *KVStore) fetchLightningNode(tx kvdb.RTx,
return node, nil return node, nil
} }
// HasNode determines if the graph has a vertex identified by the target node // HasV1Node determines if the graph has a vertex identified by the
// identity public key. If the node exists in the database, a timestamp of when // target node identity public key. If the node exists in the database, a
// the data for the node was lasted updated is returned along with a true // timestamp of when the data for the node was lasted updated is returned along
// boolean. Otherwise, an empty time.Time is returned with a false boolean. // with a true boolean. Otherwise, an empty time.Time is returned with a false
func (c *KVStore) HasNode(_ context.Context, // boolean.
func (c *KVStore) HasV1Node(_ context.Context,
nodePub [33]byte) (time.Time, bool, error) { nodePub [33]byte) (time.Time, bool, error) {
var ( var (
@@ -3457,7 +3458,6 @@ func (c *KVStore) HasNode(_ context.Context,
// exit early. // exit early.
nodeBytes := nodes.Get(nodePub[:]) nodeBytes := nodes.Get(nodePub[:])
if nodeBytes == nil { if nodeBytes == nil {
exists = false
return nil return nil
} }
@@ -3485,6 +3485,38 @@ func (c *KVStore) HasNode(_ context.Context,
return updateTime, exists, nil return updateTime, exists, nil
} }
// HasNode determines if the graph has a vertex identified by the target node
// identity public key.
func (c *KVStore) HasNode(_ context.Context, nodePub [33]byte) (bool, error) {
var exists bool
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
// First grab the nodes bucket which stores the mapping from
// pubKey to node information.
nodes := tx.ReadBucket(nodeBucket)
if nodes == nil {
return ErrGraphNotFound
}
// If a key for this serialized public key isn't found, we can
// exit early.
nodeBytes := nodes.Get(nodePub[:])
if nodeBytes == nil {
return nil
}
exists = true
return nil
}, func() {
exists = false
})
if err != nil {
return exists, err
}
return exists, nil
}
// nodeTraversal is used to traverse all channels of a node given by its // nodeTraversal is used to traverse all channels of a node given by its
// public key and passes channel information into the specified callback. // public key and passes channel information into the specified callback.
// //
+29 -2
View File
@@ -53,6 +53,7 @@ type SQLQueries interface {
DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error)
DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error) DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error)
DeleteNode(ctx context.Context, id int64) error DeleteNode(ctx context.Context, id int64) error
NodeExists(ctx context.Context, arg sqlc.NodeExistsParams) (bool, error)
GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]sqlc.GraphNodeExtraType, error) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]sqlc.GraphNodeExtraType, error)
GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]sqlc.GraphNodeExtraType, error) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]sqlc.GraphNodeExtraType, error)
@@ -283,14 +284,14 @@ func (s *SQLStore) FetchNode(ctx context.Context,
return node, nil return node, nil
} }
// HasNode determines if the graph has a vertex identified by the // HasV1Node determines if the graph has a vertex identified by the
// target node identity public key. If the node exists in the database, a // target node identity public key. If the node exists in the database, a
// 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.
// //
// NOTE: part of the Store interface. // NOTE: part of the Store interface.
func (s *SQLStore) HasNode(ctx context.Context, func (s *SQLStore) HasV1Node(ctx context.Context,
pubKey [33]byte) (time.Time, bool, error) { pubKey [33]byte) (time.Time, bool, error) {
var ( var (
@@ -326,6 +327,32 @@ func (s *SQLStore) HasNode(ctx context.Context,
return lastUpdate, exists, nil return lastUpdate, exists, nil
} }
// HasNode determines if the graph has a vertex identified by the
// target node identity public key.
//
// NOTE: part of the Store interface.
func (s *SQLStore) HasNode(ctx context.Context, pubKey [33]byte) (bool, error) {
var (
v = lnwire.GossipVersion1
exists bool
)
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
var err error
exists, err = db.NodeExists(ctx, sqlc.NodeExistsParams{
Version: int16(v),
PubKey: pubKey[:],
})
return err
}, sqldb.NoOpReset)
if err != nil {
return false, fmt.Errorf("unable to check if node (%x) "+
"exists: %w", pubKey, err)
}
return exists, nil
}
// AddrsForNode returns all known addresses for the target node public key // AddrsForNode returns all known addresses for the target node public key
// that the graph DB is aware of. The returned boolean indicates if the // that the graph DB is aware of. The returned boolean indicates if the
// given node is unknown to the graph DB or not. // given node is unknown to the graph DB or not.
+4 -4
View File
@@ -2717,11 +2717,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.HasNode(ctxb, pub1) exists1, err := ctx.graph.HasNode(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.HasNode(ctxb, pub2) exists2, err := ctx.graph.HasNode(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)
@@ -2778,11 +2778,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.HasNode(ctxb, pub1) exists1, err = ctx.graph.HasNode(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.HasNode(ctxb, pub2) exists2, err = ctx.graph.HasNode(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)
+2 -2
View File
@@ -746,7 +746,7 @@ func (r *rpcServer) addDeps(ctx context.Context, s *server,
return info.NodeKey1Bytes, info.NodeKey2Bytes, nil return info.NodeKey1Bytes, info.NodeKey2Bytes, nil
}, },
HasNode: func(nodePub route.Vertex) (bool, error) { HasNode: func(nodePub route.Vertex) (bool, error) {
_, exists, err := graph.HasNode(ctx, nodePub) exists, err := graph.HasNode(ctx, nodePub)
return exists, err return exists, err
}, },
@@ -1822,7 +1822,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.HasNode(ctx, pub) active, err := graph.HasNode(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)
} }