2020-08-31 03:32:41 +00:00
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
|
|
import
|
2022-11-04 09:52:08 +00:00
|
|
|
|
stew/byteutils,
|
|
|
|
|
stew/shims/net as stewNet,
|
2021-03-26 09:52:04 +00:00
|
|
|
|
testutils/unittests,
|
2022-11-04 09:52:08 +00:00
|
|
|
|
chronicles,
|
|
|
|
|
chronos,
|
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-11-04 09:52:08 +00:00
|
|
|
|
libp2p/nameresolving/mockresolver
|
|
|
|
|
import
|
|
|
|
|
../../waku/v2/node/waku_node,
|
2021-04-24 04:56:37 +00:00
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-11-04 09:52:08 +00:00
|
|
|
|
../../waku/v2/protocol/waku_message,
|
|
|
|
|
../../waku/v2/protocol/waku_relay,
|
|
|
|
|
../../waku/v2/utils/peers
|
2020-07-29 13:24:01 +00:00
|
|
|
|
|
2022-08-01 16:21:11 +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
|
|
|
|
|
2021-06-29 14:29:04 +00:00
|
|
|
|
asyncTest "Protocol matcher works as expected":
|
|
|
|
|
let
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(61000))
|
2021-06-29 14:29:04 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(61002))
|
2021-06-29 14:29:04 +00:00
|
|
|
|
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.} =
|
2022-11-07 15:24:16 +00:00
|
|
|
|
let msg = WakuMessage.decode(data)
|
2021-06-29 14:29:04 +00:00
|
|
|
|
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
|
2022-09-12 12:51:52 +00:00
|
|
|
|
|
|
|
|
|
await allFutures(node1.stop(), 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[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(61020), nameResolver = resolver)
|
2022-02-16 16:12:09 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(61022))
|
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)
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2Dns4Addr = "/dns4/localhost/tcp/61022/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-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-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"),
|
2022-11-03 13:47:56 +00:00
|
|
|
|
bindPort = Port(61004),
|
|
|
|
|
wsBindPort = Port(8000),
|
|
|
|
|
wssEnabled = true,
|
|
|
|
|
secureKey = "../../waku/v2/node/key_dummy.txt")
|
2021-11-10 12:05:36 +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")
|
2022-11-03 13:47:56 +00:00
|
|
|
|
bindPort = Port(61006)
|
2022-02-16 16:12:09 +00:00
|
|
|
|
extIp = some(ValidIpAddress.init("127.0.0.1"))
|
2022-11-03 13:47:56 +00:00
|
|
|
|
extPort = some(Port(61008))
|
2022-02-16 16:12:09 +00:00
|
|
|
|
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
|
2022-10-28 09:51:46 +00:00
|
|
|
|
node.switch.peerInfo.listenAddrs.len == 1
|
|
|
|
|
node.switch.peerInfo.listenAddrs.contains(bindEndpoint)
|
|
|
|
|
# Underlying peer info has not updated addrs before starting
|
|
|
|
|
node.switch.peerInfo.addrs.len == 0
|
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:
|
|
|
|
|
node.started
|
2022-10-28 09:51:46 +00:00
|
|
|
|
# Underlying peer info listenAddrs has not changed
|
|
|
|
|
node.switch.peerInfo.listenAddrs.len == 1
|
|
|
|
|
node.switch.peerInfo.listenAddrs.contains(bindEndpoint)
|
|
|
|
|
# Check that underlying peer info is updated with announced address
|
2022-02-16 16:12:09 +00:00
|
|
|
|
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")
|
2022-11-03 13:47:56 +00:00
|
|
|
|
bindPort = Port(61010)
|
2022-02-18 11:10:38 +00:00
|
|
|
|
extIp = some(ValidIpAddress.init("127.0.0.1"))
|
2022-11-03 13:47:56 +00:00
|
|
|
|
extPort = some(Port(61012))
|
2022-02-18 11:10:38 +00:00
|
|
|
|
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)
|
2022-10-28 13:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncTest "Agent string is set and advertised correctly":
|
|
|
|
|
let
|
|
|
|
|
# custom agent string
|
|
|
|
|
expectedAgentString1 = "node1-agent-string"
|
|
|
|
|
|
|
|
|
|
# bump when updating nim-libp2p
|
|
|
|
|
expectedAgentString2 = "nim-libp2p/0.0.1"
|
|
|
|
|
let
|
|
|
|
|
# node with custom agent string
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(61014),
|
2022-10-28 13:12:06 +00:00
|
|
|
|
agentString = some(expectedAgentString1))
|
|
|
|
|
|
|
|
|
|
# node with default agent string from libp2p
|
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(61016))
|
2022-10-28 13:12:06 +00:00
|
|
|
|
|
|
|
|
|
await node1.start()
|
|
|
|
|
await node1.mountRelay()
|
|
|
|
|
|
|
|
|
|
await node2.start()
|
|
|
|
|
await node2.mountRelay()
|
|
|
|
|
|
|
|
|
|
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
|
await node2.connectToNodes(@[node1.switch.peerInfo.toRemotePeerInfo()])
|
|
|
|
|
|
|
|
|
|
let node1Agent = node2.switch.peerStore[AgentBook][node1.switch.peerInfo.toRemotePeerInfo().peerId]
|
|
|
|
|
let node2Agent = node1.switch.peerStore[AgentBook][node2.switch.peerInfo.toRemotePeerInfo().peerId]
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
node1Agent == expectedAgentString1
|
|
|
|
|
node2Agent == expectedAgentString2
|
|
|
|
|
|
|
|
|
|
await allFutures(node1.stop(), node2.stop())
|