Skip to content

Commit

Permalink
chore(rln-relay): add isReady check
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 5, 2023
1 parent 97a7c9d commit b7c71b9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions waku/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ proc lightpushPublish*(node: WakuNode, pubsubTopic: Option[PubsubTopic], message
when defined(rln):
proc mountRlnRelay*(node: WakuNode,
rlnConf: WakuRlnConfig,
spamHandler: Option[SpamHandler] = none(SpamHandler),
registrationHandler: Option[RegistrationHandler] = none(RegistrationHandler)) {.async.} =
spamHandler = none(SpamHandler),
registrationHandler = none(RegistrationHandler)) {.async.} =
info "mounting rln relay"

if node.wakuRelay.isNil():
Expand Down Expand Up @@ -903,3 +903,11 @@ proc stop*(node: WakuNode) {.async.} =
await node.wakuRlnRelay.stop()

node.started = false

proc isReady*(node: WakuNode): bool =
when defined(rln):
if node.wakuRlnRelay == nil:
return false
return node.wakuRlnRelay.isReady()
## TODO: add other protocol `isReady` checks
return true
1 change: 1 addition & 0 deletions waku/waku_rln_relay/group_manager/group_manager_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type
initialized*: bool
latestIndex*: MembershipIndex
validRoots*: Deque[MerkleNode]
isReady*: bool

# This proc is used to initialize the group manager
# Any initialization logic should be implemented here
Expand Down
4 changes: 4 additions & 0 deletions waku/waku_rln_relay/group_manager/on_chain/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ proc startOnchainSync(g: OnchainGroupManager): Future[void] {.async.} =
# listen to blockheaders and contract events
try:
await g.startListeningToEvents()
# rln is ready now
g.isReady = true
except CatchableError:
raise newException(ValueError, "failed to start listening to events: " & getCurrentExceptionMsg())

Expand Down Expand Up @@ -502,6 +504,7 @@ method init*(g: OnchainGroupManager): Future[void] {.async.} =
g.rlnContractDeployedBlockNumber = cast[BlockNumber](deployedBlockNumber)

ethRpc.ondisconnect = proc() =
g.isReady = false
error "Ethereum client disconnected"
let fromBlock = max(g.latestProcessedBlock, g.rlnContractDeployedBlockNumber)
info "reconnecting with the Ethereum client, and restarting group sync", fromBlock = fromBlock
Expand All @@ -528,3 +531,4 @@ method stop*(g: OnchainGroupManager): Future[void] {.async.} =
error "failed to flush to the tree db"

g.initialized = false
g.isReady = false
1 change: 1 addition & 0 deletions waku/waku_rln_relay/group_manager/static/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ method init*(g: StaticGroupManager): Future[void] {.async,gcsafe.} =
g.latestIndex += MembershipIndex(idCommitments.len() - 1)

g.initialized = true
g.isReady = true

return

Expand Down
8 changes: 8 additions & 0 deletions waku/waku_rln_relay/rln_relay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ proc mount(conf: WakuRlnConfig,
return WakuRLNRelay(groupManager: groupManager,
messageBucket: messageBucket)

proc isReady*(rlnPeer: WakuRLNRelay): bool =
## returns true if the rln-relay protocol is ready to relay messages
## returns false otherwise

# could be nil during startup
if rlnPeer.groupManager == nil:
return false
return rlnPeer.groupManager.isReady

proc new*(T: type WakuRlnRelay,
conf: WakuRlnConfig,
Expand Down

0 comments on commit b7c71b9

Please sign in to comment.