Skip to content

Commit

Permalink
fix(rln-relay): chunk event fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jun 28, 2023
1 parent add294a commit c032765
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 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 @@ -62,6 +62,8 @@ type
const DefaultKeyStorePath* = "rlnKeystore.json"
const DefaultKeyStorePassword* = "password"

const BlockChunkSize* = 100'u64

template initializedGuard(g: OnchainGroupManager): untyped =
if not g.initialized:
raise newException(ValueError, "OnchainGroupManager is not initialized")
Expand Down Expand Up @@ -316,16 +318,28 @@ proc startListeningToEvents(g: OnchainGroupManager): Future[void] {.async.} =
proc startOnchainSync(g: OnchainGroupManager): Future[void] {.async.} =
initializedGuard(g)

let fromBlock = if g.latestProcessedBlock.isSome():
let latestBlock = cast[BlockNumber](await g.ethRpc.get().provider.eth_blockNumber())

var 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"
# chunk size is 1000 blocks
BlockNumber(0)

try:
# we always want to sync from last processed block => latest
await g.getAndHandleEvents(fromBlock, some(BlockNumber(0)))
if fromBlock == BlockNumber(0) or
fromBlock + BlockNumber(BlockChunkSize) < latestBlock:
# chunk events
for i in 0..(latestBlock - fromBlock) div BlockChunkSize:
let toBlock = fromBlock + BlockNumber(BlockChunkSize)
info "chunking events", fromBlock = fromBlock, toBlock = toBlock
await g.getAndHandleEvents(fromBlock, some(toBlock))
fromBlock = toBlock
else:
await g.getAndHandleEvents(fromBlock, some(BlockNumber(0)))
except CatchableError:
raise newException(ValueError, "failed to get the history/reconcile missed blocks: " & getCurrentExceptionMsg())

Expand Down

0 comments on commit c032765

Please sign in to comment.