2022-06-10 11:30:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-02-01 17:16:10 +00:00
|
|
|
std/[sequtils, strformat, tempfiles],
|
2022-06-10 11:30:51 +00:00
|
|
|
stew/byteutils,
|
|
|
|
stew/shims/net,
|
|
|
|
testutils/unittests,
|
2024-03-15 23:08:47 +00:00
|
|
|
presto,
|
|
|
|
presto/client as presto_client,
|
2023-02-10 14:17:50 +00:00
|
|
|
libp2p/crypto/crypto
|
2022-06-10 11:30:51 +00:00
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/[
|
|
|
|
common/base64,
|
|
|
|
waku_core,
|
|
|
|
waku_node,
|
|
|
|
waku_api/message_cache,
|
|
|
|
waku_api/rest/server,
|
|
|
|
waku_api/rest/client,
|
|
|
|
waku_api/rest/responses,
|
|
|
|
waku_api/rest/relay/types,
|
|
|
|
waku_api/rest/relay/handlers as relay_api,
|
|
|
|
waku_api/rest/relay/client as relay_api_client,
|
|
|
|
waku_relay,
|
|
|
|
waku_rln_relay,
|
|
|
|
],
|
2023-04-05 14:01:51 +00:00
|
|
|
../testlib/wakucore,
|
2024-02-01 17:16:10 +00:00
|
|
|
../testlib/wakunode,
|
|
|
|
../resources/payloads
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-10 14:17:50 +00:00
|
|
|
proc testWakuNode(): WakuNode =
|
|
|
|
let
|
2023-02-13 10:43:49 +00:00
|
|
|
privkey = generateSecp256k1Key()
|
2023-12-14 06:16:39 +00:00
|
|
|
bindIp = parseIpAddress("0.0.0.0")
|
|
|
|
extIp = parseIpAddress("127.0.0.1")
|
2023-02-13 14:22:24 +00:00
|
|
|
port = Port(0)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-04-05 14:01:51 +00:00
|
|
|
newTestWakuNode(privkey, bindIp, port, some(extIp), some(port))
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
suite "Waku v2 Rest API - Relay":
|
2023-09-26 11:33:52 +00:00
|
|
|
asyncTest "Subscribe a node to an array of pubsub topics - POST /relay/v1/subscriptions":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
2022-06-10 11:30:51 +00:00
|
|
|
restServer.start()
|
|
|
|
|
2024-09-10 21:07:12 +00:00
|
|
|
let
|
|
|
|
shard0 = RelayShard(clusterId: DefaultClusterId, shardId: 0)
|
|
|
|
shard1 = RelayShard(clusterId: DefaultClusterId, shardId: 1)
|
|
|
|
shard2 = RelayShard(clusterId: DefaultClusterId, shardId: 2)
|
|
|
|
|
|
|
|
let shards = @[$shard0, $shard1, $shard2]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2024-09-10 21:07:12 +00:00
|
|
|
let response = await client.relayPostSubscriptionsV1(shards)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
2024-09-10 21:07:12 +00:00
|
|
|
cache.isPubsubSubscribed($shard0)
|
|
|
|
cache.isPubsubSubscribed($shard1)
|
|
|
|
cache.isPubsubSubscribed($shard2)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
check:
|
2024-09-10 21:07:12 +00:00
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == shards.len
|
2023-02-10 14:17:50 +00:00
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
asyncTest "Unsubscribe a node from an array of pubsub topics - DELETE /relay/v1/subscriptions":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2024-09-10 21:07:12 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
shard0 = RelayShard(clusterId: DefaultClusterId, shardId: 0)
|
|
|
|
shard1 = RelayShard(clusterId: DefaultClusterId, shardId: 1)
|
|
|
|
shard2 = RelayShard(clusterId: DefaultClusterId, shardId: 2)
|
|
|
|
shard3 = RelayShard(clusterId: DefaultClusterId, shardId: 3)
|
|
|
|
shard4 = RelayShard(clusterId: DefaultClusterId, shardId: 4)
|
|
|
|
|
|
|
|
await node.mountRelay(@[shard0, shard1, shard2, shard3])
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2024-09-10 21:07:12 +00:00
|
|
|
cache.pubsubSubscribe($shard0)
|
|
|
|
cache.pubsubSubscribe($shard1)
|
|
|
|
cache.pubsubSubscribe($shard2)
|
|
|
|
cache.pubsubSubscribe($shard3)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
2022-06-10 11:30:51 +00:00
|
|
|
restServer.start()
|
|
|
|
|
2024-09-10 21:07:12 +00:00
|
|
|
let shards = @[$shard0, $shard1, $shard2, $shard4]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2024-09-10 21:07:12 +00:00
|
|
|
let response = await client.relayDeleteSubscriptionsV1(shards)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
2024-09-10 21:07:12 +00:00
|
|
|
not cache.isPubsubSubscribed($shard0)
|
|
|
|
not node.wakuRelay.isSubscribed($shard0)
|
|
|
|
not cache.isPubsubSubscribed($shard1)
|
|
|
|
not node.wakuRelay.isSubscribed($shard1)
|
|
|
|
not cache.isPubsubSubscribed($shard2)
|
|
|
|
not node.wakuRelay.isSubscribed($shard2)
|
|
|
|
cache.isPubsubSubscribed($shard3)
|
|
|
|
node.wakuRelay.isSubscribed($shard3)
|
|
|
|
not cache.isPubsubSubscribed($shard4)
|
|
|
|
not node.wakuRelay.isSubscribed($shard4)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
asyncTest "Get the latest messages for a pubsub topic - GET /relay/v1/messages/{topic}":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2024-07-09 15:36:12 +00:00
|
|
|
let pubSubTopic = "/waku/2/rs/0/0"
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
var messages =
|
|
|
|
@[
|
|
|
|
fakeWakuMessage(
|
|
|
|
contentTopic = "content-topic-x",
|
|
|
|
payload = toBytes("TEST-1"),
|
|
|
|
meta = toBytes("test-meta"),
|
|
|
|
ephemeral = true,
|
|
|
|
)
|
|
|
|
]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
# Prevent duplicate messages
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< 2:
|
|
|
|
var msg = fakeWakuMessage(
|
|
|
|
contentTopic = "content-topic-x",
|
|
|
|
payload = toBytes("TEST-1"),
|
|
|
|
meta = toBytes("test-meta"),
|
|
|
|
ephemeral = true,
|
|
|
|
)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
while msg == messages[i]:
|
2024-03-15 23:08:47 +00:00
|
|
|
msg = fakeWakuMessage(
|
|
|
|
contentTopic = "content-topic-x",
|
|
|
|
payload = toBytes("TEST-1"),
|
|
|
|
meta = toBytes("test-meta"),
|
|
|
|
ephemeral = true,
|
|
|
|
)
|
2024-02-14 15:29:59 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
messages.add(msg)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
|
|
|
|
|
|
|
cache.pubsubSubscribe(pubSubTopic)
|
2022-06-10 11:30:51 +00:00
|
|
|
for msg in messages:
|
2023-09-26 11:33:52 +00:00
|
|
|
cache.addMessage(pubSubTopic, msg)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
2022-06-10 11:30:51 +00:00
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
let response = await client.relayGetMessagesV1(pubSubTopic)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_JSON
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data.len == 3
|
2024-03-15 23:08:47 +00:00
|
|
|
response.data.all do(msg: RelayWakuMessage) -> bool:
|
2023-02-23 08:31:06 +00:00
|
|
|
msg.payload == base64.encode("TEST-1") and
|
2024-03-15 23:08:47 +00:00
|
|
|
msg.contentTopic.get() == "content-topic-x" and msg.version.get() == 2 and
|
|
|
|
msg.timestamp.get() != Timestamp(0) and
|
|
|
|
msg.meta.get() == base64.encode("test-meta") and msg.ephemeral.get() == true
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
check:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.isPubsubSubscribed(pubSubTopic)
|
2023-09-26 11:33:52 +00:00
|
|
|
cache.getMessages(pubSubTopic).tryGet().len == 0
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
asyncTest "Post a message to a pubsub topic - POST /relay/v1/messages/{topic}":
|
2023-02-10 14:17:50 +00:00
|
|
|
## "Relay API: publish and subscribe/unsubscribe":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2024-06-20 09:35:21 +00:00
|
|
|
let wakuRlnConfig = WakuRlnConfig(
|
|
|
|
rlnRelayDynamic: false,
|
|
|
|
rlnRelayCredIndex: some(1.uint),
|
|
|
|
rlnRelayUserMessageLimit: 20,
|
|
|
|
rlnEpochSizeSec: 1,
|
|
|
|
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1"),
|
|
|
|
)
|
|
|
|
|
2024-03-12 10:50:30 +00:00
|
|
|
await node.mountRlnRelay(wakuRlnConfig)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
2022-06-10 11:30:51 +00:00
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2023-02-10 14:17:50 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
node.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))
|
2023-02-10 14:17:50 +00:00
|
|
|
require:
|
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let response = await client.relayPostMessagesV1(
|
|
|
|
DefaultPubsubTopic,
|
|
|
|
RelayWakuMessage(
|
|
|
|
payload: base64.encode("TEST-PAYLOAD"),
|
|
|
|
contentTopic: some(DefaultContentTopic),
|
|
|
|
timestamp: some(int64(2022)),
|
|
|
|
),
|
|
|
|
)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
# Autosharding API
|
|
|
|
|
|
|
|
asyncTest "Subscribe a node to an array of content topics - POST /relay/v1/auto/subscriptions":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
2024-03-13 09:58:13 +00:00
|
|
|
require node.mountSharding(1, 8).isOk
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let contentTopics =
|
|
|
|
@[
|
|
|
|
ContentTopic("/app-1/2/default-content/proto"),
|
|
|
|
ContentTopic("/app-2/2/default-content/proto"),
|
|
|
|
ContentTopic("/app-3/2/default-content/proto"),
|
|
|
|
]
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2023-09-26 19:53:46 +00:00
|
|
|
let response = await client.relayPostAutoSubscriptionsV1(contentTopics)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.isContentSubscribed(contentTopics[0])
|
|
|
|
cache.isContentSubscribed(contentTopics[1])
|
|
|
|
cache.isContentSubscribed(contentTopics[2])
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Node should be subscribed to all shards
|
2024-03-15 23:08:47 +00:00
|
|
|
node.wakuRelay.subscribedTopics ==
|
|
|
|
@["/waku/2/rs/1/7", "/waku/2/rs/1/2", "/waku/2/rs/1/5"]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
asyncTest "Unsubscribe a node from an array of content topics - DELETE /relay/v1/auto/subscriptions":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let contentTopics =
|
|
|
|
@[
|
|
|
|
ContentTopic("/waku/2/default-content1/proto"),
|
|
|
|
ContentTopic("/waku/2/default-content2/proto"),
|
|
|
|
ContentTopic("/waku/2/default-content3/proto"),
|
|
|
|
ContentTopic("/waku/2/default-contentX/proto"),
|
|
|
|
]
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
|
|
|
cache.contentSubscribe(contentTopics[0])
|
|
|
|
cache.contentSubscribe(contentTopics[1])
|
|
|
|
cache.contentSubscribe(contentTopics[2])
|
|
|
|
cache.contentSubscribe("/waku/2/default-contentY/proto")
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2023-09-26 19:53:46 +00:00
|
|
|
let response = await client.relayDeleteAutoSubscriptionsV1(contentTopics)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
2023-11-28 12:21:41 +00:00
|
|
|
not cache.isContentSubscribed(contentTopics[1])
|
|
|
|
not cache.isContentSubscribed(contentTopics[2])
|
|
|
|
not cache.isContentSubscribed(contentTopics[3])
|
|
|
|
cache.isContentSubscribed("/waku/2/default-contentY/proto")
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
asyncTest "Get the latest messages for a content topic - GET /relay/v1/auto/messages/{topic}":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
let contentTopic = DefaultContentTopic
|
2023-11-28 12:21:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
var messages =
|
|
|
|
@[
|
|
|
|
fakeWakuMessage(contentTopic = DefaultContentTopic, payload = toBytes("TEST-1"))
|
|
|
|
]
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
# Prevent duplicate messages
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< 2:
|
|
|
|
var msg =
|
|
|
|
fakeWakuMessage(contentTopic = DefaultContentTopic, payload = toBytes("TEST-1"))
|
2023-11-28 12:21:41 +00:00
|
|
|
|
|
|
|
while msg == messages[i]:
|
2024-03-15 23:08:47 +00:00
|
|
|
msg = fakeWakuMessage(
|
|
|
|
contentTopic = DefaultContentTopic, payload = toBytes("TEST-1")
|
|
|
|
)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
messages.add(msg)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.contentSubscribe(contentTopic)
|
2023-09-26 11:33:52 +00:00
|
|
|
for msg in messages:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.addMessage(DefaultPubsubTopic, msg)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
let response = await client.relayGetAutoMessagesV1(contentTopic)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
|
|
|
$response.contentType == $MIMETYPE_JSON
|
|
|
|
response.data.len == 3
|
2024-03-15 23:08:47 +00:00
|
|
|
response.data.all do(msg: RelayWakuMessage) -> bool:
|
2023-09-26 11:33:52 +00:00
|
|
|
msg.payload == base64.encode("TEST-1") and
|
2024-03-15 23:08:47 +00:00
|
|
|
msg.contentTopic.get() == DefaultContentTopic and msg.version.get() == 2 and
|
|
|
|
msg.timestamp.get() != Timestamp(0)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
check:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.isContentSubscribed(contentTopic)
|
2024-03-15 23:08:47 +00:00
|
|
|
cache.getAutoMessages(contentTopic).tryGet().len == 0
|
|
|
|
# The cache is cleared when getMessage is called
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
asyncTest "Post a message to a content topic - POST /relay/v1/auto/messages/{topic}":
|
|
|
|
## "Relay API: publish and subscribe/unsubscribe":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
2024-06-20 09:35:21 +00:00
|
|
|
let wakuRlnConfig = WakuRlnConfig(
|
|
|
|
rlnRelayDynamic: false,
|
|
|
|
rlnRelayCredIndex: some(1.uint),
|
|
|
|
rlnRelayUserMessageLimit: 20,
|
|
|
|
rlnEpochSizeSec: 1,
|
|
|
|
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1"),
|
|
|
|
)
|
|
|
|
|
2024-03-12 10:50:30 +00:00
|
|
|
await node.mountRlnRelay(wakuRlnConfig)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
2024-01-18 12:49:13 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init()
|
2023-09-26 11:33:52 +00:00
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
|
|
|
|
node.subscribe((kind: ContentSub, topic: DefaultContentTopic))
|
|
|
|
require:
|
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
|
|
|
|
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let response = await client.relayPostAutoMessagesV1(
|
|
|
|
RelayWakuMessage(
|
|
|
|
payload: base64.encode("TEST-PAYLOAD"),
|
|
|
|
contentTopic: some(DefaultContentTopic),
|
|
|
|
timestamp: some(int64(2022)),
|
|
|
|
)
|
|
|
|
)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
|
|
|
response.data == "OK"
|
|
|
|
|
2024-01-18 12:49:13 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
asyncTest "Post a message to an invalid content topic - POST /relay/v1/auto/messages/{topic}":
|
|
|
|
## "Relay API: publish and subscribe/unsubscribe":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
2024-06-20 09:35:21 +00:00
|
|
|
let wakuRlnConfig = WakuRlnConfig(
|
|
|
|
rlnRelayDynamic: false,
|
|
|
|
rlnRelayCredIndex: some(1.uint),
|
|
|
|
rlnRelayUserMessageLimit: 20,
|
|
|
|
rlnEpochSizeSec: 1,
|
|
|
|
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1"),
|
|
|
|
)
|
|
|
|
|
2024-03-12 10:50:30 +00:00
|
|
|
await node.mountRlnRelay(wakuRlnConfig)
|
2024-01-18 12:49:13 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
var restPort = Port(0)
|
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-01-18 12:49:13 +00:00
|
|
|
|
|
|
|
let cache = MessageCache.init()
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
|
2024-06-20 13:08:55 +00:00
|
|
|
let invalidContentTopic = "invalidContentTopic"
|
2024-01-18 12:49:13 +00:00
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let response = await client.relayPostAutoMessagesV1(
|
|
|
|
RelayWakuMessage(
|
|
|
|
payload: base64.encode("TEST-PAYLOAD"),
|
2024-06-20 13:08:55 +00:00
|
|
|
contentTopic: some(invalidContentTopic),
|
2024-03-15 23:08:47 +00:00
|
|
|
timestamp: some(int64(2022)),
|
|
|
|
)
|
|
|
|
)
|
2024-01-18 12:49:13 +00:00
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 400
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2024-03-15 23:08:47 +00:00
|
|
|
response.data ==
|
2024-06-20 13:08:55 +00:00
|
|
|
"Failed to publish. Autosharding error: invalid format: content-topic '" &
|
|
|
|
invalidContentTopic & "' must start with slash"
|
2024-01-18 12:49:13 +00:00
|
|
|
|
2024-02-01 17:16:10 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
asyncTest "Post a message larger than maximum size - POST /relay/v1/messages/{topic}":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
2024-06-20 09:35:21 +00:00
|
|
|
let wakuRlnConfig = WakuRlnConfig(
|
|
|
|
rlnRelayDynamic: false,
|
|
|
|
rlnRelayCredIndex: some(1.uint),
|
|
|
|
rlnRelayUserMessageLimit: 20,
|
|
|
|
rlnEpochSizeSec: 1,
|
|
|
|
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1"),
|
|
|
|
)
|
|
|
|
|
2024-03-12 10:50:30 +00:00
|
|
|
await node.mountRlnRelay(wakuRlnConfig)
|
2024-02-01 17:16:10 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
var restPort = Port(0)
|
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2024-02-01 17:16:10 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-02-01 17:16:10 +00:00
|
|
|
|
|
|
|
let cache = MessageCache.init()
|
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
|
|
|
|
node.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))
|
|
|
|
require:
|
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
|
|
|
|
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let response = await client.relayPostMessagesV1(
|
|
|
|
DefaultPubsubTopic,
|
|
|
|
RelayWakuMessage(
|
2024-04-20 03:40:52 +00:00
|
|
|
payload: base64.encode(getByteSequence(DefaultMaxWakuMessageSize)),
|
2024-03-15 23:08:47 +00:00
|
|
|
# Message will be bigger than the max size
|
|
|
|
contentTopic: some(DefaultContentTopic),
|
|
|
|
timestamp: some(int64(2022)),
|
|
|
|
),
|
|
|
|
)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2024-02-05 08:24:54 +00:00
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 400
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2024-03-15 23:08:47 +00:00
|
|
|
response.data ==
|
2024-04-20 03:40:52 +00:00
|
|
|
fmt"Failed to publish: Message size exceeded maximum of {DefaultMaxWakuMessageSize} bytes"
|
2024-02-05 08:24:54 +00:00
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
asyncTest "Post a message larger than maximum size - POST /relay/v1/auto/messages/{topic}":
|
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
|
|
|
await node.mountRelay()
|
2024-06-20 09:35:21 +00:00
|
|
|
let wakuRlnConfig = WakuRlnConfig(
|
|
|
|
rlnRelayDynamic: false,
|
|
|
|
rlnRelayCredIndex: some(1.uint),
|
|
|
|
rlnRelayUserMessageLimit: 20,
|
|
|
|
rlnEpochSizeSec: 1,
|
|
|
|
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1"),
|
|
|
|
)
|
|
|
|
|
2024-03-12 10:50:30 +00:00
|
|
|
await node.mountRlnRelay(wakuRlnConfig)
|
2024-02-05 08:24:54 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
var restPort = Port(0)
|
|
|
|
let restAddress = parseIpAddress("0.0.0.0")
|
2024-02-29 08:48:14 +00:00
|
|
|
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2024-02-05 08:24:54 +00:00
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2024-02-05 08:24:54 +00:00
|
|
|
|
|
|
|
let cache = MessageCache.init()
|
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, cache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
|
|
|
|
node.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))
|
|
|
|
require:
|
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
|
|
|
|
|
|
|
# When
|
2024-03-15 23:08:47 +00:00
|
|
|
let response = await client.relayPostAutoMessagesV1(
|
|
|
|
RelayWakuMessage(
|
2024-04-20 03:40:52 +00:00
|
|
|
payload: base64.encode(getByteSequence(DefaultMaxWakuMessageSize)),
|
2024-03-15 23:08:47 +00:00
|
|
|
# Message will be bigger than the max size
|
|
|
|
contentTopic: some(DefaultContentTopic),
|
|
|
|
timestamp: some(int64(2022)),
|
|
|
|
)
|
|
|
|
)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2024-02-01 17:16:10 +00:00
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 400
|
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2024-03-15 23:08:47 +00:00
|
|
|
response.data ==
|
2024-04-20 03:40:52 +00:00
|
|
|
fmt"Failed to publish: Message size exceeded maximum of {DefaultMaxWakuMessageSize} bytes"
|
2024-02-01 17:16:10 +00:00
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
2024-02-29 08:48:14 +00:00
|
|
|
await node.stop()
|