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
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_core,
|
|
|
|
../../waku/waku_node,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore,
|
|
|
|
./testlib/wakunode
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
|
|
procSuite "Peer Exchange":
|
|
|
|
asyncTest "GossipSub (relay) peer exchange":
|
|
|
|
## Tests peer exchange
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
# Create nodes and ENR. These will be added to the discoverable list
|
|
|
|
let
|
2023-12-14 06:16:39 +00:00
|
|
|
bindIp = parseIpAddress("0.0.0.0")
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey1 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node1 = newTestWakuNode(nodeKey1, bindIp, Port(0))
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey2 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node2 = newTestWakuNode(nodeKey2, bindIp, Port(0), sendSignedPeerRecord = true)
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey3 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node3 = newTestWakuNode(nodeKey3, bindIp, Port(0), sendSignedPeerRecord = true)
|
2023-02-10 16:55:47 +00:00
|
|
|
|
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]()
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-05-20 10:57:19 +00:00
|
|
|
proc ignorePeerExchange(peer: PeerId, topic: string,
|
2023-02-10 16:55:47 +00:00
|
|
|
peers: seq[RoutingRecordsPair]) {.gcsafe.} =
|
2022-05-20 10:57:19 +00:00
|
|
|
discard
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
proc handlePeerExchange(peer: PeerId, topic: string,
|
2023-02-10 16:55:47 +00:00
|
|
|
peers: seq[RoutingRecordsPair]) {.gcsafe.} =
|
2022-03-29 08:09:48 +00:00
|
|
|
## Handle peers received via gossipsub peer exchange
|
|
|
|
let peerRecords = peers.mapIt(it.record.get())
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
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
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
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
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
await node1.mountRelay(@[DefaultPubsubTopic], some(emptyHandler))
|
|
|
|
await node2.mountRelay(@[DefaultPubsubTopic], some(emptyHandler))
|
|
|
|
await node3.mountRelay(@[DefaultPubsubTopic], 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()])
|
2023-02-10 16:55:47 +00:00
|
|
|
|
2022-03-29 08:09:48 +00:00
|
|
|
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()])
|