From 894f73462d659058199b9c55cd18a71944ba5c02 Mon Sep 17 00:00:00 2001 From: stubbsta Date: Thu, 23 May 2024 16:25:55 +0200 Subject: [PATCH] get anvil account private keys and use nwaku container index to select one --- docker-compose.yml | 16 ++++++++++++---- parseAccountsDetails.sh | 23 +++++++++++++++++++++++ run_nwaku.sh | 33 +++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 parseAccountsDetails.sh diff --git a/docker-compose.yml b/docker-compose.yml index 759eb58..b8e1d5d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,16 +16,20 @@ services: com.centurylinklabs.watchtower.enable: '${WATCHTOWER_ENABLED:-false}' ports: - 0.0.0.0:8545:8545 + environment: + - ANVIL_ACC=${ACCOUNTS} command: - anvil --port=8545 --host=0.0.0.0 --chain-id=1337 - --accounts=1 + --accounts=${NUM_NWAKU_NODES:-5} --allow-origin=* --block-time=12 --silent - --config-out=anvil-config.txt + --config-out=/shared/anvil-config.txt + volumes: + - accounts-volume:/shared networks: - simulation @@ -74,7 +78,6 @@ services: entrypoint: sh environment: - RPC_URL=${RPC_URL:-http://foundry:8545} - - PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 - RLN_CONTRACT_ADDRESS=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0 - RLN_CREDENTIAL_PATH=/keystore.json - RLN_CREDENTIAL_PASSWORD=passw123 @@ -82,6 +85,8 @@ services: - '/opt/run_nwaku.sh' volumes: - ./run_nwaku.sh:/opt/run_nwaku.sh:Z + - ./parseAccountsDetails.sh:/opt/parseAccountsDetails.sh:Z + - accounts-volume:/shared depends_on: contract-repo-deployer: condition: service_completed_successfully @@ -213,4 +218,7 @@ services: - redis - foundry networks: - - simulation \ No newline at end of file + - simulation + +volumes: + accounts-volume: \ No newline at end of file diff --git a/parseAccountsDetails.sh b/parseAccountsDetails.sh new file mode 100644 index 0000000..bfb3e3e --- /dev/null +++ b/parseAccountsDetails.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Read the JSON file +json_content=$(cat /shared/anvil-config.txt) + +# Extract available_accounts and private_keys arrays using jq +available_accounts=$(echo "$json_content" | jq -r '.available_accounts[]') +private_keys=$(echo "$json_content" | jq -r '.private_keys[]') + +# Convert the extracted strings to Bash arrays +available_accounts_array=() +while IFS= read -r account; do + available_accounts_array+=("$account") +done <<< "$available_accounts" + +private_keys_array=() +while IFS= read -r key; do + private_keys_array+=("$key") +done <<< "$private_keys" + +# echo "ACCOUNT: ${available_accounts_array[$1-1]}" +# echo "PRIVATE KEY: ${private_keys_array[$1-1]}" +echo ${private_keys_array[$1-1]} diff --git a/run_nwaku.sh b/run_nwaku.sh index da2d90e..70e4783 100755 --- a/run_nwaku.sh +++ b/run_nwaku.sh @@ -1,7 +1,9 @@ -#!/bin/sh +#!/bin/bash # Install bind-tools package used for domainname resolution apk add bind-tools +apk add jq +apk add bash if test -f .env; then echo "Using .env file" @@ -33,18 +35,35 @@ else fi fi +IP=$(ip a | grep "inet " | grep -Fv 127.0.0.1 | sed 's/.*inet \([^/]*\).*/\1/') +echo "IP: $IP" + +# get the service name you specified in the docker-compose.yml +# by a reverse DNS lookup on the IP +SERVICE=`dig -x $IP +short | cut -d'_' -f2` + +# the number of replicas is equal to the A records +# associated with the service name +COUNT=`dig $SERVICE +short | wc -l` + +# extract the replica number from the same PTR entry +INDEX=`dig -x $IP +short | sed 's/.*_\([0-9]*\)\..*/\1/'` + +# Hello +echo "Hello I'm container $INDEX of $COUNT" + + +########################################################################### if test -f .$RLN_CREDENTIAL_PATH; then echo "$RLN_CREDENTIAL_PATH already exists. Use it instead of creating a new one." else - - wait_time=$((RANDOM % 400)) - echo "Waiting $wait_time seconds before generating RLN keystore to avoid collision with other nodes." - sleep $wait_time; + val=$(/bin/bash ./opt/parseAccountsDetails.sh $INDEX) + echo $val echo "Generating RLN keystore" /usr/bin/wakunode generateRlnKeystore \ --rln-relay-eth-client-address="$RPC_URL" \ - --rln-relay-eth-private-key=$PRIVATE_KEY \ + --rln-relay-eth-private-key=$val \ --rln-relay-eth-contract-address=$RLN_CONTRACT_ADDRESS \ --rln-relay-cred-path=$RLN_CREDENTIAL_PATH \ --rln-relay-cred-password=$RLN_CREDENTIAL_PASSWORD \ @@ -52,8 +71,6 @@ else --execute fi -IP=$(ip a | grep "inet " | grep -Fv 127.0.0.1 | sed 's/.*inet \([^/]*\).*/\1/') - echo "I am a nwaku node" # Get an unique node index based on the container's IP