From 8333eb9cf15858c5a2b78294fd0df92e3703602a Mon Sep 17 00:00:00 2001 From: G <28568419+s1fr0@users.noreply.github.com> Date: Tue, 8 Nov 2022 14:28:11 +0100 Subject: [PATCH] chore(rln): update ganache (#1347) * chore: build rln dependecies only when building v2 * chore(rln): deprecate ganache-cli; move installation from makefile to test + uninstall; gracefully terminate ganache * chore: add package.json to gitignore * fix(rln): move ganache package to build folder and remove package once uninstalled * refactor(rln): (un)install/run/stop ganache with separate procs Co-authored-by: Lorenzo Delgado Co-authored-by: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> --- .gitignore | 1 + Makefile | 24 +------ tests/v2/test_waku_rln_relay_onchain.nim | 83 +++++++++++++++++++++++- 3 files changed, 85 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 2ac3b83c9..f531e937a 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ rln *.log package-lock.json +package.json node_modules/ /.update.timestamp diff --git a/Makefile b/Makefile index 335768cc1..6424d82c0 100644 --- a/Makefile +++ b/Makefile @@ -153,13 +153,9 @@ sim1: | build deps wakunode1 ## Waku v2 targets -test2: | build deps deps2 installganache +test2: | build deps deps2 echo -e $(BUILD_MSG) "build/$@" && \ $(ENV_SCRIPT) nim test2 $(NIM_PARAMS) waku.nims - # the following command (pkill -f ganache-cli) attempts to kill ganache-cli process on macos - # if we do not kill the process then it would hang there and causes issue in GitHub Actions macos job (the job never finsihes) - (([[ $(detected_OS) = macOS ]] && \ - pkill -f ganache-cli) || true) wakunode2: | build deps deps2 echo -e $(BUILD_MSG) "build/$@" && \ @@ -219,25 +215,9 @@ endif # control compilation of rln tests that require on chain interaction ifeq ($(ONCHAIN_RLN), true) NIM_PARAMS := $(NIM_PARAMS) -d:onchain_rln -else -ifeq ($(CI), true) -ifeq ($(detected_OS), macOS) +else ifeq ($(CI), true) NIM_PARAMS := $(NIM_PARAMS) -d:onchain_rln endif -endif -endif - - -installganache: -ifeq ($(ONCHAIN_RLN), true) - npm install ganache-cli; npx ganache-cli -p 8540 -g 0 -l 3000000000000& -else -ifeq ($(CI), true) -ifeq ($(detected_OS), macOS) - npm install ganache-cli; npx ganache-cli -p 8540 -g 0 -l 3000000000000& -endif -endif -endif rlnlib: ifeq ($(RLNKILIC), true) diff --git a/tests/v2/test_waku_rln_relay_onchain.nim b/tests/v2/test_waku_rln_relay_onchain.nim index 8f4cc3334..2937248cf 100644 --- a/tests/v2/test_waku_rln_relay_onchain.nim +++ b/tests/v2/test_waku_rln_relay_onchain.nim @@ -3,7 +3,7 @@ {.used.} import - std/options, + std/[options, osproc, streams, strutils], testutils/unittests, chronos, chronicles, stint, web3, json, stew/byteutils, stew/shims/net as stewNet, libp2p/crypto/crypto, @@ -16,6 +16,8 @@ import ../test_helpers, ./test_utils +from posix import kill, SIGINT + const RlnRelayPubsubTopic = "waku/2/rlnrelay/proto" const RlnRelayContentTopic = "waku/2/rlnrelay/proto" @@ -103,7 +105,76 @@ proc createEthAccount(): Future[(keys.PrivateKey, Address)] {.async.} = return (pk, acc) + +# Installs Ganache Daemon +proc installGanache() = + # We install Ganache. + # Packages will be installed to the ./build folder through the --prefix option + let installGanache = startProcess("npm", args = ["install", "ganache", "--prefix", "./build"], options = {poUsePath}) + let returnCode = installGanache.waitForExit() + debug "Ganache install log", returnCode=returnCode, log=installGanache.outputstream.readAll() + +# Uninstalls Ganache Daemon +proc uninstallGanache() = + # We uninstall Ganache + # Packages will be uninstalled from the ./build folder through the --prefix option. + # Passed option is + # --save: Package will be removed from your dependencies. + # See npm documentation https://docs.npmjs.com/cli/v6/commands/npm-uninstall for further details + let uninstallGanache = startProcess("npm", args = ["uninstall", "ganache", "--save", "--prefix", "./build"], options = {poUsePath}) + let returnCode = uninstallGanache.waitForExit() + debug "Ganache uninstall log", returnCode=returnCode, log=uninstallGanache.outputstream.readAll() + +# Runs Ganache daemon +proc runGanache(): Process = + # We run directly "node node_modules/ganache/dist/node/cli.js" rather than using "npx ganache", so that the daemon does not spawn in a new child process. + # In this way, we can directly send a SIGINT signal to the corresponding PID to gracefully terminate Ganache without dealing with multiple processes. + # Passed options are + # --port Port to listen on. + # --miner.blockGasLimit Sets the block gas limit in WEI. + # --wallet.defaultBalance The default account balance, specified in ether. + # See ganache documentation https://www.npmjs.com/package/ganache for more details + let runGanache = startProcess("node", args = ["./build/node_modules/ganache/dist/node/cli.js", "--port", "8540", "--miner.blockGasLimit", "300000000000000", "--wallet.defaultBalance", "10000"], options = {poUsePath}) + let ganachePID = runGanache.processID + + # We read stdout from Ganache to see when daemon is ready + var ganacheStartLog: string + var cmdline: string + while true: + if runGanache.outputstream.readLine(cmdline): + ganacheStartLog.add cmdline + if cmdline.contains("Listening on 127.0.0.1:8540"): + break + debug "Ganache daemon is running and ready", pid=ganachePID, startLog=ganacheStartLog + return runGanache + + +# Stops Ganache daemon +proc stopGanache(runGanache: Process) = + + let ganachePID = runGanache.processID + + # We gracefully terminate Ganache daemon by sending a SIGINT signal to the runGanache PID to trigger RPC server termination and clean-up + let returnCodeSIGINT = kill(ganachePID.int32, SIGINT) + debug "Sent SIGINT to Ganache", ganachePID=ganachePID, returnCode=returnCodeSIGINT + + # We wait the daemon to exit + let returnCodeExit = runGanache.waitForExit() + debug "Ganache daemon terminated", returnCode=returnCodeExit + debug "Ganache daemon run log", log=runGanache.outputstream.readAll() + procSuite "Waku-rln-relay": + + ################################ + ## Installing/running Ganache + ################################ + + # We install Ganache + installGanache() + + # We run Ganache + let runGanache = runGanache() + asyncTest "event subscription": # preparation ------------------------------ debug "ethereum client address", EthClient @@ -533,3 +604,13 @@ procSuite "Waku-rln-relay": await node.stop() await node2.stop() + + ################################ + ## Terminating/removing Ganache + ################################ + + # We stop Ganache daemon + stopGanache(runGanache) + + # We uninstall Ganache + uninstallGanache() \ No newline at end of file