This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstateless-ergo-node.sh
More file actions
307 lines (246 loc) · 9.55 KB
/
stateless-ergo-node.sh
File metadata and controls
307 lines (246 loc) · 9.55 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
#!/bin/sh
#download things
echo "Downloading stuff... please wait :)"
#apk --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ add android-tools --quiet
apk add openjdk11 python3 wget tmux curl --quiet
tmux kill-session -t node_session
clear
echo "- Retrieving latest node release."
LATEST_ERGO_RELEASE=$(curl -s "https://api.github.com/repos/ergoplatform/ergo/releases/latest" | awk -F '"' '/tag_name/{print $4}')
LATEST_ERGO_RELEASE_NUMBERS=$(echo ${LATEST_ERGO_RELEASE} | cut -c 2-)
ERGO_DOWNLOAD_URL=https://github.com/ergoplatform/ergo/releases/download/${LATEST_ERGO_RELEASE}/ergo-${LATEST_ERGO_RELEASE_NUMBERS}.jar
echo "- Downloading Latest known Ergo release: ${LATEST_ERGO_RELEASE}."
curl --silent -L ${ERGO_DOWNLOAD_URL} --output ergo.jar
export BLAKE_HASH="d3bce9a53e3fbaba4a0cb92f9e419bb47123c07ab31f626362e2658e7dcfc7c2"
PEER_CHECK_INTERVAL=10
MAX_CHECKS=2
CHECK_COUNT=0
ROUND_COUNT=0
# Set some environment variables
set_environment(){
export KEY=ee7OHzUHWFBB8eeBf9PD9BQk2
let j=0
#OS=$(uname -m)
dt=$(date '+%d/%m/%Y %H:%M:%S');
let i=0
let PERCENT_BLOCKS=100
let PERCENT_HEADERS=100
}
set_configuration(){
echo 'ergo {
networkType = "mainnet"
node.stateType = "digest"
node.blocksToKeep = 1440
node.nipopow.nipopowBootstrap = true
}
scorex {
network {
maxConnections = 1000
magicBytes = [1, 0, 2, 4]
bindAddress = "0.0.0.0:9030"
nodeName = "📱 ErgoNodeAndroid Stateless 📱"
}
restApi {
apiKeyHash = "$BLAKE_HASH"
}
}
' > ergo.conf
}
main_thing(){
set_environment
# Check for the prescence of log files
count=`ls -1 blake.conf 2>/dev/null | wc -l`
if [ $count != 0 ]; then
API_KEY=$(cat "api.conf")
echo "
Configuration is ok. Starting up..."
BLAKE_HASH=$(cat "blake.conf")
echo "
"
set_configuration
start_node
else
# If no .log file - we assume first run
first_run
fi
print_console
}
start_node(){
tmux new-session -d -s node_session 'java -jar -Xmx2g ergo.jar --mainnet -c ergo.conf'
while ! curl --output /dev/null --silent --head --fail http://localhost:9053; do sleep 1; done; # wait for node be ready with progress bar
echo "- Node has started. Searching for peers..."
end_time=$(($(date +%s) + 120))
while [ $(date +%s) -lt $end_time ]; do
PEERS=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin).get('peersCount'));")
echo -ne "- Number of connected peers: $PEERS"'\r'
done
echo "
"
}
# Set basic config for boot, boot & get the hash and then re-set config
first_run() {
KEY=ee7OHzUHWFBB8eeBf9PD9BQk2
BLAKE_HASH="d3bce9a53e3fbaba4a0cb92f9e419bb47123c07ab31f626362e2658e7dcfc7c2"
#set_configuration
### Download the latest .jar file
if [ ! -e *.jar ]; then
echo "- Retrieving latest node release."
LATEST_ERGO_RELEASE=$(curl -s "https://api.github.com/repos/ergoplatform/ergo/releases/latest" | awk -F '"' '/tag_name/{print $4}')
LATEST_ERGO_RELEASE_NUMBERS=$(echo ${LATEST_ERGO_RELEASE} | cut -c 2-)
ERGO_DOWNLOAD_URL=https://github.com/ergoplatform/ergo/releases/download/${LATEST_ERGO_RELEASE}/ergo-${LATEST_ERGO_RELEASE_NUMBERS}.jar
echo "- Downloading Latest known Ergo release: ${LATEST_ERGO_RELEASE}."
curl --silent -L ${ERGO_DOWNLOAD_URL} --output ergo.jar
fi
echo "
Generating unique API key..."
sleep 2
length=25
export API_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $length | head -n 1)
echo "$API_KEY" > api.conf
#export key=$(cat api.conf)
tmux new-session -d -s new_session 'java -jar ergo.jar --mainnet -c ergo.conf'
echo "- Node has started... Setting blake hash and finding peers"
if ! ping -c 1 http://localhost:9053/info &> /dev/null; then
echo "
"
sleep 20
fi
end_time=$(($(date +%s) + 100))
while [ $(date +%s) -lt $end_time ]; do
PEERS=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin).get('peersCount'));")
echo -ne "- Number of connected peers: $PEERS"'\r'
done
echo "
"
export BLAKE_HASH=$(curl --silent -X POST "http://localhost:9053/utils/hash/blake2b" -H "accept: application/json" -H "Content-Type: application/json" -d "\"$API_KEY\"")
echo "$BLAKE_HASH" > blake.conf
echo "BLAKE_HASH:$BLAKE_HASH"
#areyou_there
curl -X POST --max-time 10 "http://127.0.0.1:9053/node/shutdown" -H "api_key: $KEY"
sleep 10
tmux kill-session -t new_session
#rm -rf .ergo
clear
# Add blake hash
#echo "Your unique API key will be added to the configuration when you close the app completely and restart"
set_configuration
#error_log
#print_console
main_thing
}
func_kill(){
case "$(uname -s)" in
CYGWIN*|MINGW32*|MSYS*|MINGW*)
echo 'MS Windows'
netstat -ano | findstr :9053
taskkill /PID 9053 /F
;;
armv7l*|aarch64)
#echo "on Pi!"
#kill
curl -X POST --max-time 10 "http://127.0.0.1:9053/node/shutdown" -H "api_key: $API_KEY"
sleep 10
tmux kill-session -t node_session
sleep 10
;;
*) #Other
#kill -9 $(lsof -t -i:9053)
#kill -9 $(lsof -t -i:9030)
curl -X POST --max-time 10 "http://127.0.0.1:9053/node/shutdown" -H "api_key: $API_KEY"
sleep 10
tmux kill-session -t node_session
sleep 10
;;
esac
}
error_log(){
inputFile=ergo.log
if grep -E 'ERROR\|WARN' "$inputFile" ; then
echo "WARN/ERROR:" $egrep
echo "$egrep" >> error.log
elif grep -E 'Got GetReaders request in state (None,None,None,None)\|port' "$inputFile" ; then
echo "Readers not ready. If this keeps happening we'll attempt to restart: $i"
((i=i+1))
elif grep -E 'Invalid z bytes' "$inputFile" ; then
echo "zBYTES error:" $egrep
echo "$egrep" >> error.log
fi
if [ $i -gt 10 ]; then
i=0
echo i: $i
#func_kill
curl -X POST --max-time 10 "http://127.0.0.1:9053/node/shutdown" -H "api_key: $API_KEY"
tmux kill-session -t node_session
main_thing
fi
}
check_status(){
LRED="\033[1;31m" # Light Red
LGREEN="\033[1;32m" # Light Green
NC='\033[0m' # No Color
string=$(curl -sf --max-time 20 "${1}")
if [ -z "$string" ]; then
echo -e "${LRED}${1} is down${NC}"
tmux kill-session -t node_session
main_thing
else
echo -e "${LGREEN}${1} is online${NC}"
fi
}
get_heights(){
check_status "localhost:9053/info"
API_HEIGHT=$(curl --silent --max-time 10 --output -X GET "https://api.ergoplatform.com/api/v1/networkState" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin)['height']);")
HEADERS_HEIGHT=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin)['headersHeight']);")
HEIGHT=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin)['parameters']['height']);")
FULL_HEIGHT=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin)['fullHeight']);")
API_HEIGHT=${API_HEIGHT:-0}
# Calculate %
if [ "$API_HEIGHT" -gt 0 ]; then
if [ "$HEADERS_HEIGHT" -eq "$HEIGHT" ]; then
echo "HeadersHeight == Height"
echo "Node is synchronized"
exit 1
elif [ "$HEADERS_HEIGHT" -lt "$HEIGHT" ]; then
echo "HeadersHeight < Height"
echo "Mis-sync!"
exit 1
else
PERCENT_HEADERS=$(( ( ($API_HEIGHT - $HEADERS_HEIGHT) * 100) / $API_HEIGHT ))
PERCENT_BLOCKS=$(( ( ($API_HEIGHT - $HEIGHT) * 100) / $API_HEIGHT ))
fi
fi
}
## Display info to user
print_console(){
while sleep 1
do
clear
printf "%s \n\n" \
"View the Ergo node panel at 127.0.0.1:9053/panel"\
"
Your unique API key is: $API_KEY" \
" " \
"
Sync Progress;"\
"### Headers: ~$(( 100 - $PERCENT_HEADERS ))% Complete ($HEADERS_HEIGHT/$API_HEIGHT) ### "\
"### Blocks: ~$(( 100 - $PERCENT_BLOCKS ))% Complete ($HEIGHT/$API_HEIGHT) ### "
PEERS=$(curl --silent --max-time 10 --output -X GET "http://localhost:9053/info" -H "accept: application/json" | python3 -c "import sys, json; print(json.load(sys.stdin)['peersCount']);")
echo "Number of connected peers:$PEERS"
echo ""
wget -q --spider http://google.com
if [ $? -eq 0 ]; then
echo ""
else
echo "You are not connected to the internet!"
print_console
fi
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt: HEADERS: $HEADERS_HEIGHT, HEIGHT:$HEIGHT" >> height.log
get_heights
done
}
# Set some environment variables
set_environment
# pipes initial config > ergo.conf
set_configuration
main_thing