From 3eed2f94c1a5fb29a8fc5cca5175bd1988289a4a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 24 Jul 2025 13:41:43 +0200 Subject: [PATCH 1/3] sqldb/sqlc: add index on graph_channels(version, id) This new index greatly improves the speed of the ListChannelsWithPoliciesPaginated query. --- sqldb/sqlc/migrations/000007_graph.down.sql | 1 + sqldb/sqlc/migrations/000007_graph.up.sql | 1 + 2 files changed, 2 insertions(+) diff --git a/sqldb/sqlc/migrations/000007_graph.down.sql b/sqldb/sqlc/migrations/000007_graph.down.sql index da92ccf22..b23ced950 100644 --- a/sqldb/sqlc/migrations/000007_graph.down.sql +++ b/sqldb/sqlc/migrations/000007_graph.down.sql @@ -9,6 +9,7 @@ DROP INDEX IF EXISTS graph_channels_node_id_1_idx; DROP INDEX IF EXISTS graph_channels_node_id_2_idx; DROP INDEX IF EXISTS graph_channels_unique; DROP INDEX IF EXISTS graph_channels_version_outpoint_idx; +DROP INDEX IF EXISTS graph_channels_version_id_idx; DROP INDEX IF EXISTS graph_channel_features_unique; DROP INDEX IF EXISTS graph_channel_extra_types_unique; DROP INDEX IF EXISTS graph_channel_policies_unique; diff --git a/sqldb/sqlc/migrations/000007_graph.up.sql b/sqldb/sqlc/migrations/000007_graph.up.sql index c1c69058c..1cb21be78 100644 --- a/sqldb/sqlc/migrations/000007_graph.up.sql +++ b/sqldb/sqlc/migrations/000007_graph.up.sql @@ -173,6 +173,7 @@ CREATE TABLE IF NOT EXISTS graph_channels ( -- indexes on the node_id_1 and node_id_2 columns. CREATE INDEX IF NOT EXISTS graph_channels_node_id_1_idx ON graph_channels(node_id_1); CREATE INDEX IF NOT EXISTS graph_channels_node_id_2_idx ON graph_channels(node_id_2); +CREATE INDEX IF NOT EXISTS graph_channels_version_id_idx ON graph_channels(version, id); -- A channel (identified by a short channel id) can only have one active -- channel announcement per protocol version. We also order the index by From 43db6683d21477109b9958726c003a148f51e4b8 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 28 Jul 2025 10:04:00 +0200 Subject: [PATCH 2/3] graph/db+sqldb: only fetch what is needed for graph cache Add a ListChannelsWithPoliciesForCachePaginated query that only fetches the channel/policy fields that the cache requires. --- graph/db/sql_store.go | 59 ++++++++++++---- sqldb/sqlc/graph.sql.go | 129 +++++++++++++++++++++++++++++++++++ sqldb/sqlc/querier.go | 1 + sqldb/sqlc/queries/graph.sql | 45 ++++++++++++ 4 files changed, 221 insertions(+), 13 deletions(-) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index c90efac28..d6ca33bcd 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -103,6 +103,7 @@ type SQLQueries interface { HighestSCID(ctx context.Context, version int16) ([]byte, error) ListChannelsByNodeID(ctx context.Context, arg sqlc.ListChannelsByNodeIDParams) ([]sqlc.ListChannelsByNodeIDRow, error) ListChannelsWithPoliciesPaginated(ctx context.Context, arg sqlc.ListChannelsWithPoliciesPaginatedParams) ([]sqlc.ListChannelsWithPoliciesPaginatedRow, error) + ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg sqlc.ListChannelsWithPoliciesForCachePaginatedParams) ([]sqlc.ListChannelsWithPoliciesForCachePaginatedRow, error) ListChannelsPaginated(ctx context.Context, arg sqlc.ListChannelsPaginatedParams) ([]sqlc.ListChannelsPaginatedRow, error) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg sqlc.GetChannelsByPolicyLastUpdateRangeParams) ([]sqlc.GetChannelsByPolicyLastUpdateRangeRow, error) GetChannelByOutpointWithPolicies(ctx context.Context, arg sqlc.GetChannelByOutpointWithPoliciesParams) (sqlc.GetChannelByOutpointWithPoliciesRow, error) @@ -1247,8 +1248,8 @@ func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo, ctx := context.TODO() - handleChannel := func(db SQLQueries, - row sqlc.ListChannelsWithPoliciesPaginatedRow) error { + handleChannel := func( + row sqlc.ListChannelsWithPoliciesForCachePaginatedRow) error { node1, node2, err := buildNodeVertices( row.Node1Pubkey, row.Node2Pubkey, @@ -1258,7 +1259,7 @@ func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo, } edge := buildCacheableChannelInfo( - row.GraphChannel, node1, node2, + row.Scid, row.Capacity.Int64, node1, node2, ) dbPol1, dbPol2, err := extractChannelPolicies(row) @@ -1299,8 +1300,8 @@ func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo, lastID := int64(-1) for { //nolint:ll - rows, err := db.ListChannelsWithPoliciesPaginated( - ctx, sqlc.ListChannelsWithPoliciesPaginatedParams{ + rows, err := db.ListChannelsWithPoliciesForCachePaginated( + ctx, sqlc.ListChannelsWithPoliciesForCachePaginatedParams{ Version: int16(ProtocolV1), ID: lastID, Limit: pageSize, @@ -1315,12 +1316,12 @@ func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo, } for _, row := range rows { - err := handleChannel(db, row) + err := handleChannel(row) if err != nil { return err } - lastID = row.GraphChannel.ID + lastID = row.ID } } @@ -3018,7 +3019,8 @@ func forEachNodeDirectedChannel(ctx context.Context, db SQLQueries, } edge := buildCacheableChannelInfo( - row.GraphChannel, node1, node2, + row.GraphChannel.Scid, row.GraphChannel.Capacity.Int64, + node1, node2, ) dbPol1, dbPol2, err := extractChannelPolicies(row) @@ -3321,16 +3323,15 @@ func getNodeByPubKey(ctx context.Context, db SQLQueries, } // buildCacheableChannelInfo builds a models.CachedEdgeInfo instance from the -// provided database channel row and the public keys of the two nodes -// involved in the channel. -func buildCacheableChannelInfo(dbChan sqlc.GraphChannel, node1Pub, +// provided parameters. +func buildCacheableChannelInfo(scid []byte, capacity int64, node1Pub, node2Pub route.Vertex) *models.CachedEdgeInfo { return &models.CachedEdgeInfo{ - ChannelID: byteOrder.Uint64(dbChan.Scid), + ChannelID: byteOrder.Uint64(scid), NodeKey1Bytes: node1Pub, NodeKey2Bytes: node2Pub, - Capacity: btcutil.Amount(dbChan.Capacity.Int64), + Capacity: btcutil.Amount(capacity), } } @@ -4368,6 +4369,38 @@ func extractChannelPolicies(row any) (*sqlc.GraphChannelPolicy, var policy1, policy2 *sqlc.GraphChannelPolicy switch r := row.(type) { + case sqlc.ListChannelsWithPoliciesForCachePaginatedRow: + if r.Policy1Timelock.Valid { + policy1 = &sqlc.GraphChannelPolicy{ + Timelock: r.Policy1Timelock.Int32, + FeePpm: r.Policy1FeePpm.Int64, + BaseFeeMsat: r.Policy1BaseFeeMsat.Int64, + MinHtlcMsat: r.Policy1MinHtlcMsat.Int64, + MaxHtlcMsat: r.Policy1MaxHtlcMsat, + InboundBaseFeeMsat: r.Policy1InboundBaseFeeMsat, + InboundFeeRateMilliMsat: r.Policy1InboundFeeRateMilliMsat, + Disabled: r.Policy1Disabled, + MessageFlags: r.Policy1MessageFlags, + ChannelFlags: r.Policy1ChannelFlags, + } + } + if r.Policy2Timelock.Valid { + policy2 = &sqlc.GraphChannelPolicy{ + Timelock: r.Policy2Timelock.Int32, + FeePpm: r.Policy2FeePpm.Int64, + BaseFeeMsat: r.Policy2BaseFeeMsat.Int64, + MinHtlcMsat: r.Policy2MinHtlcMsat.Int64, + MaxHtlcMsat: r.Policy2MaxHtlcMsat, + InboundBaseFeeMsat: r.Policy2InboundBaseFeeMsat, + InboundFeeRateMilliMsat: r.Policy2InboundFeeRateMilliMsat, + Disabled: r.Policy2Disabled, + MessageFlags: r.Policy2MessageFlags, + ChannelFlags: r.Policy2ChannelFlags, + } + } + + return policy1, policy2, nil + case sqlc.GetChannelsBySCIDWithPoliciesRow: if r.Policy1ID.Valid { policy1 = &sqlc.GraphChannelPolicy{ diff --git a/sqldb/sqlc/graph.sql.go b/sqldb/sqlc/graph.sql.go index 89a92927f..98b120ec0 100644 --- a/sqldb/sqlc/graph.sql.go +++ b/sqldb/sqlc/graph.sql.go @@ -2230,6 +2230,135 @@ func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPag return items, nil } +const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many +SELECT + c.id as id, + c.scid as scid, + c.capacity AS capacity, + + -- Join node pubkeys + n1.pub_key AS node1_pubkey, + n2.pub_key AS node2_pubkey, + + -- Node 1 policy + cp1.timelock AS policy_1_timelock, + cp1.fee_ppm AS policy_1_fee_ppm, + cp1.base_fee_msat AS policy_1_base_fee_msat, + cp1.min_htlc_msat AS policy_1_min_htlc_msat, + cp1.max_htlc_msat AS policy_1_max_htlc_msat, + cp1.disabled AS policy_1_disabled, + cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat, + cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat, + cp1.message_flags AS policy1_message_flags, + cp1.channel_flags AS policy1_channel_flags, + + -- Node 2 policy + cp2.timelock AS policy_2_timelock, + cp2.fee_ppm AS policy_2_fee_ppm, + cp2.base_fee_msat AS policy_2_base_fee_msat, + cp2.min_htlc_msat AS policy_2_min_htlc_msat, + cp2.max_htlc_msat AS policy_2_max_htlc_msat, + cp2.disabled AS policy_2_disabled, + cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat, + cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat, + cp2.message_flags AS policy2_message_flags, + cp2.channel_flags AS policy2_channel_flags + +FROM graph_channels c +JOIN graph_nodes n1 ON c.node_id_1 = n1.id +JOIN graph_nodes n2 ON c.node_id_2 = n2.id +LEFT JOIN graph_channel_policies cp1 + ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version +LEFT JOIN graph_channel_policies cp2 + ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version +WHERE c.version = $1 AND c.id > $2 +ORDER BY c.id +LIMIT $3 +` + +type ListChannelsWithPoliciesForCachePaginatedParams struct { + Version int16 + ID int64 + Limit int32 +} + +type ListChannelsWithPoliciesForCachePaginatedRow struct { + ID int64 + Scid []byte + Capacity sql.NullInt64 + Node1Pubkey []byte + Node2Pubkey []byte + Policy1Timelock sql.NullInt32 + Policy1FeePpm sql.NullInt64 + Policy1BaseFeeMsat sql.NullInt64 + Policy1MinHtlcMsat sql.NullInt64 + Policy1MaxHtlcMsat sql.NullInt64 + Policy1Disabled sql.NullBool + Policy1InboundBaseFeeMsat sql.NullInt64 + Policy1InboundFeeRateMilliMsat sql.NullInt64 + Policy1MessageFlags sql.NullInt16 + Policy1ChannelFlags sql.NullInt16 + Policy2Timelock sql.NullInt32 + Policy2FeePpm sql.NullInt64 + Policy2BaseFeeMsat sql.NullInt64 + Policy2MinHtlcMsat sql.NullInt64 + Policy2MaxHtlcMsat sql.NullInt64 + Policy2Disabled sql.NullBool + Policy2InboundBaseFeeMsat sql.NullInt64 + Policy2InboundFeeRateMilliMsat sql.NullInt64 + Policy2MessageFlags sql.NullInt16 + Policy2ChannelFlags sql.NullInt16 +} + +func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) { + rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ListChannelsWithPoliciesForCachePaginatedRow + for rows.Next() { + var i ListChannelsWithPoliciesForCachePaginatedRow + if err := rows.Scan( + &i.ID, + &i.Scid, + &i.Capacity, + &i.Node1Pubkey, + &i.Node2Pubkey, + &i.Policy1Timelock, + &i.Policy1FeePpm, + &i.Policy1BaseFeeMsat, + &i.Policy1MinHtlcMsat, + &i.Policy1MaxHtlcMsat, + &i.Policy1Disabled, + &i.Policy1InboundBaseFeeMsat, + &i.Policy1InboundFeeRateMilliMsat, + &i.Policy1MessageFlags, + &i.Policy1ChannelFlags, + &i.Policy2Timelock, + &i.Policy2FeePpm, + &i.Policy2BaseFeeMsat, + &i.Policy2MinHtlcMsat, + &i.Policy2MaxHtlcMsat, + &i.Policy2Disabled, + &i.Policy2InboundBaseFeeMsat, + &i.Policy2InboundFeeRateMilliMsat, + &i.Policy2MessageFlags, + &i.Policy2ChannelFlags, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 9b9d010b4..3cfb8924e 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -95,6 +95,7 @@ type Querier interface { IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) + ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) diff --git a/sqldb/sqlc/queries/graph.sql b/sqldb/sqlc/queries/graph.sql index 8551a7706..509a9434e 100644 --- a/sqldb/sqlc/queries/graph.sql +++ b/sqldb/sqlc/queries/graph.sql @@ -569,6 +569,51 @@ WHERE c.version = $1 AND c.id > $2 ORDER BY c.id LIMIT $3; +-- name: ListChannelsWithPoliciesForCachePaginated :many +SELECT + c.id as id, + c.scid as scid, + c.capacity AS capacity, + + -- Join node pubkeys + n1.pub_key AS node1_pubkey, + n2.pub_key AS node2_pubkey, + + -- Node 1 policy + cp1.timelock AS policy_1_timelock, + cp1.fee_ppm AS policy_1_fee_ppm, + cp1.base_fee_msat AS policy_1_base_fee_msat, + cp1.min_htlc_msat AS policy_1_min_htlc_msat, + cp1.max_htlc_msat AS policy_1_max_htlc_msat, + cp1.disabled AS policy_1_disabled, + cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat, + cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat, + cp1.message_flags AS policy1_message_flags, + cp1.channel_flags AS policy1_channel_flags, + + -- Node 2 policy + cp2.timelock AS policy_2_timelock, + cp2.fee_ppm AS policy_2_fee_ppm, + cp2.base_fee_msat AS policy_2_base_fee_msat, + cp2.min_htlc_msat AS policy_2_min_htlc_msat, + cp2.max_htlc_msat AS policy_2_max_htlc_msat, + cp2.disabled AS policy_2_disabled, + cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat, + cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat, + cp2.message_flags AS policy2_message_flags, + cp2.channel_flags AS policy2_channel_flags + +FROM graph_channels c +JOIN graph_nodes n1 ON c.node_id_1 = n1.id +JOIN graph_nodes n2 ON c.node_id_2 = n2.id +LEFT JOIN graph_channel_policies cp1 + ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version +LEFT JOIN graph_channel_policies cp2 + ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version +WHERE c.version = $1 AND c.id > $2 +ORDER BY c.id +LIMIT $3; + -- name: DeleteChannels :exec DELETE FROM graph_channels WHERE id IN (sqlc.slice('ids')/*SLICE:ids*/); From 5a1184c664b1e6a9be1144ea2458482d9c5eb2a2 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 28 Jul 2025 10:17:56 +0200 Subject: [PATCH 3/3] graph/db+sqldb: remove LEFT JOIN for fetching node addresses In this commit, we remove the LEFT JOIN query that was used for fetching a nodes addresses. The reason it was used before was to ensure that we'd get an empty address list if the node did exist but had no addresses. This was for the purposes of the `AddrsForNode` method since it needs to return false/true to indicate if the given node exists. --- graph/db/sql_store.go | 78 +++++++++++++++++------------------- sqldb/sqlc/graph.sql.go | 30 ++++++-------- sqldb/sqlc/querier.go | 2 +- sqldb/sqlc/queries/graph.sql | 11 +++-- 4 files changed, 54 insertions(+), 67 deletions(-) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index d6ca33bcd..cdbeeab89 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -73,7 +73,7 @@ type SQLQueries interface { DeleteExtraNodeType(ctx context.Context, arg sqlc.DeleteExtraNodeTypeParams) error InsertNodeAddress(ctx context.Context, arg sqlc.InsertNodeAddressParams) error - GetNodeAddressesByPubKey(ctx context.Context, arg sqlc.GetNodeAddressesByPubKeyParams) ([]sqlc.GetNodeAddressesByPubKeyRow, error) + GetNodeAddresses(ctx context.Context, nodeID int64) ([]sqlc.GetNodeAddressesRow, error) DeleteNodeAddresses(ctx context.Context, nodeID int64) error InsertNodeFeature(ctx context.Context, arg sqlc.InsertNodeFeatureParams) error @@ -321,10 +321,21 @@ func (s *SQLStore) AddrsForNode(ctx context.Context, known bool ) err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { - var err error - known, addresses, err = getNodeAddresses( - ctx, db, nodePub.SerializeCompressed(), + // First, check if the node exists and get its DB ID if it + // does. + dbID, err := db.GetNodeIDByPubKey( + ctx, sqlc.GetNodeIDByPubKeyParams{ + Version: int16(ProtocolV1), + PubKey: nodePub.SerializeCompressed(), + }, ) + if errors.Is(err, sql.ErrNoRows) { + return nil + } + + known = true + + addresses, err = getNodeAddresses(ctx, db, dbID) if err != nil { return fmt.Errorf("unable to fetch node addresses: %w", err) @@ -3381,7 +3392,7 @@ func buildNode(ctx context.Context, db SQLQueries, dbNode *sqlc.GraphNode) ( } // Fetch the node's addresses. - _, node.Addresses, err = getNodeAddresses(ctx, db, pub[:]) + node.Addresses, err = getNodeAddresses(ctx, db, dbNode.ID) if err != nil { return nil, fmt.Errorf("unable to fetch node(%d) "+ "addresses: %w", dbNode.ID, err) @@ -3685,42 +3696,26 @@ func upsertNodeAddresses(ctx context.Context, db SQLQueries, nodeID int64, return nil } -// getNodeAddresses fetches the addresses for a node with the given public key. -func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool, - []net.Addr, error) { +// getNodeAddresses fetches the addresses for a node with the given DB ID. +func getNodeAddresses(ctx context.Context, db SQLQueries, id int64) ([]net.Addr, + error) { - // GetNodeAddressesByPubKey ensures that the addresses for a given type - // are returned in the same order as they were inserted. - rows, err := db.GetNodeAddressesByPubKey( - ctx, sqlc.GetNodeAddressesByPubKeyParams{ - Version: int16(ProtocolV1), - PubKey: nodePub, - }, - ) + // GetNodeAddresses ensures that the addresses for a given type are + // returned in the same order as they were inserted. + rows, err := db.GetNodeAddresses(ctx, id) if err != nil { - return false, nil, err - } - - // GetNodeAddressesByPubKey uses a left join so there should always be - // at least one row returned if the node exists even if it has no - // addresses. - if len(rows) == 0 { - return false, nil, nil + return nil, err } addresses := make([]net.Addr, 0, len(rows)) - for _, addr := range rows { - if !(addr.Type.Valid && addr.Address.Valid) { - continue - } + for _, row := range rows { + address := row.Address - address := addr.Address.String - - switch dbAddressType(addr.Type.Int16) { + switch dbAddressType(row.Type) { case addressTypeIPv4: tcp, err := net.ResolveTCPAddr("tcp4", address) if err != nil { - return false, nil, nil + return nil, err } tcp.IP = tcp.IP.To4() @@ -3729,21 +3724,20 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool, case addressTypeIPv6: tcp, err := net.ResolveTCPAddr("tcp6", address) if err != nil { - return false, nil, nil + return nil, err } addresses = append(addresses, tcp) case addressTypeTorV3, addressTypeTorV2: service, portStr, err := net.SplitHostPort(address) if err != nil { - return false, nil, fmt.Errorf("unable to "+ - "split tor v3 address: %v", - addr.Address) + return nil, fmt.Errorf("unable to "+ + "split tor v3 address: %v", address) } port, err := strconv.Atoi(portStr) if err != nil { - return false, nil, err + return nil, err } addresses = append(addresses, &tor.OnionAddr{ @@ -3754,8 +3748,8 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool, case addressTypeOpaque: opaque, err := hex.DecodeString(address) if err != nil { - return false, nil, fmt.Errorf("unable to "+ - "decode opaque address: %v", addr) + return nil, fmt.Errorf("unable to "+ + "decode opaque address: %v", address) } addresses = append(addresses, &lnwire.OpaqueAddrs{ @@ -3763,8 +3757,8 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool, }) default: - return false, nil, fmt.Errorf("unknown address "+ - "type: %v", addr.Type) + return nil, fmt.Errorf("unknown address type: %v", + row.Type) } } @@ -3774,7 +3768,7 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool, addresses = nil } - return true, addresses, nil + return addresses, nil } // upsertNodeExtraSignedFields updates the node's extra signed fields in the diff --git a/sqldb/sqlc/graph.sql.go b/sqldb/sqlc/graph.sql.go index 98b120ec0..4dbdb9aa8 100644 --- a/sqldb/sqlc/graph.sql.go +++ b/sqldb/sqlc/graph.sql.go @@ -1438,33 +1438,27 @@ func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphN return items, nil } -const getNodeAddressesByPubKey = `-- name: GetNodeAddressesByPubKey :many -SELECT a.type, a.address -FROM graph_nodes n -LEFT JOIN graph_node_addresses a ON a.node_id = n.id -WHERE n.pub_key = $1 AND n.version = $2 -ORDER BY a.type ASC, a.position ASC +const getNodeAddresses = `-- name: GetNodeAddresses :many +SELECT type, address +FROM graph_node_addresses +WHERE node_id = $1 +ORDER BY type ASC, position ASC ` -type GetNodeAddressesByPubKeyParams struct { - PubKey []byte - Version int16 +type GetNodeAddressesRow struct { + Type int16 + Address string } -type GetNodeAddressesByPubKeyRow struct { - Type sql.NullInt16 - Address sql.NullString -} - -func (q *Queries) GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error) { - rows, err := q.db.QueryContext(ctx, getNodeAddressesByPubKey, arg.PubKey, arg.Version) +func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) { + rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID) if err != nil { return nil, err } defer rows.Close() - var items []GetNodeAddressesByPubKeyRow + var items []GetNodeAddressesRow for rows.Next() { - var i GetNodeAddressesByPubKeyRow + var i GetNodeAddressesRow if err := rows.Scan(&i.Type, &i.Address); err != nil { return nil, err } diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 3cfb8924e..84b9f6cac 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -59,7 +59,7 @@ type Querier interface { GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error) GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error) GetMigration(ctx context.Context, version int32) (time.Time, error) - GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error) + GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) diff --git a/sqldb/sqlc/queries/graph.sql b/sqldb/sqlc/queries/graph.sql index 509a9434e..542fd6004 100644 --- a/sqldb/sqlc/queries/graph.sql +++ b/sqldb/sqlc/queries/graph.sql @@ -134,12 +134,11 @@ INSERT INTO graph_node_addresses ( $1, $2, $3, $4 ); --- name: GetNodeAddressesByPubKey :many -SELECT a.type, a.address -FROM graph_nodes n -LEFT JOIN graph_node_addresses a ON a.node_id = n.id -WHERE n.pub_key = $1 AND n.version = $2 -ORDER BY a.type ASC, a.position ASC; +-- name: GetNodeAddresses :many +SELECT type, address +FROM graph_node_addresses +WHERE node_id = $1 +ORDER BY type ASC, position ASC; -- name: GetNodesByLastUpdateRange :many SELECT *