Merge pull request #10010 from ellemouton/sqlGraphUpdates
graph/db: various misc updates
This commit is contained in:
5 files changed
+76
-95
No files matched your search
@@ -97,6 +97,7 @@ func createTestVertex(t testing.TB) *models.LightningNode {
|
||||
return createLightningNode(priv)
|
||||
}
|
||||
|
||||
// TestNodeInsertionAndDeletion tests the CRUD operations for a LightningNode.
|
||||
func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
@@ -124,9 +125,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
// First, insert the node into the graph DB. This should succeed
|
||||
// without any errors.
|
||||
node := nodeWithAddrs(testAddrs)
|
||||
if err := graph.AddLightningNode(ctx, node); err != nil {
|
||||
t.Fatalf("unable to add node: %v", err)
|
||||
}
|
||||
require.NoError(t, graph.AddLightningNode(ctx, node))
|
||||
assertNodeInCache(t, graph, node, testFeatures)
|
||||
|
||||
// Next, fetch the node from the database to ensure everything was
|
||||
@@ -135,11 +134,8 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
require.NoError(t, err, "unable to locate node")
|
||||
|
||||
_, exists, err := graph.HasLightningNode(ctx, dbNode.PubKeyBytes)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to query for node: %v", err)
|
||||
} else if !exists {
|
||||
t.Fatalf("node should be found but wasn't")
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.True(t, exists)
|
||||
|
||||
// The two nodes should match exactly!
|
||||
compareNodes(t, node, dbNode)
|
||||
@@ -194,7 +190,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
// Fetch the node and assert the empty addresses.
|
||||
dbNode, err = graph.FetchLightningNode(ctx, testPub)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, dbNode.Addresses)
|
||||
compareNodes(t, node, dbNode)
|
||||
|
||||
known, addrs, err = graph.AddrsForNode(ctx, pub)
|
||||
require.NoError(t, err)
|
||||
|
||||
+15
-23
@@ -64,7 +64,7 @@ type SQLQueries interface {
|
||||
ListNodesPaginated(ctx context.Context, arg sqlc.ListNodesPaginatedParams) ([]sqlc.Node, error)
|
||||
ListNodeIDsAndPubKeys(ctx context.Context, arg sqlc.ListNodeIDsAndPubKeysParams) ([]sqlc.ListNodeIDsAndPubKeysRow, error)
|
||||
IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error)
|
||||
GetUnconnectedNodes(ctx context.Context) ([]sqlc.GetUnconnectedNodesRow, error)
|
||||
DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error)
|
||||
DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error)
|
||||
DeleteNode(ctx context.Context, id int64) error
|
||||
|
||||
@@ -154,11 +154,6 @@ type BatchedSQLQueries interface {
|
||||
|
||||
// SQLStore is an implementation of the V1Store interface that uses a SQL
|
||||
// database as the backend.
|
||||
//
|
||||
// NOTE: currently, this temporarily embeds the KVStore struct so that we can
|
||||
// implement the V1Store interface incrementally. For any method not
|
||||
// implemented, things will fall back to the KVStore. This is ONLY the case
|
||||
// for the time being while this struct is purely used in unit tests only.
|
||||
type SQLStore struct {
|
||||
cfg *SQLStoreConfig
|
||||
db BatchedSQLQueries
|
||||
@@ -2568,30 +2563,21 @@ func (s *SQLStore) PruneTip() (*chainhash.Hash, uint32, error) {
|
||||
func (s *SQLStore) pruneGraphNodes(ctx context.Context,
|
||||
db SQLQueries) ([]route.Vertex, error) {
|
||||
|
||||
// Fetch all un-connected nodes from the database.
|
||||
// NOTE: this will not include any nodes that are listed in the
|
||||
// source table.
|
||||
nodes, err := db.GetUnconnectedNodes(ctx)
|
||||
nodeKeys, err := db.DeleteUnconnectedNodes(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to fetch unconnected nodes: %w",
|
||||
err)
|
||||
return nil, fmt.Errorf("unable to delete unconnected "+
|
||||
"nodes: %w", err)
|
||||
}
|
||||
|
||||
prunedNodes := make([]route.Vertex, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
// TODO(elle): update to use sqlc.slice() once that works.
|
||||
if err = db.DeleteNode(ctx, node.ID); err != nil {
|
||||
return nil, fmt.Errorf("unable to delete "+
|
||||
"node(id=%d): %w", node.ID, err)
|
||||
}
|
||||
|
||||
pubKey, err := route.NewVertexFromBytes(node.PubKey)
|
||||
prunedNodes := make([]route.Vertex, len(nodeKeys))
|
||||
for i, nodeKey := range nodeKeys {
|
||||
pub, err := route.NewVertexFromBytes(nodeKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse pubkey "+
|
||||
"for node(id=%d): %w", node.ID, err)
|
||||
"from bytes: %w", err)
|
||||
}
|
||||
|
||||
prunedNodes = append(prunedNodes, pubKey)
|
||||
prunedNodes[i] = pub
|
||||
}
|
||||
|
||||
return prunedNodes, nil
|
||||
@@ -3638,6 +3624,12 @@ func getNodeAddresses(ctx context.Context, db SQLQueries, nodePub []byte) (bool,
|
||||
}
|
||||
}
|
||||
|
||||
// If we have no addresses, then we'll return nil instead of an
|
||||
// empty slice.
|
||||
if len(addresses) == 0 {
|
||||
addresses = nil
|
||||
}
|
||||
|
||||
return true, addresses, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user