2021-03-23 08:04:51 +00:00
|
|
|
|
{.used.}
|
|
|
|
|
|
2020-11-24 03:44:37 +00:00
|
|
|
|
import
|
2021-07-21 18:05:10 +00:00
|
|
|
|
std/[options, sets, tables, os, strutils, sequtils, times],
|
2022-01-14 09:25:01 +00:00
|
|
|
|
chronicles,
|
2021-03-26 09:52:04 +00:00
|
|
|
|
testutils/unittests, stew/shims/net as stewNet,
|
2020-11-24 03:44:37 +00:00
|
|
|
|
json_rpc/[rpcserver, rpcclient],
|
2022-11-02 10:59:58 +00:00
|
|
|
|
eth/keys, eth/common/eth_types,
|
2021-06-09 14:37:08 +00:00
|
|
|
|
libp2p/[builders, switch, multiaddress],
|
2020-11-24 03:44:37 +00:00
|
|
|
|
libp2p/protobuf/minprotobuf,
|
|
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
|
libp2p/crypto/crypto,
|
2020-11-27 07:18:48 +00:00
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2022-09-20 09:39:52 +00:00
|
|
|
|
libp2p/protocols/pubsub/rpc/message
|
|
|
|
|
import
|
2020-12-23 09:33:28 +00:00
|
|
|
|
../../waku/v1/node/rpc/hexstrings,
|
2023-01-26 11:05:25 +00:00
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-10-18 14:05:53 +00:00
|
|
|
|
../../waku/v2/node/waku_node,
|
2020-12-23 09:33:28 +00:00
|
|
|
|
../../waku/v2/node/jsonrpc/[store_api,
|
|
|
|
|
relay_api,
|
|
|
|
|
debug_api,
|
|
|
|
|
filter_api,
|
|
|
|
|
admin_api,
|
|
|
|
|
private_api],
|
2022-09-06 22:18:56 +00:00
|
|
|
|
../../waku/v2/protocol/waku_message,
|
2021-02-05 10:49:11 +00:00
|
|
|
|
../../waku/v2/protocol/waku_relay,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
../../waku/v2/protocol/waku_archive,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/driver/queue_driver,
|
2022-07-25 11:01:37 +00:00
|
|
|
|
../../waku/v2/protocol/waku_store,
|
2022-11-09 17:50:18 +00:00
|
|
|
|
../../waku/v2/protocol/waku_store/rpc,
|
2020-12-15 15:48:00 +00:00
|
|
|
|
../../waku/v2/protocol/waku_swap/waku_swap,
|
2022-08-12 10:15:51 +00:00
|
|
|
|
../../waku/v2/protocol/waku_filter,
|
2022-11-16 19:02:38 +00:00
|
|
|
|
../../waku/v2/protocol/waku_filter/rpc,
|
2022-11-02 10:59:58 +00:00
|
|
|
|
../../waku/v2/protocol/waku_filter/client,
|
2021-10-06 12:29:08 +00:00
|
|
|
|
../../waku/v2/utils/peers,
|
2022-10-28 18:11:28 +00:00
|
|
|
|
../../waku/v2/utils/time,
|
2023-01-26 11:05:25 +00:00
|
|
|
|
./testlib/common,
|
|
|
|
|
../test_helpers
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
|
|
|
|
template sourceDir*: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
|
const sigPath = sourceDir / ParDir / ParDir / "waku" / "v2" / "node" / "jsonrpc" / "jsonrpc_callsigs.nim"
|
|
|
|
|
createRpcSigs(RpcHttpClient, sigPath)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
proc put(store: ArchiveDriver, pubsubTopic: PubsubTopic, message: WakuMessage): Result[void, string] =
|
|
|
|
|
let
|
|
|
|
|
digest = waku_archive.computeDigest(message)
|
|
|
|
|
receivedTime = if message.timestamp > 0: message.timestamp
|
|
|
|
|
else: getNanosecondTime(getTime().toUnixFloat())
|
|
|
|
|
|
|
|
|
|
store.put(pubsubTopic, message, digest, receivedTime)
|
|
|
|
|
|
2020-11-27 07:18:48 +00:00
|
|
|
|
procSuite "Waku v2 JSON-RPC API":
|
|
|
|
|
let
|
|
|
|
|
rng = crypto.newRng()
|
|
|
|
|
privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet()
|
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
|
|
|
|
port = Port(9000)
|
2021-07-14 17:58:46 +00:00
|
|
|
|
node = WakuNode.new(privkey, bindIp, port, some(extIp), some(port))
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
asyncTest "Debug API: get node info":
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.start()
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node.mountRelay()
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
2020-11-24 03:44:37 +00:00
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8546)
|
2020-11-27 07:18:48 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
|
|
|
|
installDebugApiHandlers(node, server)
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
|
|
|
|
let response = await client.get_waku_v2_debug_v1_info()
|
|
|
|
|
|
|
|
|
|
check:
|
2022-01-10 15:07:35 +00:00
|
|
|
|
response.listenAddresses == @[$node.switch.peerInfo.addrs[^1] & "/p2p/" & $node.switch.peerInfo.peerId]
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.stop()
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
asyncTest "Relay API: publish and subscribe/unsubscribe":
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.start()
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node.mountRelay()
|
2020-11-27 07:18:48 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8547)
|
2020-11-27 07:18:48 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
installRelayApiHandlers(node, server, newTable[string, seq[WakuMessage]]())
|
2020-11-27 07:18:48 +00:00
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-11-27 07:18:48 +00:00
|
|
|
|
check:
|
|
|
|
|
# At this stage the node is only subscribed to the default topic
|
|
|
|
|
PubSub(node.wakuRelay).topics.len == 1
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-11-27 07:18:48 +00:00
|
|
|
|
# Subscribe to new topics
|
|
|
|
|
let newTopics = @["1","2","3"]
|
|
|
|
|
var response = await client.post_waku_v2_relay_v1_subscriptions(newTopics)
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# Node is now subscribed to default + new topics
|
|
|
|
|
PubSub(node.wakuRelay).topics.len == 1 + newTopics.len
|
|
|
|
|
response == true
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-11-30 09:44:57 +00:00
|
|
|
|
# Publish a message on the default topic
|
2022-10-28 18:11:28 +00:00
|
|
|
|
response = await client.post_waku_v2_relay_v1_message(DefaultPubsubTopic, WakuRelayMessage(payload: @[byte 1], contentTopic: some(DefaultContentTopic), timestamp: some(getNanosecondTime(epochTime()))))
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# @TODO poll topic to verify message has been published
|
|
|
|
|
response == true
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-11-27 07:18:48 +00:00
|
|
|
|
# Unsubscribe from new topics
|
|
|
|
|
response = await client.delete_waku_v2_relay_v1_subscriptions(newTopics)
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# Node is now unsubscribed from new topics
|
|
|
|
|
PubSub(node.wakuRelay).topics.len == 1
|
|
|
|
|
response == true
|
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.stop()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
asyncTest "Relay API: get latest messages":
|
2020-12-01 09:57:54 +00:00
|
|
|
|
let
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, bindIp, Port(60300))
|
2020-12-01 09:57:54 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, bindIp, Port(60302))
|
2020-12-01 09:57:54 +00:00
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node3 = WakuNode.new(nodeKey3, bindIp, Port(60303), some(extIp), some(port))
|
2020-12-01 09:57:54 +00:00
|
|
|
|
pubSubTopic = "polling"
|
2022-10-28 18:11:28 +00:00
|
|
|
|
contentTopic = DefaultContentTopic
|
2022-01-10 14:07:01 +00:00
|
|
|
|
payload1 = @[byte 9]
|
|
|
|
|
message1 = WakuMessage(payload: payload1, contentTopic: contentTopic)
|
|
|
|
|
payload2 = @[byte 8]
|
|
|
|
|
message2 = WakuMessage(payload: payload2, contentTopic: contentTopic)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node3.mountRelay(@[pubSubTopic])
|
2020-12-01 09:57:54 +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-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8548)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-01 09:57:54 +00:00
|
|
|
|
# Let's connect to node 3 via the API
|
2020-12-23 09:33:28 +00:00
|
|
|
|
installRelayApiHandlers(node3, server, newTable[string, seq[WakuMessage]]())
|
2020-12-01 09:57:54 +00:00
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
2021-03-11 07:48:59 +00:00
|
|
|
|
# First see if we can retrieve messages published on the default topic (node is already subscribed)
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node2.publish(DefaultPubsubTopic, message1)
|
2021-03-11 07:48:59 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2021-03-11 07:48:59 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
var messages = await client.get_waku_v2_relay_v1_messages(DefaultPubsubTopic)
|
2021-03-11 07:48:59 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
messages.len == 1
|
|
|
|
|
messages[0].contentTopic == contentTopic
|
2022-01-10 14:07:01 +00:00
|
|
|
|
messages[0].payload == payload1
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:48:59 +00:00
|
|
|
|
# Ensure that read messages are cleared from cache
|
2022-11-23 09:08:00 +00:00
|
|
|
|
messages = await client.get_waku_v2_relay_v1_messages(pubSubTopic)
|
2021-03-11 07:48:59 +00:00
|
|
|
|
check:
|
|
|
|
|
messages.len == 0
|
|
|
|
|
|
2020-12-01 09:57:54 +00:00
|
|
|
|
# Now try to subscribe using API
|
|
|
|
|
|
|
|
|
|
var response = await client.post_waku_v2_relay_v1_subscriptions(@[pubSubTopic])
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# Node is now subscribed to pubSubTopic
|
|
|
|
|
response == true
|
|
|
|
|
|
|
|
|
|
# Now publish a message on node1 and see if we receive it on node3
|
2022-01-10 14:07:01 +00:00
|
|
|
|
await node1.publish(pubSubTopic, message2)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:48:59 +00:00
|
|
|
|
messages = await client.get_waku_v2_relay_v1_messages(pubSubTopic)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
messages.len == 1
|
|
|
|
|
messages[0].contentTopic == contentTopic
|
2022-01-10 14:07:01 +00:00
|
|
|
|
messages[0].payload == payload2
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-01 09:57:54 +00:00
|
|
|
|
# Ensure that read messages are cleared from cache
|
2022-11-23 09:08:00 +00:00
|
|
|
|
messages = await client.get_waku_v2_relay_v1_messages(pubSubTopic)
|
2020-12-01 09:57:54 +00:00
|
|
|
|
check:
|
|
|
|
|
messages.len == 0
|
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-01 09:57:54 +00:00
|
|
|
|
await node1.stop()
|
|
|
|
|
await node2.stop()
|
|
|
|
|
await node3.stop()
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
asyncTest "Store API: retrieve historical messages":
|
|
|
|
|
await node.start()
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node.mountRelay()
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8549)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
2020-11-27 07:18:48 +00:00
|
|
|
|
installStoreApiHandlers(node, server)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
# WakuStore setup
|
|
|
|
|
let
|
2022-10-18 14:05:53 +00:00
|
|
|
|
key = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
|
peer = PeerInfo.new(key)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
let driver: ArchiveDriver = QueueDriver.new()
|
|
|
|
|
node.mountArchive(some(driver), none(MessageValidator), none(RetentionPolicy))
|
|
|
|
|
await node.mountStore()
|
|
|
|
|
node.mountStoreClient()
|
|
|
|
|
|
2020-11-24 03:44:37 +00:00
|
|
|
|
var listenSwitch = newStandardSwitch(some(key))
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await listenSwitch.start()
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node.setStorePeer(listenSwitch.peerInfo.toRemotePeerInfo())
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2021-03-17 08:52:27 +00:00
|
|
|
|
listenSwitch.mount(node.wakuRelay)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
listenSwitch.mount(node.wakuStore)
|
|
|
|
|
|
|
|
|
|
# Now prime it with some history before tests
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let msgList = @[
|
|
|
|
|
fakeWakuMessage(@[byte 0], contentTopic=ContentTopic("2"), ts=0),
|
|
|
|
|
fakeWakuMessage(@[byte 1], ts=1),
|
|
|
|
|
fakeWakuMessage(@[byte 2], ts=2),
|
|
|
|
|
fakeWakuMessage(@[byte 3], ts=3),
|
|
|
|
|
fakeWakuMessage(@[byte 4], ts=4),
|
|
|
|
|
fakeWakuMessage(@[byte 5], ts=5),
|
|
|
|
|
fakeWakuMessage(@[byte 6], ts=6),
|
|
|
|
|
fakeWakuMessage(@[byte 7], ts=7),
|
|
|
|
|
fakeWakuMessage(@[byte 8], ts=8),
|
|
|
|
|
fakeWakuMessage(@[byte 9], contentTopic=ContentTopic("2"), ts=9)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for msg in msgList:
|
|
|
|
|
require driver.put(DefaultPubsubTopic, msg).isOk()
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
|
2022-11-09 17:50:18 +00:00
|
|
|
|
let response = await client.get_waku_v2_store_v1_messages(some(DefaultPubsubTopic), some(@[HistoryContentFilterRPC(contentTopic: DefaultContentTopic)]), some(Timestamp(0)), some(Timestamp(9)), some(StorePagingOptions()))
|
2020-11-24 03:44:37 +00:00
|
|
|
|
check:
|
|
|
|
|
response.messages.len() == 8
|
2022-10-03 15:36:17 +00:00
|
|
|
|
response.pagingOptions.isNone()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.stop()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
asyncTest "Filter API: subscribe/unsubscribe":
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
|
node1 = WakuNode.new(nodeKey1, bindIp, Port(60390))
|
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
|
node2 = WakuNode.new(nodeKey2, bindIp, Port(60392))
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await allFutures(node1.start(), node2.start())
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await node1.mountFilter()
|
|
|
|
|
await node2.mountFilterClient()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
node2.setFilterPeer(node1.peerInfo.toRemotePeerInfo())
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8550)
|
2020-11-30 09:44:57 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
installFilterApiHandlers(node2, server, newTable[ContentTopic, seq[WakuMessage]]())
|
2020-11-30 09:44:57 +00:00
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# Light node has not yet subscribed to any filters
|
2022-11-02 10:59:58 +00:00
|
|
|
|
node2.wakuFilterClient.getSubscriptionsCount() == 0
|
|
|
|
|
|
|
|
|
|
let contentFilters = @[
|
|
|
|
|
ContentFilter(contentTopic: DefaultContentTopic),
|
|
|
|
|
ContentFilter(contentTopic: ContentTopic("2")),
|
|
|
|
|
ContentFilter(contentTopic: ContentTopic("3")),
|
|
|
|
|
ContentFilter(contentTopic: ContentTopic("4")),
|
|
|
|
|
]
|
|
|
|
|
var response = await client.post_waku_v2_filter_v1_subscription(contentFilters=contentFilters, topic=some(DefaultPubsubTopic))
|
2020-11-30 09:44:57 +00:00
|
|
|
|
check:
|
|
|
|
|
response == true
|
2022-11-02 10:59:58 +00:00
|
|
|
|
# Light node has successfully subscribed to 4 content topics
|
|
|
|
|
node2.wakuFilterClient.getSubscriptionsCount() == 4
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
response = await client.delete_waku_v2_filter_v1_subscription(contentFilters=contentFilters, topic=some(DefaultPubsubTopic))
|
2020-11-30 09:44:57 +00:00
|
|
|
|
check:
|
2022-11-02 10:59:58 +00:00
|
|
|
|
response == true
|
2020-11-30 09:44:57 +00:00
|
|
|
|
# Light node has successfully unsubscribed from all filters
|
2022-11-02 10:59:58 +00:00
|
|
|
|
node2.wakuFilterClient.getSubscriptionsCount() == 0
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
## Cleanup
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await allFutures(node1.stop(), node2.stop())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:05:39 +00:00
|
|
|
|
asyncTest "Admin API: connect to ad-hoc peers":
|
|
|
|
|
# Create a couple of nodes
|
|
|
|
|
let
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60600))
|
2021-03-11 07:05:39 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60602))
|
2022-01-10 15:07:35 +00:00
|
|
|
|
peerInfo2 = node2.switch.peerInfo
|
2021-03-11 07:05:39 +00:00
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node3 = WakuNode.new(nodeKey3, ValidIpAddress.init("0.0.0.0"), Port(60604))
|
2022-01-10 15:07:35 +00:00
|
|
|
|
peerInfo3 = node3.switch.peerInfo
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:05:39 +00:00
|
|
|
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node1.mountRelay()
|
|
|
|
|
await node2.mountRelay()
|
|
|
|
|
await node3.mountRelay()
|
2021-03-11 07:05:39 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8551)
|
2021-03-11 07:05:39 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
|
|
|
|
installAdminApiHandlers(node1, server)
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2021-03-11 07:05:39 +00:00
|
|
|
|
|
|
|
|
|
# Connect to nodes 2 and 3 using the Admin API
|
|
|
|
|
let postRes = await client.post_waku_v2_admin_v1_peers(@[constructMultiaddrStr(peerInfo2),
|
|
|
|
|
constructMultiaddrStr(peerInfo3)])
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
postRes
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:05:39 +00:00
|
|
|
|
# Verify that newly connected peers are being managed
|
|
|
|
|
let getRes = await client.get_waku_v2_admin_v1_peers()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
getRes.len == 2
|
|
|
|
|
# Check peer 2
|
|
|
|
|
getRes.anyIt(it.protocol == WakuRelayCodec and
|
|
|
|
|
it.multiaddr == constructMultiaddrStr(peerInfo2))
|
|
|
|
|
# Check peer 3
|
|
|
|
|
getRes.anyIt(it.protocol == WakuRelayCodec and
|
|
|
|
|
it.multiaddr == constructMultiaddrStr(peerInfo3))
|
|
|
|
|
|
2023-01-09 13:15:03 +00:00
|
|
|
|
# Verify that raises an exception if we can't connect to the peer
|
|
|
|
|
let nonExistentPeer = "/ip4/0.0.0.0/tcp/10000/p2p/16Uiu2HAm6HZZr7aToTvEBPpiys4UxajCTU97zj5v7RNR2gbniy1D"
|
|
|
|
|
expect(ValueError):
|
|
|
|
|
discard await client.post_waku_v2_admin_v1_peers(@[nonExistentPeer])
|
|
|
|
|
|
|
|
|
|
let malformedPeer = "/malformed/peer"
|
|
|
|
|
expect(ValueError):
|
|
|
|
|
discard await client.post_waku_v2_admin_v1_peers(@[malformedPeer])
|
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-03-11 07:05:39 +00:00
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
|
asyncTest "Admin API: get managed peer information":
|
2023-01-26 11:05:25 +00:00
|
|
|
|
# Create 3 nodes and start them with relay
|
|
|
|
|
let nodes = toSeq(0..<3).mapIt(WakuNode.new(generateKey(), ValidIpAddress.init("0.0.0.0"), Port(60220+it*2)))
|
|
|
|
|
await allFutures(nodes.mapIt(it.start()))
|
|
|
|
|
await allFutures(nodes.mapIt(it.mountRelay()))
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
# Dial nodes 2 and 3 from node1
|
2023-01-26 11:05:25 +00:00
|
|
|
|
await nodes[0].connectToNodes(@[constructMultiaddrStr(nodes[1].peerInfo)])
|
|
|
|
|
await nodes[0].connectToNodes(@[constructMultiaddrStr(nodes[2].peerInfo)])
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8552)
|
2021-02-05 10:49:11 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
2023-01-26 11:05:25 +00:00
|
|
|
|
installAdminApiHandlers(nodes[0], server)
|
2021-02-05 10:49:11 +00:00
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
let response = await client.get_waku_v2_admin_v1_peers()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
response.len == 2
|
|
|
|
|
# Check peer 2
|
|
|
|
|
response.anyIt(it.protocol == WakuRelayCodec and
|
2023-01-26 11:05:25 +00:00
|
|
|
|
it.multiaddr == constructMultiaddrStr(nodes[1].peerInfo))
|
2021-02-05 10:49:11 +00:00
|
|
|
|
# Check peer 3
|
|
|
|
|
response.anyIt(it.protocol == WakuRelayCodec and
|
2023-01-26 11:05:25 +00:00
|
|
|
|
it.multiaddr == constructMultiaddrStr(nodes[2].peerInfo))
|
|
|
|
|
|
|
|
|
|
# Artificially remove the address from the book
|
|
|
|
|
nodes[0].peerManager.peerStore[AddressBook][nodes[1].peerInfo.peerId] = @[]
|
|
|
|
|
nodes[0].peerManager.peerStore[AddressBook][nodes[2].peerInfo.peerId] = @[]
|
|
|
|
|
|
|
|
|
|
# Verify that the returned addresses are empty
|
|
|
|
|
let responseEmptyAdd = await client.get_waku_v2_admin_v1_peers()
|
|
|
|
|
check:
|
|
|
|
|
responseEmptyAdd[0].multiaddr == ""
|
|
|
|
|
responseEmptyAdd[1].multiaddr == ""
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-01-26 11:05:25 +00:00
|
|
|
|
await allFutures(nodes.mapIt(it.stop()))
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
|
asyncTest "Admin API: get unmanaged peer information":
|
2021-02-09 08:31:38 +00:00
|
|
|
|
let
|
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node = WakuNode.new(nodeKey, ValidIpAddress.init("0.0.0.0"), Port(60523))
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.start()
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort = Port(8553)
|
2020-12-15 15:48:00 +00:00
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
|
|
|
|
installAdminApiHandlers(node, server)
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node.mountFilter()
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await node.mountFilterClient()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node.mountSwap()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let driver: ArchiveDriver = QueueDriver.new()
|
|
|
|
|
node.mountArchive(some(driver), none(MessageValidator), none(RetentionPolicy))
|
|
|
|
|
await node.mountStore()
|
|
|
|
|
node.mountStoreClient()
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
# Create and set some peers
|
|
|
|
|
let
|
|
|
|
|
locationAddr = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet()
|
|
|
|
|
|
2022-10-18 14:05:53 +00:00
|
|
|
|
filterKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
|
filterPeer = PeerInfo.new(filterKey, @[locationAddr])
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
2022-10-18 14:05:53 +00:00
|
|
|
|
swapKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
|
swapPeer = PeerInfo.new(swapKey, @[locationAddr])
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
2022-10-18 14:05:53 +00:00
|
|
|
|
storeKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
2021-11-04 14:46:38 +00:00
|
|
|
|
storePeer = PeerInfo.new(storeKey, @[locationAddr])
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
|
node.wakuSwap.setPeer(swapPeer.toRemotePeerInfo())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
node.setStorePeer(storePeer.toRemotePeerInfo())
|
2022-11-02 10:59:58 +00:00
|
|
|
|
node.setFilterPeer(filterPeer.toRemotePeerInfo())
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
let response = await client.get_waku_v2_admin_v1_peers()
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
## Then
|
2020-12-15 15:48:00 +00:00
|
|
|
|
check:
|
|
|
|
|
response.len == 3
|
|
|
|
|
# Check filter peer
|
|
|
|
|
(response.filterIt(it.protocol == WakuFilterCodec)[0]).multiaddr == constructMultiaddrStr(filterPeer)
|
|
|
|
|
# Check swap peer
|
|
|
|
|
(response.filterIt(it.protocol == WakuSwapCodec)[0]).multiaddr == constructMultiaddrStr(swapPeer)
|
|
|
|
|
# Check store peer
|
|
|
|
|
(response.filterIt(it.protocol == WakuStoreCodec)[0]).multiaddr == constructMultiaddrStr(storePeer)
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
## Cleanup
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server.stop()
|
|
|
|
|
await server.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
await node.stop()
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
asyncTest "Private API: generate asymmetric keys and encrypt/decrypt communication":
|
|
|
|
|
let
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, bindIp, Port(62001))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, bindIp, Port(62002))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node3 = WakuNode.new(nodeKey3, bindIp, Port(62003), some(extIp), some(port))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
pubSubTopic = "polling"
|
2022-10-28 18:11:28 +00:00
|
|
|
|
contentTopic = DefaultContentTopic
|
2020-12-23 09:33:28 +00:00
|
|
|
|
payload = @[byte 9]
|
2022-02-17 15:00:15 +00:00
|
|
|
|
message = WakuRelayMessage(payload: payload, contentTopic: some(contentTopic), timestamp: some(getNanosecondTime(epochTime())))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
topicCache = newTable[string, seq[WakuMessage]]()
|
|
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node3.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +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-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
# Setup two servers so we can see both sides of encrypted communication
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort1 = Port(8554)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
ta1 = initTAddress(bindIp, rpcPort1)
|
|
|
|
|
server1 = newRpcHttpServer([ta1])
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort3 = Port(8555)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
ta3 = initTAddress(bindIp, rpcPort3)
|
|
|
|
|
server3 = newRpcHttpServer([ta3])
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
# Let's connect to nodes 1 and 3 via the API
|
2022-09-07 15:31:27 +00:00
|
|
|
|
installPrivateApiHandlers(node1, server1, newTable[string, seq[WakuMessage]]())
|
|
|
|
|
installPrivateApiHandlers(node3, server3, topicCache)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
installRelayApiHandlers(node3, server3, topicCache)
|
|
|
|
|
server1.start()
|
|
|
|
|
server3.start()
|
|
|
|
|
|
|
|
|
|
let client1 = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client1.connect("127.0.0.1", rpcPort1, false)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
let client3 = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client3.connect("127.0.0.1", rpcPort3, false)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
# Let's get a keypair for node3
|
|
|
|
|
|
|
|
|
|
let keypair = await client3.get_waku_v2_private_v1_asymmetric_keypair()
|
|
|
|
|
|
|
|
|
|
# Now try to subscribe on node3 using API
|
|
|
|
|
|
|
|
|
|
let sub = await client3.post_waku_v2_relay_v1_subscriptions(@[pubSubTopic])
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# node3 is now subscribed to pubSubTopic
|
|
|
|
|
sub
|
|
|
|
|
|
|
|
|
|
# Now publish and encrypt a message on node1 using node3's public key
|
|
|
|
|
let posted = await client1.post_waku_v2_private_v1_asymmetric_message(pubSubTopic, message, publicKey = (%keypair.pubkey).getStr())
|
|
|
|
|
check:
|
|
|
|
|
posted
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
# Let's see if we can receive, and decrypt, this message on node3
|
2020-12-23 09:33:28 +00:00
|
|
|
|
var messages = await client3.get_waku_v2_private_v1_asymmetric_messages(pubSubTopic, privateKey = (%keypair.seckey).getStr())
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
messages.len == 1
|
|
|
|
|
messages[0].contentTopic.get == contentTopic
|
|
|
|
|
messages[0].payload == payload
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
# Ensure that read messages are cleared from cache
|
2022-11-23 09:08:00 +00:00
|
|
|
|
messages = await client3.get_waku_v2_private_v1_asymmetric_messages(pubSubTopic, privateKey = (%keypair.seckey).getStr())
|
2020-12-23 09:33:28 +00:00
|
|
|
|
check:
|
|
|
|
|
messages.len == 0
|
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server1.stop()
|
|
|
|
|
await server1.closeWait()
|
|
|
|
|
await server3.stop()
|
|
|
|
|
await server3.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
await node1.stop()
|
|
|
|
|
await node2.stop()
|
|
|
|
|
await node3.stop()
|
|
|
|
|
|
|
|
|
|
asyncTest "Private API: generate symmetric keys and encrypt/decrypt communication":
|
|
|
|
|
let
|
|
|
|
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node1 = WakuNode.new(nodeKey1, bindIp, Port(62100))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node2 = WakuNode.new(nodeKey2, bindIp, Port(62102))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-03 13:47:56 +00:00
|
|
|
|
node3 = WakuNode.new(nodeKey3, bindIp, Port(62103), some(extIp), some(port))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
pubSubTopic = "polling"
|
2022-10-28 18:11:28 +00:00
|
|
|
|
contentTopic = DefaultContentTopic
|
2020-12-23 09:33:28 +00:00
|
|
|
|
payload = @[byte 9]
|
2022-02-17 15:00:15 +00:00
|
|
|
|
message = WakuRelayMessage(payload: payload, contentTopic: some(contentTopic), timestamp: some(getNanosecondTime(epochTime())))
|
2020-12-23 09:33:28 +00:00
|
|
|
|
topicCache = newTable[string, seq[WakuMessage]]()
|
|
|
|
|
|
|
|
|
|
await node1.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node1.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
await node2.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node2.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
await node3.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
|
await node3.mountRelay(@[pubSubTopic])
|
2020-12-23 09:33:28 +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-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
# Setup two servers so we can see both sides of encrypted communication
|
|
|
|
|
let
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort1 = Port(8556)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
ta1 = initTAddress(bindIp, rpcPort1)
|
|
|
|
|
server1 = newRpcHttpServer([ta1])
|
2022-11-02 10:59:58 +00:00
|
|
|
|
rpcPort3 = Port(8557)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
ta3 = initTAddress(bindIp, rpcPort3)
|
|
|
|
|
server3 = newRpcHttpServer([ta3])
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
# Let's connect to nodes 1 and 3 via the API
|
2022-09-07 15:31:27 +00:00
|
|
|
|
installPrivateApiHandlers(node1, server1, newTable[string, seq[WakuMessage]]())
|
|
|
|
|
installPrivateApiHandlers(node3, server3, topicCache)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
installRelayApiHandlers(node3, server3, topicCache)
|
|
|
|
|
server1.start()
|
|
|
|
|
server3.start()
|
|
|
|
|
|
|
|
|
|
let client1 = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client1.connect("127.0.0.1", rpcPort1, false)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
let client3 = newRpcHttpClient()
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await client3.connect("127.0.0.1", rpcPort3, false)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
# Let's get a symkey for node3
|
|
|
|
|
|
|
|
|
|
let symkey = await client3.get_waku_v2_private_v1_symmetric_key()
|
|
|
|
|
|
|
|
|
|
# Now try to subscribe on node3 using API
|
|
|
|
|
|
|
|
|
|
let sub = await client3.post_waku_v2_relay_v1_subscriptions(@[pubSubTopic])
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
# node3 is now subscribed to pubSubTopic
|
|
|
|
|
sub
|
|
|
|
|
|
|
|
|
|
# Now publish and encrypt a message on node1 using node3's symkey
|
|
|
|
|
let posted = await client1.post_waku_v2_private_v1_symmetric_message(pubSubTopic, message, symkey = (%symkey).getStr())
|
|
|
|
|
check:
|
|
|
|
|
posted
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
await sleepAsync(100.millis)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
# Let's see if we can receive, and decrypt, this message on node3
|
2020-12-23 09:33:28 +00:00
|
|
|
|
var messages = await client3.get_waku_v2_private_v1_symmetric_messages(pubSubTopic, symkey = (%symkey).getStr())
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
messages.len == 1
|
|
|
|
|
messages[0].contentTopic.get == contentTopic
|
|
|
|
|
messages[0].payload == payload
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
# Ensure that read messages are cleared from cache
|
|
|
|
|
messages = await client3.get_waku_v2_private_v1_symmetric_messages(pubSubTopic, symkey = (%symkey).getStr())
|
|
|
|
|
check:
|
|
|
|
|
messages.len == 0
|
|
|
|
|
|
2022-01-14 09:25:01 +00:00
|
|
|
|
await server1.stop()
|
|
|
|
|
await server1.closeWait()
|
|
|
|
|
await server3.stop()
|
|
|
|
|
await server3.closeWait()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
|
await node1.stop()
|
|
|
|
|
await node2.stop()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
await node3.stop()
|