Skip to content

Commit f3b6489

Browse files
authored
Added some basic debug and relay json-rpc calls (#305)
1 parent 1b14436 commit f3b6489

File tree

5 files changed

+164
-14
lines changed

5 files changed

+164
-14
lines changed

tests/v2/test_jsonrpc_waku.nim

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import
77
libp2p/protobuf/minprotobuf,
88
libp2p/stream/[bufferstream, connection],
99
libp2p/crypto/crypto,
10+
libp2p/protocols/pubsub/pubsub,
1011
libp2p/protocols/pubsub/rpc/message,
1112
../../waku/v2/waku_types,
1213
../../waku/v2/node/wakunode2,
13-
../../waku/v2/node/jsonrpc/[jsonrpc_types,store_api],
14+
../../waku/v2/node/jsonrpc/[jsonrpc_types,store_api,relay_api,debug_api],
1415
../../waku/v2/protocol/message_notifier,
1516
../../waku/v2/protocol/waku_store/waku_store,
1617
../test_helpers
@@ -19,21 +20,87 @@ template sourceDir*: string = currentSourcePath.rsplit(DirSep, 1)[0]
1920
const sigPath = sourceDir / ParDir / ParDir / "waku" / "v2" / "node" / "jsonrpc" / "jsonrpc_callsigs.nim"
2021
createRpcSigs(RpcHttpClient, sigPath)
2122

22-
suite "Waku v2 JSON-RPC API":
23+
procSuite "Waku v2 JSON-RPC API":
24+
const defaultTopic = "/waku/2/default-waku/proto"
25+
const testCodec = "/waku/2/default-waku/codec"
2326

24-
asyncTest "get_waku_v2_store_v1_messages":
25-
const defaultTopic = "/waku/2/default-waku/proto"
26-
const testCodec = "/waku/2/default-waku/codec"
27+
let
28+
rng = crypto.newRng()
29+
privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet()
30+
bindIp = ValidIpAddress.init("0.0.0.0")
31+
extIp = ValidIpAddress.init("127.0.0.1")
32+
port = Port(9000)
33+
node = WakuNode.init(privkey, bindIp, port, some(extIp), some(port))
2734

28-
# WakuNode setup
35+
asyncTest "debug_api":
36+
waitFor node.start()
37+
38+
waitFor node.mountRelay()
39+
40+
# RPC server setup
2941
let
30-
rng = crypto.newRng()
31-
privkey = crypto.PrivateKey.random(Secp256k1, rng[]).tryGet()
32-
bindIp = ValidIpAddress.init("0.0.0.0")
33-
extIp = ValidIpAddress.init("127.0.0.1")
34-
port = Port(9000)
35-
node = WakuNode.init(privkey, bindIp, port, some(extIp), some(port))
42+
rpcPort = Port(8545)
43+
ta = initTAddress(bindIp, rpcPort)
44+
server = newRpcHttpServer([ta])
45+
46+
installDebugApiHandlers(node, server)
47+
server.start()
48+
49+
let client = newRpcHttpClient()
50+
await client.connect("127.0.0.1", rpcPort)
51+
52+
let response = await client.get_waku_v2_debug_v1_info()
53+
54+
check:
55+
response.listenStr == $node.peerInfo.addrs[0] & "/p2p/" & $node.peerInfo.peerId
56+
57+
server.stop()
58+
server.close()
59+
waitfor node.stop()
60+
61+
asyncTest "relay_api":
62+
waitFor node.start()
63+
64+
waitFor node.mountRelay()
65+
66+
# RPC server setup
67+
let
68+
rpcPort = Port(8545)
69+
ta = initTAddress(bindIp, rpcPort)
70+
server = newRpcHttpServer([ta])
71+
72+
installRelayApiHandlers(node, server)
73+
server.start()
74+
75+
let client = newRpcHttpClient()
76+
await client.connect("127.0.0.1", rpcPort)
77+
78+
check:
79+
# At this stage the node is only subscribed to the default topic
80+
PubSub(node.wakuRelay).topics.len == 1
81+
82+
# Subscribe to new topics
83+
let newTopics = @["1","2","3"]
84+
var response = await client.post_waku_v2_relay_v1_subscriptions(newTopics)
85+
86+
check:
87+
# Node is now subscribed to default + new topics
88+
PubSub(node.wakuRelay).topics.len == 1 + newTopics.len
89+
response == true
90+
91+
# Unsubscribe from new topics
92+
response = await client.delete_waku_v2_relay_v1_subscriptions(newTopics)
93+
94+
check:
95+
# Node is now unsubscribed from new topics
96+
PubSub(node.wakuRelay).topics.len == 1
97+
response == true
98+
99+
server.stop()
100+
server.close()
101+
waitfor node.stop()
36102

103+
asyncTest "store_api":
37104
waitFor node.start()
38105

39106
waitFor node.mountRelay(@[defaultTopic])
@@ -44,7 +111,7 @@ suite "Waku v2 JSON-RPC API":
44111
ta = initTAddress(bindIp, rpcPort)
45112
server = newRpcHttpServer([ta])
46113

47-
setupWakuJSONRPC(node, server)
114+
installStoreApiHandlers(node, server)
48115
server.start()
49116

50117
# WakuStore setup

waku/v2/node/jsonrpc/debug_api.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import
2+
json_rpc/rpcserver,
3+
../../waku_types,
4+
../wakunode2
5+
6+
proc installDebugApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
7+
8+
## Debug API version 1 definitions
9+
10+
rpcsrv.rpc("get_waku_v2_debug_v1_info") do() -> WakuInfo:
11+
## Returns information about WakuNode
12+
debug "get_waku_v2_debug_v1_info"
13+
14+
return node.info()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1+
# Debug API
2+
3+
proc get_waku_v2_debug_v1_info(): WakuInfo
4+
5+
# Relay API
6+
7+
proc post_waku_v2_relay_v1_subscriptions(topics: seq[string]): bool
8+
proc delete_waku_v2_relay_v1_subscriptions(topics: seq[string]): bool
9+
10+
# Store API
11+
112
proc get_waku_v2_store_v1_messages(topics: seq[ContentTopic], pagingOptions: Option[StorePagingOptions]): StoreResponse

waku/v2/node/jsonrpc/relay_api.nim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import
2+
json_rpc/rpcserver,
3+
eth/[common, rlp, keys, p2p],
4+
../../waku_types,
5+
../wakunode2
6+
7+
proc installRelayApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
8+
const futTimeout = 5.seconds
9+
10+
## Relay API version 1 definitions
11+
12+
rpcsrv.rpc("post_waku_v2_relay_v1_subscriptions") do(topics: seq[string]) -> bool:
13+
## Subscribes a node to a list of PubSub topics
14+
debug "post_waku_v2_relay_v1_subscriptions"
15+
16+
proc topicHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
17+
let msg = WakuMessage.init(data)
18+
if msg.isOk():
19+
debug "WakuMessage received", msg=msg, topic=topic
20+
# @TODO handle message
21+
else:
22+
debug "WakuMessage received but failed to decode", msg=msg, topic=topic
23+
# @TODO handle message decode failure
24+
25+
var failedTopics: seq[string]
26+
27+
# Subscribe to all requested topics
28+
for topic in topics:
29+
# If any topic fails to subscribe, add to list of failedTopics
30+
if not(await node.subscribe(topic, topicHandler).withTimeout(futTimeout)):
31+
failedTopics.add(topic)
32+
33+
if (failedTopics.len() == 0):
34+
# Successfully subscribed to all requested topics
35+
return true
36+
else:
37+
# Failed to subscribe to one or more topics
38+
raise newException(ValueError, "Failed to subscribe to topics " & repr(failedTopics))
39+
40+
rpcsrv.rpc("delete_waku_v2_relay_v1_subscriptions") do(topics: seq[string]) -> bool:
41+
## Unsubscribes a node from a list of PubSub topics
42+
debug "delete_waku_v2_relay_v1_subscriptions"
43+
44+
var failedTopics: seq[string]
45+
46+
# Unsubscribe all handlers from requested topics
47+
for topic in topics:
48+
# If any topic fails to unsubscribe, add to list of failedTopics
49+
if not(await node.unsubscribeAll(topic).withTimeout(futTimeout)):
50+
failedTopics.add(topic)
51+
52+
if (failedTopics.len() == 0):
53+
# Successfully unsubscribed from all requested topics
54+
return true
55+
else:
56+
# Failed to unsubscribe from one or more topics
57+
raise newException(ValueError, "Failed to unsubscribe from topics " & repr(failedTopics))
58+

waku/v2/node/jsonrpc/store_api.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import
66
../wakunode2,
77
./jsonrpc_types, ./jsonrpc_utils
88

9-
proc setupWakuJSONRPC*(node: WakuNode, rpcsrv: RpcServer) =
9+
proc installStoreApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
1010
const futTimeout = 5.seconds
1111

1212
## Store API version 1 definitions

0 commit comments

Comments
 (0)