2021-06-02 07:53:34 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, tables, sets],
|
|
|
|
testutils/unittests, chronos, chronicles,
|
|
|
|
stew/shims/net as stewNet,
|
|
|
|
libp2p/switch,
|
|
|
|
libp2p/protobuf/minprotobuf,
|
2021-06-15 08:55:47 +00:00
|
|
|
libp2p/protocols/ping,
|
2021-06-02 07:53:34 +00:00
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/multistream,
|
|
|
|
../../waku/v2/node/wakunode2,
|
2021-10-06 12:29:08 +00:00
|
|
|
../../waku/v2/utils/peers,
|
2021-06-02 07:53:34 +00:00
|
|
|
../test_helpers, ./utils
|
|
|
|
|
|
|
|
procSuite "Waku Keepalive":
|
|
|
|
|
2021-06-15 08:55:47 +00:00
|
|
|
asyncTest "handle ping keepalives":
|
2021-06-02 07:53:34 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60000))
|
2021-06-02 07:53:34 +00:00
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2021-06-15 08:55:47 +00:00
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
proc pingHandler(peerId: PeerID) {.async, gcsafe, raises: [Defect].} =
|
2021-06-15 08:55:47 +00:00
|
|
|
debug "Ping received"
|
|
|
|
|
|
|
|
check:
|
2021-10-06 12:29:08 +00:00
|
|
|
peerId == node1.switch.peerInfo.peerId
|
2021-06-15 08:55:47 +00:00
|
|
|
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
2021-06-02 07:53:34 +00:00
|
|
|
await node1.start()
|
|
|
|
node1.mountRelay()
|
2021-06-15 08:55:47 +00:00
|
|
|
node1.mountLibp2pPing()
|
2021-06-02 07:53:34 +00:00
|
|
|
|
|
|
|
await node2.start()
|
|
|
|
node2.mountRelay()
|
2021-06-15 08:55:47 +00:00
|
|
|
node2.switch.mount(Ping.new(handler = pingHandler))
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-06-02 07:53:34 +00:00
|
|
|
|
|
|
|
node1.startKeepalive()
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop()])
|