From 9ac41b2eda408c4f588c5ad6993fbb69b687fb62 Mon Sep 17 00:00:00 2001 From: Dean Eigenmann <7621705+decanus@users.noreply.github.com> Date: Fri, 9 Oct 2020 15:58:50 +0200 Subject: [PATCH] feature/filter-rpc-script (#200) * started working * fixes * added tutorial * added * rm from description --- docs/tutorial/filter.md | 33 +++++++++++++++++++++++ waku.nimble | 1 + waku/node/v2/config.nim | 5 ++++ waku/node/v2/rpc/rpc_subscribe_filter.nim | 32 ++++++++++++++++++++++ waku/node/v2/wakunode2.nim | 10 +++++++ 5 files changed, 81 insertions(+) create mode 100644 docs/tutorial/filter.md create mode 100644 waku/node/v2/rpc/rpc_subscribe_filter.nim diff --git a/docs/tutorial/filter.md b/docs/tutorial/filter.md new file mode 100644 index 000000000..2e339bce9 --- /dev/null +++ b/docs/tutorial/filter.md @@ -0,0 +1,33 @@ +# Running Filter 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 --filternode:/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_filter 8546 # enter your topic default is "foobar" +./build/rpc_publish 8545 # enter your message in STDIN +``` + +You should see other node receive something. diff --git a/waku.nimble b/waku.nimble index c6edf1ddf..0e3748735 100644 --- a/waku.nimble +++ b/waku.nimble @@ -72,6 +72,7 @@ task test2, "Build & run Waku v2 tests": 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_subscribe_filter", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG" buildBinary "rpc_query", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG" buildBinary "rpc_info", "waku/node/v2/rpc/", "-d:chronicles_log_level=DEBUG" diff --git a/waku/node/v2/config.nim b/waku/node/v2/config.nim index 548b0d1cc..ef1547744 100644 --- a/waku/node/v2/config.nim +++ b/waku/node/v2/config.nim @@ -73,6 +73,11 @@ type defaultValue: "" name: "storenode" }: string + filternode* {. + desc: "Enode URL to filter.", + defaultValue: "" + name: "filternode" }: string + whisper* {. desc: "Enable the Whisper protocol." defaultValue: false diff --git a/waku/node/v2/rpc/rpc_subscribe_filter.nim b/waku/node/v2/rpc/rpc_subscribe_filter.nim new file mode 100644 index 000000000..6af70f336 --- /dev/null +++ b/waku/node/v2/rpc/rpc_subscribe_filter.nim @@ -0,0 +1,32 @@ +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) + +let pubSubTopic = "waku" +let contentTopic = "foobar" +var res = waitfor node.wakuSubscribeFilter(pubSubTopic, @[@[contentTopic]]) +echo "Waku query response: ", res diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index 83e67e0c2..20d7eb4bc 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -249,6 +249,13 @@ when isMainModule: n.wakuStore.setPeer(remotePeer) + proc setFilterPeer(n: WakuNode, address: string) = + info "dialPeer", address = address + + let remotePeer = parsePeerInfo(address) + + n.wakuFilter.setPeer(remotePeer) + proc connectToNodes(n: WakuNode, nodes: openArray[string]) = for nodeId in nodes: info "connectToNodes", node = nodeId @@ -298,6 +305,9 @@ when isMainModule: if conf.storenode != "": setStorePeer(node, conf.storenode) + if conf.filternode != "": + setFilterPeer(node, conf.filternode) + if conf.rpc: startRpc(node, conf.rpcAddress, Port(conf.rpcPort + conf.portsShift))