graph/db: implement SQLStore.LookupAlias
In this commit, we let the SQLStore implement LookupAlias. This then lets us run the TestAliasLookup unit test against the SQL backends.
This commit is contained in:
2 files changed
+41
-8
No files matched your search
@@ -325,10 +325,11 @@ func TestPartialNode(t *testing.T) {
|
|||||||
require.ErrorIs(t, err, ErrGraphNodeNotFound)
|
require.ErrorIs(t, err, ErrGraphNodeNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestAliasLookup tests the alias lookup functionality of the graph store.
|
||||||
func TestAliasLookup(t *testing.T) {
|
func TestAliasLookup(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph := MakeTestGraph(t)
|
graph := MakeTestGraphNew(t)
|
||||||
|
|
||||||
// We'd like to test the alias index within the database, so first
|
// We'd like to test the alias index within the database, so first
|
||||||
// create a new test node.
|
// create a new test node.
|
||||||
@@ -336,9 +337,7 @@ func TestAliasLookup(t *testing.T) {
|
|||||||
|
|
||||||
// Add the node to the graph's database, this should also insert an
|
// Add the node to the graph's database, this should also insert an
|
||||||
// entry into the alias index for this node.
|
// entry into the alias index for this node.
|
||||||
if err := graph.AddLightningNode(testNode); err != nil {
|
require.NoError(t, graph.AddLightningNode(testNode))
|
||||||
t.Fatalf("unable to add node: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next, attempt to lookup the alias. The alias should exactly match
|
// Next, attempt to lookup the alias. The alias should exactly match
|
||||||
// the one which the test node was assigned.
|
// the one which the test node was assigned.
|
||||||
@@ -346,10 +345,7 @@ func TestAliasLookup(t *testing.T) {
|
|||||||
require.NoError(t, err, "unable to generate pubkey")
|
require.NoError(t, err, "unable to generate pubkey")
|
||||||
dbAlias, err := graph.LookupAlias(nodePub)
|
dbAlias, err := graph.LookupAlias(nodePub)
|
||||||
require.NoError(t, err, "unable to find alias")
|
require.NoError(t, err, "unable to find alias")
|
||||||
if dbAlias != testNode.Alias {
|
require.Equal(t, testNode.Alias, dbAlias)
|
||||||
t.Fatalf("aliases don't match, expected %v got %v",
|
|
||||||
testNode.Alias, dbAlias)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that looking up a non-existent alias results in an error.
|
// Ensure that looking up a non-existent alias results in an error.
|
||||||
node := createTestVertex(t)
|
node := createTestVertex(t)
|
||||||
|
|||||||
@@ -334,6 +334,43 @@ func (s *SQLStore) FetchNodeFeatures(nodePub route.Vertex) (
|
|||||||
return fetchNodeFeatures(ctx, s.db, nodePub)
|
return fetchNodeFeatures(ctx, s.db, nodePub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LookupAlias attempts to return the alias as advertised by the target node.
|
||||||
|
//
|
||||||
|
// NOTE: part of the V1Store interface.
|
||||||
|
func (s *SQLStore) LookupAlias(pub *btcec.PublicKey) (string, error) {
|
||||||
|
var (
|
||||||
|
ctx = context.TODO()
|
||||||
|
readTx = NewReadTx()
|
||||||
|
alias string
|
||||||
|
)
|
||||||
|
err := s.db.ExecTx(ctx, readTx, func(db SQLQueries) error {
|
||||||
|
dbNode, err := db.GetNodeByPubKey(
|
||||||
|
ctx, sqlc.GetNodeByPubKeyParams{
|
||||||
|
Version: int16(ProtocolV1),
|
||||||
|
PubKey: pub.SerializeCompressed(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return ErrNodeAliasNotFound
|
||||||
|
} else if err != nil {
|
||||||
|
return fmt.Errorf("unable to fetch node: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dbNode.Alias.Valid {
|
||||||
|
return ErrNodeAliasNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
alias = dbNode.Alias.String
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, func() {})
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to look up alias: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return alias, nil
|
||||||
|
}
|
||||||
|
|
||||||
// getNodeByPubKey attempts to look up a target node by its public key.
|
// getNodeByPubKey attempts to look up a target node by its public key.
|
||||||
func getNodeByPubKey(ctx context.Context, db SQLQueries,
|
func getNodeByPubKey(ctx context.Context, db SQLQueries,
|
||||||
pubKey route.Vertex) (int64, *models.LightningNode, error) {
|
pubKey route.Vertex) (int64, *models.LightningNode, error) {
|
||||||
|
|||||||
Reference in new issue
Block a user