From e8d315657136ed9c72e28c106e2f0a56b8400120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Thor=C3=A9n?= Date: Tue, 1 Sep 2020 16:32:19 +0800 Subject: [PATCH] Make basic Waku v2 examples run again (#123) * Make basic Waku v2 examples run again Regression introduced in https://github.com/status-im/nim-waku/pull/117 Happened due to example not being run on CI. - Add wakuexample to all target - Fix example2 to run - Basic formatting * Update examples/v2/basic2.nim Co-authored-by: Kim De Mey * Update examples/v2/basic2.nim * Update examples/v2/basic2.nim * Update examples/v2/basic2.nim Co-authored-by: Kim De Mey --- Makefile | 2 +- examples/v2/basic2.nim | 43 ++++++++++++++++++++------------------ waku/node/v2/wakunode2.nim | 2 +- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 4c9af9822..6094c3bde 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ GIT_SUBMODULE_UPDATE := git submodule update --init --recursive else # "variables.mk" was included. Business as usual until the end of this file. # default target, because it's the first one that doesn't start with '.' -all: | wakunode wakusim wakuexample wakunode2 wakusim2 +all: | wakunode wakusim wakuexample wakunode2 wakusim2 wakuexample2 # must be included after the default target -include $(BUILD_SYSTEM_DIR)/makefiles/targets.mk diff --git a/examples/v2/basic2.nim b/examples/v2/basic2.nim index 76dbdd507..676eccb56 100644 --- a/examples/v2/basic2.nim +++ b/examples/v2/basic2.nim @@ -1,25 +1,27 @@ -# Here's an example of how you would start a Waku node, subscribe to topics, and -# publish to them +## Here's a basic example of how you would start a Waku node, subscribe to +## topics, and publish to them. -import confutils, chronicles, chronos, os - -import stew/shims/net as stewNet -import libp2p/crypto/crypto -import libp2p/crypto/secp -import eth/keys -import json_rpc/[rpcclient, rpcserver] - -import ../../waku/node/v2/config -import ../../waku/node/v2/wakunode2 -import ../../waku/node/v2/waku_types - -# Loads the config in `waku/node/v2/config.nim` -let conf = WakuNodeConf.load() +import + std/os, + confutils, chronicles, chronos, + stew/shims/net as stewNet, + libp2p/crypto/[crypto,secp], + eth/keys, + json_rpc/[rpcclient, rpcserver], + ../../waku/node/v2/[config, wakunode2, waku_types], + ../../waku/node/common # Node operations happens asynchronously -proc runBackground(conf: WakuNodeConf) {.async.} = - # Create and start the node - let node = await WakuNode.init(conf) +proc runBackground() {.async.} = + let + conf = WakuNodeConf.load() + (extIp, extTcpPort, extUdpPort) = setupNat(conf.nat, clientId, + Port(uint16(conf.tcpPort) + conf.portsShift), + Port(uint16(conf.udpPort) + conf.portsShift)) + node = WakuNode.init(conf.nodeKey, conf.libp2pAddress, + Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort) + + await node.start() # Subscribe to a topic let topic = "foobar" @@ -31,6 +33,7 @@ proc runBackground(conf: WakuNodeConf) {.async.} = let message = cast[seq[byte]]("hello world") node.publish(topic, message) -discard runBackground(conf) +# TODO Await with try/except here +discard runBackground() runForever() diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index 31d73ec2d..9f70a081c 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -30,7 +30,7 @@ type HistoryResponse* = object messages*: seq[Message] -const clientId = "Nimbus Waku v2 node" +const clientId* = "Nimbus Waku v2 node" # NOTE Any difference here in Waku vs Eth2? # E.g. Devp2p/Libp2p support, etc.