mirror of https://github.com/waku-org/nwaku.git
fix(rln-relay): reduce sync time (#2577)
* fix(rln-relay): reduce sync time * fix: add batch handling of futures to prevent over utilization of cpu * fix: need to handle the futures on last iteration when it isnt full
This commit is contained in:
parent
d257855353
commit
480a62facd
|
@ -597,6 +597,19 @@ proc startListeningToEvents(
|
||||||
let newBlockCallback = g.getNewBlockCallback()
|
let newBlockCallback = g.getNewBlockCallback()
|
||||||
g.runInInterval(newBlockCallback, DefaultBlockPollRate)
|
g.runInInterval(newBlockCallback, DefaultBlockPollRate)
|
||||||
|
|
||||||
|
proc batchAwaitBlockHandlingFuture(
|
||||||
|
g: OnchainGroupManager, futs: seq[Future[bool]]
|
||||||
|
): Future[void] {.async: (raises: [Exception]).} =
|
||||||
|
for fut in futs:
|
||||||
|
try:
|
||||||
|
var handleBlockRes: bool
|
||||||
|
g.retryWrapper(handleBlockRes, "Failed to handle block"):
|
||||||
|
await fut
|
||||||
|
except CatchableError:
|
||||||
|
raise newException(
|
||||||
|
CatchableError, "could not fetch events from block: " & getCurrentExceptionMsg()
|
||||||
|
)
|
||||||
|
|
||||||
proc startOnchainSync(
|
proc startOnchainSync(
|
||||||
g: OnchainGroupManager
|
g: OnchainGroupManager
|
||||||
): Future[void] {.async: (raises: [Exception]).} =
|
): Future[void] {.async: (raises: [Exception]).} =
|
||||||
|
@ -606,6 +619,10 @@ proc startOnchainSync(
|
||||||
|
|
||||||
# static block chunk size
|
# static block chunk size
|
||||||
let blockChunkSize = 2_000
|
let blockChunkSize = 2_000
|
||||||
|
# delay between rpc calls to not overload the rate limit
|
||||||
|
let rpcDelay = 200.milliseconds
|
||||||
|
# max number of futures to run concurrently
|
||||||
|
let maxFutures = 10
|
||||||
|
|
||||||
var fromBlock =
|
var fromBlock =
|
||||||
if g.latestProcessedBlock > g.rlnContractDeployedBlockNumber:
|
if g.latestProcessedBlock > g.rlnContractDeployedBlockNumber:
|
||||||
|
@ -616,25 +633,36 @@ proc startOnchainSync(
|
||||||
blockNumber = g.rlnContractDeployedBlockNumber
|
blockNumber = g.rlnContractDeployedBlockNumber
|
||||||
g.rlnContractDeployedBlockNumber
|
g.rlnContractDeployedBlockNumber
|
||||||
|
|
||||||
|
var futs = newSeq[Future[bool]]()
|
||||||
|
var currentLatestBlock: BlockNumber
|
||||||
|
g.retryWrapper(currentLatestBlock, "Failed to get the latest block number"):
|
||||||
|
cast[BlockNumber](await ethRpc.provider.eth_blockNumber())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# we always want to sync from last processed block => latest
|
# we always want to sync from last processed block => latest
|
||||||
# chunk events
|
# chunk events
|
||||||
while true:
|
while true:
|
||||||
var currentLatestBlock: BlockNumber
|
# if the fromBlock is less than 2k blocks behind the current block
|
||||||
g.retryWrapper(currentLatestBlock, "Failed to get the latest block number"):
|
# then fetch the new toBlock
|
||||||
cast[BlockNumber](await ethRpc.provider.eth_blockNumber())
|
|
||||||
if fromBlock >= currentLatestBlock:
|
if fromBlock >= currentLatestBlock:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if fromBlock + blockChunkSize.uint > currentLatestBlock.uint:
|
||||||
|
g.retryWrapper(currentLatestBlock, "Failed to get the latest block number"):
|
||||||
|
cast[BlockNumber](await ethRpc.provider.eth_blockNumber())
|
||||||
|
|
||||||
|
|
||||||
let toBlock = min(fromBlock + BlockNumber(blockChunkSize), currentLatestBlock)
|
let toBlock = min(fromBlock + BlockNumber(blockChunkSize), currentLatestBlock)
|
||||||
debug "fetching events", fromBlock = fromBlock, toBlock = toBlock
|
debug "fetching events", fromBlock = fromBlock, toBlock = toBlock
|
||||||
var handleBlockRes: bool
|
await sleepAsync(rpcDelay)
|
||||||
g.retryWrapper(handleBlockRes, "Failed to handle old blocks"):
|
futs.add(g.getAndHandleEvents(fromBlock, toBlock))
|
||||||
await g.getAndHandleEvents(fromBlock, toBlock)
|
if futs.len >= maxFutures or toBlock == currentLatestBlock:
|
||||||
|
await g.batchAwaitBlockHandlingFuture(futs)
|
||||||
|
futs = newSeq[Future[bool]]()
|
||||||
fromBlock = toBlock + 1
|
fromBlock = toBlock + 1
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
raise newException(
|
raise newException(
|
||||||
ValueError,
|
CatchableError,
|
||||||
"failed to get the history/reconcile missed blocks: " & getCurrentExceptionMsg(),
|
"failed to get the history/reconcile missed blocks: " & getCurrentExceptionMsg(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue