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],
chronicles,
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
spec/[datatypes, digest, crypto, beaconstate, helpers, network, presets],
@ -168,7 +171,8 @@ proc init*(
conf.runtimePreset,
web3Provider(conf.web3Url),
conf.depositContractAddress.get,
Eth1Data(block_hash: conf.depositContractDeployedAt.get, deposit_count: 0))
Eth1Data(block_hash: conf.depositContractDeployedAt.get.asEth2Digest,
deposit_count: 0))
mainchainMonitor.start()
genesisState = await mainchainMonitor.waitGenesis()
@ -1160,11 +1164,20 @@ programMain:
altonaMetadata
else:
if fileExists(networkName):
Json.loadFile(networkName, Eth2NetworkMetadata)
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
config.runtimePreset = metadata.runtimePreset
if config.cmd == noCommand:

View File

@ -62,7 +62,7 @@ type
depositContractDeployedAt* {.
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* {.
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] =
return @[]
func parseCmdArg*(T: type Eth2Digest, input: TaintedString): T
func parseCmdArg*(T: type Eth1BlockHash, input: TaintedString): T
{.raises: [ValueError, Defect].} =
fromHex(T, string input)
func completeCmdArg*(T: type Eth2Digest, input: TaintedString): seq[string] =
func completeCmdArg*(T: type Eth1BlockHash, input: TaintedString): seq[string] =
return @[]
func parseCmdArg*(T: type WalletName, input: TaintedString): T

View File

@ -2,7 +2,8 @@ import
os, strutils, terminal,
stew/byteutils, chronicles, chronos, web3, stint, json_serialization,
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
keystore
@ -15,7 +16,6 @@ const
depositFileName* = "deposit.json"
type
Eth1Address* = eth_types.EthAddress
DelayGenerator* = proc(): chronos.Duration {.closure, gcsafe.}
{.push raises: [Defect].}

View File

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

View File

@ -1,7 +1,7 @@
import
tables, strutils, os,
stew/byteutils, stew/shims/macros, nimcrypto/hash,
eth/common/[eth_types, eth_types_json_serialization],
tables, strutils, os, options,
stew/shims/macros, nimcrypto/hash,
web3/[ethtypes, conversions],
spec/presets
# ATTENTION! This file will produce a large C file, because we are inlining
@ -15,46 +15,46 @@ import
{.push raises: [Defect].}
export
eth_types_json_serialization, RuntimePreset
ethtypes, conversions, RuntimePreset
type
Eth1Address* = eth_types.EthAddress
Eth1BlockHash* = eth_types.Hash256
Eth1Address* = ethtypes.Address
Eth1BlockHash* = ethtypes.BlockHash
Eth1Network* = enum
mainnet
rinkeby
goerli
Eth2Network* = enum
customEth2Network
altona
PresetIncompatible* = object of CatchableError
Eth2NetworkMetadata* = object
eth1Network*: Eth1Network
runtimePreset*: RuntimePreset
case incompatible*: bool
of false:
eth1Network*: Option[Eth1Network]
runtimePreset*: RuntimePreset
# Parsing `enr.Records` is still not possible at compile-time
bootstrapNodes*: seq[string]
# Parsing `enr.Records` is still not possible at compile-time
bootstrapNodes*: seq[string]
depositContractAddress*: Eth1Address
depositContractDeployedAt*: Eth1BlockHash
depositContractAddress*: Eth1Address
depositContractDeployedAt*: Eth1BlockHash
# Please note that we are using `string` here because SSZ.decode
# is not currently usable at compile time and we want to load the
# network metadata into a constant.
#
# We could have also used `seq[byte]`, but this results in a lot
# more generated code that slows down compilation. The impact on
# compilation times of embedding the genesis as a string is roughly
# 0.1s on my machine (you can test this by choosing an invalid name
# for the genesis file below).
#
# `genesisData` will have `len == 0` for networks with a still
# unknown genesis state.
genesisData*: string
# Please note that we are using `string` here because SSZ.decode
# is not currently usable at compile time and we want to load the
# network metadata into a constant.
#
# We could have also used `seq[byte]`, but this results in a lot
# more generated code that slows down compilation. The impact on
# compilation times of embedding the genesis as a string is roughly
# 0.1s on my machine (you can test this by choosing an invalid name
# for the genesis file below).
#
# `genesisData` will have `len == 0` for networks with a still
# unknown genesis state.
genesisData*: string
else:
incompatibilityDesc*: string
const presetValueLoaders = genExpr(nnkBracket):
for constName in PresetValue:
@ -92,23 +92,30 @@ proc extractRuntimePreset*(configPath: string, configData: PresetFile): RuntimeP
proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
{.raises: [CatchableError, Defect].} =
let
genesisPath = path / "genesis.ssz"
configPath = path / "config.yaml"
runtimePreset = extractRuntimePreset(configPath, readPresetFile(configPath))
try:
let
genesisPath = path / "genesis.ssz"
configPath = path / "config.yaml"
runtimePreset = extractRuntimePreset(configPath, readPresetFile(configPath))
Eth2NetworkMetadata(
eth1Network: goerli,
runtimePreset: runtimePreset,
bootstrapNodes: readFile(path / "bootstrap_nodes.txt").split("\n"),
depositContractAddress: Eth1Address.fromHex readFile(path / "deposit_contract.txt").strip,
depositContractDeployedAt: Eth1BlockHash.fromHex readFile(path / "deposit_contract_block.txt").strip,
genesisData: if fileExists(genesisPath): readFile(genesisPath) else: "")
Eth2NetworkMetadata(
incompatible: false,
eth1Network: some goerli,
runtimePreset: runtimePreset,
bootstrapNodes: readFile(path / "bootstrap_nodes.txt").split("\n"),
depositContractAddress: Eth1Address.fromHex readFile(path / "deposit_contract.txt").strip,
depositContractDeployedAt: Eth1BlockHash.fromHex readFile(path / "deposit_contract_block.txt").strip,
genesisData: if fileExists(genesisPath): readFile(genesisPath) else: "")
except PresetIncompatible as err:
Eth2NetworkMetadata(incompatible: true,
incompatibilityDesc: err.msg)
when const_preset == "mainnet":
const
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,
# TODO The values below are just placeholders for now
bootstrapNodes: @[],

View File

@ -146,9 +146,11 @@ $MAKE LOG_LEVEL="${LOG_LEVEL}" NIMFLAGS="-d:insecure -d:testnet_servers_image ${
PIDS=""
WEB3_ARG=""
DEPOSIT_CONTRACT_ARG=""
STATE_SNAPSHOT_ARG=""
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 \
--count=${TOTAL_VALIDATORS} \
@ -179,10 +181,16 @@ else
ganache-cli --blockTime 17 --gasLimit 100000000 -e 100000 --verbose > "${DATA_DIR}/log_ganache.txt" 2>&1 &
PIDS="${PIDS},$!"
echo "Deploying deposit contract"
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
MAX_DELAY=5
@ -194,7 +202,7 @@ else
--deposits-dir="${DEPOSITS_DIR}" \
--min-delay=$MIN_DELAY --max-delay=$MAX_DELAY \
$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},$!"
fi
@ -205,6 +213,20 @@ fi
--config-file "${DATA_DIR}/prometheus.yml" || true # TODO: this currently fails on macOS,
# 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
# instance as the parent and the target process name as a pattern to the
# "pkill" command.
@ -264,6 +286,7 @@ for NUM_NODE in $(seq $BOOTSTRAP_NODE -1 0); do
./build/beacon_node \
--non-interactive \
--nat:extip:127.0.0.1 \
--network="${NETWORK_METADATA_FILE}" \
--log-level="${LOG_LEVEL}" \
--tcp-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} \
${STATE_SNAPSHOT_ARG} \
${WEB3_ARG} \
${DEPOSIT_CONTRACT_ARG} \
--metrics \
--metrics-address="127.0.0.1" \
--metrics-port="$(( BASE_METRICS_PORT + NUM_NODE ))" \

