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
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(
era: EraDB,
historical_roots: openArray[Eth2Digest],
historical_summaries: openArray[HistoricalSummary],
) =
if blockNumber > 1:
importedSlot = blockNumber
# Setting the initial lower bound
importedSlot = (blockNumber - lastEra1Block) + firstSlotAfterMerge
notice "Finding slot number after resuming import", importedSlot
var parentHash: common.Hash256
let currentHash = com.db.getHeadBlockHash()
while currentHash != parentHash:
# BlockNumber based slot finding
var clNum = 0'u64
while clNum < blockNumber:
let clBlock = getBlockFromEra(
era, historical_roots, historical_summaries, Slot(importedSlot), clConfig.cfg
)
if clBlock.isSome:
let ethBlock = getEth1Block(clBlock.get())
parentHash = ethBlock.header.parentHash
).valueOr:
importedSlot += 1
continue
clNum = getEth1BlockNumber(clBlock)
# decreasing the lower bound with each iteration
importedSlot += blockNumber - clNum
importedSlot += 1
importedSlot -= 1
notice "Found the slot to start with", importedSlot
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
# 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 =
## Convert a beacon block to an eth1 block.
withBlck(blck):
@ -191,4 +201,4 @@ proc getEth1Block*(blck: ForkedTrustedSignedBeaconBlock): EthBlock =
)
return EthBlock(
header: header, transactions: txs, uncles: @[], withdrawals: ethWithdrawals
)
)