chore(rln-relay): use anvil instead of ganache in onchain tests (#2449)

* chore(rln-relay): use anvil instead of ganache in onchain tests

* fix: incl rustup in makefile
This commit is contained in:
Aaryamann Challani 2024-02-22 16:59:13 +05:30 committed by GitHub
parent 7aea145efe
commit f6332ac646
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 70 additions and 31 deletions

View File

@ -83,6 +83,20 @@ endif
################## ##################
.PHONY: deps libbacktrace .PHONY: deps libbacktrace
rustup:
ifeq (, $(shell which cargo))
# Install Rustup if it's not installed
# -y: Assume "yes" for all prompts
# --default-toolchain stable: Install the stable toolchain
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain stable
endif
anvil: rustup
ifeq (, $(shell which anvil))
# Install Anvil if it's not installed
./scripts/install_anvil.sh
endif
deps: | deps-common nat-libs waku.nims deps: | deps-common nat-libs waku.nims
@ -166,7 +180,8 @@ testcommon: | build deps
########## ##########
.PHONY: testwaku wakunode2 testwakunode2 example2 chat2 chat2bridge .PHONY: testwaku wakunode2 testwakunode2 example2 chat2 chat2bridge
testwaku: | build deps librln # install anvil only for the testwaku target
testwaku: | build deps anvil librln
echo -e $(BUILD_MSG) "build/$@" && \ echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim test -d:os=$(shell uname) $(NIM_PARAMS) waku.nims $(ENV_SCRIPT) nim test -d:os=$(shell uname) $(NIM_PARAMS) waku.nims

14
scripts/install_anvil.sh Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Install Anvil
BASE_DIR="${XDG_CONFIG_HOME:-$HOME}"
FOUNDRY_DIR="${FOUNDRY_DIR-"$BASE_DIR/.foundry"}"
FOUNDRY_BIN_DIR="$FOUNDRY_DIR/bin"
curl -L https://foundry.paradigm.xyz | bash
# Extract the source path from the download result
echo "foundryup_path: $FOUNDRY_BIN_DIR"
# run foundryup
$FOUNDRY_BIN_DIR/foundryup

View File

@ -6,7 +6,7 @@ else:
{.push raises: [].} {.push raises: [].}
import import
std/[options, osproc, sequtils, deques, streams, strutils, tempfiles], std/[options, os, osproc, sequtils, deques, streams, strutils, tempfiles],
stew/[results, byteutils], stew/[results, byteutils],
stew/shims/net as stewNet, stew/shims/net as stewNet,
testutils/unittests, testutils/unittests,
@ -38,7 +38,7 @@ proc generateCredentials(rlnInstance: ptr RLN, n: int): seq[IdentityCredential]
return credentials return credentials
# a util function used for testing purposes # a util function used for testing purposes
# it deploys membership contract on Ganache (or any Eth client available on EthClient address) # it deploys membership contract on Anvil (or any Eth client available on EthClient address)
# must be edited if used for a different contract than membership contract # must be edited if used for a different contract than membership contract
# <the difference between this and rln-v1 is that there is no need to deploy the poseidon hasher contract> # <the difference between this and rln-v1 is that there is no need to deploy the poseidon hasher contract>
proc uploadRLNContract*(ethClientAddress: string): Future[Address] {.async.} = proc uploadRLNContract*(ethClientAddress: string): Future[Address] {.async.} =
@ -117,47 +117,57 @@ proc createEthAccount(): Future[(keys.PrivateKey, Address)] {.async.} =
return (pk, acc) return (pk, acc)
# Runs Ganache daemon proc getAnvilPath(): string =
proc runGanache(): Process = var anvilPath = ""
# 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. if existsEnv("XDG_CONFIG_HOME"):
# In this way, we can directly send a SIGINT signal to the corresponding PID to gracefully terminate Ganache without dealing with multiple processes. anvilPath = joinPath(anvilPath, os.getEnv("XDG_CONFIG_HOME", ""))
else:
anvilPath = joinPath(anvilPath, os.getEnv("HOME", ""))
anvilPath = joinPath(anvilPath, ".foundry/bin/anvil")
return $anvilPath
# Runs Anvil daemon
proc runAnvil(): Process =
# Passed options are # Passed options are
# --port Port to listen on. # --port Port to listen on.
# --miner.blockGasLimit Sets the block gas limit in WEI. # --gas-limit Sets the block gas limit in WEI.
# --wallet.defaultBalance The default account balance, specified in ether. # --balance The default account balance, specified in ether.
# See ganache documentation https://www.npmjs.com/package/ganache for more details # --chain-id Chain ID of the network.
# See anvil documentation https://book.getfoundry.sh/reference/anvil/ for more details
try: try:
let runGanache = startProcess("npx", args = ["--yes", "ganache@7.9.0", "--port", "8540", "--miner.blockGasLimit", "300000000000000", "--wallet.defaultBalance", "10000"], options = {poUsePath}) let anvilPath = getAnvilPath()
let ganachePID = runGanache.processID debug "Anvil path", anvilPath
let runAnvil = startProcess(anvilPath, args = ["--port", "8540", "--gas-limit", "300000000000000", "--balance", "10000", "--chain-id", "1337"], options = {poUsePath})
let anvilPID = runAnvil.processID
# We read stdout from Ganache to see when daemon is ready # We read stdout from Anvil to see when daemon is ready
var ganacheStartLog: string var anvilStartLog: string
var cmdline: string var cmdline: string
while true: while true:
try: try:
if runGanache.outputstream.readLine(cmdline): if runAnvil.outputstream.readLine(cmdline):
ganacheStartLog.add(cmdline) anvilStartLog.add(cmdline)
if cmdline.contains("Listening on 127.0.0.1:8540"): if cmdline.contains("Listening on 127.0.0.1:8540"):
break break
except Exception, CatchableError: except Exception, CatchableError:
break break
debug "Ganache daemon is running and ready", pid=ganachePID, startLog=ganacheStartLog debug "Anvil daemon is running and ready", pid=anvilPID, startLog=anvilStartLog
return runGanache return runAnvil
except: # TODO: Fix "BareExcept" warning except: # TODO: Fix "BareExcept" warning
error "Ganache daemon run failed", err = getCurrentExceptionMsg() error "Anvil daemon run failed", err = getCurrentExceptionMsg()
# Stops Ganache daemon # Stops Anvil daemon
proc stopGanache(runGanache: Process) {.used.} = proc stopAnvil(runAnvil: Process) {.used.} =
let ganachePID = runGanache.processID let anvilPID = runAnvil.processID
# We wait the daemon to exit # We wait the daemon to exit
try: try:
# We terminate Ganache daemon by sending a SIGTERM signal to the runGanache PID to trigger RPC server termination and clean-up # We terminate Anvil daemon by sending a SIGTERM signal to the runAnvil PID to trigger RPC server termination and clean-up
discard startProcess("pkill", args = ["-f", "ganache"], options = {poUsePath}) kill(runAnvil)
debug "Sent SIGTERM to Ganache", ganachePID=ganachePID debug "Sent SIGTERM to Anvil", anvilPID=anvilPID
except: except:
error "Ganache daemon termination failed: ", err = getCurrentExceptionMsg() error "Anvil daemon termination failed: ", err = getCurrentExceptionMsg()
proc setup(): Future[OnchainGroupManager] {.async.} = proc setup(): Future[OnchainGroupManager] {.async.} =
let rlnInstanceRes = createRlnInstance(tree_path = genTempPath("rln_tree", "group_manager_onchain")) let rlnInstanceRes = createRlnInstance(tree_path = genTempPath("rln_tree", "group_manager_onchain"))
@ -185,8 +195,8 @@ proc setup(): Future[OnchainGroupManager] {.async.} =
return manager return manager
suite "Onchain group manager": suite "Onchain group manager":
# We run Ganache # We run Anvil
let runGanache {.used.} = runGanache() let runAnvil {.used.} = runAnvil()
asyncTest "should initialize successfully": asyncTest "should initialize successfully":
let manager = await setup() let manager = await setup()
@ -692,8 +702,8 @@ suite "Onchain group manager":
################################ ################################
## Terminating/removing Ganache ## Terminating/removing Anvil
################################ ################################
# We stop Ganache daemon # We stop Anvil daemon
stopGanache(runGanache) stopAnvil(runAnvil)