-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_nwaku.sh
executable file
·152 lines (131 loc) · 4.97 KB
/
run_nwaku.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh
# Check Linux Distro Version - it can differ depending on the nwaku image used
OS=$(cat /etc/os-release)
if echo $OS | grep -q "Debian"; then
echo "The operating system is Debian."
apt update
apt install -y dnsutils
apt install -y jq
elif echo $OS | grep -q "Alpine"; then
echo "The operating system is Alpine."
apk add bind-tools
apk add jq
fi
if test -f .env; then
echo "Using .env file"
. $(pwd)/.env
fi
IP=$(ip a | grep "inet " | grep -Fv 127.0.0.1 | sed 's/.*inet \([^/]*\).*/\1/')
# Function to extract IP address from URL, resolve the IP and replace it in the original URL
get_ip_address_and_replace() {
local url=$1
local domain_name=$(echo $RPC_URL | awk -F[/:] '{print $4}')
local ip_address=$(dig +short $domain_name)
valid_rpc_url="$(echo "$url" | sed "s/$domain_name/$ip_address/g")"
echo $valid_rpc_url
}
# the format of the RPC URL is checked in the generateRlnKeystore command and hostnames are not valid
pattern="^(https?):\/\/((localhost)|([\w_-]+(?:(?:\.[\w_-]+)+)))(:[0-9]{1,5})?([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])*"
# Perform regex matching
if echo "$RPC_URL" | grep -q "$pattern"; then
echo "RPC URL is valid"
else
echo "RPC URL is invalid: $RPC_URL. Attempting to resolve hostname."
resolved_rpc_url="$(get_ip_address_and_replace $RPC_URL)"
if [ -z "$resolved_rpc_url" ]; then
echo -e "Failed to retrieve IP address for $RPC_URL\n"
else
echo -e "Resolved RPC URL for $RPC_URL: $resolved_rpc_url"
RPC_URL="$resolved_rpc_url"
fi
fi
#Function to get the index of the container and use it to retrieve a private key to be used to generate the keystore, allowing for either dash or underscore container name format (for docker-compose backward compatibility)
get_private_key(){
# Read the JSON file
json_content=$(cat /shared/anvil-config.txt)
# Check if json_content has a value
if [ -z "$json_content" ]; then
echo "Error: Failed to read the JSON file or the file is empty." >&2
return 1
fi
# Extract private_keys json array using jq
private_keys=$(echo "$json_content" | jq -r '.private_keys[]')
CNTR=`dig -x $IP +short | cut -d'.' -f1`
INDEX=`echo $CNTR | sed 's/.*[-_]\([0-9]*\)/\1/'`
if [ $? -ne 0 ] || [ -z "$INDEX" ]; then
echo "Error: Failed to determine the replica index from IP." >&2
return 1
fi
# iterate through list of private keys and get the one corresponding to the container index
# we need to iterate because array objects cannot be used in /bin/ash (Alpine) and a separate script would need to be called to use bash
current_index=1
for key in $private_keys
do
if [ $current_index -eq $INDEX ]; then
pk=$key
echo $key
break
fi
current_index=$((current_index+1))
done
if [ -z "$pk" ]; then
echo "Error: Failed to get private key for the container with index=$INDEX." >&2
return 1
fi
}
if test -f .$RLN_CREDENTIAL_PATH; then
echo "$RLN_CREDENTIAL_PATH already exists. Use it instead of creating a new one."
else
private_key="$(get_private_key)"
echo "Private key: $private_key"
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-contract-address=$RLN_CONTRACT_ADDRESS \
--rln-relay-cred-path=$RLN_CREDENTIAL_PATH \
--rln-relay-cred-password=$RLN_CREDENTIAL_PASSWORD \
--rln-relay-user-message-limit=$RLN_RELAY_MSG_LIMIT \
--rln-relay-epoch-sec=$RLN_RELAY_EPOCH_SEC \
--log-level=DEBUG \
--execute
fi
echo "I am a nwaku node"
RETRIES=${RETRIES:=10}
while [ -z "${BOOTSTRAP_ENR}" ] && [ ${RETRIES} -ge 0 ]; do
BOOTSTRAP_ENR=$(wget -qO- http://bootstrap:8645/debug/v1/info --header='Content-Type:application/json' 2> /dev/null | sed 's/.*"enrUri":"\([^"]*\)".*/\1/');
echo "Bootstrap node not ready, retrying (retries left: ${RETRIES})"
sleep 1
RETRIES=$(( $RETRIES - 1 ))
done
if [ -z "${BOOTSTRAP_ENR}" ]; then
echo "Could not get BOOTSTRAP_ENR and none provided. Failing"
exit 1
fi
echo "Using bootstrap node: ${BOOTSTRAP_ENR}"
exec /usr/bin/wakunode\
--relay=true\
--lightpush=true\
--max-connections=250\
--rest=true\
--rest-address=0.0.0.0\
--rest-port=8645\
--rln-relay=true\
--rln-relay-dynamic=true\
--rln-relay-eth-client-address="$RPC_URL"\
--rln-relay-eth-contract-address=$RLN_CONTRACT_ADDRESS\
--rln-relay-cred-path=$RLN_CREDENTIAL_PATH\
--rln-relay-cred-password=$RLN_CREDENTIAL_PASSWORD\
--rln-relay-tree-path="rlnv2_tree1"\
--rln-relay-epoch-sec=$RLN_RELAY_EPOCH_SEC\
--rln-relay-user-message-limit=$RLN_RELAY_MSG_LIMIT\
--dns-discovery=true\
--discv5-discovery=true\
--discv5-enr-auto-update=True\
--log-level=DEBUG\
--metrics-server=True\
--metrics-server-address=0.0.0.0\
--discv5-bootstrap-node=${BOOTSTRAP_ENR}\
--nat=extip:${IP}\
--pubsub-topic=/waku/2/rs/66/0\
--cluster-id=66