Start relay protocol on wakunode2 (#446)

* Start relay protocol on wakunode2
This commit is contained in:
Hanno Cornelius 2021-04-01 11:37:14 +02:00 committed by GitHub
parent e7c21c2f74
commit 683577f045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import
libp2p/switch, libp2p/switch,
libp2p/protocols/pubsub/rpc/messages, libp2p/protocols/pubsub/rpc/messages,
libp2p/protocols/pubsub/pubsub, libp2p/protocols/pubsub/pubsub,
libp2p/protocols/pubsub/gossipsub,
eth/keys, eth/keys,
../../waku/v2/protocol/[waku_relay, waku_message, message_notifier], ../../waku/v2/protocol/[waku_relay, waku_message, message_notifier],
../../waku/v2/protocol/waku_store/waku_store, ../../waku/v2/protocol/waku_store/waku_store,
@ -394,6 +395,7 @@ procSuite "WakuNode":
await node1.stop() await node1.stop()
await node2.stop() await node2.stop()
await node3.stop() await node3.stop()
asyncTest "testing rln-relay with mocked zkp": asyncTest "testing rln-relay with mocked zkp":
let let
@ -449,3 +451,41 @@ asyncTest "testing rln-relay with mocked zkp":
await node1.stop() await node1.stop()
await node2.stop() await node2.stop()
await node3.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()])

View File

@ -66,6 +66,7 @@ type
filters*: Filters filters*: Filters
subscriptions*: MessageNotificationSubscriptions subscriptions*: MessageNotificationSubscriptions
rng*: ref BrHmacDrbgContext rng*: ref BrHmacDrbgContext
started*: bool # Indicates that node has started listening
# NOTE Any difference here in Waku vs Eth2? # NOTE Any difference here in Waku vs Eth2?
# E.g. Devp2p/Libp2p support, etc. # E.g. Devp2p/Libp2p support, etc.
@ -162,12 +163,19 @@ proc start*(node: WakuNode) {.async.} =
## XXX: this should be /ip4..., / stripped? ## XXX: this should be /ip4..., / stripped?
info "Listening on", full = listenStr info "Listening on", full = listenStr
if not node.wakuRelay.isNil:
await node.wakuRelay.start()
node.started = true
proc stop*(node: WakuNode) {.async.} = proc stop*(node: WakuNode) {.async.} =
if not node.wakuRelay.isNil: if not node.wakuRelay.isNil:
await node.wakuRelay.stop() await node.wakuRelay.stop()
await node.switch.stop() await node.switch.stop()
node.started = false
proc subscribe*(node: WakuNode, topic: Topic, handler: TopicHandler) = proc subscribe*(node: WakuNode, topic: Topic, handler: TopicHandler) =
## Subscribes to a PubSub topic. Triggers handler when receiving messages on ## Subscribes to a PubSub topic. Triggers handler when receiving messages on
## this topic. TopicHandler is a method that takes a topic and some data. ## this topic. TopicHandler is a method that takes a topic and some data.
@ -416,6 +424,13 @@ proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string](), rlnRela
addRLNRelayValidator(node, defaultTopic) addRLNRelayValidator(node, defaultTopic)
info "WakuRLNRelay is mounted successfully" 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 ## Helpers
proc dialPeer*(n: WakuNode, address: string) {.async.} = proc dialPeer*(n: WakuNode, address: string) {.async.} =