2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-05-20 10:17:51 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00: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.
|
|
|
|
|
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
|
|
|
{.push raises: [].}
|
|
|
|
|
2018-10-05 00:20:12 +00:00
|
|
|
import
|
2021-09-16 15:59:46 +00:00
|
|
|
std/tables,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/eip1559,
|
|
|
|
eth/common/[hashes, accounts, headers, addresses],
|
2024-05-20 10:17:51 +00:00
|
|
|
../db/[ledger, core_db],
|
2022-12-02 04:39:12 +00:00
|
|
|
../constants,
|
|
|
|
./chain_config
|
2020-01-13 18:35:40 +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
|
|
|
# ------------------------------------------------------------------------------
|
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
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
g: Genesis;
|
2024-10-26 09:26:38 +00:00
|
|
|
db: CoreDbRef;
|
2023-01-31 01:32:17 +00:00
|
|
|
fork: HardFork;
|
2024-10-26 09:26:38 +00:00
|
|
|
): Header =
|
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.
|
|
|
|
##
|
2024-10-27 18:56:28 +00:00
|
|
|
let ac = LedgerRef.init(db)
|
2022-02-11 16:28:39 +00:00
|
|
|
|
2018-08-01 12:50:44 +00:00
|
|
|
for address, account in g.alloc:
|
2024-10-26 09:26:38 +00:00
|
|
|
ac.setNonce(address, account.nonce)
|
|
|
|
ac.setBalance(address, account.balance)
|
|
|
|
ac.setCode(address, account.code)
|
2022-02-01 12:04:20 +00:00
|
|
|
|
2018-08-09 09:25:37 +00:00
|
|
|
for k, v in account.storage:
|
2024-10-26 09:26:38 +00:00
|
|
|
ac.setStorage(address, k, v)
|
2018-08-01 12:50:44 +00:00
|
|
|
|
2024-10-26 09:26:38 +00:00
|
|
|
ac.persist()
|
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
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
result = Header(
|
2018-08-01 12:50:44 +00:00
|
|
|
nonce: g.nonce,
|
|
|
|
timestamp: g.timestamp,
|
|
|
|
extraData: g.extraData,
|
|
|
|
gasLimit: g.gasLimit,
|
|
|
|
difficulty: g.difficulty,
|
2024-06-14 07:31:08 +00:00
|
|
|
mixHash: g.mixHash,
|
2018-08-01 12:50:44 +00:00
|
|
|
coinbase: g.coinbase,
|
2024-10-27 18:56:28 +00:00
|
|
|
stateRoot: ac.getStateRoot(),
|
2018-08-01 12:50:44 +00:00
|
|
|
parentHash: GENESIS_PARENT_HASH,
|
2024-09-29 12:37:09 +00:00
|
|
|
transactionsRoot: EMPTY_ROOT_HASH,
|
2024-06-14 07:31:08 +00:00
|
|
|
receiptsRoot: 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:
|
2024-06-14 07:31:08 +00:00
|
|
|
result.baseFeePerGas = Opt.some(g.baseFeePerGas.get)
|
2022-12-02 04:39:12 +00:00
|
|
|
elif fork >= London:
|
2024-06-14 07:31:08 +00:00
|
|
|
result.baseFeePerGas = Opt.some(EIP1559_INITIAL_BASE_FEE)
|
2021-06-30 13:30:39 +00:00
|
|
|
|
2023-09-13 02:32:38 +00:00
|
|
|
if g.gasLimit == 0:
|
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:
|
2024-06-14 07:31:08 +00:00
|
|
|
result.withdrawalsRoot = Opt.some(EMPTY_ROOT_HASH)
|
2023-06-25 13:30:34 +00:00
|
|
|
|
2023-07-20 23:34:56 +00:00
|
|
|
if fork >= Cancun:
|
2024-06-14 07:31:08 +00:00
|
|
|
result.blobGasUsed = Opt.some g.blobGasUsed.get(0'u64)
|
|
|
|
result.excessBlobGas = Opt.some g.excessBlobGas.get(0'u64)
|
2024-10-16 01:34:12 +00:00
|
|
|
result.parentBeaconBlockRoot = Opt.some g.parentBeaconBlockRoot.get(default(Hash32))
|
2023-07-20 23:34:56 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
genesis: Genesis;
|
|
|
|
fork: HardFork;
|
2024-10-26 09:26:38 +00:00
|
|
|
db = CoreDbRef(nil)): Header =
|
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
|
2024-05-20 10:17:51 +00:00
|
|
|
db = if db.isNil: AristoDbMemory.newCoreDbRef() else: db
|
2024-10-26 09:26:38 +00:00
|
|
|
toGenesisHeader(genesis, db, fork)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc toGenesisHeader*(
|
|
|
|
params: NetworkParams;
|
2024-05-29 11:06:49 +00:00
|
|
|
db = CoreDbRef(nil)
|
2024-10-26 09:26:38 +00:00
|
|
|
): Header =
|
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)
|
2024-06-14 07:31:08 +00:00
|
|
|
let fork = map.toHardFork(forkDeterminationInfo(0.BlockNumber, params.genesis.timestamp))
|
2024-05-29 11:06:49 +00:00
|
|
|
toGenesisHeader(params.genesis, fork, db)
|
2022-02-11 16:28:39 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|