Repository navigation
Expand file tree
/
Copy pathnode.sh
More file actions
executable file
·6638 lines (5798 loc) · 194 KB
/
Copy pathnode.sh
File metadata and controls
executable file
·6638 lines (5798 loc) · 194 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
# elastos-node - hardened fork of elastos/Elastos.Node
ELASTOS_NODE_VERSION="1.1.0"
# Reset override flags so a value inherited from the environment cannot silently enable them.
FORCE_ELA=
#
# utility
#
echo_warn()
{
if [ -t 1 ]; then
echo -e "\033[1;33mWARNING:\033[0m $1" 1>&2
else
echo "WARNING: $1" 1>&2
fi
}
echo_error()
{
if [ -t 1 ]; then
echo -e "\033[1;31mERROR:\033[0m $1" 1>&2
else
echo "ERROR: $1" 1>&2
fi
}
echo_info()
{
if [ -t 1 ]; then
echo -e "\033[1;34mINFO:\033[0m $1"
else
echo "INFO: $1"
fi
}
echo_ok()
{
if [ -t 1 ]; then
echo -e "\033[1;32mOK:\033[0m $1"
else
echo "OK: $1"
fi
}
#
# UI: color + status glyphs, honoring NO_COLOR, --no-color, and non-TTY output.
#
UI_DOT_OK=$'\xe2\x97\x8f' # filled circle (running / healthy)
UI_DOT_OFF=$'\xe2\x97\x8b' # empty circle (stopped)
UI_DOT_WARN=$'\xe2\x97\x90' # half circle (attention)
ui_use_color()
{
[ -n "$NO_COLOR" ] && return 1
[ -n "$UI_NO_COLOR" ] && return 1
[ -t 1 ] || return 1
return 0
}
ui_c() { if ui_use_color; then printf '\033[%sm%s\033[0m' "$1" "$2"; else printf '%s' "$2"; fi; }
ui_green() { ui_c '1;32' "$1"; }
ui_yellow() { ui_c '1;33' "$1"; }
ui_red() { ui_c '1;31' "$1"; }
ui_dim() { ui_c '2' "$1"; }
ui_bold() { ui_c '1' "$1"; }
# chain_running <chain>: true if the chain's process is alive (no RPC, cannot hang).
chain_running()
{
case "$1" in
ela) pgrep -x ela >/dev/null 2>&1 ;;
arbiter) pgrep -x arbiter >/dev/null 2>&1 ;;
esc|eco|pgp|pg|eid)
pgrep -f "^\./$1 .*--rpc " >/dev/null 2>&1 ;;
esc-oracle) pgrep -fx 'node crosschain_oracle.js' >/dev/null 2>&1 ;;
eid-oracle) pgrep -fx 'node crosschain_eid.js' >/dev/null 2>&1 ;;
eco-oracle) pgrep -fx 'node crosschain_eco.js' >/dev/null 2>&1 ;;
pgp-oracle) pgrep -fx 'node crosschain_pgp.js' >/dev/null 2>&1 ;;
pg-oracle) pgrep -fx 'node crosschain_pg.js' >/dev/null 2>&1 ;;
*) return 1 ;;
esac
}
# EVM_CHAINS: the geth-based side chains (single source of truth).
EVM_CHAINS="esc eco pgp pg eid"
# is_evm_chain <chain>: true if <chain> is one of the EVM side chains.
is_evm_chain() { case " $EVM_CHAINS " in *" $1 "*) return 0 ;; *) return 1 ;; esac; }
# evm_rpc_bind: the address EVM chains bind RPC/WS to. Defaults to 127.0.0.1 (localhost
# only). Override with the EVM_RPC_BIND env var (this session) or a persistent
# ~/.config/elastos/evm_rpc_bind file. An invalid value falls back to 127.0.0.1
# (fail-closed - a typo must never bind somewhere unintended).
evm_rpc_bind()
{
local b=$EVM_RPC_BIND
[ -z "$b" ] && [ -r ~/.config/elastos/evm_rpc_bind ] && b=$(tr -d '[:space:]' < ~/.config/elastos/evm_rpc_bind 2>/dev/null)
[ -z "$b" ] && b=127.0.0.1
echo "$b" | grep -qE '^([0-9]{1,3}\.){3}[0-9]{1,3}$' || b=127.0.0.1
echo "$b"
}
# did_you_mean <input> <valid words...>: print a suggestion for a near miss.
did_you_mean()
{
local input=$1; shift
local best= cand
for cand in $*; do case "$cand" in "$input"*) best=$cand; break ;; esac; done
[ -z "$best" ] && for cand in $*; do case "$cand" in *"$input"*) best=$cand; break ;; esac; done
[ -n "$best" ] && echo " Did you mean '$best'?"
}
# verify_started <chain>: confirm the daemon is alive shortly after a start; if not,
# surface the failure + the tail of its most recent log, so a dead-on-launch daemon is
# reported immediately instead of inferred from a later "stopped" status.
verify_started()
{
local chain=$1 log bind
if chain_running "$chain"; then
if is_evm_chain "$chain"; then
bind=$(evm_rpc_bind)
if [ "$bind" == "127.0.0.1" ]; then
ui_dim " $chain RPC/WS bound to 127.0.0.1 (localhost only; upstream was public 0.0.0.0). Remote access: SSH tunnel / reverse proxy."
else
echo_warn "$chain RPC/WS bound to $bind (PUBLIC - reachable off-box). Firewall it; persist via ~/.config/elastos/evm_rpc_bind, or set 127.0.0.1 to revert."
fi
fi
return 0
fi
echo_error "$chain failed to start (not running after launch)"
log=$(ls -t "$SCRIPT_PATH/$chain/logs/"*.log "$SCRIPT_PATH/$chain/elastos/logs/node/"*.log 2>/dev/null | head -1)
if [ -n "$log" ]; then
echo " last lines of $(basename "$log"):"
tail -n 6 "$log" 2>/dev/null | sed 's/^/ /'
fi
return 1
}
# --- health data substrate (timeout-bounded; never hangs; error != 0) ---
evm_port() { case "$1" in esc) echo 20636;; eco) echo 20656;; pgp) echo 20666;; pg) echo 20676;; eid) echo 20646;; *) return 1;; esac; }
# evm_rpc <chain> <method> [params-json] : raw JSON response (curl capped at 3s)
evm_rpc()
{
local port; port=$(evm_port "$1") || return 1
curl -s --max-time 3 -X POST -H 'Content-Type: application/json' \
--data "{\"jsonrpc\":\"2.0\",\"method\":\"$2\",\"params\":${3:-[]},\"id\":1}" \
"http://127.0.0.1:$port" 2>/dev/null
}
# hex_to_dec <0x..> : decimal, or empty. Avoids the $(( )) octal/zero trap.
hex_to_dec() { case "$1" in 0x*|0X*) echo $((16#${1#0[xX]})) ;; *) return 1 ;; esac; }
evm_height() { local r; r=$(evm_rpc "$1" eth_blockNumber | jq -r '.result // empty' 2>/dev/null); hex_to_dec "$r"; }
evm_peers() { local r; r=$(evm_rpc "$1" net_peerCount | jq -r '.result // empty' 2>/dev/null); hex_to_dec "$r"; }
evm_syncing()
{
local r; r=$(evm_rpc "$1" eth_syncing | jq -r '.result' 2>/dev/null)
[ -z "$r" ] && return 1
if [ "$r" == "false" ]; then echo synced; else echo syncing; fi
}
ela_height() { local h; h=$(ela_client info getcurrentheight 2>/dev/null); [[ "$h" =~ ^[0-9]+$ ]] && echo "$h" || return 1; }
ela_peers() { local p; p=$(ela_client info getconnectioncount 2>/dev/null); [[ "$p" =~ ^[0-9]+$ ]] && echo "$p" || return 1; }
# unified per-chain probes (ela vs EVM); services (oracle/arbiter) have no height/peers
chain_height() { case "$1" in ela) ela_height ;; esc|eco|pgp|pg|eid) evm_height "$1" ;; *) return 1 ;; esac; }
chain_peers() { case "$1" in ela) ela_peers ;; esc|eco|pgp|pg|eid) evm_peers "$1" ;; *) return 1 ;; esac; }
chain_synced() { case "$1" in ela) ela_synced 2>/dev/null && echo synced || echo syncing ;; esc|eco|pgp|pg|eid) evm_syncing "$1" ;; *) return 1 ;; esac; }
# evm_reward_status <chain> : cold | hot | unset (hot = reward addr == local keystore acct)
evm_reward_status()
{
local addr hot kf
[ -f "$SCRIPT_PATH/$1/data/miner_address.txt" ] && addr=$(tr -d '[:space:]' < "$SCRIPT_PATH/$1/data/miner_address.txt" 2>/dev/null)
echo "$addr" | grep -qiE '^0x[0-9a-f]{40}$' || { echo unset; return; }
kf=$(ls "$SCRIPT_PATH/$1/data/keystore/"UTC* 2>/dev/null | head -1)
[ -n "$kf" ] && hot=$(jq -r .address "$kf" 2>/dev/null)
if [ -n "$hot" ] && [ "$(echo "${addr#0x}" | tr A-Z a-z)" == "$(echo "$hot" | tr A-Z a-z)" ]; then
echo hot
else
echo cold
fi
}
# render_health <chain>: print a one-line verdict; exit 0 healthy, 1 if attention needed.
# Designed for monitoring/cron: `node.sh esc health && ok || alert`.
render_health()
{
local chain=$1 p sy
"${chain}_installed" 2>/dev/null || { echo "$chain: not installed"; return 1; }
chain_running "$chain" || { echo "$chain: stopped"; return 1; }
case "$chain" in
*-oracle|arbiter) echo "$chain: running"; return 0 ;;
esac
sy=$(chain_synced "$chain" 2>/dev/null)
p=$(chain_peers "$chain" 2>/dev/null)
if [ -z "$sy" ] || [ -z "$p" ]; then echo "$chain: running (rpc unreachable)"; return 1; fi
[ "$sy" == "syncing" ] && { echo "$chain: syncing"; return 1; }
[ -n "$p" ] && [ "$p" == "0" ] && { echo "$chain: no peers"; return 1; }
echo "$chain: healthy"; return 0
}
# render_health_all: health for every chain in the profile; exit non-zero if any unhealthy.
render_health_all()
{
local chain rc=0
for chain in $(profile_chains); do
render_health "$chain" || rc=1
done
return $rc
}
# render_summary: one row per chain in the active profile (the fleet glance).
# chain_pid <chain>: the live PID (same detection as chain_running).
chain_pid()
{
case "$1" in
ela) pgrep -x ela 2>/dev/null | head -1 ;;
arbiter) pgrep -x arbiter 2>/dev/null | head -1 ;;
esc|eco|pgp|pg|eid) pgrep -f "^\./$1 .*--rpc " 2>/dev/null | head -1 ;;
esc-oracle) pgrep -fx 'node crosschain_oracle.js' 2>/dev/null | head -1 ;;
eid-oracle) pgrep -fx 'node crosschain_eid.js' 2>/dev/null | head -1 ;;
eco-oracle) pgrep -fx 'node crosschain_eco.js' 2>/dev/null | head -1 ;;
pgp-oracle) pgrep -fx 'node crosschain_pgp.js' 2>/dev/null | head -1 ;;
pg-oracle) pgrep -fx 'node crosschain_pg.js' 2>/dev/null | head -1 ;;
esac
}
# render_status_one <chain>: the chain's own status block, with the noise fields
# (Balance / PID / #Files / #TCP / port lists) removed. Labeled + aligned, like the
# classic view the operator preferred - just without the clutter.
render_status_one()
{
# the familiar upstream labeled block, verbatim - one aligned field per line
"${1}_status"
}
# render_status_all: a card for every chain in the active profile.
render_status_all()
{
# one classic labeled block per chain, a blank line between - no extra chrome
local chain
echo
for chain in $(profile_chains); do "${chain}_status"; echo; done
echo " $(ui_dim "one-line glance: $SCRIPT_NAME summary")"
echo
}
render_summary()
{
local prof total=0 running=0 stopped=0 chain st glyph h p sy issues=
prof=$(get_profile)
echo
printf ' %s profile: %s\n' "$(ui_bold 'Elastos node')" "$prof"
printf ' %-12s %-9s %-13s %-6s %s\n' 'CHAIN' 'STATE' 'HEIGHT' 'PEERS' 'HEALTH'
printf ' %s\n' '-------------------------------------------------------'
for chain in $(profile_chains); do
total=$((total + 1)); h='-'; p='-'
if ! "${chain}_installed" 2>/dev/null; then
st='-'; glyph=$(ui_dim "$UI_DOT_OFF")
elif chain_running "$chain"; then
st='running'; glyph=$(ui_green "$UI_DOT_OK"); running=$((running + 1))
h=$(chain_height "$chain" 2>/dev/null)
p=$(chain_peers "$chain" 2>/dev/null)
case "$chain" in
*-oracle|arbiter) [ -z "$h" ] && h='-'; [ -z "$p" ] && p='-' ;;
*) [ -z "$h" ] && h='?'; [ -z "$p" ] && p='?' ;;
esac
sy=$(chain_synced "$chain" 2>/dev/null)
if [ "$sy" == "syncing" ]; then glyph=$(ui_yellow "$UI_DOT_WARN"); issues="$issues $chain(syncing)"; fi
if [ -n "$p" ] && [ "$p" == "0" ]; then glyph=$(ui_red "$UI_DOT_WARN"); issues="$issues $chain(no-peers)"; fi
else
st='stopped'; glyph=$(ui_dim "$UI_DOT_OFF"); stopped=$((stopped + 1)); issues="$issues $chain(stopped)"
fi
printf ' %-12s %-9s %-13s %-6s %s\n' "$chain" "$st" "$h" "$p" "$glyph"
done
printf ' %s\n' '-------------------------------------------------------'
echo " $(ui_green "$UI_DOT_OK") healthy $(ui_yellow "$UI_DOT_WARN") syncing/attention $(ui_dim "$UI_DOT_OFF") stopped"
if [ -z "$issues" ]; then
echo " $(ui_green '✓') $running/$total running, all healthy"
else
echo " $(ui_yellow '⚠') attention:$issues"
fi
echo
}
# chain_health_json <chain> / render_json_one / render_json_all : machine-readable
chain_health_json()
{
local chain=$1 inst=false run=false h='' p='' sy='' rw=''
"${chain}_installed" 2>/dev/null && inst=true
if [ "$inst" == true ] && chain_running "$chain"; then
run=true
h=$(chain_height "$chain" 2>/dev/null)
p=$(chain_peers "$chain" 2>/dev/null)
sy=$(chain_synced "$chain" 2>/dev/null)
case "$chain" in esc|eco|pgp|pg|eid) rw=$(evm_reward_status "$chain" 2>/dev/null) ;; esac
fi
jq -n --arg chain "$chain" --argjson installed "$inst" --argjson running "$run" \
--arg height "$h" --arg peers "$p" --arg sync "$sy" --arg reward "$rw" \
'{chain:$chain, installed:$installed, running:$running,
height:(if $height=="" then null else ($height|tonumber) end),
peers:(if $peers=="" then null else ($peers|tonumber) end),
sync:(if $sync=="" then null else $sync end),
reward:(if $reward=="" then null else $reward end)}'
}
render_json_one() { chain_health_json "$1"; }
render_json_all()
{
local chain first=1
printf '['
for chain in $(profile_chains); do
[ $first -eq 1 ] || printf ','
first=0
chain_health_json "$chain"
done
printf ']\n'
}
# noninteractive: true when stdin is not a terminal (cron / CI / pipe). Used to take a
# safe default instead of blocking on a read prompt.
noninteractive() { [ ! -t 0 ]; }
# profile_prompt_if_unset: ask main-chain-only vs full stack the first time.
profile_prompt_if_unset()
{
[ -n "$PROFILE_OVERRIDE" ] && return 0
[ -f "$PROFILE_FILE" ] && return 0
echo "What will this node run?"
echo " [1] Main chain only (ELA)"
echo " [2] Full stack (ELA + side chains + oracles + arbiter)"
local sel
read -p '? Your option: [2] ' sel || { sel=2; echo " (no input - defaulting to full stack)"; }
case "$sel" in
1) set_profile mainchain >/dev/null ;;
*) set_profile full >/dev/null ;;
esac
echo
}
update_script()
{
# The fork updates ITSELF (not upstream), so the hardening is never reverted.
local SCRIPT_URL=https://raw.githubusercontent.com/4HM3DMD/elastos-node/main/node.sh
local SHA_URL=https://raw.githubusercontent.com/4HM3DMD/elastos-node/main/node.sh.sha256
local SCRIPT=$SCRIPT_PATH/$(basename $BASH_SOURCE)
local SCRIPT_TMP=$SCRIPT.tmp
echo "Downloading $SCRIPT_URL..."
if ! curl -fsSL -o "$SCRIPT_TMP" "$SCRIPT_URL"; then
echo_error "download failed"
rm -f "$SCRIPT_TMP"
return 1
fi
# Integrity: if a published checksum exists, the download MUST match it.
local WANT=$(curl -fsSL "$SHA_URL" 2>/dev/null | awk '{print $1}')
if [ -n "$WANT" ]; then
local GOT=$(shasum -a 256 "$SCRIPT_TMP" 2>/dev/null | awk '{print $1}')
if [ "$WANT" != "$GOT" ]; then
echo_error "checksum mismatch - refusing to update (want $WANT, got $GOT)"
rm -f "$SCRIPT_TMP"
return 1
fi
echo_ok "checksum verified"
else
echo_warn "no published checksum found - updating without integrity check"
fi
# Never install a script that does not parse.
if ! bash -n "$SCRIPT_TMP"; then
echo_error "downloaded script failed syntax check - refusing to update"
rm -f "$SCRIPT_TMP"
return 1
fi
mv "$SCRIPT_TMP" "$SCRIPT"
chmod a+x "$SCRIPT"
echo_ok "$SCRIPT updated"
echo
# Re-exec the JUST-DOWNLOADED script's harden, not the old code still in memory, so
# any newly-added ports are closed in the same step (bash never reloads a running file).
"$SCRIPT" harden 2>/dev/null || harden_firewall
}
# has_cold_miner <chain>: true when the chain either does not mine or has a valid
# cold reward address set. Silent; used by warn_hot_miner and status displays.
has_cold_miner()
{
local chain=$1
is_evm_chain "$chain" || return 0 # only EVM side chains mine
local pwfile=~/.config/elastos/${chain}.txt
local addrfile=$SCRIPT_PATH/${chain}/data/miner_address.txt
[ -f "$pwfile" ] || return 0 # not a mining node; nothing to check
local addr=
[ -f "$addrfile" ] && addr=$(tr -d '[:space:]' < "$addrfile" 2>/dev/null)
echo "$addr" | grep -qiE '^0x[0-9a-f]{40}$'
}
# warn_hot_miner <chain>: a mining chain with no cold reward address still starts,
# but the operator is warned in red: rewards credit the node's LOCAL (hot) account.
warn_hot_miner()
{
local chain=$1
has_cold_miner "$chain" && return 0
echo "$(ui_red "WARNING: $chain is mining WITHOUT a cold reward address.")"
echo "$(ui_red " Block rewards will credit this node's LOCAL (hot) account.")"
echo " Set one, then restart: $SCRIPT_NAME reward set 0xYOURCOLDADDRESS"
return 0
}
set_env()
{
# ulimit open files: use system setting if it is greater
if [ $(ulimit -n) -lt 40960 ]; then
ulimit -n 40960
fi
}
check_env()
{
#echo "Checking OS Version..."
if [ "$(uname -s)" == "Linux" ]; then
local DIST_NAME=$(lsb_release -s -i 2>/dev/null)
local DIST_VER=$(lsb_release -s -r 2>/dev/null)
if [ "$DIST_NAME" == "Ubuntu" ]; then
if [ "$DIST_VER" \< "18.04" ]; then
echo_error "this script requires Ubuntu 18.04 or higher"
exit
fi
else
echo_error "do not support $(cat /etc/issue)"
exit
fi
if [ "$(uname -m)" == "aarch64" ]; then
# The ARM64 version has not been fully tested.
# USE ARM64 BUILD AT YOUR OWN RISK!
# To enable ARM64 version, set ENABLE_ARM64 to 1 or non-empty.
local ENABLE_ARM64=
if [ "$ENABLE_ARM64" == "" ]; then
echo_error "do not support $(uname -m)"
exit
else
echo_warn "the arm64 build have not been fully-tested"
fi
elif [ "$(uname -m)" == "x86_64" ]; then
true
else
echo_error "do not support $(uname -m)"
fi
else
echo_error "do not support $(uname -s)"
exit
fi
#echo "Checking sudo permission..."
sudo -n true 2>/dev/null
if [ "$?" == "0" ]; then
echo_warn "it is better to run as a normal user without sudo permission"
fi
if [ "$(which jq)" == "" ]; then
echo_error "cannot find jq (https://github.com/stedolan/jq)"
echo_info "sudo apt-get install -y jq"
exit
fi
if [ "$(which lsof)" == "" ]; then
echo_error "cannot find lsof"
echo_info "sudo apt-get install -y lsof"
exit
fi
if [ "$(which rotatelogs)" == "" ]; then
echo_error "cannot find rotatelogs"
echo_info "sudo apt-get install -y apache2-utils"
exit
fi
if [ "${BASH_ARGV[0]}" == "init" ]; then
check_env_oracle
fi
}
check_env_oracle()
{
if [ "$(uname -sm)" == "Linux aarch64" ]; then
if [ "$(which make)" == "" ]; then
echo_error "cannot find make"
echo_info "sudo apt-get install -y make"
exit
fi
if [ "$(which gcc)" == "" ]; then
echo_error "cannot find gcc"
echo_info "sudo apt-get install -y gcc"
exit
fi
if [ "$(which g++)" == "" ]; then
echo_error "cannot find g++"
echo_info "sudo apt-get install -y g++"
exit
fi
fi
}
init_config()
{
# the network config is always node.json (Elastos convention), independent of the
# script's filename - so running this as e.g. node.sh.new (migration rehearsal) still
# finds the existing config instead of prompting and writing a stray file.
local CONFIG_FILE=~/.config/elastos/node.json
if [ -f $CONFIG_FILE ]; then
echo_error "$CONFIG_FILE exist"
exit
fi
echo "Please select the network:"
echo
echo " 1. MainNet"
echo " 2. TestNet"
local SELECT=
while true; do
echo
read -p '? Your option: [1] ' SELECT || { SELECT=1; echo_info "no input - defaulting to MainNet"; }
if [ "$SELECT" == "" ] || [ "$SELECT" == "1" ]; then
local CHAIN_TYPE=mainnet
break
elif [ "$SELECT" == "2" ]; then
local CHAIN_TYPE=testnet
break
else
echo_error "no such option: $SELECT"
continue
fi
done
mkdir -p $(dirname $CONFIG_FILE)
chmod 700 $(dirname $CONFIG_FILE)
touch $CONFIG_FILE
chmod 600 $CONFIG_FILE
cat <<EOF >$CONFIG_FILE
{
"chain-type": "$CHAIN_TYPE"
}
EOF
echo_info "config file: $CONFIG_FILE"
}
load_config()
{
# the network config is always node.json (Elastos convention), independent of the
# script's filename - so running this as e.g. node.sh.new (migration rehearsal) still
# finds the existing config instead of prompting and writing a stray file.
local CONFIG_FILE=~/.config/elastos/node.json
if [ ! -f $CONFIG_FILE ]; then
# First run: choose the network and write the config, then CONTINUE with the
# requested command instead of exiting (no more running the command twice).
init_config
fi
export CHAIN_TYPE=$(cat $CONFIG_FILE | jq -r '.["chain-type"]')
if [ "$CHAIN_TYPE" != "mainnet" ] && \
[ "$CHAIN_TYPE" != "testnet" ]; then
echo_error "no chain type selected"
echo_info "Please edit $CONFIG_FILE and set chain-type as mainnet or testnet"
exit
fi
}
set_path()
{
# bash will not read .profile if .bash_profile exists
if [ -f ~/.bash_profile ]; then
local PROFILE_FILE=~/.bash_profile
elif [ -f ~/.profile ]; then
local PROFILE_FILE=~/.profile
else
touch ~/.profile
local PROFILE_FILE=~/.profile
fi
if [ ! -f $PROFILE_FILE ]; then
echo "ASSERT: no $PROFILE_FILE found, skip..."
fi
grep "export PATH=$SCRIPT_PATH:\$PATH" $PROFILE_FILE >/dev/null
if [ "$?" != "0" ]; then
echo "Updating $PROFILE_FILE..."
echo "export PATH=$SCRIPT_PATH:\$PATH" >>$PROFILE_FILE
echo_info "please re-login to make PATH effective"
fi
}
set_cron()
{
local SP0='[[:space:]]*'
local SP1='[[:space:]]\+'
crontab -l 2>/dev/null |
grep -q "^${SP0}@reboot${SP1}~/node/node.sh${SP1}start${SP0}$"
if [ "$?" != "0" ]; then
(
crontab -l 2>/dev/null
echo '@reboot ~/node/node.sh start'
) | crontab -
fi
crontab -l 2>/dev/null |
grep -q "^${SP0}\*/10${SP1}\*${SP1}\*${SP1}\*${SP1}\*${SP1}~/node/node.sh${SP1}compress_log${SP0}$"
if [ "$?" != "0" ]; then
(
crontab -l 2>/dev/null
echo '*/10 * * * * ~/node/node.sh compress_log'
) | crontab -
fi
crontab -l 2>/dev/null
}
extip()
{
curl -s https://checkip.amazonaws.com
}
trim()
{
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
mem_free()
{
free -mt | sed -n '4 s/.* //p'
}
mem_usage()
{
if [ "$1" == "" ]; then
return
fi
pmap $1 | tail -1 | sed 's/.* //' | numfmt --from=iec --to=iec
}
run_time()
{
if [ "$1" == "" ]; then
return
fi
ps -oetime= -p $PID | trim
}
num_tcps()
{
if [ "$1" == "" ]; then
return
fi
lsof -n -a -itcp -p $PID 2>/dev/null | wc -l | trim
}
num_files()
{
if [ "$1" == "" ]; then
return
fi
lsof -n -p $PID 2>/dev/null | wc -l | trim
}
disk_usage()
{
if [ "$1" == "" ] || [ ! -d "$1" ]; then
echo N/A
return
fi
du -sh $1 | sed 's/^ *//;s/\t.*$//'
}
list_tcp()
{
if [ "$1" == "" ]; then
return
fi
for i in $(lsof -nP -iTCP -sTCP:LISTEN -a -p $1 2>/dev/null | sed '1d' |
awk '{ print $5 "_" $9 }' | sort -t ':' -k 2); do
echo -n "$i "
done
echo
}
list_udp()
{
if [ "$1" == "" ]; then
return
fi
for i in $(lsof -nP -iUDP -a -p $1 2>/dev/null | sed '1d' | awk '{ print $5 "_" $9 }' | sort -u); do
echo -n "$i "
done
echo
}
status_head()
{
if [ -t 1 ]; then
if [ "$3" == "Running" ]; then
local FG_COLOR=2
elif [ "$3" == "Stopped" ]; then
local FG_COLOR=1
else
local FG_COLOR=8
fi
printf "$(tput smul)%-16s%-20s$(tput setaf $FG_COLOR)$(tput bold)%s$(tput sgr0)\n" $1 $2 $3
else
printf "%-16s%-20s%s\n" $1 $2 $3
fi
}
status_info()
{
if [ -t 1 ]; then
printf "$(tput bold)%-16s$(tput sgr0)" "$1:"
if [ "$1" == "BPoS State" ]; then
if [ "$2" == "Active" ]; then
echo "$(tput setaf 2)$(tput bold)$2$(tput sgr0)"
elif [ "$2" == "Canceled" ] || \
[ "$2" == "Illegal" ] || \
[ "$2" == "Inactive" ] || \
[ "$2" == "Pending" ] || \
[ "$2" == "Returned" ]; then
echo "$(tput setaf 1)$(tput bold)$2$(tput sgr0)"
else
echo "$2"
fi
elif [ "$1" == "CRC State" ]; then
if [ "$2" == "Elected" ]; then
echo "$(tput setaf 2)$(tput bold)$2$(tput sgr0)"
elif [ "$2" == "Illegal" ] || \
[ "$2" == "Impeached" ] || \
[ "$2" == "Inactive" ] || \
[ "$2" == "Returned" ] || \
[ "$2" == "Terminated" ]; then
echo "$(tput setaf 1)$(tput bold)$2$(tput sgr0)"
else
echo "$2"
fi
else
echo "$2"
fi
else
printf "%-16s%s\n" "$1:" "$2"
fi
}
gen_pass()
{
KEYSTORE_PASS=
local KEYSTORE_PASS_VERIFY=
echo "Please input a password (ENTER to use a random one)"
while true; do
read -s -p '? Password: ' KEYSTORE_PASS
echo
if [ "$KEYSTORE_PASS" == "" ]; then
echo "Generating random password..."
KEYSTORE_PASS=$(openssl rand -base64 100 | head -c 32)
break
elif [[ "${#KEYSTORE_PASS}" -lt 16 ]] || \
[[ ! "$KEYSTORE_PASS" =~ [a-z] ]] || \
[[ ! "$KEYSTORE_PASS" =~ [A-Z] ]] || \
[[ ! "$KEYSTORE_PASS" =~ [0-9] ]] || \
[[ ! "$KEYSTORE_PASS" =~ [^[:alnum:]] ]]; then
echo_error "the password does not meet the password policy:"
echo " - Minimum password length: 16"
echo " - Require at least one uppercase letter (A-Z)"
echo " - Require at least one lowercase letter (a-z)"
echo " - Require at least one digit (0-9)"
echo " - Require at least one non-alphanumeric character"
continue
else
read -s -p '? Password (again): ' KEYSTORE_PASS_VERIFY
echo
if [ "$KEYSTORE_PASS" == "$KEYSTORE_PASS_VERIFY" ]; then
break
else
echo_error "password mismatch"
continue
fi
fi
done
}
compress_log()
{
if [ "$1" == "" ]; then
return
fi
ls $1 1>/dev/null 2>/dev/null
if [ "$?" != "0" ]; then
return
fi
if [ -d "$1" ]; then
local LOG_DIR="$1"
local LOG_PAT=\*.log
else
local LOG_DIR=$(dirname "$1")
local LOG_PAT=$(basename "$1")
fi
if [ "$IS_DEBUG" ]; then
echo "LOG_DIR: $LOG_DIR"
echo "LOG_PAT: $LOG_PAT"
fi
if [ -d $LOG_DIR ]; then
echo "Compressing log files in $LOG_DIR..."
cd $LOG_DIR
for i in $(ls -1 $LOG_PAT 2>/dev/null | sort -r | sed 1d); do
gzip -v $i
done
fi
}
remove_log()
{
if [ "$1" == "" ]; then
return
fi
ls $1 1>/dev/null 2>/dev/null
if [ "$?" != "0" ]; then
return
fi
if [ -d "$1" ]; then
local LOG_DIR="$1"
local LOG_PAT=\*.log
else
local LOG_DIR=$(dirname "$1")
local LOG_PAT=$(basename "$1")
fi
if [ "$IS_DEBUG" ]; then
echo "LOG_DIR: $LOG_DIR"
echo "LOG_PAT: $LOG_PAT"
fi
if [ -d $LOG_DIR ]; then
echo "Removing log files in $LOG_DIR..."
cd $LOG_DIR
for i in $(ls -1 $LOG_PAT 2>/dev/null | sort -r | sed 1d); do
rm -v $i
done
for i in $(ls -1 $LOG_PAT.gz 2>/dev/null); do
rm -v $i
done
fi
}
nodejs_setenv()
{
local NODEJS_VER=v23.10.0
local OS_ARCH=$(uname -sm)
if [ "$OS_ARCH" == "Linux x86_64" ]; then
local NODEJS_PLATFORM=linux-x64
elif [ "$OS_ARCH" == "Linux aarch64" ]; then
local NODEJS_PLATFORM=linux-arm64
else
echo_error "do not support $OS_ARCH"
return
fi
local NODEJS_NAME=node-$NODEJS_VER-$NODEJS_PLATFORM
local NODEJS_TGZ=$NODEJS_NAME.tar.xz
local NODEJS_URL=https://nodejs.org/download/release/$NODEJS_VER/$NODEJS_TGZ
if [ $IS_DEBUG ]; then
echo "NODEJS_VER: $NODEJS_VER"
echo "NODEJS_NAME: $NODEJS_NAME"
echo "NODEJS_TGZ: $NODEJS_TGZ"
echo "NODEJS_URL: $NODEJS_URL"
fi
if [ ! -d $SCRIPT_PATH/extern/$NODEJS_NAME ]; then
mkdir -p $SCRIPT_PATH/extern
cd $SCRIPT_PATH/extern
echo "Downloading $NODEJS_URL..."
curl -O -# $NODEJS_URL
tar xf $NODEJS_TGZ
fi
PATH=$SCRIPT_PATH/extern/$NODEJS_NAME/bin:$PATH
echo "nodejs: $(which node)"
}
get_elastos_ver_latest()
{
# URL_PREFIX
if [ "$1" == "" ]; then
return
fi
curl -s --connect-timeout 10 --max-time 30 "$1/?F=1" | grep '\[DIR\]' \
| sed -e 's/.*href="//' -e 's/".*//' -e 's/.*-//' -e 's/\/$//' \
| sort -Vr | head -n 1
}
#
# common chain functions
#
chain_prepare_stage()
{
local CHAIN_NAME=$1
if [ "$CHAIN_NAME" != "ela" ] && \
[ "$CHAIN_NAME" != "esc" ] && \
[ "$CHAIN_NAME" != "esc-oracle" ] && \
[ "$CHAIN_NAME" != "eid" ] && \
[ "$CHAIN_NAME" != "eid-oracle" ] && \
[ "$CHAIN_NAME" != "eco" ] && \
[ "$CHAIN_NAME" != "eco-oracle" ] && \
[ "$CHAIN_NAME" != "pgp" ] && \
[ "$CHAIN_NAME" != "pgp-oracle" ] && \
[ "$CHAIN_NAME" != "pg" ] && \
[ "$CHAIN_NAME" != "pg-oracle" ] && \
[ "$CHAIN_NAME" != "arbiter" ]; then
echo_error "do not support chain: $1"
return 1
fi
if [ "$2" == "" ]; then
return 1
fi
local OS_ARCH=$(uname -sm)
if [ "$CHAIN_NAME" == "ela" ] || \
[ "$CHAIN_NAME" == "esc" ] || \
[ "$CHAIN_NAME" == "eid" ] || \
[ "$CHAIN_NAME" == "eco" ] || \
[ "$CHAIN_NAME" == "pgp" ] || \
[ "$CHAIN_NAME" == "pg" ] || \
[ "$CHAIN_NAME" == "arbiter" ]; then
if [ "$OS_ARCH" == "Linux aarch64" ]; then
local RELEASE_PLATFORM=linux-arm64
elif [ "$OS_ARCH" == "Linux x86_64" ]; then
local RELEASE_PLATFORM=linux-x86_64
else
local RELEASE_PLATFORM=nosupport
fi
elif [ "$CHAIN_NAME" == "esc-oracle" ] || \
[ "$CHAIN_NAME" == "eco-oracle" ] || \
[ "$CHAIN_NAME" == "pgp-oracle" ] || \
[ "$CHAIN_NAME" == "pg-oracle" ] || \
[ "$CHAIN_NAME" == "eid-oracle" ]; then
local RELEASE_PLATFORM=
else
local RELEASE_PLATFORM=nosupport
fi
local PATH_STAGE=$SCRIPT_PATH/.node-upload/$CHAIN_NAME
echo "Finding the latest $CHAIN_NAME release..."
if [ "$CHAIN_TYPE" == "mainnet" ]; then
local URL_PREFIX=https://download.elastos.io/elastos-$CHAIN_NAME
local VER_LATEST=$(get_elastos_ver_latest $URL_PREFIX)
else
local URL_PREFIX_BETA=https://download-beta.elastos.io/elastos-$CHAIN_NAME
local VER_LATEST_BETA=$(get_elastos_ver_latest $URL_PREFIX_BETA)
local URL_PREFIX=https://download.elastos.io/elastos-$CHAIN_NAME
local VER_LATEST=$(get_elastos_ver_latest $URL_PREFIX)
local VER_LATEST_HIGHER=$(echo -e "$VER_LATEST_BETA\n$VER_LATEST" |
sort -Vr | head -n 1)
# If beta channel has a higher release
if [ "$VER_LATEST_HIGHER" == "$VER_LATEST_BETA" ]; then