Light verification and storage mode for import (#2367)

When performing block import, we can batch state root verifications and
header checks, doing them only once per chunk of blocks, assuming that
the other blocks in the batch are valid by extension.

When we're not generating receipts, we can also skip per-transaction
state root computation pre-byzantium, which is what provides a ~20%
speedup in this PR, at least on those early blocks :)

We also stop storing transactions, receipts and uncles redundantly when
importing from era1 - there is no need to waste database storage on this
when we can load it from the era1 file (eventually).
This commit is contained in:
Jacek Sieka 2024-06-15 11:22:37 +02:00 committed by GitHub
parent 1377f93d50
commit 242bbf03fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 144 additions and 61 deletions

View File

@ -516,6 +516,29 @@ type
desc: "Save performance statistics to CSV" desc: "Save performance statistics to CSV"
name: "debug-csv-stats".}: Option[string] name: "debug-csv-stats".}: Option[string]
# TODO validation and storage options should be made non-hidden when the
# UX has stabilised and era1 storage is in the app
fullValidation* {.
hidden
desc: "Enable full per-block validation (slow)"
defaultValue: false
name: "debug-full-validation".}: bool
storeBodies* {.
hidden
desc: "Store block blodies in database"
defaultValue: false
name: "debug-store-bodies".}: bool
# TODO this option should probably only cover the redundant parts, ie
# those that are in era1 files - era files presently do not store
# receipts
storeReceipts* {.
hidden
desc: "Store receipts in database"
defaultValue: false
name: "debug-store-receipts".}: bool
func parseCmdArg(T: type NetworkId, p: string): T func parseCmdArg(T: type NetworkId, p: string): T
{.gcsafe, raises: [ValueError].} = {.gcsafe, raises: [ValueError].} =
parseInt(p).T parseInt(p).T

View File

