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()
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

View File

@ -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)

View File

@ -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.

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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
# ------------------------------------------------------------------------------

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)