From 48d497580a3921d307d2d9a0651783e78fed3aa5 Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 8 Sep 2021 20:28:17 +0700 Subject: [PATCH] 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 --- .../nodocker/consensus/consensus_sim.nim | 5 +- .../nodocker/graphql/graphql_sim.nim | 5 +- nimbus/config.nim | 9 ++- nimbus/db/db_chain.nim | 11 ++- nimbus/genesis.nim | 4 +- nimbus/nimbus.nim | 8 +-- nimbus/p2p/chain/chain_desc.nim | 15 ++-- premix/persist.nim | 2 +- tests/test_clique/pool.nim | 2 +- tests/test_difficulty.nim | 7 +- tests/test_forkid.nim | 1 - tests/test_graphql.nim | 72 ++++++++++--------- 12 files changed, 79 insertions(+), 62 deletions(-) diff --git a/hive_integration/nodocker/consensus/consensus_sim.nim b/hive_integration/nodocker/consensus/consensus_sim.nim index 063cb10e8..738183597 100644 --- a/hive_integration/nodocker/consensus/consensus_sim.nim +++ b/hive_integration/nodocker/consensus/consensus_sim.nim @@ -27,10 +27,11 @@ proc processNode(genesisFile, chainFile, conf = getConfiguration() chainDB = newBaseChainDB(newMemoryDb(), pruneTrie = false, - conf.net.networkId + conf.net.networkId, + conf.customNetwork ) - initializeEmptyDb(chainDB, conf.customNetwork) + initializeEmptyDb(chainDB) discard importRlpBlock(chainFile, chainDB) let head = chainDB.getCanonicalHead() let blockHash = "0x" & head.blockHash.data.toHex diff --git a/hive_integration/nodocker/graphql/graphql_sim.nim b/hive_integration/nodocker/graphql/graphql_sim.nim index 90f132bf7..27d8f03bd 100644 --- a/hive_integration/nodocker/graphql/graphql_sim.nim +++ b/hive_integration/nodocker/graphql/graphql_sim.nim @@ -79,10 +79,11 @@ proc main() = ethNode = setupEthNode(conf, ethCtx, eth) chainDB = newBaseChainDB(newMemoryDb(), pruneTrie = false, - conf.net.networkId + conf.net.networkId, + conf.customNetwork ) - initializeEmptyDb(chainDB, conf.customNetwork) + initializeEmptyDb(chainDB) discard importRlpBlock(blocksFile, chainDB) let ctx = setupGraphqlContext(chainDB, ethNode) diff --git a/nimbus/config.nim b/nimbus/config.nim index f37f7cf8d..83f17d237 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -166,7 +166,7 @@ proc toFork*(c: ChainConfig, number: BlockNumber): Fork = elif number >= c.homesteadBlock: FkHomestead else: FkFrontier -proc chainConfig*(id: NetworkId): ChainConfig = +proc chainConfig*(id: NetworkId, cn: CustomNetwork): ChainConfig = # For some public networks, NetworkId and ChainId value are identical # but that is not always the case @@ -245,10 +245,9 @@ proc chainConfig*(id: NetworkId): ChainConfig = londonBlock: 5_062_605.toBlockNumber # June 30, 2021 ) else: - # everything else will use CustomNet config - let conf = getConfiguration() - trace "Custom genesis block configuration loaded", conf=conf.customNetwork.config - conf.customNetwork.config + # everything else will use CustomNet config + trace "Custom genesis block configuration loaded", conf=cn.config + cn.config proc processList(v: string, o: var seq[string]) = ## Process comma-separated list of strings. diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index 7c652c71b..a74538419 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -18,6 +18,7 @@ type pruneTrie*: bool config* : ChainConfig networkId*: NetworkId + customNetwork*: CustomNetwork # startingBlock, currentBlock, and highestBlock # are progress indicator @@ -29,12 +30,18 @@ type blockNumber: BlockNumber 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) result.db = db result.pruneTrie = pruneTrie - result.config = chainConfig(id) + result.config = chainConfig(id, cn) result.networkId = id + result.customNetwork = cn proc `$`*(db: BaseChainDB): string = result = "BaseChainDB" diff --git a/nimbus/genesis.nim b/nimbus/genesis.nim index f647d5ef6..1db978ef8 100644 --- a/nimbus/genesis.nim +++ b/nimbus/genesis.nim @@ -89,7 +89,7 @@ proc commit*(g: Genesis, db: BaseChainDB) = doAssert(b.blockNumber == 0, "can't commit genesis block with number > 0") discard db.persistHeaderToDb(b) -proc initializeEmptyDb*(db: BaseChainDB, cn: CustomNetwork) = +proc initializeEmptyDb*(db: BaseChainDB) = trace "Writing genesis to DB" - let genesis = genesisBlockForNetwork(db.networkId, cn) + let genesis = genesisBlockForNetwork(db.networkId, db.customNetwork) genesis.commit(db) diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index 80de25950..4ec15f21f 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -21,7 +21,7 @@ import config, genesis, rpc/[common, p2p, debug], p2p/chain, eth/trie/db, metrics, metrics/[chronos_httpserver, chronicles_support], graphql/ethapi, context, - "."/[utils, conf_utils, sealer, constants] + "."/[conf_utils, sealer, constants] ## TODO: ## * No IPv6 support @@ -54,12 +54,13 @@ proc start(nimbus: NimbusNode) = let trieDB = trieDB newChainDb(conf.dataDir) var chainDB = newBaseChainDB(trieDB, conf.prune == PruneMode.Full, - conf.net.networkId + conf.net.networkId, + conf.customNetwork ) chainDB.populateProgress() if canonicalHeadHashKey().toOpenArray notin trieDB: - initializeEmptyDb(chainDb, conf.customNetwork) + initializeEmptyDb(chainDb) doAssert(canonicalHeadHashKey().toOpenArray in trieDB) if conf.importFile.len > 0: @@ -134,7 +135,6 @@ proc start(nimbus: NimbusNode) = # chainRef: some name to avoid module-name/filed/function misunderstandings let chainRef = newChain(chainDB) - chainRef.setForkId(conf.customNetwork) nimbus.ethNode.chain = chainRef if conf.verifyFromOk: chainRef.extraValidation = 0 < conf.verifyFrom diff --git a/nimbus/p2p/chain/chain_desc.nim b/nimbus/p2p/chain/chain_desc.nim index 47c3d6929..061cbb6c4 100644 --- a/nimbus/p2p/chain/chain_desc.nim +++ b/nimbus/p2p/chain/chain_desc.nim @@ -115,6 +115,13 @@ func calculateForkIds(c: ChainConfig, prevFork = result[fork].nextFork 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 # ------------------------------------------------------------------------------ @@ -129,6 +136,7 @@ proc initChain(c: Chain; db: BaseChainDB; poa: Clique; extraValidation: bool) if not db.config.daoForkSupport: db.config.daoForkBlock = db.config.homesteadBlock c.extraValidation = extraValidation + c.setForkId(db.customNetwork) # Initalise the PoA state regardless of whether it is needed on the current # network. For non-PoA networks (when `db.config.poaEngine` is `false`), @@ -237,13 +245,6 @@ proc `verifyFrom=`*(c: Chain; verifyFrom: uint64) {.inline.} = ## `true`. 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 # ------------------------------------------------------------------------------ diff --git a/premix/persist.nim b/premix/persist.nim index 78515175a..c690c9cc7 100644 --- a/premix/persist.nim +++ b/premix/persist.nim @@ -47,7 +47,7 @@ proc main() {.used.} = if canonicalHeadHashKey().toOpenArray notin trieDB: persistToDb(db): - initializeEmptyDb(chainDB, CustomNetwork()) + initializeEmptyDb(chainDB) doAssert(canonicalHeadHashKey().toOpenArray in trieDB) var head = chainDB.getCanonicalHead() diff --git a/tests/test_clique/pool.nim b/tests/test_clique/pool.nim index 0fe98b13e..02b244ca4 100644 --- a/tests/test_clique/pool.nim +++ b/tests/test_clique/pool.nim @@ -244,7 +244,7 @@ proc newVoterPool*(networkId = GoerliNet): TesterPool = TesterPool( boot: CustomNetwork( genesis: genesisBlockForNetwork(networkId, CustomNetwork()), - config: chainConfig(networkId))).initTesterPool + config: chainConfig(networkId, CustomNetwork()))).initTesterPool # ------------------------------------------------------------------------------ # Public: getter diff --git a/tests/test_difficulty.nim b/tests/test_difficulty.nim index 8bbfe05fd..5bbf5663b 100644 --- a/tests/test_difficulty.nim +++ b/tests/test_difficulty.nim @@ -1,7 +1,8 @@ import unittest2, strutils, tables, os, json, ../nimbus/utils/difficulty, stint, times, eth/common, test_helpers, stew/byteutils, - ../nimbus/constants, ../nimbus/config + ../nimbus/constants, ../nimbus/config, + ../nimbus/chain_config type Tester = object @@ -60,11 +61,11 @@ template runTests(name: string, hex: bool, calculator: typed) = check diff == t.currentDifficulty proc difficultyMain*() = - let mainnetConfig = chainConfig(MainNet) + let mainnetConfig = chainConfig(MainNet, CustomNetwork()) func calcDifficultyMainNetWork(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = mainnetConfig.calcDifficulty(timeStamp, parent) - let ropstenConfig = chainConfig(RopstenNet) + let ropstenConfig = chainConfig(RopstenNet, CustomNetwork()) func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = ropstenConfig.calcDifficulty(timeStamp, parent) diff --git a/tests/test_forkid.nim b/tests/test_forkid.nim index 633168878..eea578c9e 100644 --- a/tests/test_forkid.nim +++ b/tests/test_forkid.nim @@ -89,7 +89,6 @@ template runTest(network: untyped) = chainDB = newBaseChainDB(memDB, true, network) chain = newChain(chainDB) - chain.setForkId() for x in `network IDs`: let id = chain.getForkId(x.blockNumber.toBlockNumber) check id.crc == x.id.crc diff --git a/tests/test_graphql.nim b/tests/test_graphql.nim index 2b02f422b..45c42c5ff 100644 --- a/tests/test_graphql.nim +++ b/tests/test_graphql.nim @@ -31,28 +31,50 @@ proc toBlock(n: JsonNode, key: string): EthBlock = let rlpBlob = hexToSeqByte(n[key].str) 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") for k, v in jn: if v["network"].str == "Istanbul": jn = v break - let genesis = jn.toBlock("genesisRLP") - - let conf = getConfiguration() - conf.customNetwork.genesis.nonce = genesis.header.nonce - conf.customNetwork.genesis.extraData = genesis.header.extraData - conf.customNetwork.genesis.gasLimit = genesis.header.gasLimit - conf.customNetwork.genesis.difficulty = genesis.header.difficulty - conf.customNetwork.genesis.mixHash = genesis.header.mixDigest - conf.customNetwork.genesis.coinBase = genesis.header.coinbase - conf.customNetwork.genesis.timestamp = genesis.header.timestamp - conf.customNetwork.genesis.baseFeePerGas = genesis.header.fee - if not parseGenesisAlloc($(jn["pre"]), conf.customNetwork.genesis.alloc): + let gen = jn.toBlock("genesisRLP") + var genesis = Genesis( + nonce : gen.header.nonce, + extraData : gen.header.extraData, + gasLimit : gen.header.gasLimit, + difficulty: gen.header.difficulty, + mixHash : gen.header.mixDigest, + coinBase : gen.header.coinbase, + timestamp : gen.header.timestamp, + baseFeePerGas: gen.header.fee + ) + if not parseGenesisAlloc($(jn["pre"]), genesis.alloc): 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"] var headers: seq[BlockHeader] @@ -69,29 +91,15 @@ proc setupChain(chainDB: BaseChainDB) = let res = chain.persistBlocks(headers, bodies) assert(res == ValidationResult.OK) -proc graphqlMain*() = - 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 - ) + chainDB +proc graphqlMain*() = let + conf = getConfiguration() ethCtx = newEthContext() ethNode = setupEthNode(conf, ethCtx, eth) - chainDB = newBaseChainDB(newMemoryDb(), - pruneTrie = false, - conf.net.networkId - ) + chainDB = setupChain() - chainDB.setupChain() let ctx = setupGraphqlContext(chainDB, ethNode) when isMainModule: ctx.main(caseFolder, purgeSchema = false)