2022-03-29 08:09:48 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[sequtils, options],
|
2022-07-25 11:01:37 +00:00
|
|
|
stew/shims/net,
|
|
|
|
testutils/unittests,
|
2022-11-22 07:13:51 +00:00
|
|
|
chronicles,
|
2022-03-29 08:09:48 +00:00
|
|
|
chronos,
|
2022-09-06 21:54:48 +00:00
|
|
|
libp2p/peerid,
|
2022-03-29 08:09:48 +00:00
|
|
|
libp2p/crypto/crypto,
|
2022-07-25 11:01:37 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub
|
|
|
|
import
|
2022-10-18 14:05:53 +00:00
|
|
|
../../waku/v2/node/waku_node,
|
2022-07-25 11:01:37 +00:00
|
|
|
../../waku/v2/utils/peers,
|
2022-11-09 08:55:47 +00:00
|
|
|
../test_helpers,
|
|
|
|
./testlib/common
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
|
|
procSuite "Peer Exchange":
|
|
|
|
asyncTest "GossipSub (relay) peer exchange":
|
|
|
|
## Tests peer exchange
|
|
|
|
|
|
|
|
# Create nodes and ENR. These will be added to the discoverable list
|
|
|
|
let
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, bindIp, Port(61100))
|
2022-03-29 08:09:48 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, bindIp, Port(61102), sendSignedPeerRecord = true)
|
2022-03-29 08:09:48 +00:00
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
node3 = WakuNode.new(nodeKey3, bindIp, Port(61103), sendSignedPeerRecord = true)
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
|
|
var
|
2022-05-20 10:57:19 +00:00
|
|
|
peerExchangeHandler, emptyHandler: RoutingRecordsHandler
|
2022-03-29 08:09:48 +00:00
|
|
|
completionFut = newFuture[bool]()
|
|
|
|
|
2022-05-20 10:57:19 +00:00
|
|
|
proc ignorePeerExchange(peer: PeerId, topic: string,
|
|
|
|
peers: seq[RoutingRecordsPair]) {.gcsafe, raises: [Defect].} =
|
|
|
|
discard
|
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
proc handlePeerExchange(peer: PeerId, topic: string,
|
|
|
|
peers: seq[RoutingRecordsPair]) {.gcsafe, raises: [Defect].} =
|
|
|
|
## Handle peers received via gossipsub peer exchange
|
|
|
|
let peerRecords = peers.mapIt(it.record.get())
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Node 3 is informed of node 2 via peer exchange
|
|
|
|
peer == node1.switch.peerInfo.peerId
|
2022-11-09 08:55:47 +00:00
|
|
|
topic == DefaultPubsubTopic
|
2022-03-29 08:09:48 +00:00
|
|
|
peerRecords.countIt(it.peerId == node2.switch.peerInfo.peerId) == 1
|
|
|
|
|
|
|
|
if (not completionFut.completed()):
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
peerExchangeHandler = handlePeerExchange
|
2022-05-20 10:57:19 +00:00
|
|
|
emptyHandler = ignorePeerExchange
|
2022-03-29 08:09:48 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(peerExchangeHandler = some(emptyHandler))
|
|
|
|
await node2.mountRelay(peerExchangeHandler = some(emptyHandler))
|
|
|
|
await node3.mountRelay(peerExchangeHandler = some(peerExchangeHandler))
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
|
|
# Ensure that node1 prunes all peers after the first connection
|
|
|
|
node1.wakuRelay.parameters.dHigh = 1
|
|
|
|
|
|
|
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
|
|
|
|
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
|
|
|
|
await node3.connectToNodes(@[node1.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|