diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 0b812426d..2b5d0dbce 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -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) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 4e6c876cf..971a3acff 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -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) {