Oskar Thorén a8dbf8a7b6
Bump submodules (#157)
* Upgrade all submodules

* Use stock standard_setup and remove our version

Switch no longer relies on Pubsub argument

* Fix peerId

* Add reference to WakuRelay in WakuNode

* Use WakuRelay ref directly instead of via switch

* Tweak standard switch sig

* Fix start_network peerid

* Import nim-libp2p utils test

* Use WakuRelay in test utils

* Fix utils imports

* Tweak

* Fix trigger self test

* Disable broken filter test

* Fix and amend logscope topics

* Make subscribe calls async to use await

* Add debug stuff to nimble file

* Await for subscribe content

* Sleeping in tests

* Local checkout fixes

* XXX: Try to use .PubSub on WakuRelay

* Revert "XXX: Try to use .PubSub on WakuRelay"

This reverts commit 3a3139e4cfbb5ae9500fd30b2e79c676ccc4a53b.

* Only using gossip seems to work

Subscribe for floodsub broken

* Fix await in examples

* Get rid of double publish, still need sleep
2020-09-16 12:23:10 +08:00

68 lines
2.4 KiB
Nim

import
std/options,
json_rpc/rpcserver,
nimcrypto/[sysrand, hmac, sha2],
eth/[common, rlp, keys, p2p],
../../../protocol/v2/waku_relay,
../waku_types, ../wakunode2
# Instead of using rlpx waku_protocol here, lets do mock waku2_protocol
# This should wrap GossipSub, not use EthereumNode here
# In Waku0/1 we use node.protocolState(Waku) a lot to get information
# Also keys to get priate key, etc
# 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*(node: WakuNode, 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 = WakuRelayCodec
# TODO: Implement symkey etc logic
rpcsrv.rpc("waku_publish") do(topic: string, payload: seq[byte]) -> bool:
let wakuRelay = node.wakuRelay
# 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:
# raise newException(ValueError, "Message could not be posted")
rpcsrv.rpc("waku_publish2") do(topic: string, payload: seq[byte]) -> bool:
let msg = WakuMessage.init(payload)
if msg.isOk():
debug "waku_publish", msg=msg
else:
warn "waku_publish decode error", msg=msg
debug "waku_publish", topic=topic, payload=payload, msg=msg[]
node.publish(topic, msg[])
return true
#if not result:
# raise newException(ValueError, "Message could not be posted")
# TODO: Handler / Identifier logic
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
debug "waku_subscribe", topic=topic
# XXX: Hacky in-line handler
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
let msg = WakuMessage.init(data)
if msg.isOk():
debug "waku_subscribe handler", msg=msg
else:
warn "waku_subscribe decode error", msg=msg
var readable_str = cast[string](msg[].payload)
info "Hit subscribe handler", topic=topic, msg=msg[], payload=readable_str
# XXX: Can we make this context async to use await?
discard node.subscribe(topic, handler)
return true
#if not result:
# raise newException(ValueError, "Message could not be posted")