refactor genesis.nim

This commit is contained in:
jangko 2022-02-15 10:22:05 +07:00
parent b951139af9
commit 9a1c8fc779
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 34 additions and 27 deletions

View File

@ -10,7 +10,7 @@ import
testutils/unittests, chronos, testutils/unittests, chronos,
eth/[keys, trie/db, trie/hexary], eth/[keys, trie/db, trie/hexary],
eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/routing_table, eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/routing_table,
../../nimbus/[genesis, chain_config, config, db/db_chain], ../../nimbus/[genesis, chain_config, config, db/db_chain, db/state_db],
../network/wire/portal_protocol, ../network/wire/portal_protocol,
../network/state/[state_content, state_network], ../network/state/[state_content, state_network],
../content_db, ../content_db,
@ -28,12 +28,11 @@ proc genesisToTrie(filePath: string): HexaryTrie =
CustomNet, CustomNet,
cn cn
) )
# TODO: this actually also creates a HexaryTrie and AccountStateDB, which we
# could skip
let header = toGenesisHeader(chainDB)
# Trie exists already in flat db, but need to provide the root let sdb = newStateDB(chainDB.db, chainDB.pruneTrie)
initHexaryTrie(chainDB.db, header.stateRoot, chainDB.pruneTrie) let header = toGenesisHeader(chainDB, sdb)
sdb.getTrie
procSuite "State Content Network": procSuite "State Content Network":
let rng = newRng() let rng = newRng()

View File

