Skip to content

Commit 500808f

Browse files
authored
Merge pull request #10010 from ellemouton/sqlGraphUpdates
graph/db: various misc updates
2 parents 5a8606c + f1b7ccc commit 500808f

File tree

5 files changed

+76
-95
lines changed

5 files changed

+76
-95
lines changed

graph/db/graph_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func createTestVertex(t testing.TB) *models.LightningNode {
9797
return createLightningNode(priv)
9898
}
9999

100+
// TestNodeInsertionAndDeletion tests the CRUD operations for a LightningNode.
100101
func TestNodeInsertionAndDeletion(t *testing.T) {
101102
t.Parallel()
102103
ctx := context.Background()
@@ -124,9 +125,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
124125
// First, insert the node into the graph DB. This should succeed
125126
// without any errors.
126127
node := nodeWithAddrs(testAddrs)
127-
if err := graph.AddLightningNode(ctx, node); err != nil {
128-
t.Fatalf("unable to add node: %v", err)
129-
}
128+
require.NoError(t, graph.AddLightningNode(ctx, node))
130129
assertNodeInCache(t, graph, node, testFeatures)
131130

132131
// Next, fetch the node from the database to ensure everything was
@@ -135,11 +134,8 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
135134
require.NoError(t, err, "unable to locate node")
136135

137136
_, exists, err := graph.HasLightningNode(ctx, dbNode.PubKeyBytes)
138-
if err != nil {
139-
t.Fatalf("unable to query for node: %v", err)
140-
} else if !exists {
141-
t.Fatalf("node should be found but wasn't")
142-
}
137+
require.NoError(t, err)
138+
require.True(t, exists)
143139

144140
// The two nodes should match exactly!
145141
compareNodes(t, node, dbNode)
@@ -194,7 +190,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
194190
// Fetch the node and assert the empty addresses.
195191
dbNode, err = graph.FetchLightningNode(ctx, testPub)
196192
require.NoError(t, err)
197-
require.Empty(t, dbNode.Addresses)
193+
compareNodes(t, node, dbNode)
198194

199195
known, addrs, err = graph.AddrsForNode(ctx, pub)
200196
require.NoError(t, err)

graph/db/sql_store.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type SQLQueries interface {
6464
ListNodesPaginated(ctx context.Context, arg sqlc.ListNodesPaginatedParams) ([]sqlc.Node, error)
6565
ListNodeIDsAndPubKeys(ctx context.Context, arg sqlc.ListNodeIDsAndPubKeysParams) ([]sqlc.ListNodeIDsAndPubKeysRow, error)
6666
IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error)
67-
GetUnconnectedNodes(ctx context.Context) ([]sqlc.GetUnconnectedNodesRow, error)
67+
DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error)
6868
DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error)
6969
DeleteNode(ctx context.Context, id int64) error
7070

@@ -154,11 +154,6 @@ type BatchedSQLQueries interface {
154154

155155
// SQLStore is an implementation of the V1Store interface that uses a SQL
156156
// database as the backend.
157-
//
158-
// NOTE: currently, this temporarily embeds the KVStore struct so that we can
159-
// implement the V1Store interface incrementally. For any method not
160-
// implemented, things will fall back to the KVStore. This is ONLY the case
161-
// for the time being while this struct is purely used in unit tests only.
162157
type SQLStore struct {
163158
cfg *SQLStoreConfig
164159
db BatchedSQLQueries
@@ -2568,30 +2563,21 @@ func (s *SQLStore) PruneTip() (*chainhash.Hash, uint32, error) {
25682563
func (s *SQLStore) pruneGraphNodes(ctx context.Context,
25692564
db SQLQueries) ([]route.Vertex, error) {
25702565

2571-
// Fetch all un-connected nodes from the database.
2572-
// NOTE: this will not include any nodes that are listed in the
2573-
// source table.
2574-
nodes, err := db.GetUnconnectedNodes(ctx)
2566+
nodeKeys, err := db.DeleteUnconnectedNodes(ctx)
25752567
if err != nil {
2576-
return nil, fmt.Errorf("unable to fetch unconnected nodes: %w",
2577-
err)
2568+
return nil, fmt.Errorf("unable to delete unconnected "+
2569+
"nodes: %w", err)
25782570
}
25792571

2580-
prunedNodes := make([]route.Vertex, 0, len(nodes))
2581-
for _, node := range nodes {
2582-
// TODO(elle): update to use sqlc.slice() once that works.
2583-
if err = db.DeleteNode(ctx, node.ID); err != nil {
2584-
return nil, fmt.Errorf("unable to delete "+
2585-
"node(id=%d): %w", node.ID, err)
2586-
}
2587-
2588-
pubKey, err := route.NewVertexFromBytes(node.PubKey)
2572+
prunedNodes := make([]route.Vertex, len(nodeKeys))
2573+
for i, nodeKey := range nodeKeys {
2574+
pub, err := route.NewVertexFromBytes(nodeKey)
25892575
if err != nil {
25902576
return nil, fmt.Errorf("unable to parse pubkey "+
2591-
"for node(id=%d): %w", node.ID, err)
2577+
"from bytes: %w", err)
25922578
}
25932579

2594-
prunedNodes = append(prunedNodes, pubKey)
2580+
prunedNodes[i] = pub
25952581
}
25962582

25972583
return prunedNodes, nil
@@ -3638,6 +3624,12 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool,
36383624
}
36393625
}
36403626

3627+
// If we have no addresses, then we'll return nil instead of an
3628+
// empty slice.
3629+
if len(addresses) == 0 {
3630+
addresses = nil
3631+
}
3632+
36413633
return true, addresses, nil
36423634
}
36433635

sqldb/sqlc/graph.sql.go

Lines changed: 40 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/graph.sql

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ SELECT EXISTS (
6565
AND n.pub_key = $1
6666
);
6767

68-
-- name: GetUnconnectedNodes :many
69-
SELECT n.id, n.pub_key
70-
FROM nodes n
71-
-- Select all nodes that do not have any channels.
72-
WHERE NOT EXISTS (
73-
SELECT 1
74-
FROM channels c
75-
WHERE c.node_id_1 = n.id OR c.node_id_2 = n.id
76-
)
77-
-- Ignore any of our source nodes.
78-
AND NOT EXISTS (
79-
SELECT 1
80-
FROM source_nodes sn
81-
WHERE sn.node_id = n.id
82-
);
68+
-- name: DeleteUnconnectedNodes :many
69+
DELETE FROM nodes
70+
WHERE
71+
-- Ignore any of our source nodes.
72+
NOT EXISTS (
73+
SELECT 1
74+
FROM source_nodes sn
75+
WHERE sn.node_id = nodes.id
76+
)
77+
-- Select all nodes that do not have any channels.
78+
AND NOT EXISTS (
79+
SELECT 1
80+
FROM channels c
81+
WHERE c.node_id_1 = nodes.id OR c.node_id_2 = nodes.id
82+
) RETURNING pub_key;
8383

8484
-- name: DeleteNodeByPubKey :execresult
8585
DELETE FROM nodes

0 commit comments

Comments
 (0)