From 6974d8d90fefb559d8b50749596a2e1ede65366d Mon Sep 17 00:00:00 2001 From: Oskar Thoren Date: Fri, 22 May 2020 14:18:14 +0800 Subject: [PATCH] Change Waku RPC to have access to wakuProto; basic publish rpc --- waku/node/v2/rpc/wakucallsigs.nim | 1 + waku/node/v2/rpc/wakurpc.nim | 15 +++++++++++++- waku/node/v2/wakunode.nim | 33 ++++++++++++------------------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/waku/node/v2/rpc/wakucallsigs.nim b/waku/node/v2/rpc/wakucallsigs.nim index ba6ba5185..b24847f6b 100644 --- a/waku/node/v2/rpc/wakucallsigs.nim +++ b/waku/node/v2/rpc/wakucallsigs.nim @@ -1,5 +1,6 @@ # NOTE: Taken from v1, only version exists right now proc waku_version(): string +proc waku_publish(message: string): bool proc waku_info(): WakuInfo proc waku_setMaxMessageSize(size: uint64): bool diff --git a/waku/node/v2/rpc/wakurpc.nim b/waku/node/v2/rpc/wakurpc.nim index fd1ba46f5..96df9f03b 100644 --- a/waku/node/v2/rpc/wakurpc.nim +++ b/waku/node/v2/rpc/wakurpc.nim @@ -18,13 +18,26 @@ from stew/byteutils import hexToSeqByte, hexToByteArray # Where is the equivalent in Waku/2? # TODO: Extend to get access to protocol state and keys #proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) = -proc setupWakuRPC*(rpcsrv: RpcServer) = +proc setupWakuRPC*(wakuProto: WakuProto, rpcsrv: RpcServer) = # Seems easy enough, lets try to get this first rpcsrv.rpc("waku_version") do() -> string: ## Returns string of the current Waku protocol version. result = WakuSubCodec + # XXX Type + # TODO: Implement symkey etc logic + rpcsrv.rpc("waku_post") do(message: string) -> bool: + let data = cast[seq[byte]](message) + # Assumes someone subscribing on this topic + #let wakuSub = wakuProto.switch.pubsub + let wakuSub = cast[WakuSub](wakuProto.switch.pubSub.get()) + # XXX also future return type + discard wakuSub.publish("foobar", data) + return true + #if not result: + # raise newException(ValueError, "Message could not be posted") + # TODO: Dial/Connect # XXX: Though wrong layer for that - wait how does this work in devp2p sim? # We connect to nodes there, should be very similar here diff --git a/waku/node/v2/wakunode.nim b/waku/node/v2/wakunode.nim index 44479e87c..170ce248c 100644 --- a/waku/node/v2/wakunode.nim +++ b/waku/node/v2/wakunode.nim @@ -144,26 +144,6 @@ proc run(config: WakuNodeConf) = peerInfo.addrs.add(Multiaddress.init(DefaultAddr)) - # XXX: It isn't clear that it is a good idea use dial/connect as RPC - # But let's try it - # More of a hello world thing, we'd want to mount it on to of gossipsub - if config.rpc: - # What is ta? transport address...right. - let ta = initTAddress(config.rpcAddress, - Port(config.rpcPort + config.portsShift)) - var rpcServer = newRpcHttpServer([ta]) - - # Not using keys right now - let keys = newKeyStorage() - #setupWakuRPC(node, keys, rpcServer) - #setupWakuSimRPC(node, rpcServer) - setupWakuRPC(rpcServer) - rpcServer.start() - - # TODO: Use it to get waku version - # Huh not printed - info "rpcServer started", ta=ta - # TODO: Here setup a libp2p node # Essentially something like this in nbc/eth2_network: # proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] @@ -179,10 +159,23 @@ proc run(config: WakuNodeConf) = # Is it a "Standard" Switch? Assume it is for now # NOTE: This should be WakuSub here + + # XXX: Do we want to use this wakuProto? Or Switch? + # We need access to the WakuSub thing + # switch.pubsub = wakusub, plus all the peer info etc + # And it has wakuProto lets use wakuProto maybe, cause it has switch var switch = newStandardSwitch(some keys.seckey, hostAddress, triggerSelf = true, gossip = false) let wakuProto = newWakuProto(switch) switch.mount(wakuProto) + if config.rpc: + let ta = initTAddress(config.rpcAddress, + Port(config.rpcPort + config.portsShift)) + var rpcServer = newRpcHttpServer([ta]) + setupWakuRPC(wakuProto, rpcServer) + rpcServer.start() + info "rpcServer started", ta=ta + # TODO: Make context async #let fut = await switch.start() discard switch.start()