faster slot finding in nimbus import (#2491)

* faster slot finding in nimbus import

* feat: blocknumber based slot finding

* fix: formatting

* added comments

* fix: added is_execution_block

* added comment
This commit is contained in:
Advaita Saha 2024-07-23 02:47:07 +05:30 committed by GitHub
parent 1452e7b1c0
commit 08bbb0079f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 11 deletions

View File

@ -220,26 +220,36 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
warn "Could not write csv", err = exc.msg warn "Could not write csv", err = exc.msg
blocks.setLen(0) blocks.setLen(0)
# Finds the slot number to resume the import process
# First it sets the initial lower bound to `firstSlotAfterMerge` + number of blocks after Era1
# Then it iterates over the slots to find the current slot number, along with reducing the
# search space by calculating the difference between the `blockNumber` and the `block_number` from the executionPayload
# of the slot, then adding the difference to the importedSlot. This pushes the lower bound more,
# making the search way smaller
template updateLastImportedSlot( template updateLastImportedSlot(
era: EraDB, era: EraDB,
historical_roots: openArray[Eth2Digest], historical_roots: openArray[Eth2Digest],
historical_summaries: openArray[HistoricalSummary], historical_summaries: openArray[HistoricalSummary],
) = ) =
if blockNumber > 1: if blockNumber > 1:
importedSlot = blockNumber # Setting the initial lower bound
importedSlot = (blockNumber - lastEra1Block) + firstSlotAfterMerge
notice "Finding slot number after resuming import", importedSlot notice "Finding slot number after resuming import", importedSlot
var parentHash: common.Hash256
let currentHash = com.db.getHeadBlockHash() # BlockNumber based slot finding
while currentHash != parentHash: var clNum = 0'u64
while clNum < blockNumber:
let clBlock = getBlockFromEra( let clBlock = getBlockFromEra(
era, historical_roots, historical_summaries, Slot(importedSlot), clConfig.cfg era, historical_roots, historical_summaries, Slot(importedSlot), clConfig.cfg
) ).valueOr:
if clBlock.isSome:
let ethBlock = getEth1Block(clBlock.get())
parentHash = ethBlock.header.parentHash
importedSlot += 1 importedSlot += 1
importedSlot -= 1 continue
clNum = getEth1BlockNumber(clBlock)
# decreasing the lower bound with each iteration
importedSlot += blockNumber - clNum
notice "Found the slot to start with", importedSlot notice "Found the slot to start with", importedSlot
if isDir(conf.era1Dir.string) or isDir(conf.eraDir.string): if isDir(conf.era1Dir.string) or isDir(conf.eraDir.string):

View File

@ -134,6 +134,16 @@ proc getWithdrawals*(x: seq[capella.Withdrawal]): seq[common.Withdrawal] =
) )
return withdrawals return withdrawals
# This is a helper function to get the eth1 block number
# from a beacon block in slot finding process
proc getEth1BlockNumber*(blck: ForkedTrustedSignedBeaconBlock): uint64 =
## Get the eth1 block number from a beacon block.
## This does not check for pre/post merge, despite having only
## post merge meaning
withBlck(blck):
when consensusFork >= ConsensusFork.Bellatrix:
return forkyBlck.message.body.execution_payload.block_number
proc getEth1Block*(blck: ForkedTrustedSignedBeaconBlock): EthBlock = proc getEth1Block*(blck: ForkedTrustedSignedBeaconBlock): EthBlock =
## Convert a beacon block to an eth1 block. ## Convert a beacon block to an eth1 block.
withBlck(blck): withBlck(blck):