-
Notifications
You must be signed in to change notification settings - Fork 7
/
runSystems.sh
executable file
·327 lines (266 loc) · 10.1 KB
/
runSystems.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
# This script is creating n dela voting nodes needed to run
# an evoting system. User can pass number of nodes, window attach mode useful for autotest,
# and docker usage.
set -e
# by default run on local
DOCKER=false
ATTACH=true
# by default run and setup everything
RUN=true
SETUP=true
FRONTEND=true
BACKEND=true
N_NODE=0
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
echo "This script is creating n dela voting nodes"
echo "Options:"
echo "-h | --help program help (this file)"
echo "-n | --node number of d-voting nodes"
echo "-a | --attach attach tmux window to current shell true/false, by default true"
echo "-d | --docker launch nodes on docker containers true/false, by default false"
echo "-r | --run run the nodes true/false, by default true"
echo "-s | --setup setup the nodes true/false, by default true"
echo "-f | --frontend setup the frontend true/false, by default true"
echo "-b | --backend setup the backend true/false, by default true"
exit 0
;;
-r | --run)
RUN="$2"
shift # past argument
shift # past value
;;
-s | --setup)
SETUP="$2"
shift # past argument
shift # past value
;;
-f | --frontend)
FRONTEND="$2"
shift # past argument
shift # past value
;;
-b | --backend)
BACKEND="$2"
shift # past argument
shift # past value
;;
-n | --node)
N_NODE="$2"
shift # past argument
shift # past value
;;
-a | --attach)
ATTACH="$2"
shift # past argument
shift # past value
;;
-d | --docker)
DOCKER="$2"
shift # past argument
shift # past value
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
set -o errexit
command -v tmux >/dev/null 2>&1 || {
echo >&2 "tmux is not on your PATH!"
exit 1
}
pk=adbacd10fdb9822c71025d6d00092b8a4abb5ebcb673d28d863f7c7c5adaddf3
# Launch session
s="d-voting-test"
from=0
if [ "$RUN" == true ]; then
#check that N_NODE is between 1 and 20
if [ "$N_NODE" -lt 1 ]; then
echo "N_NODE must be greater than 0"
exit 1
fi
if [ "$N_NODE" -gt 20 ]; then
echo "N_NODE must be less than 20"
exit 1
fi
# check if session already exists, if so run the kill_test.sh script
if tmux has-session -t $s 2>/dev/null; then
echo "Session $s already exists, killing it"
./kill_test.sh
fi
tmux new-session -d -s $s
# Checks that we can afford to have at least one Byzantine node and keep the
# system working, which is not possible with less than 4 nodes.
if [ $N_NODE -lt 4 ]; then
echo "Warning: the number of nodes is less than 4, it will not be resilient if one node is down"
fi
# Clean logs
rm -rf ./log
mkdir -p ./log
crypto bls signer new --save private.key --force
if [ "$DOCKER" == false ]; then
go build -o dvoting ./cli/dvoting
else
# Clean created containers and tmp dir
if [[ $(docker ps -a -q --filter ancestor=node) ]]; then
docker rm -f $(docker ps -a -q --filter ancestor=node)
fi
rm -rf ./nodedata
mkdir nodedata
# Create docker network (only run once)
docker network create --driver bridge evoting-net || true
fi
from=1
to=$N_NODE
while [ $from -le $to ]; do
echo $from
tmux new-window -t $s
window=$from
node_name="node$from"
if [ "$DOCKER" == false ]; then
tmux send-keys -t $s:$window "PROXY_LOG=info LLVL=info ./dvoting \
--config /tmp/$node_name \
start \
--postinstall \
--promaddr :$((9099 + $from)) \
--proxyaddr :$((9079 + $from)) \
--proxykey $pk \
--listen tcp://0.0.0.0:$((2000 + $from)) \
--routing tree \
--public //localhost:$((2000 + $from))| tee ./log/$node_name.log" C-m
else
docker run -d -it --env LLVL=info --name $node_name --network evoting-net -v "$(pwd)"/nodedata:/tmp --publish $((9079 + $from)):9080 node
tmux send-keys -t $s:$window "docker exec $node_name dvoting --config /tmp/$node_name start --postinstall \
--promaddr :9100 --proxyaddr :9080 --proxykey $pk --listen tcp://0.0.0.0:2001 --public //$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $node_name):2001 | tee ./log/$node_name.log" C-m
fi
((from++))
done
fi
if [ "$BACKEND" == true ]; then
if tmux has-session -t $s 2>/dev/null; then
# window for the backend
tmux new-window -t $s -n "backend"
tmux send-keys -t $s:{end} "cd web/backend && npm install && cp config.env.template config.env && npm start" C-m
else
#run it in the current shell
cd web/backend && npm start
fi
fi
# window for the frontend
if [ "$FRONTEND" == true ]; then
if tmux has-session -t $s 2>/dev/null; then
tmux new-window -t $s -n "frontend"
tmux send-keys -t $s:{end} "cd web/frontend && npm install && REACT_APP_PROXY=http://localhost:9081 REACT_APP_NOMOCK=on npm start" C-m
else
#run it in the current shell
cd web/frontend && REACT_APP_PROXY=http://localhost:9081 REACT_APP_NOMOCK=on npm start
fi
fi
((from++))
# Setup
if [ "$SETUP" == true ]; then
#check that N_NODE is between 1 and 20
if [ "$N_NODE" -lt 1 ]; then
echo "N_NODE must be greater than 0"
exit 1
fi
if [ "$N_NODE" -gt 20 ]; then
echo "N_NODE must be less than 20"
exit 1
fi
#If we runned the system as well, we should wait for it to be ready
if [ "$RUN" == true ]; then
sleep 8
fi
if tmux has-session -t $s 2>/dev/null; then
# window for the setup
GREEN='\033[0;32m'
NC='\033[0m' # No Color
if [ "$DOCKER" == false ]; then
echo "${GREEN}[1/4]${NC} connect nodes"
from=2
to=$N_NODE
while [ $from -le $to ]; do
node_name="node$from"
./dvoting --config /tmp/$node_name minogrpc join \
--address //localhost:2001 $(./dvoting --config /tmp/node1 minogrpc token)
((from++))
done
echo "${GREEN}[2/4]${NC} create a chain"
ARRAY=""
from=1
to=$N_NODE
while [ $from -le $to ]; do
node_name="node$from"
ARRAY+="--member "
ARRAY+="$(./dvoting --config /tmp/$node_name ordering export) "
((from++))
done
./dvoting --config /tmp/node1 ordering setup $ARRAY
echo "${GREEN}[3/4]${NC} setup access rights on each node"
from=1
while [ $from -le $to ]; do
node_name="node$from"
./dvoting --config /tmp/$node_name access add \
--identity $(crypto bls signer read --path private.key --format BASE64_PUBKEY)
((from++))
done
echo "${GREEN}[4/4]${NC} grant access on the chain"
./dvoting --config /tmp/node1 pool add --key private.key --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access --args access:grant_id --args 0300000000000000000000000000000000000000000000000000000000000000 --args access:grant_contract --args go.dedis.ch/dela.Evoting --args access:grant_command --args all --args access:identity --args $(crypto bls signer read --path private.key --format BASE64_PUBKEY) \
--args access:command --args GRANT
from=1
while [ $from -le $to ]; do
node_name="node$from"
./dvoting --config /tmp/node1 pool add --key private.key --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access --args access:grant_id --args 0300000000000000000000000000000000000000000000000000000000000000 --args access:grant_contract --args go.dedis.ch/dela.Evoting --args access:grant_command --args all --args access:identity --args $(crypto bls signer read --path /tmp/$node_name/private.key --format BASE64_PUBKEY) \
--args access:command --args GRANT
((from++))
done
else
echo "${GREEN}[1/4]${NC} connect nodes"
conn_token=$(docker exec node1 dvoting --config /tmp/node1 minogrpc token)
vals=($(seq 2 1 $N_NODE))
for i in "${vals[@]}"; do
docker exec node$i dvoting --config /tmp/node$i minogrpc join \
--address //$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' node1):2001 $conn_token
done
echo "${GREEN}[2/4]${NC} create a chain"
vals=($(seq 1 1 $N_NODE))
ARRAY=""
for i in "${vals[@]}"; do
ARRAY+="--member "
ARRAY+="$(docker exec node$i dvoting --config /tmp/node$i ordering export) "
echo "Node$i addr is:"
echo $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' node$i)
done
docker exec node1 dvoting --config /tmp/node1 ordering setup $ARRAY
echo "${GREEN}[3/4]${NC} setup access rights on each node"
access_token=$(docker exec node1 crypto bls signer read --path private.key --format BASE64_PUBKEY)
for i in "${vals[@]}"; do
docker exec node$i dvoting --config /tmp/node$i access add \
--identity $access_token
sleep 1
done
echo "${GREEN}[4/4]${NC} grant access on the chain"
docker exec node1 dvoting --config /tmp/node1 pool add --key private.key --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access --args access:grant_id --args 0300000000000000000000000000000000000000000000000000000000000000 --args access:grant_contract --args go.dedis.ch/dela.Evoting --args access:grant_command --args all --args access:identity --args $access_token --args access:command --args GRANT
sleep 1
for i in "${vals[@]}"; do
access_token_tmp=$(docker exec node$i crypto bls signer read --path /tmp/node$i/private.key --format BASE64_PUBKEY)
docker exec node1 dvoting --config /tmp/node1 pool add --key private.key --args go.dedis.ch/dela.ContractArg --args go.dedis.ch/dela.Access --args access:grant_id --args 0300000000000000000000000000000000000000000000000000000000000000 --args access:grant_contract --args go.dedis.ch/dela.Evoting --args access:grant_command --args all --args access:identity --args $access_token_tmp --args access:command --args GRANT
sleep 1
done
fi
fi
fi
if [ "$ATTACH" == true ]; then
tmux a
fi