#!/usr/bin/env bash set -e # DEFAULTS BASE_PORT="49000" BASE_METRICS_PORT="48008" BASE_REST_PORT="47000" RESTTEST_DELAY="30" TEST_DIRNAME="resttest0_data" KILL_OLD_PROCESSES="0" #################### # argument parsing # #################### GETOPT_BINARY="getopt" if uname | grep -qi darwin; then # macOS GETOPT_BINARY=$(find /opt/homebrew/opt/gnu-getopt/bin/getopt /usr/local/opt/gnu-getopt/bin/getopt 2> /dev/null || true) [[ -f "$GETOPT_BINARY" ]] || { echo "GNU getopt not installed. Please run 'brew install gnu-getopt'. Aborting."; exit 1; } fi ! ${GETOPT_BINARY} --test > /dev/null if [ ${PIPESTATUS[0]} != 4 ]; then echo '`getopt --test` failed in this environment.' exit 1 fi OPTS="h" LONGOPTS="help,data-dir:,base-port:,base-rest-port:,base-metrics-port:,resttest-delay:,kill-old-processes" print_help() { cat < "${TOKEN_FILE}" HAVE_LSOF=0 # Windows detection if uname | grep -qiE "mingw|msys"; then MAKE="mingw32-make" else MAKE="make" which lsof &>/dev/null && HAVE_LSOF=1 || { echo "'lsof' not installed and we need it to check for ports already in use. Aborting."; exit 1; } fi # number of CPU cores if uname | grep -qi darwin; then NPROC="$(sysctl -n hw.logicalcpu)" else NPROC="$(nproc)" fi # kill lingering processes from a previous run if [[ "${HAVE_LSOF}" == "1" ]]; then for PORT in ${BASE_PORT} ${BASE_METRICS_PORT} ${BASE_REST_PORT}; do for PID in $(lsof -n -i tcp:${PORT} -sTCP:LISTEN -t); do echo -n "Found old process listening on port ${PORT}, with PID ${PID}. " if [[ "${KILL_OLD_PROCESSES}" == "1" ]]; then echo "Killing it." kill -9 ${PID} || true else echo "Aborting." exit 1 fi done done fi build_if_missing () { if [[ ! -e "${GIT_ROOT}/build/${1}" ]]; then ${MAKE} -C "${GIT_ROOT}" -j ${NPROC} ${1} fi } 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 if [[ ${EXISTING_VALIDATORS} -ne ${NUM_VALIDATORS} ]]; then build_if_missing deposit_contract rm -rf "${VALIDATORS_DIR}" "${SECRETS_DIR}" ${DEPOSIT_CONTRACT_BIN} generateSimulationDeposits \ --count="${NUM_VALIDATORS}" \ --out-validators-dir="${VALIDATORS_DIR}" \ --out-secrets-dir="${SECRETS_DIR}" \ --out-deposits-file="${DEPOSITS_FILE}" echo "All deposits prepared" fi build_if_missing nimbus_beacon_node build_if_missing resttest # 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. cleanup() { pkill -f -P $$ nimbus_beacon_node &>/dev/null || true pkill -f -P $$ resttest &>/dev/null || true sleep 2 pkill -f -9 -P $$ nimbus_beacon_node &>/dev/null || true pkill -f -9 -P $$ resttest &>/dev/null || true } trap 'cleanup' SIGINT SIGTERM EXIT echo "Creating testnet genesis..." ${NIMBUS_BEACON_NODE_BIN} \ --data-dir="${TEST_DIR}" \ createTestnet \ --deposits-file="${DEPOSITS_FILE}" \ --total-validators="${NUM_VALIDATORS}" \ --output-genesis="${SNAPSHOT_FILE}" \ --output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \ --netkey-file=network_key.json \ --insecure-netkey-password=true \ --genesis-offset=-12 # Chain that has already started allows testing empty slots # Make sure we use the newly generated genesis echo "Removing existing database..." rm -rf "${TEST_DIR}/db" DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000" DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000" echo Wrote $RUNTIME_CONFIG_FILE: tee "$RUNTIME_CONFIG_FILE" < ${LOG_NODE_FILE} 2>&1 & BEACON_NODE_STATUS=$? if [[ ${BEACON_NODE_STATUS} -eq 0 ]]; then echo "nimbus_beacon_node has been successfully started" BEACON_NODE_PID="$(jobs -p)" ${RESTTEST_BIN} \ --delay=${RESTTEST_DELAY} \ --timeout=60 \ --skip-topic=slow \ --connections=4 \ --rules-file="${RESTTEST_RULES}" \ http://${REST_ADDRESS}:${BASE_REST_PORT} \ > ${LOG_TEST_FILE} 2>&1 RESTTEST_STATUS=$? kill -SIGINT ${BEACON_NODE_PID} if [[ ${RESTTEST_STATUS} -eq 0 ]]; then echo "All tests were completed successfully!" else echo "Some of the tests failed!" tail -n 100 ${LOG_NODE_FILE} exit 1 fi else echo "nimbus_beacon_node failed to start" fi