2021-11-01 18:02:39 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-04-05 07:46:13 +00:00
|
|
|
stew/[results, byteutils],
|
|
|
|
stew/shims/net,
|
2021-11-01 18:02:39 +00:00
|
|
|
chronos,
|
2023-02-07 13:06:50 +00:00
|
|
|
chronicles,
|
2021-11-01 18:02:39 +00:00
|
|
|
testutils/unittests,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
eth/keys,
|
2022-11-04 09:52:08 +00:00
|
|
|
eth/p2p/discoveryv5/enr
|
|
|
|
import
|
2023-04-05 07:46:13 +00:00
|
|
|
../../waku/v2/waku_node,
|
2023-04-19 11:29:23 +00:00
|
|
|
../../waku/v2/waku_core,
|
2023-04-18 13:22:10 +00:00
|
|
|
../../waku/v2/waku_discv5,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore,
|
|
|
|
./testlib/wakunode
|
2021-11-01 18:02:39 +00:00
|
|
|
|
|
|
|
procSuite "Waku Discovery v5":
|
|
|
|
asyncTest "Waku Discovery v5 end-to-end":
|
|
|
|
## Tests integrated discovery v5
|
|
|
|
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(61500)
|
2021-11-01 18:02:39 +00:00
|
|
|
nodeUdpPort1 = Port(9000)
|
2023-04-05 14:01:51 +00:00
|
|
|
node1 = newTestWakuNode(nodeKey1, bindIp, nodeTcpPort1)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey2 = generateSecp256k1Key()
|
2022-11-03 13:47:56 +00:00
|
|
|
nodeTcpPort2 = Port(61502)
|
2021-11-01 18:02:39 +00:00
|
|
|
nodeUdpPort2 = Port(9002)
|
2023-04-05 14:01:51 +00:00
|
|
|
node2 = newTestWakuNode(nodeKey2, bindIp, nodeTcpPort2)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey3 = generateSecp256k1Key()
|
2022-11-03 13:47:56 +00:00
|
|
|
nodeTcpPort3 = Port(61504)
|
2021-11-01 18:02:39 +00:00
|
|
|
nodeUdpPort3 = Port(9004)
|
2023-04-05 14:01:51 +00:00
|
|
|
node3 = newTestWakuNode(nodeKey3, bindIp, nodeTcpPort3)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-03-08 14:44:10 +00:00
|
|
|
flags = CapabilitiesBitfield.init(
|
|
|
|
lightpush = false,
|
|
|
|
filter = false,
|
|
|
|
store = false,
|
|
|
|
relay = true
|
|
|
|
)
|
2021-11-12 14:10:54 +00:00
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
# E2E relay test paramaters
|
|
|
|
pubSubTopic = "/waku/2/default-waku/proto"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "Can you see me?".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2021-11-01 18:02:39 +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](),
|
2021-11-01 18:02:39 +00:00
|
|
|
false,
|
|
|
|
keys.PrivateKey(nodeKey1.skkey),
|
2021-11-12 14:10:54 +00:00
|
|
|
flags,
|
2023-02-07 13:06:50 +00:00
|
|
|
newSeq[MultiAddress](), # Empty multiaddr fields, for now
|
2021-11-01 18:02:39 +00:00
|
|
|
node1.rng
|
|
|
|
)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
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
|
2021-11-01 18:02:39 +00:00
|
|
|
false,
|
|
|
|
keys.PrivateKey(nodeKey2.skkey),
|
2021-11-12 14:10:54 +00:00
|
|
|
flags,
|
2023-02-07 13:06:50 +00:00
|
|
|
newSeq[MultiAddress](), # Empty multiaddr fields, for now
|
2021-11-01 18:02:39 +00:00
|
|
|
node2.rng
|
|
|
|
)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
node3.wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
some(extIp), some(nodeTcpPort3), some(nodeUdpPort3),
|
|
|
|
bindIp,
|
|
|
|
nodeUdpPort3,
|
2023-03-30 07:35:13 +00:00
|
|
|
@[node2.wakuDiscv5.protocol.localNode.record], # Bootstrap with node2
|
2021-11-01 18:02:39 +00:00
|
|
|
false,
|
|
|
|
keys.PrivateKey(nodeKey3.skkey),
|
2021-11-12 14:10:54 +00:00
|
|
|
flags,
|
2023-02-07 13:06:50 +00:00
|
|
|
newSeq[MultiAddress](), # Empty multiaddr fields, for now
|
2021-11-01 18:02:39 +00:00
|
|
|
node3.rng
|
|
|
|
)
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
|
|
|
await node3.mountRelay()
|
2021-11-01 18:02:39 +00:00
|
|
|
|
|
|
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
|
|
|
|
|
|
|
await allFutures([node1.startDiscv5(), node2.startDiscv5(), node3.startDiscv5()])
|
|
|
|
|
|
|
|
await sleepAsync(3000.millis) # Give the algorithm some time to work its magic
|
|
|
|
check:
|
|
|
|
node1.wakuDiscv5.protocol.nodesDiscovered > 0
|
|
|
|
node2.wakuDiscv5.protocol.nodesDiscovered > 0
|
|
|
|
node3.wakuDiscv5.protocol.nodesDiscovered > 0
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
# Let's see if we can deliver a message end-to-end
|
2021-11-03 10:59:51 +00:00
|
|
|
# var completionFut = newFuture[bool]()
|
|
|
|
# proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2022-11-07 15:24:16 +00:00
|
|
|
# let msg = WakuMessage.decode(data)
|
2021-11-03 10:59:51 +00:00
|
|
|
# if msg.isOk():
|
|
|
|
# let val = msg.value()
|
|
|
|
# check:
|
|
|
|
# topic == pubSubTopic
|
|
|
|
# val.contentTopic == contentTopic
|
|
|
|
# val.payload == payload
|
|
|
|
# completionFut.complete(true)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2021-11-03 10:59:51 +00:00
|
|
|
# node3.subscribe(pubSubTopic, relayHandler)
|
|
|
|
# await sleepAsync(2000.millis)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2021-11-03 10:59:51 +00:00
|
|
|
# await node1.publish(pubSubTopic, message)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2021-11-03 10:59:51 +00:00
|
|
|
# check:
|
|
|
|
# (await completionFut.withTimeout(6.seconds)) == true
|
2021-11-01 18:02:39 +00:00
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
2023-02-07 13:06:50 +00:00
|
|
|
|
|
|
|
asyncTest "Custom multiaddresses are advertised correctly":
|
|
|
|
let
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
|
|
|
expectedMultiAddr = MultiAddress.init("/ip4/200.200.200.200/tcp/9000/wss").tryGet()
|
|
|
|
|
2023-03-08 14:44:10 +00:00
|
|
|
flags = CapabilitiesBitfield.init(
|
|
|
|
lightpush = false,
|
|
|
|
filter = false,
|
|
|
|
store = false,
|
|
|
|
relay = true
|
|
|
|
)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
|
|
|
nodeTcpPort1 = Port(9010)
|
|
|
|
nodeUdpPort1 = Port(9012)
|
2023-02-13 10:43:49 +00:00
|
|
|
node1Key = generateSecp256k1Key()
|
2023-02-07 13:06:50 +00:00
|
|
|
node1NetConfig = NetConfig.init(bindIp = bindIp,
|
|
|
|
extIp = some(extIp),
|
|
|
|
extPort = some(nodeTcpPort1),
|
|
|
|
bindPort = nodeTcpPort1,
|
|
|
|
extmultiAddrs = @[expectedMultiAddr],
|
|
|
|
wakuFlags = some(flags),
|
2023-04-05 07:46:13 +00:00
|
|
|
discv5UdpPort = some(nodeUdpPort1)).get()
|
2023-02-07 13:06:50 +00:00
|
|
|
node1discV5 = WakuDiscoveryV5.new(extIp = node1NetConfig.extIp,
|
|
|
|
extTcpPort = node1NetConfig.extPort,
|
|
|
|
extUdpPort = node1NetConfig.discv5UdpPort,
|
|
|
|
bindIp = node1NetConfig.bindIp,
|
|
|
|
discv5UdpPort = node1NetConfig.discv5UdpPort.get(),
|
|
|
|
privateKey = keys.PrivateKey(node1Key.skkey),
|
|
|
|
multiaddrs = node1NetConfig.enrMultiaddrs,
|
|
|
|
flags = node1NetConfig.wakuFlags.get(),
|
|
|
|
rng = rng)
|
|
|
|
node1 = WakuNode.new(nodekey = node1Key,
|
|
|
|
netConfig = node1NetConfig,
|
|
|
|
wakuDiscv5 = some(node1discV5),
|
|
|
|
rng = rng)
|
|
|
|
|
|
|
|
|
|
|
|
nodeTcpPort2 = Port(9014)
|
|
|
|
nodeUdpPort2 = Port(9016)
|
2023-02-13 10:43:49 +00:00
|
|
|
node2Key = generateSecp256k1Key()
|
2023-02-07 13:06:50 +00:00
|
|
|
node2NetConfig = NetConfig.init(bindIp = bindIp,
|
|
|
|
extIp = some(extIp),
|
|
|
|
extPort = some(nodeTcpPort2),
|
|
|
|
bindPort = nodeTcpPort2,
|
|
|
|
wakuFlags = some(flags),
|
2023-04-05 07:46:13 +00:00
|
|
|
discv5UdpPort = some(nodeUdpPort2)).get()
|
2023-02-07 13:06:50 +00:00
|
|
|
node2discV5 = WakuDiscoveryV5.new(extIp = node2NetConfig.extIp,
|
|
|
|
extTcpPort = node2NetConfig.extPort,
|
|
|
|
extUdpPort = node2NetConfig.discv5UdpPort,
|
|
|
|
bindIp = node2NetConfig.bindIp,
|
|
|
|
discv5UdpPort = node2NetConfig.discv5UdpPort.get(),
|
|
|
|
bootstrapEnrs = @[node1.wakuDiscv5.protocol.localNode.record],
|
|
|
|
privateKey = keys.PrivateKey(node2Key.skkey),
|
|
|
|
flags = node2NetConfig.wakuFlags.get(),
|
|
|
|
rng = rng)
|
|
|
|
node2 = WakuNode.new(nodeKey = node2Key,
|
|
|
|
netConfig = node2NetConfig,
|
|
|
|
wakuDiscv5 = some(node2discV5))
|
|
|
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
|
|
|
await allFutures([node1.startDiscv5(), node2.startDiscv5()])
|
|
|
|
|
|
|
|
await sleepAsync(3000.millis) # Give the algorithm some time to work its magic
|
|
|
|
|
|
|
|
let node1Enr = node2.wakuDiscv5.protocol.routingTable.buckets[0].nodes[0].record
|
2023-03-30 07:35:13 +00:00
|
|
|
let multiaddrs = node1Enr.toTyped().get().multiaddrs.get()
|
2023-02-07 13:06:50 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
node1.wakuDiscv5.protocol.nodesDiscovered > 0
|
|
|
|
node2.wakuDiscv5.protocol.nodesDiscovered > 0
|
|
|
|
multiaddrs.contains(expectedMultiAddr)
|
|
|
|
|