Add basic rpc scripts to publish and subscribe (#140)

* Add basic rpc scripts to publish and subscribe

* Fix publish topic and payload script

Also change parameter name in waku relay due to weird shadowing of log
topic:

DBG 2020-09-09 12:07:54+08:00 waku_publish                               tid=8795 file=wakurpc.nim:30 topic=waku payload=@[]
DBG 2020-09-09 12:07:54+08:00 publish                                    tid=8795 file=waku_relay.nim:65 topic=WakuRelay

Above should show topic=waku but it gets topic=WakuRelay from log scope
for some reason.

* Scripts take arguments

* Add basic nangang tutorial

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* meh

* ENsure subscribe call succeeds

Co-authored-by: Kim De Mey <kim.demey@gmail.com>
This commit is contained in:
Oskar Thorén 2020-09-10 12:18:35 +08:00 committed by GitHub
parent 76e0b49e2b
commit e5f0f36f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 112 additions and 8 deletions

View File

@ -90,6 +90,10 @@ wakusim2: | build deps wakunode2
echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim wakusim2 $(NIM_PARAMS) waku.nims
scripts2: | build deps wakunode2
echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim scripts2 $(NIM_PARAMS) waku.nims
protocol2:
echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim protocol2 $(NIM_PARAMS) waku.nims

36
docs/tutorial/nangang.md Normal file
View File

@ -0,0 +1,36 @@
# Nangang Test
Nangang is the first internal testnet. See
https://github.com/vacp2p/research/issues/43 for more.
## How to
Build:
```
# make wakunode2 is run as part of scripts2 target
make scripts2
```
Run two nodes and connect them:
```
# Starts listening on 60000 with RPC server on 8545.
# Note the "listening on address" in logs.
./build/wakunode2 --ports-shift:0
# Run another node with staticnode argument
./build/wakunode2 --ports-shift:1 --staticnode:/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmF4tuht6fmna6uDqoSMgFqhUrdaVR6VQRyGr6sCpfS2jp
```
You should see your nodes connecting.
Do basic RPC calls:
```
./build/rpc_subscribe 8545
./build/rpc_subscribe 8546
./build/rpc_publish 8545 # enter your message in STDIN
```
You should see other node receive something.

View File

@ -64,6 +64,10 @@ task wakusim2, "Build Experimental Waku simulation tools":
buildBinary "quicksim2", "waku/node/v2/", "-d:chronicles_log_level=DEBUG"
buildBinary "start_network2", "waku/node/v2/", "-d:chronicles_log_level=TRACE"
task scripts2, "Build Waku v2 scripts":
buildBinary "rpc_publish", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG"
buildBinary "rpc_subscribe", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG"
task wakuexample2, "Build example Waku usage":
let name = "basic2"
buildBinary name, "examples/v2/", "-d:chronicles_log_level=DEBUG"

View File

@ -0,0 +1,31 @@
import
os, strutils, strformat, chronicles, json_rpc/[rpcclient, rpcserver], nimcrypto/sysrand,
libp2p/protobuf/minprotobuf,
eth/common as eth_common, eth/keys,
system,
options
from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
const sigWakuPath = sourceDir / "wakucallsigs.nim"
createRpcSigs(RpcHttpClient, sigWakuPath)
if paramCount() < 1:
echo "Please provide rpcPort as argument."
quit(1)
let rpcPort = Port(parseInt(paramStr(1)))
echo "Please enter your message:"
let message = readLine(stdin)
echo "Message is:", message
var node = newRpcHttpClient()
waitfor node.connect("localhost", rpcPort)
# Subscribe ourselves to topic
#var res = node.wakuSubscribe("waku")
# TODO When RPC uses Node, create WakuMessage and pass instead
var res2 = waitfor node.wakuPublish("waku", cast[seq[byte]]("hello world"))

View File

@ -0,0 +1,27 @@
import
os, strutils, strformat, chronicles, json_rpc/[rpcclient, rpcserver], nimcrypto/sysrand,
libp2p/protobuf/minprotobuf,
eth/common as eth_common, eth/keys,
system,
options
from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
const sigWakuPath = sourceDir / "wakucallsigs.nim"
createRpcSigs(RpcHttpClient, sigWakuPath)
if paramCount() < 1:
echo "Please provide rpcPort as argument."
quit(1)
let rpcPort = Port(parseInt(paramStr(1)))
var client = newRpcHttpClient()
waitfor client.connect("localhost", rpcPort)
echo "Subscribing"
# Subscribe to waku topic
var res = waitFor client.wakuSubscribe("waku")
echo res

View File

@ -27,6 +27,7 @@ proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
# XXX also future return type
# TODO: Shouldn't we really be doing WakuNode publish here?
debug "waku_publish", topic=topic, payload=payload
discard wakuRelay.publish(topic, payload)
return true
#if not result:
@ -34,6 +35,7 @@ proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
# TODO: Handler / Identifier logic
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
debug "waku_subscribe", topic=topic
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
# XXX: Hacky in-line handler

View File

@ -50,24 +50,24 @@ method initPubSub*(w: WakuRelay) =
w.init()
method subscribe*(w: WakuRelay,
topic: string,
pubSubTopic: string,
handler: TopicHandler) {.async.} =
debug "subscribe", topic=topic
debug "subscribe", pubSubTopic=pubSubTopic
if w.gossipEnabled:
await procCall GossipSub(w).subscribe(topic, handler)
await procCall GossipSub(w).subscribe(pubSubTopic, handler)
else:
await procCall FloodSub(w).subscribe(topic, handler)
await procCall FloodSub(w).subscribe(pubSubTopic, handler)
method publish*(w: WakuRelay,
topic: string,
pubSubTopic: string,
message: seq[byte]
): Future[int] {.async.} =
debug "publish", topic=topic
debug "publish", pubSubTopic=pubSubTopic, message=message
if w.gossipEnabled:
return await procCall GossipSub(w).publish(topic, message)
return await procCall GossipSub(w).publish(pubSubTopic, message)
else:
return await procCall FloodSub(w).publish(topic, message)
return await procCall FloodSub(w).publish(pubSubTopic, message)
method unsubscribe*(w: WakuRelay,
topics: seq[TopicPair]) {.async.} =