From 42ca6afae25497bd0bbb9f941fc02cc1809acff0 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Tue, 19 Mar 2019 21:50:22 +0200 Subject: [PATCH] Helpful scripts for resetting the testnets --- beacon_chain/beacon_node.nim | 23 ++++++++---------- beacon_chain/conf.nim | 14 ++++++----- beacon_chain/eth2_network.nim | 6 +++++ scripts/reset_testnet.sh | 45 +++++++++++++++++++++++++++++++++++ scripts/reset_testnet0.sh | 5 ++++ scripts/reset_testnet1.sh | 5 ++++ scripts/testnet0.env | 6 +++++ scripts/testnet1.env | 6 +++++ 8 files changed, 91 insertions(+), 19 deletions(-) create mode 100755 scripts/reset_testnet.sh create mode 100755 scripts/reset_testnet0.sh create mode 100755 scripts/reset_testnet1.sh create mode 100644 scripts/testnet0.env create mode 100644 scripts/testnet1.env diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index d3fb24960..834beada5 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -47,7 +47,8 @@ proc downloadFile(url: string): Future[string] {.async.} = return fileContents proc updateTestnetMetadata(conf: BeaconNodeConf): Future[NetworkMetadata] {.async.} = - let latestMetadata = await downloadFile(testnetsBaseUrl // $conf.network // networkMetadataFile) + let latestMetadata = await downloadFile(testnetsBaseUrl // $conf.network // + netBackendName & "-" & networkMetadataFile) let localMetadataFile = conf.dataDir / networkMetadataFile if fileExists(localMetadataFile) and readFile(localMetadataFile).string == latestMetadata: @@ -716,28 +717,24 @@ when isMainModule: of updateTestnet: discard waitFor updateTestnetMetadata(config) - of importValidator: + of importValidators: template reportFailureFor(keyExpr) = error "Failed to import validator key", key = keyExpr programResult = 1 - var downloadKey = true - - if config.key.isSome: - downloadKey = false + for key in config.keys: try: - ValidatorPrivKey.init(config.key.get).saveValidatorKey(config) + ValidatorPrivKey.init(key).saveValidatorKey(config) except: - reportFailureFor config.key.get + reportFailureFor key - if config.keyFile.isSome: - downloadKey = false + for keyFile in config.keyFiles: try: - config.keyFile.get.load.saveValidatorKey(config) + keyFile.load.saveValidatorKey(config) except: - reportFailureFor config.keyFile.get.string + reportFailureFor keyFile.string - if downloadKey: + if (config.keys.len + config.keyFiles.len) == 0: if config.network in ["testnet0", "testnet1"]: try: let key = waitFor obtainTestnetKey(config) diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 8e4863e17..958ab33bd 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -12,7 +12,7 @@ type StartUpCommand* = enum noCommand createTestnet - importValidator + importValidators updateTestnet BeaconNodeConf* = object @@ -107,12 +107,14 @@ type outputNetwork* {. desc: "Output file where to write the initial state snapshot".}: OutFile - of importValidator: - keyFile* {. - desc: "File with validator key to be imported (in hex form)".}: Option[ValidatorKeyPath] + of importValidators: + keyFiles* {. + longform: "keyfile" + desc: "File with validator key to be imported (in hex form)".}: seq[ValidatorKeyPath] - key* {. - desc: "Validator key to be imported (in hex form)".}: Option[string] + keys* {. + longform: "key" + desc: "Validator key to be imported (in hex form)".}: seq[string] of updateTestnet: discard diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 8f3f6c3e9..4f25cd7a1 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -13,6 +13,9 @@ when useRLPx: export p2p, rlp, gossipsub_protocol + const + netBackendName* = "rlpx" + type Eth2Node* = EthereumNode BootstrapAddr* = ENode @@ -82,6 +85,9 @@ else: type BootstrapAddr* = PeerInfo + const + netBackendName* = "libp2p" + proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} = writer.writeValue value.pretty diff --git a/scripts/reset_testnet.sh b/scripts/reset_testnet.sh new file mode 100755 index 000000000..9c9c33f36 --- /dev/null +++ b/scripts/reset_testnet.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -eu + +cd $(dirname "$0") + +NETWORK_NAME=$1 +source "$NETWORK_NAME.env" + +cd .. + +if [ -f .env ]; then + # allow server overrides for WWW_DIR and DATA_DIR + source .env +fi + +PUBLIC_IP=$(curl -s ifconfig.me) +NETWORK_DIR=$WWW_DIR/$NETWORK_NAME + +regenTestnetFiles() { + NIM_FLAGS="-d:release -d:SHARD_COUNT=$SHARD_COUNT -d:SLOTS_PER_EPOCH=$SLOTS_PER_EPOCH ${2:-}" + NETWORK_FLAVOUR=$1 + + if [ ! -f $NETWORK_DIR/genesis.json ]; then + rm $NETWORK_DIR/* + nim c -r $NIM_FLAGS beacon_chain/validator_keygen \ + --validators=$VALIDATOR_COUNT \ + --outputDir="$NETWORK_DIR" + fi + + nim c -r $NIM_FLAGS beacon_chain/beacon_node --dataDir=$DATA_DIR/node-0 \ + createTestnet \ + --networkId=$NETWORK_ID \ + --validatorsDir=$NETWORK_DIR \ + --numValidators=$VALIDATOR_COUNT \ + --firstUserValidator=$FIRST_USER_VALIDATOR \ + --outputGenesis=$NETWORK_DIR/genesis.json \ + --outputNetwork=$NETWORK_DIR/$NETWORK_FLAVOUR-network.json \ + --bootstrapAddress=$PUBLIC_IP \ + --genesisOffset=600 # Delay in seconds +} + +regenTestnetFiles rlpx +# regenTestnetFiles libp2p -d:withLibP2P + diff --git a/scripts/reset_testnet0.sh b/scripts/reset_testnet0.sh new file mode 100755 index 000000000..4aa0d8686 --- /dev/null +++ b/scripts/reset_testnet0.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd $(dirname "$0") +./reset_testnet.sh testnet0 + diff --git a/scripts/reset_testnet1.sh b/scripts/reset_testnet1.sh new file mode 100755 index 000000000..5c6cd4e80 --- /dev/null +++ b/scripts/reset_testnet1.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd $(dirname "$0") +./reset_testnet.sh testnet1 + diff --git a/scripts/testnet0.env b/scripts/testnet0.env new file mode 100644 index 000000000..da3ecd8be --- /dev/null +++ b/scripts/testnet0.env @@ -0,0 +1,6 @@ +NETWORK_ID=1000000 +SHARD_COUNT=8 +SLOTS_PER_EPOCH=8 +VALIDATOR_COUNT=400 +FIRST_USER_VALIDATOR=340 + diff --git a/scripts/testnet1.env b/scripts/testnet1.env new file mode 100644 index 000000000..ae447f40c --- /dev/null +++ b/scripts/testnet1.env @@ -0,0 +1,6 @@ +NETWORK_ID=2000000 +SHARD_COUNT=128 +SLOTS_PER_EPOCH=64 +VALIDATOR_COUNT=20000 +FIRST_USER_VALIDATOR=19000 +