2021-07-14 16:13:27 +01:00
|
|
|
# Nimbus
|
2024-02-13 17:49:41 +08:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-07-14 16:13:27 +01:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-02-14 21:27:17 +01:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-14 16:13:27 +01:00
|
|
|
import
|
2024-12-18 13:21:20 +01:00
|
|
|
stew/assign2,
|
2024-05-31 09:13:56 +02:00
|
|
|
results,
|
2024-06-17 14:56:39 +07:00
|
|
|
../../evm/state,
|
|
|
|
../../evm/types,
|
2021-07-14 16:13:27 +01:00
|
|
|
../executor,
|
|
|
|
../validate,
|
|
|
|
./chain_desc,
|
|
|
|
chronicles,
|
|
|
|
stint
|
|
|
|
|
|
|
|
when not defined(release):
|
2021-07-30 15:06:51 +01:00
|
|
|
import
|
2024-06-05 20:52:04 +00:00
|
|
|
#../../tracer,
|
2022-12-06 17:35:56 +00:00
|
|
|
../../utils/utils
|
2021-07-14 16:13:27 +01:00
|
|
|
|
2024-05-31 09:13:56 +02:00
|
|
|
export results
|
|
|
|
|
2022-07-04 19:31:41 +07:00
|
|
|
type
|
2024-06-15 11:22:37 +02:00
|
|
|
PersistBlockFlag* = enum
|
2024-12-18 13:21:20 +01:00
|
|
|
NoValidation # Disable chunk state root validation
|
|
|
|
NoFullValidation # Disable per-block validation
|
2022-07-04 19:31:41 +07:00
|
|
|
NoPersistHeader
|
2024-06-15 11:22:37 +02:00
|
|
|
NoPersistTransactions
|
|
|
|
NoPersistUncles
|
|
|
|
NoPersistWithdrawals
|
|
|
|
NoPersistReceipts
|
2024-08-16 08:22:51 +02:00
|
|
|
NoPersistSlotHashes
|
2022-07-04 19:31:41 +07:00
|
|
|
|
2024-06-15 11:22:37 +02:00
|
|
|
PersistBlockFlags* = set[PersistBlockFlag]
|
2022-07-04 19:31:41 +07:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
Persister* = object
|
|
|
|
c: ChainRef
|
|
|
|
flags: PersistBlockFlags
|
2024-05-31 09:13:56 +02:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
vmState: BaseVMState
|
|
|
|
dbTx: CoreDbTxRef
|
|
|
|
stats*: PersistStats
|
2024-06-15 11:22:37 +02:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
parent: Header
|
|
|
|
|
|
|
|
PersistStats* = tuple[blocks: int, txs: int, gas: GasInt]
|
|
|
|
|
|
|
|
const NoPersistBodies* = {NoPersistTransactions, NoPersistUncles, NoPersistWithdrawals}
|
2024-04-26 13:43:52 +00:00
|
|
|
|
2021-07-14 16:13:27 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-08-16 08:22:51 +02:00
|
|
|
proc getVmState(
|
2024-12-18 13:21:20 +01:00
|
|
|
p: var Persister, header: Header, storeSlotHash = false
|
2024-08-16 08:22:51 +02:00
|
|
|
): Result[BaseVMState, string] =
|
2024-12-18 13:21:20 +01:00
|
|
|
if p.vmState == nil:
|
|
|
|
let vmState = BaseVMState()
|
|
|
|
if not vmState.init(header, p.c.com, storeSlotHash = storeSlotHash):
|
|
|
|
return err("Could not initialise VMState")
|
|
|
|
p.vmState = vmState
|
|
|
|
else:
|
|
|
|
if header.number != p.parent.number + 1:
|
|
|
|
return err("Only linear histories supported by Persister")
|
|
|
|
|
|
|
|
if not p.vmState.reinit(p.parent, header, linear = true):
|
|
|
|
return err("Could not update VMState for new block")
|
|
|
|
|
|
|
|
ok(p.vmState)
|
|
|
|
|
|
|
|
proc dispose*(p: var Persister) =
|
|
|
|
if p.dbTx != nil:
|
|
|
|
p.dbTx.dispose()
|
|
|
|
p.dbTx = nil
|
|
|
|
|
|
|
|
proc init*(T: type Persister, c: ChainRef, flags: PersistBlockFlags): T =
|
|
|
|
T(c: c, flags: flags)
|
|
|
|
|
|
|
|
proc checkpoint*(p: var Persister): Result[void, string] =
|
|
|
|
if NoValidation notin p.flags:
|
|
|
|
let stateRoot = p.c.db.ctx.getAccounts().getStateRoot().valueOr:
|
|
|
|
return err($$error)
|
|
|
|
|
|
|
|
if p.parent.stateRoot != stateRoot:
|
|
|
|
# TODO replace logging with better error
|
|
|
|
debug "wrong state root in block",
|
|
|
|
blockNumber = p.parent.number,
|
|
|
|
blockHash = p.parent.blockHash,
|
|
|
|
parentHash = p.parent.parentHash,
|
|
|
|
expected = p.parent.stateRoot,
|
|
|
|
actual = stateRoot
|
|
|
|
return err(
|
|
|
|
"stateRoot mismatch, expect: " & $p.parent.stateRoot & ", got: " & $stateRoot
|
|
|
|
)
|
2022-03-11 15:13:59 +07:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
if p.dbTx != nil:
|
|
|
|
p.dbTx.commit()
|
|
|
|
p.dbTx = nil
|
2022-07-04 19:31:41 +07:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
# Save and record the block number before the last saved block state.
|
|
|
|
p.c.db.persistent(p.parent.number).isOkOr:
|
|
|
|
return err("Failed to save state: " & $$error)
|
2021-07-14 16:13:27 +01:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
ok()
|
|
|
|
|
|
|
|
proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =
|
|
|
|
template header(): Header =
|
|
|
|
blk.header
|
|
|
|
|
|
|
|
let c = p.c
|
|
|
|
|
|
|
|
# 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 = NoValidation in p.flags
|
|
|
|
vmState = ?p.getVmState(header, storeSlotHash = NoPersistSlotHashes notin p.flags)
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
?c.com.validateHeaderAndKinship(blk, vmState.parent)
|
|
|
|
|
|
|
|
# Generate receipts for storage or validation but skip them otherwise
|
|
|
|
?vmState.processBlock(
|
|
|
|
blk,
|
|
|
|
skipValidation,
|
|
|
|
skipReceipts = skipValidation and NoPersistReceipts in p.flags,
|
|
|
|
skipUncles = NoPersistUncles in p.flags,
|
|
|
|
taskpool = c.com.taskpool,
|
|
|
|
)
|
|
|
|
|
|
|
|
if NoPersistHeader notin p.flags:
|
|
|
|
let blockHash = header.blockHash()
|
|
|
|
?c.db.persistHeaderAndSetHead(blockHash, header, c.com.startOfHistory)
|
2023-05-23 12:25:42 +07:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
if NoPersistTransactions notin p.flags:
|
|
|
|
c.db.persistTransactions(header.number, header.txRoot, blk.transactions)
|
2022-03-11 15:13:59 +07:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
if NoPersistReceipts notin p.flags:
|
|
|
|
c.db.persistReceipts(header.receiptsRoot, vmState.receipts)
|
2024-07-01 14:07:39 +02:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
if NoPersistWithdrawals notin p.flags and blk.withdrawals.isSome:
|
|
|
|
c.db.persistWithdrawals(
|
|
|
|
header.withdrawalsRoot.expect("WithdrawalsRoot should be verified before"),
|
|
|
|
blk.withdrawals.get,
|
|
|
|
)
|
2021-07-14 16:13:27 +01:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
p.stats.blocks += 1
|
|
|
|
p.stats.txs += blk.transactions.len
|
|
|
|
p.stats.gas += blk.header.gasUsed
|
2024-04-26 13:43:52 +00:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
assign(p.parent, header)
|
2024-04-26 13:43:52 +00:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
ok()
|
2024-04-19 18:37:27 +00:00
|
|
|
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 16:32:20 +02:00
|
|
|
proc persistBlocks*(
|
2024-10-16 07:04:12 +05:30
|
|
|
c: ChainRef, blocks: openArray[Block], flags: PersistBlockFlags = {}
|
2024-06-11 17:50:22 +02:00
|
|
|
): Result[PersistStats, string] =
|
2021-07-14 16:13:27 +01:00
|
|
|
# Run the VM here
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 16:32:20 +02:00
|
|
|
if blocks.len == 0:
|
2021-07-14 16:13:27 +01:00
|
|
|
debug "Nothing to do"
|
2024-05-31 09:13:56 +02:00
|
|
|
return ok(default(PersistStats)) # TODO not nice to return nil
|
2021-07-14 16:13:27 +01:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
var p = Persister.init(c, flags)
|
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
p.persistBlock(blk).isOkOr:
|
|
|
|
p.dispose()
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# update currentBlock *after* we persist it
|
|
|
|
# so the rpc return consistent result
|
|
|
|
# between eth_blockNumber and eth_syncing
|
|
|
|
c.com.syncCurrent = p.parent.number
|
|
|
|
|
|
|
|
let res = p.checkpoint()
|
|
|
|
p.dispose()
|
|
|
|
res and ok(p.stats)
|
2021-07-14 16:13:27 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|