-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwatchBlocksLive
240 lines (197 loc) · 6.66 KB
/
watchBlocksLive
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
#!/bin/bash
#Requires JQ to be installed for parsing Json from BscScan (sudo apt install jq)
#Needs execute rights (sudo chmod +x watchBlocksLive)
#Script assumes it's in the same directory as "geth", if not you'll need to edit the paths
#=======Options===========
#paste your bscan API key here
BSCSCANAPIKEY=APIKEYPASTEHERE
#bsc.log location
BSCLOG='node/bsc.log'
#geth executable location
GETHLOCATION='./geth'
#=========================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
function checkDepends(){
if ! command -v jq &> /dev/null
then
throwErr "JQ Not installed" "n/a" "n/a" "Please install JQ with 'apt install jq'" ;
exit
fi
if [ ! -f "$BSCLOG" ]; then
throwErr "BSC.log not found at $BSCLOG" "n/a" "n/a" "Please edit the path to the BSC log file" ;
exit
fi
if [ ! -f "$GETHLOCATION" ]; then
throwErr "Geth executable not found at $GETHLOCATION" "n/a" "n/a" "Please edit the path to the GETH executable" ;
exit
fi
if [ -z "$BSCSCANAPIKEY" ] || [ "$BSCSCANAPIKEY" == "APIKEYPASTEHERE" ]; then
throwErr "Missing API Key" "n/a" "n/a" "Please update your BSCScan API key in the script";
exit
fi
}
function get_age() {
echo "$(tail -1 $BSCLOG | awk '/age/ {print $11}')";
}
function get_highest_block(){
echo $($GETHLOCATION attach http://127.0.0.1:8545 --exec 'eth.syncing.highestBlock')
}
function get_current_block(){
echo $($GETHLOCATION attach http://127.0.0.1:8545 --exec 'eth.syncing.currentBlock')
}
function get_peerCount(){
echo $($GETHLOCATION attach http://127.0.0.1:8545 --exec 'net.peerCount')
}
export firstsync="1"
function stillSyncing(){
if [[ "$firstsync" == "0" ]]; then
echo -e "\e[6A" #move cursor to start of table
fi
if [[ "$var" == "" ]]; then
var="$(get_age)"
fi
var2="$(get_age)"
if [[ "$var" != "$var2" ]] && [[ "$var2" != "" ]]; then
var=$var2
AGE=${var//"age="/}
fi
currentBlock=$(get_current_block)
highestBlock=$(get_highest_block)
currentDiff="$(($highestBlock - $currentBlock))"
peerCount=$(get_peerCount)
TABLEDATA="Highest,Current,Diff,AgeDiff,PeerCount\n$highestBlock,$currentBlock,$currentDiff,$AGE,$peerCount"
printTable ',' $(echo $TABLEDATA)
firstsync="0"
sleep 3
}
function printTable()
{
local -r delimiter="${1}"
local -r data="$(removeEmptyLines "${2}")"
if [[ "${delimiter}" != '' && "$(isEmptyString "${data}")" = 'false' ]]
then
local -r numberOfLines="$(wc -l <<< "${data}")"
if [[ "${numberOfLines}" -gt '0' ]]
then
local table=''
local i=1
for ((i = 1; i <= "${numberOfLines}"; i = i + 1))
do
local line=''
line="$(sed "${i}q;d" <<< "${data}")"
local numberOfColumns='0'
numberOfColumns="$(awk -F "${delimiter}" '{print NF}' <<< "${line}")"
# Add Line Delimiter
if [[ "${i}" -eq '1' ]]
then
table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
fi
# Add Header Or Body
table="${table}\n"
local j=1
for ((j = 1; j <= "${numberOfColumns}"; j = j + 1))
do
table="${table}$(printf '#| %s' "$(cut -d "${delimiter}" -f "${j}" <<< "${line}")")"
done
table="${table}#|\n"
# Add Line Delimiter
if [[ "${i}" -eq '1' ]] || [[ "${numberOfLines}" -gt '1' && "${i}" -eq "${numberOfLines}" ]]
then
table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
fi
done
if [[ "$(isEmptyString "${table}")" = 'false' ]]
then
echo -e "${table}" | column -s '#' -t | awk '/^\+/{gsub(" ", "-", $0)}1'
fi
fi
fi
}
function removeEmptyLines()
{
local -r content="${1}"
echo -e "${content}" | sed '/^\s*$/d'
}
function repeatString()
{
local -r string="${1}"
local -r numberToRepeat="${2}"
if [[ "${string}" != '' && "${numberToRepeat}" =~ ^[1-9][0-9]*$ ]]
then
local -r result="$(printf "%${numberToRepeat}s")"
echo -e "${result// /${string}}"
fi
}
function isEmptyString()
{
local -r string="${1}"
if [[ "$(trimString "${string}")" = '' ]]
then
echo 'true' && return 0
fi
echo 'false' && return 1
}
function trimString()
{
local -r string="${1}"
sed 's,^[[:blank:]]*,,' <<< "${string}" | sed 's,[[:blank:]]*$,,'
}
function throwErr(){
echo ""
echo "=============== ERROR =================="
echo -e "${RED}ERROR:${NC} $1"
echo -e "${RED}API Response:${NC} $2"
echo -e "${RED}URL:${NC} $3"
echo ""
echo -e "${RED}Note:${YELLOW} $4 ${NC}"
echo "========================================"
echo ""
}
function curlBscscan(){
#url='https://api.bscscan.com/api?module=block&action=getblocknobytime×tamp='$(date +"%s")'&closest=before&apikey='$BSCSCANAPIKEY
#old URL was throwing getting out of sync with BSCScan
url='https://api.bscscan.com/api?module=proxy&action=eth_blockNumber&apikey='$BSCSCANAPIKEY
response=$(curl -s "$url")
hexresult=$(echo $response | jq -r '.result')
result=$(($hexresult))
if [[ $result == ?(-)+([0-9]) ]]; then
#reponse is an block number
currentblock=$result
else
throwErr "BSCScan API" "$response" "$url" "BSCScans API could be down or out of sync"
exit
fi
}
#===================================
checkDepends
curlBscscan
highest=$($GETHLOCATION attach http://127.0.0.1:8545 --exec 'eth.blockNumber')
diff="$(($currentblock - $highest))"
echo ' _ __ _
|_) (_ / |\ | _ _| _ |\/| _ ._ o _|_ _ ._
|_) __) \_ | \| (_) (_| (/_ | | (_) | | | |_ (_) |
';
echo =============================
echo BSC Latest Block: $currentblock
echo NODE Local Block: $highest
echo ""
if [[ "0" -eq $highest ]]; then
echo -e "${RED}Out of Sync!...${NC}";
echo =============================
echo -e "${GREEN}Searching Log + Geth:${NC} $BSCLOG";
while true; do
stillSyncing;
sleep 2;
done
else
if [[ "0" -lt $diff ]]; then
echo -e "${RED}You are full node but $diff blocks behind${NC}"
echo =============================
else
echo -e "${GREEN}You are synced and AHEAD of BSCscan.com by $((-1 * $diff)) blocks${NC}"
echo =============================
fi
fi