Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rln-relay): add isReady check #1989

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,36 @@ suite "Onchain group manager":
manager.validRootBuffer.len() == 0
manager.validRoots[credentialCount - 2] == expectedLastRoot

asyncTest "isReady should return false if ethRpc is none":
var manager = await setup()
await manager.init()

manager.ethRpc = none(Web3)

check:
(await manager.isReady()) == false

asyncTest "isReady should return false if lastSeenBlockHead > lastProcessed":
var manager = await setup()
await manager.init()

manager.lastSeenBlockHead = 10
manager.latestProcessedBlock = 5

check:
(await manager.isReady()) == false

asyncTest "isReady should return true if ethRpc is not syncing":
# not syncing implies the node is ready
var manager = await setup()
await manager.init()

manager.latestProcessedBlock = 10
manager.lastSeenBlockHead = 10

check:
(await manager.isReady()) == true


################################
## Terminating/removing Ganache
Expand Down
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): Future[bool] {.async.} =
when defined(rln):
if node.wakuRlnRelay == nil:
return false
return await node.wakuRlnRelay.isReady()
## TODO: add other protocol `isReady` checks
return true
3 changes: 3 additions & 0 deletions waku/waku_rln_relay/group_manager/group_manager_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,6 @@ method generateProof*(g: GroupManager,
if proofGenRes.isErr():
return err("proof generation failed: " & $proofGenRes.error())
return ok(proofGenRes.value())

method isReady*(g: GroupManager): Future[bool] {.base,gcsafe.} =
raise newException(CatchableError, "isReady proc for " & $g.type & " is not implemented yet")
35 changes: 33 additions & 2 deletions waku/waku_rln_relay/group_manager/on_chain/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type
# in event of a reorg. we store 5 in the buffer. Maybe need to revisit this,
# because the average reorg depth is 1 to 2 blocks.
validRootBuffer*: Deque[MerkleNode]
# this variable tracks the last seen head
lastSeenBlockHead*: BlockNumber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure i understand the need of this variable, and perhaps there is an edge case?

afaiu we sync in batches:

  • batch1: fromblock1 toblock1
  • batch2: fromblock2 toblock2
  • etc

And lastSeenBlockHead takes the values of toblock1, toblock2, etc?

So what happens when we just processed batch1. In this exact moment we latestProcessedBlock = lastSeenBlockHead so we may think we are in sync. But in reality we are in batch1 and batch2 is still left.

So the lastSeenBlockHead should be the last head block known by the blockchain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I'll just move it to the newHeadCallback, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 3c9d4be

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the lastSeenBlockHead should be the last head block known by the blockchain.

I think its safer to get this from eth_blockNumber endpoint rather than relying on the newHeadcallback. If the eth node fails to keep you updated with the latest head (but without disconnecting) then your lastSeenBlockHead will be outdated.

But if you use eth_blockNumber, since its req/rep, you ensure that your latest head is correct. If then node fails to reply, then its not ready.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 15bfde4


const DefaultKeyStorePath* = "rlnKeystore.json"
const DefaultKeyStorePassword* = "password"
Expand Down Expand Up @@ -313,13 +315,18 @@ proc getAndHandleEvents(g: OnchainGroupManager,
fromBlock: BlockNumber,
toBlock: Option[BlockNumber] = none(BlockNumber)): Future[void] {.async.} =
initializedGuard(g)
proc getLatestBlockNumber(): BlockNumber =
if toBlock.isSome():
return toBlock.get()
return fromBlock

let blockTable = await g.getBlockTable(fromBlock, toBlock)
if blockTable.len() > 0:
g.lastSeenBlockHead = getLatestBlockNumber()
await g.handleEvents(blockTable)
await g.handleRemovedEvents(blockTable)

g.latestProcessedBlock = if toBlock.isSome(): toBlock.get()
else: fromBlock
g.latestProcessedBlock = getLatestBlockNumber()
let metadataSetRes = g.setMetadata()
if metadataSetRes.isErr():
# this is not a fatal error, hence we don't raise an exception
Expand Down Expand Up @@ -528,3 +535,27 @@ method stop*(g: OnchainGroupManager): Future[void] {.async.} =
error "failed to flush to the tree db"

g.initialized = false

proc isSyncing*(g: OnchainGroupManager): Future[bool] {.async,gcsafe.} =
let ethRpc = g.ethRpc.get()

try:
let syncing = await ethRpc.provider.eth_syncing()
return syncing.getBool()
except CatchableError:
error "failed to get the syncing status", error = getCurrentExceptionMsg()
return false

method isReady*(g: OnchainGroupManager): Future[bool] {.async,gcsafe.} =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps its cleaner to run the checks for true all together?

if g.ethRpc.isSome() && g.lastSeenBlockHead !=0 && g.latestProcessedBlock >= g.lastSeenBlockHead && !await g.isSyncing():
  return true
return false

maybe just a personal prefernce, just a sugerence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I did think of this, imo the way it is right now is slightly more readable

initializedGuard(g)

if g.ethRpc.isNone():
return false

if g.lastSeenBlockHead == 0:
return false

if g.latestProcessedBlock < g.lastSeenBlockHead:
return false

return not (await g.isSyncing())
6 changes: 6 additions & 0 deletions waku/waku_rln_relay/group_manager/static/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ method stop*(g: StaticGroupManager): Future[void] =
var retFut = newFuture[void]("StaticGroupManager.stop")
retFut.complete()
return retFut

method isReady*(g: StaticGroupManager): Future[bool] {.gcsafe.} =
initializedGuard(g)
var retFut = newFuture[bool]("StaticGroupManager.isReady")
retFut.complete(true)
return retFut
12 changes: 12 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,18 @@ proc mount(conf: WakuRlnConfig,
return WakuRLNRelay(groupManager: groupManager,
messageBucket: messageBucket)

proc isReady*(rlnPeer: WakuRLNRelay): Future[bool] {.async.} =
## 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
try:
return await rlnPeer.groupManager.isReady()
except CatchableError:
error "could not check if the rln-relay protocol is ready", err = getCurrentExceptionMsg()
return false

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