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,
eth/[keys, trie/db, trie/hexary],
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/state/[state_content, state_network],
../content_db,
@ -28,12 +28,11 @@ proc genesisToTrie(filePath: string): HexaryTrie =
CustomNet,
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
initHexaryTrie(chainDB.db, header.stateRoot, chainDB.pruneTrie)
let sdb = newStateDB(chainDB.db, chainDB.pruneTrie)
let header = toGenesisHeader(chainDB, sdb)
sdb.getTrie
procSuite "State Content Network":
let rng = newRng()

View File

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

View File

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