@ -29,17 +29,21 @@ when not defined(release):
export results export results
type type
PersistBlockFlag = enum PersistBlockFlag* = enum
NoFullValidation # Validate the batch instead of validating each block in it
NoPersistHeader NoPersistHeader
NoSaveTxs NoPersistTransactions
NoSaveReceipts NoPersistUncles
NoSaveWithdrawals NoPersistWithdrawals
NoPersistReceipts
PersistBlockFlags = set[PersistBlockFlag] PersistBlockFlags* = set[PersistBlockFlag]
PersistStats = tuple[blocks: int, txs: int, gas: GasInt] PersistStats = tuple[blocks: int, txs: int, gas: GasInt]
const const
NoPersistBodies* = {NoPersistTransactions, NoPersistUncles, NoPersistWithdrawals}
CleanUpEpoch = 30_000.BlockNumber CleanUpEpoch = 30_000.BlockNumber
## Regular checks for history clean up (applies to single state DB). This ## Regular checks for history clean up (applies to single state DB). This
## is mainly a debugging/testing feature so that the database can be held ## is mainly a debugging/testing feature so that the database can be held
@ -95,10 +99,30 @@ proc persistBlocksImpl(
template header(): BlockHeader = template header(): BlockHeader =
blk.header blk.header
# Full validation means validating the state root at every block and
# performing the more expensive hash computations on the block itself, ie
# verifying that the transaction and receipts roots are valid - when not
# doing full validation, we skip these expensive checks relying instead
# on the source of the data to have performed them previously or because
# the cost of failure is low.
# TODO Figure out the right balance for header fields - in particular, if
# we receive instruction from the CL while syncing that a block is
# CL-valid, do we skip validation while "far from head"? probably yes.
# This requires performing a header-chain validation from that CL-valid
# block which the current code doesn't express.
# Also, the potential avenues for corruption should be described with
# more rigor, ie if the txroot doesn't match but everything else does,
# can the state root of the last block still be correct? Dubious, but
# what would be the consequences? We would roll back the full set of
# blocks which is fairly low-cost.
let skipValidation = NoFullValidation in flags and header.number != toBlock
c.com.hardForkTransition(header) c.com.hardForkTransition(header)
if blks > 0: if blks > 0:
template parent(): BlockHeader = blocks[blks - 1].header template parent(): BlockHeader =
blocks[blks - 1].header
let updated = let updated =
if header.number == parent.number + 1 and header.parentHash == parentHash: if header.number == parent.number + 1 and header.parentHash == parentHash:
vmState.reinit(parent = parent, header = header, linear = true) vmState.reinit(parent = parent, header = header, linear = true)
@ -115,32 +139,43 @@ proc persistBlocksImpl(
# in theory it's a fresh instance that should not need it (?) # in theory it's a fresh instance that should not need it (?)
doAssert vmState.reinit(header = header) doAssert vmState.reinit(header = header)
if c.extraValidation and c.verifyFrom <= header.number: # TODO even if we're skipping validation, we should perform basic sanity
# checks on the block and header - that fields are sanely set for the
# given hard fork and similar path-independent checks - these same
# sanity checks should be performed early in the processing pipeline no
# matter their provenance.
if not skipValidation and c.extraValidation and c.verifyFrom <= header.number:
# TODO: how to checkseal from here # TODO: how to checkseal from here
?c.com.validateHeaderAndKinship(blk, checkSealOK = false) ?c.com.validateHeaderAndKinship(blk, vmState.parent, checkSealOK = false)
?vmState.processBlock(blk) # Generate receipts for storage or validation but skip them otherwise
?vmState.processBlock(
blk,
skipValidation,
skipReceipts = skipValidation and NoPersistReceipts in flags,
skipUncles = NoPersistUncles in flags,
)
# when defined(nimbusDumpDebuggingMetaData): # when defined(nimbusDumpDebuggingMetaData):
# if validationResult == ValidationResult.Error and # if validationResult == ValidationResult.Error and
# body.transactions.calcTxRoot == header.txRoot: # body.transactions.calcTxRoot == header.txRoot:
# vmState.dumpDebuggingMetaData(header, body) # vmState.dumpDebuggingMetaData(header, body)
# warn "Validation error. Debugging metadata dumped." # warn "Validation error. Debugging metadata dumped."
let blockHash = header.blockHash() let blockHash = header.blockHash()
if NoPersistHeader notin flags: if NoPersistHeader notin flags:
if not c.db.persistHeader( if not c.db.persistHeader(
blockHash, header, c.com.consensus == ConsensusType.POS, blockHash, header, c.com.consensus == ConsensusType.POS, c.com.startOfHistory
c.com.startOfHistory
): ):
return err("Could not persist header") return err("Could not persist header")
if NoSaveTxs notin flags: if NoPersistTransactions notin flags:
c.db.persistTransactions(header.number, blk.transactions) c.db.persistTransactions(header.number, blk.transactions)
if NoSaveReceipts notin flags: if NoPersistReceipts notin flags:
c.db.persistReceipts(vmState.receipts) c.db.persistReceipts(vmState.receipts)
if NoSaveWithdrawals notin flags and blk.withdrawals.isSome: if NoPersistWithdrawals notin flags and blk.withdrawals.isSome:
c.db.persistWithdrawals(blk.withdrawals.get) c.db.persistWithdrawals(blk.withdrawals.get)
# update currentBlock *after* we persist it # update currentBlock *after* we persist it
@ -176,7 +211,7 @@ proc persistBlocksImpl(
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc insertBlockWithoutSetHead*(c: ChainRef, blk: EthBlock): Result[void, string] = proc insertBlockWithoutSetHead*(c: ChainRef, blk: EthBlock): Result[void, string] =
discard ?c.persistBlocksImpl([blk], {NoPersistHeader, NoSaveReceipts}) discard ?c.persistBlocksImpl([blk], {NoPersistHeader, NoPersistReceipts})
if not c.db.persistHeader(blk.header.blockHash, blk.header, c.com.startOfHistory): if not c.db.persistHeader(blk.header.blockHash, blk.header, c.com.startOfHistory):
return err("Could not persist header") return err("Could not persist header")
@ -203,7 +238,7 @@ proc setCanonical*(c: ChainRef, header: BlockHeader): Result[void, string] =
discard discard
?c.persistBlocksImpl( ?c.persistBlocksImpl(
[EthBlock.init(header, move(body))], {NoPersistHeader, NoSaveTxs} [EthBlock.init(header, move(body))], {NoPersistHeader, NoPersistTransactions}
) )
try: try:
@ -221,14 +256,14 @@ proc setCanonical*(c: ChainRef, blockHash: Hash256): Result[void, string] =
setCanonical(c, header) setCanonical(c, header)
proc persistBlocks*( proc persistBlocks*(
c: ChainRef, blocks: openArray[EthBlock] c: ChainRef, blocks: openArray[EthBlock], flags: PersistBlockFlags = {}
): Result[PersistStats, string] = ): Result[PersistStats, string] =
# Run the VM here # Run the VM here
if blocks.len == 0: if blocks.len == 0:
debug "Nothing to do" debug "Nothing to do"
return ok(default(PersistStats)) # TODO not nice to return nil return ok(default(PersistStats)) # TODO not nice to return nil
c.persistBlocksImpl(blocks) c.persistBlocksImpl(blocks, flags)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# End # End

View File

@ -28,9 +28,12 @@ import
# Factored this out of procBlkPreamble so that it can be used directly for # Factored this out of procBlkPreamble so that it can be used directly for
# stateless execution of specific transactions. # stateless execution of specific transactions.
proc processTransactions*( proc processTransactions*(
vmState: BaseVMState, header: BlockHeader, transactions: seq[Transaction] vmState: BaseVMState,
header: BlockHeader,
transactions: seq[Transaction],
skipReceipts = false,
): Result[void, string] = ): Result[void, string] =
vmState.receipts = newSeq[Receipt](transactions.len) vmState.receipts.setLen(if skipReceipts: 0 else: transactions.len)
vmState.cumulativeGasUsed = 0 vmState.cumulativeGasUsed = 0
for txIndex, tx in transactions: for txIndex, tx in transactions:
@ -40,10 +43,17 @@ proc processTransactions*(
let rc = vmState.processTransaction(tx, sender, header) let rc = vmState.processTransaction(tx, sender, header)
if rc.isErr: if rc.isErr:
return err("Error processing tx with index " & $(txIndex) & ":" & rc.error) return err("Error processing tx with index " & $(txIndex) & ":" & rc.error)
vmState.receipts[txIndex] = vmState.makeReceipt(tx.txType) if skipReceipts:
# TODO don't generate logs at all if we're not going to put them in
# receipts
discard vmState.getAndClearLogEntries()
else:
vmState.receipts[txIndex] = vmState.makeReceipt(tx.txType)
ok() ok()
proc procBlkPreamble(vmState: BaseVMState, blk: EthBlock): Result[void, string] = proc procBlkPreamble(
vmState: BaseVMState, blk: EthBlock, skipValidation, skipReceipts, skipUncles: bool
): Result[void, string] =
template header(): BlockHeader = template header(): BlockHeader =
blk.header blk.header
@ -51,8 +61,9 @@ proc procBlkPreamble(vmState: BaseVMState, blk: EthBlock): Result[void, string]
vmState.mutateStateDB: vmState.mutateStateDB:
db.applyDAOHardFork() db.applyDAOHardFork()
if blk.transactions.calcTxRoot != header.txRoot: if not skipValidation: # Expensive!
return err("Mismatched txRoot") if blk.transactions.calcTxRoot != header.txRoot:
return err("Mismatched txRoot")
if vmState.determineFork >= FkCancun: if vmState.determineFork >= FkCancun:
if header.parentBeaconBlockRoot.isNone: if header.parentBeaconBlockRoot.isNone:
@ -67,7 +78,7 @@ proc procBlkPreamble(vmState: BaseVMState, blk: EthBlock): Result[void, string]
if blk.transactions.len == 0: if blk.transactions.len == 0:
return err("Transactions missing from body") return err("Transactions missing from body")
?processTransactions(vmState, header, blk.transactions) ?processTransactions(vmState, header, blk.transactions, skipReceipts)
elif blk.transactions.len > 0: elif blk.transactions.len > 0:
return err("Transactions in block with empty txRoot") return err("Transactions in block with empty txRoot")
@ -92,15 +103,22 @@ proc procBlkPreamble(vmState: BaseVMState, blk: EthBlock): Result[void, string]
return err("gasUsed mismatch") return err("gasUsed mismatch")
if header.ommersHash != EMPTY_UNCLE_HASH: if header.ommersHash != EMPTY_UNCLE_HASH:
let h = vmState.com.db.persistUncles(blk.uncles) # TODO It's strange that we persist uncles before processing block but the
if h != header.ommersHash: # rest after...
if not skipUncles:
let h = vmState.com.db.persistUncles(blk.uncles)
if h != header.ommersHash:
return err("ommersHash mismatch")
elif not skipValidation and rlpHash(blk.uncles) != header.ommersHash:
return err("ommersHash mismatch") return err("ommersHash mismatch")
elif blk.uncles.len > 0: elif blk.uncles.len > 0:
return err("Uncles in block with empty uncle hash") return err("Uncles in block with empty uncle hash")
ok() ok()
proc procBlkEpilogue(vmState: BaseVMState, header: BlockHeader): Result[void, string] = proc procBlkEpilogue(
vmState: BaseVMState, header: BlockHeader, skipValidation: bool
): Result[void, string] =
# Reward beneficiary # Reward beneficiary
vmState.mutateStateDB: vmState.mutateStateDB:
if vmState.collectWitnessData: if vmState.collectWitnessData:
@ -108,28 +126,30 @@ proc procBlkEpilogue(vmState: BaseVMState, header: BlockHeader): Result[void, st
db.persist(clearEmptyAccount = vmState.determineFork >= FkSpurious) db.persist(clearEmptyAccount = vmState.determineFork >= FkSpurious)
let stateDB = vmState.stateDB if not skipValidation:
if header.stateRoot != stateDB.rootHash: let stateDB = vmState.stateDB
# TODO replace logging with better error if header.stateRoot != stateDB.rootHash:
debug "wrong state root in block", # TODO replace logging with better error
blockNumber = header.number, debug "wrong state root in block",
expected = header.stateRoot, blockNumber = header.number,
actual = stateDB.rootHash, expected = header.stateRoot,
arrivedFrom = vmState.com.db.getCanonicalHead().stateRoot actual = stateDB.rootHash,
return err("stateRoot mismatch") arrivedFrom = vmState.com.db.getCanonicalHead().stateRoot
return err("stateRoot mismatch")
let bloom = createBloom(vmState.receipts) let bloom = createBloom(vmState.receipts)
if header.logsBloom != bloom:
return err("bloom mismatch")
let receiptsRoot = calcReceiptsRoot(vmState.receipts) if header.logsBloom != bloom:
if header.receiptsRoot != receiptsRoot: return err("bloom mismatch")
# TODO replace logging with better error
debug "wrong receiptRoot in block", let receiptsRoot = calcReceiptsRoot(vmState.receipts)
blockNumber = header.number, if header.receiptsRoot != receiptsRoot:
actual = receiptsRoot, # TODO replace logging with better error
expected = header.receiptsRoot debug "wrong receiptRoot in block",
return err("receiptRoot mismatch") blockNumber = header.number,
actual = receiptsRoot,
expected = header.receiptsRoot
return err("receiptRoot mismatch")
ok() ok()
@ -140,19 +160,22 @@ proc procBlkEpilogue(vmState: BaseVMState, header: BlockHeader): Result[void, st
proc processBlock*( proc processBlock*(
vmState: BaseVMState, ## Parent environment of header/body block vmState: BaseVMState, ## Parent environment of header/body block
blk: EthBlock, ## Header/body block to add to the blockchain blk: EthBlock, ## Header/body block to add to the blockchain
skipValidation: bool = false,
skipReceipts: bool = false,
skipUncles: bool = false,
): Result[void, string] = ): Result[void, string] =
## Generalised function to processes `blk` for any network. ## Generalised function to processes `blk` for any network.
var dbTx = vmState.com.db.newTransaction() var dbTx = vmState.com.db.newTransaction()
defer: defer:
dbTx.dispose() dbTx.dispose()
?vmState.procBlkPreamble(blk) ?vmState.procBlkPreamble(blk, skipValidation, skipReceipts, skipUncles)
# EIP-3675: no reward for miner in POA/POS # EIP-3675: no reward for miner in POA/POS
if vmState.com.consensus == ConsensusType.POW: if vmState.com.consensus == ConsensusType.POW:
vmState.calculateReward(blk.header, blk.uncles) vmState.calculateReward(blk.header, blk.uncles)
?vmState.procBlkEpilogue(blk.header) ?vmState.procBlkEpilogue(blk.header, skipValidation)
dbTx.commit() dbTx.commit()

View File

@ -373,6 +373,7 @@ proc validateTransaction*(
proc validateHeaderAndKinship*( proc validateHeaderAndKinship*(
com: CommonRef; com: CommonRef;
blk: EthBlock; blk: EthBlock;
parent: BlockHeader;
checkSealOK: bool; checkSealOK: bool;
): Result[void, string] ): Result[void, string]
{.gcsafe, raises: [].} = {.gcsafe, raises: [].} =
@ -383,12 +384,6 @@ proc validateHeaderAndKinship*(
return err("BlockHeader.extraData larger than 32 bytes") return err("BlockHeader.extraData larger than 32 bytes")
return ok() return ok()
let chainDB = com.db
let parent = try:
chainDB.getBlockHeader(header.parentHash)
except CatchableError as err:
return err("Failed to load block header from DB")
? com.validateHeader(blk, parent, checkSealOK) ? com.validateHeader(blk, parent, checkSealOK)
if blk.uncles.len > MAX_UNCLES: if blk.uncles.len > MAX_UNCLES:

View File

@ -31,10 +31,6 @@ type
blockNumber: BlockNumber blockNumber: BlockNumber
index: uint index: uint
const
extraTraceMessages = false
## Enabled additional logging noise
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Forward declarations # Forward declarations
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -62,6 +62,12 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
start = com.db.getSavedStateBlockNumber() + 1 start = com.db.getSavedStateBlockNumber() + 1
chain = com.newChain() chain = com.newChain()
template boolFlag(flags, b): PersistBlockFlags =
if b:
flags
else:
{}
var var
imported = 0'u64 imported = 0'u64
gas = GasInt(0) gas = GasInt(0)
@ -80,6 +86,11 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
quit(QuitFailure) quit(QuitFailure)
else: else:
File(nil) File(nil)
flags =
boolFlag({PersistBlockFlag.NoFullValidation}, not conf.fullValidation) +
boolFlag(NoPersistBodies, not conf.storeBodies) +
boolFlag({PersistBlockFlag.NoPersistReceipts}, not conf.storeReceipts)
defer: defer:
if csv != nil: if csv != nil:
close(csv) close(csv)
@ -109,7 +120,7 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
template process() = template process() =
let let
time1 = Moment.now() time1 = Moment.now()
statsRes = chain.persistBlocks(blocks) statsRes = chain.persistBlocks(blocks, flags)
if statsRes.isErr(): if statsRes.isErr():
error "Failed to persist blocks", error = statsRes.error error "Failed to persist blocks", error = statsRes.error
quit(QuitFailure) quit(QuitFailure)