-
Notifications
You must be signed in to change notification settings - Fork 0
/
synchronize-resources.sh
executable file
·113 lines (84 loc) · 3.06 KB
/
synchronize-resources.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
#!/bin/bash
set -x
trap graceful_shutdown SIGINT SIGQUIT SIGTERM
graceful_shutdown()
{
echo "begin shutdown..."
# stop target
kill -2 $TARGET_PID
# stop teku
kill -2 $TEKU_PID
# check that processes have terminated.
TARGET_GREP="target"
TEKU_GREP="teku"
while [ ! -z "$TARGET_GREP" ] || [ ! -z "$TEKU_GREP" ]
do
sleep 10
TARGET_GREP=""
if [ ! -z "$TARGET_GREP_STR" ]; then
TARGET_GREP=`ps axo pid,ppid,cmd | grep "$TARGET_GREP_STR"`
fi
TEKU_GREP=""
if [ ! -z "$TEKU_GREP_STR" ]; then
TEKU_GREP=`ps axo pid,ppid,cmd | grep "$TEKU_GREP_STR"`
fi
done
echo "Sync shutdown success"
exit
}
CONFIG_FILE=$(pwd)/config.toml
TARGET=$1
get_config () {
stoml $CONFIG_FILE $1
}
# get working dir
WORKING_DIR=$HOME
OUTPUT_DIR=$(get_config "output_dir")
echo "WAIT" > $OUTPUT_DIR/ipc-$TARGET.dat
# start target
TARGET_LOG="$OUTPUT_DIR/$TARGET-sync-$(date -Iseconds).log"
TARGET_CMD=$(get_config "$TARGET.exec_cmd")
DATA_DIR_PARAM=$(get_config "$TARGET.datadir_flag")=$WORKING_DIR/$(get_config "$TARGET.datadir")
JWT_FLAG=$(get_config "$TARGET.jwt_flag")
TARGET_JWT_FILE=$WORKING_DIR/$(get_config "$TARGET.jwt_path")
if [ ! -z $JWT_FLAG ]; then
JWT_PARAM="$JWT_FLAG=$TARGET_JWT_FILE"
fi
{ $TARGET_CMD $JWT_PARAM $DATA_DIR_PARAM &> $TARGET_LOG; } &
sleep 2
TARGET_PPID=$!
TARGET_GREP_STR=$TARGET_PPID.*$(get_config "$TARGET.grep_str")
TARGET_PID=`ps axo pid,ppid,cmd | grep "$TARGET_GREP_STR" | awk '{print $1}'`
sleep 60
# start teku
TEKU_LOG=$OUTPUT_DIR/teku-sync-$(date -Iseconds).log
{ teku --ee-endpoint=http://localhost:8551 --ee-jwt-secret-file=$TARGET_JWT_FILE --data-beacon-path=$WORKING_DIR/nvme/teku-data-dir/ &> $TEKU_LOG; } &
sleep 2
TEKU_PPID=$!
TEKU_GREP_STR=$TEKU_PPID.*teku\\.home
TEKU_PID=`ps axo pid,ppid,cmd | grep $TEKU_GREP_STR | awk '{print $1}'`
sleep 2
# check is synchonized < 2 blocks from etherscan
SYNC_DISTANCE=10000
# wait ~10 min for nethermind ports to init
if [ "$TARGET" = "nethermind" ]; then
sleep 120
fi
while [ true ]
do
# wait 30 seconds
sleep 30
top -n 1 -b -p $TARGET_PPID | tail -n 1 >> $OUTPUT_DIR/top-$TARGET.dat
# curl to etherscan
ETHERSCAN_BLOCK_HEX=`curl 'https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey='"$ETHERSCAN_API_KEY" | jq -r .result | awk '{ print substr( $0, 3 ) }' | awk '{print toupper($0)}'`
ETHERSCAN_BLOCK=`echo "obase=10; ibase=16; $ETHERSCAN_BLOCK_HEX" | bc`
# curl to target and get number
TARGET_BLOCK_HEX=`curl --data '{"method":"eth_getBlockByNumber","params":["latest", false],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST 127.0.0.1:8545 | jq -r .result.number | awk '{ print substr( $0, 3 ) }' | awk '{print toupper($0)}'`
TARGET_BLOCK=`echo "obase=10; ibase=16; $TARGET_BLOCK_HEX" | bc`
# compute distances
SYNC_DISTANCE=$(( $ETHERSCAN_BLOCK - $TARGET_BLOCK ))
echo "Sync distance: $SYNC_DISTANCE"
if [ $SYNC_DISTANCE -lt 2 ]; then
echo "READY" > $OUTPUT_DIR/ipc-$TARGET.dat
fi
done