config: remove last instance of getConfiguration usage from nimbus code

this is a preparation for migration to confutils based config
although there is still some getConfiguration usage in tests code
it will be removed after new config arrived
This commit is contained in:
jangko 2021-09-08 20:28:17 +07:00
parent c9cfebfa97
commit 48d497580a
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
12 changed files with 79 additions and 62 deletions

View File

@ -27,10 +27,11 @@ proc processNode(genesisFile, chainFile,
conf = getConfiguration() conf = getConfiguration()
chainDB = newBaseChainDB(newMemoryDb(), chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false, pruneTrie = false,
conf.net.networkId conf.net.networkId,
conf.customNetwork
) )
initializeEmptyDb(chainDB, conf.customNetwork) initializeEmptyDb(chainDB)
discard importRlpBlock(chainFile, chainDB) discard importRlpBlock(chainFile, chainDB)
let head = chainDB.getCanonicalHead() let head = chainDB.getCanonicalHead()
let blockHash = "0x" & head.blockHash.data.toHex let blockHash = "0x" & head.blockHash.data.toHex

View File

@ -79,10 +79,11 @@ proc main() =
ethNode = setupEthNode(conf, ethCtx, eth) ethNode = setupEthNode(conf, ethCtx, eth)
chainDB = newBaseChainDB(newMemoryDb(), chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false, pruneTrie = false,
conf.net.networkId conf.net.networkId,
conf.customNetwork
) )
initializeEmptyDb(chainDB, conf.customNetwork) initializeEmptyDb(chainDB)
discard importRlpBlock(blocksFile, chainDB) discard importRlpBlock(blocksFile, chainDB)
let ctx = setupGraphqlContext(chainDB, ethNode) let ctx = setupGraphqlContext(chainDB, ethNode)

View File

@ -166,7 +166,7 @@ proc toFork*(c: ChainConfig, number: BlockNumber): Fork =
elif number >= c.homesteadBlock: FkHomestead elif number >= c.homesteadBlock: FkHomestead
else: FkFrontier else: FkFrontier
proc chainConfig*(id: NetworkId): ChainConfig = proc chainConfig*(id: NetworkId, cn: CustomNetwork): ChainConfig =
# For some public networks, NetworkId and ChainId value are identical # For some public networks, NetworkId and ChainId value are identical
# but that is not always the case # but that is not always the case
@ -245,10 +245,9 @@ proc chainConfig*(id: NetworkId): ChainConfig =
londonBlock: 5_062_605.toBlockNumber # June 30, 2021 londonBlock: 5_062_605.toBlockNumber # June 30, 2021
) )
else: else:
# everything else will use CustomNet config # everything else will use CustomNet config
let conf = getConfiguration() trace "Custom genesis block configuration loaded", conf=cn.config
trace "Custom genesis block configuration loaded", conf=conf.customNetwork.config cn.config
conf.customNetwork.config
proc processList(v: string, o: var seq[string]) = proc processList(v: string, o: var seq[string]) =
## Process comma-separated list of strings. ## Process comma-separated list of strings.

View File

@ -18,6 +18,7 @@ type
pruneTrie*: bool pruneTrie*: bool
config* : ChainConfig config* : ChainConfig
networkId*: NetworkId networkId*: NetworkId
customNetwork*: CustomNetwork
# startingBlock, currentBlock, and highestBlock # startingBlock, currentBlock, and highestBlock
# are progress indicator # are progress indicator
@ -29,12 +30,18 @@ type
blockNumber: BlockNumber blockNumber: BlockNumber
index: int index: int
proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true, id: NetworkId = MainNet): BaseChainDB = proc newBaseChainDB*(
db: TrieDatabaseRef,
pruneTrie: bool = true,
id: NetworkId = MainNet,
cn = CustomNetwork() ): BaseChainDB =
new(result) new(result)
result.db = db result.db = db
result.pruneTrie = pruneTrie result.pruneTrie = pruneTrie
result.config = chainConfig(id) result.config = chainConfig(id, cn)
result.networkId = id result.networkId = id
result.customNetwork = cn
proc `$`*(db: BaseChainDB): string = proc `$`*(db: BaseChainDB): string =
result = "BaseChainDB" result = "BaseChainDB"

