-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrun_dcl_node
executable file
·559 lines (438 loc) · 15.9 KB
/
run_dcl_node
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/bash
# Copyright 2020 DSR Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -eu
set -o pipefail
export PATH="$PATH:/usr/bin"
SCRIPT_PATH="$(readlink -f "$0")"
BASEDIR="$(dirname "$SCRIPT_PATH")"
JSON_PARSER=
# TODO errors to stderr
#
# TODO
# - check wherther ip6 and domain names in persistent-peers work
# - do not show private data
# - requries 'dcld keys' adjustment, pipes configuration is not an option
SERVICE_FILE="./cosmovisor.service"
PERSISTENT_PEERS_FILE='./persistent_peers.txt'
GENESIS_IN_FILE="./genesis.json"
DCL_HOME="$HOME/.dcl"
CONFIG_FILE="$DCL_HOME/config/config.toml"
GENESIS_FILE="$DCL_HOME/config/genesis.json"
GENESIS_APP_DIR="$DCL_HOME/cosmovisor/genesis/bin"
CURRENT_APP_DIR="$DCL_HOME/cosmovisor/current/bin"
CLI_PORT=26657
NT_GENESIS="genesis"
NT_VALIDATOR="validator"
NT_OBSERVER="observer"
NT_PRIVATE_SENTRY="private-sentry"
NT_PUBLIC_SENTRY="public-sentry"
NT_SEED="seed"
PERSISTENT_PEERS=
PEER=
PEER_ID=
PEER_HOSTSPEC="localhost:$CLI_PORT"
DEF_NODE_TYPE="$NT_VALIDATOR"
DEF_CHAIN_ID="testnet"
DEF_NODE_USER="ubuntu"
verbosity=0
NODE_NAME=
KEY_NAME=
KEY_NAME_GEN_TRUSTEE=
PEER=
NODE_TYPE="$DEF_NODE_TYPE"
CHAIN_ID="$DEF_CHAIN_ID"
CURR_USER="${USER:-$(whoami 2>/dev/null)}"
NODE_USER="${CURR_USER:-$DEF_NODE_USER}"
KEYRING_BACKEND="${KEYRING_BACKEND:-}"
# ================ FUNCTIONS ============================
function usage {
echo "\
Usage: $0 [options] node-name
Initializes DCL node of a given type.
Options:
-c, --chain-id chain id, default: '$DEF_CHAIN_ID'
-h, --help print this help and exit
-k, --gen-key-name node admin account key name, required for genesis account
--gen-key-name-trustee genesis Trustee account key name, optional, expected for genesis account only
-t, --type node type, one of '$NT_GENESIS', '$NT_VALIDATOR', '$NT_OBSERVER', '$NT_PRIVATE_SENTRY', '$NT_PUBLIC_SENTRY', '$NT_SEED'. default: '$DEF_NODE_TYPE'
"
# -u, --user node service user, default: '$DEF_NODE_USER'
#-p, --peer peer hostspec as 'HOST:PORT', default: first entry of 'persistent_peers.txt'
# -v, --verbose be more verbose
}
function parse_args {
set -eu
#getopt --test > /dev/null
#if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
# >&2 echo 'Error: getopt is not functional.'
# exit 1
#fi
local _opts=hc:k:p:u:t:v
local _long_opts=help,chain-id:,gen-key-name:,gen-key-name-trustee:,peer:,user:type:,verbose
local _getopt_res
_getopt_res="$(getopt --name "$0" --options=$_opts --longoptions=$_long_opts -- "$@")" || exit 1
#if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# exit 2
#fi
eval set -- "$_getopt_res"
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-c|--chain-id)
CHAIN_ID="$2"
shift 2
;;
-k|--gen-key-name)
KEY_NAME="$2"
shift 2
;;
--gen-key-name-trustee)
KEY_NAME_GEN_TRUSTEE="$2"
shift 2
;;
-p|--peer)
PEER="$2"
shift 2
;;
-u|--user)
NODE_USER="$2"
shift 2
;;
-t|--type)
NODE_TYPE="$2"
shift 2
;;
-v|--verbose)
((verbosity=verbosity+1))
shift
;;
--)
shift
break
;;
?)
>&2 echo "Runtime error"
exit 3
;;
*)
>&2 echo "Parser error"
exit 3
esac
done
if [[ $# -ne 1 ]]; then
echo "Wrong number of positional arguments"
usage
exit 1
fi
NODE_NAME="$1"
if [[ "$NODE_TYPE" == "$NT_GENESIS" && -z "$KEY_NAME" ]]; then
echo "Error. key name is required for a genesis node run"
exit 1
fi
if [[ "$NODE_TYPE" == "$NT_GENESIS" ]]; then
PEER_ADDR="localhost"
else
if [[ ! -f "$PERSISTENT_PEERS_FILE" ]]; then
echo "Error. '$PERSISTENT_PEERS_FILE' file not found"
exit 1
fi
if [[ ! -f "$GENESIS_IN_FILE" ]]; then
echo "Error. '$GENESIS_IN_FILE' file not found"
exit 1
fi
PERSISTENT_PEERS="$(cat $PERSISTENT_PEERS_FILE | head -n1)"
if [[ -z "$PEER" ]]; then
PEER="$(echo "$PERSISTENT_PEERS" | awk -F',' '{print $1}')"
fi
PEER_ID="$(echo "$PEER" | awk -F'@' '{print $1}')"
PEER_HOSTSPEC="$(echo "$PEER" | awk -F'@' '{print $2}')"
PEER_ADDR="$(echo "$PEER_HOSTSPEC" | awk -F':' '{print $1}')"
fi
}
function _jq() {
set -eu
set -o pipefail
local _input="$1"
if [[ "$JSON_PARSER" == "python3" ]]; then
echo "$_input" | python3 -c 'import json; import sys; json.dump(json.load(sys.stdin), sys.stdout, indent=4)'
else
echo "$_input" | jq
fi
}
function validate_env() {
JSON_PARSER="$(jq --version >/dev/null 2>&1 && echo 'jq' || true)"
if [[ -z "$JSON_PARSER" ]]; then
JSON_PARSER="$(python3 --version >/dev/null 2>&1 && echo "python3" || true)"
fi
if [[ -z "$JSON_PARSER" ]]; then
echo "neither jq nor python3 are available" && exit 1
fi
}
function config_cli {
set -eu
set -o pipefail
local _chain_id="$1"
local _peer_host="${2:-}"
./dcld config chain-id "$_chain_id"
./dcld config output json
./dcld config broadcast-mode sync
if [[ -n "$KEYRING_BACKEND" ]]; then
./dcld config keyring-backend "$KEYRING_BACKEND"
fi
if [[ -n "$_peer_host" ]]; then
./dcld config node tcp://"$_peer_host"
fi
}
function add_key {
set -eu
set -o pipefail
local _key_name="$1"
local _output="${2:-${_key_name}.dclkey.json}"
./dcld keys add "$_key_name" 2>&1 | tee "$_output"
echo -e "\tSaved key data in '$_output'"
}
function node_init {
set -eu
set -o pipefail
local _name="$1"
local _chain_id="$2"
rm -rf "$DCL_HOME/config/gentx"
rm -f "$GENESIS_FILE"
./dcld init "$_name" --chain-id "$_chain_id"
}
function build_genesis {
set -eu
set -o pipefail
local _key_name_admin="$1"
local _acc_address="$2"
local _acc_pubkey="$3"
local _acc_address_tr="${4:-}"
local _acc_pubkey_tr="${5:-}"
./dcld add-genesis-account --address "$_acc_address" --pubkey "$_acc_pubkey" --roles "NodeAdmin"
if [[ -n "$_acc_address_tr" && -n "$_acc_pubkey_tr" ]]; then
./dcld add-genesis-account --address "$_acc_address_tr" --pubkey "$_acc_pubkey_tr" --roles "Trustee"
fi
./dcld gentx --chain-id "$CHAIN_ID" "$_key_name_admin"
./dcld collect-gentxs
./dcld validate-genesis
}
function config_node {
set -eu
set -o pipefail
local _node_name="$1"
local _node_user="$2"
local _cosmovisor_service_p="${3:-./cosmovisor.service}"
local _peers="${4:-}"
local _debug="${DEBUG:-}"
sed -i -r "s~^moniker = .*~moniker = \"$_node_name\"~" "$CONFIG_FILE"
sed -i -r 's~^laddr = "tcp://127.0.0.1:~laddr = "tcp://0.0.0.0:~' "$CONFIG_FILE"
if [[ -n "$_peers" ]]; then
sed -i -r "s~^persistent_peers = .*~persistent_peers = \"$_peers\"~" "$CONFIG_FILE"
fi
if [[ -z "$_debug" ]]; then
# config the service
test -d "/etc/systemd/system" && {
if [[ ! -f "$_cosmovisor_service_p" ]]; then
echo "Error. '$_cosmovisor_service_p' file not found"
exit 1
fi
# TODO move ot a separate script
# Open '26656' (p2p) and '26657' (RPC) ports.
# sudo ufw allow 26656/tcp
# sudo ufw allow 26657/tcp
# set systemd cosmovisor.service
sed -i -r "s~^User=ubuntu$~User=${_node_user}~" "$_cosmovisor_service_p"
sed -i -r "s~\"DAEMON_HOME=/var/lib/ubuntu/.dcl\"~\"DAEMON_HOME=${DCL_HOME}\"~" "$_cosmovisor_service_p"
sudo cp -f "$_cosmovisor_service_p" /etc/systemd/system/cosmovisor.service
} || { echo "WARNING: Not a systemd system. Skipping systemd service configuration";}
fi
}
function run_node {
set -eu
set -o pipefail
local _debug="${DEBUG:-}"
if [[ -z "$_debug" ]]; then
test -d "/etc/systemd/system" && {
# Enable the service
sudo systemctl enable cosmovisor
# Start node
sudo systemctl start cosmovisor
} || { echo "WARNING: Not a systemd system. Skipping service start..."; return 0; }
echo "Node has been stared as a service."
echo -e "\tUse 'systemctl status cosmovisor' to get the node service status."
echo "Use 'journalctl -u cosmovisor.service -f' to see node logs."
else
cosmovisor run start >./cosmovisor.log 2>&1 &
echo "Node has been stared in the backgroud."
fi
}
function wait_node_up {
local _timeout="${1:-60}"
local _try=1
echo -e "Waiting the node becomes up"
until ./dcld status >/dev/null 2>&1
do
if [[ "$_try" -gt "$_timeout" ]]; then
echo -e "\nERROR: dcld node seems not ready after $_timeout seconds."
return 1
fi
echo -n "."
_try=$(( $_try + 1 ))
sleep 1
done
echo -e "\n\tNode is responding"
}
function verify_node {
set -eu
set -o pipefail
local _node_host="${1:-0.0.0.0}"
local _node_port="${2:-26657}"
echo "Checking a node's 'sync_info.latest_block_height' value is not increasing..."
echo "sync_info.latest_block_height Try 1"
local status="$(./dcld status --node "tcp://${_node_host}:${_node_port}")"
local check1="$(_jq "$status" | grep latest_block_height)"
echo "sync_info.latest_block_height Try 2"
# TODO it should depend on empty block creation setting value
sleep 6
status="$(./dcld status --node "tcp://${_node_host}:${_node_port}")"
local check2="$(_jq "$status" | grep latest_block_height)"
if [[ "$check1" == "$check2" ]]; then
echo "Error: sync_info.latest_block_height value is not increasing..."
exit 1
fi
# TODO improve: makes sense only for initial genesis node
echo "Checking the node is participating in consensus..."
./dcld query tendermint-validator-set | grep -q address
# TODO check: catching_up is false
echo "Passed"
}
function add_account {
set -eu
set -o pipefail
local _acc_address="$1"
local _acc_pubkey="$2"
local _acc_roles="$3"
local _from="$4"
./dcld tx auth propose-add-account --address "$_acc_address" --pubkey "$_acc_pubkey" \
--roles "$_acc_roles" --from "$_from"
echo "Account added"
echo "To approve run: dcld tx auth approve-add-account --address=<string> --from=<account>"
}
function approve_account {
set -eu
set -o pipefail
local _acc_address="$1"
local _from="$2"
./dcld tx auth approve-add-account --address "$_acc_address" --from "$_from"
echo "Account approved by $_from"
}
# =======================================================
validate_env
parse_args "$@"
if [[ "$verbosity" -ge 2 ]]; then
set -x
fi
if [[ "$verbosity" -ge 1 ]]; then
echo ""
#parsed_args=""
#parsed_args+="\t CHAIN_ID=$output_dir\n\toutput_type=$output_type"
#parsed_args+="\n\tin_docker=$in_docker\n\tinput_dir=$input_dir"
#parsed_args+="\n\tfpm_tool=$fpm_tool\n\tverbosity=$verbosity"
#parsed_args+="\n\trelease=$pkg_version"
#echo -e "Parsed arguments:\n$parsed_args"
fi
sudo chown "$NODE_USER" ./dcld
sudo chmod u+x ./dcld
echo "Configuring CLI"
# Note. we consider that cli would connect to local node (default setting)
config_cli "$CHAIN_ID"
ACC_ADDR="<acc-addr>"
ACC_PUBKEY="<acc-pubkey>"
if [[ -n "$KEY_NAME" ]]; then
KEY_INFO="$(./dcld keys show --output text "${KEY_NAME}")"
ACC_ADDR="$(grep address <<<"$KEY_INFO" | awk '{print $NF}')"
ACC_PUBKEY="$(grep pubkey <<<"$KEY_INFO" | awk '{print $NF}' | sed -e "s/^'//" -e "s/'$//")"
fi
if [[ -n "$KEY_NAME_GEN_TRUSTEE" ]]; then
KEY_INFO="$(./dcld keys show --output text "${KEY_NAME_GEN_TRUSTEE}")"
ACC_ADDR_TR="$(grep address <<<"$KEY_INFO" | awk '{print $NF}')"
ACC_PUBKEY_TR="$(grep pubkey <<<"$KEY_INFO" | awk '{print $NF}' | sed -e "s/^'//" -e "s/'$//")"
fi
#echo "Initializing the node"
#node_init "$NODE_NAME" "$CHAIN_ID"
if [[ "$NODE_TYPE" == "$NT_GENESIS" ]]; then
echo "Building genesis file"
build_genesis "$KEY_NAME" "$ACC_ADDR" "$ACC_PUBKEY" "$ACC_ADDR_TR" "$ACC_PUBKEY_TR"
else
echo "Locating the genesis file at $GENESIS_FILE"
cp -f "$GENESIS_IN_FILE" "$GENESIS_FILE"
fi
echo "Configuring node"
config_node "$NODE_NAME" "$NODE_USER" "$SERVICE_FILE" "$PERSISTENT_PEERS"
echo -e "\tDone."
echo -e "\nOptionally, edit '$CONFIG_FILE' in order to set different setting (like listen address)"
echo "Locating the genesis app version to $GENESIS_APP_DIR directory"
mkdir -p "$GENESIS_APP_DIR"
cp -f ./dcld "$GENESIS_APP_DIR"/
sudo chown "$NODE_USER" "$GENESIS_APP_DIR"/dcld
# Execution permissions on `dcld` are granted to all (i.e. User, Group and Others classes)
# because cosmovisor requires execution permission on the application binary to be granted to Others class:
# https://github.com/cosmos/cosmos-sdk/blob/cosmovisor/v1.0.0/cosmovisor/upgrade.go#L164
sudo chmod a+x "$GENESIS_APP_DIR"/dcld
echo "Running the node"
run_node
VAL_ID="unknown"
wait_node_up
echo -e "\nexport PATH=\$PATH:$CURRENT_APP_DIR" >> "$HOME"/.profile
source "$HOME"/.profile
echo "Added '${CURRENT_APP_DIR}' to \$PATH to maintain 'dcld' command associated with the latest installed app binary."
echo "Execute 'source \$HOME/.profile' or restart shell to take the \$PATH change effective."
STATUS="$(dcld status)"
VAL_ID="$(_jq "$STATUS"| grep '"id"' | awk '{print $NF}' | sed -e 's/^"//' -e 's/[",]\+$//')"
VAL_ADDR="$(dcld tendermint show-address)"
VAL_PUBKEY="$(dcld tendermint show-validator)"
echo "Done"
echo -e "\n====================================\n"
echo "Generated data:"
echo -e "\n\tValidator node data:"
echo -e "\t\tname: '$NODE_NAME'"
echo -e "\t\taddress: '$VAL_ADDR'"
echo -e "\t\tpublic key: '$VAL_PUBKEY'"
echo -e "\t\tID: '$VAL_ID'"
if [[ "$NODE_TYPE" == "$NT_GENESIS" ]]; then
echo -e "\n\tGenesis file: '$GENESIS_FILE'"
elif [[ "$NODE_TYPE" == "$NT_VALIDATOR" ]]; then
if [[ -z "$KEY_NAME" ]]; then
KEY_NAME="<acc-keyname>"
fi
echo -e "\nNext steps:"
echo -e "\n\t1. If you have an approved NodeAdmin account please run the following command to make the node a validator:"
echo -e "\t\tdcld tx validator add-node --pubkey '$VAL_PUBKEY' --moniker '$NODE_NAME' --from '$KEY_NAME'"
echo -e "\n\t2. Explore the node external IP and provide it along with validator ID '$VAL_ID'"
echo -e "\t\t to DCLedger '$CHAIN_ID' network validator administrators"
fi
# useful commands
# - keys:
# dcld keys show <name>: to get address and pubkey for a keyname
# - node status:
# - systemctl status cosmovisor: to get the node service status.
# - journalctl -u cosmovisor.service -f: to see node logs.
# - dcld status [--node tcp://<node host>:<node port>`]: to get the current status.
# - dcld query tendermint-validator-set [height]: list of nodes participating in consensus
# - account status:
# - dcld query auth account --address=<address>: to ensure that account is created and has assigned role