multi: let chan and graph db implement AddrSource

Then use both to construct a multiAddrSource AddrSource and use that
around the code-base.
This commit is contained in:
Elle Mouton
2024-10-22 14:35:37 +02:00
parent 51c2f709e1
commit 2c083bc017
4 changed files with 55 additions and 70 deletions

View File

@@ -185,9 +185,10 @@ func TestFetchClosedChannelForID(t *testing.T) {
}
}
// TestAddrsForNode tests the we're able to properly obtain all the addresses
// for a target node.
func TestAddrsForNode(t *testing.T) {
// TestMultiSourceAddrsForNode tests the we're able to properly obtain all the
// addresses for a target node from multiple backends - in this case, the
// channel db and graph db.
func TestMultiSourceAddrsForNode(t *testing.T) {
t.Parallel()
fullDB, err := MakeTestDB(t)
@@ -201,9 +202,7 @@ func TestAddrsForNode(t *testing.T) {
testNode := createTestVertex(t)
require.NoError(t, err, "unable to create test node")
testNode.Addresses = []net.Addr{testAddr}
if err := graph.SetSourceNode(testNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
require.NoError(t, graph.SetSourceNode(testNode))
// Next, we'll make a link node with the same pubkey, but with an
// additional address.
@@ -213,28 +212,27 @@ func TestAddrsForNode(t *testing.T) {
fullDB.channelStateDB.linkNodeDB, wire.MainNet, nodePub,
anotherAddr,
)
if err := linkNode.Sync(); err != nil {
t.Fatalf("unable to sync link node: %v", err)
}
require.NoError(t, linkNode.Sync())
// Create a multi-backend address source from the channel db and graph
// db.
addrSource := NewMultiAddrSource(fullDB, graph)
// Now that we've created a link node, as well as a vertex for the
// node, we'll query for all its addresses.
_, nodeAddrs, err := fullDB.AddrsForNode(nodePub)
known, nodeAddrs, err := addrSource.AddrsForNode(nodePub)
require.NoError(t, err, "unable to obtain node addrs")
require.True(t, known)
expectedAddrs := make(map[string]struct{})
expectedAddrs[testAddr.String()] = struct{}{}
expectedAddrs[anotherAddr.String()] = struct{}{}
// Finally, ensure that all the expected addresses are found.
if len(nodeAddrs) != len(expectedAddrs) {
t.Fatalf("expected %v addrs, got %v",
len(expectedAddrs), len(nodeAddrs))
}
require.Len(t, nodeAddrs, len(expectedAddrs))
for _, addr := range nodeAddrs {
if _, ok := expectedAddrs[addr.String()]; !ok {
t.Fatalf("unexpected addr: %v", addr)
}
require.Contains(t, expectedAddrs, addr.String())
}
}