View File

@ -89,7 +89,7 @@ proc commit*(g: Genesis, db: BaseChainDB) =
doAssert(b.blockNumber == 0, "can't commit genesis block with number > 0") doAssert(b.blockNumber == 0, "can't commit genesis block with number > 0")
discard db.persistHeaderToDb(b) discard db.persistHeaderToDb(b)
proc initializeEmptyDb*(db: BaseChainDB, cn: CustomNetwork) = proc initializeEmptyDb*(db: BaseChainDB) =
trace "Writing genesis to DB" trace "Writing genesis to DB"
let genesis = genesisBlockForNetwork(db.networkId, cn) let genesis = genesisBlockForNetwork(db.networkId, db.customNetwork)
genesis.commit(db) genesis.commit(db)

View File

@ -21,7 +21,7 @@ import
config, genesis, rpc/[common, p2p, debug], p2p/chain, config, genesis, rpc/[common, p2p, debug], p2p/chain,
eth/trie/db, metrics, metrics/[chronos_httpserver, chronicles_support], eth/trie/db, metrics, metrics/[chronos_httpserver, chronicles_support],
graphql/ethapi, context, graphql/ethapi, context,
"."/[utils, conf_utils, sealer, constants] "."/[conf_utils, sealer, constants]
## TODO: ## TODO:
## * No IPv6 support ## * No IPv6 support
@ -54,12 +54,13 @@ proc start(nimbus: NimbusNode) =
let trieDB = trieDB newChainDb(conf.dataDir) let trieDB = trieDB newChainDb(conf.dataDir)
var chainDB = newBaseChainDB(trieDB, var chainDB = newBaseChainDB(trieDB,
conf.prune == PruneMode.Full, conf.prune == PruneMode.Full,
conf.net.networkId conf.net.networkId,
conf.customNetwork
) )
chainDB.populateProgress() chainDB.populateProgress()
if canonicalHeadHashKey().toOpenArray notin trieDB: if canonicalHeadHashKey().toOpenArray notin trieDB:
initializeEmptyDb(chainDb, conf.customNetwork) initializeEmptyDb(chainDb)
doAssert(canonicalHeadHashKey().toOpenArray in trieDB) doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
if conf.importFile.len > 0: if conf.importFile.len > 0:
@ -134,7 +135,6 @@ proc start(nimbus: NimbusNode) =
# chainRef: some name to avoid module-name/filed/function misunderstandings # chainRef: some name to avoid module-name/filed/function misunderstandings
let chainRef = newChain(chainDB) let chainRef = newChain(chainDB)
chainRef.setForkId(conf.customNetwork)
nimbus.ethNode.chain = chainRef nimbus.ethNode.chain = chainRef
if conf.verifyFromOk: if conf.verifyFromOk:
chainRef.extraValidation = 0 < conf.verifyFrom chainRef.extraValidation = 0 < conf.verifyFrom

View File

