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

fix(rln-relay): persist metadata every batch during initial sync #2649

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 4 additions & 6 deletions tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ suite "Onchain group manager":
let merkleRootAfter = manager.rlnInstance.getMerkleRoot().valueOr:
raiseAssert $error

let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
raiseAssert $error
check:
metadataOpt.get().validRoots == manager.validRoots.toSeq()
merkleRootBefore != merkleRootAfter
await manager.stop()

Expand Down Expand Up @@ -481,13 +484,8 @@ suite "Onchain group manager":
except Exception, CatchableError:
assert false, "exception raised: " & getCurrentExceptionMsg()

await fut
check await fut.withTimeout(5.seconds)
rymnc marked this conversation as resolved.
Show resolved Hide resolved

let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
raiseAssert $error
assert metadataOpt.isSome(), "metadata is not set"
check:
metadataOpt.get().validRoots == manager.validRoots.toSeq()
await manager.stop()

asyncTest "withdraw: should guard against uninitialized state":
Expand Down
33 changes: 18 additions & 15 deletions waku/waku_rln_relay/group_manager/on_chain/group_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,18 @@ template retryWrapper(
retryWrapper(res, RetryStrategy.new(), errStr, g.onFatalErrorAction):
body

proc setMetadata*(g: OnchainGroupManager): RlnRelayResult[void] =
proc setMetadata*(
g: OnchainGroupManager, lastProcessedBlock = none(BlockNumber)
): GroupManagerResult[void] =
let normalizedBlock =
if lastProcessedBlock.isSome():
lastProcessedBlock.get()
else:
g.latestProcessedBlock
try:
let metadataSetRes = g.rlnInstance.setMetadata(
RlnMetadata(
lastProcessedBlock: g.latestProcessedBlock,
lastProcessedBlock: normalizedBlock,
chainId: uint64(g.chainId.get()),
contractAddress: g.ethContractAddress,
validRoots: g.validRoots.toSeq(),
Expand Down Expand Up @@ -183,9 +190,6 @@ when defined(rln_v2):
await g.registerCb.get()(membersSeq)

g.validRootBuffer = g.slideRootQueue()
let setMetadataRes = g.setMetadata()
if setMetadataRes.isErr():
error "failed to persist rln metadata", error = setMetadataRes.error

else:
method atomicBatch*(
Expand Down Expand Up @@ -214,9 +218,6 @@ else:
await g.registerCb.get()(membersSeq)

g.validRootBuffer = g.slideRootQueue()
let setMetadataRes = g.setMetadata()
if setMetadataRes.isErr():
error "failed to persist rln metadata", error = setMetadataRes.error

when defined(rln_v2):
method register*(
Expand Down Expand Up @@ -550,13 +551,6 @@ proc getAndHandleEvents(
raise newException(ValueError, "failed to handle events")

g.latestProcessedBlock = toBlock
let metadataSetRes = g.setMetadata()
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:
trace "rln metadata persisted", blockNumber = g.latestProcessedBlock

return true

proc runInInterval(g: OnchainGroupManager, cb: proc, interval: Duration) =
Expand Down Expand Up @@ -591,6 +585,13 @@ proc getNewBlockCallback(g: OnchainGroupManager): proc =
var handleBlockRes: bool
g.retryWrapper(handleBlockRes, "Failed to handle new block"):
await g.getAndHandleEvents(fromBlock, latestBlock)

# cannot use isOkOr here because results in a compile-time error that
# shows the error is void for some reason
let setMetadataRes = g.setMetadata()
if setMetadataRes.isErr():
error "failed to persist rln metadata", error = setMetadataRes.error
rymnc marked this conversation as resolved.
Show resolved Hide resolved

return handleBlockRes

return wrappedCb
Expand Down Expand Up @@ -664,6 +665,8 @@ proc startOnchainSync(
futs.add(g.getAndHandleEvents(fromBlock, toBlock))
if futs.len >= maxFutures or toBlock == currentLatestBlock:
await g.batchAwaitBlockHandlingFuture(futs)
g.setMetadata(lastProcessedBlock = some(toBlock)).isOkOr:
error "failed to persist rln metadata", error = $error
futs = newSeq[Future[bool]]()
fromBlock = toBlock + 1
except CatchableError:
Expand Down
Loading