2022-09-20 11:03:34 +00:00
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
|
|
import
|
2023-05-25 14:42:48 +00:00
|
|
|
|
std/[options, sequtils, tables],
|
2022-09-20 11:03:34 +00:00
|
|
|
|
testutils/unittests,
|
|
|
|
|
chronos,
|
|
|
|
|
chronicles,
|
|
|
|
|
stew/shims/net,
|
|
|
|
|
libp2p/switch,
|
2023-02-09 15:59:29 +00:00
|
|
|
|
libp2p/peerId,
|
2022-09-20 11:03:34 +00:00
|
|
|
|
libp2p/crypto/crypto,
|
2023-05-25 14:42:48 +00:00
|
|
|
|
libp2p/multistream,
|
|
|
|
|
libp2p/muxers/muxer,
|
2022-09-20 11:03:34 +00:00
|
|
|
|
eth/keys,
|
|
|
|
|
eth/p2p/discoveryv5/enr
|
|
|
|
|
import
|
2023-04-05 09:58:59 +00:00
|
|
|
|
../../waku/v2/waku_node,
|
2023-02-06 09:03:30 +00:00
|
|
|
|
../../waku/v2/node/peer_manager,
|
2023-04-18 13:22:10 +00:00
|
|
|
|
../../waku/v2/waku_discv5,
|
|
|
|
|
../../waku/v2/waku_peer_exchange,
|
|
|
|
|
../../waku/v2/waku_peer_exchange/rpc,
|
|
|
|
|
../../waku/v2/waku_peer_exchange/rpc_codec,
|
2023-05-25 14:42:48 +00:00
|
|
|
|
../../waku/v2/waku_peer_exchange/protocol,
|
2023-04-05 14:01:51 +00:00
|
|
|
|
./testlib/wakucore,
|
|
|
|
|
./testlib/wakunode
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Extend test coverage
|
|
|
|
|
procSuite "Waku Peer Exchange":
|
|
|
|
|
|
|
|
|
|
asyncTest "encode and decode peer exchange response":
|
|
|
|
|
## Setup
|
|
|
|
|
var
|
|
|
|
|
enr1 = enr.Record(seqNum: 0, raw: @[])
|
|
|
|
|
enr2 = enr.Record(seqNum: 0, raw: @[])
|
|
|
|
|
|
2023-02-09 15:59:29 +00:00
|
|
|
|
check enr1.fromUri("enr:-JK4QPmO-sE2ELiWr8qVFs1kaY4jQZQpNaHvSPRmKiKcaDoqYRdki2c1BKSliImsxFeOD_UHnkddNL2l0XT9wlsP0WEBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQIMwKqlOl3zpwnrsKRKHuWPSuFzit1Cl6IZvL2uzBRe8oN0Y3CC6mKDdWRwgiMqhXdha3UyDw")
|
|
|
|
|
check enr2.fromUri("enr:-Iu4QK_T7kzAmewG92u1pr7o6St3sBqXaiIaWIsFNW53_maJEaOtGLSN2FUbm6LmVxSfb1WfC7Eyk-nFYI7Gs3SlchwBgmlkgnY0gmlwhI5d6VKJc2VjcDI1NmsxoQLPYQDvrrFdCrhqw3JuFaGD71I8PtPfk6e7TJ3pg_vFQYN0Y3CC6mKDdWRwgiMq")
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
let peerInfos = @[
|
|
|
|
|
PeerExchangePeerInfo(enr: enr1.raw),
|
|
|
|
|
PeerExchangePeerInfo(enr: enr2.raw),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
var rpc = PeerExchangeRpc(
|
|
|
|
|
response: PeerExchangeResponse(
|
|
|
|
|
peerInfos: peerInfos
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
## When
|
|
|
|
|
let
|
|
|
|
|
rpcBuffer: seq[byte] = rpc.encode().buffer
|
2022-11-07 15:24:16 +00:00
|
|
|
|
res = PeerExchangeRpc.decode(rpcBuffer)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
## Then
|
|
|
|
|
check:
|
|
|
|
|
res.isOk
|
|
|
|
|
res.get().response.peerInfos == peerInfos
|
|
|
|
|
|
|
|
|
|
## When
|
|
|
|
|
var
|
|
|
|
|
resEnr1 = enr.Record(seqNum: 0, raw: @[])
|
|
|
|
|
resEnr2 = enr.Record(seqNum: 0, raw: @[])
|
|
|
|
|
|
|
|
|
|
discard resEnr1.fromBytes(res.get().response.peerInfos[0].enr)
|
|
|
|
|
discard resEnr2.fromBytes(res.get().response.peerInfos[1].enr)
|
|
|
|
|
|
|
|
|
|
## Then
|
|
|
|
|
check:
|
|
|
|
|
resEnr1 == enr1
|
|
|
|
|
resEnr2 == enr2
|
|
|
|
|
|
|
|
|
|
asyncTest "retrieve and provide peer exchange peers from discv5":
|
|
|
|
|
## Setup (copied from test_waku_discv5.nim)
|
|
|
|
|
let
|
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
|
nodeKey1 = generateSecp256k1Key()
|
2022-11-03 13:47:56 +00:00
|
|
|
|
nodeTcpPort1 = Port(64010)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
nodeUdpPort1 = Port(9000)
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node1 = newTestWakuNode(nodeKey1, bindIp, nodeTcpPort1)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
|
nodeKey2 = generateSecp256k1Key()
|
2022-11-03 13:47:56 +00:00
|
|
|
|
nodeTcpPort2 = Port(64012)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
nodeUdpPort2 = Port(9002)
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node2 = newTestWakuNode(nodeKey2, bindIp, nodeTcpPort2)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
|
nodeKey3 = generateSecp256k1Key()
|
2022-11-03 13:47:56 +00:00
|
|
|
|
nodeTcpPort3 = Port(64014)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
nodeUdpPort3 = Port(9004)
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node3 = newTestWakuNode(nodeKey3, bindIp, nodeTcpPort3)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
# todo: px flag
|
2023-03-08 14:44:10 +00:00
|
|
|
|
flags = CapabilitiesBitfield.init(
|
|
|
|
|
lightpush = false,
|
|
|
|
|
filter = false,
|
|
|
|
|
store = false,
|
|
|
|
|
relay = true
|
|
|
|
|
)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
# Mount discv5
|
|
|
|
|
node1.wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
|
some(extIp), some(nodeTcpPort1), some(nodeUdpPort1),
|
|
|
|
|
bindIp,
|
|
|
|
|
nodeUdpPort1,
|
2023-03-30 07:35:13 +00:00
|
|
|
|
newSeq[enr.Record](),
|
2022-09-20 11:03:34 +00:00
|
|
|
|
false,
|
|
|
|
|
keys.PrivateKey(nodeKey1.skkey),
|
|
|
|
|
flags,
|
2023-02-07 13:06:50 +00:00
|
|
|
|
newSeq[MultiAddress](), # Empty multiaddr fields, for now
|
2022-09-20 11:03:34 +00:00
|
|
|
|
node1.rng
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
node2.wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
|
some(extIp), some(nodeTcpPort2), some(nodeUdpPort2),
|
|
|
|
|
bindIp,
|
|
|
|
|
nodeUdpPort2,
|
2023-03-30 07:35:13 +00:00
|
|
|
|
@[node1.wakuDiscv5.protocol.localNode.record], # Bootstrap with node1
|
2022-09-20 11:03:34 +00:00
|
|
|
|
false,
|
|
|
|
|
keys.PrivateKey(nodeKey2.skkey),
|
|
|
|
|
flags,
|
2023-02-07 13:06:50 +00:00
|
|
|
|
newSeq[MultiAddress](), # Empty multiaddr fields, for now
|
2022-09-20 11:03:34 +00:00
|
|
|
|
node2.rng
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
## Given
|
|
|
|
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
|
|
|
|
await allFutures([node1.startDiscv5(), node2.startDiscv5()])
|
|
|
|
|
|
2023-04-25 07:50:13 +00:00
|
|
|
|
var attempts = 10
|
|
|
|
|
while (node1.wakuDiscv5.protocol.nodesDiscovered < 1 or
|
|
|
|
|
node2.wakuDiscv5.protocol.nodesDiscovered < 1) and
|
|
|
|
|
attempts > 0:
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
attempts -= 1
|
2023-04-19 14:12:00 +00:00
|
|
|
|
|
|
|
|
|
# node2 can be connected, so will be returned by peer exchange
|
|
|
|
|
require (await node1.peerManager.connectRelay(node2.switch.peerInfo.toRemotePeerInfo()))
|
|
|
|
|
|
2022-09-20 11:03:34 +00:00
|
|
|
|
# Mount peer exchange
|
2022-11-02 08:45:21 +00:00
|
|
|
|
await node1.mountPeerExchange()
|
|
|
|
|
await node3.mountPeerExchange()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2023-04-25 07:50:13 +00:00
|
|
|
|
var peerInfosLen = 0
|
|
|
|
|
var response: WakuPeerExchangeResult[PeerExchangeResponse]
|
|
|
|
|
attempts = 10
|
|
|
|
|
while peerInfosLen == 0 and attempts > 0:
|
|
|
|
|
var connOpt = await node3.peerManager.dialPeer(node1.switch.peerInfo.toRemotePeerInfo(), WakuPeerExchangeCodec)
|
|
|
|
|
require connOpt.isSome
|
|
|
|
|
response = await node3.wakuPeerExchange.request(1, connOpt.get())
|
|
|
|
|
require response.isOk
|
|
|
|
|
peerInfosLen = response.get().peerInfos.len
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
attempts -= 1
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
check:
|
2023-02-09 15:59:29 +00:00
|
|
|
|
response.get().peerInfos.len == 1
|
2023-04-25 07:50:13 +00:00
|
|
|
|
response.get().peerInfos[0].enr == node2.wakuDiscV5.protocol.localNode.record.raw
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
2023-02-09 15:59:29 +00:00
|
|
|
|
|
|
|
|
|
asyncTest "peer exchange request functions returns some discovered peers":
|
|
|
|
|
let
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node1 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
|
|
|
|
node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
2023-02-09 15:59:29 +00:00
|
|
|
|
|
|
|
|
|
# Start and mount peer exchange
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
await allFutures([node1.mountPeerExchange(), node2.mountPeerExchange()])
|
|
|
|
|
|
|
|
|
|
# Create connection
|
|
|
|
|
let connOpt = await node2.peerManager.dialPeer(node1.switch.peerInfo.toRemotePeerInfo(), WakuPeerExchangeCodec)
|
|
|
|
|
require:
|
|
|
|
|
connOpt.isSome
|
|
|
|
|
|
|
|
|
|
# Create some enr and add to peer exchange (sumilating disv5)
|
|
|
|
|
var enr1, enr2 = enr.Record()
|
|
|
|
|
check enr1.fromUri("enr:-Iu4QGNuTvNRulF3A4Kb9YHiIXLr0z_CpvWkWjWKU-o95zUPR_In02AWek4nsSk7G_-YDcaT4bDRPzt5JIWvFqkXSNcBgmlkgnY0gmlwhE0WsGeJc2VjcDI1NmsxoQKp9VzU2FAh7fwOwSpg1M_Ekz4zzl0Fpbg6po2ZwgVwQYN0Y3CC6mCFd2FrdTIB")
|
|
|
|
|
check enr2.fromUri("enr:-Iu4QGJllOWlviPIh_SGR-VVm55nhnBIU5L-s3ran7ARz_4oDdtJPtUs3Bc5aqZHCiPQX6qzNYF2ARHER0JPX97TFbEBgmlkgnY0gmlwhE0WsGeJc2VjcDI1NmsxoQP3ULycvday4EkvtVu0VqbBdmOkbfVLJx8fPe0lE_dRkIN0Y3CC6mCFd2FrdTIB")
|
|
|
|
|
|
|
|
|
|
# Mock that we have discovered these enrs
|
|
|
|
|
node1.wakuPeerExchange.enrCache.add(enr1)
|
|
|
|
|
node1.wakuPeerExchange.enrCache.add(enr2)
|
|
|
|
|
|
|
|
|
|
# Request 2 peer from px. Test all request variants
|
|
|
|
|
let response1 = await node2.wakuPeerExchange.request(2)
|
|
|
|
|
let response2 = await node2.wakuPeerExchange.request(2, node1.peerInfo.toRemotePeerInfo())
|
|
|
|
|
let response3 = await node2.wakuPeerExchange.request(2, connOpt.get())
|
|
|
|
|
|
|
|
|
|
# Check the response or dont even continue
|
|
|
|
|
require:
|
|
|
|
|
response1.isOk
|
|
|
|
|
response2.isOk
|
|
|
|
|
response3.isOk
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
response1.get().peerInfos.len == 2
|
|
|
|
|
response2.get().peerInfos.len == 2
|
|
|
|
|
response3.get().peerInfos.len == 2
|
|
|
|
|
|
|
|
|
|
# Since it can return duplicates test that at least one of the enrs is in the response
|
|
|
|
|
response1.get().peerInfos.anyIt(it.enr == enr1.raw) or response1.get().peerInfos.anyIt(it.enr == enr2.raw)
|
|
|
|
|
response2.get().peerInfos.anyIt(it.enr == enr1.raw) or response2.get().peerInfos.anyIt(it.enr == enr2.raw)
|
|
|
|
|
response3.get().peerInfos.anyIt(it.enr == enr1.raw) or response3.get().peerInfos.anyIt(it.enr == enr2.raw)
|
|
|
|
|
|
|
|
|
|
asyncTest "peer exchange handler works as expected":
|
|
|
|
|
let
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node1 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
|
|
|
|
node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
2023-02-09 15:59:29 +00:00
|
|
|
|
|
|
|
|
|
# Start and mount peer exchange
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
await allFutures([node1.mountPeerExchange(), node2.mountPeerExchange()])
|
|
|
|
|
|
|
|
|
|
# Mock that we have discovered these enrs
|
|
|
|
|
var enr1 = enr.Record()
|
|
|
|
|
check enr1.fromUri("enr:-Iu4QGNuTvNRulF3A4Kb9YHiIXLr0z_CpvWkWjWKU-o95zUPR_In02AWek4nsSk7G_-YDcaT4bDRPzt5JIWvFqkXSNcBgmlkgnY0gmlwhE0WsGeJc2VjcDI1NmsxoQKp9VzU2FAh7fwOwSpg1M_Ekz4zzl0Fpbg6po2ZwgVwQYN0Y3CC6mCFd2FrdTIB")
|
|
|
|
|
node1.wakuPeerExchange.enrCache.add(enr1)
|
|
|
|
|
|
|
|
|
|
# Create connection
|
|
|
|
|
let connOpt = await node2.peerManager.dialPeer(node1.switch.peerInfo.toRemotePeerInfo(), WakuPeerExchangeCodec)
|
|
|
|
|
require connOpt.isSome
|
|
|
|
|
let conn = connOpt.get()
|
|
|
|
|
|
|
|
|
|
# Send bytes so that they directly hit the handler
|
|
|
|
|
let rpc = PeerExchangeRpc(
|
|
|
|
|
request: PeerExchangeRequest(numPeers: 1))
|
|
|
|
|
|
|
|
|
|
var buffer: seq[byte]
|
|
|
|
|
await conn.writeLP(rpc.encode().buffer)
|
|
|
|
|
buffer = await conn.readLp(MaxRpcSize.int)
|
|
|
|
|
|
|
|
|
|
# Decode the response
|
|
|
|
|
let decodedBuff = PeerExchangeRpc.decode(buffer)
|
|
|
|
|
require decodedBuff.isOk
|
|
|
|
|
|
|
|
|
|
# Check we got back the enr we mocked
|
|
|
|
|
check:
|
|
|
|
|
decodedBuff.get().response.peerInfos.len == 1
|
|
|
|
|
decodedBuff.get().response.peerInfos[0].enr == enr1.raw
|
|
|
|
|
|
|
|
|
|
asyncTest "peer exchange request fails gracefully":
|
|
|
|
|
let
|
2023-04-05 14:01:51 +00:00
|
|
|
|
node1 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
|
|
|
|
node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0))
|
2023-02-09 15:59:29 +00:00
|
|
|
|
|
|
|
|
|
# Start and mount peer exchange
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
await allFutures([node1.mountPeerExchange(), node2.mountPeerExchange()])
|
|
|
|
|
|
|
|
|
|
# Create connection
|
|
|
|
|
let connOpt = await node2.peerManager.dialPeer(node1.switch.peerInfo.toRemotePeerInfo(), WakuPeerExchangeCodec)
|
|
|
|
|
require connOpt.isSome
|
|
|
|
|
|
|
|
|
|
# Force closing the connection to simulate a failed peer
|
|
|
|
|
await connOpt.get().close()
|
|
|
|
|
|
|
|
|
|
# Request 2 peer from px
|
|
|
|
|
let response = await node1.wakuPeerExchange.request(2, connOpt.get())
|
|
|
|
|
|
|
|
|
|
# Check that it failed gracefully
|
|
|
|
|
check: response.isErr
|
2023-05-25 14:42:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncTest "connections are closed after response is sent":
|
|
|
|
|
# Create 3 nodes
|
|
|
|
|
let nodes = toSeq(0..<3).mapIt(newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0)))
|
|
|
|
|
|
|
|
|
|
await allFutures(nodes.mapIt(it.start()))
|
|
|
|
|
await allFutures(nodes.mapIt(it.mountPeerExchange()))
|
|
|
|
|
|
|
|
|
|
# Multiple nodes request to node 0
|
|
|
|
|
for i in 1..<3:
|
|
|
|
|
let resp = await nodes[i].wakuPeerExchange.request(2, nodes[0].switch.peerInfo.toRemotePeerInfo())
|
|
|
|
|
require resp.isOk
|
|
|
|
|
|
|
|
|
|
# Wait for streams to be closed
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
|
|
|
|
|
# Check that all streams are closed for px
|
|
|
|
|
check:
|
|
|
|
|
nodes[0].peerManager.getNumStreams(WakuPeerExchangeCodec) == (0, 0)
|
|
|
|
|
nodes[1].peerManager.getNumStreams(WakuPeerExchangeCodec) == (0, 0)
|
|
|
|
|
nodes[2].peerManager.getNumStreams(WakuPeerExchangeCodec) == (0, 0)
|