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
This commit is contained in:
Oskar Thorén 2020-10-14 18:34:29 +08:00 committed by GitHub
parent f31ab81546
commit d6fdecafd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -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()

View File

@ -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"

View File

@ -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