2018-12-19 12:58:53 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-09-05 10:55:47 +00:00
|
|
|
set -eo pipefail
|
2019-01-25 17:35:22 +00:00
|
|
|
|
2020-06-15 22:20:31 +00:00
|
|
|
# To allow overriding the program names
|
2020-06-16 19:24:29 +00:00
|
|
|
TMUX_CMD="${TMUX_CMD:-tmux}"
|
2020-06-16 18:38:51 +00:00
|
|
|
MULTITAIL_CMD="${MULTITAIL_CMD:-multitail}"
|
|
|
|
GANACHE_CMD="${GANACHE_CMD:-ganache-cli}"
|
|
|
|
PROMETHEUS_CMD="${PROMETHEUS_CMD:-prometheus}"
|
|
|
|
CTAIL_CMD="${CTAIL_CMD:-ctail}"
|
2020-06-15 22:20:31 +00:00
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
TMUX_SESSION_NAME="${TMUX_SESSION_NAME:-nbc-sim}"
|
2020-06-15 22:20:31 +00:00
|
|
|
|
|
|
|
WAIT_GENESIS="${WAIT_GENESIS:-no}"
|
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
USE_MULTITAIL="${USE_MULTITAIL:-no}"
|
|
|
|
if [[ "$USE_MULTITAIL" != "no" ]]; then
|
2020-06-16 19:24:29 +00:00
|
|
|
type "$MULTITAIL_CMD" &>/dev/null || { echo "${MULTITAIL_CMD}" is missing; USE_MULTITAIL="no"; }
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
|
|
|
|
2020-06-16 19:24:29 +00:00
|
|
|
USE_TMUX="${USE_TMUX:-no}"
|
2020-06-25 12:58:00 +00:00
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
2020-06-16 19:24:29 +00:00
|
|
|
type "$TMUX_CMD" &>/dev/null || { echo "${TMUX_CMD}" is missing; USE_TMUX="no"; }
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
|
|
|
|
2020-06-15 22:20:31 +00:00
|
|
|
USE_GANACHE="${USE_GANACHE:-yes}"
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_GANACHE" == "yes" ]]; then
|
2020-06-16 19:24:29 +00:00
|
|
|
type "$GANACHE_CMD" &>/dev/null || { echo $GANACHE_CMD is missing; USE_GANACHE="no"; }
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
2020-06-15 22:20:31 +00:00
|
|
|
|
|
|
|
USE_PROMETHEUS="${USE_PROMETHEUS:-yes}"
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_PROMETHEUS" == "yes" ]]; then
|
2020-06-16 19:24:29 +00:00
|
|
|
type "$PROMETHEUS_CMD" &>/dev/null || { echo $PROMETHEUS_CMD is missing; USE_PROMETHEUS="no"; }
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
2020-06-15 22:20:31 +00:00
|
|
|
|
|
|
|
USE_CTAIL="${USE_CTAIL:-yes}"
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_CTAIL" == "yes" ]]; then
|
2020-06-19 17:42:28 +00:00
|
|
|
type "$CTAIL_CMD" &>/dev/null || { USE_CTAIL="no"; }
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
2020-06-15 22:20:31 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# Read in variables
|
2019-11-14 17:37:51 +00:00
|
|
|
# shellcheck source=/dev/null
|
2019-09-05 10:55:47 +00:00
|
|
|
source "$(dirname "$0")/vars.sh"
|
|
|
|
|
2019-03-28 15:18:59 +00:00
|
|
|
cd "$SIM_ROOT"
|
2019-01-14 11:35:23 +00:00
|
|
|
mkdir -p "$SIMULATION_DIR"
|
2019-03-07 13:59:28 +00:00
|
|
|
mkdir -p "$VALIDATORS_DIR"
|
2020-06-01 19:48:20 +00:00
|
|
|
mkdir -p "$SECRETS_DIR"
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-28 15:18:59 +00:00
|
|
|
cd "$GIT_ROOT"
|
2019-09-01 15:02:49 +00:00
|
|
|
|
2020-08-12 14:16:59 +00:00
|
|
|
CUSTOM_NIMFLAGS="${NIMFLAGS} -d:useSysAsserts -d:chronicles_sinks:textlines,json[file] -d:const_preset=mainnet -d:insecure -d:local_testnet"
|
2020-06-28 21:06:43 +00:00
|
|
|
GANACHE_BLOCK_TIME=5
|
2019-01-16 23:01:15 +00:00
|
|
|
|
2019-11-12 23:39:24 +00:00
|
|
|
# Run with "SLOTS_PER_EPOCH=8 ./start.sh" to change these
|
2019-09-01 15:02:49 +00:00
|
|
|
DEFS=""
|
2020-08-06 12:27:03 +00:00
|
|
|
DEFS+="-d:SECONDS_PER_ETH1_BLOCK=$GANACHE_BLOCK_TIME "
|
2019-11-12 05:35:52 +00:00
|
|
|
DEFS+="-d:MAX_COMMITTEES_PER_SLOT=${MAX_COMMITTEES_PER_SLOT:-1} " # Spec default: 64
|
2020-02-06 11:11:51 +00:00
|
|
|
DEFS+="-d:SLOTS_PER_EPOCH=${SLOTS_PER_EPOCH:-6} " # Spec default: 32
|
2019-11-12 05:35:52 +00:00
|
|
|
DEFS+="-d:SECONDS_PER_SLOT=${SECONDS_PER_SLOT:-6} " # Spec default: 12
|
2019-01-09 01:01:07 +00:00
|
|
|
|
2020-02-18 16:53:05 +00:00
|
|
|
# Windows detection
|
|
|
|
if uname | grep -qiE "mingw|msys"; then
|
|
|
|
MAKE="mingw32-make"
|
|
|
|
else
|
|
|
|
MAKE="make"
|
|
|
|
fi
|
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
make_once () {
|
|
|
|
target_flag_var="$1_name"
|
|
|
|
if [[ -z "${!target_flag_var}" ]]; then
|
|
|
|
export $target_flag_var=1
|
|
|
|
$MAKE $1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:37:36 +00:00
|
|
|
mkdir -p "${METRICS_DIR}"
|
2020-06-10 15:21:32 +00:00
|
|
|
./scripts/make_prometheus_config.sh \
|
2020-06-25 09:42:17 +00:00
|
|
|
--nodes ${TOTAL_NODES} \
|
|
|
|
--base-metrics-port ${BASE_METRICS_PORT} \
|
|
|
|
--config-file "${METRICS_DIR}/prometheus.yml" || true # TODO: this currently fails on macOS,
|
|
|
|
# but it can be considered non-critical
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
COMMANDS=()
|
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_GANACHE" == "yes" ]]; then
|
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
2020-06-28 21:06:43 +00:00
|
|
|
$TMUX_CMD new-window -d -t $TMUX_SESSION_NAME -n "$GANACHE_CMD" "$GANACHE_CMD --blockTime $GANACHE_BLOCK_TIME --gasLimit 100000000 -e 100000 --verbose"
|
2020-03-24 11:13:07 +00:00
|
|
|
else
|
2020-06-25 12:58:00 +00:00
|
|
|
echo NOTICE: $GANACHE_CMD will be started automatically only with USE_TMUX=yes
|
2020-05-25 18:55:07 +00:00
|
|
|
USE_GANACHE="no"
|
2020-03-24 11:13:07 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_PROMETHEUS" == "yes" ]]; then
|
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
|
|
|
rm -rf "${METRICS_DIR}/data"
|
|
|
|
mkdir -p "${METRICS_DIR}/data"
|
|
|
|
# TODO: Prometheus is not shut down properly on tmux kill-session
|
2020-07-23 15:58:54 +00:00
|
|
|
killall prometheus &>/dev/null || true
|
2020-08-05 12:51:55 +00:00
|
|
|
PROMETHEUS_FLAGS="--config.file=./prometheus.yml --storage.tsdb.path=./prometheus"
|
2020-06-16 18:38:51 +00:00
|
|
|
$TMUX_CMD new-window -d -t $TMUX_SESSION_NAME -n "$PROMETHEUS_CMD" "cd '$METRICS_DIR' && $PROMETHEUS_CMD $PROMETHEUS_FLAGS"
|
2020-03-24 11:13:07 +00:00
|
|
|
else
|
2020-06-25 12:58:00 +00:00
|
|
|
echo NOTICE: $PROMETHEUS_CMD will be started automatically only with USE_TMUX=yes
|
2020-05-25 18:55:07 +00:00
|
|
|
USE_PROMETHEUS="no"
|
2020-03-24 11:13:07 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-11-07 18:00:31 +00:00
|
|
|
$MAKE -j2 --no-print-directory NIMFLAGS="$CUSTOM_NIMFLAGS $DEFS" LOG_LEVEL="${LOG_LEVEL:-DEBUG}" nimbus_beacon_node nimbus_signing_process nimbus_validator_client
|
2020-06-02 19:59:51 +00:00
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
EXISTING_VALIDATORS=0
|
|
|
|
if [[ -f "$DEPOSITS_FILE" ]]; then
|
|
|
|
# We count the number of deposits by counting the number of
|
|
|
|
# occurrences of the 'deposit_data_root' field:
|
|
|
|
EXISTING_VALIDATORS=$(grep -o -i deposit_data_root "$DEPOSITS_FILE" | wc -l)
|
|
|
|
fi
|
2020-06-02 19:59:51 +00:00
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
if [[ $EXISTING_VALIDATORS -ne $NUM_VALIDATORS ]]; then
|
2020-07-27 19:26:30 +00:00
|
|
|
make_once deposit_contract
|
|
|
|
|
2020-06-02 19:59:51 +00:00
|
|
|
rm -rf "$VALIDATORS_DIR"
|
|
|
|
rm -rf "$SECRETS_DIR"
|
2020-01-23 21:16:48 +00:00
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
build/deposit_contract generateSimulationDeposits \
|
2020-06-01 19:48:20 +00:00
|
|
|
--count="${NUM_VALIDATORS}" \
|
2020-07-27 19:26:30 +00:00
|
|
|
--out-validators-dir="$VALIDATORS_DIR" \
|
2020-06-01 19:48:20 +00:00
|
|
|
--out-secrets-dir="$SECRETS_DIR" \
|
2020-07-17 20:59:50 +00:00
|
|
|
--out-deposits-file="$DEPOSITS_FILE"
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
echo "All deposits prepared"
|
2019-01-09 01:01:07 +00:00
|
|
|
fi
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [ ! -f "${SNAPSHOT_FILE}" ]; then
|
|
|
|
if [[ "${WAIT_GENESIS}" != "yes" ]]; then
|
|
|
|
echo Creating testnet genesis...
|
|
|
|
$BEACON_NODE_BIN \
|
2020-06-22 18:22:45 +00:00
|
|
|
--data-dir="${SIMULATION_DIR}/node-$BOOTSTRAP_NODE" \
|
2020-06-16 18:38:51 +00:00
|
|
|
createTestnet \
|
2020-06-28 20:47:04 +00:00
|
|
|
$WEB3_ARG \
|
2020-07-17 20:59:50 +00:00
|
|
|
--deposits-file="${DEPOSITS_FILE}" \
|
2020-06-16 18:38:51 +00:00
|
|
|
--total-validators="${NUM_VALIDATORS}" \
|
|
|
|
--output-genesis="${SNAPSHOT_FILE}" \
|
|
|
|
--output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \
|
|
|
|
--bootstrap-address=127.0.0.1 \
|
2020-06-22 18:22:45 +00:00
|
|
|
--bootstrap-port=$(( BASE_P2P_PORT + BOOTSTRAP_NODE )) \
|
2020-08-25 12:49:05 +00:00
|
|
|
--netkey-file=network_key.json \
|
|
|
|
--insecure-netkey-password=true \
|
2020-06-29 12:02:30 +00:00
|
|
|
--genesis-offset=30 # Delay in seconds
|
2020-06-16 18:38:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-06-15 22:20:31 +00:00
|
|
|
function run_cmd {
|
|
|
|
i=$1
|
|
|
|
CMD=$2
|
|
|
|
bin_name=$3
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
2020-06-15 22:20:31 +00:00
|
|
|
echo "Starting node $i..."
|
2020-06-16 19:45:52 +00:00
|
|
|
$TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim"
|
2020-07-23 15:58:54 +00:00
|
|
|
$TMUX_CMD split-window -t "${TMUX_SESSION_NAME}" "if ! $CMD; then read; fi"
|
2020-06-16 18:38:51 +00:00
|
|
|
$TMUX_CMD select-layout -t "${TMUX_SESSION_NAME}:sim" tiled
|
2020-06-15 22:20:31 +00:00
|
|
|
elif [[ "$USE_MULTITAIL" != "no" ]]; then
|
2020-06-22 18:22:45 +00:00
|
|
|
if [[ "$i" == "$BOOTSTRAP_NODE" ]]; then
|
2020-06-15 22:20:31 +00:00
|
|
|
SLEEP="0"
|
|
|
|
else
|
|
|
|
SLEEP="3"
|
|
|
|
fi
|
|
|
|
# "multitail" closes the corresponding panel when a command exits, so let's make sure it doesn't exit
|
|
|
|
COMMANDS+=( " -cT ansi -t '$bin_name #$i' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
|
|
|
|
else
|
|
|
|
eval "${CMD}" &
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:08:54 +00:00
|
|
|
DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000"
|
|
|
|
DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [ "$USE_GANACHE" != "no" ]; then
|
2020-07-27 19:26:30 +00:00
|
|
|
make_once deposit_contract
|
2020-06-15 22:20:31 +00:00
|
|
|
echo Deploying the validator deposit contract...
|
2020-06-28 21:06:43 +00:00
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
DEPLOY_CMD_OUTPUT=$($DEPOSIT_CONTRACT_BIN deploy $WEB3_ARG)
|
2020-06-28 21:06:43 +00:00
|
|
|
# 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
|
2020-06-15 22:20:31 +00:00
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$WAIT_GENESIS" == "yes" ]]; then
|
2020-07-27 19:26:30 +00:00
|
|
|
run_cmd "(deposit maker)" "$DEPOSIT_CONTRACT_BIN sendDeposits \
|
2020-07-17 20:59:50 +00:00
|
|
|
--deposits-file='$DEPOSITS_FILE' \
|
2020-06-17 16:12:42 +00:00
|
|
|
--min-delay=0 --max-delay=1 \
|
2020-06-15 22:20:31 +00:00
|
|
|
$WEB3_ARG \
|
|
|
|
--deposit-contract=${DEPOSIT_CONTRACT_ADDRESS}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-07-09 22:08:54 +00:00
|
|
|
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,
|
2020-08-06 09:08:54 +00:00
|
|
|
"GENESIS_FORK_VERSION": "0x00000000",
|
|
|
|
"ETH1_FOLLOW_DISTANCE": 1,
|
2020-07-09 22:08:54 +00:00
|
|
|
},
|
|
|
|
"depositContractAddress": "${DEPOSIT_CONTRACT_ADDRESS}",
|
|
|
|
"depositContractDeployedAt": "${DEPOSIT_CONTRACT_BLOCK}"
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2020-06-16 19:24:29 +00:00
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
|
|
|
$TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim"
|
|
|
|
fi
|
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
# Delete any leftover address files from a previous session
|
2020-06-22 18:22:45 +00:00
|
|
|
if [ -f "${BOOTSTRAP_ENR_FILE}" ]; then
|
|
|
|
rm "${BOOTSTRAP_ENR_FILE}"
|
2018-12-28 16:51:40 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-30 13:59:57 +00:00
|
|
|
# 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.
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_MULTITAIL" == "no" && "$USE_TMUX" != "yes" ]]; then
|
2020-11-07 18:00:31 +00:00
|
|
|
trap 'pkill -P $$ nimbus_beacon_node' SIGINT EXIT
|
2019-03-28 15:18:59 +00:00
|
|
|
fi
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
LAST_WAITING_NODE=0
|
2020-01-23 18:18:50 +00:00
|
|
|
|
2020-06-22 18:22:45 +00:00
|
|
|
for i in $(seq $BOOTSTRAP_NODE -1 $TOTAL_USER_NODES); do
|
|
|
|
if [[ "$i" != "$BOOTSTRAP_NODE" && "$USE_MULTITAIL" == "no" ]]; then
|
2020-06-05 09:57:40 +00:00
|
|
|
# Wait for the master node to write out its address file
|
2020-06-22 18:22:45 +00:00
|
|
|
while [ ! -f "${BOOTSTRAP_ENR_FILE}" ]; do
|
2020-06-05 09:57:40 +00:00
|
|
|
if (( LAST_WAITING_NODE != i )); then
|
2020-06-22 18:22:45 +00:00
|
|
|
echo Waiting for $BOOTSTRAP_ENR_FILE to appear...
|
2020-06-05 09:57:40 +00:00
|
|
|
LAST_WAITING_NODE=i
|
|
|
|
fi
|
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-06-19 09:21:17 +00:00
|
|
|
run_cmd $i "${SIM_ROOT}/run_node.sh ${i} --verify-finalization" "node"
|
2020-06-05 09:57:40 +00:00
|
|
|
|
2020-07-15 13:15:25 +00:00
|
|
|
if [ "${USE_BN_VC_VALIDATOR_SPLIT:-}" == "yes" ]; then
|
|
|
|
# start the VC with a few seconds of delay so that there are less RPC connection retries
|
2020-06-19 09:21:17 +00:00
|
|
|
run_cmd $i "sleep 3 && ${SIM_ROOT}/run_validator.sh ${i}" "validator"
|
2020-06-05 09:57:40 +00:00
|
|
|
fi
|
2018-12-19 12:58:53 +00:00
|
|
|
done
|
|
|
|
|
2020-06-15 22:20:31 +00:00
|
|
|
if [[ "$USE_CTAIL" != "no" ]]; then
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
|
|
|
$TMUX_CMD new-window -d -t $TMUX_SESSION_NAME -n "$CTAIL_CMD" "$CTAIL_CMD tail -q -n +1 -f ${SIMULATION_DIR}/node-*/beacon_node.log"
|
2020-06-15 22:20:31 +00:00
|
|
|
else
|
2020-06-16 18:38:51 +00:00
|
|
|
echo NOTICE: $CTAIL_CMD will be started automatically only with USE_TMUX=1
|
2020-06-15 22:20:31 +00:00
|
|
|
USE_CTAIL="no"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
if [[ "$USE_TMUX" == "yes" ]]; then
|
2020-03-24 11:13:07 +00:00
|
|
|
# kill the console window in the pane where the simulation is running
|
2020-06-16 18:38:51 +00:00
|
|
|
$TMUX_CMD kill-pane -t $TMUX_SESSION_NAME:sim.0
|
|
|
|
$TMUX_CMD select-window -t "${TMUX_SESSION_NAME}:sim"
|
|
|
|
$TMUX_CMD select-layout tiled
|
2020-01-23 18:18:50 +00:00
|
|
|
elif [[ "$USE_MULTITAIL" != "no" ]]; then
|
2020-06-16 18:38:51 +00:00
|
|
|
eval $MULTITAIL_CMD -s 3 -M 0 -x \"Nimbus beacon chain\" "${COMMANDS[@]}"
|
2019-02-20 02:22:25 +00:00
|
|
|
else
|
|
|
|
wait # Stop when all nodes have gone down
|
|
|
|
fi
|