From 97960946c47eb0340b84f434a1172c55faa19045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Thor=C3=A9n?= Date: Wed, 14 Oct 2020 18:34:29 +0800 Subject: [PATCH] Fix timing issue with subscribe in chat2 (#226) - Support static node - Ensure nodes are connected before subscribing - Sleep to allow settling down before subscribing - Default chat2 log to DEBUG --- examples/v2/chat2.nim | 15 +++++++++++++-- waku.nimble | 3 ++- waku/node/v2/wakunode2.nim | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/v2/chat2.nim b/examples/v2/chat2.nim index 84b60531a..8f34e5c6f 100644 --- a/examples/v2/chat2.nim +++ b/examples/v2/chat2.nim @@ -66,6 +66,11 @@ proc dialPeer(c: Chat, address: string) {.async.} = discard await c.node.switch.dial(remotePeer, WakuRelayCodec) c.connected = true +proc connectToNodes(c: Chat, nodes: openArray[string]) = + echo "Connecting to nodes" + for nodeId in nodes: + discard dialPeer(c, nodeId) + proc publish(c: Chat, line: string) = let payload = cast[seq[byte]](line) let message = WakuMessage(payload: payload, contentTopic: DefaultContentTopic) @@ -158,6 +163,11 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} = # waitFor vs await await node.start() + var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true) + + if conf.staticnodes.len > 0: + connectToNodes(chat, conf.staticnodes) + let peerInfo = node.peerInfo let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId echo &"Listening on\n {listenStr}" @@ -171,9 +181,10 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} = let payload = cast[string](message.payload) echo &"{payload}" info "Hit subscribe handler", topic=topic, payload=payload, contentTopic=message.contentTopic - await node.subscribe(topic, handler) - var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true) + # XXX Timing issue with subscribe, need to wait a bit to ensure GRAFT message is sent + await sleepAsync(5.seconds) + await node.subscribe(topic, handler) await chat.readWriteLoop() runForever() diff --git a/waku.nimble b/waku.nimble index 0e3748735..951b83a72 100644 --- a/waku.nimble +++ b/waku.nimble @@ -80,4 +80,5 @@ task chat2, "Build example Waku v2 chat usage": let name = "chat2" # NOTE For debugging, set debug level. For chat usage we want minimal log # output to STDOUT. Can be fixed by redirecting logs to file (e.g.) - buildBinary name, "examples/v2/", "-d:chronicles_log_level=WARN" + #buildBinary name, "examples/v2/", "-d:chronicles_log_level=WARN" + buildBinary name, "examples/v2/", "-d:chronicles_log_level=DEBUG" diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index 4e864a20e..bf9806c2c 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -97,6 +97,8 @@ proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey, filters: initTable[string, Filter]() ) + # TODO This is _not_ safe. Subscribe should happen in start and after things settled down. + # Otherwise GRAFT message isn't sent to a node. for topic in topics: proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = debug "Hit handler", topic=topic, data=data