From add22a20e1ed685e571cb8dcaaa0dabb7c8eb047 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 25 Aug 2020 15:49:05 +0300 Subject: [PATCH] Update local_testnet and simulation scripts to use netkey-file and insecure-netkey-password. Add more logging --- beacon_chain/conf.nim | 34 ++++++++++------------------ beacon_chain/eth2_network.nim | 18 ++++++++++----- beacon_chain/keystore_management.nim | 5 +++- scripts/launch_local_testnet.sh | 4 +++- tests/simulation/run_node.sh | 2 ++ tests/simulation/start.sh | 2 ++ 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 122c226e3..faf5e0397 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -82,6 +82,18 @@ type desc: "Do not display interative prompts. Quit on missing configuration" name: "non-interactive" }: bool + netKeyFile* {. + defaultValue: "random", + desc: "Source of network (secp256k1) private key file " & + "(random|) (default: random)" + name: "netkey-file" }: string + + netKeyInsecurePassword* {. + defaultValue: false, + desc: "Use pre-generated INSECURE password for network private key " & + "file (default: false)" + name: "insecure-netkey-password" }: bool + case cmd* {. command defaultValue: noCommand }: BNStartUpCmd @@ -218,18 +230,6 @@ type desc: "Write SSZ dumps of blocks, attestations and states to data dir" name: "dump" }: bool - netKeyFile* {. - defaultValue: "random", - desc: "Source of network (secp256k1) private key file " & - "(random|) (default: random)" - name: "netkey-file" }: string - - netKeyInsecurePassword* {. - defaultValue: false, - desc: "Use pre-generated INSECURE password for network private key " & - "file (default: false)" - name: "insecure-netkey-password" }: bool - of createTestnet: testnetDepositsFile* {. desc: "A LaunchPad deposits file for the genesis state validators" @@ -277,16 +277,6 @@ type desc: "Output file with list of bootstrap nodes for the network" name: "output-bootstrap-file" }: OutFile - outputNetkeyFile* {. - desc: "Output file with network private key for the network" - name: "netkey-file" }: OutFile - - outputNetKeyInsecurePassword* {. - defaultValue: false, - desc: "Use pre-generated INSECURE password for network private key " & - "file (default: false)" - name: "insecure-netkey-password" }: bool - of wallets: case walletsCmd* {.command.}: WalletsCmd of WalletsCmd.create: diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 6b5fe2dbf..bcafe15c4 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -1214,6 +1214,9 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext, fatal "Could not generate random network key file" quit QuitFailure let privKey = res.get() + let pubKey = privKey.getKey().tryGet() + info "Using random network key", + network_public_key = byteutils.toHex(pubKey.getBytes().tryGet()) return KeyPair(seckey: privKey, pubkey: privKey.getKey().tryGet()) else: let keyPath = @@ -1270,12 +1273,15 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext, return KeyPair(seckey: privKey, pubkey: pubKey) of createTestnet: - let netKeyFile = string(conf.outputNetkeyFile) + if conf.netKeyFile == "random": + fatal "Could not create testnet using `random` network key" + quit QuitFailure + let keyPath = - if isAbsolute(netKeyFile): - netKeyFile + if isAbsolute(conf.netKeyFile): + conf.netKeyFile else: - conf.dataDir / netKeyFile + conf.dataDir / conf.netKeyFile let rres = PrivateKey.random(Secp256k1, rng) if rres.isErr(): @@ -1287,14 +1293,14 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext, # Insecure password used only for automated testing. let insecurePassword = - if conf.outputNetKeyInsecurePassword: + if conf.netKeyInsecurePassword: some(NetworkInsecureKeyPassword) else: none[string]() let sres = saveNetKeystore(rng, keyPath, privKey, insecurePassword) if sres.isErr(): - fatal "Could not create network key file" + fatal "Could not create network key file", key_path = keyPath quit QuitFailure info "New network key storage was created", key_path = keyPath, diff --git a/beacon_chain/keystore_management.nim b/beacon_chain/keystore_management.nim index 6831a884d..f28b93205 100644 --- a/beacon_chain/keystore_management.nim +++ b/beacon_chain/keystore_management.nim @@ -202,7 +202,8 @@ proc saveNetKeystore*(rng: var BrHmacDrbgContext, keyStorePath: string, ): Result[void, KeystoreGenerationError] = var password, confirmedPassword: TaintedString if insecurePwd.isSome(): - warn "Using insecure password to lock networking key" + warn "Using insecure password to lock networking key", + key_path = keyStorePath password = insecurePwd.get() else: while true: @@ -244,12 +245,14 @@ proc saveNetKeystore*(rng: var BrHmacDrbgContext, keyStorePath: string, try: encodedStorage = Json.encode(keyStore) except SerializationError: + error "Could not serialize network key storage", key_path = keyStorePath return err(FailedToCreateKeystoreFile) let res = writeFile(keyStorePath, encodedStorage, 0o600) if res.isOk(): ok() else: + error "Could not write to network key storage file", key_path = keyStorePath err(FailedToCreateKeystoreFile) proc saveKeystore(rng: var BrHmacDrbgContext, diff --git a/scripts/launch_local_testnet.sh b/scripts/launch_local_testnet.sh index 27e69d38c..5f7514890 100755 --- a/scripts/launch_local_testnet.sh +++ b/scripts/launch_local_testnet.sh @@ -218,6 +218,8 @@ if [[ $USE_GANACHE == "0" ]]; then --output-bootstrap-file="${NETWORK_DIR}/bootstrap_nodes.txt" \ --bootstrap-address=${BOOTSTRAP_IP} \ --bootstrap-port=${BASE_PORT} \ + --netkey-file=network_key.json \ + --insecure-netkey-password=true \ --genesis-offset=${GENESIS_OFFSET} # Delay in seconds STATE_SNAPSHOT_ARG="--state-snapshot=${NETWORK_DIR}/genesis.ssz" @@ -315,7 +317,7 @@ BOOTSTRAP_ENR="${DATA_DIR}/node${BOOTSTRAP_NODE}/beacon_node.enr" for NUM_NODE in $(seq 0 $(( NUM_NODES - 1 ))); do if [[ ${NUM_NODE} == ${BOOTSTRAP_NODE} ]]; then - BOOTSTRAP_ARG="" + BOOTSTRAP_ARG="--netkey-file=network_key.json --insecure-netkey-password=true" else BOOTSTRAP_ARG="--bootstrap-file=${BOOTSTRAP_ENR}" # Wait for the master node to write out its address file diff --git a/tests/simulation/run_node.sh b/tests/simulation/run_node.sh index a9039a272..dc865a2bc 100755 --- a/tests/simulation/run_node.sh +++ b/tests/simulation/run_node.sh @@ -29,6 +29,8 @@ BOOTSTRAP_ADDRESS_FILE="${SIMULATION_DIR}/node-${BOOTSTRAP_NODE_ID}/beacon_node. if [[ "$NODE_ID" != "$BOOTSTRAP_NODE" ]]; then BOOTSTRAP_ARG="--bootstrap-file=$BOOTSTRAP_ADDRESS_FILE" +else + BOOTSTRAP_ARG="--netkey-file=network_key.json --insecure-netkey-password" fi # set up the environment diff --git a/tests/simulation/start.sh b/tests/simulation/start.sh index 01ad60613..23543f325 100755 --- a/tests/simulation/start.sh +++ b/tests/simulation/start.sh @@ -143,6 +143,8 @@ if [ ! -f "${SNAPSHOT_FILE}" ]; then --output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \ --bootstrap-address=127.0.0.1 \ --bootstrap-port=$(( BASE_P2P_PORT + BOOTSTRAP_NODE )) \ + --netkey-file=network_key.json \ + --insecure-netkey-password=true \ --genesis-offset=30 # Delay in seconds fi fi