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:
Elle Mouton
2025-05-19 11:32:24 +02:00
parent d1f7cce68b
commit 7a120cb584
2 changed files with 41 additions and 8 deletions

View File

@@ -325,10 +325,11 @@ func TestPartialNode(t *testing.T) {
require.ErrorIs(t, err, ErrGraphNodeNotFound)
}
// TestAliasLookup tests the alias lookup functionality of the graph store.
func TestAliasLookup(t *testing.T) {
t.Parallel()
graph := MakeTestGraph(t)
graph := MakeTestGraphNew(t)
// We'd like to test the alias index within the database, so first
// 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
// entry into the alias index for this node.
if err := graph.AddLightningNode(testNode); err != nil {
t.Fatalf("unable to add node: %v", err)
}
require.NoError(t, graph.AddLightningNode(testNode))
// Next, attempt to lookup the alias. The alias should exactly match
// 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")
dbAlias, err := graph.LookupAlias(nodePub)
require.NoError(t, err, "unable to find alias")
if dbAlias != testNode.Alias {
t.Fatalf("aliases don't match, expected %v got %v",
testNode.Alias, dbAlias)
}
require.Equal(t, testNode.Alias, dbAlias)
// Ensure that looking up a non-existent alias results in an error.
node := createTestVertex(t)

View File

@@ -334,6 +334,43 @@ func (s *SQLStore) FetchNodeFeatures(nodePub route.Vertex) (
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.
func getNodeByPubKey(ctx context.Context, db SQLQueries,
pubKey route.Vertex) (int64, *models.LightningNode, error) {