2022-12-02 04:39:12 +00:00
|
|
|
# Nimbus
|
2024-02-20 03:07:38 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-12-02 04:39:12 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[options],
|
2022-12-02 04:39:12 +00:00
|
|
|
chronicles,
|
|
|
|
eth/trie/trie_defs,
|
2023-08-04 11:10:09 +00:00
|
|
|
../core/[pow, clique, casper],
|
2023-12-12 19:12:56 +00:00
|
|
|
../db/[core_db, ledger, storage_types],
|
2022-12-02 04:39:12 +00:00
|
|
|
../utils/[utils, ec_recover],
|
2023-08-04 11:10:09 +00:00
|
|
|
".."/[constants, errors],
|
|
|
|
"."/[chain_config, evmforks, genesis, hardforks]
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
chain_config,
|
2023-08-04 11:10:09 +00:00
|
|
|
core_db,
|
|
|
|
constants,
|
|
|
|
errors,
|
2022-12-02 04:39:12 +00:00
|
|
|
options,
|
|
|
|
evmforks,
|
|
|
|
hardforks,
|
|
|
|
genesis,
|
|
|
|
utils
|
|
|
|
|
|
|
|
type
|
|
|
|
SyncProgress = object
|
|
|
|
start : BlockNumber
|
|
|
|
current: BlockNumber
|
|
|
|
highest: BlockNumber
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
SyncReqNewHeadCB* = proc(header: BlockHeader) {.gcsafe, raises: [].}
|
2023-01-17 09:28:14 +00:00
|
|
|
## Update head for syncing
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
CommonRef* = ref object
|
|
|
|
# all purpose storage
|
2023-08-04 11:10:09 +00:00
|
|
|
db: CoreDbRef
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
# prune underlying state db?
|
|
|
|
pruneTrie: bool
|
|
|
|
|
|
|
|
# block chain config
|
|
|
|
config: ChainConfig
|
|
|
|
|
|
|
|
# cache of genesis
|
|
|
|
genesisHash: KeccakHash
|
|
|
|
genesisHeader: BlockHeader
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
# map block number and ttd and time to
|
2022-12-02 04:39:12 +00:00
|
|
|
# HardFork
|
2023-02-16 11:40:07 +00:00
|
|
|
forkTransitionTable: ForkTransitionTable
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
# Eth wire protocol need this
|
2023-10-24 10:39:19 +00:00
|
|
|
forkIdCalculator: ForkIdCalculator
|
2022-12-02 04:39:12 +00:00
|
|
|
networkId: NetworkId
|
|
|
|
|
|
|
|
# synchronizer need this
|
|
|
|
syncProgress: SyncProgress
|
|
|
|
|
|
|
|
# current hard fork, updated after calling `hardForkTransition`
|
|
|
|
currentFork: HardFork
|
|
|
|
|
|
|
|
# one of POW/POA/POS, updated after calling `hardForkTransition`
|
|
|
|
consensusType: ConsensusType
|
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
syncReqNewHead: SyncReqNewHeadCB
|
2023-05-16 13:52:44 +00:00
|
|
|
## Call back function for the sync processor. This function stages
|
|
|
|
## the arguent header to a private aerea for subsequent processing.
|
|
|
|
|
|
|
|
syncReqRelaxV2: bool
|
|
|
|
## Allow processing of certain RPC/V2 messages type while syncing (i.e.
|
|
|
|
## `syncReqNewHead` is set.) although `shanghaiTime` is unavailable
|
|
|
|
## or has not reached, yet.
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
startOfHistory: Hash256
|
|
|
|
## This setting is needed for resuming blockwise syncying after
|
|
|
|
## installing a snapshot pivot. The default value for this field is
|
|
|
|
## `GENESIS_PARENT_HASH` to start at the very beginning.
|
|
|
|
|
|
|
|
pow: PowRef
|
2022-12-05 11:25:44 +00:00
|
|
|
## Wrapper around `hashimotoLight()` and lookup cache
|
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
poa: Clique
|
2022-12-05 11:25:44 +00:00
|
|
|
## For non-PoA networks this descriptor is ignored.
|
|
|
|
|
2022-12-06 05:55:40 +00:00
|
|
|
pos: CasperRef
|
|
|
|
## Proof Of Stake descriptor
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType: LedgerType
|
|
|
|
## Optional suggestion for the ledger cache to be used as state DB
|
|
|
|
|
|
|
|
const
|
2024-02-23 09:17:24 +00:00
|
|
|
CommonLegacyDbLedgerTypeDefault = LegacyAccountsCache
|
|
|
|
## Default ledger type to use, see `ldgType` above. This default will be
|
|
|
|
## superseded by `LedgerCache` as default for `Aristo` type deb backend.
|
2023-12-12 19:12:56 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-12-05 11:25:44 +00:00
|
|
|
# Forward declarations
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc hardForkTransition*(
|
|
|
|
com: CommonRef, forkDeterminer: ForkDeterminationInfo)
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].}
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-10-18 02:16:11 +00:00
|
|
|
func cliquePeriod*(com: CommonRef): EthTime
|
2022-12-05 11:25:44 +00:00
|
|
|
|
|
|
|
func cliqueEpoch*(com: CommonRef): int
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helper functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc consensusTransition(com: CommonRef, fork: HardFork) =
|
|
|
|
if fork >= MergeFork:
|
|
|
|
com.consensusType = ConsensusType.POS
|
|
|
|
else:
|
|
|
|
# restore consensus type to original config
|
|
|
|
# this could happen during reorg
|
|
|
|
com.consensusType = com.config.consensusType
|
|
|
|
|
2023-10-24 10:39:19 +00:00
|
|
|
proc setForkId(com: CommonRef, genesis: BlockHeader) =
|
|
|
|
com.genesisHash = genesis.blockHash
|
2022-12-02 04:39:12 +00:00
|
|
|
let genesisCRC = crc32(0, com.genesisHash.data)
|
2023-10-24 10:39:19 +00:00
|
|
|
com.forkIdCalculator = initForkIdCalculator(
|
|
|
|
com.forkTransitionTable,
|
|
|
|
genesisCRC,
|
|
|
|
genesis.timestamp.uint64)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
proc daoCheck(conf: ChainConfig) =
|
|
|
|
if not conf.daoForkSupport or conf.daoForkBlock.isNone:
|
|
|
|
conf.daoForkBlock = conf.homesteadBlock
|
|
|
|
|
|
|
|
if conf.daoForkSupport and conf.daoForkBlock.isNone:
|
|
|
|
conf.daoForkBlock = conf.homesteadBlock
|
|
|
|
|
|
|
|
proc init(com : CommonRef,
|
2023-08-04 11:10:09 +00:00
|
|
|
db : CoreDbRef,
|
2022-12-02 04:39:12 +00:00
|
|
|
pruneTrie: bool,
|
|
|
|
networkId: NetworkId,
|
|
|
|
config : ChainConfig,
|
Optional accounts cache module for creating genesis (#1897)
* Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim`
why:
Apart from testing, applications use `ReadOnlyStateDB` as an easy
way to access the accounts ledger. This is well supported by the
`Aristo` db, but writable mode is only parially supported.
The writable AccountStateDB` object for modifying accounts is not
used by production code.
So, for lecgacy and testing apps, the full support of the previous
`AccountStateDB` is now enabled by `import db/state_db/read_write`
and the `import db/state_db` provides read-only mode.
* Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation
why:
`AccountStateDB` has poor support for `Aristo` and is not widely used
in favour of `AccountsLedger` (which will be abstracted as `ledger`.)
Currently, using other than the `AccountStateDB` ledgers within the
`GenesisLedgerRef` wrapper is experimental and test only. Eventually,
the wrapper should disappear so that the `Ledger` object (which
encapsulates `AccountsCache` and `AccountsLedger`) will prevail.
* For the `Ledger`, provide access to raw accounts `MPT`
why:
This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is
the legacy version of CoreDxMptRef`.) For the new `ledger` API, the
accounts are based on the `CoreDxMAccRef` descriptor which uses a
particular sub-system for accounts while legacy applications use the
`CoreDbPhkRef` equivalent of the `SecureHexaryTrie`.
The only place where this feature will currently be used is the
`genesis.nim` source file.
* Fix `Aristo` bugs, missing boundary checks, typos, etc.
* Verify root vertex in `MPT` and account constructors
why:
Was missing so far, in particular the accounts constructor must
verify `VertexID(1)
* Fix include file
2023-11-20 11:51:43 +00:00
|
|
|
genesis : Genesis,
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType : LedgerType,
|
Optional accounts cache module for creating genesis (#1897)
* Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim`
why:
Apart from testing, applications use `ReadOnlyStateDB` as an easy
way to access the accounts ledger. This is well supported by the
`Aristo` db, but writable mode is only parially supported.
The writable AccountStateDB` object for modifying accounts is not
used by production code.
So, for lecgacy and testing apps, the full support of the previous
`AccountStateDB` is now enabled by `import db/state_db/read_write`
and the `import db/state_db` provides read-only mode.
* Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation
why:
`AccountStateDB` has poor support for `Aristo` and is not widely used
in favour of `AccountsLedger` (which will be abstracted as `ledger`.)
Currently, using other than the `AccountStateDB` ledgers within the
`GenesisLedgerRef` wrapper is experimental and test only. Eventually,
the wrapper should disappear so that the `Ledger` object (which
encapsulates `AccountsCache` and `AccountsLedger`) will prevail.
* For the `Ledger`, provide access to raw accounts `MPT`
why:
This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is
the legacy version of CoreDxMptRef`.) For the new `ledger` API, the
accounts are based on the `CoreDxMAccRef` descriptor which uses a
particular sub-system for accounts while legacy applications use the
`CoreDbPhkRef` equivalent of the `SecureHexaryTrie`.
The only place where this feature will currently be used is the
`genesis.nim` source file.
* Fix `Aristo` bugs, missing boundary checks, typos, etc.
* Verify root vertex in `MPT` and account constructors
why:
Was missing so far, in particular the accounts constructor must
verify `VertexID(1)
* Fix include file
2023-11-20 11:51:43 +00:00
|
|
|
) {.gcsafe, raises: [CatchableError].} =
|
2023-12-12 19:12:56 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
config.daoCheck()
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
com.db = db
|
2022-12-02 04:39:12 +00:00
|
|
|
com.pruneTrie = pruneTrie
|
|
|
|
com.config = config
|
2023-02-16 11:40:07 +00:00
|
|
|
com.forkTransitionTable = config.toForkTransitionTable()
|
2022-12-02 04:39:12 +00:00
|
|
|
com.networkId = networkId
|
|
|
|
com.syncProgress= SyncProgress()
|
2024-02-23 09:17:24 +00:00
|
|
|
com.ldgType = block:
|
|
|
|
if ldgType != LedgerType(0):
|
|
|
|
ldgType
|
|
|
|
elif db.dbType in {AristoDbMemory,AristoDbRocks,AristoDbVoid}:
|
|
|
|
# The `Aristo` backend does not work well with the `LegacyAccountsCache`
|
|
|
|
LedgerCache
|
|
|
|
else:
|
|
|
|
CommonLegacyDbLedgerTypeDefault
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-08-16 10:05:14 +00:00
|
|
|
# Initalise the PoA state regardless of whether it is needed on the current
|
|
|
|
# network. For non-PoA networks this descriptor is ignored.
|
|
|
|
com.poa = newClique(com.db, com.cliquePeriod, com.cliqueEpoch)
|
|
|
|
|
|
|
|
# Always initialise the PoW epoch cache even though it migh no be used
|
|
|
|
com.pow = PowRef.new
|
|
|
|
com.pos = CasperRef.new
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# com.currentFork and com.consensusType
|
|
|
|
# is set by hardForkTransition.
|
|
|
|
# set it before creating genesis block
|
|
|
|
# TD need to be some(0.u256) because it can be the genesis
|
|
|
|
# already at the MergeFork
|
2023-10-18 02:16:11 +00:00
|
|
|
const TimeZero = EthTime(0)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-10-24 10:39:19 +00:00
|
|
|
# com.forkIdCalculator and com.genesisHash are set
|
2022-12-02 04:39:12 +00:00
|
|
|
# by setForkId
|
|
|
|
if genesis.isNil.not:
|
2023-08-16 10:05:14 +00:00
|
|
|
com.hardForkTransition(ForkDeterminationInfo(
|
|
|
|
blockNumber: 0.toBlockNumber,
|
|
|
|
td: some(0.u256),
|
|
|
|
time: some(genesis.timestamp)
|
|
|
|
))
|
2022-12-02 04:39:12 +00:00
|
|
|
com.genesisHeader = toGenesisHeader(genesis,
|
2023-12-12 19:12:56 +00:00
|
|
|
com.currentFork, com.db, com.ldgType)
|
2022-12-02 04:39:12 +00:00
|
|
|
com.setForkId(com.genesisHeader)
|
2023-08-16 10:05:14 +00:00
|
|
|
com.pos.timestamp = genesis.timestamp
|
|
|
|
else:
|
|
|
|
com.hardForkTransition(ForkDeterminationInfo(
|
|
|
|
blockNumber: 0.toBlockNumber,
|
|
|
|
td: some(0.u256),
|
|
|
|
time: some(TimeZero)
|
|
|
|
))
|
2022-12-05 11:25:44 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
# By default, history begins at genesis.
|
|
|
|
com.startOfHistory = GENESIS_PARENT_HASH
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
proc getTd(com: CommonRef, blockHash: Hash256): Option[DifficultyInt] =
|
|
|
|
var td: DifficultyInt
|
|
|
|
if not com.db.getTd(blockHash, td):
|
|
|
|
# TODO: Is this really ok?
|
|
|
|
none[DifficultyInt]()
|
|
|
|
else:
|
|
|
|
some(td)
|
|
|
|
|
|
|
|
proc needTdForHardForkDetermination(com: CommonRef): bool =
|
|
|
|
let t = com.forkTransitionTable.mergeForkTransitionThreshold
|
2023-08-06 07:55:11 +00:00
|
|
|
t.ttdPassed.isNone and t.blockNumber.isNone and t.ttd.isSome
|
2023-02-16 11:40:07 +00:00
|
|
|
|
|
|
|
proc getTdIfNecessary(com: CommonRef, blockHash: Hash256): Option[DifficultyInt] =
|
|
|
|
if needTdForHardForkDetermination(com):
|
|
|
|
getTd(com, blockHash)
|
|
|
|
else:
|
|
|
|
none[DifficultyInt]()
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc new*(
|
|
|
|
_: type CommonRef;
|
|
|
|
db: CoreDbRef;
|
|
|
|
pruneTrie: bool = true;
|
|
|
|
networkId: NetworkId = MainNet;
|
|
|
|
params = networkParams(MainNet);
|
2024-02-23 09:17:24 +00:00
|
|
|
ldgType = LedgerType(0);
|
2023-12-12 19:12:56 +00:00
|
|
|
): CommonRef
|
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
## If genesis data is present, the forkIds will be initialized
|
|
|
|
## empty data base also initialized with genesis block
|
|
|
|
new(result)
|
|
|
|
result.init(
|
|
|
|
db,
|
|
|
|
pruneTrie,
|
|
|
|
networkId,
|
|
|
|
params.config,
|
Optional accounts cache module for creating genesis (#1897)
* Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim`
why:
Apart from testing, applications use `ReadOnlyStateDB` as an easy
way to access the accounts ledger. This is well supported by the
`Aristo` db, but writable mode is only parially supported.
The writable AccountStateDB` object for modifying accounts is not
used by production code.
So, for lecgacy and testing apps, the full support of the previous
`AccountStateDB` is now enabled by `import db/state_db/read_write`
and the `import db/state_db` provides read-only mode.
* Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation
why:
`AccountStateDB` has poor support for `Aristo` and is not widely used
in favour of `AccountsLedger` (which will be abstracted as `ledger`.)
Currently, using other than the `AccountStateDB` ledgers within the
`GenesisLedgerRef` wrapper is experimental and test only. Eventually,
the wrapper should disappear so that the `Ledger` object (which
encapsulates `AccountsCache` and `AccountsLedger`) will prevail.
* For the `Ledger`, provide access to raw accounts `MPT`
why:
This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is
the legacy version of CoreDxMptRef`.) For the new `ledger` API, the
accounts are based on the `CoreDxMAccRef` descriptor which uses a
particular sub-system for accounts while legacy applications use the
`CoreDbPhkRef` equivalent of the `SecureHexaryTrie`.
The only place where this feature will currently be used is the
`genesis.nim` source file.
* Fix `Aristo` bugs, missing boundary checks, typos, etc.
* Verify root vertex in `MPT` and account constructors
why:
Was missing so far, in particular the accounts constructor must
verify `VertexID(1)
* Fix include file
2023-11-20 11:51:43 +00:00
|
|
|
params.genesis,
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType)
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
_: type CommonRef;
|
|
|
|
db: CoreDbRef;
|
|
|
|
config: ChainConfig;
|
|
|
|
pruneTrie: bool = true;
|
|
|
|
networkId: NetworkId = MainNet;
|
2024-02-23 09:17:24 +00:00
|
|
|
ldgType = LedgerType(0);
|
2023-12-12 19:12:56 +00:00
|
|
|
): CommonRef
|
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
## There is no genesis data present
|
|
|
|
## Mainly used for testing without genesis
|
|
|
|
new(result)
|
|
|
|
result.init(
|
|
|
|
db,
|
|
|
|
pruneTrie,
|
|
|
|
networkId,
|
|
|
|
config,
|
Optional accounts cache module for creating genesis (#1897)
* Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim`
why:
Apart from testing, applications use `ReadOnlyStateDB` as an easy
way to access the accounts ledger. This is well supported by the
`Aristo` db, but writable mode is only parially supported.
The writable AccountStateDB` object for modifying accounts is not
used by production code.
So, for lecgacy and testing apps, the full support of the previous
`AccountStateDB` is now enabled by `import db/state_db/read_write`
and the `import db/state_db` provides read-only mode.
* Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation
why:
`AccountStateDB` has poor support for `Aristo` and is not widely used
in favour of `AccountsLedger` (which will be abstracted as `ledger`.)
Currently, using other than the `AccountStateDB` ledgers within the
`GenesisLedgerRef` wrapper is experimental and test only. Eventually,
the wrapper should disappear so that the `Ledger` object (which
encapsulates `AccountsCache` and `AccountsLedger`) will prevail.
* For the `Ledger`, provide access to raw accounts `MPT`
why:
This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is
the legacy version of CoreDxMptRef`.) For the new `ledger` API, the
accounts are based on the `CoreDxMAccRef` descriptor which uses a
particular sub-system for accounts while legacy applications use the
`CoreDbPhkRef` equivalent of the `SecureHexaryTrie`.
The only place where this feature will currently be used is the
`genesis.nim` source file.
* Fix `Aristo` bugs, missing boundary checks, typos, etc.
* Verify root vertex in `MPT` and account constructors
why:
Was missing so far, in particular the accounts constructor must
verify `VertexID(1)
* Fix include file
2023-11-20 11:51:43 +00:00
|
|
|
nil,
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc clone*(com: CommonRef, db: CoreDbRef): CommonRef =
|
2022-12-02 04:39:12 +00:00
|
|
|
## clone but replace the db
|
|
|
|
## used in EVM tracer whose db is CaptureDB
|
|
|
|
CommonRef(
|
2023-08-04 11:10:09 +00:00
|
|
|
db : db,
|
2022-12-02 04:39:12 +00:00
|
|
|
pruneTrie : com.pruneTrie,
|
|
|
|
config : com.config,
|
2023-02-16 11:40:07 +00:00
|
|
|
forkTransitionTable: com.forkTransitionTable,
|
2023-10-24 10:39:19 +00:00
|
|
|
forkIdCalculator: com.forkIdCalculator,
|
2022-12-02 04:39:12 +00:00
|
|
|
genesisHash : com.genesisHash,
|
|
|
|
genesisHeader: com.genesisHeader,
|
|
|
|
syncProgress : com.syncProgress,
|
|
|
|
networkId : com.networkId,
|
|
|
|
currentFork : com.currentFork,
|
2022-12-06 05:55:40 +00:00
|
|
|
consensusType: com.consensusType,
|
|
|
|
pow : com.pow,
|
|
|
|
poa : com.poa,
|
2023-12-12 19:12:56 +00:00
|
|
|
pos : com.pos,
|
|
|
|
ldgType : com.ldgType
|
2022-12-02 04:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc clone*(com: CommonRef): CommonRef =
|
2023-08-04 11:10:09 +00:00
|
|
|
com.clone(com.db)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
func toHardFork*(
|
|
|
|
com: CommonRef, forkDeterminer: ForkDeterminationInfo): HardFork =
|
2023-02-16 11:40:07 +00:00
|
|
|
toHardFork(com.forkTransitionTable, forkDeterminer)
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc hardForkTransition(
|
|
|
|
com: CommonRef, forkDeterminer: ForkDeterminationInfo)
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
## When consensus type already transitioned to POS,
|
|
|
|
## the storage can choose not to store TD anymore,
|
|
|
|
## at that time, TD is no longer needed to find a fork
|
|
|
|
## TD only needed during transition from POW/POA to POS.
|
|
|
|
## Same thing happen before London block, TD can be ignored.
|
2023-08-16 10:05:14 +00:00
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
let fork = com.toHardFork(forkDeterminer)
|
|
|
|
com.currentFork = fork
|
|
|
|
com.consensusTransition(fork)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc hardForkTransition*(
|
|
|
|
com: CommonRef,
|
|
|
|
number: BlockNumber,
|
|
|
|
td: Option[DifficultyInt],
|
|
|
|
time: Option[EthTime])
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2023-05-10 16:04:35 +00:00
|
|
|
com.hardForkTransition(ForkDeterminationInfo(
|
|
|
|
blockNumber: number, time: time, td: td))
|
|
|
|
|
|
|
|
proc hardForkTransition*(
|
|
|
|
com: CommonRef,
|
|
|
|
parentHash: Hash256,
|
|
|
|
number: BlockNumber,
|
|
|
|
time: Option[EthTime])
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2023-02-16 11:40:07 +00:00
|
|
|
com.hardForkTransition(number, getTdIfNecessary(com, parentHash), time)
|
2022-12-05 08:46:37 +00:00
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc hardForkTransition*(
|
|
|
|
com: CommonRef, header: BlockHeader)
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2023-05-10 16:04:35 +00:00
|
|
|
com.hardForkTransition(
|
|
|
|
header.parentHash, header.blockNumber, some(header.timestamp))
|
2022-12-05 08:46:37 +00:00
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
func toEVMFork*(com: CommonRef, forkDeterminer: ForkDeterminationInfo): EVMFork =
|
2022-12-02 04:39:12 +00:00
|
|
|
## similar to toFork, but produce EVMFork
|
2023-02-16 11:40:07 +00:00
|
|
|
let fork = com.toHardFork(forkDeterminer)
|
2022-12-02 04:39:12 +00:00
|
|
|
ToEVMFork[fork]
|
|
|
|
|
2023-01-09 14:39:36 +00:00
|
|
|
func toEVMFork*(com: CommonRef): EVMFork =
|
|
|
|
ToEVMFork[com.currentFork]
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
func isLondon*(com: CommonRef, number: BlockNumber): bool =
|
|
|
|
# TODO: Fixme, use only London comparator
|
2023-10-24 10:39:19 +00:00
|
|
|
com.toHardFork(number.forkDeterminationInfo) >= London
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-05-30 08:58:41 +00:00
|
|
|
func isLondon*(com: CommonRef, number: BlockNumber, timestamp: EthTime): bool =
|
|
|
|
# TODO: Fixme, use only London comparator
|
|
|
|
com.toHardFork(forkDeterminationInfo(number, timestamp)) >= London
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
func forkGTE*(com: CommonRef, fork: HardFork): bool =
|
|
|
|
com.currentFork >= fork
|
|
|
|
|
|
|
|
# TODO: move this consensus code to where it belongs
|
|
|
|
proc minerAddress*(com: CommonRef; header: BlockHeader): EthAddress
|
2023-01-31 01:32:17 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
if com.consensusType != ConsensusType.POA:
|
|
|
|
# POW and POS return header.coinbase
|
|
|
|
return header.coinbase
|
|
|
|
|
|
|
|
# POA return ecRecover
|
|
|
|
let account = header.ecRecover
|
|
|
|
if account.isErr:
|
|
|
|
let msg = "Could not recover account address: " & $account.error
|
|
|
|
raise newException(ValidationError, msg)
|
|
|
|
|
|
|
|
account.value
|
|
|
|
|
2023-10-24 10:39:19 +00:00
|
|
|
func forkId*(com: CommonRef, head, time: uint64): ForkID {.gcsafe.} =
|
2022-12-02 04:39:12 +00:00
|
|
|
## EIP 2364/2124
|
2023-10-24 10:39:19 +00:00
|
|
|
com.forkIdCalculator.newID(head, time)
|
|
|
|
|
|
|
|
func forkId*(com: CommonRef, head: BlockNumber, time: EthTime): ForkID {.gcsafe.} =
|
|
|
|
## EIP 2364/2124
|
|
|
|
com.forkIdCalculator.newID(head.truncate(uint64), time.uint64)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
func isEIP155*(com: CommonRef, number: BlockNumber): bool =
|
|
|
|
com.config.eip155Block.isSome and number >= com.config.eip155Block.get
|
|
|
|
|
|
|
|
proc isBlockAfterTtd*(com: CommonRef, header: BlockHeader): bool
|
2023-01-31 01:32:17 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
if com.config.terminalTotalDifficulty.isNone:
|
|
|
|
return false
|
|
|
|
|
|
|
|
let
|
|
|
|
ttd = com.config.terminalTotalDifficulty.get()
|
|
|
|
ptd = com.db.getScore(header.parentHash)
|
|
|
|
td = ptd + header.difficulty
|
|
|
|
ptd >= ttd and td >= ttd
|
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
func isShanghaiOrLater*(com: CommonRef, t: EthTime): bool =
|
|
|
|
com.config.shanghaiTime.isSome and t >= com.config.shanghaiTime.get
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
func isCancunOrLater*(com: CommonRef, t: EthTime): bool =
|
|
|
|
com.config.cancunTime.isSome and t >= com.config.cancunTime.get
|
|
|
|
|
2024-03-28 06:47:02 +00:00
|
|
|
func isPragueOrLater*(com: CommonRef, t: EthTime): bool =
|
|
|
|
com.config.pragueTime.isSome and t >= com.config.pragueTime.get
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType
|
2023-01-31 01:32:17 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
if com.isBlockAfterTtd(header):
|
|
|
|
return ConsensusType.POS
|
|
|
|
|
|
|
|
return com.config.consensusType
|
|
|
|
|
|
|
|
proc initializeEmptyDb*(com: CommonRef)
|
2023-02-14 20:27:17 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2023-08-04 11:10:09 +00:00
|
|
|
let kvt = com.db.kvt()
|
|
|
|
if canonicalHeadHashKey().toOpenArray notin kvt:
|
2022-12-02 04:39:12 +00:00
|
|
|
trace "Writing genesis to DB"
|
|
|
|
doAssert(com.genesisHeader.blockNumber.isZero,
|
|
|
|
"can't commit genesis block with number > 0")
|
2022-12-10 01:32:55 +00:00
|
|
|
discard com.db.persistHeaderToDb(com.genesisHeader,
|
|
|
|
com.consensusType == ConsensusType.POS)
|
2023-08-04 11:10:09 +00:00
|
|
|
doAssert(canonicalHeadHashKey().toOpenArray in kvt)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
proc syncReqNewHead*(com: CommonRef; header: BlockHeader)
|
2023-05-16 04:15:10 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2023-01-17 09:28:14 +00:00
|
|
|
## Used by RPC to update the beacon head for snap sync
|
|
|
|
if not com.syncReqNewHead.isNil:
|
2023-02-14 20:27:17 +00:00
|
|
|
com.syncReqNewHead(header)
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Getters
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-04-14 22:28:57 +00:00
|
|
|
|
|
|
|
func startOfHistory*(com: CommonRef): Hash256 =
|
|
|
|
## Getter
|
|
|
|
com.startOfHistory
|
|
|
|
|
|
|
|
func poa*(com: CommonRef): Clique =
|
2022-12-05 11:25:44 +00:00
|
|
|
## Getter
|
|
|
|
com.poa
|
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
func pow*(com: CommonRef): PowRef =
|
2022-12-05 11:25:44 +00:00
|
|
|
## Getter
|
|
|
|
com.pow
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
func pos*(com: CommonRef): CasperRef =
|
2022-12-06 05:55:40 +00:00
|
|
|
## Getter
|
|
|
|
com.pos
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
func db*(com: CommonRef): CoreDbRef =
|
2022-12-02 04:39:12 +00:00
|
|
|
com.db
|
|
|
|
|
|
|
|
func consensus*(com: CommonRef): ConsensusType =
|
|
|
|
com.consensusType
|
|
|
|
|
|
|
|
func eip150Block*(com: CommonRef): Option[BlockNumber] =
|
|
|
|
com.config.eip150Block
|
|
|
|
|
|
|
|
func eip150Hash*(com: CommonRef): Hash256 =
|
|
|
|
com.config.eip150Hash
|
|
|
|
|
|
|
|
func daoForkBlock*(com: CommonRef): Option[BlockNumber] =
|
|
|
|
com.config.daoForkBlock
|
|
|
|
|
|
|
|
func daoForkSupport*(com: CommonRef): bool =
|
|
|
|
com.config.daoForkSupport
|
|
|
|
|
|
|
|
func ttd*(com: CommonRef): Option[DifficultyInt] =
|
|
|
|
com.config.terminalTotalDifficulty
|
|
|
|
|
2023-10-19 03:29:06 +00:00
|
|
|
func ttdPassed*(com: CommonRef): bool =
|
|
|
|
com.config.terminalTotalDifficultyPassed.get(false)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# if you messing with clique period and
|
|
|
|
# and epoch, it likely will fail clique verification
|
|
|
|
# at epoch * blocknumber
|
2023-10-18 02:16:11 +00:00
|
|
|
func cliquePeriod*(com: CommonRef): EthTime =
|
2022-12-02 04:39:12 +00:00
|
|
|
if com.config.clique.period.isSome:
|
2023-10-18 02:16:11 +00:00
|
|
|
return EthTime com.config.clique.period.get()
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
func cliqueEpoch*(com: CommonRef): int =
|
|
|
|
if com.config.clique.epoch.isSome:
|
|
|
|
return com.config.clique.epoch.get()
|
|
|
|
|
|
|
|
func pruneTrie*(com: CommonRef): bool =
|
|
|
|
com.pruneTrie
|
|
|
|
|
|
|
|
# always remember ChainId and NetworkId
|
|
|
|
# are two distinct things that often got mixed
|
|
|
|
# because some client do not make distinction
|
|
|
|
# between them.
|
|
|
|
# And popular networks such as MainNet
|
2023-10-24 04:44:15 +00:00
|
|
|
# Goerli add more confusion to this
|
2022-12-02 04:39:12 +00:00
|
|
|
# by not make distinction too in their value.
|
|
|
|
func chainId*(com: CommonRef): ChainId =
|
|
|
|
com.config.chainId
|
|
|
|
|
|
|
|
func networkId*(com: CommonRef): NetworkId =
|
|
|
|
com.networkId
|
|
|
|
|
|
|
|
func blockReward*(com: CommonRef): UInt256 =
|
|
|
|
BlockRewards[com.currentFork]
|
|
|
|
|
|
|
|
func genesisHash*(com: CommonRef): Hash256 =
|
|
|
|
## Getter
|
|
|
|
com.genesisHash
|
|
|
|
|
|
|
|
func genesisHeader*(com: CommonRef): BlockHeader =
|
|
|
|
## Getter
|
|
|
|
com.genesisHeader
|
|
|
|
|
|
|
|
func syncStart*(com: CommonRef): BlockNumber =
|
|
|
|
com.syncProgress.start
|
|
|
|
|
|
|
|
func syncCurrent*(com: CommonRef): BlockNumber =
|
|
|
|
com.syncProgress.current
|
|
|
|
|
|
|
|
func syncHighest*(com: CommonRef): BlockNumber =
|
|
|
|
com.syncProgress.highest
|
|
|
|
|
2023-05-16 13:52:44 +00:00
|
|
|
func syncReqRelaxV2*(com: CommonRef): bool =
|
|
|
|
com.syncReqRelaxV2
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
func ledgerType*(com: CommonRef): LedgerType =
|
|
|
|
com.ldgType
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `syncStart=`*(com: CommonRef, number: BlockNumber) =
|
|
|
|
com.syncProgress.start = number
|
|
|
|
|
|
|
|
proc `syncCurrent=`*(com: CommonRef, number: BlockNumber) =
|
|
|
|
com.syncProgress.current = number
|
|
|
|
|
|
|
|
proc `syncHighest=`*(com: CommonRef, number: BlockNumber) =
|
|
|
|
com.syncProgress.highest = number
|
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
proc `startOfHistory=`*(com: CommonRef, val: Hash256) =
|
|
|
|
## Setter
|
|
|
|
com.startOfHistory = val
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc setTTD*(com: CommonRef, ttd: Option[DifficultyInt]) =
|
|
|
|
## useful for testing
|
|
|
|
com.config.terminalTotalDifficulty = ttd
|
2023-02-16 11:40:07 +00:00
|
|
|
# rebuild the MergeFork piece of the forkTransitionTable
|
|
|
|
com.forkTransitionTable.mergeForkTransitionThreshold = com.config.mergeForkTransitionThreshold
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2024-02-20 03:07:38 +00:00
|
|
|
proc setFork*(com: CommonRef, fork: HardFork): HardFork =
|
2022-12-02 04:39:12 +00:00
|
|
|
## useful for testing
|
|
|
|
result = com.currentFork
|
|
|
|
com.currentFork = fork
|
|
|
|
com.consensusTransition(fork)
|
2023-01-17 09:28:14 +00:00
|
|
|
|
|
|
|
proc `syncReqNewHead=`*(com: CommonRef; cb: SyncReqNewHeadCB) =
|
2023-05-16 13:52:44 +00:00
|
|
|
## Activate or reset a call back handler for syncing. When resetting (by
|
|
|
|
## passing `cb` as `nil`), the `syncReqRelaxV2` value is also reset.
|
2023-01-17 09:28:14 +00:00
|
|
|
com.syncReqNewHead = cb
|
2023-05-16 13:52:44 +00:00
|
|
|
if cb.isNil:
|
|
|
|
com.syncReqRelaxV2 = false
|
|
|
|
|
|
|
|
proc `syncReqRelaxV2=`*(com: CommonRef; val: bool) =
|
|
|
|
## Allow processing of certain RPC/V2 messages type while syncing (i.e.
|
|
|
|
## `syncReqNewHead` is set.) although `shanghaiTime` is unavailable
|
|
|
|
## or has not reached, yet.
|
|
|
|
##
|
|
|
|
## This setter is effective only while `syncReqNewHead` is activated.
|
|
|
|
if not com.syncReqNewHead.isNil:
|
|
|
|
com.syncReqRelaxV2 = val
|
2023-01-17 09:28:14 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|