From 9a8e81ceec20369f27da9b531a5ec64a6c99292a Mon Sep 17 00:00:00 2001 From: Viktor Kirilov Date: Tue, 1 Sep 2020 12:01:57 +0300 Subject: [PATCH] added the --network= option to the tools for which it matters --- beacon_chain/beacon_node.nim | 38 ++++---------------------- beacon_chain/deposit_contract.nim | 7 ++++- beacon_chain/network_metadata.nim | 44 +++++++++++++++++++++++++++++-- ncli/ncli.nim | 9 ++++++- ncli/ncli_db.nim | 22 ++++++++++------ 5 files changed, 75 insertions(+), 45 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 5b833b6b9..af058c80f 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -1038,35 +1038,7 @@ programMain: setupLogging(config.logLevel, config.logFile) if config.eth2Network.isSome: - let - networkName = config.eth2Network.get - metadata = case toLowerAscii(networkName) - of "mainnet": - mainnetMetadata - of "altona": - altonaMetadata - of "medalla": - medallaMetadata - of "testnet0": - testnet0Metadata - of "testnet1": - testnet1Metadata - else: - if fileExists(networkName): - try: - Json.loadFile(networkName, Eth2NetworkMetadata) - except SerializationError as err: - echo err.formatMsg(networkName) - quit 1 - else: - fatal "Unrecognized network name", networkName - quit 1 - - if metadata.incompatible: - fatal "The selected network is not compatible with the current build", - reason = metadata.incompatibilityDesc - quit 1 - + let metadata = getMetadataForNetwork(config.eth2Network.get) config.runtimePreset = metadata.runtimePreset if config.cmd == noCommand: @@ -1083,7 +1055,7 @@ programMain: # regular command-line options (that may conflict). if config.fieldName.isSome: fatal "Invalid CLI arguments specified. You must not specify '--network' and '" & flagName & "' at the same time", - networkParam = networkName, `flagName` = config.fieldName.get + networkParam = config.eth2Network.get, `flagName` = config.fieldName.get quit 1 checkForIncompatibleOption "deposit-contract", depositContractAddress @@ -1120,7 +1092,7 @@ programMain: else: (waitFor getEth1BlockHash(config.web3Url, blockId("latest"))).asEth2Digest var initialState = initialize_beacon_state_from_eth1( - defaultRuntimePreset, eth1Hash, startTime, deposits, {skipBlsValidation}) + config.runtimePreset, eth1Hash, startTime, deposits, {skipBlsValidation}) # https://github.com/ethereum/eth2.0-pm/tree/6e41fcf383ebeb5125938850d8e9b4e9888389b4/interop/mocked_start#create-genesis-state initialState.genesis_time = startTime @@ -1140,7 +1112,7 @@ programMain: if bootstrapFile.len > 0: let networkKeys = getPersistentNetKeys(rng[], config) - metadata = getPersistentNetMetadata(config) + netMetadata = getPersistentNetMetadata(config) bootstrapEnr = enr.Record.init( 1, # sequence number networkKeys.seckey.asEthKey, @@ -1148,7 +1120,7 @@ programMain: config.bootstrapPort, config.bootstrapPort, [toFieldPair("eth2", SSZ.encode(enrForkIdFromState initialState[])), - toFieldPair("attnets", SSZ.encode(metadata.attnets))]) + toFieldPair("attnets", SSZ.encode(netMetadata.attnets))]) writeFile(bootstrapFile, bootstrapEnr.tryGet().toURI) echo "Wrote ", bootstrapFile diff --git a/beacon_chain/deposit_contract.nim b/beacon_chain/deposit_contract.nim index 42e71d52a..82fb88908 100644 --- a/beacon_chain/deposit_contract.nim +++ b/beacon_chain/deposit_contract.nim @@ -1,6 +1,7 @@ import os, sequtils, strutils, options, json, terminal, random, chronos, chronicles, confutils, stint, json_serialization, + ../beacon_chain/network_metadata, web3, web3/confutils_defs, eth/keys, spec/[datatypes, crypto, presets], ssz/merkleization, keystore_management @@ -34,6 +35,10 @@ type desc: "Ask for an Eth1 private key interactively" name: "ask-for-key" }: bool + eth2Network* {. + desc: "The Eth2 network preset to use" + name: "network" }: Option[string] + case cmd* {.command.}: StartUpCommand of deploy: discard @@ -171,7 +176,7 @@ proc main() {.async.} = if cfg.cmd == StartUpCommand.generateSimulationDeposits: let mnemonic = generateMnemonic(rng[]) - runtimePreset = defaultRuntimePreset + runtimePreset = getRuntimePresetForNetwork(cfg.eth2Network) createDir(string cfg.outValidatorsDir) createDir(string cfg.outSecretsDir) diff --git a/beacon_chain/network_metadata.nim b/beacon_chain/network_metadata.nim index 76596aeea..b0fb7a54f 100644 --- a/beacon_chain/network_metadata.nim +++ b/beacon_chain/network_metadata.nim @@ -1,8 +1,12 @@ import - tables, strutils, os, options, + tables, strutils, os, stew/shims/macros, nimcrypto/hash, web3/[ethtypes, conversions], - spec/presets + chronicles, + spec/presets, + spec/datatypes, + json_serialization, + json_serialization/std/[options, sets, net], serialization/errors # ATTENTION! This file will produce a large C file, because we are inlining # genesis states as C literals in the generated code (and blobs in the final @@ -166,3 +170,39 @@ const testnet1Metadata* = loadEth2NetworkMetadata( currentSourcePath.parentDir / ".." / "vendor" / "eth2-testnets" / "nimbus" / "testnet1") +{.pop.} # the following pocedures raise more than just `Defect` + +proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata = + let + metadata = case toLowerAscii(networkName) + of "mainnet": + mainnetMetadata + of "altona": + altonaMetadata + of "medalla": + medallaMetadata + of "testnet0": + testnet0Metadata + of "testnet1": + testnet1Metadata + else: + if fileExists(networkName): + try: + Json.loadFile(networkName, Eth2NetworkMetadata) + except SerializationError as err: + echo err.formatMsg(networkName) + quit 1 + else: + fatal "Unrecognized network name", networkName + quit 1 + + if metadata.incompatible: + fatal "The selected network is not compatible with the current build", + reason = metadata.incompatibilityDesc + quit 1 + return metadata + +proc getRuntimePresetForNetwork*(eth2Network: Option[string]): RuntimePreset = + if eth2Network.isSome: + return getMetadataForNetwork(eth2Network.get).runtimePreset + return defaultRuntimePreset diff --git a/ncli/ncli.nim b/ncli/ncli.nim index 1361bffb1..f8e1f5886 100644 --- a/ncli/ncli.nim +++ b/ncli/ncli.nim @@ -3,6 +3,7 @@ import stew/byteutils, ../beacon_chain/spec/[crypto, datatypes, digest, state_transition], ../beacon_chain/extras, + ../beacon_chain/network_metadata, ../beacon_chain/ssz/[merkleization, ssz_serialization] type @@ -12,6 +13,11 @@ type transition = "Run state transition function" NcliConf* = object + + eth2Network* {. + desc: "The Eth2 network preset to use" + name: "network" }: Option[string] + # TODO confutils argument pragma doesn't seem to do much; also, the cases # are largely equivalent, but this helps create command line usage text case cmd* {.command}: Cmd @@ -61,7 +67,8 @@ proc doTransition(conf: NcliConf) = stateY.root = hash_tree_root(stateY.data) - if not state_transition(defaultRuntimePreset, stateY[], blckX, flags, noRollback): + if not state_transition(getRuntimePresetForNetwork(conf.eth2Network), + stateY[], blckX, flags, noRollback): error "State transition failed" quit 1 else: diff --git a/ncli/ncli_db.nim b/ncli/ncli_db.nim index 82a2ca96c..179813cc8 100644 --- a/ncli/ncli_db.nim +++ b/ncli/ncli_db.nim @@ -1,6 +1,7 @@ import confutils, stats, chronicles, strformat, tables, stew/byteutils, + ../beacon_chain/network_metadata, ../beacon_chain/[beacon_chain_db, extras], ../beacon_chain/block_pools/[chain_dag], ../beacon_chain/spec/[crypto, datatypes, digest, helpers, @@ -31,6 +32,10 @@ type desc: "Directory where `nbc.sqlite` is stored" name: "db" }: InputDir + eth2Network* {. + desc: "The Eth2 network preset to use" + name: "network" }: Option[string] + case cmd* {. command desc: "" @@ -63,7 +68,7 @@ type argument desc: "Slot".}: uint64 -proc cmdBench(conf: DbConf) = +proc cmdBench(conf: DbConf, runtimePreset: RuntimePreset) = var timers: array[Timers, RunningStat] echo "Opening database..." @@ -79,7 +84,7 @@ proc cmdBench(conf: DbConf) = echo "Initializing block pool..." let pool = withTimerRet(timers[tInit]): - ChainDAGRef.init(defaultRuntimePreset, db, {}) + ChainDAGRef.init(runtimePreset, db, {}) echo &"Loaded {pool.blocks.len} blocks, head slot {pool.head.slot}" @@ -111,7 +116,7 @@ proc cmdBench(conf: DbConf) = isEpoch = state[].data.get_current_epoch() != b.message.slot.compute_epoch_at_slot withTimer(timers[if isEpoch: tApplyEpochBlock else: tApplyBlock]): - if not state_transition(defaultRuntimePreset, state[], b, {}, noRollback): + if not state_transition(runtimePreset, state[], b, {}, noRollback): dump("./", b) echo "State transition failed (!)" quit 1 @@ -152,7 +157,7 @@ proc cmdDumpBlock(conf: DbConf) = except CatchableError as e: echo "Couldn't load ", blockRoot, ": ", e.msg -proc cmdRewindState(conf: DbConf) = +proc cmdRewindState(conf: DbConf, runtimePreset: RuntimePreset) = echo "Opening database..." let db = BeaconChainDB.init( @@ -163,7 +168,7 @@ proc cmdRewindState(conf: DbConf) = quit 1 echo "Initializing block pool..." - let dag = init(ChainDAGRef, defaultRuntimePreset, db) + let dag = init(ChainDAGRef, runtimePreset, db) let blckRef = dag.getRef(fromHex(Eth2Digest, conf.blockRoot)) if blckRef == nil: @@ -175,15 +180,16 @@ proc cmdRewindState(conf: DbConf) = dump("./", hashedState, blck) when isMainModule: - let + var conf = DbConf.load() + runtimePreset = getRuntimePresetForNetwork(conf.eth2Network) case conf.cmd of bench: - cmdBench(conf) + cmdBench(conf, runtimePreset) of dumpState: cmdDumpState(conf) of dumpBlock: cmdDumpBlock(conf) of rewindState: - cmdRewindState(conf) + cmdRewindState(conf, runtimePreset)