2020-08-31 03:32:41 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2021-03-26 09:52:04 +00:00
|
|
|
testutils/unittests,
|
2021-11-10 12:05:36 +00:00
|
|
|
chronicles, chronos, stew/shims/net as stewNet, stew/byteutils, std/os,
|
2020-08-31 03:32:41 +00:00
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/crypto/secp,
|
2021-01-25 11:03:52 +00:00
|
|
|
libp2p/multiaddress,
|
2020-09-02 03:15:25 +00:00
|
|
|
libp2p/switch,
|
2021-03-11 23:42:26 +00:00
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2021-04-01 09:37:14 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
2022-02-16 16:12:09 +00:00
|
|
|
libp2p/nameresolving/mockresolver,
|
2020-08-31 03:32:41 +00:00
|
|
|
eth/keys,
|
2021-07-13 07:18:51 +00:00
|
|
|
../../waku/v2/protocol/[waku_relay, waku_message],
|
2022-08-12 10:15:51 +00:00
|
|
|
../../waku/v2/protocol/waku_filter,
|
2021-04-24 04:56:37 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2021-01-25 11:03:52 +00:00
|
|
|
../../waku/v2/utils/peers,
|
2022-06-28 23:59:38 +00:00
|
|
|
../../waku/v2/node/wakunode2
|
2020-07-29 13:24:01 +00:00
|
|
|
|
2022-08-01 16:21:11 +00:00
|
|
|
|
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
template sourceDir: string = currentSourcePath.parentDir()
|
|
|
|
const KEY_PATH = sourceDir / "resources/test_key.pem"
|
|
|
|
const CERT_PATH = sourceDir / "resources/test_cert.pem"
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
procSuite "WakuNode":
|
2022-09-07 15:31:27 +00:00
|
|
|
let rng = crypto.newRng()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2020-10-22 11:12:00 +00:00
|
|
|
asyncTest "Messages are correctly relayed":
|
|
|
|
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"),
|
2020-10-22 11:12:00 +00:00
|
|
|
Port(60000))
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
2020-10-22 11:12:00 +00:00
|
|
|
Port(60002))
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"),
|
2020-10-22 11:12:00 +00:00
|
|
|
Port(60003))
|
|
|
|
pubSubTopic = "test"
|
2021-04-08 09:55:19 +00:00
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
2020-10-22 11:12:00 +00:00
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2020-10-22 11:12:00 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2020-10-22 11:12:00 +00:00
|
|
|
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node3.mountRelay(@[pubSubTopic])
|
2020-10-22 11:12:00 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
await node3.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2020-10-22 11:12:00 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
2021-02-02 11:33:59 +00:00
|
|
|
node3.subscribe(pubSubTopic, relayHandler)
|
2020-10-22 11:12:00 +00:00
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
2020-12-02 08:40:53 +00:00
|
|
|
await node1.publish(pubSubTopic, message)
|
2020-10-22 11:12:00 +00:00
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
|
|
|
await node3.stop()
|
2021-06-29 14:29:04 +00:00
|
|
|
|
|
|
|
asyncTest "Protocol matcher works as expected":
|
|
|
|
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"),
|
2021-06-29 14:29:04 +00:00
|
|
|
Port(60000))
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
2021-06-29 14:29:04 +00:00
|
|
|
Port(60002))
|
|
|
|
pubSubTopic = "/waku/2/default-waku/proto"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
# Setup node 1 with stable codec "/vac/waku/relay/2.0.0"
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-06-29 14:29:04 +00:00
|
|
|
node1.wakuRelay.codec = "/vac/waku/relay/2.0.0"
|
|
|
|
|
|
|
|
# Setup node 2 with beta codec "/vac/waku/relay/2.0.0-beta2"
|
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-06-29 14:29:04 +00:00
|
|
|
node2.wakuRelay.codec = "/vac/waku/relay/2.0.0-beta2"
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Check that mounted codecs are actually different
|
|
|
|
node1.wakuRelay.codec == "/vac/waku/relay/2.0.0"
|
|
|
|
node2.wakuRelay.codec == "/vac/waku/relay/2.0.0-beta2"
|
|
|
|
|
|
|
|
# Now verify that protocol matcher returns `true` and relay works
|
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-06-29 14:29:04 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node2.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
await node1.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
2021-01-25 11:03:52 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "resolve and connect to dns multiaddrs":
|
|
|
|
let resolver = MockResolver.new()
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
resolver.ipResponses[("localhost", false)] = @["127.0.0.1"]
|
|
|
|
|
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60000), nameResolver = resolver)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
# Construct DNS multiaddr for node2
|
|
|
|
let
|
|
|
|
node2PeerId = $(node2.switch.peerInfo.peerId)
|
|
|
|
node2Dns4Addr = "/dns4/localhost/tcp/60002/p2p/" & node2PeerId
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
|
|
|
await node1.connectToNodes(@[node2Dns4Addr])
|
|
|
|
|
|
|
|
check:
|
|
|
|
node1.switch.connManager.connCount(node2.switch.peerInfo.peerId) == 1
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop()])
|
|
|
|
|
2021-03-11 23:42:26 +00:00
|
|
|
asyncTest "filtering relayed messages using topic validators":
|
2022-05-16 10:51:10 +00:00
|
|
|
## test scenario:
|
2021-03-11 23:42:26 +00:00
|
|
|
## node1 and node3 set node2 as their relay node
|
2022-05-16 10:51:10 +00:00
|
|
|
## node3 publishes two messages with two different contentTopics but on the same pubsub topic
|
|
|
|
## node1 is also subscribed to the same pubsub topic
|
2021-03-11 23:42:26 +00:00
|
|
|
## node2 sets a validator for the same pubsub topic
|
|
|
|
## only one of the messages gets delivered to node1 because the validator only validates one of the content topics
|
|
|
|
|
|
|
|
let
|
|
|
|
# publisher node
|
|
|
|
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-03-11 23:42:26 +00:00
|
|
|
# Relay node
|
|
|
|
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-03-11 23:42:26 +00:00
|
|
|
# Subscriber
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"), Port(60003))
|
2021-03-11 23:42:26 +00:00
|
|
|
|
|
|
|
pubSubTopic = "test"
|
2021-04-08 09:55:19 +00:00
|
|
|
contentTopic1 = ContentTopic("/waku/2/default-content/proto")
|
2021-03-11 23:42:26 +00:00
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message1 = WakuMessage(payload: payload, contentTopic: contentTopic1)
|
|
|
|
|
|
|
|
payload2 = "you should not see this message!".toBytes()
|
2021-04-08 09:55:19 +00:00
|
|
|
contentTopic2 = ContentTopic("2")
|
2021-03-11 23:42:26 +00:00
|
|
|
message2 = WakuMessage(payload: payload2, contentTopic: contentTopic2)
|
|
|
|
|
|
|
|
# start all the nodes
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-03-11 23:42:26 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-03-11 23:42:26 +00:00
|
|
|
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node3.mountRelay(@[pubSubTopic])
|
2021-03-11 23:42:26 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
await node3.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-03-11 23:42:26 +00:00
|
|
|
|
|
|
|
var completionFutValidatorAcc = newFuture[bool]()
|
|
|
|
var completionFutValidatorRej = newFuture[bool]()
|
|
|
|
|
|
|
|
proc validator(topic: string, message: messages.Message): Future[ValidationResult] {.async.} =
|
|
|
|
## the validator that only allows messages with contentTopic1 to be relayed
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
2022-05-16 10:51:10 +00:00
|
|
|
let msg = WakuMessage.init(message.data)
|
2021-03-11 23:42:26 +00:00
|
|
|
if msg.isOk():
|
|
|
|
# only relay messages with contentTopic1
|
|
|
|
if msg.value().contentTopic == contentTopic1:
|
|
|
|
result = ValidationResult.Accept
|
|
|
|
completionFutValidatorAcc.complete(true)
|
|
|
|
else:
|
|
|
|
result = ValidationResult.Reject
|
|
|
|
completionFutValidatorRej.complete(true)
|
|
|
|
|
2022-05-16 10:51:10 +00:00
|
|
|
# set a topic validator for pubSubTopic
|
2021-03-11 23:42:26 +00:00
|
|
|
let pb = PubSub(node2.wakuRelay)
|
|
|
|
pb.addValidator(pubSubTopic, validator)
|
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
debug "relayed pubsub topic:", topic
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
# check that only messages with contentTopic1 is relayed (but not contentTopic2)
|
|
|
|
val.contentTopic == contentTopic1
|
|
|
|
# relay handler is called
|
|
|
|
completionFut.complete(true)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-03-11 23:42:26 +00:00
|
|
|
|
|
|
|
node3.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
await node1.publish(pubSubTopic, message1)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-03-11 23:42:26 +00:00
|
|
|
# message2 never gets relayed because of the validator
|
|
|
|
await node1.publish(pubSubTopic, message2)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(10.seconds)) == true
|
|
|
|
# check that validator is called for message1
|
|
|
|
(await completionFutValidatorAcc.withTimeout(10.seconds)) == true
|
|
|
|
# check that validator is called for message2
|
|
|
|
(await completionFutValidatorRej.withTimeout(10.seconds)) == true
|
|
|
|
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-03-11 23:42:26 +00:00
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
|
|
|
await node3.stop()
|
2022-09-06 18:43:46 +00:00
|
|
|
|
2021-04-01 09:37:14 +00:00
|
|
|
asyncTest "Relay protocol is started correctly":
|
|
|
|
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"),
|
2021-04-01 09:37:14 +00:00
|
|
|
Port(60000))
|
|
|
|
|
|
|
|
# Relay protocol starts if mounted after node start
|
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
2021-04-01 09:37:14 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
GossipSub(node1.wakuRelay).heartbeatFut.isNil == false
|
|
|
|
|
|
|
|
# Relay protocol starts if mounted before node start
|
|
|
|
|
|
|
|
let
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2021-07-14 17:58:46 +00:00
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
2021-04-01 09:37:14 +00:00
|
|
|
Port(60002))
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay()
|
2021-04-01 09:37:14 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Relay has not yet started as node has not yet started
|
|
|
|
GossipSub(node2.wakuRelay).heartbeatFut.isNil
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-04-01 09:37:14 +00:00
|
|
|
await node2.start()
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Relay started on node start
|
|
|
|
GossipSub(node2.wakuRelay).heartbeatFut.isNil == false
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-04-01 09:37:14 +00:00
|
|
|
await allFutures([node1.stop(), node2.stop()])
|
|
|
|
|
2021-10-12 12:48:48 +00:00
|
|
|
asyncTest "Maximum connections can be configured":
|
|
|
|
let
|
|
|
|
maxConnections = 2
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
Port(60010), maxConnections = maxConnections)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
Port(60012))
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
Port(60013))
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-10-12 12:48:48 +00:00
|
|
|
check:
|
|
|
|
# Sanity check, to verify config was applied
|
|
|
|
node1.switch.connManager.inSema.size == maxConnections
|
|
|
|
|
|
|
|
# Node with connection limit set to 1
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
2021-10-12 12:48:48 +00:00
|
|
|
|
|
|
|
# Remote node 1
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay()
|
2021-10-12 12:48:48 +00:00
|
|
|
|
|
|
|
# Remote node 2
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node3.mountRelay()
|
2021-10-12 12:48:48 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
discard await node1.peerManager.dialPeer(node2.switch.peerInfo.toRemotePeerInfo(), WakuRelayCodec)
|
2021-10-12 12:48:48 +00:00
|
|
|
await sleepAsync(3.seconds)
|
2022-01-10 15:07:35 +00:00
|
|
|
discard await node1.peerManager.dialPeer(node3.switch.peerInfo.toRemotePeerInfo(), WakuRelayCodec)
|
2021-10-12 12:48:48 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Verify that only the first connection succeeded
|
2022-01-10 15:07:35 +00:00
|
|
|
node1.switch.isConnected(node2.switch.peerInfo.peerId)
|
|
|
|
node1.switch.isConnected(node3.switch.peerInfo.peerId) == false
|
2021-10-12 12:48:48 +00:00
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages are relayed between two websocket nodes":
|
2021-11-02 10:29:11 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000), wsBindPort = Port(8000), wsEnabled = true)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60002), wsBindPort = Port(8100), wsEnabled = true)
|
|
|
|
pubSubTopic = "test"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node1.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
await node2.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
|
|
|
|
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages are relayed between nodes with multiple transports (TCP and Websockets)":
|
2021-11-02 10:29:11 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000), wsBindPort = Port(8000), wsEnabled = true)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60002))
|
|
|
|
pubSubTopic = "test"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node1.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
await node2.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages relaying fails with non-overlapping transports (TCP or Websockets)":
|
2021-11-02 10:29:11 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000))
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60002), wsBindPort = Port(8100), wsEnabled = true)
|
|
|
|
pubSubTopic = "test"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
#delete websocket peer address
|
2021-12-06 19:51:37 +00:00
|
|
|
# TODO: a better way to find the index - this is too brittle
|
2022-01-10 15:07:35 +00:00
|
|
|
node2.switch.peerInfo.addrs.delete(0)
|
2021-11-02 10:29:11 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node1.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
await node2.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == false
|
|
|
|
await node1.stop()
|
2021-11-10 12:05:36 +00:00
|
|
|
await node2.stop()
|
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages are relayed between nodes with multiple transports (TCP and secure Websockets)":
|
2021-11-10 12:05:36 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000), wsBindPort = Port(8000), wssEnabled = true, secureKey = KEY_PATH, secureCert = CERT_PATH)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60002))
|
|
|
|
pubSubTopic = "test"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node1.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
await node2.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
|
|
|
await node2.stop()
|
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages fails with wrong key path":
|
2021-11-10 12:05:36 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
expect IOError:
|
|
|
|
# gibberish
|
|
|
|
discard WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000), wsBindPort = Port(8000), wssEnabled = true, secureKey = "../../waku/v2/node/key_dummy.txt")
|
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Messages are relayed between nodes with multiple transports (websocket and secure Websockets)":
|
2021-11-10 12:05:36 +00:00
|
|
|
let
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60000), wsBindPort = Port(8000), wssEnabled = true, secureKey = KEY_PATH, secureCert = CERT_PATH)
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
bindPort = Port(60002),wsBindPort = Port(8100), wsEnabled = true )
|
|
|
|
pubSubTopic = "test"
|
|
|
|
contentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
payload = "hello world".toBytes()
|
|
|
|
message = WakuMessage(payload: payload, contentTopic: contentTopic)
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
2022-01-10 15:07:35 +00:00
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
let val = msg.value()
|
|
|
|
check:
|
|
|
|
topic == pubSubTopic
|
|
|
|
val.contentTopic == contentTopic
|
|
|
|
val.payload == payload
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
node1.subscribe(pubSubTopic, relayHandler)
|
|
|
|
await sleepAsync(2000.millis)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
await node2.publish(pubSubTopic, message)
|
|
|
|
await sleepAsync(2000.millis)
|
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
|
|
|
await node1.stop()
|
2021-11-30 09:12:09 +00:00
|
|
|
await node2.stop()
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
asyncTest "Peer info updates with correct announced addresses":
|
|
|
|
let
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
bindPort = Port(60000)
|
|
|
|
extIp = some(ValidIpAddress.init("127.0.0.1"))
|
|
|
|
extPort = some(Port(60002))
|
|
|
|
node = WakuNode.new(
|
|
|
|
nodeKey,
|
|
|
|
bindIp, bindPort,
|
|
|
|
extIp, extPort)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
let
|
|
|
|
bindEndpoint = MultiAddress.init(bindIp, tcpProtocol, bindPort)
|
|
|
|
announcedEndpoint = MultiAddress.init(extIp.get(), tcpProtocol, extPort.get())
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Check that underlying peer info contains only bindIp before starting
|
|
|
|
node.switch.peerInfo.addrs.len == 1
|
|
|
|
node.switch.peerInfo.addrs.contains(bindEndpoint)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
node.announcedAddresses.len == 1
|
|
|
|
node.announcedAddresses.contains(announcedEndpoint)
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
await node.start()
|
2021-11-30 09:12:09 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
check:
|
|
|
|
# Check that underlying peer info is updated with announced address
|
|
|
|
node.started
|
|
|
|
node.switch.peerInfo.addrs.len == 1
|
|
|
|
node.switch.peerInfo.addrs.contains(announcedEndpoint)
|
2021-11-30 09:12:09 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
await node.stop()
|
2022-02-18 11:10:38 +00:00
|
|
|
|
|
|
|
asyncTest "Node can use dns4 in announced addresses":
|
|
|
|
let
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
bindPort = Port(60000)
|
|
|
|
extIp = some(ValidIpAddress.init("127.0.0.1"))
|
|
|
|
extPort = some(Port(60002))
|
|
|
|
domainName = "example.com"
|
|
|
|
expectedDns4Addr = MultiAddress.init("/dns4/" & domainName & "/tcp/" & $(extPort.get())).get()
|
|
|
|
node = WakuNode.new(
|
|
|
|
nodeKey,
|
|
|
|
bindIp, bindPort,
|
|
|
|
extIp, extPort,
|
|
|
|
dns4DomainName = some(domainName))
|
2022-05-16 10:51:10 +00:00
|
|
|
|
2022-02-18 11:10:38 +00:00
|
|
|
check:
|
|
|
|
node.announcedAddresses.len == 1
|
|
|
|
node.announcedAddresses.contains(expectedDns4Addr)
|