View File

@ -81,18 +81,12 @@ if [ -f "${SNAPSHOT_FILE}" ]; then
SNAPSHOT_ARG="--state-snapshot=${SNAPSHOT_FILE}"
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"
$BEACON_NODE_BIN \
--log-level=${LOG_LEVEL:-DEBUG} \
$BOOTSTRAP_ARG \
--network=$NETWORK_METADATA_FILE \
--data-dir=$NODE_DATA_DIR \
--secrets-dir=$NODE_SECRETS_DIR \
--node-name=$NODE_ID \
@ -100,7 +94,7 @@ $BEACON_NODE_BIN \
--udp-port=$PORT \
$SNAPSHOT_ARG \
$NAT_ARG \
$DEPOSIT_CONTRACT_ARGS \
$WEB3_ARG \
--rpc \
--rpc-address="127.0.0.1" \
--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
DEFS=""
DEFS+="-d:MIN_GENESIS_ACTIVE_VALIDATOR_COUNT=${NUM_VALIDATORS} \
-d:MIN_GENESIS_TIME=0 \
-d:GENESIS_DELAY=10 \
-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME \
DEFS+="-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME \
-d:ETH1_FOLLOW_DISTANCE=1 "
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
@ -163,6 +160,9 @@ function run_cmd {
fi
}
DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000"
DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000"
if [ "$USE_GANACHE" != "no" ]; then
make deposit_contract
echo Deploying the validator deposit contract...
@ -174,8 +174,6 @@ if [ "$USE_GANACHE" != "no" ]; then
DEPOSIT_CONTRACT_BLOCK=${OUTPUT_PIECES[1]}
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
run_cmd "(deposit maker)" "$BEACON_NODE_BIN deposits send \
@ -187,6 +185,20 @@ if [ "$USE_GANACHE" != "no" ]; then
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
$TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim"
fi

View File

@ -31,14 +31,17 @@ VALIDATORS_DIR="${SIM_ROOT}/validators"
SECRETS_DIR="${SIM_ROOT}/secrets"
SNAPSHOT_FILE="${SIMULATION_DIR}/state_snapshot.ssz"
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"
VALIDATOR_CLIENT_BIN="${GIT_ROOT}/build/validator_client"
DEPLOY_DEPOSIT_CONTRACT_BIN="${GIT_ROOT}/build/deposit_contract"
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_RPC_PORT=7000

2
vendor/nim-web3 vendored

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