diff --git a/tests/v2/test_wakunode.nim b/tests/v2/test_wakunode.nim index 1f3f90a26..cb33697ca 100644 --- a/tests/v2/test_wakunode.nim +++ b/tests/v2/test_wakunode.nim @@ -10,6 +10,7 @@ import libp2p/switch, libp2p/protocols/pubsub/rpc/messages, libp2p/protocols/pubsub/pubsub, + libp2p/protocols/pubsub/gossipsub, eth/keys, ../../waku/v2/protocol/[waku_relay, waku_message, message_notifier], ../../waku/v2/protocol/waku_store/waku_store, @@ -394,7 +395,8 @@ procSuite "WakuNode": await node1.stop() await node2.stop() await node3.stop() -asyncTest "testing rln-relay with mocked zkp": + + asyncTest "testing rln-relay with mocked zkp": let # publisher node @@ -449,3 +451,41 @@ asyncTest "testing rln-relay with mocked zkp": await node1.stop() await node2.stop() await node3.stop() + + asyncTest "Relay protocol is started correctly": + let + nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[] + node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"), + Port(60000)) + + # Relay protocol starts if mounted after node start + + await node1.start() + + node1.mountRelay() + + check: + GossipSub(node1.wakuRelay).heartbeatFut.isNil == false + + # Relay protocol starts if mounted before node start + + let + nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[] + node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"), + Port(60002)) + + node2.mountRelay() + + check: + # Relay has not yet started as node has not yet started + GossipSub(node2.wakuRelay).heartbeatFut.isNil + + await node2.start() + + check: + # Relay started on node start + GossipSub(node2.wakuRelay).heartbeatFut.isNil == false + + await allFutures([node1.stop(), node2.stop()]) + + diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index de62ae25f..0211d3bcb 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -66,6 +66,7 @@ type filters*: Filters subscriptions*: MessageNotificationSubscriptions rng*: ref BrHmacDrbgContext + started*: bool # Indicates that node has started listening # NOTE Any difference here in Waku vs Eth2? # E.g. Devp2p/Libp2p support, etc. @@ -162,12 +163,19 @@ proc start*(node: WakuNode) {.async.} = ## XXX: this should be /ip4..., / stripped? info "Listening on", full = listenStr + if not node.wakuRelay.isNil: + await node.wakuRelay.start() + + node.started = true + proc stop*(node: WakuNode) {.async.} = if not node.wakuRelay.isNil: await node.wakuRelay.stop() await node.switch.stop() + node.started = false + proc subscribe*(node: WakuNode, topic: Topic, handler: TopicHandler) = ## Subscribes to a PubSub topic. Triggers handler when receiving messages on ## this topic. TopicHandler is a method that takes a topic and some data. @@ -415,6 +423,13 @@ proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string](), rlnRela # TODO currently the message validator is set for the defaultTopic, this can be configurable to accept other pubsub topics as well addRLNRelayValidator(node, defaultTopic) info "WakuRLNRelay is mounted successfully" + + if node.started: + # Node has already started. Start the WakuRelay protocol + + waitFor node.wakuRelay.start() + + info "relay mounted and started successfully" ## Helpers