-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_chain.sh
1871 lines (1698 loc) · 66 KB
/
build_chain.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
set -e
# default value
ca_path= #CA key
gmca_path= #gm CA key
node_num=1
ip_file=
ip_param=
use_ip_param=
agency_array=
group_array=
ports_array=
ip_array=
output_dir=nodes
port_start=(30300 20200 8545)
state_type=storage
storage_type=rocksdb
supported_storage=(rocksdb mysql external scalable)
conf_path="conf"
bin_path=
make_tar=
binary_log="false"
sm_crypto_channel="false"
log_level="info"
logfile=${PWD}/build.log
listen_ip="127.0.0.1"
default_listen_ip="0.0.0.0"
bcos_bin_name=fisco-bcos
guomi_mode=
docker_mode=
gm_conf_path="gmconf/"
current_dir=$(pwd)
consensus_type="pbft"
supported_consensus=(pbft raft rpbft)
TASSL_CMD="${HOME}"/.fisco/tassl
auto_flush="true"
enable_statistic="false"
enable_free_storage="false"
deploy_mode=
root_crt=
gmroot_crt=
copy_cert=
no_agency=
days=36500 # 100 years
# trans timestamp from seconds to milliseconds
timestamp=$(($(date '+%s')*1000))
chain_id=1
compatibility_version=""
default_version="2.7.2"
macOS=""
x86_64_arch="true"
download_timeout=240
cdn_link_header="https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS"
use_ipv6=
help() {
cat << EOF
Usage:
-l <IP list> [Required] "ip1:nodeNum1,ip2:nodeNum2" e.g:"192.168.0.1:2,192.168.0.2:3"
-f <IP list file> [Optional] split by line, every line should be "ip:nodeNum agencyName groupList p2p_port,channel_port,jsonrpc_port". eg "127.0.0.1:4 agency1 1,2 30300,20200,8545"
-v <FISCO-BCOS binary version> Default is the latest v${default_version}
-e <FISCO-BCOS binary path> Default download fisco-bcos from GitHub. If set -e, use the binary at the specified location
-o <Output Dir> Default ./nodes/
-p <Start Port> Default 30300,20200,8545 means p2p_port start from 30300, channel_port from 20200, jsonrpc_port from 8545
-q <List FISCO-BCOS releases> List FISCO-BCOS released versions
-i <Host ip> Default 127.0.0.1. If set -i, listen 0.0.0.0
-s <DB type> Default rocksdb. Options can be rocksdb / mysql / scalable, rocksdb is recommended
-d <docker mode> Default off. If set -d, build with docker
-c <Consensus Algorithm> Default PBFT. Options can be pbft / raft /rpbft, pbft is recommended
-C <Chain id> Default 1. Can set uint.
-g <Generate guomi nodes> Default no
-z <Generate tar packet> Default no
-t <Cert config file> Default auto generate
-6 <Use ipv6> Default no. If set -6, treat IP as IPv6
-k <The path of ca root> Default auto generate, the ca.crt and ca.key must in the path, if use intermediate the root.crt must in the path
-K <The path of sm crypto ca root> Default auto generate, the gmca.crt and gmca.key must in the path, if use intermediate the gmroot.crt must in the path
-D <Use Deployment mode> Default false, If set -D, use deploy mode directory struct and make tar
-G <channel use sm crypto ssl> Default false, only works for guomi mode
-X <Certificate expiration time> Default 36500 days
-T <Enable debug log> Default off. If set -T, enable debug log
-S <Enable statistics> Default off. If set -S, enable statistics
-F <Disable log auto flush> Default on. If set -F, disable log auto flush
-E <Enable free_storage_evm> Default off. If set -E, enable free_storage_evm
-h Help
e.g
$0 -l "127.0.0.1:4"
EOF
exit 0
}
LOG_WARN()
{
local content=${1}
echo -e "\033[31m[WARN] ${content}\033[0m"
}
LOG_INFO()
{
local content=${1}
echo -e "\033[32m[INFO] ${content}\033[0m"
}
get_value()
{
local var_name="${1}"
var_name="${var_name//./}"
var_name="var_${var_name//-/}"
local res=$(eval echo '$'"${var_name}")
echo ${res}
}
set_value()
{
local var_name="${1}"
var_name="${var_name//./}"
var_name="var_${var_name//-/}"
local var_value=${2}
eval "${var_name}=${var_value}"
}
exit_with_clean()
{
local content=${1}
echo -e "\033[31m[ERROR] ${content}\033[0m"
if [ -d "${output_dir}" ];then
rm -rf ${output_dir}
fi
exit 1
}
parse_params()
{
while getopts "f:l:o:p:e:t:v:s:C:c:k:K:X:izhgGTNFSdEDZ6q" option;do
case $option in
f) ip_file=$OPTARG
use_ip_param="false"
;;
l) ip_param=$OPTARG
use_ip_param="true"
;;
o) output_dir=$OPTARG;;
i) listen_ip="0.0.0.0" && LOG_WARN "jsonrpc_listen_ip linstens 0.0.0.0 is unsafe.";;
q) LOG_INFO "Show the history releases of FISCO-BCOS " && curl -sS https://gitee.com/api/v5/repos/FISCO-BCOS/FISCO-BCOS/tags | grep -oe "\"name\":\"v[2-9]*\.[0-9]*\.[0-9]*\"" | cut -d \" -f 4 | sort -V && exit 0;;
v) compatibility_version="${OPTARG//[vV]/}";;
p) port_start=(${OPTARG//,/ })
if [ ${#port_start[@]} -ne 3 ];then LOG_WARN "start port error. e.g: 30300,20200,8545" && exit 1;fi
;;
e) bin_path=$OPTARG;;
k) ca_path="$OPTARG" && ca_key="$OPTARG/ca.key" && ca_crt="$OPTARG/ca.crt"
if [[ ! -f "${ca_key}" ]];then
LOG_WARN "${ca_key} not exist."
exit 1;
fi
if [[ ! -f "${ca_crt}" ]];then
LOG_WARN "${ca_crt} not exist."
exit 1;
fi
;;
K) gmca_path="$OPTARG" && gmca_key="$OPTARG/gmca.key" && gmca_crt="$OPTARG/gmca.crt"
if [[ ! -f "${gmca_key}" ]];then
LOG_WARN "${gmca_key} not exist."
exit 1;
fi
if [[ ! -f "${gmca_crt}" ]];then
LOG_WARN "${gmca_crt} not exist."
exit 1;
fi
;;
s) storage_type=$OPTARG
if ! echo "${supported_storage[*]}" | grep -i "${storage_type}" &>/dev/null; then
LOG_WARN "${storage_type} is not supported. Please set one of ${supported_storage[*]}"
exit 1;
fi
;;
t) CertConfig=$OPTARG;;
c) consensus_type=$OPTARG
if ! echo "${supported_consensus[*]}" | grep -i "${consensus_type}" &>/dev/null; then
LOG_WARN "${consensus_type} is not supported. Please set one of ${supported_consensus[*]}"
exit 1;
fi
;;
C) chain_id=$OPTARG
if [ -z $(grep '^[[:digit:]]*$' <<< "${chain_id}") ];then
LOG_WARN "${chain_id} is not a positive integer."
exit 1;
fi
;;
X) days="$OPTARG";;
G) sm_crypto_channel="true";;
T) log_level="debug";;
F) auto_flush="false";;
N) no_agency="true";;
S) enable_statistic="true";;
E) enable_free_storage="true";;
D) deploy_mode="true" && make_tar="true";;
Z) copy_cert="true";;
z) make_tar="true";;
g) guomi_mode="true";;
6) use_ipv6="true" && default_listen_ip="::";;
d) docker_mode="true"
if [ "$(uname)" == "Darwin" ];then LOG_WARN "Docker desktop of macOS can't support docker mode of FISCO BCOS!" && exit 1;fi
;;
h) help;;
esac
done
if [ "${storage_type}" == "scalable" ]; then
echo "use scalable storage, so turn on binary log"
binary_log="true"
fi
# sm_crypto_channel only works in guomi mode
if [[ -n "${guomi_mode}" && "${sm_crypto_channel}" == "true" ]];then
sm_crypto_channel="true"
else
sm_crypto_channel="false"
fi
}
print_result()
{
echo "=============================================================="
[ -z "${docker_mode}" ] && [ -f "${bin_path}" ] && LOG_INFO "FISCO-BCOS Path : $bin_path"
[ -n "${docker_mode}" ] && LOG_INFO "Docker tag : latest"
[ -n "${ip_file}" ] && LOG_INFO "IP List File : $ip_file"
LOG_INFO "Start Port : ${port_start[*]}"
LOG_INFO "Server IP : ${ip_array[*]}"
LOG_INFO "Output Dir : ${output_dir}"
LOG_INFO "CA Path : $ca_path"
[ -n "${guomi_mode}" ] && LOG_INFO "Guomi CA Path : $gmca_path"
[ -n "${guomi_mode}" ] && LOG_INFO "Guomi mode : $guomi_mode"
echo "=============================================================="
LOG_INFO "Execute the download_console.sh script in directory named by IP to get FISCO-BCOS console."
echo "e.g. bash ${output_dir}/${ip_array[0]%:*}/download_console.sh -f"
echo "=============================================================="
LOG_INFO "All completed. Files in ${output_dir}"
}
check_env() {
if [ "$(uname)" == "Darwin" ];then
export PATH="/usr/local/opt/openssl/bin:$PATH"
macOS="macOS"
fi
[ ! -z "$(openssl version | grep 1.0.2)" ] || [ ! -z "$(openssl version | grep 1.1)" ] || {
echo "please install openssl!"
#echo "download openssl from https://www.openssl.org."
echo "use \"openssl version\" command to check."
exit 1
}
if [ "$(uname -m)" != "x86_64" ];then
x86_64_arch="false"
fi
}
check_and_install_tassl(){
if [ -n "${guomi_mode}" ]; then
if [ ! -f "${TASSL_CMD}" ];then
local tassl_link_perfix="${cdn_link_header}/FISCO-BCOS/tools/tassl-1.0.2"
LOG_INFO "Downloading tassl binary from ${tassl_link_perfix}..."
if [[ -n "${macOS}" ]];then
curl -#LO "${tassl_link_perfix}/tassl_mac.tar.gz"
mv tassl_mac.tar.gz tassl.tar.gz
else
if [[ "$(uname -p)" == "aarch64" ]];then
curl -#LO "${tassl_link_perfix}/tassl-aarch64.tar.gz"
mv tassl-aarch64.tar.gz tassl.tar.gz
elif [[ "$(uname -p)" == "x86_64" ]];then
curl -#LO "${tassl_link_perfix}/tassl.tar.gz"
else
LOG_ERROR "Unsupported platform"
exit 1
fi
fi
tar zxvf tassl.tar.gz && rm tassl.tar.gz
chmod u+x tassl
mkdir -p "${HOME}"/.fisco
mv tassl "${HOME}"/.fisco/tassl
fi
fi
}
check_name() {
local name="$1"
local value="$2"
[[ "$value" =~ ^[a-zA-Z0-9._-]+$ ]] || {
exit_with_clean "$name name [$value] invalid, it should match regex: ^[a-zA-Z0-9._-]+\$"
}
}
file_must_exists() {
if [ ! -f "$1" ]; then
exit_with_clean "$1 file does not exist, please check!"
fi
}
dir_must_exists() {
if [ ! -d "$1" ]; then
exit_with_clean "$1 DIR does not exist, please check!"
fi
}
dir_must_not_exists() {
if [ -e "$1" ]; then
LOG_WARN "$1 DIR exists, please clean old DIR!"
exit 1
fi
}
gen_chain_cert() {
local path="${1}"
name=$(basename "$path")
echo "$path --- $name"
dir_must_not_exists "$path"
check_name chain "$name"
chaindir=$path
mkdir -p $chaindir
# openssl genrsa -out "$chaindir/ca.key" 2048
openssl ecparam -out "$chaindir/secp256k1.param" -name secp256k1 2> /dev/null
openssl genpkey -paramfile "$chaindir/secp256k1.param" -out "$chaindir/ca.key" 2> /dev/null
openssl req -new -x509 -days "${days}" -subj "/CN=$name/O=fisco-bcos/OU=chain" -key "$chaindir/ca.key" -out "$chaindir/ca.crt"
rm -f "$chaindir/secp256k1.param"
}
gen_agency_cert() {
local chain="${1}"
local agencypath="${2}"
name=$(basename "$agencypath")
dir_must_exists "$chain"
file_must_exists "$chain/ca.key"
check_name agency "$name"
agencydir=$agencypath
dir_must_not_exists "$agencydir"
mkdir -p $agencydir
# openssl genrsa -out "$agencydir/agency.key" 2048 2> /dev/null
openssl ecparam -out "$agencydir/secp256k1.param" -name secp256k1 2> /dev/null
openssl genpkey -paramfile "$agencydir/secp256k1.param" -out "$agencydir/agency.key" 2> /dev/null
openssl req -new -sha256 -subj "/CN=$name/O=fisco-bcos/OU=agency" -key "$agencydir/agency.key" -out "$agencydir/agency.csr" 2> /dev/null
openssl x509 -req -days 3650 -sha256 -CA "$chain/ca.crt" -CAkey "$chain/ca.key" -CAcreateserial\
-in "$agencydir/agency.csr" -out "$agencydir/agency.crt" -extensions v4_req -extfile "$chain/cert.cnf" 2> /dev/null
# cat "$chain/ca.crt" >> "$agencydir/agency.crt"
cp $chain/ca.crt $chain/cert.cnf $agencydir/
rm -f "$agencydir/agency.csr" "$agencydir/secp256k1.param"
echo "build $name cert successful!"
}
gen_cert_secp256k1() {
agpath="$1"
certpath="$2"
name="$3"
type="$4"
openssl ecparam -out "$certpath/secp256k1.param" -name secp256k1 2> /dev/null
openssl genpkey -paramfile "$certpath/secp256k1.param" -out "$certpath/${type}.key" 2> /dev/null
openssl pkey -in "$certpath/${type}.key" -pubout -out "$certpath/${type}.pubkey" 2> /dev/null
openssl req -new -sha256 -subj "/CN=${name}/O=fisco-bcos/OU=${type}" -key "$certpath/${type}.key" -out "$certpath/${type}.csr" 2> /dev/null
if [ -n "${no_agency}" ];then
echo "not use $(basename $agpath) to sign $(basename $certpath) ${type}" >>"${logfile}"
openssl x509 -req -days "${days}" -sha256 -in "$certpath/${type}.csr" -CAkey "$agpath/../ca.key" -CA "$agpath/../ca.crt" \
-force_pubkey "$certpath/${type}.pubkey" -out "$certpath/${type}.crt" -CAcreateserial -extensions v3_req -extfile "$agpath/cert.cnf" 2> /dev/null
else
openssl x509 -req -days "${days}" -sha256 -in "$certpath/${type}.csr" -CAkey "$agpath/agency.key" -CA "$agpath/agency.crt" \
-force_pubkey "$certpath/${type}.pubkey" -out "$certpath/${type}.crt" -CAcreateserial -extensions v3_req -extfile "$agpath/cert.cnf" 2> /dev/null
# openssl ec -in $certpath/${type}.key -outform DER | tail -c +8 | head -c 32 | xxd -p -c 32 | cat >$certpath/${type}.private
cat "${agpath}/agency.crt" >> "$certpath/${type}.crt"
fi
cat "${agpath}/../ca.crt" >> "$certpath/${type}.crt"
if [[ -n "${root_crt}" ]];then
echo "Use user specified root cert as ca.crt, $certpath" >> "${logfile}"
cp "${root_crt}" "$certpath/ca.crt"
else
cp "${agpath}/../ca.crt" "$certpath/"
fi
rm -f "$certpath/${type}.csr" "$certpath/${type}.pubkey" "$certpath/secp256k1.param"
}
gen_cert() {
if [ "" == "$(openssl ecparam -list_curves 2>&1 | grep secp256k1)" ]; then
exit_with_clean "openssl don't support secp256k1, please upgrade openssl!"
fi
agpath="${1}"
agency=$(basename "$agpath")
ndpath="${2}"
local cert_name="${3}"
node=$(basename "$ndpath")
dir_must_exists "$agpath"
file_must_exists "$agpath/agency.key"
check_name agency "$agency"
dir_must_not_exists "$ndpath"
check_name node "$node"
mkdir -p $ndpath
gen_cert_secp256k1 "$agpath" "$ndpath" "$node" "${cert_name}"
#nodeid is pubkey
openssl ec -text -noout -in "$ndpath/${cert_name}.key" 2> /dev/null | sed -n '7,11p' | tr -d ": \n" | awk '{print substr($0,3);}' | cat >"$ndpath"/node.nodeid
# openssl x509 -serial -noout -in $ndpath/node.crt | awk -F= '{print $2}' | cat >$ndpath/node.serial
cd "$ndpath"
if [ "${cert_name}" == "node" ];then
mkdir -p "${conf_path}/"
mv ./*.* "${conf_path}/"
cd "${output_dir}"
fi
}
generate_gmsm2_param()
{
local output=$1
cat << EOF > "${output}"
-----BEGIN EC PARAMETERS-----
BggqgRzPVQGCLQ==
-----END EC PARAMETERS-----
EOF
}
gen_chain_cert_gm() {
local path="${1}"
name=$(basename "$path")
echo "$path --- $name"
dir_must_not_exists "$path"
check_name chain "$name"
chaindir=$path
mkdir -p $chaindir
$TASSL_CMD genpkey -paramfile gmsm2.param -out "$chaindir/gmca.key" 2> /dev/null
$TASSL_CMD req -config gmcert.cnf -x509 -days "${days}" -subj "/CN=${name}/O=fisco-bcos/OU=chain" -key "$chaindir/gmca.key" -extensions v3_ca -out "$chaindir/gmca.crt" 2> /dev/null
}
gen_agency_cert_gm() {
local chain="${1}"
local agencypath="${2}"
name=$(basename "$agencypath")
dir_must_exists "$chain"
file_must_exists "$chain/gmca.key"
check_name agency "$name"
agencydir=$agencypath
dir_must_not_exists "$agencydir"
mkdir -p $agencydir
$TASSL_CMD genpkey -paramfile "$chain/gmsm2.param" -out "$agencydir/gmagency.key" 2> /dev/null
$TASSL_CMD req -new -subj "/CN=${name}_son/O=fisco-bcos/OU=agency" -key "$agencydir/gmagency.key" -config "$chain/gmcert.cnf" -out "$agencydir/gmagency.csr" 2> /dev/null
$TASSL_CMD x509 -sm3 -req -CA "$chain/gmca.crt" -CAkey "$chain/gmca.key" -days 3650 -CAcreateserial -in "$agencydir/gmagency.csr" -out "$agencydir/gmagency.crt" -extfile "$chain/gmcert.cnf" -extensions v3_agency_root 2> /dev/null
# cat "$chain/gmca.crt" >> "$agencydir/gmagency.crt"
cp "$chain/gmca.crt" "$chain/gmcert.cnf" "$chain/gmsm2.param" "$agencydir/"
rm -f "$agencydir/gmagency.csr"
}
gen_node_cert_with_extensions_gm() {
capath="$1"
certpath="$2"
name="$3"
type="$4"
extensions="$5"
$TASSL_CMD genpkey -paramfile "$capath/gmsm2.param" -out "$certpath/gm${type}.key" 2> /dev/null
$TASSL_CMD req -new -subj "/CN=$name/O=fisco-bcos/OU=${type}" -key "$certpath/gm${type}.key" -config "$capath/gmcert.cnf" -out "$certpath/gm${type}.csr" 2> /dev/null
if [ -n "${no_agency}" ];then
echo "not use $(basename $capath) to sign $(basename $certpath) ${type}" >>"${logfile}"
$TASSL_CMD x509 -sm3 -req -CA "$capath/../gmca.crt" -CAkey "$capath/../gmca.key" -days "${days}" -CAcreateserial -in "$certpath/gm${type}.csr" -out "$certpath/gm${type}.crt" -extfile "$capath/gmcert.cnf" -extensions "$extensions" 2> /dev/null
else
$TASSL_CMD x509 -sm3 -req -CA "$capath/gmagency.crt" -CAkey "$capath/gmagency.key" -days "${days}" -CAcreateserial -in "$certpath/gm${type}.csr" -out "$certpath/gm${type}.crt" -extfile "$capath/gmcert.cnf" -extensions "$extensions" 2> /dev/null
fi
rm -f $certpath/gm${type}.csr
}
gen_node_cert_gm() {
agpath="${1}"
agency=$(basename "$agpath")
ndpath="${2}"
node=$(basename "$ndpath")
dir_must_exists "$agpath"
file_must_exists "$agpath/gmagency.key"
check_name agency "$agency"
mkdir -p $ndpath
dir_must_exists "$ndpath"
check_name node "$node"
mkdir -p $ndpath
gen_node_cert_with_extensions_gm "$agpath" "$ndpath" "$node" node v3_req
if [ -z "${no_agency}" ];then cat "${agpath}/gmagency.crt" >> "$ndpath/gmnode.crt";fi
cat "${agpath}/../gmca.crt" >> "$ndpath/gmnode.crt"
gen_node_cert_with_extensions_gm "$agpath" "$ndpath" "$node" ennode v3enc_req
#nodeid is pubkey
$TASSL_CMD ec -in "$ndpath/gmnode.key" -text -noout 2> /dev/null | sed -n '7,11p' | sed 's/://g' | tr "\n" " " | sed 's/ //g' | awk '{print substr($0,3);}' | cat > "$ndpath/gmnode.nodeid"
#serial
if [ "" != "$($TASSL_CMD version 2> /dev/null | grep 1.0.2)" ];
then
$TASSL_CMD x509 -text -in "$ndpath/gmnode.crt" 2> /dev/null | sed -n '5p' | sed 's/://g' | tr "\n" " " | sed 's/ //g' | sed 's/[a-z]/\u&/g' | cat > "$ndpath/gmnode.serial"
else
$TASSL_CMD x509 -text -in "$ndpath/gmnode.crt" 2> /dev/null | sed -n '4p' | sed 's/ //g' | sed 's/.*(0x//g' | sed 's/)//g' |sed 's/[a-z]/\u&/g' | cat > "$ndpath/gmnode.serial"
fi
if [[ -n "${gmroot_crt}" ]];then
echo "Use user specified gmroot cert as gmca.crt, $ndpath" >>"${logfile}"
cp "${gmroot_crt}" "$certpath/gmca.crt"
else
cp "$agpath/../gmca.crt" "$ndpath"
fi
cd "$ndpath"
mkdir -p "${gm_conf_path}/"
mv ./*.* "${gm_conf_path}/"
cd "${output_dir}"
}
generate_config_ini()
{
local output=${1}
local ip=${2}
local offset=0
offset=$(get_value "${ip//[\.:]/_}_port_offset")
local node_groups=(${3//,/ })
local port_array=
read -r -a port_array <<< "${port_start[*]}"
local node_index="${5}"
if [[ -n "${4}" ]];then
read -r -a port_array <<< "${4//,/ }"
[ "${node_index}" == "0" ] && { offset=0 && set_value "${ip//[\.:]/_}_port_offset" 0; }
fi
sm_crypto="false"
local prefix=""
if [ -n "${guomi_mode}" ]; then
sm_crypto="true"
prefix="gm"
fi
cat << EOF > "${output}"
[rpc]
channel_listen_ip=${default_listen_ip}
channel_listen_port=$(( offset + port_array[1] ))
jsonrpc_listen_ip=${listen_ip}
jsonrpc_listen_port=$(( offset + port_array[2] ))
[p2p]
listen_ip=${default_listen_ip}
listen_port=$(( offset + port_array[0] ))
; nodes to connect
$ip_list
[certificate_blacklist]
; crl.0 should be nodeid, nodeid's length is 128
;crl.0=
[certificate_whitelist]
; cal.0 should be nodeid, nodeid's length is 128
;cal.0=
[group]
group_data_path=data/
group_config_path=${conf_path}/
[network_security]
; directory the certificates located in
data_path=${conf_path}/
; the node private key file
key=${prefix}node.key
; the node certificate file
cert=${prefix}node.crt
; the ca certificate file
ca_cert=${prefix}ca.crt
[storage_security]
enable=false
key_manager_ip=
key_manager_port=
cipher_data_key=
[chain]
id=${chain_id}
; use SM crypto or not, should nerver be changed
sm_crypto=${sm_crypto}
sm_crypto_channel=${sm_crypto_channel}
[compatibility]
; supported_version should nerver be changed
supported_version=${compatibility_version}
[log]
enable=true
log_path=./log
; enable/disable the statistics function
enable_statistic=${enable_statistic}
; network statistics interval, unit is second, default is 60s
stat_flush_interval=60
; info debug trace
level=${log_level}
; MB
max_log_file_size=200
flush=${auto_flush}
[flow_control]
; restrict QPS of the node
;limit_req=1000
; restrict the outgoing bandwidth of the node
; Mb, can be a decimal
; when the outgoing bandwidth exceeds the limit, the block synchronization operation will not proceed
;outgoing_bandwidth_limit=2
EOF
printf " [%d] p2p:%-5d channel:%-5d jsonrpc:%-5d\n" "${node_index}" $(( offset + port_array[0] )) $(( offset + port_array[1] )) $(( offset + port_array[2] )) >>"${logfile}"
}
generate_group_genesis()
{
local output=$1
local index=$2
local node_list=$3
local sealer_size=$4
cat << EOF > "${output}"
[consensus]
; consensus algorithm now support PBFT(consensus_type=pbft), Raft(consensus_type=raft)
; rpbft(consensus_type=rpbft)
consensus_type=${consensus_type}
; the max number of transactions of a block
max_trans_num=1000
; in seconds, block consensus timeout, at least 3s
consensus_timeout=3
; rpbft related configuration
; the working sealers num of each consensus epoch
epoch_sealer_num=${sealer_size}
; the number of generated blocks each epoch
epoch_block_num=1000
; the node id of consensusers
${node_list}
[state]
type=${state_type}
[tx]
; transaction gas limit
gas_limit=300000000
[group]
id=${index}
timestamp=${timestamp}
[evm]
enable_free_storage=${enable_free_storage}
EOF
}
function generate_group_ini()
{
local output="${1}"
cat << EOF > "${output}"
[consensus]
; the ttl for broadcasting pbft message
ttl=2
; min block generation time(ms)
min_block_generation_time=500
enable_dynamic_block_size=true
enable_ttl_optimization=true
enable_prepare_with_txsHash=true
; The following is the relevant configuration of rpbft
; set true to enable broadcast prepare request by tree
broadcast_prepare_by_tree=true
; percent of nodes that broadcast prepare status to, must be between 25 and 100
prepare_status_broadcast_percent=33
; max wait time before request missed transactions, ms, must be between 5ms and 1000ms
max_request_missedTxs_waitTime=100
; maximum wait time before requesting a prepare, ms, must be between 10ms and 1000ms
max_request_prepare_waitTime=100
[storage]
; storage db type, rocksdb / mysql / scalable, rocksdb is recommended
type=${storage_type}
; set true to turn on binary log
binary_log=${binary_log}
; scroll_threshold=scroll_threshold_multiple*1000, only for scalable
scroll_threshold_multiple=2
; set fasle to disable CachedStorage
cached_storage=true
; max cache memeory, MB
max_capacity=32
max_forward_block=10
; only for external, deprecated in v2.3.0
max_retry=60
topic=DB
; only for mysql
db_ip=127.0.0.1
db_port=3306
db_username=
db_passwd=
db_name=
[tx_pool]
limit=150000
; transaction pool memory size limit, MB
memory_limit=512
; number of threads responsible for transaction notification,
; default is 2, not recommended for more than 8
notify_worker_num=2
[sync]
; max memory size used for block sync, must >= 32MB
max_block_sync_memory_size=512
idle_wait_ms=200
; send block status by tree-topology, only supported when use pbft
sync_block_by_tree=true
; send transaction by tree-topology, only supported when use pbft
; recommend to use when deploy many consensus nodes
send_txs_by_tree=true
; must between 1000 to 3000
; only enabled when sync_by_tree is true
gossip_interval_ms=1000
gossip_peers_number=3
; max number of nodes that broadcast txs status to, recommended less than 5
txs_max_gossip_peers_num=5
[flow_control]
; restrict QPS of the group
;limit_req=1000
; restrict the outgoing bandwidth of the group
; Mb, can be a decimal
; when the outgoing bandwidth exceeds the limit, the block synchronization operation will not proceed
;outgoing_bandwidth_limit=2
[sdk_allowlist]
; When sdk_allowlist is empty, all SDKs can connect to this node
; when sdk_allowlist is not empty, only the SDK in the allowlist can connect to this node
; public_key.0 should be nodeid, nodeid's length is 128
;public_key.0=
EOF
}
generate_cert_conf()
{
local output=$1
cat << EOF > "${output}"
[ca]
default_ca=default_ca
[default_ca]
default_days = 365
default_md = sha256
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
[req_distinguished_name]
countryName = CN
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default =GuangDong
localityName = Locality Name (eg, city)
localityName_default = ShenZhen
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = fisco-bcos
commonName = Organizational commonName (eg, fisco-bcos)
commonName_default = fisco-bcos
commonName_max = 64
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
[ v4_req ]
basicConstraints = CA:TRUE
EOF
}
generate_script_template()
{
local filepath=$1
mkdir -p $(dirname $filepath)
cat << EOF > "${filepath}"
#!/bin/bash
SHELL_FOLDER=\$(cd \$(dirname \$0);pwd)
LOG_ERROR() {
content=\${1}
echo -e "\033[31m[ERROR] \${content}\033[0m"
}
LOG_INFO() {
content=\${1}
echo -e "\033[32m[INFO] \${content}\033[0m"
}
EOF
chmod +x ${filepath}
}
generate_cert_conf_gm()
{
local output=$1
cat << EOF > "${output}"
HOME = .
RANDFILE = $ENV::HOME/.rnd
oid_section = new_oids
[ new_oids ]
tsa_policy1 = 1.2.3.4.1
tsa_policy2 = 1.2.3.4.5.6
tsa_policy3 = 1.2.3.4.5.7
####################################################################
[ ca ]
default_ca = CA_default # The default ca section
####################################################################
[ CA_default ]
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
#unique_subject = no # Set to 'no' to allow creation of
# several ctificates with same subject.
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
# must be commented out to leave a V1 CRL
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/cakey.pem # The private key
RANDFILE = $dir/private/.rand # private random number file
x509_extensions = usr_cert # The extensions to add to the cert
name_opt = ca_default # Subject Name options
cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = default # use public key default MD
preserve = no # keep passed DN ordering
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
####################################################################
[ req ]
default_bits = 2048
default_md = sm3
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
x509_extensions = v3_ca # The extensions to add to the self signed cert
string_mask = utf8only
# req_extensions = v3_req # The extensions to add to a certificate request
[ req_distinguished_name ]
countryName = CN
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default =GuangDong
localityName = Locality Name (eg, city)
localityName_default = ShenZhen
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = fisco
commonName = Organizational commonName (eg, fisco)
commonName_default = fisco
commonName_max = 64
[ usr_cert ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature
[ v3enc_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = keyAgreement, keyEncipherment, dataEncipherment
[ v3_agency_root ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = CA:true
keyUsage = cRLSign, keyCertSign
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = CA:true
keyUsage = cRLSign, keyCertSign
EOF
}
generate_node_scripts()
{
local output=$1
local docker_tag="v${compatibility_version}"
generate_script_template "$output/start.sh"
local ps_cmd="\$(ps aux|grep \${fisco_bcos}|grep -v grep|awk '{print \$2}')"
local start_cmd="nohup \${fisco_bcos} -c config.ini >>nohup.out 2>&1 &"
local stop_cmd="kill \${node_pid}"
local pid="pid"
local log_cmd="tail -n20 nohup.out"
local check_success="\$(${log_cmd} | grep running)"
local fisco_bin_path="../${bcos_bin_name}"
if [ -n "${deploy_mode}" ];then fisco_bin_path="${bcos_bin_name}"; fi
if [ -n "${docker_mode}" ];then
ps_cmd="\$(docker ps |grep \${SHELL_FOLDER//\//} | grep -v grep|awk '{print \$1}')"
start_cmd="docker run -d --rm --name \${SHELL_FOLDER//\//} -v \${SHELL_FOLDER}:/data --network=host -w=/data fiscoorg/fiscobcos:${docker_tag} -c config.ini"
stop_cmd="docker kill \${node_pid} 2>/dev/null"
pid="container id"
log_cmd="tail -n20 \$(docker inspect --format='{{.LogPath}}' \${SHELL_FOLDER//\//})"
check_success="success"
fi
cat << EOF >> "$output/start.sh"
fisco_bcos=\${SHELL_FOLDER}/${fisco_bin_path}
cd \${SHELL_FOLDER}
node=\$(basename \${SHELL_FOLDER})
node_pid=${ps_cmd}
if [ -n "\${node_pid}" ];then
echo " \${node} is running, ${pid} is \$node_pid."
exit 0
else
${start_cmd}
sleep 1.5
fi
try_times=4
i=0
while [ \$i -lt \${try_times} ]
do
node_pid=${ps_cmd}
success_flag=${check_success}
if [[ -n "\${node_pid}" && -n "\${success_flag}" ]];then
echo -e "\033[32m \${node} start successfully\033[0m"
exit 0
fi
sleep 0.5
((i=i+1))
done
echo -e "\033[31m Exceed waiting time. Please try again to start \${node} \033[0m"
${log_cmd}
exit 1
EOF
generate_script_template "$output/stop.sh"
cat << EOF >> "$output/stop.sh"
fisco_bcos=\${SHELL_FOLDER}/${fisco_bin_path}
node=\$(basename \${SHELL_FOLDER})
node_pid=${ps_cmd}
try_times=20
i=0
if [ -z \${node_pid} ];then
echo " \${node} isn't running."
exit 0
fi
[ -n "\${node_pid}" ] && ${stop_cmd} > /dev/null
while [ \$i -lt \${try_times} ]
do
sleep 0.6
node_pid=${ps_cmd}
if [ -z \${node_pid} ];then
echo -e "\033[32m stop \${node} success.\033[0m"
exit 0
fi
((i=i+1))
done
echo " Exceed maximum number of retries. Please try again to stop \${node}"
exit 1
EOF
generate_script_template "$output/scripts/load_new_groups.sh"
cat << EOF >> "$output/scripts/load_new_groups.sh"
cd \${SHELL_FOLDER}/../
NODE_FOLDER=\$(pwd)
fisco_bcos=\${NODE_FOLDER}/${fisco_bin_path}
node=\$(basename \${NODE_FOLDER})
node_pid=${ps_cmd}
if [ -n "\${node_pid}" ];then
echo "\${node} is trying to load new groups. Check log for more information."
DATA_FOLDER=\${NODE_FOLDER}/data
for dir in \$(ls \${DATA_FOLDER})
do
if [[ -d "\${DATA_FOLDER}/\${dir}" ]] && [[ -n "\$(echo \${dir} | grep -E "^group\d+$")" ]]; then
STATUS_FILE=\${DATA_FOLDER}/\${dir}/.group_status
if [ ! -f "\${STATUS_FILE}" ]; then
echo "STOPPED" > \${STATUS_FILE}
fi