forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.sh
622 lines (564 loc) · 15.3 KB
/
start.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
#!/bin/bash
#############################################################################
#
# GNU LESSER GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) [2007] [TRON Foundation], Inc. <https://fsf.org/>
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
#
# This version of the GNU Lesser General Public License incorporates
# the terms and conditions of version 3 of the GNU General Public
# License, supplemented by the additional permissions listed below.
#
# You can find java-tron at https://github.com/tronprotocol/java-tron/
#
##############################################################################
# Build FullNode config
FULL_NODE_DIR="FullNode"
FULL_NODE_CONFIG_DIR="config"
# config file
FULL_NODE_CONFIG_MAIN_NET="main_net_config.conf"
FULL_NODE_CONFIG_TEST_NET="test_net_config.conf"
FULL_NODE_CONFIG_PRIVATE_NET="private_net_config.conf"
DEFAULT_FULL_NODE_CONFIG='config.conf'
JAR_NAME="FullNode.jar"
FULL_START_OPT=''
# Github
GITHUB_BRANCH='master'
GITHUB_CLONE_TYPE='HTTPS'
GITHUB_REPOSITORY=''
GITHUB_REPOSITORY_HTTPS_URL='https://github.com/tronprotocol/java-tron.git'
GITHUB_REPOSITORY_SSH_URL='[email protected]:tronprotocol/java-tron.git'
# Shell option
ALL_OPT_LENGTH=$#
# Start service option
MAX_STOP_TIME=60
# Modify this option to allow the minimum memory to be started, unit MB
ALLOW_MIN_MEMORY=8192
# JVM option
MAX_DIRECT_MEMORY=1g
JVM_MS=4g
JVM_MX=4g
IS_BACKUP_GC_LOG=true
SPECIFY_MEMORY=0
RUN=false
UPGRADE=false
# Rebuild manifest
REBUILD_MANIFEST=true
REBUILD_DIR="$PWD/output-directory/database"
REBUILD_MANIFEST_SIZE=0
REBUILD_BATCH_SIZE=80000
# Download and upgrade
DOWNLOAD=false
RELEASE_URL='https://github.com/tronprotocol/java-tron/releases'
QUICK_START=false
CLONE_BUILD=false
if [[ $GITHUB_CLONE_TYPE == 'HTTPS' ]]; then
GITHUB_REPOSITORY=$GITHUB_REPOSITORY_HTTPS_URL
else
GITHUB_REPOSITORY=$GITHUB_REPOSITORY_SSH_URL
fi
# Determine the Java command to use to start the JVM.
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
if $darwin ; then
javaHome="`dirname \"$javaExecutable\"`"
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
else
javaExecutable="`readlink -f \"$javaExecutable\"`"
fi
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly." >&2
echo " We cannot execute $JAVACMD" >&2
exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi
backupGCLog() {
local maxFile=5
local gcLogDir=logs/gc_logs/
if [ ! -d "$gcLogDir" ];then
mkdir -p 'logs/gc_logs'
fi
if [ -f 'gc.log' ]; then
echo '[info] backup gc.log'
local dateformat=`date "+%Y-%m-%d_%H-%M-%S"`
tar -czvf gc.log_$dateformat'.tar.gz' gc.log
mv gc.log_$dateformat'.tar.gz' $gcLogDir
rm -rf gc.log
# checking the number of backups
local currentDirCount=`ls -l $gcLogDir | grep "gc.log*" | wc -l`
if [ $currentDirCount -gt $maxFile ]; then
local oldFileSize=`expr $currentDirCount - $maxFile`
local oldGcLogFiles=(`ls -1 $gcLogDir |head -n $oldFileSize`)
fi
for fileName in ${oldGcLogFiles[@]}; do
rm -rf $gcLogDir$fileName
done
fi
}
getLatestReleaseVersion() {
full_node_version=`git ls-remote --tags $GITHUB_REPOSITORY |grep GreatVoyage- | awk -F '/' 'END{print $3}'`
if [[ -n $full_node_version ]]; then
echo $full_node_version
else
echo ''
fi
}
checkVersion() {
github_release_version=$(`echo getLatestReleaseVersion`)
if [[ -n $github_release_version ]]; then
echo "info: github latest version: $github_release_version"
echo $github_release_version
else
echo 'info: not getting the latest version'
exit
fi
}
upgrade() {
latest_version=$(`echo getLatestReleaseVersion`)
echo "info: latest version: $latest_version"
if [[ -n $latest_version ]]; then
old_jar="$PWD/$JAR_NAME"
if [[ -f $old_jar ]]; then
echo "info: backup $old_jar"
mv $PWD/$JAR_NAME $PWD/$JAR_NAME'_bak'
fi
download $RELEASE_URL/download/$latest_version/$JAR_NAME $JAR_NAME
if [[ $? == 0 ]]; then
echo "info: download version $latest_version success"
fi
else
echo 'info: nothing to upgrade'
fi
}
download() {
local url=$1
local file_name=$2
if type wget >/dev/null 2>&1; then
wget --no-check-certificate -q $url
elif type curl >/dev/null 2>&1; then
echo "curl -OLJ $url"
curl -OLJ $url
else
echo 'info: no exists wget or curl, make sure the system can use the "wget" or "curl" command'
fi
}
mkdirFullNode() {
if [ ! -d $FULL_NODE_DIR ]; then
echo "info: create $FULL_NODE_DIR"
mkdir $FULL_NODE_DIR
$(cp $0 $FULL_NODE_DIR)
cd $FULL_NODE_DIR
elif [ -d $FULL_NODE_DIR ]; then
cd $FULL_NODE_DIR
fi
}
quickStart() {
full_node_version=$(`echo getLatestReleaseVersion`)
if [[ -n $full_node_version ]]; then
mkdirFullNode
echo "info: check latest version: $full_node_version"
echo 'info: download config'
download https://raw.githubusercontent.com/tronprotocol/tron-deployment/$GITHUB_BRANCH/$FULL_NODE_CONFIG_MAIN_NET $FULL_NODE_CONFIG_MAIN_NET
mv $FULL_NODE_CONFIG_MAIN_NET 'config.conf'
echo "info: download $full_node_version"
download $RELEASE_URL/download/$full_node_version/$JAR_NAME $JAR_NAME
checkSign
else
echo 'info: not getting the latest version'
exit
fi
}
cloneCode() {
if type git >/dev/null 2>&1; then
git_clone=$(git clone -b $GITHUB_BRANCH $GITHUB_REPOSITORY)
if [[ git_clone == 0 ]]; then
echo 'info: git clone java-tron success'
fi
else
echo 'info: no exists git, make sure the system can use the "git" command'
fi
}
cloneBuild() {
local currentPwd=$PWD
echo 'info: clone java-tron'
cloneCode
echo 'info: build java-tron'
cd java-tron
sh gradlew clean build -x test
if [[ $? == 0 ]];then
cd $currentPwd
mkdirFullNode
cp '../java-tron/build/libs/FullNode.jar' $PWD
cp '../java-tron/framework/src/main/resources/config.conf' $PWD
else
exit
fi
}
checkPid() {
if [[ $JAR_NAME =~ '/' ]]; then
JAR_NAME=$(echo $JAR_NAME |awk -F '/' '{print $NF}')
fi
pid=$(ps -ef | grep -v start | grep $JAR_NAME | grep -v grep | awk '{print $2}')
return $pid
}
stopService() {
count=1
while [ $count -le $MAX_STOP_TIME ]; do
checkPid
if [ $pid ]; then
kill -15 $pid
sleep 1
else
echo "info: java-tron stop"
return
fi
count=$(($count + 1))
if [ $count -eq $MAX_STOP_TIME ]; then
kill -9 $pid
sleep 1
fi
done
sleep 5
}
checkAllowMemory() {
os=`uname`
totalMemory=$(`echo getTotalMemory`)
total=`expr $totalMemory / 1024`
if [[ $os == 'Darwin' ]]; then
return
fi
if [[ $total -lt $ALLOW_MIN_MEMORY ]]; then
echo "warn: the memory $total MB cannot be smaller than the minimum memory $ALLOW_MIN_MEMORY MB"
exit
elif [[ $SPECIFY_MEMORY -gt 0 ]] &&
[[ $SPECIFY_MEMORY -lt $ALLOW_MIN_MEMORY ]]; then
echo "warn: the specified memory $SPECIFY_MEMORY MB cannot be smaller than the minimum memory $ALLOW_MIN_MEMORY MB"
echo 'warn: start abort'
exit
fi
}
setTCMalloc() {
os=`uname`
if [[ $os == 'Linux' ]] || [[ $os == 'linux' ]] ; then
lib_tc_malloc="/usr/lib64/libtcmalloc.so"
if [[ -f $lib_tc_malloc ]]; then
export LD_PRELOAD="$lib_tc_malloc"
export TCMALLOC_RELEASE_RATE=10
else
echo 'info: recommended for linux systems using tcmalloc as the default memory management tool'
fi
fi
}
getTotalMemory() {
os=`uname`
if [[ $os == 'Linux' ]] || [[ $os == 'linux' ]] ; then
total=$(cat /proc/meminfo | grep MemTotal | awk -F ' ' '{print $2}')
echo $total
return
elif [[ $os == 'Darwin' ]]; then
total=$(sysctl -a | grep mem |grep hw.memsize |awk -F ' ' '{print $2}')
echo `expr $total / 1024`
fi
}
setJVMMemory() {
os=`uname`
if [[ $os == 'Linux' ]] || [[ $os == 'linux' ]] ; then
if [[ $SPECIFY_MEMORY >0 ]]; then
max_direct=$(echo "$SPECIFY_MEMORY/1024*0.1" | bc | awk -F. '{print $1"g"}')
if [[ "$max_direct" != "g" ]]; then
MAX_DIRECT_MEMORY=$max_direct
fi
JVM_MX=$(echo "$SPECIFY_MEMORY/1024*0.6" | bc | awk -F. '{print $1"g"}')
JVM_MS=$JVM_MX
else
total=$(`echo getTotalMemory`)
MAX_DIRECT_MEMORY=$(echo "$total/1024/1024*0.1" | bc | awk -F. '{print $1"g"}')
JVM_MX=$(echo "$total/1024/1024*0.6" | bc | awk -F. '{print $1"g"}')
JVM_MS=$JVM_MX
fi
elif [[ $os == 'Darwin' ]]; then
MAX_DIRECT_MEMORY='1g'
fi
}
startService() {
echo $(date) >>start.log
if [[ ! $JAR_NAME =~ '-c' ]]; then
FULL_START_OPT="$FULL_START_OPT -c $DEFAULT_FULL_NODE_CONFIG"
fi
if [[ ! -f $JAR_NAME ]]; then
echo "warn: jar file $JAR_NAME not exist"
exit
fi
nohup $JAVACMD -Xms$JVM_MS -Xmx$JVM_MX -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -Xloggc:./gc.log \
-XX:+PrintGCDateStamps -XX:+CMSParallelRemarkEnabled -XX:ReservedCodeCacheSize=256m -XX:+UseCodeCacheFlushing \
-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m \
-XX:MaxDirectMemorySize=$MAX_DIRECT_MEMORY -XX:+HeapDumpOnOutOfMemoryError \
-XX:NewRatio=2 -jar \
$JAR_NAME $FULL_START_OPT >>start.log 2>&1 &
checkPid
echo "info: start java-tron with pid $pid on $HOSTNAME"
echo "info: if you need to stop the service, execute: sh start.sh --stop"
}
rebuildManifest() {
if [[ $REBUILD_MANIFEST = false ]]; then
echo 'info: disable rebuild manifest!'
return
fi
if [[ ! -d $REBUILD_DIR ]]; then
echo "info: database not exists, skip rebuild manifest"
return
fi
ARCHIVE_JAR='ArchiveManifest.jar'
if [[ -f $ARCHIVE_JAR ]]; then
echo 'info: execute rebuild manifest.'
$JAVACMD -jar $ARCHIVE_JAR -d $REBUILD_DIR -m $REBUILD_MANIFEST_SIZE -b $REBUILD_BATCH_SIZE
else
echo 'info: download the rebuild manifest plugin from the github'
local latest=$(`echo getLatestReleaseVersion`)
download $RELEASE_URL/download/GreatVoyage-v"$latest"/$ARCHIVE_JAR $ARCHIVE_JAR
if [[ $download == 0 ]]; then
echo 'info: download success, rebuild manifest'
$JAVACMD -jar $ARCHIVE_JAR $REBUILD_DIR -m $REBUILD_MANIFEST_SIZE -b $REBUILD_BATCH_SIZE
fi
fi
if [[ $? == 0 ]]; then
echo 'info: rebuild manifest success'
else
echo 'info: rebuild manifest fail, log in logs/archive.log'
fi
}
specifyConfig(){
echo "info: specify the net: $1"
local netType=$1
local configName;
if [[ "$netType" = 'test' ]]; then
configName=$FULL_NODE_CONFIG_TEST_NET
elif [[ "$netType" = 'private' ]]; then
configName=$FULL_NODE_CONFIG_PRIVATE_NET
else
echo "warn: no support config $nodeType"
exit
fi
if [[ ! -d $FULL_NODE_CONFIG_DIR ]]; then
mkdir -p $FULL_NODE_CONFIG_DIR
fi
if [[ -d $FULL_NODE_CONFIG_DIR/$configName ]]; then
DEFAULT_FULL_NODE_CONFIG=$FULL_NODE_CONFIG_DIR/$configName
break
fi
if [[ ! -f $FULL_NODE_CONFIG_DIR/$configName ]]; then
download https://raw.githubusercontent.com/tronprotocol/tron-deployment/$GITHUB_BRANCH/$configName $configName
mv $configName $FULL_NODE_CONFIG_DIR/$configName
DEFAULT_FULL_NODE_CONFIG=$FULL_NODE_CONFIG_DIR/$configName
fi
}
checkSign() {
echo 'info: verify signature'
local latest_version=$(`echo getLatestReleaseVersion`)
download $RELEASE_URL/download/$latest_version/sha256sum.txt sha256sum.txt
fullNodeSha256=$(cat sha256sum.txt|grep 'FullNode'| awk -F ' ' '{print $1}')
os=`uname`
if [[ $os == 'Linux' ]] || [[ $os == 'linux' ]] ; then
releaseFullNodeSha256=$(sha256sum FullNode.jar| grep FullNode | awk -F ' ' '{print $1}')
elif [[ $os == 'Darwin' ]]; then
releaseFullNodeSha256=$(shasum -a 256 FullNode.jar| grep FullNode | awk -F ' ' '{print $1}')
cat $releaseFullNodeSha256 | awk -F ' ' '{print $0}'
fi
echo "info: release sha256sum sign: $releaseFullNodeSha256"
echo "info: FullNode.jar sha256sum sign: $fullNodeSha256"
if [[ "$fullNodeSha256" == "$releaseFullNodeSha256" ]]; then
echo 'info: sha256 signatures pass'
else
echo 'info: sha256 signature exception!!!'
echo 'info: please compile from the code or download the latest version from https://github.com/tronprotocol/java-tron'
fi
}
restart() {
stopService
checkAllowMemory
rebuildManifest
setTCMalloc
setJVMMemory
startService
}
while [ -n "$1" ]; do
case "$1" in
-c)
DEFAULT_FULL_NODE_CONFIG=$2
shift 2
;;
-d)
REBUILD_DIR=$2/database
FULL_START_OPT="$FULL_START_OPT $1 $2"
shift 2
;;
-j)
JAR_NAME=$2
shift 2
;;
-p)
FULL_START_OPT="$FULL_START_OPT $1 $2"
shift 2
;;
-w)
FULL_START_OPT="$FULL_START_OPT $1"
shift 1
;;
--witness)
FULL_START_OPT="$FULL_START_OPT $1"
shift 1
;;
--net)
specifyConfig $2
shift 2
;;
-m)
REBUILD_MANIFEST_SIZE=$2
shift 2
;;
-n)
JAR_NAME=$2
shift 2
;;
-b)
REBUILD_BATCH_SIZE=$2
shift 2
;;
-cb)
CLONE_BUILD=true
shift 1
;;
--download)
DOWNLOAD=true
shift 1
;;
--deploy)
QUICK_START=true
shift 1
;;
--release)
QUICK_START=true
shift 1
;;
--clone)
cloneCode
exit
;;
-mem)
SPECIFY_MEMORY=$2
shift 2
;;
--disable-rewrite-manifes)
REBUILD_MANIFEST=false
shift 1
;;
-dr)
REBUILD_MANIFEST=false
shift 1
;;
--upgrade)
UPGRADE=true
shift 1
;;
--run)
if [[ $ALL_OPT_LENGTH -eq 1 ]]; then
restart
fi
RUN=true
shift 1
;;
--stop)
stopService
;;
FullNode)
RUN=true
shift 1
;;
FullNode.jar)
RUN=true
shift 1
;;
*.jar)
RUN=true
shift 1
;;
*)
if [[ $ALL_OPT_LENGTH -eq 1 ]]; then
if [[ ! "$1" =~ "-" ]] && [[ ! "$1" =~ "--" ]]; then
if [[ $1 =~ '.jar' ]]; then
JAR_NAME=$1
else
JAR_NAME="$1.jar"
fi
restart
exit
fi
fi
FULL_START_OPT="$FULL_START_OPT $@"
break
;;
esac
done
if [[ $IS_BACKUP_GC_LOG = true ]]; then
backupGCLog
fi
if [[ $CLONE_BUILD == true ]];then
cloneBuild
fi
if [[ $QUICK_START == true ]]; then
quickStart
if [[ $? == 0 ]] ; then
if [[ $RUN == true ]]; then
cd $FULL_NODE_DIR
FULL_START_OPT=''
restart
fi
fi
fi
if [[ $UPGRADE == true ]]; then
upgrade
fi
if [[ $DOWNLOAD == true ]]; then
latest=$(`echo getLatestReleaseVersion`)
if [[ -n $latest ]]; then
download $RELEASE_URL/download/$latest/$JAR_NAME $latest
exit
else
echo 'info: not getting the latest version'
fi
fi
if [[ $ALL_OPT_LENGTH -eq 0 || $ALL_OPT_LENGTH -gt 0 ]]; then
restart
fi
if [[ $RUN == true ]]; then
restart
fi