graph/db: more test coverage for node addresses
In this commit we add more test coverage for the persistence of the addresses of a LightningNode. This is so that we have unit test coverage that ensures that all the various address types can be persisted and that the order of the addresses (within a type) are preserved.
This commit is contained in:
2 files changed
+130
-41
No files matched your search
+29
-21
@@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/tor"
|
"github.com/lightningnetwork/lnd/tor"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type unknownAddrType struct{}
|
type unknownAddrType struct{}
|
||||||
@@ -14,37 +15,47 @@ type unknownAddrType struct{}
|
|||||||
func (t unknownAddrType) Network() string { return "unknown" }
|
func (t unknownAddrType) Network() string { return "unknown" }
|
||||||
func (t unknownAddrType) String() string { return "unknown" }
|
func (t unknownAddrType) String() string { return "unknown" }
|
||||||
|
|
||||||
var testIP4 = net.ParseIP("192.168.1.1")
|
var testIP4 = net.ParseIP("192.168.1.1").To4()
|
||||||
var testIP6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
|
var testIP6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
|
||||||
|
|
||||||
|
var (
|
||||||
|
testIPV4Addr = &net.TCPAddr{
|
||||||
|
IP: testIP4,
|
||||||
|
Port: 12345,
|
||||||
|
}
|
||||||
|
|
||||||
|
testIPV6Addr = &net.TCPAddr{
|
||||||
|
IP: testIP6,
|
||||||
|
Port: 65535,
|
||||||
|
}
|
||||||
|
|
||||||
|
testOnionV2Addr = &tor.OnionAddr{
|
||||||
|
OnionService: "3g2upl4pq6kufc4m.onion",
|
||||||
|
Port: 9735,
|
||||||
|
}
|
||||||
|
|
||||||
|
testOnionV3Addr = &tor.OnionAddr{
|
||||||
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", //nolint:ll
|
||||||
|
Port: 80,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
var addrTests = []struct {
|
var addrTests = []struct {
|
||||||
expAddr net.Addr
|
expAddr net.Addr
|
||||||
serErr string
|
serErr string
|
||||||
}{
|
}{
|
||||||
// Valid addresses.
|
// Valid addresses.
|
||||||
{
|
{
|
||||||
expAddr: &net.TCPAddr{
|
expAddr: testIPV4Addr,
|
||||||
IP: testIP4,
|
|
||||||
Port: 12345,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
expAddr: &net.TCPAddr{
|
expAddr: testIPV6Addr,
|
||||||
IP: testIP6,
|
|
||||||
Port: 65535,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
expAddr: &tor.OnionAddr{
|
expAddr: testOnionV2Addr,
|
||||||
OnionService: "3g2upl4pq6kufc4m.onion",
|
|
||||||
Port: 9735,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
expAddr: &tor.OnionAddr{
|
expAddr: testOnionV3Addr,
|
||||||
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion",
|
|
||||||
Port: 80,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Invalid addresses.
|
// Invalid addresses.
|
||||||
@@ -141,9 +152,6 @@ func TestAddrSerialization(t *testing.T) {
|
|||||||
t.Fatalf("unable to deserialize address: %v", err)
|
t.Fatalf("unable to deserialize address: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if addr.String() != test.expAddr.String() {
|
require.Equal(t, test.expAddr, addr)
|
||||||
t.Fatalf("expected address %v after serialization, "+
|
|
||||||
"got %v", addr, test.expAddr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+101
-20
@@ -35,7 +35,10 @@ var (
|
|||||||
Port: 9000}
|
Port: 9000}
|
||||||
anotherAddr, _ = net.ResolveTCPAddr("tcp",
|
anotherAddr, _ = net.ResolveTCPAddr("tcp",
|
||||||
"[2001:db8:85a3:0:0:8a2e:370:7334]:80")
|
"[2001:db8:85a3:0:0:8a2e:370:7334]:80")
|
||||||
testAddrs = []net.Addr{testAddr, anotherAddr}
|
testAddrs = []net.Addr{testAddr, anotherAddr}
|
||||||
|
testOpaqueAddr = &lnwire.OpaqueAddrs{
|
||||||
|
Payload: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
|
||||||
|
}
|
||||||
|
|
||||||
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571319d18" +
|
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571319d18" +
|
||||||
"e949ddfa2965fb6caa1bf0314f882d7")
|
"e949ddfa2965fb6caa1bf0314f882d7")
|
||||||
@@ -102,20 +105,23 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
|||||||
|
|
||||||
// We'd like to test basic insertion/deletion for vertexes from the
|
// We'd like to test basic insertion/deletion for vertexes from the
|
||||||
// graph, so we'll create a test vertex to start with.
|
// graph, so we'll create a test vertex to start with.
|
||||||
node := &models.LightningNode{
|
nodeWithAddrs := func(addrs []net.Addr) *models.LightningNode {
|
||||||
HaveNodeAnnouncement: true,
|
return &models.LightningNode{
|
||||||
AuthSigBytes: testSig.Serialize(),
|
HaveNodeAnnouncement: true,
|
||||||
LastUpdate: time.Unix(1232342, 0),
|
AuthSigBytes: testSig.Serialize(),
|
||||||
Color: color.RGBA{1, 2, 3, 0},
|
LastUpdate: time.Unix(1232342, 0),
|
||||||
Alias: "kek",
|
Color: color.RGBA{1, 2, 3, 0},
|
||||||
Features: testFeatures,
|
Alias: "kek",
|
||||||
Addresses: testAddrs,
|
Features: testFeatures,
|
||||||
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
|
Addresses: addrs,
|
||||||
PubKeyBytes: testPub,
|
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
|
||||||
|
PubKeyBytes: testPub,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// First, insert the node into the graph DB. This should succeed
|
// First, insert the node into the graph DB. This should succeed
|
||||||
// without any errors.
|
// without any errors.
|
||||||
|
node := nodeWithAddrs(testAddrs)
|
||||||
if err := graph.AddLightningNode(node); err != nil {
|
if err := graph.AddLightningNode(node); err != nil {
|
||||||
t.Fatalf("unable to add node: %v", err)
|
t.Fatalf("unable to add node: %v", err)
|
||||||
}
|
}
|
||||||
@@ -136,15 +142,6 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
|||||||
// The two nodes should match exactly!
|
// The two nodes should match exactly!
|
||||||
compareNodes(t, node, dbNode)
|
compareNodes(t, node, dbNode)
|
||||||
|
|
||||||
// Check that the addresses for the node are fetched correctly.
|
|
||||||
pub, err := node.PubKey()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
known, addrs, err := graph.AddrsForNode(pub)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.True(t, known)
|
|
||||||
require.Equal(t, testAddrs, addrs)
|
|
||||||
|
|
||||||
// Check that the node's features are fetched correctly. This check
|
// Check that the node's features are fetched correctly. This check
|
||||||
// will use the graph cache to fetch the features.
|
// will use the graph cache to fetch the features.
|
||||||
features, err := graph.FetchNodeFeatures(node.PubKeyBytes)
|
features, err := graph.FetchNodeFeatures(node.PubKeyBytes)
|
||||||
@@ -172,6 +169,90 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
|||||||
// node should have been deleted from the database.
|
// node should have been deleted from the database.
|
||||||
_, err = graph.FetchLightningNode(testPub)
|
_, err = graph.FetchLightningNode(testPub)
|
||||||
require.ErrorIs(t, err, ErrGraphNodeNotFound)
|
require.ErrorIs(t, err, ErrGraphNodeNotFound)
|
||||||
|
|
||||||
|
// Now, we'll specifically test the updating of addresses of a node
|
||||||
|
// since the serialisation and persistence of addresses is a bit
|
||||||
|
// tricky.
|
||||||
|
|
||||||
|
pub, err := node.PubKey()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Initially, the node is unknown to the graph and there should be no
|
||||||
|
// addresses for it.
|
||||||
|
known, addrs, err := graph.AddrsForNode(pub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, known)
|
||||||
|
require.Empty(t, addrs)
|
||||||
|
|
||||||
|
// Add the node without any addresses.
|
||||||
|
node = nodeWithAddrs(nil)
|
||||||
|
require.NoError(t, graph.AddLightningNode(node))
|
||||||
|
|
||||||
|
// Fetch the node and assert the empty addresses.
|
||||||
|
dbNode, err = graph.FetchLightningNode(testPub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Empty(t, dbNode.Addresses)
|
||||||
|
|
||||||
|
known, addrs, err = graph.AddrsForNode(pub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, known)
|
||||||
|
require.Empty(t, addrs)
|
||||||
|
|
||||||
|
// Now, update the node's addresses.
|
||||||
|
expAddrs := []net.Addr{
|
||||||
|
// Add 2 IPV4 addresses.
|
||||||
|
testAddr,
|
||||||
|
testIPV4Addr,
|
||||||
|
// Add 2 IPV6 addresses.
|
||||||
|
testIPV6Addr,
|
||||||
|
anotherAddr,
|
||||||
|
// Add one v2 and one v3 onion address.
|
||||||
|
testOnionV2Addr,
|
||||||
|
testOnionV3Addr,
|
||||||
|
// Make sure to also test the opaque address type.
|
||||||
|
testOpaqueAddr,
|
||||||
|
}
|
||||||
|
node = nodeWithAddrs(expAddrs)
|
||||||
|
require.NoError(t, graph.AddLightningNode(node))
|
||||||
|
|
||||||
|
// Fetch the node and assert the updated addresses.
|
||||||
|
dbNode, err = graph.FetchLightningNode(testPub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expAddrs, dbNode.Addresses)
|
||||||
|
|
||||||
|
known, addrs, err = graph.AddrsForNode(pub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, known)
|
||||||
|
require.EqualValues(t, expAddrs, addrs)
|
||||||
|
|
||||||
|
// Now, change the address set a bit: change the order of the
|
||||||
|
// IPV4 addresses, remove one IPV6 address and remove both onion
|
||||||
|
// addresses.
|
||||||
|
expAddrs = []net.Addr{
|
||||||
|
testIPV4Addr,
|
||||||
|
testAddr,
|
||||||
|
testIPV6Addr,
|
||||||
|
}
|
||||||
|
node = nodeWithAddrs(expAddrs)
|
||||||
|
require.NoError(t, graph.AddLightningNode(node))
|
||||||
|
|
||||||
|
// Fetch the node and assert the updated addresses.
|
||||||
|
dbNode, err = graph.FetchLightningNode(testPub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expAddrs, dbNode.Addresses)
|
||||||
|
|
||||||
|
// Finally, update the set to only contain the Tor addresses.
|
||||||
|
expAddrs = []net.Addr{
|
||||||
|
testOnionV2Addr,
|
||||||
|
testOnionV3Addr,
|
||||||
|
}
|
||||||
|
node = nodeWithAddrs(expAddrs)
|
||||||
|
require.NoError(t, graph.AddLightningNode(node))
|
||||||
|
|
||||||
|
// Fetch the node and assert the updated addresses.
|
||||||
|
dbNode, err = graph.FetchLightningNode(testPub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expAddrs, dbNode.Addresses)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPartialNode checks that we can add and retrieve a LightningNode where
|
// TestPartialNode checks that we can add and retrieve a LightningNode where
|
||||||
|
|||||||
Reference in new issue
Block a user