-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinstall.sh
executable file
·1589 lines (1335 loc) · 56.4 KB
/
install.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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
account_addr=""
docker_swarm_started=0
voi_home="${HOME}/voi"
headless_install=0
is_root=0
skip_account_setup=0
migrate_host_based_setup=0
wallet_password=""
new_user_setup=0
new_network=0
network_status_url=""
network_identifier=""
staking_url=""
container_id=""
latest_key_end_block=""
previous_network_addresses=""
bold=$(tput bold)
normal=$(tput sgr0)
execute_sudo() {
if sudo -v &> /dev/null; then
if [[ $(id -u) -eq 0 ]]; then
bash -c "$1"
else
sudo bash -c "$1"
fi
else
abort "Your user does not have sudo privileges. Exiting the program."
fi
}
abort() {
if [[ -z "$2" || "$2" != "true" ]]; then
if [[ ${docker_swarm_started} -eq 1 ]]; then
echo "Shutting down docker swarm"
echo ""
execute_sudo 'docker swarm leave --force'
fi
fi
echo "$1"
exit 1
}
execute_docker_command_internal() {
local interactive=$1
local command=$2
local options=""
if [[ $interactive = "yes" ]]; then
options="-it"
fi
execute_sudo "docker exec -e account_addr=${account_addr} ${options} \"${container_id}\" bash -c \"${command}\""
}
execute_interactive_docker_command() {
local retries=0
while [[ $retries -lt 10 ]]; do
execute_docker_command_internal "yes" "$1"
local exit_code=$?
if [[ ${exit_code} -eq 130 ]]; then
exit ${exit_code} >&2
fi
if [[ ${exit_code} -ne 0 ]]; then
echo "Error executing command. Please try again or press CTRL-C to exit."
((retries++))
else
break
fi
done
if [[ $retries -eq 10 ]]; then
abort "Command failed after 10 attempts. Exiting the program."
fi
}
execute_docker_command() {
execute_docker_command_internal "no" "$1"
}
cleanup_deprecated_files_and_folders() {
if [[ -d ${voi_home}/docker-swarm ]]; then
rm -rf "${voi_home}/docker-swarm" 2>/dev/null || echo "Failed to delete deprecated folder: ${voi_home}/docker-swarm"
fi
if [[ -f ${voi_home}/profile ]]; then
rm -rf "${voi_home}/profile" 2>/dev/null || echo "Failed to delete deprecated file: ${voi_home}/profile"
fi
if [[ -f ${voi_home}/.profile ]]; then
if grep -q "^export VOINETWORK_IMPORT_ACCOUNT=" "${voi_home}/.profile"; then
sed -i '/^export VOINETWORK_IMPORT_ACCOUNT=/d' "${voi_home}/.profile"
unset VOINETWORK_IMPORT_ACCOUNT
fi
fi
}
start_docker_swarm() {
local docker_swarm
docker_swarm=$(execute_sudo 'docker info | grep Swarm | cut -d\ -f3')
if [[ ${docker_swarm} != "active" ]]; then
command="docker swarm init"
if [[ -n ${VOINETWORK_DOCKER_SWARM_INIT_SETTINGS} ]]; then
command+=" ${VOINETWORK_DOCKER_SWARM_INIT_SETTINGS}"
else
command+=" --listen-addr lo --advertise-addr lo"
fi
execute_sudo "$command"
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
docker_swarm_instructions
fi
docker_swarm_started=1
fi
}
start_stack() {
case ${VOINETWORK_PROFILE} in
"relay")
docker_file="${voi_home}/docker/relay.yml"
;;
"archiver")
docker_file="${voi_home}/docker/archiver.yml"
;;
"developer")
docker_file="${voi_home}/docker/developer.yml"
;;
"participation")
docker_file="${voi_home}/docker/compose.yml"
;;
*)
abort "Invalid profile. Exiting the program."
;;
esac
command="source ${voi_home}/.profile && docker stack deploy -c ${docker_file}"
if [[ -f "${voi_home}/docker/notification.yml" ]]; then
command+=" -c ${voi_home}/docker/notification.yml"
fi
command+=" voinetwork"
execute_sudo "$command"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
abort "Error starting stack. Exiting the program."
fi
}
# shellcheck disable=SC2120
wait_for_stack_to_be_ready() {
while true; do
service_info=$(execute_sudo 'docker stack ps voinetwork --format json')
stack_ready=true
while read -r line; do
current_state=$(echo "${line}" | jq -r '.CurrentState')
desired_state=$(echo "${line}" | jq -r '.DesiredState')
service_error=$(echo "${line}" | jq -r '.Error')
if [[ (${desired_state} == "Ready" || ${desired_state} == "Running") && ${current_state} != Running* ]]; then
stack_ready=false
break
fi
done < <(echo "${service_info}")
if [[ ${stack_ready} == false && -n ${service_error} ]]; then
local number_of_interfaces
number_of_interfaces=$(execute_sudo "ip -d link show | grep vx | wc -l")
if [[ number_of_interfaces -le 2 ]]; then
echo "Docker has a network interface that is lingering and preventing startup. We'll attempt to auto-delete it."
execute_sudo "ip -d link show | grep vx | grep DOWN | awk '{print $2}' | tr -d ':' | xargs -rn1 ip link delete"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
echo "Docker swarm is unable to start services. https://github.com/moby/libnetwork/issues/1765"
abort "Exiting the program."
fi
elif [[ number_of_interfaces -ge 3 ]]; then
echo "Docker swarm is unable to start services. https://github.com/moby/libnetwork/issues/1765"
abort "Multiple vx interfaces found. Please delete all vx interfaces or reboot the server"
fi
fi
echo "Waiting for stack to be ready..."
sleep 2
if [[ ${stack_ready} == true ]]; then
break
fi
done
display_banner "Stack is ready!"
}
function get_container_id() {
case ${VOINETWORK_PROFILE} in
"relay")
container_id=$(docker ps -q -f name=voinetwork_relay)
;;
"developer")
container_id=$(docker ps -q -f name=voinetwork_developer)
;;
"archiver")
container_id=$(docker ps -q -f name=voinetwork_archiver)
;;
"participation")
container_id=$(docker ps -q -f name=voinetwork_algod)
;;
*)
abort "Invalid profile. Exiting the program."
;;
esac
}
verify_node_is_running() {
local retries=0
local max_retries=5
while [[ $retries -lt $max_retries ]]; do
if [[ -n "$container_id" ]]; then
execute_sudo "docker exec ${container_id} bash -c \"curl -sS http://localhost:8080/health > /dev/null\""
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
break
fi
fi
echo "Error connecting to node. Retrying in 10 seconds..."
sleep 10
((retries++))
done
if [[ $retries -eq $max_retries ]]; then
abort "Error connecting to node after $max_retries attempts. Exiting the program."
fi
}
get_current_net_round() {
local retries=0
local max_retries=5
while [[ $retries -lt $max_retries ]]; do
current_net_round=$(curl -s ${network_status_url} | jq -r '.["last-round"]' )
exit_code=$?
if [[ $exit_code -eq 0 ]] && [[ -n $current_net_round ]]; then
break
fi
echo "Error fetching network status. Retrying in 5 seconds..."
sleep 5
((retries++))
done
if [[ $retries -eq $max_retries ]]; then
abort "Error fetching network status after $max_retries attempts. Please check your internet connection and try again."
fi
}
get_node_status() {
current_node_round=$(execute_docker_command '/node/bin/goal node lastround | head -n 1')
if [[ ${network_status_url} != "" ]]; then
get_current_net_round
current_node_round=${current_node_round//[!0-9]/}
fi
}
set_network_status_url() {
case ${VOINETWORK_NETWORK} in
"mainnet")
network_status_url="https://mainnet-api.voi.nodely.dev/v2/status"
;;
"betanet")
network_status_url="https://betanet-api.voi.nodely.dev/v2/status"
;;
"testnet")
network_status_url="https://testnet-api.voi.nodely.dev/v2/status"
;;
"testnet-v1.1")
network_status_url="https://testnet-api.voi.nodely.dev/v2/status"
;;
*)
abort "Unable to find status URL. Swarm is running in the background." true
;;
esac
}
set_staking_url() {
case ${VOINETWORK_NETWORK} in
"mainnet")
staking_url="https://mainnet-idx.nautilus.sh/v1/scs/accounts"
;;
"betanet")
staking_url="https://betanet-idx.nautilus.sh/v1/scs/accounts"
;;
"testnet")
staking_url="https://arc72-idx.nautilus.sh/v1/scs/accounts"
;;
"testnet-v1.1")
staking_url="https://arc72-idx.nautilus.sh/v1/scs/accounts"
;;
*)
abort "Unable to find staking URL. Exiting the program."
;;
esac
}
set_network_identifier() {
case ${VOINETWORK_NETWORK} in
"mainnet")
network_identifier="voimain-v1.0"
;;
"betanet")
network_identifier="voibeta-v1.0"
;;
"testnet")
network_identifier="voitest-v1.1"
;;
"testnet-v1.1")
network_identifier="voitest-v1.1"
;;
*)
network_identifier="voimain-v1.0"
;;
esac
}
set_docker_image() {
if [[ -z ${VOINETWORK_DOCKER_IMAGE} ]]; then
case ${VOINETWORK_PROFILE} in
"relay")
VOINETWORK_DOCKER_IMAGE="ghcr.io/voinetwork/voi-node-${VOINETWORK_NETWORK}:latest"
;;
"developer")
VOINETWORK_DOCKER_IMAGE="ghcr.io/voinetwork/voi-node-${VOINETWORK_NETWORK}:latest"
;;
"archiver")
VOINETWORK_DOCKER_IMAGE="ghcr.io/voinetwork/voi-node-${VOINETWORK_NETWORK}:latest"
;;
"participation")
VOINETWORK_DOCKER_IMAGE="ghcr.io/voinetwork/voi-node-participation-${VOINETWORK_NETWORK}:latest"
;;
*)
abort "Invalid profile. Exiting the program."
;;
esac
fi
update_profile_setting "VOINETWORK_DOCKER_IMAGE" "${VOINETWORK_DOCKER_IMAGE}"
}
get_network_identifier() {
case $1 in
"mainnet")
echo "voimain-v1.0"
;;
"betanet")
echo "voibeta-v1.0"
;;
"testnet")
echo "voitest-v1.1"
;;
"testnet-v1.1")
echo "voitest-v1.1"
;;
*)
echo "voitest-v1.1"
;;
esac
}
catchup_node() {
display_banner "Catching up with the network... This might take some time, and numbers might briefly increase"
set_network_status_url
get_node_status
while [[ ${current_node_round} -lt ${current_net_round} ]]; do
rounds_to_go=$((current_net_round - current_node_round))
if [[ ${rounds_to_go} -gt 1 ]]; then
printf "\rWaiting for catchup: %d blocks to go " "${rounds_to_go}"
else
printf "\rWaiting for catchup: One more block to go! "
fi
get_node_status
sleep 5
done
echo ""
display_banner "Caught up with the network!"
}
ask_for_password() {
local password
local password_repeat
while true; do
# shellcheck disable=SC2162
read -sp "Password: " password
echo
# shellcheck disable=SC2162
read -sp "Repeat password: " password_repeat
echo
if [[ ${password} == "${password_repeat}" ]]; then
wallet_password=${password}
break
else
# shellcheck disable=SC2028
echo -e "\n"
echo "Passwords do not match. Please try again."
fi
done
}
create_wallet() {
if [[ $(execute_docker_command "/node/bin/goal wallet list | wc -l") -eq 1 ]]; then
local kmd_token
echo "Let's create a new wallet for you. Please provide a password for security."
kmd_token=$(get_kmd_token)
ask_for_password
read -r -d '' json_data <<EOF
{\"wallet_name\": \"voi\",\"wallet_driver_name\": \"sqlite\", \"wallet_password\": \"${wallet_password}\"}
EOF
response_code=$(execute_docker_command "curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -H 'X-KMD-API-Token: ${kmd_token}' -d '${json_data}' http://localhost:7833/v1/wallet")
if [[ response_code -ne 200 ]]; then
abort "Error creating wallet. Exiting the program."
fi
else
echo "Wallet already exists. Skipping wallet creation."
fi
}
get_account_balance() {
local balance
balance=$(execute_docker_command "/node/bin/goal account balance -a $1")
balance=${balance//[!0-9]/}
echo "$balance"
}
busy_wait_until_balance_is_sufficient() {
local balance
display_banner "Waiting for balance (account: ${account_addr}) to be 0.001 Voi"
balance=$(get_account_balance "${account_addr}")
while [[ ${balance} -lt "1000" ]]; do
echo "Waiting for balance to be 0.001 Voi at minimum"
balance=$(get_account_balance "${account_addr}")
sleep 10
done
display_banner "Account has balance of 0.001 Voi or greater!"
}
get_account_info() {
allow_one_account=$1
if execute_sudo "test ! -f \"/var/lib/voi/algod/data/${network_identifier}/accountList.json\""; then
return 0
fi
accounts_json=$(execute_sudo "cat /var/lib/voi/algod/data/${network_identifier}/accountList.json")
number_of_accounts=$(echo "${accounts_json}" | jq '.Accounts | length')
if [[ $number_of_accounts -gt 1 ]]; then
skip_account_setup=1
return 1
elif [[ $number_of_accounts -eq 1 ]] && [[ $allow_one_account != "true" ]]; then
get_account_address
skip_account_setup=1
return 1
fi
account_address=$(echo "$accounts_json" | jq '.Accounts | keys[0]')
echo "${account_address}"
}
check_if_account_exists() {
allow_one_account_override="$1"
get_account_info "$allow_one_account_override"
}
get_account_address() {
local account_address
account_address=$(get_account_info true)
account_address=${account_address//\"/}
if [[ ! "${account_address}" =~ ^[A-Za-z0-9]{58}$ ]]; then
abort "Invalid account address: \"${account_address}\""
fi
account_addr=$account_address
}
get_participation_expiration_eta() {
local active_key_last_valid_round=$1
local last_committed_block=$2
local current_key_blocks_remaining=$((active_key_last_valid_round - last_committed_block))
local remaining_seconds
local current_timestamp
local expiration_timestamp
local expiration_date
remaining_seconds=$(echo "${current_key_blocks_remaining}*2.9" | bc)
current_timestamp=$(date +%s)
expiration_timestamp=$(echo "${current_timestamp}+${remaining_seconds}" | bc)
# Convert the new timestamp to a date and time
expiration_date=$(date -d "@${expiration_timestamp}" '+%Y-%m-%d %H:%M')
echo "${expiration_date}"
}
get_current_account_keys_info() {
execute_docker_command "/node/bin/goal account listpartkeys -a $1"
}
get_last_committed_block() {
execute_docker_command "/node/bin/goal node status" | grep 'Last committed block' | cut -d\ -f4 | tr -d '\r'
}
get_account_addresses() {
local network_identifier_override
local local_network_identifier
network_identifier_override=$1
local_network_identifier=${network_identifier}
if [[ -n ${network_identifier_override} ]]; then
local_network_identifier=${network_identifier_override}
fi
if execute_sudo "test ! -f \"/var/lib/voi/algod/data/${local_network_identifier}/accountList.json\""; then
abort "Account list not found. Exiting the program."
fi
accounts_json=$(execute_sudo "cat /var/lib/voi/algod/data/${local_network_identifier}/accountList.json")
number_of_accounts=$(echo "${accounts_json}" | jq '.Accounts | length')
if [[ $number_of_accounts -eq 0 ]]; then
return 1
fi
account_addresses=$(echo "$accounts_json" | jq -r '.Accounts | keys[]')
echo "${account_addresses}"
}
generate_new_key() {
local address
local start_block
local end_block
local expiration_date
start_block=$(get_last_committed_block)
end_block=$((start_block + 2000000))
expiration_date=$(get_participation_expiration_eta "${end_block}" "${start_block}")
address=$1
latest_key_end_block=${end_block}
echo "Generating participation key for ${address} with start block ${start_block} and end block ${end_block}"
echo "New key is expected to be valid until: ${expiration_date}"
execute_interactive_docker_command "/node/bin/goal account addpartkey -a ${address} --roundFirstValid ${start_block} --roundLastValid ${end_block}"
}
ensure_accounts_are_offline() {
local local_network_identifier
local_network_identifier=$(get_network_identifier "$1")
account_addresses=$(get_account_addresses "${local_network_identifier}")
previous_network_addresses="${account_addresses}"
if [[ -z ${account_addresses} ]]; then
return
fi
# if [[ ${container_id} == "" ]]; then
# echo "Skipping account offline check as no existing Voi Swarm container could be found."
# return
# fi
# display_banner "Migrating to new network. Ensuring all accounts are offline."
#
# echo "Checking if accounts are online and have a balance of 1,000 microVoi or more."
# echo "Accounts with a balance of 1,000 microVoi or more will be taken offline."
#
# for account in ${account_addresses}; do
# local balance
# balance=$(get_account_balance "${account}")
# account_status=$(execute_docker_command "/node/bin/goal account dump -a ${account}" | jq -r .onl)
#
# if [[ ${balance} -ge 1000 && ${account_status} -eq 1 ]]; then
# echo ""
# echo "Balance is ${balance} which is above 1,000 microVoi. Taking account ${account} offline."
# execute_interactive_docker_command "/node/bin/goal account changeonlinestatus -a ${account} -o=false"
# echo "Account ${account} is now offline!"
# fi
# done
}
get_participation_key_id_from_vote_key() {
local vote_key
local output
vote_key=$1
output=$(execute_docker_command "/node/bin/goal account partkeyinfo")
echo "${output}" | grep -B 10 "${vote_key}" | grep 'Participation ID:' | head -n 1 | cut -d ':' -f2- | xargs
}
get_most_recent_participation_key_details() {
local address=$1
local output
output=$(execute_docker_command "/node/bin/goal account partkeyinfo")
local first_round
first_round=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'First round:' | head -n 1 | cut -d ':' -f2- | xargs)
local last_round
last_round=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'Last round:' | head -n 1 | cut -d ':' -f2- | xargs)
local key_dilution
key_dilution=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'Key dilution:' | head -n 1 | cut -d ':' -f2- | xargs)
local selection_key
selection_key=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'Selection key:' | head -n 1 | cut -d ':' -f2- | xargs)
local voting_key
voting_key=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'Voting key:' | head -n 1 | cut -d ':' -f2- | xargs)
local stateproof_key
# shellcheck disable=SC2034
stateproof_key=$(echo "$output" | grep -A 4 -B 5 "${latest_key_end_block}" | grep 'State proof key:' | head -n 1 | cut -d ':' -f2- | xargs)
key_info_json=$(jq --null-input --arg address "$address" --arg first_round "$first_round" --arg last_round "$last_round" --arg key_dilution "$key_dilution" --arg selection_key "$selection_key" --arg voting_key "$voting_key" --arg stateproof_key "$stateproof_key" '{
"address": $address,
"first_round": $first_round,
"last_valid": $last_round,
"key_dilution": $key_dilution,
"selection_key": $selection_key,
"voting_key": $voting_key,
"stateproof_key": $stateproof_key
}')
echo "${key_info_json}"
}
generate_participation_key() {
display_banner "Generating/Updating participation key"
if [[ ${new_network} -eq 1 ]]; then
display_banner "New network detected: ${VOINETWORK_NETWORK}. Regenerating participation keys for all accounts."
output=$(execute_docker_command "/node/bin/goal account partkeyinfo")
participation_ids=$(echo "$output" | grep "Participation ID:" | awk '{print $3}')
for id in $participation_ids; do
execute_interactive_docker_command "/node/bin/goal account deletepartkey --partkeyid ${id}"
done
fi
account_addresses=$(get_account_addresses)
account_addresses_length=$(echo "${account_addresses}" | wc -w)
for account in ${account_addresses}; do
local active_key_last_valid_round
local last_committed_block
active_key_last_valid_round=$(docker exec -it "${container_id}" bash -c '/node/bin/goal account listpartkeys' | awk -v addr="${account}" '$1=="yes" && substr($2, 1, 4) == substr(addr, 1, 4) && substr($2, length($2)-3) == substr(addr, length(addr)-3) {print $6}' | tr -cd '[:digit:]')
last_committed_block=$(get_last_committed_block)
if [[ -z ${active_key_last_valid_round} ]]; then
if [[ ${new_user_setup} -eq 0 && account_addresses_length -le 1 ]]; then
return 1
fi
if [[ ${account_addresses_length} -eq 1 ]]; then
generate_new_key "${account}"
else
local balance
balance=$(get_account_balance "${account}")
if [[ ${balance} -ge 1000 ]]; then
echo "Balance is equal/above 1,000 microVoi. Generating participation key for account ${account}"
generate_new_key "${account}"
change_account_online_status "${account}"
account_status=$(execute_docker_command "/node/bin/goal account dump -a ${account}" | jq -r .onl)
if [[ ${account_status} -eq 1 ]]; then
echo "Account ${account} is now online!"
else
echo "Account ${account} is currently offline."
fi
else
echo "Balance is below 1,000 microVoi. Skipping participation key generation for account ${account}"
fi
echo ""
fi
elif [[ $((active_key_last_valid_round-last_committed_block)) -le 417104 ]]; then
local existing_expiration_date
local new_expiration_date
local current_key_prefix
local current_key_id
local end_block
end_block=$((last_committed_block + 2000000))
existing_expiration_date=$(get_participation_expiration_eta "${active_key_last_valid_round}" "${last_committed_block}")
new_expiration_date=$(get_participation_expiration_eta "${end_block}" "${last_committed_block}")
current_key_prefix=$(docker exec -it "${container_id}" bash -c '/node/bin/goal account listpartkeys' | awk -v addr="${account_address}" '$1=="yes" && substr($2, 1, 4) == substr(addr, 1, 4) && substr($2, length($2)-3) == substr(addr, length(addr)-3) {print $3}' | tr -cd '[:digit:]')
current_key_id=$(execute_docker_command "/node/bin/goal account partkeyinfo" | grep "${current_key_prefix}" | awk '{print $3}')
echo "Current participation key for account ${account} is expected to expire at: ${existing_expiration_date}"
echo "Currently the network is at block: ${last_committed_block}"
echo "Current participation key expires at block: ${active_key_last_valid_round}"
echo ""
echo "This is below the required threshold of 417,104 blocks / ~14 days."
echo "Generating participation key for account ${account} with end block ${end_block}."
echo "New key is expected to be valid until: ${new_expiration_date}"
echo ""
echo "You will be asked to enter your password to activate the new key."
execute_interactive_docker_command "/node/bin/goal account renewpartkey -a ${account} --roundLastValid ${end_block}"
if [[ -n ${current_key_id} ]]; then
execute_interactive_docker_command "/node/bin/goal account deletepartkey --partkeyid ${current_key_id}"
fi
else
local existing_expiration_date
existing_expiration_date=$(get_participation_expiration_eta "${active_key_last_valid_round}" "${last_committed_block}")
echo "Current participation key for account ${account} is expected to expire at: ${existing_expiration_date}"
echo "This is above the required threshold of 417,104 blocks / ~14 days."
echo "No new participation key will be generated."
echo ""
fi
done
}
start_kmd() {
execute_docker_command "/node/bin/goal kmd start -t 600"
}
get_kmd_token() {
local kmd_token
kmd_token=$(execute_docker_command "cat /algod/data/kmd-v0.5/kmd.token")
echo "${kmd_token}"
}
get_algod_token() {
local algod_token
algod_token=$(execute_sudo 'cat /var/lib/voi/algod/data/algod.token')
echo "${algod_token}"
}
display_banner() {
echo
echo "********************************************************************************"
echo "* ${bold}$1${normal}"
echo "********************************************************************************"
echo
}
docker_swarm_instructions() {
echo ""
echo "Error initializing Docker Swarm."
echo ""
echo "To fix this, set VOINETWORK_DOCKER_SWARM_INIT_SETTINGS to the settings you want to use to initialize Docker Swarm and try again."
echo "Parameters that can be passed to the swarm can be found at: https://docs.docker.com/engine/reference/commandline/swarm_init/"
echo ""
if [[ ${VOINETWORK_PROFILE} == "relay" ]]; then
echo "If you encounter issues, please troubleshoot on your own first."
echo "If you need further assistance, join #relay-runners on Discord (https://discord.com/invite/vnFbrJrHeW) to engage with the community and get help after describing all troubleshooting steps performed."
echo "Having a strong grasp of technical details, including the ability to execute commands directly on the server, and understanding cloud resources and logs, is required for managing this setup."
echo "If you prefer other ways to engage with the community and contribute, consider exploring different options."
else
echo "Join #node-resources on Discord (https://discord.com/invite/vnFbrJrHeW) to engage with the community and get help by using the Discord ((https://discord.com/invite/vnFbrJrHeW))."
fi
abort "Exiting the program."
}
joined_network_instructions() {
echo "You can find utility commands for managing your setup in ${voi_home}/bin"
if [[ ${is_root} -eq 0 ]]; then
echo ""
echo "Ensure you restart your shell to use them, or type 'newgrp docker' in your existing shell before using."
fi
echo ""
echo "For useful information, check out:"
echo " - Voi Swarm documentation: https://voinetwork.github.io/voi-swarm/"
echo " - Get notified when to renew participation keys: https://voinetwork.github.io/voi-swarm/operating/setup-notifications/"
echo " - Voi Swarm CLI Tools: https://voinetwork.github.io/voi-swarm/cli-tools/"
echo ""
if [[ ${skip_account_setup} -eq 1 ]]; then
if [[ -z ${account_addr} ]]; then
echo "Account setup skipped. Multiple accounts detected in your wallet."
else
echo "Account setup skipped. Detected existing account with address: ${account_addr}"
echo "To see network participation status use ${HOME}/voi/bin/get-participation-status ${account_addr}"
echo "To go online use ${HOME}/voi/bin/go-online ${account_addr}"
fi
fi
# Display information informing the user that the network will catch up in the background if used in non-interactive mode
if [[ $1 == "true" ]]; then
echo ""
echo "The network is now catching up and will continue to do so in the background."
fi
if [[ ${VOINETWORK_PROFILE} != "participation" ]]; then
echo ""
display_banner "Node setup"
echo "Due to the nature of ${VOINETWORK_PROFILE} nodes, you will not be able to participate in the consensus network on this server."
echo ""
echo "By running this software you acknowledge the following:"
echo " - It is your responsibility to monitor the software and how it is performing."
echo " - You are responsible for operating the software and server, this includes, but is not limited to security, maintenance, updates, access controls, and monitoring."
fi
echo ""
echo "To easily access commands from ${voi_home}/bin, add the following to ${HOME}/.bashrc or ${HOME}/.profile:"
echo " export PATH=\"\$PATH:${voi_home}/bin\""
echo ""
echo "To add to your ~/.bashrc, run:"
echo " echo 'export PATH=\"\$PATH:${voi_home}/bin\"' >> ~/.bashrc && source ~/.bashrc"
echo ""
# if [[ ${skip_account_setup} -eq 0 && ${VOINETWORK_PROFILE} == "participation" ]]; then
# echo "${bold}*********************************** READ THIS! ***********************************${normal}"
# echo "After joining the network, it might take up to 2 hours for your server to appear on telemetry"
# echo "tracking services. Initially, you can identify your server using the 12-digit short GUID shown by"
# echo "the command ${voi_home}/bin/get-node-status."
# echo ""
# echo "At first, your node's health scores ${bold}will be low${normal}. ${bold}This is normal.${normal}"
# echo "After running your node for 5-7 days, you should see the health score increase."
# echo ""
# fi
}
check_staking_accounts() {
account_addresses=$(get_account_addresses)
set_staking_url
for account in ${account_addresses}; do
local staking_endpoint
local response
local http_code
local json_response
staking_endpoint="${staking_url}?owner=${account}&deleted=0"
response=$(curl -s --max-time 5 -w "%{http_code}" "${staking_endpoint}")
http_code=$(echo "${response}" | awk '{print substr($0, length($0) - 2)}')
json_response=$(echo "${response}" | awk '{print substr($0, 1, length($0) - 3)}')
if [[ "${http_code}" -eq 200 ]]; then
accounts_length=$(jq '.accounts | length' <<< "${json_response}")
if [[ "${accounts_length}" -gt 0 ]]; then
display_banner "Staking contract detected"
echo "Staking contract has been detected for owner ${account}"
echo ""
local balance
balance=$(get_account_balance "${account}")
if [[ ${balance} -lt "1000" ]]; then
echo "Balance is below 0.001 Voi. Skipping staking account setup."
continue
fi
local staking_accounts
staking_accounts=$(jq -c '.accounts[]' <<< "${json_response}")
for staking_account in ${staking_accounts}; do
local last_committed_block
local contract_address
local contract_id
local part_vote_k
local part_vote_lst
local participation_key_id
contract_id=$(jq -r '.contractId' <<< "${staking_account}")
contract_address=$(jq -r '.contractAddress' <<< "${staking_account}")
last_committed_block=$(get_last_committed_block)
part_vote_k=$(jq -r '.part_vote_k' <<< "${staking_account}")
part_vote_lst=$(jq -r '.part_vote_lst' <<< "${staking_account}")
# shellcheck disable=SC2046
participation_key_id=$(get_participation_key_id_from_vote_key "${part_vote_k}")
if [[ "${part_vote_k}" == "null" || $((part_vote_lst-last_committed_block)) -le 417104 || -z "${participation_key_id}" ]]; then
if [[ "${part_vote_k}" == "null" ]]; then
echo "No staking participation key detected."
elif [[ -z ${participation_key_id} ]]; then
echo "Registered staking participation key is not installed locally."
else
local existing_expiration_date
existing_expiration_date=$(get_participation_expiration_eta "${part_vote_lst}" "${last_committed_block}")
echo "Current participation key for account ${account} is expected to expire at: ${existing_expiration_date}"
echo "Currently the network is at block: ${last_committed_block}"
echo "Current participation key expires at block: ${part_vote_lst}"
echo ""
echo "This is below the required threshold of 417,104 blocks / ~14 days."
fi
echo ""
echo "Generating new key for owner ${account} and contract address ${contract_address}"
generate_new_key "${contract_address}"
local partkey_info
local voting_key
local selection_key
local first_round
local last_round
local key_dilution
local stateproof_key
partkey_info=$(get_most_recent_participation_key_details "${contract_address}")
voting_key=$(jq -r '.voting_key' <<< "${partkey_info}")
selection_key=$(jq -r '.selection_key' <<< "${partkey_info}")
first_round=$(jq -r '.first_round' <<< "${partkey_info}")
last_round=$(jq -r '.last_valid' <<< "${partkey_info}")
key_dilution=$(jq -r '.key_dilution' <<< "${partkey_info}")
stateproof_key=$(jq -r '.stateproof_key' <<< "${partkey_info}")
echo "Sending transactions to staking contract ${contract_address} for owner ${account}"
echo "This will update the staking contract with the new participation key info, allowing participation in the network"
echo "You will be asked to enter your password multiple times to confirm the transactions."
execute_interactive_docker_command "/node/bin/goal clerk send -a 1000 -f ${account} -t ${contract_address} --out=/tmp/payment.txn"
execute_interactive_docker_command "/node/bin/goal app call --app-id ${contract_id} --from ${account} --app-arg 'b64:zSTeiA==' --app-arg 'b64:${voting_key}' --app-arg 'b64:${selection_key}' --app-arg 'int:${first_round}' --app-arg 'int:${last_round}' --app-arg 'int:${key_dilution}' --app-arg 'b64:${stateproof_key}' --out=/tmp/app_call.txn"
execute_docker_command "cat /tmp/{payment,app_call}.txn > /tmp/combined.txn"
execute_interactive_docker_command "/node/bin/goal clerk group -i /tmp/combined.txn -o /tmp/grouped.txn > /dev/null"
execute_interactive_docker_command "/node/bin/goal clerk split -i /tmp/grouped.txn -o /tmp/split.txn > /dev/null"
execute_interactive_docker_command "/node/bin/goal clerk sign -i /tmp/split-0.txn -o /tmp/signed-0.txn"
execute_interactive_docker_command "/node/bin/goal clerk sign -i /tmp/split-1.txn -o /tmp/signed-1.txn"
execute_docker_command "cat /tmp/signed-{0,1}.txn > /tmp/signed-combined.txn"
execute_interactive_docker_command "/node/bin/goal clerk rawsend -f /tmp/signed-combined.txn"
if [[ -n ${participation_key_id} ]]; then
execute_interactive_docker_command "/node/bin/goal account deletepartkey --partkeyid ${participation_key_id}"
fi
else
local existing_expiration_date
existing_expiration_date=$(get_participation_expiration_eta "${part_vote_lst}" "${last_committed_block}")
echo "Current staking participation key for owner ${account}, and"
echo "contract address ${contract_address} is expected to expire at: ${existing_expiration_date}"
echo "This is above the required threshold of 417,104 blocks / ~14 days."
echo "No new staking participation key will be generated."
echo ""
fi
done
echo ""
echo "${bold}To learn how to use your staking contract visit: https://staking.voi.network${normal}"
else
display_banner "No staking contract detected"
echo "No staking contracts found for owner ${account}. To learn more visit: https://staking.voi.network"
fi
fi
done
}
change_account_online_status() {
local account
account=$1
echo "Enter your password to join the network for account ${account}."
execute_interactive_docker_command "/node/bin/goal account changeonlinestatus -a ${account}"
}
join_as_new_user() {
new_user_setup=1
local account_addresses
account_addresses=$(get_account_addresses)
display_banner "Joining network"
generate_participation_key
# If there are multiple accounts, we don't want to go online automatically, unless previously done
# as part of participation key generation.
if [[ $(echo "$account_addresses" | wc -l) -gt 1 ]]; then
display_banner "Welcome to Voi!"
joined_network_instructions
return
fi
busy_wait_until_balance_is_sufficient
change_account_online_status "${account}"