2018-10-05 00:20:12 +00:00
|
|
|
import
|
2021-09-16 15:59:46 +00:00
|
|
|
std/tables,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/[common, rlp, eip1559],
|
2023-08-04 11:10:09 +00:00
|
|
|
eth/trie/trie_defs,
|
|
|
|
../db/[core_db, state_db],
|
2022-12-02 04:39:12 +00:00
|
|
|
../constants,
|
|
|
|
./chain_config
|
2020-01-13 18:35:40 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
2022-02-01 12:04:20 +00:00
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-02-15 03:22:05 +00:00
|
|
|
# Public functions
|
2022-02-11 16:28:39 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-01-31 01:32:17 +00:00
|
|
|
proc newStateDB*(
|
2023-08-04 11:10:09 +00:00
|
|
|
db: CoreDbRef;
|
2023-01-31 01:32:17 +00:00
|
|
|
pruneTrie: bool;
|
|
|
|
): AccountStateDB
|
|
|
|
{.gcsafe, raises: [].}=
|
2022-02-15 03:22:05 +00:00
|
|
|
newAccountStateDB(db, emptyRlpHash, pruneTrie)
|
2022-02-11 16:28:39 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
g: Genesis;
|
|
|
|
sdb: AccountStateDB;
|
|
|
|
fork: HardFork;
|
|
|
|
): BlockHeader
|
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2022-02-11 16:28:39 +00:00
|
|
|
## Initialise block chain DB accounts derived from the `genesis.alloc` table
|
|
|
|
## of the `db` descriptor argument.
|
|
|
|
##
|
|
|
|
## The function returns the `Genesis` block header.
|
|
|
|
##
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
# The following kludge is needed for the `LegacyDbPersistent` type database
|
|
|
|
# when `pruneTrie` is enabled. For other cases, this code is irrelevant.
|
|
|
|
sdb.db.compensateLegacySetup()
|
2018-08-01 12:50:44 +00:00
|
|
|
|
|
|
|
for address, account in g.alloc:
|
2018-08-09 09:25:37 +00:00
|
|
|
sdb.setAccount(address, newAccount(account.nonce, account.balance))
|
2020-04-20 18:12:44 +00:00
|
|
|
sdb.setCode(address, account.code)
|
2022-02-01 12:04:20 +00:00
|
|
|
|
|
|
|
# Kludge:
|
|
|
|
#
|
2023-08-04 11:10:09 +00:00
|
|
|
# See https://github.com/status-im/nim-eth/issues/9 where other,
|
2022-02-01 12:04:20 +00:00
|
|
|
# probably related debilities are discussed.
|
|
|
|
#
|
|
|
|
# This kludge also fixes the initial crash described in
|
|
|
|
# https://github.com/status-im/nimbus-eth1/issues/932.
|
2022-12-02 04:39:12 +00:00
|
|
|
if sdb.pruneTrie and 0 < account.storage.len:
|
2023-08-04 11:10:09 +00:00
|
|
|
sdb.db.compensateLegacySetup() # <-- kludge
|
2022-02-01 12:04:20 +00:00
|
|
|
|
2018-08-09 09:25:37 +00:00
|
|
|
for k, v in account.storage:
|
|
|
|
sdb.setStorage(address, k, v)
|
2018-08-01 12:50:44 +00:00
|
|
|
|
|
|
|
result = BlockHeader(
|
|
|
|
nonce: g.nonce,
|
|
|
|
timestamp: g.timestamp,
|
|
|
|
extraData: g.extraData,
|
|
|
|
gasLimit: g.gasLimit,
|
|
|
|
difficulty: g.difficulty,
|
2022-04-08 04:54:11 +00:00
|
|
|
mixDigest: g.mixHash,
|
2018-08-01 12:50:44 +00:00
|
|
|
coinbase: g.coinbase,
|
2021-05-13 09:01:58 +00:00
|
|
|
stateRoot: sdb.rootHash,
|
2018-08-01 12:50:44 +00:00
|
|
|
parentHash: GENESIS_PARENT_HASH,
|
2022-09-03 18:15:35 +00:00
|
|
|
txRoot: EMPTY_ROOT_HASH,
|
|
|
|
receiptRoot: EMPTY_ROOT_HASH,
|
2018-08-01 12:50:44 +00:00
|
|
|
ommersHash: EMPTY_UNCLE_HASH
|
|
|
|
)
|
|
|
|
|
2021-06-30 13:30:39 +00:00
|
|
|
if g.baseFeePerGas.isSome:
|
|
|
|
result.baseFee = g.baseFeePerGas.get()
|
2022-12-02 04:39:12 +00:00
|
|
|
elif fork >= London:
|
2021-06-30 13:30:39 +00:00
|
|
|
result.baseFee = EIP1559_INITIAL_BASE_FEE.u256
|
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
if g.gasLimit.isZero:
|
2018-08-01 12:50:44 +00:00
|
|
|
result.gasLimit = GENESIS_GAS_LIMIT
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
if g.difficulty.isZero and fork <= London:
|
2018-08-01 12:50:44 +00:00
|
|
|
result.difficulty = GENESIS_DIFFICULTY
|
|
|
|
|
2023-06-25 13:30:34 +00:00
|
|
|
if fork >= Shanghai:
|
|
|
|
result.withdrawalsRoot = some(EMPTY_ROOT_HASH)
|
|
|
|
|
2023-07-20 23:34:56 +00:00
|
|
|
if fork >= Cancun:
|
2023-08-31 03:58:31 +00:00
|
|
|
result.blobGasUsed = g.blobGasUsed.get(0'u64).some
|
|
|
|
result.excessBlobGas = g.excessBlobGas.get(0'u64).some
|
|
|
|
result.parentBeaconBlockRoot = g.parentBeaconBlockRoot.get(Hash256()).some
|
2023-07-20 23:34:56 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
genesis: Genesis;
|
|
|
|
fork: HardFork;
|
2023-08-04 11:10:09 +00:00
|
|
|
db = CoreDbRef(nil);
|
2023-01-31 01:32:17 +00:00
|
|
|
): BlockHeader
|
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2023-08-04 11:10:09 +00:00
|
|
|
## Generate the genesis block header from the `genesis` and `config`
|
|
|
|
## argument value.
|
2022-12-02 04:39:12 +00:00
|
|
|
let
|
2023-08-04 11:10:09 +00:00
|
|
|
db = if db.isNil: newCoreDbRef LegacyDbMemory else: db
|
2022-12-02 04:39:12 +00:00
|
|
|
sdb = newStateDB(db, pruneTrie = true)
|
|
|
|
toGenesisHeader(genesis, sdb, fork)
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
params: NetworkParams;
|
2023-08-04 11:10:09 +00:00
|
|
|
db = CoreDbRef(nil);
|
2023-01-31 01:32:17 +00:00
|
|
|
): BlockHeader
|
|
|
|
{.raises: [RlpError].} =
|
2023-08-04 11:10:09 +00:00
|
|
|
## Generate the genesis block header from the `genesis` and `config`
|
|
|
|
## argument value.
|
2023-02-16 11:40:07 +00:00
|
|
|
let map = toForkTransitionTable(params.config)
|
|
|
|
let fork = map.toHardFork(forkDeterminationInfo(0.toBlockNumber, params.genesis.timestamp))
|
2022-12-02 04:39:12 +00:00
|
|
|
toGenesisHeader(params.genesis, fork, db)
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
# End
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|