@ -72,6 +72,12 @@ proc newAccountStateDB*(backingStore: TrieDatabaseRef,
when aleth_compat: when aleth_compat:
result.cleared = initHashSet[EthAddress]() result.cleared = initHashSet[EthAddress]()
proc getTrie*(db: AccountStateDB): HexaryTrie =
HexaryTrie db.trie
proc getSecureTrie*(db: AccountStateDB): SecureHexaryTrie =
db.trie
proc getAccount*(db: AccountStateDB, address: EthAddress): Account = proc getAccount*(db: AccountStateDB, address: EthAddress): Account =
let recordFound = db.trie.get(address) let recordFound = db.trie.get(address)
if recordFound.len > 0: if recordFound.len > 0:
@ -93,15 +99,15 @@ proc getBalance*(db: AccountStateDB, address: EthAddress): UInt256 =
let account = db.getAccount(address) let account = db.getAccount(address)
account.balance account.balance
proc setBalance*(db: var AccountStateDB, address: EthAddress, balance: UInt256) = proc setBalance*(db: AccountStateDB, address: EthAddress, balance: UInt256) =
var account = db.getAccount(address) var account = db.getAccount(address)
account.balance = balance account.balance = balance
db.setAccount(address, account) db.setAccount(address, account)
proc addBalance*(db: var AccountStateDB, address: EthAddress, delta: UInt256) = proc addBalance*(db: AccountStateDB, address: EthAddress, delta: UInt256) =
db.setBalance(address, db.getBalance(address) + delta) db.setBalance(address, db.getBalance(address) + delta)
proc subBalance*(db: var AccountStateDB, address: EthAddress, delta: UInt256) = proc subBalance*(db: AccountStateDB, address: EthAddress, delta: UInt256) =
db.setBalance(address, db.getBalance(address) - delta) db.setBalance(address, db.getBalance(address) - delta)
template createTrieKeyFromSlot(slot: UInt256): auto = template createTrieKeyFromSlot(slot: UInt256): auto =
@ -120,7 +126,7 @@ template getAccountTrie(db: AccountStateDB, account: Account): auto =
# see nim-eth#9 # see nim-eth#9
initSecureHexaryTrie(trieDB(db), account.storageRoot, false) initSecureHexaryTrie(trieDB(db), account.storageRoot, false)
proc clearStorage*(db: var AccountStateDB, address: EthAddress) = proc clearStorage*(db: AccountStateDB, address: EthAddress) =
var account = db.getAccount(address) var account = db.getAccount(address)
account.storageRoot = emptyRlpHash account.storageRoot = emptyRlpHash
db.setAccount(address, account) db.setAccount(address, account)
@ -131,7 +137,7 @@ proc getStorageRoot*(db: AccountStateDB, address: EthAddress): Hash256 =
var account = db.getAccount(address) var account = db.getAccount(address)
account.storageRoot account.storageRoot
proc setStorage*(db: var AccountStateDB, proc setStorage*(db: AccountStateDB,
address: EthAddress, address: EthAddress,
slot: UInt256, value: UInt256) = slot: UInt256, value: UInt256) =
var account = db.getAccount(address) var account = db.getAccount(address)

View File

@ -8,10 +8,12 @@ import
{.push raises: [Defect].} {.push raises: [Defect].}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private functions # Public functions
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc newStateDB*(db: TrieDatabaseRef, pruneTrie: bool): AccountStateDB =
newAccountStateDB(db, emptyRlpHash, pruneTrie)
proc initDbAccounts(db: BaseChainDB): BlockHeader proc toGenesisHeader*(db: BaseChainDB, sdb: AccountStateDB): BlockHeader
{.raises: [Defect, RlpError].} = {.raises: [Defect, RlpError].} =
## Initialise block chain DB accounts derived from the `genesis.alloc` table ## Initialise block chain DB accounts derived from the `genesis.alloc` table
## of the `db` descriptor argument. ## of the `db` descriptor argument.
@ -24,7 +26,6 @@ proc initDbAccounts(db: BaseChainDB): BlockHeader
# function `eth/trie/db.trieDB()`. # function `eth/trie/db.trieDB()`.
db.db.put(emptyRlpHash.data, emptyRlp) db.db.put(emptyRlpHash.data, emptyRlp)
var sdb = newAccountStateDB(db.db, emptyRlpHash, db.pruneTrie)
let g = db.genesis let g = db.genesis
for address, account in g.alloc: for address, account in g.alloc:
@ -86,18 +87,16 @@ proc initDbAccounts(db: BaseChainDB): BlockHeader
if g.difficulty.isZero: if g.difficulty.isZero:
result.difficulty = GENESIS_DIFFICULTY result.difficulty = GENESIS_DIFFICULTY
# ------------------------------------------------------------------------------ proc toGenesisHeader*(params: NetworkParams): BlockHeader
# Public functions
# ------------------------------------------------------------------------------
proc toGenesisHeader*(params: NetworkParams, db = newMemoryDb()): BlockHeader
{.raises: [Defect, RlpError].} = {.raises: [Defect, RlpError].} =
## Generate the genesis block header from the `params` argument value. ## Generate the genesis block header from the `params` argument value.
newBaseChainDB( let cdb = newBaseChainDB(
db = db, db = newMemoryDb(),
id = params.config.chainID.NetworkId, id = params.config.chainID.NetworkId,
params = params, params = params,
pruneTrie = true).initDbAccounts pruneTrie = true)
let sdb = newStateDB(cdb.db, cdb.pruneTrie)
cdb.toGenesisHeader(sdb)
proc toGenesisHeader*(db: BaseChainDB): BlockHeader proc toGenesisHeader*(db: BaseChainDB): BlockHeader
{.raises: [Defect, RlpError].} = {.raises: [Defect, RlpError].} =
@ -105,14 +104,17 @@ proc toGenesisHeader*(db: BaseChainDB): BlockHeader
## fields of the argument `db` descriptor. ## fields of the argument `db` descriptor.
NetworkParams( NetworkParams(
config: db.config, config: db.config,
genesis: db.genesis).toGenesisHeader(db.db) genesis: db.genesis).toGenesisHeader()
proc initializeEmptyDb*(db: BaseChainDB) proc initializeEmptyDb*(cdb: BaseChainDB)
{.raises: [Defect, CatchableError].} = {.raises: [Defect, CatchableError].} =
trace "Writing genesis to DB" trace "Writing genesis to DB"
let b = db.initDbAccounts let sdb = newStateDB(cdb.db, cdb.pruneTrie)
doAssert(b.blockNumber.isZero, "can't commit genesis block with number > 0") let header = cdb.toGenesisHeader(sdb)
discard db.persistHeaderToDb(b) doAssert(header.blockNumber.isZero, "can't commit genesis block with number > 0")
# faster lookup of curent total difficulty
cdb.totalDifficulty = header.difficulty
discard cdb.persistHeaderToDb(header)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# End # End