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 <kim.demey@gmail.com>

* Update waku/node/v2/wakunode2.nim

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

Co-authored-by: Kim De Mey <kim.demey@gmail.com>
This commit is contained in:
Oskar Thorén 2020-08-27 18:15:46 +08:00 committed by GitHub
parent e875dfd1d7
commit bab0e12d68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 58 deletions

View File

@ -20,11 +20,10 @@ proc init*(T: type WakuNode, conf: WakuNodeConf): Future[T]
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler)
## Subscribes to a PubSub topic. Triggers handler when receiving messages on ## 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. ## NOTE The data field SHOULD be decoded as a WakuMessage.
## TODO Implement as wrapper around `waku_protocol`, and ensure Message is ## Status: Implemented.
## passed, not `data` field.
method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler)
## Subscribes to a ContentFilter. Triggers handler when receiving messages on ## 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`. ## has to match the `ContentTopic`.
## Status: Not yet implemented. ## Status: Not yet implemented.
## TODO Implement as wrapper around `waku_protocol` and `subscribe` above, and ## TODO Implement as wrapper around `waku_filter` and `subscribe` above.
## ensure Message is passed, not `data` field.
method unsubscribe*(w: WakuNode, topic: Topic) method unsubscribe*(w: WakuNode, topic: Topic)
## Unsubscribe from a topic. ## Unsubscribe from a topic.
@ -51,24 +49,22 @@ method unsubscribe*(w: WakuNode, contentFilter: ContentFilter)
method publish*(w: WakuNode, topic: Topic, message: Message) method publish*(w: WakuNode, topic: Topic, message: Message)
## Publish a `Message` to a PubSub topic. ## Publish a `Message` to a PubSub topic.
## ##
## Status: Not yet implemented. ## Status: Partially implemented.
## TODO Implement as wrapper around `waku_protocol`, and ensure Message is ## TODO WakuMessage OR seq[byte]. NOT PubSub Message.
## passed, not `data` field.
method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message: Message) method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message: Message)
## Publish a `Message` to a PubSub topic with a specific content filter. ## Publish a `Message` to a PubSub topic with a specific content filter.
## Currently this means a `contentTopic`. ## Currently this means a `contentTopic`.
## ##
## Status: Not yet implemented. ## Status: Not yet implemented.
## TODO Implement as wrapper around `waku_protocol` and `publish`, and ensure ## TODO Implement as wrapper around `waku_relay` and `publish`.
## Message is passed, not `data` field. Also ensure content filter is in ## TODO WakuMessage. Ensure content filter is in it.
## Message.
method query*(w: WakuNode, query: HistoryQuery): HistoryResponse method query*(w: WakuNode, query: HistoryQuery): HistoryResponse
## Queries for historical messages. ## Queries for historical messages.
## ##
## Status: Not yet implemented. ## 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 ## JSON RPC

View File

@ -33,30 +33,3 @@ proc runBackground(conf: WakuNodeConf) {.async.} =
discard runBackground(conf) discard runBackground(conf)
runForever() 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

View File

@ -243,24 +243,21 @@ method init*(T: type WakuNode, conf: WakuNodeConf): Future[T] {.async.} =
await node.start(conf) await node.start(conf)
return node return node
# TODO Update TopicHandler to take Message, not seq[byte] data # NOTE TopicHandler is defined in pubsub.nim, roughly:
#type TopicHandler* = proc(topic: Topic, message: Message)
# Currently this is using the one in pubsub.nim, roughly:
#type TopicHandler* = proc(topic: string, data: seq[byte]) #type TopicHandler* = proc(topic: string, data: seq[byte])
type ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message) type ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message)
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) = method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) =
## Subscribes to a PubSub topic. Triggers handler when receiving messages on ## 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. ## NOTE The data field SHOULD be decoded as a WakuMessage.
## TODO Ensure Message is passed, not `data` field. This means modifying ## Status: Implemented.
## TopicHandler.
let wakuSub = w.switch.pubSub.get() let wakuRelay = w.switch.pubSub.get()
# XXX Consider awaiting here # XXX Consider awaiting here
discard wakuSub.subscribe(topic, handler) discard wakuRelay.subscribe(topic, handler)
method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) = method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) =
echo "NYI" echo "NYI"
@ -270,8 +267,7 @@ method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFil
## has to match the `ContentTopic`. ## has to match the `ContentTopic`.
## Status: Not yet implemented. ## Status: Not yet implemented.
## TODO Implement as wrapper around `waku_protocol` and `subscribe` above, and ## TODO Implement as wrapper around `waku_filter` and `subscribe` above.
## ensure Message is passed, not `data` field.
method unsubscribe*(w: WakuNode, topic: Topic) = method unsubscribe*(w: WakuNode, topic: Topic) =
echo "NYI" echo "NYI"
@ -291,7 +287,8 @@ method publish*(w: WakuNode, topic: Topic, message: Message) =
## Publish a `Message` to a PubSub topic. ## Publish a `Message` to a PubSub topic.
## ##
## Status: Partially implemented. ## 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() let wakuSub = w.switch.pubSub.get()
# XXX Consider awaiting here # XXX Consider awaiting here
discard wakuSub.publish(topic, message) discard wakuSub.publish(topic, message)
@ -301,24 +298,21 @@ method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message
## Currently this means a `contentTopic`. ## Currently this means a `contentTopic`.
## ##
## Status: Not yet implemented. ## Status: Not yet implemented.
## TODO Implement as wrapper around `waku_protocol` and `publish`, and ensure ## TODO Implement as wrapper around `waku_relay` and `publish`.
## Message is passed, not `data` field. Also ensure content filter is in ## TODO WakuMessage. Ensure content filter is in it.
## Message.
##
w.messages.insert((contentFilter.contentTopic, message)) w.messages.insert((contentFilter.contentTopic, message))
let wakuSub = w.switch.pubSub.get() let wakuSub = w.switch.pubSub.get()
# XXX Consider awaiting here # XXX Consider awaiting here
# @TODO MAKE SURE WE PASS CONTENT FILTER
discard wakuSub.publish(topic, message) discard wakuSub.publish(topic, message)
method query*(w: WakuNode, query: HistoryQuery): HistoryResponse = method query*(w: WakuNode, query: HistoryQuery): HistoryResponse =
## Queries for historical messages. ## Queries for historical messages.
## ##
## Status: Not yet implemented. ## 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]() result.messages = newSeq[Message]()
for msg in w.messages: for msg in w.messages: