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
|
|
|
|
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"
|
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-04-20 17:27:52 +00:00
|
|
|
CUSTOM_NIMFLAGS="${NIMFLAGS} -d:useSysAsserts -d:chronicles_sinks:textlines,json[file] -d:const_preset=mainnet"
|
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=""
|
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
|
|
|
|
2019-08-14 10:25:39 +00:00
|
|
|
LAST_VALIDATOR_NUM=$(( NUM_VALIDATORS - 1 ))
|
2019-03-07 13:59:28 +00:00
|
|
|
LAST_VALIDATOR="$VALIDATORS_DIR/v$(printf '%07d' $LAST_VALIDATOR_NUM).deposit.json"
|
|
|
|
|
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-03-24 11:13:07 +00:00
|
|
|
# to allow overriding the program names
|
|
|
|
MULTITAIL="${MULTITAIL:-multitail}"
|
|
|
|
TMUX="${TMUX:-tmux}"
|
|
|
|
GANACHE="${GANACHE:-ganache-cli}"
|
|
|
|
PROMETHEUS="${PROMETHEUS:-prometheus}"
|
|
|
|
TMUX_SESSION_NAME="${TMUX_SESSION_NAME:-nbc-sim}"
|
|
|
|
|
|
|
|
WAIT_GENESIS="${WAIT_GENESIS:-no}"
|
|
|
|
|
|
|
|
# Using tmux or multitail is an opt-in
|
|
|
|
USE_MULTITAIL="${USE_MULTITAIL:-no}"
|
|
|
|
type "$MULTITAIL" &>/dev/null || { echo "${MULTITAIL}" is missing; USE_MULTITAIL="no"; }
|
|
|
|
|
|
|
|
USE_TMUX="${USE_TMUX:-no}"
|
|
|
|
type "$TMUX" &>/dev/null || { echo "${TMUX}" is missing; USE_TMUX="no"; }
|
|
|
|
|
|
|
|
USE_GANACHE="${USE_GANACHE:-no}"
|
|
|
|
type "$GANACHE" &>/dev/null || { echo $GANACHE is missing; USE_GANACHE="no"; }
|
|
|
|
|
|
|
|
USE_PROMETHEUS="${LAUNCH_PROMETHEUS:-no}"
|
|
|
|
type "$PROMETHEUS" &>/dev/null || { echo $PROMETHEUS is missing; USE_PROMETHEUS="no"; }
|
|
|
|
|
|
|
|
# Prometheus config (continued inside the loop)
|
|
|
|
mkdir -p "${METRICS_DIR}"
|
|
|
|
cat > "${METRICS_DIR}/prometheus.yml" <<EOF
|
|
|
|
global:
|
|
|
|
scrape_interval: 1s
|
|
|
|
|
|
|
|
scrape_configs:
|
|
|
|
- job_name: "nimbus"
|
|
|
|
static_configs:
|
|
|
|
EOF
|
|
|
|
|
|
|
|
for i in $(seq $MASTER_NODE -1 $TOTAL_USER_NODES); do
|
|
|
|
# Prometheus config
|
|
|
|
cat >> "${METRICS_DIR}/prometheus.yml" <<EOF
|
|
|
|
- targets: ['127.0.0.1:$(( BASE_METRICS_PORT + i ))']
|
|
|
|
labels:
|
|
|
|
node: '$i'
|
|
|
|
EOF
|
|
|
|
done
|
|
|
|
|
|
|
|
COMMANDS=()
|
|
|
|
|
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
|
|
|
$TMUX new-session -s "${TMUX_SESSION_NAME}" -d
|
|
|
|
|
|
|
|
# maybe these should be moved to a user config file
|
|
|
|
$TMUX set-option -t "${TMUX_SESSION_NAME}" history-limit 999999
|
|
|
|
$TMUX set-option -t "${TMUX_SESSION_NAME}" remain-on-exit on
|
|
|
|
$TMUX set -t "${TMUX_SESSION_NAME}" mouse on
|
|
|
|
|
|
|
|
# We create a new window, so the above settings can take place
|
|
|
|
$TMUX new-window -d -t "${TMUX_SESSION_NAME}" -n "sim"
|
|
|
|
|
|
|
|
trap 'tmux kill-session -t "${TMUX_SESSION_NAME}"' SIGINT EXIT
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$USE_GANACHE" != "no" ]]; then
|
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
|
|
|
$TMUX new-window -d -t $TMUX_SESSION_NAME -n "$GANACHE" "$GANACHE"
|
|
|
|
elif [[ "$USE_MULTITAIL" != "no" ]]; then
|
|
|
|
COMMANDS+=( " -cT ansi -t '$GANACHE'" )
|
|
|
|
else
|
|
|
|
$GANACHE &
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$USE_PROMETHEUS" != "no" ]]; then
|
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
|
|
|
$TMUX new-window -d -t $TMUX_SESSION_NAME -n "$PROMETHEUS" "cd '$METRICS_DIR' && $PROMETHEUS"
|
|
|
|
else
|
|
|
|
echo "$PROMETHEUS can be used currently only with USE_TMUX=1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
|
|
|
$TMUX select-window -t "${TMUX_SESSION_NAME}:sim"
|
|
|
|
fi
|
|
|
|
|
2020-01-23 21:16:48 +00:00
|
|
|
build_beacon_node () {
|
|
|
|
OUTPUT_BIN=$1; shift
|
2020-02-14 12:35:23 +00:00
|
|
|
PARAMS="$CUSTOM_NIMFLAGS $DEFS $@"
|
2020-01-23 21:16:48 +00:00
|
|
|
echo "Building $OUTPUT_BIN ($PARAMS)"
|
2020-03-09 18:05:17 +00:00
|
|
|
$MAKE NIMFLAGS="-o:$OUTPUT_BIN $PARAMS" LOG_LEVEL="${LOG_LEVEL:-DEBUG}" beacon_node
|
2020-01-23 21:16:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 20:54:47 +00:00
|
|
|
build_beacon_node $BEACON_NODE_BIN
|
2020-01-23 21:16:48 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
if [ ! -f "${LAST_VALIDATOR}" ]; then
|
2020-03-26 20:32:13 +00:00
|
|
|
echo Building "${DEPLOY_DEPOSIT_CONTRACT_BIN}"
|
2020-02-18 16:53:05 +00:00
|
|
|
$MAKE NIMFLAGS="-o:\"$DEPLOY_DEPOSIT_CONTRACT_BIN\" $CUSTOM_NIMFLAGS $DEFS" deposit_contract
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
if [ "$WEB3_ARG" != "" ]; then
|
|
|
|
echo Deploying the validator deposit contract...
|
|
|
|
DEPOSIT_CONTRACT_ADDRESS=$($DEPLOY_DEPOSIT_CONTRACT_BIN deploy $WEB3_ARG)
|
|
|
|
echo Contract deployed at $DEPOSIT_CONTRACT_ADDRESS
|
2019-08-14 10:25:39 +00:00
|
|
|
export DEPOSIT_CONTRACT_ADDRESS
|
2019-07-18 11:36:53 +00:00
|
|
|
fi
|
2019-07-12 14:24:11 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
DELAY_ARGS=""
|
|
|
|
|
|
|
|
# Uncomment this line to slow down the initial deposits.
|
|
|
|
# This will spread them across multiple blocks which is
|
|
|
|
# a more realistic scenario.
|
|
|
|
DELAY_ARGS="--min-delay=1 --max-delay=5"
|
|
|
|
|
|
|
|
MAKE_DEPOSITS_WEB3_ARG=$WEB3_ARG
|
|
|
|
if [[ "$WAIT_GENESIS" == "no" ]]; then
|
|
|
|
MAKE_DEPOSITS_WEB3_ARG=""
|
|
|
|
fi
|
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
$BEACON_NODE_BIN makeDeposits \
|
2019-10-29 02:43:23 +00:00
|
|
|
--quickstart-deposits="${NUM_VALIDATORS}" \
|
2019-10-29 19:48:32 +00:00
|
|
|
--deposits-dir="$VALIDATORS_DIR" \
|
2020-03-24 11:13:07 +00:00
|
|
|
$MAKE_DEPOSITS_WEB3_ARG $DELAY_ARGS \
|
2019-10-28 23:04:52 +00:00
|
|
|
--deposit-contract="${DEPOSIT_CONTRACT_ADDRESS}"
|
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
|
|
|
|
2019-08-14 10:25:39 +00:00
|
|
|
if [ ! -f "${SNAPSHOT_FILE}" ]; then
|
2020-03-24 11:13:07 +00:00
|
|
|
if [[ "${WAIT_GENESIS}" == "no" ]]; then
|
|
|
|
echo Creating testnet genesis...
|
|
|
|
$BEACON_NODE_BIN \
|
|
|
|
--data-dir="${SIMULATION_DIR}/node-$MASTER_NODE" \
|
|
|
|
createTestnet \
|
|
|
|
--validators-dir="${VALIDATORS_DIR}" \
|
|
|
|
--total-validators="${NUM_VALIDATORS}" \
|
|
|
|
--output-genesis="${SNAPSHOT_FILE}" \
|
|
|
|
--output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \
|
|
|
|
--bootstrap-address=127.0.0.1 \
|
|
|
|
--bootstrap-port=$(( BASE_P2P_PORT + MASTER_NODE )) \
|
|
|
|
--genesis-offset=5 # Delay in seconds
|
|
|
|
fi
|
2018-12-19 12:58:53 +00:00
|
|
|
fi
|
|
|
|
|
2020-02-14 12:35:23 +00:00
|
|
|
rm -f beacon_node.log
|
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
# Delete any leftover address files from a previous session
|
2020-03-23 11:26:44 +00:00
|
|
|
if [ -f "${MASTER_NODE_ADDRESS_FILE}" ]; then
|
|
|
|
rm "${MASTER_NODE_ADDRESS_FILE}"
|
2018-12-28 16:51:40 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-05 22:02:39 +00:00
|
|
|
PROCESS_DASHBOARD_BIN="build/process_dashboard"
|
2019-10-28 13:28:45 +00:00
|
|
|
|
2020-02-14 12:35:23 +00:00
|
|
|
if [[ ! -f "$PROCESS_DASHBOARD_BIN" ]]; then
|
2020-02-18 16:53:05 +00:00
|
|
|
$MAKE NIMFLAGS="$CUSTOM_NIMFLAGS" process_dashboard
|
2019-10-28 13:28:45 +00:00
|
|
|
fi
|
|
|
|
|
2019-10-17 13:18:58 +00:00
|
|
|
# use the exported Grafana dashboard for a single node to create one for all nodes
|
2020-03-24 11:13:07 +00:00
|
|
|
echo Creating grafana dashboards...
|
2020-02-14 12:35:23 +00:00
|
|
|
"${PROCESS_DASHBOARD_BIN}" \
|
2019-11-11 01:28:13 +00:00
|
|
|
--nodes=${TOTAL_NODES} \
|
2019-10-17 13:18:58 +00:00
|
|
|
--in="${SIM_ROOT}/beacon-chain-sim-node0-Grafana-dashboard.json" \
|
|
|
|
--out="${SIM_ROOT}/beacon-chain-sim-all-nodes-Grafana-dashboard.json"
|
|
|
|
|
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-03-24 11:13:07 +00:00
|
|
|
if [[ "$USE_MULTITAIL" == "no" && "$USE_TMUX" == "no" ]]; then
|
2020-04-01 10:53:05 +00:00
|
|
|
trap 'pkill -P $$ 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
|
|
|
|
2019-11-12 19:24:43 +00:00
|
|
|
for i in $(seq $MASTER_NODE -1 $TOTAL_USER_NODES); do
|
2019-11-11 01:28:13 +00:00
|
|
|
if [[ "$i" != "$MASTER_NODE" && "$USE_MULTITAIL" == "no" ]]; then
|
2018-12-28 16:51:40 +00:00
|
|
|
# Wait for the master node to write out its address file
|
2020-03-23 11:26:44 +00:00
|
|
|
while [ ! -f "${MASTER_NODE_ADDRESS_FILE}" ]; do
|
2020-03-24 11:13:07 +00:00
|
|
|
if (( LAST_WAITING_NODE != i )); then
|
|
|
|
echo Waiting for $MASTER_NODE_ADDRESS_FILE to appear...
|
|
|
|
LAST_WAITING_NODE=i
|
|
|
|
fi
|
2018-12-28 16:51:40 +00:00
|
|
|
sleep 0.1
|
|
|
|
done
|
2018-12-19 12:58:53 +00:00
|
|
|
fi
|
|
|
|
|
2020-03-26 20:32:13 +00:00
|
|
|
CMD="${SIM_ROOT}/run_node.sh ${i} --verify-finalization"
|
2019-02-20 02:22:25 +00:00
|
|
|
|
2020-01-23 18:18:50 +00:00
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
2020-03-24 11:13:07 +00:00
|
|
|
echo "Starting node $i..."
|
|
|
|
echo $TMUX split-window -t "${TMUX_SESSION_NAME}" "$CMD"
|
2020-03-26 20:32:13 +00:00
|
|
|
$TMUX split-window -t "${TMUX_SESSION_NAME}" "$CMD"
|
|
|
|
$TMUX select-layout -t "${TMUX_SESSION_NAME}" tiled
|
2020-01-23 18:18:50 +00:00
|
|
|
elif [[ "$USE_MULTITAIL" != "no" ]]; then
|
2019-11-12 17:45:57 +00:00
|
|
|
if [[ "$i" == "$MASTER_NODE" ]]; then
|
2019-02-20 02:22:25 +00:00
|
|
|
SLEEP="0"
|
|
|
|
else
|
2019-11-12 17:45:57 +00:00
|
|
|
SLEEP="3"
|
2019-02-20 02:22:25 +00:00
|
|
|
fi
|
|
|
|
# "multitail" closes the corresponding panel when a command exits, so let's make sure it doesn't exit
|
|
|
|
COMMANDS+=( " -cT ansi -t 'node #$i' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
|
|
|
|
else
|
2019-08-14 10:25:39 +00:00
|
|
|
eval "${CMD}" &
|
2019-02-20 02:22:25 +00:00
|
|
|
fi
|
2018-12-19 12:58:53 +00:00
|
|
|
done
|
|
|
|
|
2020-01-23 18:18:50 +00:00
|
|
|
if [[ "$USE_TMUX" != "no" ]]; then
|
2020-03-24 11:13:07 +00:00
|
|
|
# kill the console window in the pane where the simulation is running
|
|
|
|
$TMUX kill-pane -t $TMUX_SESSION_NAME:sim.0
|
|
|
|
# kill the original console window
|
|
|
|
# (this one doesn't have the right history-limit)
|
2020-01-23 18:18:50 +00:00
|
|
|
$TMUX kill-pane -t $TMUX_SESSION_NAME:0.0
|
2020-03-26 20:32:13 +00:00
|
|
|
$TMUX select-layout -t "${TMUX_SESSION_NAME}" tiled
|
|
|
|
$TMUX attach-session -t "${TMUX_SESSION_NAME}" -d
|
2020-01-23 18:18:50 +00:00
|
|
|
elif [[ "$USE_MULTITAIL" != "no" ]]; then
|
2019-02-28 21:21:29 +00:00
|
|
|
eval $MULTITAIL -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
|