From bab0e12d6859ffe1382afab117735877f9da8aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Thor=C3=A9n?= Date: Thu, 27 Aug 2020 18:15:46 +0800 Subject: [PATCH] Waku v2: Update docs and API with current state (#110) * Waku v2: Update docs and API with current state - Publish and subscribe implemented - Three protocol split * Update docs/api/v2/node.md Co-authored-by: Kim De Mey * Update waku/node/v2/wakunode2.nim Co-authored-by: Kim De Mey Co-authored-by: Kim De Mey --- docs/api/v2/node.md | 22 +++++++++------------- examples/v2/basic2.nim | 27 --------------------------- waku/node/v2/wakunode2.nim | 30 ++++++++++++------------------ 3 files changed, 21 insertions(+), 58 deletions(-) diff --git a/docs/api/v2/node.md b/docs/api/v2/node.md index 57dabbe5d..a57ae5ce3 100644 --- a/docs/api/v2/node.md +++ b/docs/api/v2/node.md @@ -20,11 +20,10 @@ proc init*(T: type WakuNode, conf: WakuNodeConf): Future[T] method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) ## Subscribes to a PubSub topic. Triggers handler when receiving messages on - ## this topic. TopicHandler is a method that takes a topic and a `Message`. + ## this topic. TopicHandler is a method that takes a topic and some data. ## - ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol`, and ensure Message is - ## passed, not `data` field. + ## NOTE The data field SHOULD be decoded as a WakuMessage. + ## Status: Implemented. method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) ## Subscribes to a ContentFilter. Triggers handler when receiving messages on @@ -33,8 +32,7 @@ method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFil ## has to match the `ContentTopic`. ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and `subscribe` above, and - ## ensure Message is passed, not `data` field. + ## TODO Implement as wrapper around `waku_filter` and `subscribe` above. method unsubscribe*(w: WakuNode, topic: Topic) ## Unsubscribe from a topic. @@ -51,24 +49,22 @@ method unsubscribe*(w: WakuNode, contentFilter: ContentFilter) method publish*(w: WakuNode, topic: Topic, message: Message) ## Publish a `Message` to a PubSub topic. ## - ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol`, and ensure Message is - ## passed, not `data` field. + ## Status: Partially implemented. + ## TODO WakuMessage OR seq[byte]. NOT PubSub Message. method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message: Message) ## Publish a `Message` to a PubSub topic with a specific content filter. ## Currently this means a `contentTopic`. ## ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and `publish`, and ensure - ## Message is passed, not `data` field. Also ensure content filter is in - ## Message. + ## TODO Implement as wrapper around `waku_relay` and `publish`. + ## TODO WakuMessage. Ensure content filter is in it. method query*(w: WakuNode, query: HistoryQuery): HistoryResponse ## Queries for historical messages. ## ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and send `RPCMsg`. + ## TODO Implement as wrapper around `waku_store` and send RPC. ``` ## JSON RPC diff --git a/examples/v2/basic2.nim b/examples/v2/basic2.nim index 8b62f43bd..7d42dd31d 100644 --- a/examples/v2/basic2.nim +++ b/examples/v2/basic2.nim @@ -33,30 +33,3 @@ proc runBackground(conf: WakuNodeConf) {.async.} = discard runBackground(conf) runForever() - -# TODO Lets start with Nim Node API first - -# To do more operations on this node, we can do this in two ways: -# - We can use the Nim Node API, which is currently not exposed -# - We can use JSON RPC - -# TODO Subscribe and publish to topic via Node API -# Requires https://github.com/status-im/nim-waku/issues/53 - -# Here's how to do it with JSON RPC - -# Get RPC call signatures in Nim -#from strutils import rsplit -#template sourceDir: string = currentSourcePath.parentDir().parentDir().rsplit(DirSep, 1)[0] -#const sigWakuPath = sourceDir / "waku" / "node" / "v2" / "rpc" / "wakucallsigs.nim" - -#createRpcSigs(RpcHttpClient, sigWakuPath) - -# Create RPC Client and connect to it -#var node = newRpcHttpClient() -#waitfor node.connect("localhost", Port(8547)) - -# TODO Do something with res -#var res = waitFor node.wakuSubscribe("apptopic") - -# TODO Use publish as well diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index b740d98f7..445b6bcef 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -243,24 +243,21 @@ method init*(T: type WakuNode, conf: WakuNodeConf): Future[T] {.async.} = await node.start(conf) return node -# TODO Update TopicHandler to take Message, not seq[byte] data -#type TopicHandler* = proc(topic: Topic, message: Message) -# Currently this is using the one in pubsub.nim, roughly: +# NOTE TopicHandler is defined in pubsub.nim, roughly: #type TopicHandler* = proc(topic: string, data: seq[byte]) type ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message) method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) = ## Subscribes to a PubSub topic. Triggers handler when receiving messages on - ## this topic. TopicHandler is a method that takes a topic and a `Message`. + ## this topic. TopicHandler is a method that takes a topic and some data. ## - ## Status: Partially implemented. - ## TODO Ensure Message is passed, not `data` field. This means modifying - ## TopicHandler. + ## NOTE The data field SHOULD be decoded as a WakuMessage. + ## Status: Implemented. - let wakuSub = w.switch.pubSub.get() + let wakuRelay = w.switch.pubSub.get() # XXX Consider awaiting here - discard wakuSub.subscribe(topic, handler) + discard wakuRelay.subscribe(topic, handler) method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) = echo "NYI" @@ -270,8 +267,7 @@ method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFil ## has to match the `ContentTopic`. ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and `subscribe` above, and - ## ensure Message is passed, not `data` field. + ## TODO Implement as wrapper around `waku_filter` and `subscribe` above. method unsubscribe*(w: WakuNode, topic: Topic) = echo "NYI" @@ -291,7 +287,8 @@ method publish*(w: WakuNode, topic: Topic, message: Message) = ## Publish a `Message` to a PubSub topic. ## ## Status: Partially implemented. - ## TODO: Esure Message is passed, not seq[byte] `data` field. + ## + ## TODO WakuMessage OR seq[byte]. NOT PubSub Message. let wakuSub = w.switch.pubSub.get() # XXX Consider awaiting here discard wakuSub.publish(topic, message) @@ -301,24 +298,21 @@ method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message ## Currently this means a `contentTopic`. ## ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and `publish`, and ensure - ## Message is passed, not `data` field. Also ensure content filter is in - ## Message. - ## + ## TODO Implement as wrapper around `waku_relay` and `publish`. + ## TODO WakuMessage. Ensure content filter is in it. w.messages.insert((contentFilter.contentTopic, message)) let wakuSub = w.switch.pubSub.get() # XXX Consider awaiting here - # @TODO MAKE SURE WE PASS CONTENT FILTER discard wakuSub.publish(topic, message) method query*(w: WakuNode, query: HistoryQuery): HistoryResponse = ## Queries for historical messages. ## ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol` and send `RPCMsg`. + ## TODO Implement as wrapper around `waku_store` and send RPC. result.messages = newSeq[Message]() for msg in w.messages: