2021-07-14 15:13:27 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
|
|
|
../../chain_config,
|
|
|
|
../../db/db_chain,
|
|
|
|
../../genesis,
|
|
|
|
../../utils,
|
2021-12-10 08:49:57 +00:00
|
|
|
../../utils/pow,
|
2021-09-07 15:00:21 +00:00
|
|
|
../../chain_config,
|
2021-07-14 15:13:27 +00:00
|
|
|
../clique,
|
|
|
|
../validate,
|
|
|
|
chronicles,
|
2022-10-10 02:31:28 +00:00
|
|
|
eth/[common],
|
2021-07-14 15:13:27 +00:00
|
|
|
stew/endians2,
|
|
|
|
stint
|
|
|
|
|
|
|
|
type
|
|
|
|
ChainFork* = enum
|
2021-12-10 13:12:19 +00:00
|
|
|
## `ChainFork` has extra forks not in the EVM fork list. These are the
|
|
|
|
## unique `DAOFork`, and Glacier forks `MuirGlacier` and `ArrowGlacier`.
|
|
|
|
## At the Glacier forks, only block difficulty calculation changed.
|
2021-07-14 15:13:27 +00:00
|
|
|
Frontier,
|
|
|
|
Homestead,
|
|
|
|
DAOFork,
|
|
|
|
Tangerine,
|
|
|
|
Spurious,
|
|
|
|
Byzantium,
|
|
|
|
Constantinople,
|
|
|
|
Petersburg,
|
|
|
|
Istanbul,
|
|
|
|
MuirGlacier,
|
|
|
|
Berlin,
|
2021-12-10 13:12:19 +00:00
|
|
|
London,
|
2022-03-24 08:54:11 +00:00
|
|
|
ArrowGlacier,
|
2022-10-17 10:22:01 +00:00
|
|
|
GrayGlacier,
|
|
|
|
MergeFork,
|
|
|
|
Shanghai,
|
|
|
|
Cancun
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-10-10 02:31:28 +00:00
|
|
|
Chain* = ref object of RootRef
|
2021-07-14 15:13:27 +00:00
|
|
|
db: BaseChainDB
|
|
|
|
forkIds: array[ChainFork, ForkID]
|
2022-01-18 16:19:32 +00:00
|
|
|
|
|
|
|
blockZeroHash: KeccakHash ##\
|
|
|
|
## Overload cache for `genesisHash()` method
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
blockZeroStateRoot: KeccakHash
|
|
|
|
|
2022-09-16 04:23:25 +00:00
|
|
|
validateBlock: bool ##\
|
|
|
|
## If turn off, `persistBlocks` will always return
|
|
|
|
## ValidationResult.OK and disable extraValidation too.
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
extraValidation: bool ##\
|
2021-07-30 14:06:51 +00:00
|
|
|
## Trigger extra validation, currently within `persistBlocks()`
|
|
|
|
## function only.
|
|
|
|
|
|
|
|
verifyFrom: BlockNumber ##\
|
|
|
|
## First block to when `extraValidation` will be applied (only
|
|
|
|
## effective if `extraValidation` is true.)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-12-10 08:49:57 +00:00
|
|
|
pow: PowRef ##\
|
|
|
|
## Wrapper around `hashimotoLight()` and lookup cache
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
poa: Clique ##\
|
|
|
|
## For non-PoA networks (when `db.config.poaEngine` is `false`),
|
|
|
|
## this descriptor is ignored.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func toNextFork(n: BlockNumber): uint64 =
|
|
|
|
if n == high(BlockNumber):
|
|
|
|
result = 0'u64
|
|
|
|
else:
|
|
|
|
result = n.truncate(uint64)
|
|
|
|
|
2022-03-24 08:54:11 +00:00
|
|
|
func toNextFork(n: Option[BlockNumber]): uint64 =
|
|
|
|
if n.isSome:
|
|
|
|
n.get.truncate(uint64)
|
|
|
|
else:
|
|
|
|
0'u64
|
|
|
|
|
2022-07-04 12:31:41 +00:00
|
|
|
proc isBlockAfterTtd*(c: Chain, header: BlockHeader): bool
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2022-09-26 16:14:12 +00:00
|
|
|
isBlockAfterTtd(c.db, header)
|
2022-03-21 09:15:52 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
func getNextFork(c: ChainConfig, fork: ChainFork): uint64 =
|
|
|
|
let next: array[ChainFork, uint64] = [
|
|
|
|
0'u64,
|
|
|
|
toNextFork(c.homesteadBlock),
|
|
|
|
toNextFork(c.daoForkBlock),
|
|
|
|
toNextFork(c.eip150Block),
|
|
|
|
toNextFork(c.eip158Block),
|
|
|
|
toNextFork(c.byzantiumBlock),
|
|
|
|
toNextFork(c.constantinopleBlock),
|
|
|
|
toNextFork(c.petersburgBlock),
|
|
|
|
toNextFork(c.istanbulBlock),
|
|
|
|
toNextFork(c.muirGlacierBlock),
|
|
|
|
toNextFork(c.berlinBlock),
|
2021-12-10 13:12:19 +00:00
|
|
|
toNextFork(c.londonBlock),
|
2022-03-24 08:54:11 +00:00
|
|
|
toNextFork(c.arrowGlacierBlock),
|
2022-10-17 10:22:01 +00:00
|
|
|
toNextFork(c.grayGlacierBlock),
|
|
|
|
toNextFork(c.mergeForkBlock),
|
|
|
|
toNextFork(c.shanghaiBlock),
|
|
|
|
toNextFork(c.cancunBlock),
|
2021-07-14 15:13:27 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if fork == high(ChainFork):
|
|
|
|
result = 0
|
|
|
|
return
|
|
|
|
|
|
|
|
result = next[fork]
|
|
|
|
for x in fork..high(ChainFork):
|
|
|
|
if result != next[x]:
|
|
|
|
result = next[x]
|
|
|
|
break
|
|
|
|
|
|
|
|
func calculateForkId(c: ChainConfig, fork: ChainFork,
|
|
|
|
prevCRC: uint32, prevFork: uint64): ForkID =
|
|
|
|
result.nextFork = c.getNextFork(fork)
|
|
|
|
|
|
|
|
if result.nextFork != prevFork:
|
|
|
|
result.crc = crc32(prevCRC, toBytesBE(prevFork))
|
|
|
|
else:
|
|
|
|
result.crc = prevCRC
|
|
|
|
|
|
|
|
func calculateForkIds(c: ChainConfig,
|
|
|
|
genesisCRC: uint32): array[ChainFork, ForkID] =
|
|
|
|
var prevCRC = genesisCRC
|
|
|
|
var prevFork = c.getNextFork(Frontier)
|
|
|
|
|
|
|
|
for fork in ChainFork:
|
|
|
|
result[fork] = calculateForkId(c, fork, prevCRC, prevFork)
|
|
|
|
prevFork = result[fork].nextFork
|
|
|
|
prevCRC = result[fork].crc
|
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
proc setForkId(c: Chain)
|
2021-09-08 13:28:17 +00:00
|
|
|
{. raises: [Defect,CatchableError].} =
|
2022-05-09 14:04:48 +00:00
|
|
|
let blockZero = c.db.toGenesisHeader
|
|
|
|
c.blockZeroHash = blockZero.blockHash
|
|
|
|
c.blockZeroStateRoot = blockZero.stateRoot
|
2021-09-08 13:28:17 +00:00
|
|
|
let genesisCRC = crc32(0, c.blockZeroHash.data)
|
|
|
|
c.forkIds = calculateForkIds(c.db.config, genesisCRC)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-30 14:06:51 +00:00
|
|
|
# Private constructor helper
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc initChain(c: Chain; db: BaseChainDB; poa: Clique; extraValidation: bool)
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Constructor for the `Chain` descriptor object. For most applications,
|
|
|
|
## the `poa` argument is transparent and should be initilaised on the fly
|
|
|
|
## which is available below.
|
2021-07-30 14:06:51 +00:00
|
|
|
c.db = db
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
if not db.config.daoForkSupport:
|
|
|
|
db.config.daoForkBlock = db.config.homesteadBlock
|
2022-09-16 04:23:25 +00:00
|
|
|
c.validateBlock = true
|
2021-07-30 14:06:51 +00:00
|
|
|
c.extraValidation = extraValidation
|
2021-09-16 15:59:46 +00:00
|
|
|
c.setForkId()
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# Initalise the PoA state regardless of whether it is needed on the current
|
|
|
|
# network. For non-PoA networks (when `db.config.poaEngine` is `false`),
|
|
|
|
# this descriptor is ignored.
|
2021-07-30 14:06:51 +00:00
|
|
|
c.poa = db.newClique
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-12-10 08:49:57 +00:00
|
|
|
# Always initialise the PoW epoch cache even though it migh no be used
|
2021-07-14 15:13:27 +00:00
|
|
|
# unless `extraValidation` is set `true`.
|
2021-12-10 08:49:57 +00:00
|
|
|
c.pow = PowRef.new
|
2021-07-30 14:06:51 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc newChain*(db: BaseChainDB; poa: Clique; extraValidation: bool): Chain
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Constructor for the `Chain` descriptor object. For most applications,
|
|
|
|
## the `poa` argument is transparent and should be initilaised on the fly
|
|
|
|
## which is available below. The argument `extraValidation` enables extra
|
|
|
|
## block chain validation if set `true`.
|
|
|
|
new result
|
|
|
|
result.initChain(db, poa, extraValidation)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
proc newChain*(db: BaseChainDB, extraValidation: bool): Chain
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Constructor for the `Chain` descriptor object with default initialisation
|
2021-07-30 14:06:51 +00:00
|
|
|
## for the PoA handling. The argument `extraValidation` enables extra block
|
|
|
|
## chain validation if set `true`.
|
|
|
|
new result
|
|
|
|
result.initChain(db, db.newClique, extraValidation)
|
|
|
|
|
|
|
|
proc newChain*(db: BaseChainDB): Chain
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Constructor for the `Chain` descriptor object. All sub-object descriptors
|
|
|
|
## are initialised with defaults. So is extra block chain validation
|
|
|
|
## * `enabled` for PoA networks (such as Goerli)
|
|
|
|
## * `disabled` for nopn-PaA networks
|
|
|
|
new result
|
|
|
|
result.initChain(db, db.newClique, db.config.poaEngine)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc clique*(c: Chain): var Clique =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
c.poa
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc pow*(c: Chain): PowRef =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
2021-12-10 08:49:57 +00:00
|
|
|
c.pow
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc db*(c: Chain): BaseChainDB =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
c.db
|
|
|
|
|
2022-09-16 04:23:25 +00:00
|
|
|
proc validateBlock*(c: Chain): bool =
|
|
|
|
## Getter
|
|
|
|
c.validateBlock
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc extraValidation*(c: Chain): bool =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
c.extraValidation
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc forkIds*(c: Chain): array[ChainFork,ForkID] =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Getter
|
|
|
|
c.forkIds
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc verifyFrom*(c: Chain): BlockNumber =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Getter
|
|
|
|
c.verifyFrom
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
proc currentBlock*(c: Chain): BlockHeader
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## currentBlock retrieves the current head block of the canonical chain.
|
|
|
|
## Ideally the block should be retrieved from the blockchain's internal cache.
|
|
|
|
## but now it's enough to retrieve it from database
|
|
|
|
c.db.getCanonicalHead()
|
|
|
|
|
2022-10-10 02:31:28 +00:00
|
|
|
func genesisHash*(c: Chain): Hash256 =
|
|
|
|
## Getter
|
|
|
|
c.blockZeroHash
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `Chain` setters
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-16 04:23:25 +00:00
|
|
|
proc `validateBlock=`*(c: Chain; validateBlock: bool) =
|
|
|
|
## Setter. If set `true`, the assignment value `validateBlock` enables
|
|
|
|
## block execution, else it will always return ValidationResult.OK
|
|
|
|
c.validateBlock = validateBlock
|
2021-07-30 14:06:51 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc `extraValidation=`*(c: Chain; extraValidation: bool) =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Setter. If set `true`, the assignment value `extraValidation` enables
|
|
|
|
## extra block chain validation.
|
|
|
|
c.extraValidation = extraValidation
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc `verifyFrom=`*(c: Chain; verifyFrom: BlockNumber) =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Setter. The assignment value `verifyFrom` defines the first block where
|
|
|
|
## validation should start if the `Clique` field `extraValidation` was set
|
|
|
|
## `true`.
|
2022-01-10 09:04:06 +00:00
|
|
|
c.verifyFrom = verifyFrom
|
|
|
|
|
|
|
|
proc `verifyFrom=`*(c: Chain; verifyFrom: uint64) =
|
|
|
|
## Variant of `verifyFrom=`
|
2021-07-30 14:06:51 +00:00
|
|
|
c.verifyFrom = verifyFrom.u256
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|