-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsccmultitool.sh
More file actions
2812 lines (2252 loc) · 87.4 KB
/
sccmultitool.sh
File metadata and controls
2812 lines (2252 loc) · 87.4 KB
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
#Coin info
version="3.5.1.0"
prereleaseavailable=0
coinname=stakecubecoin
coinnamed=sccd
coinnamecli=scc-cli
ticker=SCC
coindir=scc
sleeptimerinsec=120
sleeprandomtimer=65
binaries="https://github.com/stakecube/StakeCubeCoin/releases/download/v3.5.1.0/scc-v3.5.1.0-1542625b0-linux-nodes.zip"
prereleasebinaries="https://github.com/stakecube/StakeCubeCoin/releases/download/v3.4.7.1/scc-3.4.7.1-linux-nodes.zip"
snapshot='https://stakecubecoin.net/bootstrap.zip'
port=40000
rpcport=39999
discord='https://discord.gg/xxjZzJE'
#setup variables for passwords
pass=`pwgen 14 1 b`
rpcuser=`pwgen 14 1 b`
rpcpass=`pwgen 36 1 b`
#color variables
readonly GRAY='\e[1;30m'
readonly DARKRED='\e[0;31m'
readonly RED='\e[1;31m'
readonly DARKGREEN='\e[0;32m'
readonly GREEN='\e[1;32m'
readonly DARKYELLOW='\e[0;33m'
readonly YELLOW='\e[1;33m'
readonly DARKBLUE='\e[0;34m'
readonly BLUE='\e[1;34m'
readonly DARKMAGENTA='\e[0;35m'
readonly MAGENTA='\e[1;35m'
readonly DARKCYAN='\e[0;36m'
readonly CYAN='\e[1;36m'
readonly UNDERLINE='\e[1;4m'
readonly NC='\e[0m'
readonly ERASEBACK='\e[1K'
readonly BEGINLINE='\e[0G'
#############################
### Functions and Methods ###
#############################
# Console message functions
function msg {
echo -e "${1}${NC}"
}
function msgc {
echo -e "${2}${1}${NC}"
}
# Gets the platform we are running on.
function getPlt {
if [ "$(uname)" == "Darwin" ]; then
echo 1
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
echo 0
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
echo 1
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
echo 1
elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then
echo 1
else
echo 1
fi
}
if [ `getPlt` == 0 ]; then
msgc "Running on Linux.." $YELLOW
else
msgc "Not running on Linux..." $RED
exit
fi
#############################
#############################
#pre-setup checks and dependencies installs
checkforrunningapt=$(ps -e | grep apt)
if [[ $checkforrunningapt == "" ]]
then
echo -e "Apt not currently running"
echo -e ""
else
echo -e "${RED}Error:${NC} Apt is already running, aborting script"
exit
fi
echo -e "Checking/installing/updating other script dependency's"
apt -y -qq install curl zip unzip nano ufw software-properties-common pwgen p7zip-full p7zip-rar
clear
sccmultitool_update=$(curl -s https://raw.githubusercontent.com/stakecube/SCC-Multitool/master/sccmultitool.sh)
if [[ $(cmp <(echo "$sccmultitool_update") ~/sccmultitool.sh) ]] && [[ $(diff <(echo "$sccmultitool_update") ~/sccmultitool.sh) ]]
then
updatesccmultitool=$([[ -f ~/sccmultitool.sh ]] && echo "1" || echo "0")
else
updatesccmultitool=0
fi
function displayname() {
cat << "EOF"
_____ _ _ _____ _
/ ____| | | | / ____| | |
| (___ | |_ __ _| | _____| | _ _| |__ ___
\___ \| __/ _` | |/ / _ \ | | | | | _ \ / _ \
____) | || (_| | < __/ |___| |_| | |_) | __/
|_____/ \__\__,_|_|\_\___|\_____\__,_|_.__/ \___|
EOF
}
displayname
#Tool menu
echo
echo -e "${UNDERLINE}${CYAN}Welcome to the StakeCube Multitools ${version}${NC}"
#echo -e '\e[4mWelcome to the StakeCube Multitools '${version}' \e[24m'
echo -e "${YELLOW}Please enter a number from the list and press [ENTER] to start tool"
echo -e "${YELLOW}1 - Newserver 8GB swap + IPv6 setup. REQUIRES RESTART ${MAGENTA}Contabo VPS only${NC}"
echo -e "${YELLOW}2 - Setup/Resize/Delete swap space with X MB swap space"
echo -e "${YELLOW}3 - Wallet update (all ${ticker} nodes)"
echo -e "${YELLOW}4 - Masternode stop/start/restart (stop/start/restart all ${ticker} nodes)"
echo -e "${YELLOW}5 - Remove MasterNode"
echo -e "${YELLOW}6 - Remove Multiple Masternodes"
echo -e "${YELLOW}7 - Masternode install"
echo -e "${YELLOW}8 - Masternode install with optional sleep delay function"
echo -e "${YELLOW}9 - Install New Node with manually specified IPv4/IPv6 Address"
echo -e "${YELLOW}10 - Download/Update StakeCubeCoin local bootstrap file from stakecube"
echo -e "${YELLOW}11 - Make/Update your StakeCubeCoin local bootstrap file from an existing SCC node"
echo -e "${YELLOW}12 - Chain/PoSe maintenance tool (single ${ticker} node)"
echo -e "${YELLOW}13 - Check block count status against explorer (all ${ticker} nodes)"
echo -e "${YELLOW}14 - Check MN health status and optional repair (all ${ticker} nodes)"
echo -e "${YELLOW}15 - Check block count and optional chain repair (all ${ticker} nodes)"
echo -e "${YELLOW}16 - Output some system diagnostic info"
echo
if [[ $prereleaseavailable == 1 ]]; then echo -e "${CYAN}97 - Pre-Release Menu${NC}"; fi
echo -e "${YELLOW}98 - Maintenance Sub-Menu - extra functions"
echo -e "${YELLOW}99 - Check for updated script from GitHub${NC}"
echo
echo -e "${YELLOW}0 - Exit"
echo
echo -e "${MAGENTA}This script can now use an optional sleep delay of up to 5 minutes per node on startup${NC}"
echo -e "${MAGENTA}This helps to prevent the VPS from being overloaded upon reboot when there are many nodes installed${NC}"
echo -e "${MAGENTA}This can make the install/start/restart/repair node(s) look like it has stopped working${NC}"
echo -e "${MAGENTA}This is just a side effect of the delay only(if it occurs)${NC}"
echo
echo -e "${YELLOW}This is an ${RED}OPTIONAL${YELLOW} feature that you can use by selecting appropriate options${NC}"
echo -e "${YELLOW}Please run the check install/update service files before installing nodes${NC}"
echo -e "${RED}only IF${YELLOW} you want to use the sleep delay functionality, it only needs to be ran once${NC}"
echo -e "${NC}"
if [[ $updatesccmultitool == "1" ]]
then
echo -e "${CYAN}New version of ${GREEN}SCCMultitool${NC}${CYAN} available${NC}"
echo -e ""
fi
read -rp "> " start
echo
function displaypause() {
local delaycount=$1
echo -e "${YELLOW}Press any key to abort countdown and continue${NC}"
while [ $delaycount -ge 0 ]
do
echo -en "${GREEN}Countdown ${NC}$delaycount"
sleep 1
echo -en "${ERASEBACK}${NC}${BEGINLINE}${NC}"
read -n 1 -t 0.05 $anykey
anykeystatus=$?
if [[ $anykeystatus -le 127 ]]
then
echo -en "${ERASEBACK}${NC}${BEGINLINE}${NC}"
return
fi
delaycount=$(($delaycount - 1))
done
echo
return
}
function is_number() {
# Author: neo3587
# Source: https://github.com/neo3587/dupmn
# <$1 = number>
[[ "$1" =~ ^[0-9]+$ ]] && echo "1"
}
function pad() {
[ "$#" -gt 1 ] && [ -n "$2" ] && printf "%$2.${2#-}s" "$1";
}
function checkprocess() {
local processname="$1"
# Retrieve process info for the user
local processidentoutput
processidentoutput=$(ps -U "$processname" -jh)
# Count how many matches for process name
local processident
processident=$(echo "$processidentoutput" | grep -c "$processname")
local processidentstatus=$?
# Check if process is in sleep mode
local checkprocessname
checkprocessname=$(echo "$processidentoutput" | grep -c sleep)
local checkprocessstatus=$?
if [[ "$checkprocessstatus" -eq 0 ]]; then
echo -e ""
echo -e "${YELLOW}Process is in sleep mode waiting to start still${NC}"
# Extract sleep counter
local sleepcounter
sleepcounter=$(echo "$processidentoutput" | grep 'sleep[ 0-9]' | awk '{print $NF}')
echo -e "${YELLOW}$sleepcounter"
# Add 15 seconds and pause
sleepcounter=$(( sleepcounter + 15 ))
echo -e "$sleepcounter"
displaypause "$sleepcounter"
fi
# Return 1 if process exists, 0 if not
if [[ "$processident" -gt 0 ]]; then
return 0 # process found
else
return 1 # process not found
fi
}
function checkyesno() {
local yesno="$1"
if [[ "$yesno" == "yes" || "$yesno" == "no" ]]; then
return
else
echo -e "${YELLOW}Please enter only${MAGENTA} yes${YELLOW} or${MAGENTA} no${NC}" >&2
echo >&2
echo -e "${RED}Aborting script${NC}" >&2
echo >&2
exit
fi
}
function prompt_yes_no() {
local prompt="$1"
local response
echo >&2
echo -e "${YELLOW}${prompt}${NC}" >&2
echo -e "${CYAN}Please enter ${MAGENTA}yes${NC} ${CYAN}or${NC} ${MAGENTA}no${CYAN} only${NC}" >&2
read -r response
checkyesno "$response"
echo "$response"
}
function checkaliasvalidity() {
local aliasname="$1"
local confFile="/home/$aliasname/.${coindir}/${coinname}.conf"
# Basic sanity-check: only allow alphanumeric + hyphen/underscore to
# prevent path-traversal or command injection when the value is used
# in filesystem paths and systemctl calls.
if [[ ! "$aliasname" =~ ^[A-Za-z0-9_-]+$ ]]; then
echo -e "${RED}Invalid alias '${aliasname}' — aborting${NC}" >&2
echo
return 1
fi
if [[ ! -f "$confFile" ]]; then
echo -e "${RED}Error: node not found at $confFile${NC}" >&2
echo
return 1
fi
}
# --------------------------------------------------------------
# prompt_for_alias – ask the user for a masternode alias
# --------------------------------------------------------------
# Usage:
# alias=$(prompt_for_alias) # ask only if $alias is empty
# alias=$(prompt_for_alias "$alias") # reuse an already‑provided alias
#
# The function prints the list of aliases, validates the choice
# with `checkaliasvalidity` and echoes the (valid) alias.
# --------------------------------------------------------------
function prompt_for_alias() {
local alias_input="${1:-}" # optional pre‑filled alias (may be empty)
# If the caller already gave us a non‑empty alias, just validate it.
if [[ -n "$alias_input" ]]; then
checkaliasvalidity "$alias_input" || exit 1
echo "$alias_input"
return 0
fi
# -----------------------------------------------------------------
# No alias supplied – show a friendly menu and read the input.
# -----------------------------------------------------------------
echo >&2
echo -e "${YELLOW}Checking home directory for masternode alias's${NC}" >&2
echo >&2
ls /home >&2 # list all accounts (you can filter if you wish)
echo >&2
echo -e "${YELLOW}Above are the alias names for the installed masternodes${NC}" >&2
echo -e "${CYAN}Please enter the masternode alias name to repair${NC}" >&2
echo >&2
# Read the answer (trim surrounding whitespace)
read -r alias_input </dev/tty
alias_input=$(printf '%s' "$alias_input" | xargs) # strip leading/trailing ws
# Validate – `checkaliasvalidity` will exit with an error message if it fails.
checkaliasvalidity "$alias_input" || exit 1
# If we get here the alias is good – echo it so the caller can capture it.
echo "$alias_input"
}
function debugmodeonoffsub() {
local alias=$1
local onoff=$2
local debugcmd=$3
local debugcount=$4
local grepcheckstatus=$5
# Uncomment for debugging
# echo -e ""
# echo -e "debugcount=$debugcount"
# echo -e "debugcmd=$debugcmd"
# echo -e "grepcheckstatus=$grepcheckstatus"
if [[ "$grepcheckstatus" -eq 1 && "$debugcount" -eq 0 && "$onoff" -eq 1 ]]; then
echo -e ""
echo -e "${YELLOW}Debug line not found, inserting turned on${NC}"
echo 'debug=1' >> "/home/$alias/.scc/stakecubecoin.conf"
echo -e ""
echo -e "${YELLOW}Restarting node and pausing ${sleeptimerinsec} seconds${NC}"
systemctl restart "$alias" --no-block
displaypause "$sleeptimerinsec"
return
elif [[ "$grepcheckstatus" -eq 1 && "$debugcount" -eq 0 && "$onoff" -eq 0 ]]; then
echo -e ""
echo -e "${YELLOW}Debug line not found, inserting turned off${NC}"
echo 'debug=0' >> "/home/$alias/.scc/stakecubecoin.conf"
echo -e ""
echo -e "${YELLOW}Restarting node and pausing ${sleeptimerinsec} seconds${NC}"
systemctl restart "$alias" --no-block
displaypause "$sleeptimerinsec"
return
fi
local debugcmd_lower="${debugcmd,,}"
case "$debugcmd_lower" in
"debug=0")
if [[ "$onoff" -eq 0 ]]; then
echo -e "${YELLOW}Debugging already disabled on node ${CYAN}$alias${NC}"
return
elif [[ "$onoff" -eq 1 ]]; then
echo -e "${YELLOW}Enabling debugging on node ${CYAN}$alias${NC}"
sed -i 's/debug=0/debug=1/gi' "/home/$alias/.scc/stakecubecoin.conf"
echo -e "${YELLOW}Restarting node and pausing ${sleeptimerinsec} seconds${NC}"
systemctl restart "$alias" --no-block
displaypause "$sleeptimerinsec"
return
fi
;;
"debug=1")
if [[ "$onoff" -eq 1 ]]; then
echo -e "${YELLOW}Debugging already enabled on node ${CYAN}$alias${NC}"
return
elif [[ "$onoff" -eq 0 ]]; then
echo -e "${YELLOW}Disabling debugging on node ${CYAN}$alias${NC}"
sed -i 's/debug=1/debug=0/gi' "/home/$alias/.scc/stakecubecoin.conf"
echo -e "${YELLOW}Restarting node and pausing ${sleeptimerinsec} seconds${NC}"
systemctl restart "$alias" --no-block
displaypause "$sleeptimerinsec"
return
fi
;;
*)
# fallback or do nothing
;;
esac
return
}
function checkifstart() {
local alias="$1"
local yesnostart
yesnostart=$(prompt_yes_no "Do you wish to try and start ${CYAN}$alias${YELLOW}?${NC}")
if [[ "$yesnostart" == "yes" ]]; then
echo
echo -e "${YELLOW}This will only attempt a start and will not verify it started${NC}"
systemctl start "$alias" --no-block
echo -e "${CYAN}Sent start command${NC}"
else
echo
echo -e "${YELLOW}Not starting${NC}"
fi
return
}
function debugmodeonoff() {
local alias="$1"
local onoff="$2"
local errorpid=0
local foundone=0
local debugcmd="empty"
local debugcount
local grepcheckstatus
echo -e ""
echo -e "${YELLOW}Checking for ${ticker} MN's${NC}"
echo -e ""
# Check specific alias
if [[ "$alias" != "0" ]]; then
if ! checkprocess "$alias"; then
errorpid=1
echo -e ""
echo -e "${RED}ERROR ${YELLOW}process for ${CYAN}$alias${YELLOW} not found${NC}"
return
fi
debugcmd=$(grep -ix 'debug=[0-1]' "/home/$alias/.scc/stakecubecoin.conf")
grepcheckstatus=$?
# Determine if debug is not set
if [[ -z "$debugcmd" && $grepcheckstatus -eq 1 ]]; then
debugcmd="empty"
fi
debugcount=$(grep -cFi 'debug' "/home/$alias/.scc/stakecubecoin.conf")
debugcountstatus=$?
# Debugging output (keep as per your instructions)
# echo -e "debugcmd=$debugcmd"
# echo -e "debugcount=$debugcount"
# echo -e "debugcountstatus=$debugcountstatus"
if [[ "$debugcount" -eq 1 && $debugcountstatus -eq 1 ]]; then
debugcount=0
fi
echo -e "found ${CYAN}$alias${NC}..."
debugmodeonoffsub "$alias" "$onoff" "$debugcmd" "$debugcount" "$grepcheckstatus"
return
fi
# Loop through /home/ to find relevant nodes
for i in /home/*; do
basename_i=$(basename "$i")
echo -e ""
if [[ "$basename_i" == *scc* ]]; then
foundone=1
errorpid=0
grepcheckstatus=""
debugcmd=""
echo -e "found ${CYAN}$basename_i${NC}..."
debugcount=$(grep -cFi 'debug' "$i/.scc/stakecubecoin.conf")
debugcmd=$(grep -ix 'debug=[0-1]' "$i/.scc/stakecubecoin.conf")
grepcheckstatus=$?
# Debugging output (keep as per your instructions)
# echo -e "debugcmd=$debugcmd"
# echo -e "debugcount=$debugcount"
# echo -e "debugcountstatus=$debugcountstatus"
# Check if process exists
if ! checkprocess "$basename_i"; then
errorpid=1
echo -e ""
echo -e "${RED}ERROR ${YELLOW}process for ${CYAN}$basename_i${YELLOW} not found${NC}"
fi
if [[ $errorpid -eq 0 ]]; then
if [[ -z "$debugcmd" ]]; then
debugcmd=1
debugcount=0
fi
debugmodeonoffsub "$basename_i" "$onoff" "$debugcmd" "$debugcount" "$grepcheckstatus"
fi
fi
done
if [[ "$foundone" -eq 0 ]]; then
echo -e ""
echo -e "${CYAN}No ${ticker} nodes found${NC}"
fi
}
function checknetcfgfile() {
local netdone=0
local netcfg="/etc/netplan/01-netcfg.yaml"
# Check 1st candidate
if [[ $netdone -eq 0 ]]; then
if [[ -f "$netcfg" ]]; then
netdone=1
else
netcfg="/etc/netplan/00-installer-config.yaml"
netdone=0
fi
fi
# Debugging output
# echo -e "$netdone"
# echo -e "$netcfg"
# Check 2nd candidate
if [[ $netdone -eq 0 ]]; then
if [[ -f "$netcfg" ]]; then
netdone=1
else
netcfg="/etc/netplan/50-cloud-init.yaml"
netdone=0
fi
fi
# Debugging output
# echo -e "$netdone"
# echo -e "$netcfg"
# Check 3rd candidate
if [[ $netdone -eq 0 ]]; then
if [[ -f "$netcfg" ]]; then
netdone=1
else
netdone=0
fi
fi
# Debugging output
# echo -e "$netdone"
# echo -e "$netcfg"
# Final validation
if [[ $netdone -eq 0 ]]; then
msg ""
msgc "Error - network config file not found (01-netcfg.yaml or 00-installer-config.yaml or 50-cloud-init.yaml)" "$red"
msg ""
exit
fi
}
function sleeprandomfilecheck() {
local maxwait="$1" # The desired maximum wait time
local sleepnumberfile="/usr/local/bin/sleeprandom"
# Check if the file exists
if [[ -e "$sleepnumberfile" ]]; then
# Read current MAXWAIT from the script
current_maxwait=$(grep -E '^MAXWAIT=' "$sleepnumberfile" | cut -d'=' -f2)
if [[ "$current_maxwait" != "$maxwait" ]]; then
echo -e "${CYAN}Updating MAXWAIT from $current_maxwait to $maxwait${NC}"
# Update the MAXWAIT line
sed -i "s/^MAXWAIT=.*/MAXWAIT=$maxwait/" "$sleepnumberfile"
else
echo -e "${CYAN}MAXWAIT already set to $maxwait, no change needed${NC}"
fi
else
# Create the file if it doesn't exist
echo -e "${CYAN}Installing sleep/delay file with MAXWAIT=$maxwait${NC}"
mkdir -p /usr/local/bin
cat << EOF > "$sleepnumberfile"
#!/bin/bash
MINWAIT=5
MAXWAIT=$maxwait
sleep \$((MINWAIT + RANDOM % (MAXWAIT - MINWAIT)))
EOF
chmod +x "$sleepnumberfile"
fi
}
function chain_repair() {
local alias="$1"
local bootstrapchoice="$2"
local aliasvalidstatus
local chaindownload=0
local nodesblock=0
local curl_output
local currentblock
local upperlimit lowerlimit
local forcechainrepair
local chainrepair2
# ------------------------------------------------------------------
# 1. Resolve alias
# ------------------------------------------------------------------
alias=$(prompt_for_alias "$alias") || exit 1
if [[ -z "$alias" ]]; then
echo -e "${RED}No alias provided — aborting${NC}"
return 1
fi
echo
echo -e "${YELLOW}Checking ${CYAN}$alias${YELLOW} block count against explorer${NC}"
echo
# -----------------------------------------------------------------
# Get the current block height from the explorer API.
# -----------------------------------------------------------------
curl_output=$(curl -s https://www.coinexplorer.net/api/v1/SCC/getblockcount)
if [[ -z "$curl_output" ]]; then
echo -e "${RED}Failed to contact explorer API${NC}"
return 1
fi
currentblock=$(printf '%s' "$curl_output" | tr -dc '0-9')
upperlimit=$((currentblock + 5))
lowerlimit=$((currentblock - 5))
echo
echo -e "${YELLOW}Explorer Block Height: ${CYAN}$currentblock${NC}"
echo -e "${YELLOW}Lower Block Height: ${CYAN}$lowerlimit${NC}"
echo -e "${YELLOW}Upper Block Height: ${CYAN}$upperlimit${NC}"
echo
nodesblock=$($alias getblockcount)
nodestatus=$?
if [[ $nodestatus == 0 ]]; then
# Compare node block height with explorer height
if [[ $nodestatus -eq 0 ]]; then
if [[ $currentblock -eq $nodesblock ]]; then
echo -e "${CYAN}$i ${NC}sccnode: $nodesblock explorer: $currentblock ${CYAN}Same as explorer${NC}"
echo
echo -e "${MAGENTA}Chain Repair is not needed for this node"
echo
forcechainrepair=$(prompt_yes_no "${YELLOW}Do you wish to still do the chain repair?")
if [[ $forcechainrepair == "no" ]]; then
return
fi
elif [[ $nodesblock -le $upperlimit && $nodesblock -ge $lowerlimit ]]; then
echo -e "${CYAN}$i ${NC}sccnode: $nodesblock explorer: $currentblock ${YELLOW}Different block count from explorer within variance${NC}"
echo
echo -e "${CYAN}Continuing with repair of node${NC}"
echo
else
echo -e "${CYAN}$i ${NC}sccnode: $nodesblock explorer: $currentblock ${RED}Different block count from explorer${NC}"
fi
else
echo -e "${RED}Something is wrong with ${CYAN}$i${NC}"
fi
else
echo -e "${RED}Something is wrong with ${CYAN}$alias${NC}"
echo -e "${YELLOW}Continuing repair${NC}"
echo
if [[ $updateallnodes == "no" ]]; then
checkchainrepair2=$(prompt_yes_no "${YELLOW}Do you wish to still chain repair?")
else
checkchainrepair2="yes"
fi
if [[ $checkchainrepair2 == "no" ]]
then
exit
fi
fi
echo
echo -e "Stopping ${MAGENTA}$alias${NC}"
aliasvalid=$(systemctl stop $alias)
aliasvalidstatus=$?
if [[ $aliasvalidstatus != 0 ]]; then
echo
echo -e "${RED}Error: ${CYAN}$alias ${MAGENTA} does not exist or has other errors${NC}"
echo
exit 1
fi
displaypause 10
if [[ $bootstrapchoice != "yes" ]]; then
echo
bootstrapchoice=$(prompt_yes_no "${YELLOW}Use offline bootstrap?${NC}")
else
echo -e "${YELLOW}Using offline bootstrap file${NC}"
fi
echo
cd /home/$alias
find /home/$alias/.${coindir}/ -name ".lock" -delete
find /home/$alias/.${coindir}/ -name ".walletlock" -delete
find /home/$alias/.${coindir}/* ! -name "wallet.dat" ! -name "*.conf" -delete
echo -e "${YELLOW}Downloading and/or Unzipping and replacing chain files for ${MAGENTA}$alias${NC}"
if [[ $bootstrapchoice == "yes" ]]; then
sccfile=~/${coinname}.zip
if test -e "$sccfile"; then
7za x ~/${coinname}.zip
echo -e "${YELLOW}$coinname local chain directory updated${NC}"
else
echo
echo -e "${RED}File doesn't exist${NC}, ${YELLOW}downloading chain${NC}"
wget -nv --show-progress ${snapshot} -O ~/${coinname}.zip
7za x ~/${coinname}.zip
echo
echo -e "${YELLOW}$coinname chain directory updated${NC}"
fi
else
echo
chaindownload=$(prompt_yes_no "${YELLOW}Do you wish to download from the web (${CYAN}yes${YELLOW}) or full chain downlaod (${CYAN}no${YELLOW})${NC}")
fi
if [[ $chaindownload == yes ]]; then
echo
echo -e "${YELLOW}Downloading bootstrap for offline use as well${NC}"
echo
wget -nv --show-progress ${snapshot} -O ~/${coinname}.zip
7za x ~/${coinname}.zip
echo
echo -e "${YELLOW}$coinname chain directory updated${NC}"
fi
chown -R $alias:$alias /home/${alias}
echo
echo -e "${CYAN}Starting $alias after repair${NC}"
echo
systemctl start --no-block ${alias}.service
displaypause 10
echo
echo -e "${YELLOW}Please wait for a moment.. and use ${CYAN}$alias masternode status${YELLOW} to check if $alais is ready for POSE unban or still showing READY${NC}"
echo -e "${YELLOW}If $alias showing POSE banned you will need to run the protx update command to unban${NC}"
echo -e "${YELLOW}Below is an example of the protx update command to use in your main wallets debug console${NC}"
echo -e "${YELLOW}protx update_service proTxHash ipAndPort operatorKey (operatorPayoutAddress feeSourceAddress)${NC}"
echo
echo -e "${YELLOW}Example: protx update_service proTxHash (127.0.0.1:40000 or [2001:2000:2000:2000:0000:0000:0000:0052]:40000) (blsprivatekey) '""' (feeSourceAddress)${NC}"
echo -e "${CYAN}Chain repair tool finished${NC}"
}
function install_mn() {
local bypassipv6setup=$1
local bypassipv6addr=$2
local sleepdelay=$3
if [[ $bypassipv6setup == yes ]]
then
# test=$bypassipv6addr
echo -e "${MAGENTA}Setting config file for IPV6 address $bypassipv6addr${NC}"
echo -e ""
ipchoice=yes
# else
# test=0
fi
#get user input alias and bind set varible#
echo -e ""
echo -e "${YELLOW}Checking home directory for masternode alias's${NC}"
echo -e ""
ls /home
echo -e ""
echo -e "${YELLOW}Above are the alias names for the installed masternodes${NC}"
echo -e "${YELLOW}Please enter MN alias. Example: ${CYAN}sccmn001${NC}"
echo -e "${YELLOW}To use other tools you must include ${CYAN}$ticker${YELLOW} in the alias${NC}"
read alias
if [[ -f /home/$alias/.${coindir}/${coinname}.conf ]]
then
echo -e ""
echo -e "${RED}Error duplicate node name${NC}"
echo -e ""
exit
fi
echo -e ""
echo -e "${YELLOW}${UNDERLINE}Enter the BLS secret key${NC}"
read key
echo -e ""
echo -e "${YELLOW}${UNDERLINE}Please enter a unique RPC port number. Default is ${CYAN}$rpcport${NC}"
echo -e "${YELLOW}Examples: for ${CYAN}sccmn001 ${YELLOW}use ${CYAN}40010 ${YELLOW}and so on${NC}"
echo -e "${YELLOW}So it's 4(node number)0 (40010 for sccmn001)${NC}"
read rpcport
if [[ $bypassipv6setup == no ]]
then
#IPv4/v6 choice and setup
echo -e ""
# echo -e "${YELLOW}Would you like to setup with an IPv6 address?{$NC}"
# echo -e "${CYAN}Please enter ${MAGENTA}yes${NC} ${CYAN}or${NC} ${MAGENTA}no${CYAN} only${NC}"
# read ipchoice
# checkyesno $ipchoice
ipchoice=$(prompt_yes_no "${YELLOW}Would you like to setup with an IPv6 address?{$NC}")
# echo -e "Passed yes/no check"
#script network config dependency
echo -e ""
echo -e "Checking/installing dependency for auto IP setup"
if [[ $ipchoice == yes ]]
then
#set default IPv6
checknetcfgfile
sed -i '1{/^$/d}' $netcfg
netconfcount=$(grep -c "/64" $netcfg)
linenumber1=$((grep -n "/64" $netcfg) | cut -d\: -f1 | head -n 1)
linenumber2=$(( $linenumber1+$netconfcount ))
# echo -e "$linenumber1"
# echo -e "$linenumber2"
dipv6=$(sed -n "$linenumber1"p $netcfg)
spaces=$(echo -e "$dipv6" | tr -cd ' \t' | wc -c)
spaces=$(( $spaces-2 ))
ipv6test="$(echo $dipv6 | grep -E '.{0,4}\/64')"
ipv6test2="$(echo $ipv6test | awk 'match($0,"/64"){print substr($0,RSTART-4,4)}')"
ipv6test2="$(echo $ipv6test2 | cut -d\: -f3)"
ipv6test2="$(echo $ipv6test2 | tr -d ':')"
ipv6test3=$(( $ipv6test2 + $netconfcount + 50 ))
# echo -e "ipv6test $ipv6test"
# echo -e "ipv6test2 $ipv6test2"
# echo -e "ipv6test3 $ipv6test3"
# echo -e " 2 $dipv6"
# echo -e " 3 $spaces"
# echo -e "$netconfcount"
cipv6=$(( $ipv6test3 ))
# echo -e "$cipv6"
ipv6="$(echo $dipv6 | sed "s/\:$ipv6test2/\:$cipv6/g")"
echo -e ""
echo -e "New IPv6 is $ipv6"
finalconfigipv6="$(echo -e "$(pad " " $spaces) ${ipv6}")"
# echo -e "$finalconfigipv6"
# sed -i "${linenumber2}i\\${finalconfigipv6}" $netcfg
# sed -i '1{/^$/d}' $netcfg
# netconfcount=$(grep -c :0000:0000 $netcfg)
# linenumber1=$((grep -n ":0000:0000" $netcfg) | cut -d\: -f1 | head -n 1)
# echo -e "$linenumber1"
# dipv6=$(sed -n "$linenumber1"p $netcfg)
# echo -e " 2 $dipv6"
# echo -e "$netconfcount"
# cipv6=$(( $netconfcount+51 ))
# echo -e "$cipv6"
# ipv6="$(echo $dipv6 | sed "s/:0001/:$cipv6/g")"
# echo -e "New IPv6 is $ipv6"
# #Add IPv6 address to netcfg file
# sed -i "/gateway6/i \ \ \ \ \ \ \ \ ${ipv6}" $netcfg
# netplan apply
#tidy IP input for conf
ipv6conf="$(echo $ipv6 | sed 's/.\{3\}$//')"
ipv6conf="$(echo $ipv6conf | sed "s/- //g")"
else
#check ip set IP/bind variable
echo -e "Finding IPv4 address"
ipadd=$(curl http://ifconfig.me/ip)
echo -e "Your IPv4 is $ipadd"
echo -e "Auto IPv4 set"
fi
else
ipadd=$bypassipv6addr
fi
echo -e ""
# echo -e "${YELLOW}Use offline bootstrap?${NC}"
# echo -e "${CYAN}Please enter ${MAGENTA}yes${NC} ${CYAN}or${NC} ${MAGENTA}no${CYAN} only${NC}"
# read bootstrapchoice
# checkyesno $bootstrapchoice
bootstrapchoice=$(prompt_yes_no "${YELLOW}Use offline bootstrap?${NC}")
if [[ $bootstrapchoice == no ]]
then
echo -e ""
# echo -e "${YELLOW}Do you wish to download from the web (${CYAN}yes${YELLOW}) or full chain downlaod (${CYAN}no${YELLOW})${NC}"
# echo -e "${CYAN}Please enter ${MAGENTA}yes${NC} ${CYAN}or${NC} ${MAGENTA}no${CYAN} only${NC}"
# read chaindownload
# checkyesno $chaindownload
chaindownload=$(prompt_yes_no "${YELLOW}Do you wish to download from the web (${CYAN}yes${YELLOW}) or full chain downlaod (${CYAN}no${YELLOW})${NC}")
else
chaindownload=0
fi
if [[ $ipchoice == yes ]]
then
echo -e ""
echo -e "${CYAN}Applying IP configuration${NC}"
sed -i "${linenumber2}i\\${finalconfigipv6}" $netcfg
netplan apply
fi
echo -e ""
ufw allow ssh
echo -e "${CYAN}Making sure your VPS is up to date before continuing install${NC}"
apt update -y
#setup user
echo -e ""
echo -e "${YELLOW}Setting up user ${CYAN}$alias${NC}"
adduser "$alias" <<EOF
echo ${pass}
echo ${pass}
/
/
/
/
/
/
/
EOF
echo -e ""
echo -e "${CYAN}User ${CYAN}$alias${CYAN} setup${NC}"
echo -e ""
#Sleep/Delay check and install
if [[ $sleepdelay == yes ]]
then
sleeprandomfilecheck $sleeprandomtimer
fi
#Node binaries check and install if needed
cd /usr/local/bin
binfile=/usr/local/bin/${coinnamecli}
if test -e "$binfile"
then
echo -e "${CYAN}Node binaries already downloaded and setup${NC}"
echo -e ""
else
echo -e "${YELLOW}Installing node binaries for ${MAGENTA}$alias${NC}"
cd /usr/local/bin
wget -nv --show-progress ${binaries} -O ${coinname}.zip
7za x ${coinname}.zip
chmod +x ${coinnamecli} ${coinnamed}
rm ${coinname}.zip
echo -e "${CYAN}$alias node binaries downloaded and installed${NC}"
echo -e ""
fi
#Node intergration - creation of alias in /usr/local/bin for executing commands to users daemon
echo -e "${YELLOW}Node Intergration${NC}"