2021-03-23 08:04:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
import
|
2022-11-04 09:52:08 +00:00
|
|
|
std/[options, sequtils],
|
2022-10-28 18:13:41 +00:00
|
|
|
stew/shims/net as stewNet,
|
|
|
|
testutils/unittests,
|
2022-11-04 09:52:08 +00:00
|
|
|
json_rpc/rpcserver,
|
|
|
|
json_rpc/rpcclient,
|
2022-10-28 18:13:41 +00:00
|
|
|
eth/keys,
|
|
|
|
eth/common/eth_types,
|
2021-06-09 14:37:08 +00:00
|
|
|
libp2p/[builders, switch, multiaddress],
|
2021-02-04 10:32:58 +00:00
|
|
|
libp2p/protobuf/minprotobuf,
|
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2022-08-05 15:05:17 +00:00
|
|
|
libp2p/protocols/pubsub/rpc/message
|
|
|
|
import
|
2022-10-28 18:13:41 +00:00
|
|
|
../../waku/common/sqlite,
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-11-04 08:40:13 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_store/waku_peer_storage,
|
2022-10-28 18:13:41 +00:00
|
|
|
../../waku/v2/node/waku_node,
|
2022-09-06 19:07:37 +00:00
|
|
|
../../waku/v2/protocol/waku_relay,
|
2022-08-05 15:05:17 +00:00
|
|
|
../../waku/v2/protocol/waku_store,
|
2022-08-05 14:38:00 +00:00
|
|
|
../../waku/v2/protocol/waku_filter,
|
2022-09-06 18:36:53 +00:00
|
|
|
../../waku/v2/protocol/waku_swap/waku_swap,
|
2021-02-04 10:32:58 +00:00
|
|
|
../test_helpers
|
|
|
|
|
|
|
|
procSuite "Peer Manager":
|
2021-02-05 10:49:11 +00:00
|
|
|
asyncTest "Peer dialing works":
|
2021-02-04 10:32:58 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60800))
|
2021-02-04 10:32:58 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60802))
|
2022-01-10 15:07:35 +00:00
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
# Dial node2 from node1
|
2021-10-06 12:29:08 +00:00
|
|
|
let conn = (await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec)).get()
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
# Check connection
|
|
|
|
check:
|
|
|
|
conn.activity
|
2021-10-06 12:29:08 +00:00
|
|
|
conn.peerId == peerInfo2.peerId
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
# Check that node2 is being managed in node1
|
|
|
|
check:
|
|
|
|
node1.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
|
|
|
|
# Check connectedness
|
|
|
|
check:
|
2021-02-12 08:53:52 +00:00
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == Connectedness.Connected
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop()])
|
|
|
|
|
|
|
|
asyncTest "Dialing fails gracefully":
|
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60810))
|
2021-02-05 10:49:11 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60812))
|
2022-01-10 15:07:35 +00:00
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
# Purposefully don't start node2
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
2021-02-04 10:32:58 +00:00
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
# Dial node2 from node1
|
2021-10-06 12:29:08 +00:00
|
|
|
let connOpt = await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec, 2.seconds)
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
# Check connection failed gracefully
|
|
|
|
check:
|
|
|
|
connOpt.isNone()
|
|
|
|
|
|
|
|
await node1.stop()
|
2021-02-11 08:58:25 +00:00
|
|
|
|
|
|
|
asyncTest "Adding, selecting and filtering peers work":
|
|
|
|
let
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node = WakuNode.new(nodeKey, ValidIpAddress.init("0.0.0.0"), Port(60820))
|
2021-02-11 08:58:25 +00:00
|
|
|
# Create filter peer
|
|
|
|
filterLoc = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet()
|
2022-10-18 14:05:53 +00:00
|
|
|
filterKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
filterPeer = PeerInfo.new(filterKey, @[filterLoc])
|
2021-02-11 08:58:25 +00:00
|
|
|
# Create swap peer
|
|
|
|
swapLoc = MultiAddress.init("/ip4/127.0.0.2/tcp/2").tryGet()
|
2022-10-18 14:05:53 +00:00
|
|
|
swapKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
swapPeer = PeerInfo.new(swapKey, @[swapLoc])
|
2021-02-11 08:58:25 +00:00
|
|
|
# Create store peer
|
|
|
|
storeLoc = MultiAddress.init("/ip4/127.0.0.3/tcp/4").tryGet()
|
2022-10-18 14:05:53 +00:00
|
|
|
storeKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
storePeer = PeerInfo.new(storeKey, @[storeLoc])
|
2021-02-11 08:58:25 +00:00
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
await node.mountFilterClient()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountSwap()
|
2022-10-28 18:11:28 +00:00
|
|
|
node.mountStoreClient()
|
2021-02-11 08:58:25 +00:00
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
node.wakuSwap.setPeer(swapPeer.toRemotePeerInfo())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
|
|
|
node.setStorePeer(storePeer.toRemotePeerInfo())
|
2022-11-02 10:59:58 +00:00
|
|
|
node.setFilterPeer(filterPeer.toRemotePeerInfo())
|
2021-02-11 08:58:25 +00:00
|
|
|
|
|
|
|
# Check peers were successfully added to peer manager
|
|
|
|
check:
|
|
|
|
node.peerManager.peers().len == 3
|
|
|
|
node.peerManager.peers(WakuFilterCodec).allIt(it.peerId == filterPeer.peerId and
|
|
|
|
it.addrs.contains(filterLoc) and
|
|
|
|
it.protos.contains(WakuFilterCodec))
|
|
|
|
node.peerManager.peers(WakuSwapCodec).allIt(it.peerId == swapPeer.peerId and
|
|
|
|
it.addrs.contains(swapLoc) and
|
|
|
|
it.protos.contains(WakuSwapCodec))
|
|
|
|
node.peerManager.peers(WakuStoreCodec).allIt(it.peerId == storePeer.peerId and
|
|
|
|
it.addrs.contains(storeLoc) and
|
|
|
|
it.protos.contains(WakuStoreCodec))
|
|
|
|
|
|
|
|
await node.stop()
|
2021-02-12 08:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
asyncTest "Peer manager keeps track of connections":
|
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60830))
|
2021-02-12 08:53:52 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60832))
|
2022-01-10 15:07:35 +00:00
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-02-12 08:53:52 +00:00
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
2021-02-12 08:53:52 +00:00
|
|
|
|
|
|
|
# Test default connectedness for new peers
|
2021-10-06 12:29:08 +00:00
|
|
|
node1.peerManager.addPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec)
|
2021-02-12 08:53:52 +00:00
|
|
|
check:
|
|
|
|
# No information about node2's connectedness
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == NotConnected
|
|
|
|
|
|
|
|
# Purposefully don't start node2
|
|
|
|
# Attempt dialing node2 from node1
|
2021-10-06 12:29:08 +00:00
|
|
|
discard await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec, 2.seconds)
|
2021-02-12 08:53:52 +00:00
|
|
|
check:
|
|
|
|
# Cannot connect to node2
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == CannotConnect
|
|
|
|
|
|
|
|
# Successful connection
|
|
|
|
await node2.start()
|
2021-10-06 12:29:08 +00:00
|
|
|
discard await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec, 2.seconds)
|
2021-02-12 08:53:52 +00:00
|
|
|
check:
|
|
|
|
# Currently connected to node2
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == Connected
|
|
|
|
|
|
|
|
# Stop node. Gracefully disconnect from all peers.
|
|
|
|
await node1.stop()
|
|
|
|
check:
|
|
|
|
# Not currently connected to node2, but had recent, successful connection.
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == CanConnect
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
await node2.stop()
|
|
|
|
|
|
|
|
asyncTest "Peer manager can use persistent storage and survive restarts":
|
|
|
|
let
|
2022-10-28 18:13:41 +00:00
|
|
|
database = SqliteDatabase.new(":memory:")[]
|
2021-03-26 08:49:51 +00:00
|
|
|
storage = WakuPeerStorage.new(database)[]
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60840), peerStorage = storage)
|
2021-03-26 08:49:51 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60842))
|
2022-01-10 15:07:35 +00:00
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
await node2.start()
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
2021-03-26 08:49:51 +00:00
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
discard await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), WakuRelayCodec, 2.seconds)
|
2021-03-26 08:49:51 +00:00
|
|
|
check:
|
|
|
|
# Currently connected to node2
|
|
|
|
node1.peerManager.peers().len == 1
|
|
|
|
node1.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == Connected
|
|
|
|
|
|
|
|
# Simulate restart by initialising a new node using the same storage
|
|
|
|
let
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"), Port(60844), peerStorage = storage)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
await node3.start()
|
|
|
|
check:
|
|
|
|
# Node2 has been loaded after "restart", but we have not yet reconnected
|
|
|
|
node3.peerManager.peers().len == 1
|
|
|
|
node3.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node3.peerManager.connectedness(peerInfo2.peerId) == NotConnected
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node3.mountRelay() # This should trigger a reconnect
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Reconnected to node2 after "restart"
|
|
|
|
node3.peerManager.peers().len == 1
|
|
|
|
node3.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node3.peerManager.connectedness(peerInfo2.peerId) == Connected
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
2021-07-27 06:48:56 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
asyncTest "Peer manager support multiple protocol IDs when reconnecting to peers":
|
2021-07-27 06:48:56 +00:00
|
|
|
let
|
2022-10-28 18:13:41 +00:00
|
|
|
database = SqliteDatabase.new(":memory:")[]
|
2021-07-27 06:48:56 +00:00
|
|
|
storage = WakuPeerStorage.new(database)[]
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60850), peerStorage = storage)
|
2021-07-27 06:48:56 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60852))
|
2022-01-10 15:07:35 +00:00
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-07-27 06:48:56 +00:00
|
|
|
betaCodec = "/vac/waku/relay/2.0.0-beta2"
|
|
|
|
stableCodec = "/vac/waku/relay/2.0.0"
|
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
await node2.start()
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
2021-07-27 06:48:56 +00:00
|
|
|
node1.wakuRelay.codec = betaCodec
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay()
|
2021-07-27 06:48:56 +00:00
|
|
|
node2.wakuRelay.codec = betaCodec
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
discard await node1.peerManager.dialPeer(peerInfo2.toRemotePeerInfo(), node2.wakuRelay.codec, 2.seconds)
|
2021-07-27 06:48:56 +00:00
|
|
|
check:
|
|
|
|
# Currently connected to node2
|
|
|
|
node1.peerManager.peers().len == 1
|
|
|
|
node1.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node1.peerManager.peers().anyIt(it.protos.contains(node2.wakuRelay.codec))
|
|
|
|
node1.peerManager.connectedness(peerInfo2.peerId) == Connected
|
|
|
|
|
|
|
|
# Simulate restart by initialising a new node using the same storage
|
|
|
|
let
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"), Port(60854), peerStorage = storage)
|
2021-07-27 06:48:56 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node3.mountRelay()
|
2021-07-27 06:48:56 +00:00
|
|
|
node3.wakuRelay.codec = stableCodec
|
|
|
|
check:
|
|
|
|
# Node 2 and 3 have differing codecs
|
|
|
|
node2.wakuRelay.codec == betaCodec
|
|
|
|
node3.wakuRelay.codec == stableCodec
|
|
|
|
# Node2 has been loaded after "restart", but we have not yet reconnected
|
|
|
|
node3.peerManager.peers().len == 1
|
|
|
|
node3.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node3.peerManager.peers().anyIt(it.protos.contains(betaCodec))
|
|
|
|
node3.peerManager.connectedness(peerInfo2.peerId) == NotConnected
|
|
|
|
|
|
|
|
await node3.start() # This should trigger a reconnect
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Reconnected to node2 after "restart"
|
|
|
|
node3.peerManager.peers().len == 1
|
|
|
|
node3.peerManager.peers().anyIt(it.peerId == peerInfo2.peerId)
|
|
|
|
node3.peerManager.peers().anyIt(it.protos.contains(betaCodec))
|
|
|
|
node3.peerManager.peers().anyIt(it.protos.contains(stableCodec))
|
|
|
|
node3.peerManager.connectedness(peerInfo2.peerId) == Connected
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|