@ -115,6 +115,13 @@ func calculateForkIds(c: ChainConfig,
prevFork = result[fork].nextFork prevFork = result[fork].nextFork
prevCRC = result[fork].crc prevCRC = result[fork].crc
proc setForkId(c: Chain, cn: CustomNetwork)
{. raises: [Defect,CatchableError].} =
let g = genesisBlockForNetwork(c.db.networkId, cn)
c.blockZeroHash = g.toBlock.blockHash
let genesisCRC = crc32(0, c.blockZeroHash.data)
c.forkIds = calculateForkIds(c.db.config, genesisCRC)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private constructor helper # Private constructor helper
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -129,6 +136,7 @@ proc initChain(c: Chain; db: BaseChainDB; poa: Clique; extraValidation: bool)
if not db.config.daoForkSupport: if not db.config.daoForkSupport:
db.config.daoForkBlock = db.config.homesteadBlock db.config.daoForkBlock = db.config.homesteadBlock
c.extraValidation = extraValidation c.extraValidation = extraValidation
c.setForkId(db.customNetwork)
# Initalise the PoA state regardless of whether it is needed on the current # Initalise the PoA state regardless of whether it is needed on the current
# network. For non-PoA networks (when `db.config.poaEngine` is `false`), # network. For non-PoA networks (when `db.config.poaEngine` is `false`),
@ -237,13 +245,6 @@ proc `verifyFrom=`*(c: Chain; verifyFrom: uint64) {.inline.} =
## `true`. ## `true`.
c.verifyFrom = verifyFrom.u256 c.verifyFrom = verifyFrom.u256
proc setForkId*(c: Chain, cn = CustomNetwork())
{. raises: [Defect,CatchableError].} =
let g = genesisBlockForNetwork(c.db.networkId, cn)
c.blockZeroHash = g.toBlock.blockHash
let genesisCRC = crc32(0, c.blockZeroHash.data)
c.forkIds = calculateForkIds(c.db.config, genesisCRC)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# End # End
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -47,7 +47,7 @@ proc main() {.used.} =
if canonicalHeadHashKey().toOpenArray notin trieDB: if canonicalHeadHashKey().toOpenArray notin trieDB:
persistToDb(db): persistToDb(db):
initializeEmptyDb(chainDB, CustomNetwork()) initializeEmptyDb(chainDB)
doAssert(canonicalHeadHashKey().toOpenArray in trieDB) doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
var head = chainDB.getCanonicalHead() var head = chainDB.getCanonicalHead()

View File

@ -244,7 +244,7 @@ proc newVoterPool*(networkId = GoerliNet): TesterPool =
TesterPool( TesterPool(
boot: CustomNetwork( boot: CustomNetwork(
genesis: genesisBlockForNetwork(networkId, CustomNetwork()), genesis: genesisBlockForNetwork(networkId, CustomNetwork()),
config: chainConfig(networkId))).initTesterPool config: chainConfig(networkId, CustomNetwork()))).initTesterPool
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public: getter # Public: getter

View File

@ -1,7 +1,8 @@
import unittest2, strutils, tables, os, json, import unittest2, strutils, tables, os, json,
../nimbus/utils/difficulty, stint, times, ../nimbus/utils/difficulty, stint, times,
eth/common, test_helpers, stew/byteutils, eth/common, test_helpers, stew/byteutils,
../nimbus/constants, ../nimbus/config ../nimbus/constants, ../nimbus/config,
../nimbus/chain_config
type type
Tester = object Tester = object
@ -60,11 +61,11 @@ template runTests(name: string, hex: bool, calculator: typed) =
check diff == t.currentDifficulty check diff == t.currentDifficulty
proc difficultyMain*() = proc difficultyMain*() =
let mainnetConfig = chainConfig(MainNet) let mainnetConfig = chainConfig(MainNet, CustomNetwork())
func calcDifficultyMainNetWork(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = func calcDifficultyMainNetWork(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
mainnetConfig.calcDifficulty(timeStamp, parent) mainnetConfig.calcDifficulty(timeStamp, parent)
let ropstenConfig = chainConfig(RopstenNet) let ropstenConfig = chainConfig(RopstenNet, CustomNetwork())
func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
ropstenConfig.calcDifficulty(timeStamp, parent) ropstenConfig.calcDifficulty(timeStamp, parent)

View File

@ -89,7 +89,6 @@ template runTest(network: untyped) =
chainDB = newBaseChainDB(memDB, true, network) chainDB = newBaseChainDB(memDB, true, network)
chain = newChain(chainDB) chain = newChain(chainDB)
chain.setForkId()
for x in `network IDs`: for x in `network IDs`:
let id = chain.getForkId(x.blockNumber.toBlockNumber) let id = chain.getForkId(x.blockNumber.toBlockNumber)
check id.crc == x.id.crc check id.crc == x.id.crc

View File

@ -31,28 +31,50 @@ proc toBlock(n: JsonNode, key: string): EthBlock =
let rlpBlob = hexToSeqByte(n[key].str) let rlpBlob = hexToSeqByte(n[key].str)
rlp.decode(rlpBlob, EthBlock) rlp.decode(rlpBlob, EthBlock)
proc setupChain(chainDB: BaseChainDB) = proc setupChain(): BaseChainDB =
let config = ChainConfig(
chainId : MainNet.ChainId,
byzantiumBlock : 0.toBlockNumber,
constantinopleBlock : 0.toBlockNumber,
petersburgBlock : 0.toBlockNumber,
istanbulBlock : 0.toBlockNumber,
muirGlacierBlock : 0.toBlockNumber,
berlinBlock : 10.toBlockNumber,
londonBlock : high(BlockNumber).toBlockNumber
)
var jn = json.parseFile(dataFolder / "oneUncle.json") var jn = json.parseFile(dataFolder / "oneUncle.json")
for k, v in jn: for k, v in jn:
if v["network"].str == "Istanbul": if v["network"].str == "Istanbul":
jn = v jn = v
break break
let genesis = jn.toBlock("genesisRLP") let gen = jn.toBlock("genesisRLP")
var genesis = Genesis(
let conf = getConfiguration() nonce : gen.header.nonce,
conf.customNetwork.genesis.nonce = genesis.header.nonce extraData : gen.header.extraData,
conf.customNetwork.genesis.extraData = genesis.header.extraData gasLimit : gen.header.gasLimit,
conf.customNetwork.genesis.gasLimit = genesis.header.gasLimit difficulty: gen.header.difficulty,
conf.customNetwork.genesis.difficulty = genesis.header.difficulty mixHash : gen.header.mixDigest,
conf.customNetwork.genesis.mixHash = genesis.header.mixDigest coinBase : gen.header.coinbase,
conf.customNetwork.genesis.coinBase = genesis.header.coinbase timestamp : gen.header.timestamp,
conf.customNetwork.genesis.timestamp = genesis.header.timestamp baseFeePerGas: gen.header.fee
conf.customNetwork.genesis.baseFeePerGas = genesis.header.fee )
if not parseGenesisAlloc($(jn["pre"]), conf.customNetwork.genesis.alloc): if not parseGenesisAlloc($(jn["pre"]), genesis.alloc):
quit(QuitFailure) quit(QuitFailure)
chainDB.initializeEmptyDb(conf.customNetwork) let customNetwork = CustomNetwork(
config: config,
genesis: genesis
)
let chainDB = newBaseChainDB(
newMemoryDb(),
pruneTrie = false,
CustomNet,
customNetwork
)
chainDB.initializeEmptyDb()
let blocks = jn["blocks"] let blocks = jn["blocks"]
var headers: seq[BlockHeader] var headers: seq[BlockHeader]
@ -69,29 +91,15 @@ proc setupChain(chainDB: BaseChainDB) =
let res = chain.persistBlocks(headers, bodies) let res = chain.persistBlocks(headers, bodies)
assert(res == ValidationResult.OK) assert(res == ValidationResult.OK)
proc graphqlMain*() = chainDB
let conf = getConfiguration()
conf.net.networkId = CustomNet
conf.customNetwork.config = ChainConfig(
chainId : MainNet.ChainId,
byzantiumBlock : 0.toBlockNumber,
constantinopleBlock : 0.toBlockNumber,
petersburgBlock : 0.toBlockNumber,
istanbulBlock : 0.toBlockNumber,
muirGlacierBlock : 0.toBlockNumber,
berlinBlock : 10.toBlockNumber,
londonBlock : high(BlockNumber).toBlockNumber
)
proc graphqlMain*() =
let let
conf = getConfiguration()
ethCtx = newEthContext() ethCtx = newEthContext()
ethNode = setupEthNode(conf, ethCtx, eth) ethNode = setupEthNode(conf, ethCtx, eth)
chainDB = newBaseChainDB(newMemoryDb(), chainDB = setupChain()
pruneTrie = false,
conf.net.networkId
)
chainDB.setupChain()
let ctx = setupGraphqlContext(chainDB, ethNode) let ctx = setupGraphqlContext(chainDB, ethNode)
when isMainModule: when isMainModule:
ctx.main(caseFolder, purgeSchema = false) ctx.main(caseFolder, purgeSchema = false)