Fix init signature according to Node API (#80)

* Fix init signature according to Node API

See https://github.com/status-im/nim-waku/blob/master/docs/api/v2/node.md

* Update docs and example
This commit is contained in:
Oskar Thorén 2020-07-28 16:06:00 +08:00 committed by GitHub
parent 75d7d4780e
commit 1ca3962e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 35 deletions

View File

@ -16,8 +16,7 @@ five method are:
proc init*(conf: WakuNodeConf): Future[WakuNode]
## Creates and starts a Waku node.
##
## Status: Partially implemented.
## TODO Take conf as a parameter and return a started WakuNode
## Status: Implemented.
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler)
## Subscribes to a PubSub topic. Triggers handler when receiving messages on

View File

@ -3,6 +3,7 @@
import confutils, chronicles, chronos, os
import stew/shims/net as stewNet
import libp2p/crypto/crypto
import libp2p/crypto/secp
import eth/keys
@ -14,16 +15,18 @@ import ../../waku/node/v2/wakunode2
# Loads the config in `waku/node/v2/config.nim`
let conf = WakuNodeConf.load()
# Start the node
# Running this should give you output like this:
# INF Listening on tid=5719 full=/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAmNiAqr1cwhyntotP9fiSDyBvfKtB4ZiaDsrkipSCoKCB4
# TODO Run this in background
# See https://github.com/status-im/nim-waku/pull/59
proc runBackground(conf: WakuNodeConf) {.async.} =
run(conf)
runForever()
# Create and start the node
#proc runBackground(conf: WakuNodeConf) {.async.} =
# init(conf)
# runForever()
discard runBackground(conf)
discard init(conf)
echo("Do stuff after with node")
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
@ -35,17 +38,17 @@ discard runBackground(conf)
# 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"
#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)
#createRpcSigs(RpcHttpClient, sigWakuPath)
# Create RPC Client and connect to it
var node = newRpcHttpClient()
waitfor node.connect("localhost", Port(8547))
#var node = newRpcHttpClient()
#waitfor node.connect("localhost", Port(8547))
# TODO Do something with res
var res = waitFor node.wakuSubscribe("apptopic")
#var res = waitFor node.wakuSubscribe("apptopic")
# TODO Use publish as well

View File

@ -218,27 +218,12 @@ proc start*(node: WakuNode, conf: WakuNodeConf) {.async.} =
addTimer(Moment.fromNow(2.seconds), logMetrics)
addTimer(Moment.fromNow(2.seconds), logMetrics)
# TODO Get rid of this
# runForever()
#proc run(conf: WakuNodeConf) {.async, gcsafe.} =
## Public API
##
# TODO Take conf as a parameter and return a started WakuNode
proc init*() {.async.} =
let conf = WakuNodeConf.load()
let network = await createWakuNode(conf)
waitFor network.start(conf)
runForever()
# TODO Replace init above
method init2*(conf: WakuNodeConf): Future[WakuNode] {.async.} =
method init*(conf: WakuNodeConf): Future[WakuNode] {.async.} =
## Creates and starts a Waku node.
##
## Status: Partially implemented.
## TODO Take conf as a parameter and return a started WakuNode
let node = await createWakuNode(conf)
await node.start(conf)
return node
@ -316,4 +301,6 @@ method query*(w: WakuNode, query: HistoryQuery): HistoryResponse =
## TODO Implement as wrapper around `waku_protocol` and send `RPCMsg`.
when isMainModule:
discard init()
let conf = WakuNodeConf.load()
discard init(conf)
runForever()