mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-27 23:27:27 +00:00
fix(rln-relay): persist metadata every batch during initial sync (#2649)
* fix(rln-relay): persist metadata every batch during initial sync * fix: test * Apply suggestions from code review Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> * patch: isOkOr template --------- Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
This commit is contained in:
parent
4a110f65ff
commit
a9e19efd7a
@ -328,7 +328,10 @@ suite "Onchain group manager":
|
|||||||
let merkleRootAfter = manager.rlnInstance.getMerkleRoot().valueOr:
|
let merkleRootAfter = manager.rlnInstance.getMerkleRoot().valueOr:
|
||||||
raiseAssert $error
|
raiseAssert $error
|
||||||
|
|
||||||
|
let metadataOpt = manager.rlnInstance.getMetadata().valueOr:
|
||||||
|
raiseAssert $error
|
||||||
check:
|
check:
|
||||||
|
metadataOpt.get().validRoots == manager.validRoots.toSeq()
|
||||||
merkleRootBefore != merkleRootAfter
|
merkleRootBefore != merkleRootAfter
|
||||||
await manager.stop()
|
await manager.stop()
|
||||||
|
|
||||||
@ -481,13 +484,8 @@ suite "Onchain group manager":
|
|||||||
except Exception, CatchableError:
|
except Exception, CatchableError:
|
||||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||||
|
|
||||||
await fut
|
check await fut.withTimeout(5.seconds)
|
||||||
|
|
||||||
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()
|
await manager.stop()
|
||||||
|
|
||||||
asyncTest "withdraw: should guard against uninitialized state":
|
asyncTest "withdraw: should guard against uninitialized state":
|
||||||
|
@ -134,11 +134,18 @@ template retryWrapper(
|
|||||||
retryWrapper(res, RetryStrategy.new(), errStr, g.onFatalErrorAction):
|
retryWrapper(res, RetryStrategy.new(), errStr, g.onFatalErrorAction):
|
||||||
body
|
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:
|
try:
|
||||||
let metadataSetRes = g.rlnInstance.setMetadata(
|
let metadataSetRes = g.rlnInstance.setMetadata(
|
||||||
RlnMetadata(
|
RlnMetadata(
|
||||||
lastProcessedBlock: g.latestProcessedBlock,
|
lastProcessedBlock: normalizedBlock,
|
||||||
chainId: uint64(g.chainId.get()),
|
chainId: uint64(g.chainId.get()),
|
||||||
contractAddress: g.ethContractAddress,
|
contractAddress: g.ethContractAddress,
|
||||||
validRoots: g.validRoots.toSeq(),
|
validRoots: g.validRoots.toSeq(),
|
||||||
@ -183,9 +190,6 @@ when defined(rln_v2):
|
|||||||
await g.registerCb.get()(membersSeq)
|
await g.registerCb.get()(membersSeq)
|
||||||
|
|
||||||
g.validRootBuffer = g.slideRootQueue()
|
g.validRootBuffer = g.slideRootQueue()
|
||||||
let setMetadataRes = g.setMetadata()
|
|
||||||
if setMetadataRes.isErr():
|
|
||||||
error "failed to persist rln metadata", error = setMetadataRes.error
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
method atomicBatch*(
|
method atomicBatch*(
|
||||||
@ -214,9 +218,6 @@ else:
|
|||||||
await g.registerCb.get()(membersSeq)
|
await g.registerCb.get()(membersSeq)
|
||||||
|
|
||||||
g.validRootBuffer = g.slideRootQueue()
|
g.validRootBuffer = g.slideRootQueue()
|
||||||
let setMetadataRes = g.setMetadata()
|
|
||||||
if setMetadataRes.isErr():
|
|
||||||
error "failed to persist rln metadata", error = setMetadataRes.error
|
|
||||||
|
|
||||||
when defined(rln_v2):
|
when defined(rln_v2):
|
||||||
method register*(
|
method register*(
|
||||||
@ -550,13 +551,6 @@ proc getAndHandleEvents(
|
|||||||
raise newException(ValueError, "failed to handle events")
|
raise newException(ValueError, "failed to handle events")
|
||||||
|
|
||||||
g.latestProcessedBlock = toBlock
|
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
|
return true
|
||||||
|
|
||||||
proc runInInterval(g: OnchainGroupManager, cb: proc, interval: Duration) =
|
proc runInInterval(g: OnchainGroupManager, cb: proc, interval: Duration) =
|
||||||
@ -591,6 +585,13 @@ proc getNewBlockCallback(g: OnchainGroupManager): proc =
|
|||||||
var handleBlockRes: bool
|
var handleBlockRes: bool
|
||||||
g.retryWrapper(handleBlockRes, "Failed to handle new block"):
|
g.retryWrapper(handleBlockRes, "Failed to handle new block"):
|
||||||
await g.getAndHandleEvents(fromBlock, latestBlock)
|
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
|
||||||
|
|
||||||
return handleBlockRes
|
return handleBlockRes
|
||||||
|
|
||||||
return wrappedCb
|
return wrappedCb
|
||||||
@ -664,6 +665,8 @@ proc startOnchainSync(
|
|||||||
futs.add(g.getAndHandleEvents(fromBlock, toBlock))
|
futs.add(g.getAndHandleEvents(fromBlock, toBlock))
|
||||||
if futs.len >= maxFutures or toBlock == currentLatestBlock:
|
if futs.len >= maxFutures or toBlock == currentLatestBlock:
|
||||||
await g.batchAwaitBlockHandlingFuture(futs)
|
await g.batchAwaitBlockHandlingFuture(futs)
|
||||||
|
g.setMetadata(lastProcessedBlock = some(toBlock)).isOkOr:
|
||||||
|
error "failed to persist rln metadata", error = $error
|
||||||
futs = newSeq[Future[bool]]()
|
futs = newSeq[Future[bool]]()
|
||||||
fromBlock = toBlock + 1
|
fromBlock = toBlock + 1
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user