2018-10-05 00:20:12 +00:00
|
|
|
import
|
2021-09-16 15:59:46 +00:00
|
|
|
std/tables,
|
|
|
|
eth/[common, rlp, trie, p2p],
|
2021-10-28 03:48:28 +00:00
|
|
|
chronicles, eth/trie/[db, trie_defs],
|
2021-05-13 09:01:58 +00:00
|
|
|
./db/[db_chain, state_db],
|
2021-10-28 03:48:28 +00:00
|
|
|
"."/[constants, chain_config, forks, p2p/gaslimit]
|
2020-01-13 18:35:40 +00:00
|
|
|
|
2021-11-18 16:52:44 +00:00
|
|
|
proc toBlock*(g: Genesis, db: BaseChainDB = nil):
|
|
|
|
BlockHeader {.raises: [Defect, RlpError].} =
|
2018-11-30 10:07:20 +00:00
|
|
|
let (tdb, pruneTrie) = if db.isNil: (newMemoryDB(), true)
|
|
|
|
else: (db.db, db.pruneTrie)
|
2021-11-02 10:25:22 +00:00
|
|
|
tdb.put(emptyRlpHash.data, emptyRlp)
|
2021-10-28 03:48:28 +00:00
|
|
|
var sdb = newAccountStateDB(tdb, emptyRlpHash, pruneTrie)
|
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)
|
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,
|
|
|
|
mixDigest: g.mixhash,
|
|
|
|
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,
|
|
|
|
txRoot: BLANK_ROOT_HASH,
|
|
|
|
receiptRoot: BLANK_ROOT_HASH,
|
|
|
|
ommersHash: EMPTY_UNCLE_HASH
|
|
|
|
)
|
|
|
|
|
2021-06-30 13:30:39 +00:00
|
|
|
if g.baseFeePerGas.isSome:
|
|
|
|
result.baseFee = g.baseFeePerGas.get()
|
|
|
|
elif db.isNil.not and db.config.toFork(0.toBlockNumber) >= FkLondon:
|
|
|
|
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
|
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
if g.difficulty.isZero:
|
2018-08-01 12:50:44 +00:00
|
|
|
result.difficulty = GENESIS_DIFFICULTY
|
|
|
|
|
2021-09-08 13:28:17 +00:00
|
|
|
proc initializeEmptyDb*(db: BaseChainDB) =
|
2019-01-08 10:47:40 +00:00
|
|
|
trace "Writing genesis to DB"
|
2021-09-16 15:59:46 +00:00
|
|
|
let b = db.genesis.toBlock(db)
|
|
|
|
doAssert(b.blockNumber.isZero, "can't commit genesis block with number > 0")
|
|
|
|
discard db.persistHeaderToDb(b)
|