Adapt the local sim scripts to use the new run-time presets

This commit is contained in:
Zahary Karadjov 2020-07-10 01:08:54 +03:00
parent 318b225ccd
commit 540b2828b2
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
10 changed files with 126 additions and 76 deletions

View File

@ -14,8 +14,11 @@ import
chronos, confutils, metrics, json_rpc/[rpcserver, jsonmarshal], chronos, confutils, metrics, json_rpc/[rpcserver, jsonmarshal],
chronicles, chronicles,
json_serialization/std/[options, sets, net], serialization/errors, json_serialization/std/[options, sets, net], serialization/errors,
eth/db/kvstore, eth/db/kvstore_sqlite3,
eth/p2p/enode, eth/[keys, async_utils], eth/p2p/discoveryv5/[protocol, enr], eth/[keys, async_utils],
eth/common/eth_types_json_serialization,
eth/db/[kvstore, kvstore_sqlite3],
eth/p2p/enode, eth/p2p/discoveryv5/[protocol, enr],
# Local modules # Local modules
spec/[datatypes, digest, crypto, beaconstate, helpers, network, presets], spec/[datatypes, digest, crypto, beaconstate, helpers, network, presets],
@ -168,7 +171,8 @@ proc init*(
conf.runtimePreset, conf.runtimePreset,
web3Provider(conf.web3Url), web3Provider(conf.web3Url),
conf.depositContractAddress.get, conf.depositContractAddress.get,
Eth1Data(block_hash: conf.depositContractDeployedAt.get, deposit_count: 0)) Eth1Data(block_hash: conf.depositContractDeployedAt.get.asEth2Digest,
deposit_count: 0))
mainchainMonitor.start() mainchainMonitor.start()
genesisState = await mainchainMonitor.waitGenesis() genesisState = await mainchainMonitor.waitGenesis()
@ -1160,11 +1164,20 @@ programMain:
altonaMetadata altonaMetadata
else: else:
if fileExists(networkName): if fileExists(networkName):
try:
Json.loadFile(networkName, Eth2NetworkMetadata) Json.loadFile(networkName, Eth2NetworkMetadata)
except SerializationError as err:
echo err.formatMsg(networkName)
quit 1
else: else:
fatal "Unrecognized network name", networkName fatal "Unrecognized network name", networkName
quit 1 quit 1
if metadata.incompatible:
fatal "The selected network is not compatible with the current build",
reason = metadata.incompatibilityDesc
quit 1
config.runtimePreset = metadata.runtimePreset config.runtimePreset = metadata.runtimePreset
if config.cmd == noCommand: if config.cmd == noCommand:

View File

