From a0d10f5728bee82a0aeb34ee1aaf34d0b2132e4c Mon Sep 17 00:00:00 2001 From: jangko Date: Thu, 20 May 2021 13:01:57 +0700 Subject: [PATCH] drop PublicNetwork enum usage and replace it with NetworkId we cannot limit the `--networkid` switch to values available in `PublicNetwork` enum. it should able to accept very wide range of custom NetworkId. --- .../nodocker/consensus/consensus_sim.nim | 2 +- .../nodocker/graphql/graphql_sim.nim | 4 +- nimbus/chain_config.nim | 8 +++ nimbus/config.nim | 68 ++++++++----------- nimbus/db/db_chain.nim | 8 +-- nimbus/genesis.nim | 14 ++-- nimbus/nimbus.nim | 3 +- nimbus/vm/state.nim | 5 +- nimbus/vm2/state.nim | 5 +- premix/configuration.nim | 31 ++++++--- tests/test_difficulty.nim | 4 +- tests/test_forkid.nim | 4 +- tests/test_graphql.nim | 6 +- tests/test_rpc.nim | 2 +- tests/test_tools_build.nim | 5 +- 15 files changed, 90 insertions(+), 79 deletions(-) diff --git a/hive_integration/nodocker/consensus/consensus_sim.nim b/hive_integration/nodocker/consensus/consensus_sim.nim index 8b7fee58e..24d38d5e7 100644 --- a/hive_integration/nodocker/consensus/consensus_sim.nim +++ b/hive_integration/nodocker/consensus/consensus_sim.nim @@ -27,7 +27,7 @@ proc processNode(genesisFile, chainFile, conf = getConfiguration() chainDB = newBaseChainDB(newMemoryDb(), pruneTrie = false, - id = toPublicNetwork(conf.net.networkId) + conf.net.networkId ) initializeEmptyDb(chainDB) diff --git a/hive_integration/nodocker/graphql/graphql_sim.nim b/hive_integration/nodocker/graphql/graphql_sim.nim index d29021217..d25e8d82c 100644 --- a/hive_integration/nodocker/graphql/graphql_sim.nim +++ b/hive_integration/nodocker/graphql/graphql_sim.nim @@ -78,11 +78,11 @@ proc main() = ethNode = setupEthNode(eth) chainDB = newBaseChainDB(newMemoryDb(), pruneTrie = false, - id = toPublicNetwork(conf.net.networkId) + conf.net.networkId ) initializeEmptyDb(chainDB) - importRlpBlock(blocksFile, chainDB) + discard importRlpBlock(blocksFile, chainDB) let ctx = setupGraphqlContext(chainDB, ethNode) runTest("GraphQL", caseFolder): diff --git a/nimbus/chain_config.nim b/nimbus/chain_config.nim index 53e972afc..78d599623 100644 --- a/nimbus/chain_config.nim +++ b/nimbus/chain_config.nim @@ -53,6 +53,10 @@ type muirGlacierBlock* : BlockNumber berlinBlock* : BlockNumber + # TODO: this need to be fixed somehow + # using `real` engine configuration + poaEngine* : bool + Genesis* = object nonce* : BlockNonce timestamp* : EthTime @@ -156,6 +160,10 @@ proc loadCustomGenesis*(fileName: string, cg: var CustomGenesis): bool = cg.config.daoForkSupport = cc.config.daoForkSupport cg.config.eip150Hash = cc.config.eip150Hash + # TODO: this need to be fixed somehow + # using `real` engine configuration + cg.config.poaEngine = false + template validateFork(forkName: untyped, nextBlock: BlockNumber) = let fork = astToStr(forkName) if cc.config.forkName.isSome: diff --git a/nimbus/config.nim b/nimbus/config.nim index 13fce062c..0f341b762 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -75,19 +75,11 @@ type enabled*: bool address*: TransportAddress - PublicNetwork* = enum - CustomNet = 0 - MainNet = 1 - # No longer used: MordenNet = 2 - RopstenNet = 3 - RinkebyNet = 4 - GoerliNet = 5 - KovanNet = 42 - NetworkFlags* = enum ## Ethereum network flags - NoDiscover, ## Peer discovery disabled - V5Discover, ## Dicovery V5 enabled + NoDiscover ## Peer discovery disabled + V5Discover ## Dicovery V5 enabled + NetworkIdSet ## prevent CustomNetwork replacement DebugFlags* {.pure.} = enum ## Debug selection flags @@ -148,6 +140,16 @@ type accounts*: Table[EthAddress, NimbusAccount] importFile*: string +const + # these are public network id + CustomNet* = 0.NetworkId + MainNet* = 1.NetworkId + # No longer used: MordenNet = 2 + RopstenNet* = 3.NetworkId + RinkebyNet* = 4.NetworkId + GoerliNet* = 5.NetworkId + KovanNet* = 42.NetworkId + const defaultRpcApi = {RpcFlags.Eth, RpcFlags.Shh} defaultProtocols = {ProtocolFlags.Eth, ProtocolFlags.Shh} @@ -172,7 +174,7 @@ proc toFork*(c: ChainConfig, number: BlockNumber): Fork = elif number >= c.homesteadBlock: FkHomestead else: FkFrontier -proc publicChainConfig*(id: PublicNetwork): ChainConfig = +proc chainConfig*(id: NetworkId): ChainConfig = # For some public networks, NetworkId and ChainId value are identical # but that is not always the case @@ -242,14 +244,11 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig = muirGlacierBlock: 4_460_644.toBlockNumber, # never occured in goerli network berlinBlock: 4_460_644.toBlockNumber ) - of CustomNet: + else: + # everything else will use CustomNet config let conf = getConfiguration() trace "Custom genesis block configuration loaded", conf=conf.customGenesis.config conf.customGenesis.config - else: - error "No chain config for public network", networkId = id - doAssert(false, "No chain config for " & $id) - ChainConfig() proc processList(v: string, o: var seq[string]) = ## Process comma-separated list of strings. @@ -418,18 +417,9 @@ proc setBootnodes(onodes: var seq[ENode], nodeUris: openarray[string]) = doAssert(processENode(item, node) == Success) onodes.add(node) -macro availableEnumValues(T: type enum): untyped = - let impl = getTypeImpl(T)[1].getTypeImpl() - result = newNimNode(nnkBracket) - for i in 1 ..< impl.len: result.add(newCall("NetworkId", copyNimTree(impl[i]))) - -proc toPublicNetwork*(id: NetworkId): PublicNetwork {.inline.} = - if id in availableEnumValues(PublicNetwork): - result = PublicNetwork(id) - -proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) = +proc setNetwork(conf: var NetConfiguration, id: NetworkId) = ## Set network id and default network bootnodes - conf.networkId = NetworkId(id) + conf.networkId = id case id of MainNet: conf.bootNodes.setBootnodes(MainnetBootnodes) @@ -441,16 +431,10 @@ proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) = conf.bootNodes.setBootnodes(GoerliBootnodes) of KovanNet: conf.bootNodes.setBootnodes(KovanBootnodes) - of CustomNet: - discard - -proc setNetwork(conf: var NetConfiguration, id: NetworkId) = - ## Set network id and default network bootnodes - let pubNet = toPublicNetwork(id) - if pubNet == CustomNet: - conf.networkId = id else: - conf.setNetwork(pubNet) + # everything else will use bootnodes + # from --bootnodes switch + discard proc processNetArguments(key, value: string): ConfigStatus = ## Processes only `Networking` related command line options @@ -480,12 +464,18 @@ proc processNetArguments(key, value: string): ConfigStatus = elif skey == "customnetwork": if not loadCustomGenesis(value, config.customGenesis): result = Error - config.net.networkId = NetworkId(CustomNet) + if NetworkIdSet notin config.net.flags: + # prevent clash with --networkid if it already set + # because any --networkid value that is not + # in the public network will also translated as + # CustomNetwork + config.net.networkId = CustomNet elif skey == "networkid": var res = 0 result = processInteger(value, res) if result == Success: config.net.setNetwork(NetworkId(result)) + config.net.flags.incl NetworkIdSet elif skey == "nodiscover": config.net.flags.incl(NoDiscover) elif skey == "v5discover": @@ -655,7 +645,7 @@ proc initConfiguration(): NimbusConfiguration = result.rpc.binds = @[initTAddress("127.0.0.1:8545")] ## Network defaults - result.net.setNetwork(defaultNetwork.NetworkId) + result.net.setNetwork(defaultNetwork) result.net.maxPeers = 25 result.net.maxPendingPeers = 0 result.net.bindPort = 30303'u16 diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index 9019b1d4a..7c652c71b 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -8,7 +8,7 @@ import sequtils, algorithm, stew/[byteutils], eth/trie/[hexary, db], - eth/[common, rlp], chronicles, + eth/[common, rlp, p2p], chronicles, ../errors, ../constants, ./storage_types, ../utils, ../config, ../chain_config @@ -17,7 +17,7 @@ type db* : TrieDatabaseRef pruneTrie*: bool config* : ChainConfig - networkId*: PublicNetwork + networkId*: NetworkId # startingBlock, currentBlock, and highestBlock # are progress indicator @@ -29,11 +29,11 @@ type blockNumber: BlockNumber index: int -proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true, id: PublicNetwork = MainNet): BaseChainDB = +proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true, id: NetworkId = MainNet): BaseChainDB = new(result) result.db = db result.pruneTrie = pruneTrie - result.config = publicChainConfig(id) + result.config = chainConfig(id) result.networkId = id proc `$`*(db: BaseChainDB): string = diff --git a/nimbus/genesis.nim b/nimbus/genesis.nim index 1922e9e62..2bebfb602 100644 --- a/nimbus/genesis.nim +++ b/nimbus/genesis.nim @@ -1,12 +1,12 @@ import std/[json, strutils, times, tables], - eth/[common, rlp, trie], stew/[byteutils], + eth/[common, rlp, trie, p2p], stew/[byteutils], chronicles, eth/trie/db, ./db/[db_chain, state_db], ./genesis_alloc, ./config, ./constants, ./chain_config -proc defaultGenesisBlockForNetwork*(id: PublicNetwork): Genesis = +proc defaultGenesisBlockForNetwork*(id: NetworkId): Genesis = result = case id of MainNet: Genesis( @@ -42,14 +42,10 @@ proc defaultGenesisBlockForNetwork*(id: PublicNetwork): Genesis = difficulty: 1.u256, alloc: decodePrealloc(goerliAllocData) ) - of CustomNet: + else: + # everything else will use custom genesis let customGenesis = getConfiguration().customGenesis customGenesis.genesis - else: - # TODO: Fill out the rest - error "No default genesis for network", id - doAssert(false, "No default genesis for " & $id) - Genesis() proc toBlock*(g: Genesis, db: BaseChainDB = nil): BlockHeader = let (tdb, pruneTrie) = if db.isNil: (newMemoryDB(), true) @@ -91,5 +87,5 @@ proc commit*(g: Genesis, db: BaseChainDB) = proc initializeEmptyDb*(db: BaseChainDB) = trace "Writing genesis to DB" - let networkId = getConfiguration().net.networkId.toPublicNetwork() + let networkId = getConfiguration().net.networkId defaultGenesisBlockForNetwork(networkId).commit(db) diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index bf7c2c4f5..790737501 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -52,7 +52,8 @@ proc start(nimbus: NimbusNode) = let trieDB = trieDB newChainDb(conf.dataDir) var chainDB = newBaseChainDB(trieDB, conf.prune == PruneMode.Full, - conf.net.networkId.toPublicNetwork()) + conf.net.networkId + ) chainDB.populateProgress() if canonicalHeadHashKey().toOpenArray notin trieDB: diff --git a/nimbus/vm/state.nim b/nimbus/vm/state.nim index 9e80eab8e..554c462f5 100644 --- a/nimbus/vm/state.nim +++ b/nimbus/vm/state.nim @@ -69,9 +69,10 @@ proc setupTxContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt, vmState.gasCosts = vmState.fork.forkToSchedule proc consensusEnginePoA*(vmState: BaseVMState): bool = - let networkId = vmState.chainDB.networkId # PoA consensus engine have no reward for miner - result = networkId in {GoerliNet, RinkebyNet, KovanNet} + # TODO: this need to be fixed somehow + # using `real` engine configuration + vmState.chainDB.config.poaEngine proc getSignature(bytes: openArray[byte], output: var Signature): bool = let sig = Signature.fromRaw(bytes) diff --git a/nimbus/vm2/state.nim b/nimbus/vm2/state.nim index b5b7434f7..bf474d0e3 100644 --- a/nimbus/vm2/state.nim +++ b/nimbus/vm2/state.nim @@ -57,9 +57,10 @@ proc newBaseVMState*(prevStateRoot: Hash256, result.init(prevStateRoot, header, chainDB, tracerFlags) proc consensusEnginePoA*(vmState: BaseVMState): bool = - let networkId = vmState.chainDB.networkId # PoA consensus engine have no reward for miner - result = networkId in {GoerliNet, RinkebyNet, KovanNet} + # TODO: this need to be fixed somehow + # using `real` engine configuration + vmState.chainDB.config.poaEngine proc getSignature(bytes: openArray[byte], output: var Signature): bool = let sig = Signature.fromRaw(bytes) diff --git a/premix/configuration.nim b/premix/configuration.nim index 3e44a8a4a..ecbaf9b1d 100644 --- a/premix/configuration.nim +++ b/premix/configuration.nim @@ -1,5 +1,16 @@ -import stint, os, parseopt, strutils -from ../nimbus/config import getDefaultDataDir, ConfigStatus, processInteger, PublicNetwork +import + std/[os, parseopt, strutils], + eth/p2p, stint + +from ../nimbus/config import + getDefaultDataDir, + ConfigStatus, + processInteger, + MainNet, + RopstenNet, + RinkebyNet, + GoerliNet, + KovanNet export ConfigStatus @@ -9,7 +20,7 @@ type head*: Uint256 maxBlocks*: int numCommits*: int - netId*: PublicNetwork + netId*: NetworkId var premixConfig {.threadvar.}: PremixConfiguration @@ -38,7 +49,7 @@ proc processU256(val: string, o: var Uint256): ConfigStatus = o = parse(val, Uint256) result = Success -proc processNetId(val: string, o: var PublicNetwork): ConfigStatus = +proc processNetId(val: string, o: var NetworkId): ConfigStatus = case val.toLowerAscii() of "main": o = MainNet of "ropsten": o = RopstenNet @@ -46,9 +57,9 @@ proc processNetId(val: string, o: var PublicNetwork): ConfigStatus = of "goerli": o = GoerliNet of "kovan": o = KovanNet -template checkArgument(fun, o: untyped) = +template checkArgument(fun, o, value: untyped) = ## Checks if arguments got processed successfully - var res = (fun)(value, o) + let res = fun(value, o) if res == Success: continue elif res == ErrorParseOption: @@ -76,14 +87,14 @@ proc processArguments*(msg: var string): ConfigStatus = case key.toLowerAscii() of "datadir": config.dataDir = value of "maxblocks": - checkArgument processInteger, config.maxBlocks + checkArgument(processInteger, config.maxBlocks, value) of "head": - checkArgument processU256, config.head + checkArgument(processU256, config.head, value) of "numcommits": - checkArgument processInteger, config.numCommits + checkArgument(processInteger, config.numCommits, value) config.numCommits = max(config.numCommits, 512) of "netid": - checkArgument processNetId, config.netId + checkArgument(processNetId, config.netId, value) else: msg = "Unknown option " & key if value.len > 0: msg = msg & " : " & value diff --git a/tests/test_difficulty.nim b/tests/test_difficulty.nim index b63cc68ac..682ba7cef 100644 --- a/tests/test_difficulty.nim +++ b/tests/test_difficulty.nim @@ -61,11 +61,11 @@ template runTests(name: string, hex: bool, calculator: typed) = check diff == t.currentDifficulty proc difficultyMain*() = - let mainnetConfig = publicChainConfig(MainNet) + let mainnetConfig = chainConfig(MainNet) func calcDifficultyMainNetWork(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = mainnetConfig.calcDifficulty(timeStamp, parent) - let ropstenConfig = publicChainConfig(RopstenNet) + let ropstenConfig = chainConfig(RopstenNet) func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt = ropstenConfig.calcDifficulty(timeStamp, parent) diff --git a/tests/test_forkid.nim b/tests/test_forkid.nim index 3f7d1edff..607afed19 100644 --- a/tests/test_forkid.nim +++ b/tests/test_forkid.nim @@ -1,5 +1,5 @@ import - unittest2, eth/common, eth/trie/db, + unittest2, eth/[common, p2p], eth/trie/db, ../nimbus/db/db_chain, ../nimbus/p2p/chain, ../nimbus/config @@ -74,7 +74,7 @@ const (blockNumber: 5000000'u64, id: (crc: 0x757a1c47'u32, nextFork: 0'u64)), # Future Berlin block ] -template runTest(network: PublicNetwork) = +template runTest(network: untyped) = test network.astToStr: var memDB = newMemoryDB() diff --git a/tests/test_graphql.nim b/tests/test_graphql.nim index 6ea792ef8..0be5e3a23 100644 --- a/tests/test_graphql.nim +++ b/tests/test_graphql.nim @@ -49,7 +49,7 @@ proc setupChain(chainDB: BaseChainDB) = conf.customGenesis.genesis.timestamp = genesis.header.timestamp if not parseGenesisAlloc($(jn["pre"]), conf.customGenesis.genesis.alloc): quit(QuitFailure) - + chainDB.initializeEmptyDb() let blocks = jn["blocks"] @@ -69,7 +69,7 @@ proc setupChain(chainDB: BaseChainDB) = proc graphqlMain*() = let conf = getConfiguration() - conf.net.networkId = NetworkId(CustomNet) + conf.net.networkId = CustomNet conf.customGenesis.config = ChainConfig( chainId : MainNet.ChainId, byzantiumBlock : 0.toBlockNumber, @@ -84,7 +84,7 @@ proc graphqlMain*() = ethNode = setupEthNode(eth) chainDB = newBaseChainDB(newMemoryDb(), pruneTrie = false, - id = toPublicNetwork(conf.net.networkId) + conf.net.networkId ) chainDB.setupChain() diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index bd859cf49..219e23d32 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -142,7 +142,7 @@ proc doTests {.async.} = debugEcho unlock.error doAssert(unlock.isOk) - defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain) + defaultGenesisBlockForNetwork(conf.net.networkId).commit(chain) doAssert(canonicalHeadHashKey().toOpenArray in chain.db) let env = setupEnv(chain, signer, ks2, conf) diff --git a/tests/test_tools_build.nim b/tests/test_tools_build.nim index 86f17d1eb..c8f1fa316 100644 --- a/tests/test_tools_build.nim +++ b/tests/test_tools_build.nim @@ -19,4 +19,7 @@ import ../premix/hunter, ../premix/regress, ./tracerTestGen, - ./persistBlockTestGen + ./persistBlockTestGen, + ../hive_integration/nodocker/consensus/extract_consensus_data, + ../hive_integration/nodocker/consensus/consensus_sim, + ../hive_integration/nodocker/graphql/graphql_sim