-
Notifications
You must be signed in to change notification settings - Fork 29
/
starting.sh
executable file
·332 lines (302 loc) · 10.3 KB
/
starting.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
#!/bin/bash
cd "${BASH_SOURCE%/*}" || exit
RESET="\033[0m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
# default to LABS.
override_args=""
notary="LABS"
notary_arg="-labsnotary=1"
if [[ ! -z "${1}" ]]; then
notary="${1}"
if [[ "${notary}" == "KMD" ]]; then
notary_arg="-notary -gen -genproclimit=1"
fi
fi
echo "Starting ${notary}... "
cp assetchains_${notary}.json assetchains.json > /dev/null 2>&1
ac_json=$(cat assetchains.json)
echo ${ac_json} | jq .[] > /dev/null 2>&1
outcome=$(echo $?)
if (( outcome != 0 )); then
echo -e ${RED}"[${notary}] assetchains.json is invalid, check git blame and start flame war..."${RESET}
exit 1
fi
override_args="${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10}"
longestchain () {
chain=$1
cli=$(./listclis.sh ${chain})
tries=0
lc=0
while (( lc == 0 )); do
info=$(${cli} getinfo 2> /dev/null)
lc=$(jq -r '.longestchain'<<<"${info}")
tries=$(( tries +1 ))
connections=$(jq -r .connections <<<"${info}")
if (( tries > 10 )) && (( lc == 0 )) && (( connections > 1 )); then
headers=$(${cli} getpeerinfo 2> /dev/null | jq -r '[sort_by(.synced_headers) | .[].synced_headers]')
n=$(( $(jq length <<<"${headers}") -1 ))
lc=$(jq -r .[${n}] <<<"${headers}")
fi
if (( tries > 30 )) && (( lc == 0 )); then
echo "${lc}"
return 1
fi
sleep 2
done
echo "${lc}"
return 0
}
checksync () {
chain=$1
cli=$(./listclis.sh ${chain})
lc=$(longestchain $1)
blocks=$(${cli} getblockcount 2> /dev/null)
while (( blocks < lc )); do
sleep 60
lc=$(longestchain ${chain})
blocks=$(${cli} getblockcount 2> /dev/null)
progress=$(echo "scale=3;${blocks}/${lc}" | bc -l)
echo "[${chain}] $(echo ${progress}*100|bc)% ${blocks} of ${lc}"
info=$(${cli} getnetworkinfo 2> /dev/null)
connections=$(jq -r .connections <<<"${info}")
if (( connections == 0 )); then
break
fi
done
echo "[${chain}] Synced on block: ${lc}"
return 0
}
checkSuperNETRepo () {
prevdir=${PWD}
if [[ ! -f iguana/${1}/lastbuildcommit ]]; then
eval cd "${HOME}/SuperNET"
git pull > /dev/null 2>&1
git checkout ${1} > /dev/null 2>&1
localrev=$(git rev-parse HEAD)
mkdir -p ${HOME}/LabsNotary/iguana/${1}
echo ${localrev} > ${HOME}/LabsNotary/iguana/${1}/lastbuildcommit
cd ${prevdir}
fi
localrev=$(cat iguana/${1}/lastbuildcommit)
eval cd "${HOME}/SuperNET"
git remote update > /dev/null 2>&1
remoterev=$(git rev-parse origin/${1})
cd ${prevdir}
if [[ "${localrev}" != "${remoterev}" ]]; then
return 0
else
return 1
fi
}
stop_daemon() {
chain=${1}
echo "[kmd->${chain}] Stopping ..."
cli=$(./listclis.sh ${chain})
${cli} stop > /dev/null 2>&1
daemon_stopped "${chain}"
echo "[kmd->${chain}] Stopped."
}
daemon_stopped () {
if [[ ${1} == "KMD" ]]; then
pidfile="${HOME}/.komodo/komodod.pid"
else
pidfile="${HOME}/.komodo/${1}/komodod.pid"
fi
while [[ -f ${pidfile} ]]; do
pid=$(cat ${pidfile} 2> /dev/null)
pspid=$(ps -p ${pid} 2> /dev/null | grep komodod)
outcome=$(echo $?)
#echo -n "ps -p ${pid} grep komodod = ${pspid} "
if (( outcome != 0 )); then
rm ${pidfile}
fi
echo "${outcome}"
sleep 2
done
}
check_chain_started () {
chain=${1}
cli=$(./listclis.sh ${chain})
echo "[${chain}] : Waiting for ${chain} daemon to start..."
./validateaddress.sh ${chain}
${cli} getblockcount > /dev/null 2>&1
outcome=$(echo $?)
if (( outcome == 1 )); then
echo -e ${RED}"Starting ${chain} failed. fix it! ${RESET}
$(chain_start_cmd ${chain})"
exit 1
fi
}
get_kmdbranch() {
branch="master"
attempt=$(jq -r --arg chain ${1} '.[] | select (.ac_name == $chain) | .branch' <<<"${ac_json}")
if [[ ${attempt} != "null" ]] && [[ ${attempt} != "" ]]; then
branch=${attempt}
fi
echo ${branch}
}
get_iguanabranch() {
branch="blackjok3r"
attempt=$(jq -r --arg chain ${1} '.[] | select (.ac_name == $chain) | .iguana' <<<"${ac_json}")
if [[ ${attempt} != "null" ]] && [[ ${attempt} != "" ]]; then
branch=${attempt}
fi
echo ${branch}
}
chain_start_cmd () {
chain="${1}"
branch=$(get_kmdbranch "${chain}")
params="$(./listassetchainparams.py "${chain}")"
echo ""${PWD}"/komodo/"${branch}"/komodod "${params}""
}
start_assetchain () {
chain=${1}
iguana=$(jq -r --arg chain "${chain}" '.[] | select (.ac_name == $chain) | .iguana' <<<"${ac_json}")
rpc=$(jq -r --arg chain "${chain}" '.[] | select (.ac_name == $chain) | .iguana_rpc' <<<"${ac_json}")
conf="${HOME}/.komodo/${chain}/${chain}.conf"
if [[ "${iguana}" == "blackjok3r" ]];then
if [[ -f "${conf}" ]]; then
sed -i '/blocknotify/d' "${conf}"
echo "blocknotify=curl -s --url \"http://127.0.0.1:${rpc}\" --data \"{\\\"agent\\\":\\\"dpow\\\",\\\"method\\\":\\\"updatechaintip\\\",\\\"blockhash\\\":\\\"%s\\\",\\\"symbol\\\":\\\"${chain}\\\"}\"" >> ${conf}
else
echo "${chain}" >> restart_queue
fi
fi
screen -S ${chain} -d -m $(chain_start_cmd ${chain}) -pubkey=${pubkey} ${override_args} &
}
pubkey=$(./printkey.py pub)
Radd=$(./printkey.py Radd)
privkey=$(./printkey.py wif)
rm restart_queue > /dev/null 2>&1
if [[ ${#pubkey} != 66 ]]; then
echo -e ${RED}" Pubkey invalid: Please check your config.ini "${RESET}
exit 1
fi
if [[ ${#Radd} != 34 ]]; then
echo -e ${RED}" R-address invalid: Please check your config.ini "${RESET}
exit 1
fi
if [[ ${#privkey} != 52 ]]; then
echo -e ${RED}" WIF-key invalid: Please check your config.ini "${RESET}
exit 1
fi
# update all branches that need updating
declare -a updated_branches=()
let n=0
while read -r branch; do
echo "[kmd->${branch}] Checking for updates and building, could take years off your life..."
# try 3 times before annoying the OP with an error ^^
let ret=3
for (( i = 0; i < 3; i++ )); do
ret=$(./update_komodo.sh ${branch} ${i})
if [[ "${ret}" == "2" ]]; then
echo "[kmd->${branch}] no update... "
break
elif [[ "${ret}" == "0" ]]; then
echo "[kmd->${branch}] Updated to $(cat komodo/${branch}/lastbuildcommit 2> /dev/null)"
updated_branches+=("${branch}")
break
elif (( i == 3 )); then
echo -e ${RED}"[kmd->${branch}] Failed to update 3 times! Build manually or fix the problem." ${RESET}"
cd LABSKomodo
git checkout ${branch}
./zcutil/build.sh -j""$(nproc)"
exit 1
else
echo "[kmd->${branch}] retrying build $(( 3-i)) more times"
fi
done
let n=n+1
done < <(./listbranches.py | uniq)
wait_for_last_function () {
waitfor=${1}
for pid in ${waitfor[*]}; do
wait ${pid}
done
}
# stop all daemons that are using an updated branch, to restart them later.
pids=()
while read -r chain; do
if [[ ${updated_branches[*]} =~ $(get_kmdbranch ${chain}) ]]; then
stop_daemon "${chain}" &
pids[${chain}]=$!
fi
done < <(./listcoins.sh)
wait_for_last_function ${pids}
# Start KMD
sed -i '/daemon/d' "${HOME}/.komodo/komodo.conf" > /dev/null 2>&1
echo "[kmd->master] : Starting KMD"
screen -S "KMD" -d -m ${HOME}/LabsNotary/komodo/master/komodod ${notary_arg} -pubkey=${pubkey} &
# Start assetchains
while read -r chain; do
start_assetchain ${chain} &
done < <(./listassetchains.py)
# We can add another start scipt for 3rd party coins here :)
# extract all iguanas in assetchains.json and update them if needed
while read -r branch; do
if $(checkSuperNETRepo "${branch}"); then
rm iguana/${branch}/iguana 2> /dev/null
fi
if [[ ! -f iguana/${branch}/iguana ]]; then
echo "[iguana->${branch}] building ...."
./build_iguana ${branch}
if [[ -f iguana/${branch}/iguana ]]; then
eval cd "${HOME}/SuperNET"
localrev=$(git rev-parse HEAD)
echo ${localrev} > ${HOME}/LabsNotary/iguana/${branch}/lastbuildcommit
cd ${HOME}/LabsNotary
if [[ ${notary} == "KMD" ]]; then
json="elected.json"
else
json="${branch}.json"
fi
kill -15 $(pgrep -af "iguana ${elected}" | grep -v "$0" | grep -v "SCREEN" | awk '{print $1}')
fi
else
echo "[iguana->${branch}] no update"
fi
done < <(./listlizards.py | uniq)
pids=()
while read -r chain; do
# Move our auto generated coins file to the iguana coins dir
# this file is not auto generated by non KMD daemons and must be created manually
if [[ "${chain}" != "KMD" ]] && [[ -f "${chain}"_7776 ]]; then
chmod +x "${chain}"_7776
mv "${chain}"_7776 iguana/coins
fi
check_chain_started "${chain}" &
pids[${chain}]=$!
if [[ $(cat restart_queue 2> /dev/null | grep "${chain}") == "${chain}" ]]; then
stop_daemon "${chain}"
start_assetchain "${chain}"
echo "[${chain}] : Waiting for ${chain} daemon to restart with blocknotify added to conf..."
check_chain_started "${chain}" &
pids[${chain}]=$!
fi
done < <(./listcoins.sh)
wait_for_last_function ${pids}
pids=()
while read chain; do
checksync "${chain}"
pids[${chain}]=$!
done < <(./listcoins.sh)
wait_for_last_function ${pids}
if [[ ${notary} == "KMD" ]]; then
json="elected.json"
else
iguanajson=$(jq -c '.' <labs.json)
newiguanajson=$(komodo/master/komodo-cli -ac_name=LABS getiguanajson | jq -c '.')
if [[ "${iguanajson}" != "${newiguanajson}" ]]; then
echo "${newiguanajson}" > labs.json
while read -r branch; do
echo "[iguana->${branch}] Updated elected notaries json, stopping iguana..."
kill -15 $(pgrep -af "iguana ${branch}.json" | grep -v "$0" | grep -v "SCREEN" | awk '{print $1}') > /dev/null 2>&1
done < <(./listlizards.py | uniq)
fi
fi
echo -e ${GREEN}"All chains started sucessfully, party time... "${RESET}
while read -r branch; do
./start_iguana.sh "${branch}"
done < <(./listlizards.py | uniq)