@ -62,7 +62,7 @@ type
depositContractDeployedAt* {. depositContractDeployedAt* {.
desc: "The Eth1 block hash where the deposit contract has been deployed" desc: "The Eth1 block hash where the deposit contract has been deployed"
name: "deposit-contract-block" }: Option[Eth2Digest] name: "deposit-contract-block" }: Option[Eth1BlockHash]
nonInteractive* {. nonInteractive* {.
desc: "Do not display interative prompts. Quit on missing configuration" desc: "Do not display interative prompts. Quit on missing configuration"
@ -419,11 +419,11 @@ func parseCmdArg*(T: type Eth1Address, input: TaintedString): T
func completeCmdArg*(T: type Eth1Address, input: TaintedString): seq[string] = func completeCmdArg*(T: type Eth1Address, input: TaintedString): seq[string] =
return @[] return @[]
func parseCmdArg*(T: type Eth2Digest, input: TaintedString): T func parseCmdArg*(T: type Eth1BlockHash, input: TaintedString): T
{.raises: [ValueError, Defect].} = {.raises: [ValueError, Defect].} =
fromHex(T, string input) fromHex(T, string input)
func completeCmdArg*(T: type Eth2Digest, input: TaintedString): seq[string] = func completeCmdArg*(T: type Eth1BlockHash, input: TaintedString): seq[string] =
return @[] return @[]
func parseCmdArg*(T: type WalletName, input: TaintedString): T func parseCmdArg*(T: type WalletName, input: TaintedString): T

View File

@ -2,7 +2,8 @@ import
os, strutils, terminal, os, strutils, terminal,
stew/byteutils, chronicles, chronos, web3, stint, json_serialization, stew/byteutils, chronicles, chronos, web3, stint, json_serialization,
serialization, blscurve, eth/common/eth_types, eth/keys, confutils, bearssl, serialization, blscurve, eth/common/eth_types, eth/keys, confutils, bearssl,
spec/[datatypes, digest, crypto, keystore], conf, ssz/merkleization, merkle_minimal spec/[datatypes, digest, crypto, keystore],
conf, ssz/merkleization, merkle_minimal, network_metadata
export export
keystore keystore
@ -15,7 +16,6 @@ const
depositFileName* = "deposit.json" depositFileName* = "deposit.json"
type type
Eth1Address* = eth_types.EthAddress
DelayGenerator* = proc(): chronos.Duration {.closure, gcsafe.} DelayGenerator* = proc(): chronos.Duration {.closure, gcsafe.}
{.push raises: [Defect].} {.push raises: [Defect].}

View File

@ -3,7 +3,7 @@ import
chronos, web3, web3/ethtypes as web3Types, json, chronicles, chronos, web3, web3/ethtypes as web3Types, json, chronicles,
eth/common/eth_types, eth/async_utils, eth/common/eth_types, eth/async_utils,
spec/[datatypes, digest, crypto, beaconstate, helpers], spec/[datatypes, digest, crypto, beaconstate, helpers],
merkle_minimal network_metadata, merkle_minimal
from times import epochTime from times import epochTime
@ -29,7 +29,6 @@ contract(DepositContract):
# Exceptions being reported from Chronos's asyncfutures2. # Exceptions being reported from Chronos's asyncfutures2.
type type
Eth1Address = eth_types.EthAddress
Eth1BlockNumber* = uint64 Eth1BlockNumber* = uint64
Eth1BlockTimestamp* = uint64 Eth1BlockTimestamp* = uint64
Eth1BlockHeader = web3Types.BlockHeader Eth1BlockHeader = web3Types.BlockHeader

View File

@ -1,7 +1,7 @@
import import
tables, strutils, os, tables, strutils, os, options,
stew/byteutils, stew/shims/macros, nimcrypto/hash, stew/shims/macros, nimcrypto/hash,
eth/common/[eth_types, eth_types_json_serialization], web3/[ethtypes, conversions],
spec/presets spec/presets
# ATTENTION! This file will produce a large C file, because we are inlining # ATTENTION! This file will produce a large C file, because we are inlining
@ -15,25 +15,23 @@ import
{.push raises: [Defect].} {.push raises: [Defect].}
export export
eth_types_json_serialization, RuntimePreset ethtypes, conversions, RuntimePreset
type type
Eth1Address* = eth_types.EthAddress Eth1Address* = ethtypes.Address
Eth1BlockHash* = eth_types.Hash256 Eth1BlockHash* = ethtypes.BlockHash
Eth1Network* = enum Eth1Network* = enum
mainnet mainnet
rinkeby rinkeby
goerli goerli
Eth2Network* = enum
customEth2Network
altona
PresetIncompatible* = object of CatchableError PresetIncompatible* = object of CatchableError
Eth2NetworkMetadata* = object Eth2NetworkMetadata* = object
eth1Network*: Eth1Network case incompatible*: bool
of false:
eth1Network*: Option[Eth1Network]
runtimePreset*: RuntimePreset runtimePreset*: RuntimePreset
# Parsing `enr.Records` is still not possible at compile-time # Parsing `enr.Records` is still not possible at compile-time
@ -55,6 +53,8 @@ type
# `genesisData` will have `len == 0` for networks with a still # `genesisData` will have `len == 0` for networks with a still
# unknown genesis state. # unknown genesis state.
genesisData*: string genesisData*: string
else:
incompatibilityDesc*: string
const presetValueLoaders = genExpr(nnkBracket): const presetValueLoaders = genExpr(nnkBracket):
for constName in PresetValue: for constName in PresetValue:
@ -92,23 +92,30 @@ proc extractRuntimePreset*(configPath: string, configData: PresetFile): RuntimeP
proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
{.raises: [CatchableError, Defect].} = {.raises: [CatchableError, Defect].} =
try:
let let
genesisPath = path / "genesis.ssz" genesisPath = path / "genesis.ssz"
configPath = path / "config.yaml" configPath = path / "config.yaml"
runtimePreset = extractRuntimePreset(configPath, readPresetFile(configPath)) runtimePreset = extractRuntimePreset(configPath, readPresetFile(configPath))
Eth2NetworkMetadata( Eth2NetworkMetadata(
eth1Network: goerli, incompatible: false,
eth1Network: some goerli,
runtimePreset: runtimePreset, runtimePreset: runtimePreset,
bootstrapNodes: readFile(path / "bootstrap_nodes.txt").split("\n"), bootstrapNodes: readFile(path / "bootstrap_nodes.txt").split("\n"),
depositContractAddress: Eth1Address.fromHex readFile(path / "deposit_contract.txt").strip, depositContractAddress: Eth1Address.fromHex readFile(path / "deposit_contract.txt").strip,
depositContractDeployedAt: Eth1BlockHash.fromHex readFile(path / "deposit_contract_block.txt").strip, depositContractDeployedAt: Eth1BlockHash.fromHex readFile(path / "deposit_contract_block.txt").strip,
genesisData: if fileExists(genesisPath): readFile(genesisPath) else: "") genesisData: if fileExists(genesisPath): readFile(genesisPath) else: "")
except PresetIncompatible as err:
Eth2NetworkMetadata(incompatible: true,
incompatibilityDesc: err.msg)
when const_preset == "mainnet": when const_preset == "mainnet":
const const
mainnetMetadata* = Eth2NetworkMetadata( mainnetMetadata* = Eth2NetworkMetadata(
eth1Network: mainnet, incompatible: false, # TODO: This can be more accurate if we verify
# that there are no constant overrides
eth1Network: some mainnet,
runtimePreset: mainnetRuntimePreset, runtimePreset: mainnetRuntimePreset,
# TODO The values below are just placeholders for now # TODO The values below are just placeholders for now
bootstrapNodes: @[], bootstrapNodes: @[],

View File

@ -146,9 +146,11 @@ $MAKE LOG_LEVEL="${LOG_LEVEL}" NIMFLAGS="-d:insecure -d:testnet_servers_image ${
PIDS="" PIDS=""
WEB3_ARG="" WEB3_ARG=""
DEPOSIT_CONTRACT_ARG=""
STATE_SNAPSHOT_ARG="" STATE_SNAPSHOT_ARG=""
BOOTSTRAP_TIMEOUT=10 # in seconds BOOTSTRAP_TIMEOUT=10 # in seconds
DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000"
DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000"
NETWORK_METADATA_FILE="${DATA_DIR}/network.json"
./build/beacon_node deposits create \ ./build/beacon_node deposits create \
--count=${TOTAL_VALIDATORS} \ --count=${TOTAL_VALIDATORS} \
@ -179,10 +181,16 @@ else
ganache-cli --blockTime 17 --gasLimit 100000000 -e 100000 --verbose > "${DATA_DIR}/log_ganache.txt" 2>&1 & ganache-cli --blockTime 17 --gasLimit 100000000 -e 100000 --verbose > "${DATA_DIR}/log_ganache.txt" 2>&1 &
PIDS="${PIDS},$!" PIDS="${PIDS},$!"
echo "Deploying deposit contract"
WEB3_ARG="--web3-url=ws://localhost:8545" WEB3_ARG="--web3-url=ws://localhost:8545"
DEPOSIT_CONTRACT_ADDRESS=$(./build/deposit_contract deploy $WEB3_ARG)
DEPOSIT_CONTRACT_ARG="--deposit-contract=$DEPOSIT_CONTRACT_ADDRESS" echo "Deploying deposit contract"
DEPLOY_CMD_OUTPUT=$(./build/deposit_contract deploy $WEB3_ARG)
# https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
OUTPUT_PIECES=(${DEPLOY_CMD_OUTPUT//;/ })
DEPOSIT_CONTRACT_ADDRESS=${OUTPUT_PIECES[0]}
DEPOSIT_CONTRACT_BLOCK=${OUTPUT_PIECES[1]}
echo Contract deployed at $DEPOSIT_CONTRACT_ADDRESS:$DEPOSIT_CONTRACT_BLOCK
MIN_DELAY=1 MIN_DELAY=1
MAX_DELAY=5 MAX_DELAY=5
@ -194,7 +202,7 @@ else
--deposits-dir="${DEPOSITS_DIR}" \ --deposits-dir="${DEPOSITS_DIR}" \
--min-delay=$MIN_DELAY --max-delay=$MAX_DELAY \ --min-delay=$MIN_DELAY --max-delay=$MAX_DELAY \
$WEB3_ARG \ $WEB3_ARG \
$DEPOSIT_CONTRACT_ARG > "${DATA_DIR}/log_deposit_maker.txt" 2>&1 & --deposit-contract=${DEPOSIT_CONTRACT_ADDRESS} > "${DATA_DIR}/log_deposit_maker.txt" 2>&1 &
PIDS="${PIDS},$!" PIDS="${PIDS},$!"
fi fi
@ -205,6 +213,20 @@ fi
--config-file "${DATA_DIR}/prometheus.yml" || true # TODO: this currently fails on macOS, --config-file "${DATA_DIR}/prometheus.yml" || true # TODO: this currently fails on macOS,
# but it can be considered non-critical # but it can be considered non-critical
echo Wrote $NETWORK_METADATA_FILE:
tee "$NETWORK_METADATA_FILE" <<EOF
{
"runtimePreset": {
"MIN_GENESIS_ACTIVE_VALIDATOR_COUNT": ${TOTAL_VALIDATORS},
"MIN_GENESIS_TIME": 0,
"GENESIS_DELAY": 10,
"GENESIS_FORK_VERSION": "0x00000000"
},
"depositContractAddress": "${DEPOSIT_CONTRACT_ADDRESS}",
"depositContractDeployedAt": "${DEPOSIT_CONTRACT_BLOCK}"
}
EOF
# Kill child processes on Ctrl-C/SIGTERM/exit, passing the PID of this shell # Kill child processes on Ctrl-C/SIGTERM/exit, passing the PID of this shell
# instance as the parent and the target process name as a pattern to the # instance as the parent and the target process name as a pattern to the
# "pkill" command. # "pkill" command.
@ -264,6 +286,7 @@ for NUM_NODE in $(seq $BOOTSTRAP_NODE -1 0); do
./build/beacon_node \ ./build/beacon_node \
--non-interactive \ --non-interactive \
--nat:extip:127.0.0.1 \ --nat:extip:127.0.0.1 \
--network="${NETWORK_METADATA_FILE}" \
--log-level="${LOG_LEVEL}" \ --log-level="${LOG_LEVEL}" \
--tcp-port=$(( BASE_PORT + NUM_NODE )) \ --tcp-port=$(( BASE_PORT + NUM_NODE )) \
--udp-port=$(( BASE_PORT + NUM_NODE )) \ --udp-port=$(( BASE_PORT + NUM_NODE )) \
@ -271,7 +294,6 @@ for NUM_NODE in $(seq $BOOTSTRAP_NODE -1 0); do
${BOOTSTRAP_ARG} \ ${BOOTSTRAP_ARG} \
${STATE_SNAPSHOT_ARG} \ ${STATE_SNAPSHOT_ARG} \
${WEB3_ARG} \ ${WEB3_ARG} \
${DEPOSIT_CONTRACT_ARG} \
--metrics \ --metrics \
--metrics-address="127.0.0.1" \ --metrics-address="127.0.0.1" \
--metrics-port="$(( BASE_METRICS_PORT + NUM_NODE ))" \ --metrics-port="$(( BASE_METRICS_PORT + NUM_NODE ))" \

View File

@ -81,18 +81,12 @@ if [ -f "${SNAPSHOT_FILE}" ]; then
SNAPSHOT_ARG="--state-snapshot=${SNAPSHOT_FILE}" SNAPSHOT_ARG="--state-snapshot=${SNAPSHOT_FILE}"
fi fi
DEPOSIT_CONTRACT_ARGS=""
if [ -f "${DEPOSIT_CONTRACT_FILE}" ]; then
DEPOSIT_CONTRACT_ARGS="$WEB3_ARG \
--deposit-contract=$(cat $DEPOSIT_CONTRACT_FILE) \
--deposit-contract-block=$(cat $DEPOSIT_CONTRACT_BLOCK_FILE)"
fi
cd "$NODE_DATA_DIR" cd "$NODE_DATA_DIR"
$BEACON_NODE_BIN \ $BEACON_NODE_BIN \
--log-level=${LOG_LEVEL:-DEBUG} \ --log-level=${LOG_LEVEL:-DEBUG} \
$BOOTSTRAP_ARG \ $BOOTSTRAP_ARG \
--network=$NETWORK_METADATA_FILE \
--data-dir=$NODE_DATA_DIR \ --data-dir=$NODE_DATA_DIR \
--secrets-dir=$NODE_SECRETS_DIR \ --secrets-dir=$NODE_SECRETS_DIR \
--node-name=$NODE_ID \ --node-name=$NODE_ID \
@ -100,7 +94,7 @@ $BEACON_NODE_BIN \
--udp-port=$PORT \ --udp-port=$PORT \
$SNAPSHOT_ARG \ $SNAPSHOT_ARG \
$NAT_ARG \ $NAT_ARG \
$DEPOSIT_CONTRACT_ARGS \ $WEB3_ARG \
--rpc \ --rpc \
--rpc-address="127.0.0.1" \ --rpc-address="127.0.0.1" \
--rpc-port="$(( $BASE_RPC_PORT + $NODE_ID ))" \ --rpc-port="$(( $BASE_RPC_PORT + $NODE_ID ))" \

View File

@ -54,10 +54,7 @@ GANACHE_BLOCK_TIME=5
# Run with "SLOTS_PER_EPOCH=8 ./start.sh" to change these # Run with "SLOTS_PER_EPOCH=8 ./start.sh" to change these
DEFS="" DEFS=""
DEFS+="-d:MIN_GENESIS_ACTIVE_VALIDATOR_COUNT=${NUM_VALIDATORS} \ DEFS+="-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME \
-d:MIN_GENESIS_TIME=0 \
-d:GENESIS_DELAY=10 \
-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME \
-d:ETH1_FOLLOW_DISTANCE=1 " -d:ETH1_FOLLOW_DISTANCE=1 "
DEFS+="-d:MAX_COMMITTEES_PER_SLOT=${MAX_COMMITTEES_PER_SLOT:-1} " # Spec default: 64 DEFS+="-d:MAX_COMMITTEES_PER_SLOT=${MAX_COMMITTEES_PER_SLOT:-1} " # Spec default: 64
DEFS+="-d:SLOTS_PER_EPOCH=${SLOTS_PER_EPOCH:-6} " # Spec default: 32 DEFS+="-d:SLOTS_PER_EPOCH=${SLOTS_PER_EPOCH:-6} " # Spec default: 32
@ -163,6 +160,9 @@ function run_cmd {
fi fi
} }
DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000"
DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000"
if [ "$USE_GANACHE" != "no" ]; then if [ "$USE_GANACHE" != "no" ]; then
make deposit_contract make deposit_contract
echo Deploying the validator deposit contract... echo Deploying the validator deposit contract...
@ -174,8 +174,6 @@ if [ "$USE_GANACHE" != "no" ]; then
DEPOSIT_CONTRACT_BLOCK=${OUTPUT_PIECES[1]} DEPOSIT_CONTRACT_BLOCK=${OUTPUT_PIECES[1]}
echo Contract deployed at $DEPOSIT_CONTRACT_ADDRESS:$DEPOSIT_CONTRACT_BLOCK echo Contract deployed at $DEPOSIT_CONTRACT_ADDRESS:$DEPOSIT_CONTRACT_BLOCK
echo $DEPOSIT_CONTRACT_ADDRESS > $DEPOSIT_CONTRACT_FILE
echo $DEPOSIT_CONTRACT_BLOCK > $DEPOSIT_CONTRACT_BLOCK_FILE
if [[ "$WAIT_GENESIS" == "yes" ]]; then if [[ "$WAIT_GENESIS" == "yes" ]]; then
run_cmd "(deposit maker)" "$BEACON_NODE_BIN deposits send \ run_cmd "(deposit maker)" "$BEACON_NODE_BIN deposits send \
@ -187,6 +185,20 @@ if [ "$USE_GANACHE" != "no" ]; then
fi fi
fi fi
echo Wrote $NETWORK_METADATA_FILE:
tee "$NETWORK_METADATA_FILE" <<EOF
{
"runtimePreset": {
"MIN_GENESIS_ACTIVE_VALIDATOR_COUNT": ${NUM_VALIDATORS},
"MIN_GENESIS_TIME": 0,
"GENESIS_DELAY": 10,
"GENESIS_FORK_VERSION": "0x00000000"
},
"depositContractAddress": "${DEPOSIT_CONTRACT_ADDRESS}",
"depositContractDeployedAt": "${DEPOSIT_CONTRACT_BLOCK}"
}
EOF
if [[ "$USE_TMUX" == "yes" ]]; then if [[ "$USE_TMUX" == "yes" ]]; then
$TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim" $TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim"
fi fi

View File

@ -31,14 +31,17 @@ VALIDATORS_DIR="${SIM_ROOT}/validators"
SECRETS_DIR="${SIM_ROOT}/secrets" SECRETS_DIR="${SIM_ROOT}/secrets"
SNAPSHOT_FILE="${SIMULATION_DIR}/state_snapshot.ssz" SNAPSHOT_FILE="${SIMULATION_DIR}/state_snapshot.ssz"
NETWORK_BOOTSTRAP_FILE="${SIMULATION_DIR}/bootstrap_nodes.txt" NETWORK_BOOTSTRAP_FILE="${SIMULATION_DIR}/bootstrap_nodes.txt"
DEPOSIT_CONTRACT_FILE="${SIMULATION_DIR}/deposit_contract.txt"
DEPOSIT_CONTRACT_BLOCK_FILE="${SIMULATION_DIR}/deposit_contract_block.txt"
BEACON_NODE_BIN="${GIT_ROOT}/build/beacon_node" BEACON_NODE_BIN="${GIT_ROOT}/build/beacon_node"
VALIDATOR_CLIENT_BIN="${GIT_ROOT}/build/validator_client" VALIDATOR_CLIENT_BIN="${GIT_ROOT}/build/validator_client"
DEPLOY_DEPOSIT_CONTRACT_BIN="${GIT_ROOT}/build/deposit_contract" DEPLOY_DEPOSIT_CONTRACT_BIN="${GIT_ROOT}/build/deposit_contract"
BOOTSTRAP_ENR_FILE="${SIMULATION_DIR}/node-${BOOTSTRAP_NODE}/beacon_node.enr" BOOTSTRAP_ENR_FILE="${SIMULATION_DIR}/node-${BOOTSTRAP_NODE}/beacon_node.enr"
NETWORK_METADATA_FILE="${SIMULATION_DIR}/network.json"
WEB3_ARG="--web3-url=ws://localhost:8545" if [[ "$USE_GANACHE" == "yes" ]]; then
WEB3_ARG="--web3-url=ws://localhost:8545"
else
WEB3_ARG=""
fi
BASE_P2P_PORT=30000 BASE_P2P_PORT=30000
BASE_RPC_PORT=7000 BASE_RPC_PORT=7000

2
vendor/nim-web3 vendored

@ -1 +1 @@
Subproject commit dd132ba024fd8784aab7b5c306c4ec61c86e8613 Subproject commit 04be808890ced3f47e9bb93267992f07c25acfbc