Skip to content

Commit

Permalink
feat(rln-relay): resume onchain sync from persisted tree db (#1805)
Browse files Browse the repository at this point in the history
* feat(rln-relay): resume onchain sync from persisted tree db

* chore(rln-relay): bump zerokit
  • Loading branch information
rymnc committed Jun 19, 2023
1 parent f8e270f commit bbded9e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ proc setupProtocols(node: WakuNode, conf: WakuNodeConf,
rlnRelayEthAccountPrivateKey: conf.rlnRelayEthAccountPrivateKey,
rlnRelayEthAccountAddress: conf.rlnRelayEthAccountAddress,
rlnRelayCredPath: conf.rlnRelayCredPath,
rlnRelayCredentialsPassword: conf.rlnRelayCredentialsPassword
rlnRelayCredentialsPassword: conf.rlnRelayCredentialsPassword,
rlnRelayTreePath: conf.rlnRelayTreePath
)

try:
Expand Down
2 changes: 1 addition & 1 deletion vendor/zerokit
29 changes: 26 additions & 3 deletions waku/v2/waku_rln_relay/group_manager/on_chain/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ proc handleEvents(g: OnchainGroupManager,
raise newException(ValueError, "failed to insert members into the tree")
trace "new members added to the Merkle tree", commitments=members.mapIt(it[0].idCommitment.inHex())
g.latestProcessedBlock = some(blockNumber)
let metadataSetRes = g.rlnInstance.setMetadata(RlnMetadata(
lastProcessedBlock: blockNumber))
if metadataSetRes.isErr():
# this is not a fatal error, hence we don't raise an exception
warn "failed to persist rln metadata", error=metadataSetRes.error()
else:
info "rln metadata persisted", lastProcessedBlock = blockNumber

return

Expand Down Expand Up @@ -306,11 +313,19 @@ proc startListeningToEvents(g: OnchainGroupManager): Future[void] {.async.} =
except CatchableError:
raise newException(ValueError, "failed to subscribe to block headers: " & getCurrentExceptionMsg())

proc startOnchainSync(g: OnchainGroupManager, fromBlock: BlockNumber = BlockNumber(0)): Future[void] {.async.} =
proc startOnchainSync(g: OnchainGroupManager): Future[void] {.async.} =
initializedGuard(g)

let fromBlock = if g.latestProcessedBlock.isSome():
info "resuming onchain sync from block", fromBlock = g.latestProcessedBlock.get()
g.latestProcessedBlock.get()
else:
info "starting onchain sync from scratch"
BlockNumber(0)

try:
await g.getAndHandleEvents(fromBlock, some(fromBlock))
# we always want to sync from last processed block => latest
await g.getAndHandleEvents(fromBlock, some(BlockNumber(0)))
except CatchableError:
raise newException(ValueError, "failed to get the history/reconcile missed blocks: " & getCurrentExceptionMsg())

Expand Down Expand Up @@ -445,12 +460,20 @@ method init*(g: OnchainGroupManager): Future[void] {.async.} =
g.idCredentials = some(parsedCreds[g.keystoreIndex].identityCredential)
g.membershipIndex = some(parsedCreds[g.keystoreIndex].membershipGroups[g.membershipGroupIndex].treeIndex)

let metadataGetRes = g.rlnInstance.getMetadata()
if metadataGetRes.isErr():
warn "could not initialize with persisted rln metadata"
g.latestProcessedBlock = some(BlockNumber(0))
else:
let metadata = metadataGetRes.get()
g.latestProcessedBlock = some(metadata.lastProcessedBlock)

ethRpc.ondisconnect = proc() =
error "Ethereum client disconnected"
let fromBlock = g.latestProcessedBlock.get()
info "reconnecting with the Ethereum client, and restarting group sync", fromBlock = fromBlock
try:
asyncSpawn g.startOnchainSync(fromBlock)
asyncSpawn g.startOnchainSync()
except CatchableError:
error "failed to restart group sync", error = getCurrentExceptionMsg()

Expand Down

0 comments on commit bbded9e

Please sign in to comment.