2023-10-05 12:00:09 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-10-28 08:17:46 +00:00
|
|
|
std/[sequtils, strformat, net],
|
2023-10-05 12:00:09 +00:00
|
|
|
stew/shims/net,
|
|
|
|
testutils/unittests,
|
2024-03-15 23:08:47 +00:00
|
|
|
presto,
|
|
|
|
presto/client as presto_client,
|
2023-10-05 12:00:09 +00:00
|
|
|
libp2p/crypto/crypto
|
|
|
|
|
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/[
|
|
|
|
waku_core,
|
|
|
|
waku_node,
|
|
|
|
waku_filter_v2/client,
|
|
|
|
node/peer_manager,
|
|
|
|
waku_api/rest/server,
|
|
|
|
waku_api/rest/client,
|
|
|
|
waku_api/rest/responses,
|
|
|
|
waku_api/rest/admin/types,
|
|
|
|
waku_api/rest/admin/handlers as admin_api,
|
|
|
|
waku_api/rest/admin/client as admin_api_client,
|
|
|
|
waku_relay,
|
2024-07-23 10:58:56 +00:00
|
|
|
waku_peer_exchange,
|
2024-07-05 22:03:38 +00:00
|
|
|
],
|
2023-10-05 12:00:09 +00:00
|
|
|
../testlib/wakucore,
|
|
|
|
../testlib/wakunode,
|
|
|
|
../testlib/testasync
|
|
|
|
|
|
|
|
suite "Waku v2 Rest API - Admin":
|
|
|
|
var node1 {.threadvar.}: WakuNode
|
|
|
|
var node2 {.threadvar.}: WakuNode
|
|
|
|
var node3 {.threadvar.}: WakuNode
|
2024-01-09 10:42:29 +00:00
|
|
|
var peerInfo1 {.threadvar.}: RemotePeerInfo
|
2023-10-05 12:00:09 +00:00
|
|
|
var peerInfo2 {.threadvar.}: RemotePeerInfo
|
|
|
|
var peerInfo3 {.threadvar.}: RemotePeerInfo
|
2024-02-29 08:48:14 +00:00
|
|
|
var restServer {.threadvar.}: WakuRestServerRef
|
2024-03-15 23:08:47 +00:00
|
|
|
var client {.threadvar.}: RestClientRef
|
2023-10-05 12:00:09 +00:00
|
|
|
|
|
|
|
asyncSetup:
|
2024-10-28 08:17:46 +00:00
|
|
|
node1 = newTestWakuNode(generateSecp256k1Key(), getPrimaryIPAddr(), Port(60600))
|
|
|
|
node2 = newTestWakuNode(generateSecp256k1Key(), getPrimaryIPAddr(), Port(60602))
|
|
|
|
node3 = newTestWakuNode(generateSecp256k1Key(), getPrimaryIPAddr(), Port(60604))
|
2023-10-05 12:00:09 +00:00
|
|
|
|
|
|
|
await allFutures(node1.start(), node2.start(), node3.start())
|
2024-07-23 10:58:56 +00:00
|
|
|
await allFutures(
|
|
|
|
node1.mountRelay(),
|
|
|
|
node2.mountRelay(),
|
|
|
|
node3.mountRelay(),
|
|
|
|
node3.mountPeerExchange(),
|
|
|
|
)
|
2023-10-05 12:00:09 +00:00
|
|
|
|
2024-06-26 15:59:12 +00:00
|
|
|
peerInfo1 = node1.switch.peerInfo
|
|
|
|
peerInfo2 = node2.switch.peerInfo
|
|
|
|
peerInfo3 = node3.switch.peerInfo
|
|
|
|
|
2024-06-12 13:07:33 +00:00
|
|
|
var restPort = Port(0)
|
2023-12-14 06:16:39 +00:00
|
|
|
let restAddress = parseIpAddress("127.0.0.1")
|
2024-02-29 08:48:14 +00:00
|
|
|
restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
|
2024-06-12 13:07:33 +00:00
|
|
|
restPort = restServer.httpServer.address.port # update with bound port for client use
|
2023-10-05 12:00:09 +00:00
|
|
|
|
|
|
|
installAdminApiHandlers(restServer.router, node1)
|
|
|
|
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
|
|
|
|
asyncTearDown:
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await allFutures(node1.stop(), node2.stop(), node3.stop())
|
|
|
|
|
|
|
|
asyncTest "Set and get remote peers":
|
|
|
|
# Connect to nodes 2 and 3 using the Admin API
|
2024-03-15 23:08:47 +00:00
|
|
|
let postRes = await client.postPeers(
|
|
|
|
@[constructMultiaddrStr(peerInfo2), constructMultiaddrStr(peerInfo3)]
|
|
|
|
)
|
2023-10-05 12:00:09 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
postRes.status == 200
|
|
|
|
|
|
|
|
# Verify that newly connected peers are being managed
|
|
|
|
let getRes = await client.getPeers()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 200
|
|
|
|
$getRes.contentType == $MIMETYPE_JSON
|
|
|
|
getRes.data.len() == 2
|
|
|
|
# Check peer 2
|
2024-03-15 23:08:47 +00:00
|
|
|
getRes.data.anyIt(
|
|
|
|
it.protocols.find(WakuRelayCodec) >= 0 and
|
|
|
|
it.multiaddr == constructMultiaddrStr(peerInfo2)
|
|
|
|
)
|
2023-10-05 12:00:09 +00:00
|
|
|
# Check peer 3
|
2024-03-15 23:08:47 +00:00
|
|
|
getRes.data.anyIt(
|
|
|
|
it.protocols.find(WakuRelayCodec) >= 0 and
|
|
|
|
it.multiaddr == constructMultiaddrStr(peerInfo3)
|
|
|
|
)
|
2023-10-05 12:00:09 +00:00
|
|
|
|
2024-07-23 10:58:56 +00:00
|
|
|
# Check peer 3
|
|
|
|
getRes.data.anyIt(
|
|
|
|
it.protocols.find(WakuPeerExchangeCodec) >= 0 and
|
|
|
|
it.multiaddr == constructMultiaddrStr(peerInfo3)
|
|
|
|
)
|
|
|
|
|
2023-10-05 12:00:09 +00:00
|
|
|
asyncTest "Set wrong peer":
|
2024-03-15 23:08:47 +00:00
|
|
|
let nonExistentPeer =
|
|
|
|
"/ip4/0.0.0.0/tcp/10000/p2p/16Uiu2HAm6HZZr7aToTvEBPpiys4UxajCTU97zj5v7RNR2gbniy1D"
|
2023-10-05 12:00:09 +00:00
|
|
|
let postRes = await client.postPeers(@[nonExistentPeer])
|
|
|
|
|
|
|
|
check:
|
|
|
|
postRes.status == 400
|
|
|
|
$postRes.contentType == $MIMETYPE_TEXT
|
|
|
|
postRes.data == "Failed to connect to peer at index: 0 - " & nonExistentPeer
|
|
|
|
|
|
|
|
# Verify that newly connected peers are being managed
|
|
|
|
let getRes = await client.getPeers()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 200
|
|
|
|
$getRes.contentType == $MIMETYPE_JSON
|
|
|
|
getRes.data.len() == 0
|
2024-01-09 10:42:29 +00:00
|
|
|
|
|
|
|
asyncTest "Get filter data":
|
2024-03-15 23:08:47 +00:00
|
|
|
await allFutures(
|
|
|
|
node1.mountFilter(), node2.mountFilterClient(), node3.mountFilterClient()
|
|
|
|
)
|
2024-01-09 10:42:29 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
contentFiltersNode2 = @[DefaultContentTopic, ContentTopic("2"), ContentTopic("3")]
|
|
|
|
contentFiltersNode3 = @[ContentTopic("3"), ContentTopic("4")]
|
|
|
|
pubsubTopicNode2 = DefaultPubsubTopic
|
|
|
|
pubsubTopicNode3 = PubsubTopic("/waku/2/custom-waku/proto")
|
|
|
|
|
|
|
|
let
|
|
|
|
subscribeResponse2 = await node2.wakuFilterClient.subscribe(
|
|
|
|
peerInfo1, pubsubTopicNode2, contentFiltersNode2
|
|
|
|
)
|
|
|
|
subscribeResponse3 = await node3.wakuFilterClient.subscribe(
|
|
|
|
peerInfo1, pubsubTopicNode3, contentFiltersNode3
|
|
|
|
)
|
|
|
|
|
|
|
|
assert subscribeResponse2.isOk(), $subscribeResponse2.error
|
|
|
|
assert subscribeResponse3.isOk(), $subscribeResponse3.error
|
|
|
|
|
|
|
|
let getRes = await client.getFilterSubscriptions()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 200
|
|
|
|
$getRes.contentType == $MIMETYPE_JSON
|
|
|
|
getRes.data.len() == 2
|
2024-01-17 08:42:22 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
peers = @[getRes.data[0].peerId, getRes.data[1].peerId]
|
2024-03-15 23:08:47 +00:00
|
|
|
numCriteria =
|
|
|
|
@[getRes.data[0].filterCriteria.len, getRes.data[1].filterCriteria.len]
|
2024-01-17 08:42:22 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
$peerInfo2 in peers
|
|
|
|
$peerInfo3 in peers
|
|
|
|
2 in numCriteria
|
|
|
|
3 in numCriteria
|
2024-01-09 10:42:29 +00:00
|
|
|
|
|
|
|
asyncTest "Get filter data - no filter subscribers":
|
|
|
|
await node1.mountFilter()
|
|
|
|
|
|
|
|
let getRes = await client.getFilterSubscriptions()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 200
|
|
|
|
$getRes.contentType == $MIMETYPE_JSON
|
|
|
|
getRes.data.len() == 0
|
|
|
|
|
|
|
|
asyncTest "Get filter data - filter not mounted":
|
|
|
|
let getRes = await client.getFilterSubscriptionsFilterNotMounted()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 400
|
2024-01-16 16:27:40 +00:00
|
|
|
getRes.data == "Error: Filter Protocol is not mounted to the node"
|
2024-06-26 15:59:12 +00:00
|
|
|
|
|
|
|
asyncTest "Get peer origin":
|
2024-07-23 10:58:56 +00:00
|
|
|
# Adding peers to the Peer Store
|
2024-06-26 15:59:12 +00:00
|
|
|
node1.peerManager.addPeer(peerInfo2, Discv5)
|
|
|
|
node1.peerManager.addPeer(peerInfo3, PeerExchange)
|
|
|
|
|
|
|
|
# Connecting to both peers
|
2024-10-29 16:37:07 +00:00
|
|
|
let conn2 = await node1.peerManager.connectPeer(peerInfo2)
|
|
|
|
let conn3 = await node1.peerManager.connectPeer(peerInfo3)
|
2024-06-26 15:59:12 +00:00
|
|
|
|
|
|
|
# Check successful connections
|
|
|
|
check:
|
|
|
|
conn2 == true
|
|
|
|
conn3 == true
|
|
|
|
|
|
|
|
# Query peers REST endpoint
|
|
|
|
let getRes = await client.getPeers()
|
|
|
|
|
|
|
|
check:
|
|
|
|
getRes.status == 200
|
|
|
|
$getRes.contentType == $MIMETYPE_JSON
|
|
|
|
getRes.data.len() == 2
|
|
|
|
# Check peer 2
|
|
|
|
getRes.data.anyIt(it.origin == Discv5)
|
|
|
|
# Check peer 3
|
|
|
|
getRes.data.anyIt(it.origin == PeerExchange)
|