From a99604c7d7770f4ceaf48539a1eb17a886f3c8d5 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 19 Nov 2025 18:01:27 +0200 Subject: [PATCH] sqldb/sqlc: add IsPublicV2Node query Add SQL query to determine if a node has public v2 channels. Unlike v1 which requires all four individual signatures, v2 channels are considered public when the single aggregated signature is present. --- sqldb/sqlc/graph.sql.go | 30 ++++++++++++++++++++++++++++++ sqldb/sqlc/querier.go | 1 + sqldb/sqlc/queries/graph.sql | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/sqldb/sqlc/graph.sql.go b/sqldb/sqlc/graph.sql.go index c08ba967f..135f00d6e 100644 --- a/sqldb/sqlc/graph.sql.go +++ b/sqldb/sqlc/graph.sql.go @@ -2751,6 +2751,36 @@ func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, erro return exists, err } +const isPublicV2Node = `-- name: IsPublicV2Node :one +SELECT EXISTS ( + SELECT 1 + FROM graph_channels c + JOIN graph_nodes n ON n.id = c.node_id_1 + -- NOTE: we hard-code the version here since the clauses + -- here that determine if a node is public is specific + -- to the V2 gossip protocol. + WHERE c.version = 2 + AND c.signature IS NOT NULL + AND n.pub_key = $1 + + UNION ALL + + SELECT 1 + FROM graph_channels c + JOIN graph_nodes n ON n.id = c.node_id_2 + WHERE c.version = 2 + AND COALESCE(length(c.signature), 0) > 0 + AND n.pub_key = $1 +) +` + +func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) { + row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey) + var exists bool + err := row.Scan(&exists) + return exists, err +} + const isZombieChannel = `-- name: IsZombieChannel :one SELECT EXISTS ( SELECT 1 diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 05bf91877..d26f845d0 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -117,6 +117,7 @@ type Querier interface { InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) + IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) diff --git a/sqldb/sqlc/queries/graph.sql b/sqldb/sqlc/queries/graph.sql index 4f71fc8fa..1817fbf3b 100644 --- a/sqldb/sqlc/queries/graph.sql +++ b/sqldb/sqlc/queries/graph.sql @@ -112,6 +112,28 @@ SELECT EXISTS ( AND n.pub_key = $1 ); +-- name: IsPublicV2Node :one +SELECT EXISTS ( + SELECT 1 + FROM graph_channels c + JOIN graph_nodes n ON n.id = c.node_id_1 + -- NOTE: we hard-code the version here since the clauses + -- here that determine if a node is public is specific + -- to the V2 gossip protocol. + WHERE c.version = 2 + AND c.signature IS NOT NULL + AND n.pub_key = $1 + + UNION ALL + + SELECT 1 + FROM graph_channels c + JOIN graph_nodes n ON n.id = c.node_id_2 + WHERE c.version = 2 + AND COALESCE(length(c.signature), 0) > 0 + AND n.pub_key = $1 +); + -- name: DeleteUnconnectedNodes :many DELETE FROM graph_nodes WHERE