From 2768a3e89e5e6333e6e4f7091bc910694c9ffabb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Thu, 11 Jul 2019 04:36:07 +0200 Subject: [PATCH 1/3] Ctrl+C handling and: - added a "quitProc" for shutting down the p2pd process, after seeing it survive an unhandled exception (this required a global var for the DaemonApi instance) - Makefile testnet0/1 targets now depend on the "p2pd" binary - prefixed the testnet command line in the build script message with "../../env.sh" so it can find the superproject's "p2pd" --- Makefile | 5 ++++- beacon_chain/beacon_node.nim | 9 +++++++++ beacon_chain/eth2_network.nim | 19 +++++++++++++------ scripts/build_testnet_node.sh | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 2a33fa060..5dead2fb6 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,9 @@ build: nat-libs: | deps + $(MAKE) --silent -C ../../ nat-libs +p2pd: | deps + + $(MAKE) --silent -C ../../ vendor/go/bin/p2pd + # Windows 10 with WSL enabled, but no distro installed, fails if "../../nimble.sh" is executed directly # in a Makefile recipe but works when prefixing it with `bash`. No idea how the PATH is overridden. test: | build deps nat-libs @@ -50,7 +53,7 @@ clean_eth2_network_simulation_files: eth2_network_simulation: | beacon_node validator_keygen clean_eth2_network_simulation_files SKIP_BUILDS=1 GIT_ROOT="$$PWD" BUILD_OUTPUTS_DIR="./build" tests/simulation/start.sh -testnet0 testnet1: | build deps nat-libs +testnet0 testnet1: | build deps nat-libs p2pd ../../env.sh scripts/build_testnet_node.sh $@ clean-testnet0: diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 0a15ba535..90f65b9d1 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -723,6 +723,15 @@ when isMainModule: if config.logLevel != LogLevel.NONE: setLogLevel(config.logLevel) + ## Ctrl+C handling + proc controlCHandler() {.noconv.} = + when defined(windows): + # workaround for https://github.com/nim-lang/Nim/issues/4057 + setupForeignThreadGc() + debug "Shutting down after having received SIGINT" + quit(1) + setControlCHook(controlCHandler) + case config.cmd of createTestnet: var deposits: seq[Deposit] diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index ee6d9d960..9d7cedb79 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -166,6 +166,8 @@ else: template tcpEndPoint(address, port): auto = MultiAddress.init(address, Protocol.IPPROTO_TCP, port) + var mainDaemon: DaemonAPI + proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async.} = var (extIp, extTcpPort, extUdpPort) = setupNat(conf) @@ -174,13 +176,18 @@ else: else: @[tcpEndPoint(extIp, extTcpPort)] keyFile = conf.ensureNetworkIdFile - info "Starting LibP2P deamon", hostAddress, announcedAddresses, keyFile - let daemon = await newDaemonApi({PSGossipSub}, - id = keyFile, - hostAddresses = @[hostAddress], - announcedAddresses = announcedAddresses) + info "Starting the LibP2P daemon", hostAddress, announcedAddresses, keyFile + mainDaemon = await newDaemonApi({PSGossipSub}, + id = keyFile, + hostAddresses = @[hostAddress], + announcedAddresses = announcedAddresses) - return await Eth2Node.init(daemon) + proc closeDaemon() {.noconv.} = + info "Shutting down the LibP2P daemon" + waitFor mainDaemon.close() + addQuitProc(closeDaemon) + + return await Eth2Node.init(mainDaemon) proc getPersistenBootstrapAddr*(conf: BeaconNodeConf, ip: IpAddress, port: Port): BootstrapAddr = diff --git a/scripts/build_testnet_node.sh b/scripts/build_testnet_node.sh index a3b2a84c2..aca85b819 100755 --- a/scripts/build_testnet_node.sh +++ b/scripts/build_testnet_node.sh @@ -27,7 +27,7 @@ fi echo echo "Done! You're now ready to connect to $NETWORK_NAME by running:" echo -echo " $BEACON_NODE_BIN" +echo " ../../env.sh $BEACON_NODE_BIN" echo echo "Database and configuration files will be placed in:" echo From d5543744098729c9bc7f2ec28ec20026e93dd180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Thu, 11 Jul 2019 12:28:11 +0200 Subject: [PATCH 2/3] Makefile: mark the "p2pd" target as phony --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5dead2fb6..fcedb50d5 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ TOOLS := beacon_node validator_keygen bench_bls_sig_agggregation state_sim TOOLS_DIRS := beacon_chain benchmarks research TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(TOOLS)) -.PHONY: all sanity-checks deps nat-libs test $(TOOLS) clean_eth2_network_simulation_files eth2_network_simulation clean-testnet0 testnet0-nocleaning testnet0 clean-testnet1 testnet1-nocleaning testnet1 clean +.PHONY: all sanity-checks deps nat-libs p2pd test $(TOOLS) clean_eth2_network_simulation_files eth2_network_simulation clean-testnet0 testnet0-nocleaning testnet0 clean-testnet1 testnet1-nocleaning testnet1 clean all: | $(TOOLS) From 44af08cc866b5e8360cbd7f239a875b8a8306a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Thu, 11 Jul 2019 13:52:59 +0200 Subject: [PATCH 3/3] beacon node wrapper script Bonus: `make NIMFLAGS="--stackTrace:on" testnet1` now works as expected --- .gitignore | 3 +++ Makefile | 2 +- scripts/build_testnet_node.sh | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ef56d2862..2306d0cae 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ build/ *.la *.exe *.dll + +/scripts/testnet*.sh + diff --git a/Makefile b/Makefile index fcedb50d5..d82ef5a0f 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ eth2_network_simulation: | beacon_node validator_keygen clean_eth2_network_simul SKIP_BUILDS=1 GIT_ROOT="$$PWD" BUILD_OUTPUTS_DIR="./build" tests/simulation/start.sh testnet0 testnet1: | build deps nat-libs p2pd - ../../env.sh scripts/build_testnet_node.sh $@ + NIM_PARAMS="$(NIM_PARAMS)" ../../env.sh scripts/build_testnet_node.sh $@ clean-testnet0: rm -rf ~/.cache/nimbus/BeaconNode/testnet0 diff --git a/scripts/build_testnet_node.sh b/scripts/build_testnet_node.sh index aca85b819..c176e0de2 100755 --- a/scripts/build_testnet_node.sh +++ b/scripts/build_testnet_node.sh @@ -3,7 +3,7 @@ [ -z "$1" ] && { echo "Usage: `basename $0` testnetX"; exit 1; } -set -eu +set -e cd $(dirname "$0") @@ -12,11 +12,13 @@ source "$NETWORK_NAME.env" cd .. -NIM_FLAGS="-d:release --lineTrace:on -d:chronicles_log_level=DEBUG -d:network_type=$NETWORK_TYPE -d:SECONDS_PER_SLOT=$SECONDS_PER_SLOT -d:SHARD_COUNT=$SHARD_COUNT -d:SLOTS_PER_EPOCH=$SLOTS_PER_EPOCH -d:DEFAULT_NETWORK=$NETWORK_NAME --hints:off --verbosity:0" +# the NIM_PARAMS env var will be set in the Makefile, based on NIMFLAGS passed on the `make` command line +# (i.e.: make NIMFLAGS="--stackTrace:on" testnet1). +OUR_NIM_FLAGS="-d:release --lineTrace:on -d:chronicles_log_level=DEBUG -d:network_type=$NETWORK_TYPE -d:SECONDS_PER_SLOT=$SECONDS_PER_SLOT -d:SHARD_COUNT=$SHARD_COUNT -d:SLOTS_PER_EPOCH=$SLOTS_PER_EPOCH -d:DEFAULT_NETWORK=$NETWORK_NAME $NIM_PARAMS" BEACON_NODE_BIN="build/${NETWORK_NAME}_node" -CMD="nim c $NIM_FLAGS -o:$BEACON_NODE_BIN beacon_chain/beacon_node" +CMD="nim c $OUR_NIM_FLAGS -o:$BEACON_NODE_BIN beacon_chain/beacon_node" echo "$CMD" $CMD @@ -24,10 +26,20 @@ if [ ! -d ~/.cache/nimbus/BeaconNode/${NETWORK_NAME}/validators ]; then $BEACON_NODE_BIN --network=$NETWORK_NAME importValidator fi +# simple wrapper script +BEACON_NODE_SCRIPT="scripts/${NETWORK_NAME}.sh" +cat > "$BEACON_NODE_SCRIPT" <