This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 632
/
common-functions.sh
323 lines (275 loc) · 8.23 KB
/
common-functions.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
#!/usr/bin/env bash
base_common="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function find_binary {
local nix=""
[[ -z $2 ]] || nix="--nix"
pushd "$base_common/.." > /dev/null || exit
binpath=$(stack ${nix} path --local-install-root)/bin
popd > /dev/null || exit
echo "$binpath/$1"
}
function find_build_binary {
local nix=""
[[ -z $2 ]] || nix="--nix"
pushd "$base_common/.." > /dev/null || exit
binpath=$(stack ${nix} path --dist-dir)/build
popd > /dev/null || exit
echo "$binpath/$1/$1"
}
function ensure_run {
if [[ "$1" == "" ]]
then
local run_dir="$base_common/../run"
else
run_dir="$1"
fi
mkdir -p "$run_dir"
}
LOGS_TIME=$(date '+%F_%H%M%S')
function ensure_logs {
if [[ $1 == "" ]]
then
logs_dir="$base_common/../logs/$LOGS_TIME"
else
logs_dir=$1
fi
mkdir -p "${logs_dir}/logs"
}
function dump_path {
ensure_logs
echo -n "$logs_dir/dump/$1"
}
function logs {
local log_file=$2
ensure_logs "$1"
local conf_dir="$logs_dir/conf"
local template_name="../log-configs/template-demo.yaml"
if [[ "$LOG_TEMPLATE" != "" ]]; then
local template="$LOG_TEMPLATE"
else
local template_name="../log-configs/template-demo.yaml"
local template="$base_common/$template_name"
fi
mkdir -p "$conf_dir"
mkdir -p "$logs_dir/dump"
local conf_file="$conf_dir/$log_file.yaml"
# log files are named under the $logs_dir directory
# sed "s|{{file}}|$logs_dir/$log_file|g" "$template" > "$conf_file"
sed "s|{{file}}|$log_file|g" "$template" > "$conf_file"
echo -n " --json-log=$logs_dir/logs/node$i.json "
echo -n " --logs-prefix $logs_dir/logs --log-config $conf_file "
}
function get_port {
local i=$1
local i2=$i
if [[ $i -lt 10 ]]; then
i2="0$i"
fi
echo "30$i2"
}
function dht_key {
local i=$1
local i2=$i
if [[ $i -lt 10 ]]; then
i2="0$i"
fi
$(find_binary cardano-dht-keygen) -n "000000000000$i2" | tr -d '\n'
}
# Generates kademliaN.yaml and topologyN.yaml for demo setup
# for N \in {0..n-1} where n is number of nodes specified.
function gen_kademlia_topology {
local total_nodes=$1
local npred=$((total_nodes-1))
if [[ $total_nodes -le 0 ]]; then
echo "gen_kademlia_topology: n should be positive, but is $total_nodes"
exit
fi
if [[ "$2" == "" ]]; then
local out_dir="./run/"
else
local out_dir="$2"
fi
echo "Cleaning up topology/kademlia files"
rm -fv "$out_dir/topology*.yaml"
rm -fv "$out_dir/kademlia*.yaml"
echo "Generating new topology/kademlia files"
for ((i=0; i<=total_nodes; i++)); do
# generate kademlia config
if [[ "$i" -eq "$total_nodes" ]]; then
local kfile="$out_dir/kademlia_explorer.yaml"
local others_n="$total_nodes"
else
local kfile="$out_dir/kademlia$i.yaml"
local others_n="$npred"
fi
touch "$kfile"
if [[ "$total_nodes" -eq 1 ]]; then
echo "peers: []" > "$kfile"
else
echo "peers: " > "$kfile"
fi
for ((j=0; j<=others_n; j++)); do
if [[ $j -eq $i ]]; then continue; fi
{
echo " - host: '127.0.0.1'"
echo " port: 300$j"
} >> "$kfile"
done
{
echo "address:"
echo " host: '127.0.0.1'"
echo " port: 300$i"
} >> "$kfile"
# generate topology config (it's the same for all nodes)
# we generate n-1 topology configs, there's no explorer topology (should there be?)
if [[ $i -eq $total_nodes ]]; then continue; fi
local tfile="$out_dir/topology$i.yaml"
echo "nodes: " > "$tfile"
for ((j=0; j<=npred; j++)); do
local routes="["
for ((k=0; k<=npred; k++)); do
if [ "$k" -eq "$j" ]; then continue; fi
routes="$routes[\"node$k\"], "
done
# If we have explorer add it so that the other nodes converse with it.
routes="$routes[\"explorer\"]]"
{
echo " \"node$j\":"
echo " type: core"
echo " region: undefined"
echo " static-routes: $routes"
echo " addr: 127.0.0.1"
echo " port: 300$j"
} >> "$tfile"
# add explorer (as relay node)
if [[ $j -eq $npred ]]; then
# count port
local exp=$((j + 1))
# explorers routes
local exr="["
for ((k=0; k<=npred; k++)); do
exr="$exr[\"node$k\"]"
# don't put comma after last element and after pre-last element of last list item
if ! ([ "$k" -eq "$npred" ]); then
exr=$exr", "
fi
done
exr="$exr]"
{
echo ' "explorer":'
echo " type: relay"
echo " region: undefined"
echo " static-routes: $exr"
echo " addr: 127.0.0.1"
echo " port: 300${exp[0]}"
} >> "$tfile"
fi
done
done
}
function node_cmd {
local i=$1
local wallet_args=$2
local system_start=$3
local config_dir=$4
local conf_file=$5
local log_dir=$6
local run_dir=$7
local reb=''
local web=''
local configuration=''
ensure_run "$run_dir"
keys_args="--genesis-secret $i"
if [[ "$CSL_PRODUCTION" != "" ]]; then
keys_args="--keyfile \"secrets/secret-$((i+1)).key\""
elif [[ "$DEMO_GENESIS_KEYS_DIR" != "" ]]; then
keys_args="--keyfile \"$DEMO_GENESIS_KEYS_DIR/key$i.sk\""
fi
if [[ $NO_REBUILD == "" ]]; then
reb=" --rebuild-db "
fi
#shellcheck disable=SC2153
if [[ "$REPORT_SERVER" != "" ]]; then
report_server=" --report-server $REPORT_SERVER "
fi
if [[ "$CSL_RTS" != "" ]] && [[ $i -eq 0 ]]; then
rts_opts="+RTS -N -pa -A6G -qg -RTS"
fi
if [[ "$conf_file" != "" ]]; then
configuration=" --configuration-file $conf_file "
fi
if [[ "$CONFIG_KEY" != "" ]]; then
configuration=" $configuration --configuration-key $CONFIG_KEY "
fi
local topology_file="$config_dir/topology$i.yaml"
#local kademlia_file="$config_dir/kademlia$i.yaml"
local updater_file="$config_dir/updater$i.sh"
echo -n " --db-path $run_dir/node-db$i $rts_opts $reb $keys_args"
ekg_server="127.0.0.1:$((8000+i))"
export statsd_server="127.0.0.1:$((8125+i))"
# A sloppy test but it'll do for now.
local topology_first_six_bytes
topology_first_six_bytes=$(head -c 6 "$topology_file" )
if [[ "$topology_first_six_bytes" != "wallet" ]]; then
# 'wallet' may not bind, otherwise throws error "BehindNAT topology is used, no bind address is expected"
echo -n " --listen 127.0.0.1:$(get_port "$i")"
fi
if [[ "$configuration" != "" ]]; then
echo -n " $configuration "
fi
declare time_lord
declare stats
echo -n " $(logs "$log_dir" "node$i.log") ${time_lord} ${stats}"
echo -n " $web "
echo -n " $report_server "
echo -n " $wallet_args "
echo -n " --system-start $system_start"
echo -n " --metrics +RTS -T -RTS"
echo -n " --ekg-server $ekg_server"
#echo -n " --statsd-server $statsd_server"
echo -n " --node-id node$i"
echo -n " --topology $topology_file"
#echo -n " --kademlia $kademlia_file"
# Use the policies option if you want to change enqueue/dequeue/failure
# policies without re-compiling. See example files
# scripts/policies/policy_core.yaml
# scripts/policies/policy_relay.yaml
#echo -n " --policies $config_dir/policy$i.yaml"
echo -n " --update-latest-path $updater_file"
echo -n " --update-with-package"
echo -n " --update-server 'http://127.0.0.1:10228/'"
echo ''
sleep 0.8
}
function bench_cmd {
local i=$1
local system_start=$2
local time=$3
local conc=$4
local delay=$5
ensure_run
echo -n "$(find_binary cardano-auxx)"
# This assumes that the n-1 node is the relay
echo -n " --peer 127.0.0.1:$(get_port $((i-1)))"
echo -n " $(logs "" node_auxx.log)"
echo -n " --system-start $system_start"
# First arg is the number of transactions with input from genesis block and
# the second is the total number of transactions to be sent.
echo -n " cmd --commands \"send-to-all-genesis $time $time $conc $delay ./tps-sent.csv\""
echo -n " --configuration-key smallbench "
echo -n " --rebuild-db "
echo ''
}
function has_nix {
which nix-shell 2> /dev/null
return $?
}
function stack_build {
if [[ $(has_nix) == 0 ]]; then
echo "Building with nix-shell"
stack --nix build --test --no-run-tests --bench --no-run-benchmarks --fast
else
echo "Building normally"
stack build --test --no-run-tests --bench --no-run-benchmarks --fast
fi
}