From bcc931baf514d3ac5f7a93d66ca458fddacde453 Mon Sep 17 00:00:00 2001 From: Dean Eigenmann <7621705+decanus@users.noreply.github.com> Date: Fri, 25 Sep 2020 13:35:32 +0200 Subject: [PATCH] feature/rpc-query (#186) * started working on rpc query * rpc * fixes * setup * made it work * update * Create store.md * Update nangang.md --- docs/tutorial/store.md | 34 ++++++++++++++++++++++++++++++++++ waku.nimble | 1 + waku/node/v2/config.nim | 5 +++++ waku/node/v2/rpc/rpc_query.nim | 30 ++++++++++++++++++++++++++++++ waku/node/v2/wakunode2.nim | 22 +++++++++++++++++----- 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 docs/tutorial/store.md create mode 100644 waku/node/v2/rpc/rpc_query.nim diff --git a/docs/tutorial/store.md b/docs/tutorial/store.md new file mode 100644 index 000000000..0f17852c0 --- /dev/null +++ b/docs/tutorial/store.md @@ -0,0 +1,34 @@ +# Running Store Protocol + +## 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 --storenode:/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 +./build/rpc_query 8546 # enter your topic default is "foobar" +``` + +You should see other node receive something. diff --git a/waku.nimble b/waku.nimble index ee6f5c2fe..171bd1cef 100644 --- a/waku.nimble +++ b/waku.nimble @@ -71,6 +71,7 @@ task wakusim2, "Build Experimental Waku simulation tools": 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" + buildBinary "rpc_query", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG" task wakuexample2, "Build example Waku usage": let name = "basic2" diff --git a/waku/node/v2/config.nim b/waku/node/v2/config.nim index 6442d52a4..548b0d1cc 100644 --- a/waku/node/v2/config.nim +++ b/waku/node/v2/config.nim @@ -68,6 +68,11 @@ type desc: "Enode URL to directly connect with. Argument may be repeated." name: "staticnode" }: seq[string] + storenode* {. + desc: "Enode URL to query for storage.", + defaultValue: "" + name: "storenode" }: string + whisper* {. desc: "Enable the Whisper protocol." defaultValue: false diff --git a/waku/node/v2/rpc/rpc_query.nim b/waku/node/v2/rpc/rpc_query.nim new file mode 100644 index 000000000..780537e80 --- /dev/null +++ b/waku/node/v2/rpc/rpc_query.nim @@ -0,0 +1,30 @@ +import + os, strutils, strformat, chronicles, json_rpc/[rpcclient, rpcserver], nimcrypto/sysrand, + libp2p/protobuf/minprotobuf, + libp2p/[peerinfo, multiaddress], + 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 topic:" +let raw_input = readLine(stdin) +let input = fmt"{raw_input}" +echo "Input is:", input + +var node = newRpcHttpClient() +waitfor node.connect("localhost", rpcPort) + +var res = waitfor node.wakuQuery("foo", @[input]) +echo "Waku query response: ", res diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index 661dfb4db..1f798a18e 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -199,15 +199,17 @@ when isMainModule: confutils, json_rpc/rpcserver, metrics, ./config, ./rpc/wakurpc, ../common + proc parsePeerInfo(address: string): PeerInfo = + let multiAddr = MultiAddress.initAddress(address) + let parts = address.split("/") + return PeerInfo.init(parts[^1], [multiAddr]) + proc dialPeer(n: WakuNode, address: string) {.async.} = info "dialPeer", address = address # XXX: This turns ipfs into p2p, not quite sure why - let multiAddr = MultiAddress.initAddress(address) - info "multiAddr", ma = multiAddr - let parts = address.split("/") - let remotePeer = PeerInfo.init(parts[^1], [multiAddr]) + let remotePeer = parsePeerInfo(address) - info "Dialing peer", multiAddr + info "Dialing peer", ma = remotePeer.addrs[0] # NOTE This is dialing on WakuRelay protocol specifically # TODO Keep track of conn and connected state somewhere (WakuRelay?) #p.conn = await p.switch.dial(remotePeer, WakuRelayCodec) @@ -215,6 +217,13 @@ when isMainModule: discard n.switch.dial(remotePeer, WakuRelayCodec) info "Post switch dial" + proc setStorePeer(n: WakuNode, address: string) = + info "dialPeer", address = address + + let remotePeer = parsePeerInfo(address) + + n.wakuStore.setPeer(remotePeer) + proc connectToNodes(n: WakuNode, nodes: openArray[string]) = for nodeId in nodes: info "connectToNodes", node = nodeId @@ -261,6 +270,9 @@ when isMainModule: if conf.staticnodes.len > 0: connectToNodes(node, conf.staticnodes) + if conf.storenode != "": + setStorePeer(node, conf.storenode) + if conf.rpc: startRpc(node, conf.rpcAddress, Port(conf.rpcPort + conf.portsShift))