forked from KMDLabs/LabsNotary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·265 lines (242 loc) · 7.82 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
#!/bin/bash
longestchain () {
chain=$1
if [[ $chain == "KMD" ]]; then
chain=""
fi
tries=0
longestchain=0
while [[ $longestchain -eq 0 ]]; do
info=$(komodo-cli -ac_name=$chain getinfo)
longestchain=$(echo ${info} | jq -r '.longestchain')
tries=$(( $tries +1 ))
if (( $tries > 60)); then
echo "0"
return 0
fi
sleep 1
done
echo $longestchain
return 1
}
checksync () {
chain=$1
if [[ $chain == "KMD" ]]; then
chain=""
fi
lc=$(longestchain $1)
if [[ $lc = "0" ]]; then
connections=$(komodo-cli -ac_name=$chain getinfo | jq -r .connections)
if [[ $connections = "0" ]]; then
echo -e "\033[1;31m [$1] ABORTING - $1 has no network connections, Help Human! \033[0m"
komodo-cli -ac_name=$chain stop
return 0
else
lc=$(longestchain $1)
fi
fi
if [[ $lc = "0" ]]; then
blocks=$(komodo-cli -ac_name=$chain getblockcount)
tries=0
while (( $blocks < 128 )) && (( $tries < 90 )); do
echo "[$1] $blocks blocks"
blocks=$(komodo-cli -ac_name=$chain getblockcount)
tries=$(( $tries +1 ))
lc=$(longestchain $1)
if (( $blocks == $lc )); then
echo "[$1] Synced on block: $lc"
return 1
fi
done
if (( blocks == 0 )) && (( lc == 0 )); then
# this chain is just not syncing even though it has network connections we will stop its deamon and abort for now. Myabe next time it will work.
komodo-cli -ac_name=$chain stop
echo -e "\033[1;31m [$1] ABORTING no blocks or longest chain found, Help Human! \033[0m"
return 0
elif (( blocks == 0 )) && (( lc != 0 )); then
# This chain has connections and knows longest chain, but will not sync, we will kill it. Maybe next time it will work.
echo -e "\033[1;31m [$1] ABORTING - No blocks synced of $lc. Help Human! \033[0m"
komodo-cli -ac_name=$chain stop
return 0
elif (( blocks > 128 )) && (( lc == 0 )); then
# This chain is syncing but does not have longest chain. Likey it is just stalled. Start anyway.
echo -e "\033[1;31m [$1] Synced to $blocks, but no longest chain is found. Starting anyway.\033[0m"
return 1
fi
fi
blocks=$(komodo-cli -ac_name=$chain getblockcount)
while (( $blocks < $lc )); do
sleep 60
lc=$(longestchain $1)
blocks=$(komodo-cli -ac_name=$chain getblockcount)
progress=$(echo "scale=3;$blocks/$lc" | bc -l)
echo "[$1] $(echo $progress*100|bc)% $blocks of $lc"
done
echo "[$1] Synced on block: $lc"
return 1
}
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)
outcome=$(ps -p $pid 2> /dev/null | grep komodod)
if [[ ${outcome} == "" ]]; then
rm $pidfile
fi
sleep 2
done
}
cd /home/$USER/StakedNotary
export LABS_FLAGS="ENABLE_LABS"
git pull
pubkey=$(./printkey.py pub)
Radd=$(./printkey.py Radd)
privkey=$(./printkey.py wif)
if [[ ${#pubkey} != 66 ]]; then
echo -e "\033[1;31m ABORTING!!! pubkey invalid: Please check your config.ini \033[0m"
exit 1
fi
if [[ ${#Radd} != 34 ]]; then
echo -e "\033[1;31m [$1] ABORTING!!! R-address invalid: Please check your config.ini \033[0m"
exit 1
fi
if [[ ${#privkey} != 52 ]]; then
echo -e "\033[1;31m [$1] ABORTING!!! WIF-key invalid: Please check your config.ini \033[0m"
exit 1
fi
ac_json=$(cat assetchains.json)
echo $ac_json | jq .[] > /dev/null 2>&1
outcome=$(echo $?)
if [[ $outcome != 0 ]]; then
echo -e "\033[1;31m ABORTING!!! assetchains.json is invalid, Help Human! \033[0m"
exit 1
fi
# Here we will update/add the master branch of StakedNotary/komodo StakedNotary/komodo/<branch>
# and stop komodo if it was updated
echo "[master] Checking for updates and building if required..."
result=$(./update_komodo.sh master)
if [[ $result = "updated" ]]; then
echo "[master] Updated to latest"
master_updated=1
echo "[KMD] Stopping ..."
komodo-cli stop > /dev/null 2>&1
daemon_stopped "KMD"
echo "[KMD] Stopped."
elif [[ $result = "update_failed" ]]; then
echo -e "\033[1;31m [master] ABORTING!!! failed to update, Help Human! \033[0m"
exit 1
else
echo "[master] No update required"
fi
# Here we will extract all branches in assetchain.json and build them and move them to StakedNotary/komodo/<branch>
# and stop any staked chains that use master branch if it was updated
i=0
./listbranches.py | while read branch; do
if [[ $branch != "master" ]]; then
echo "[$branch] Checking for updates and building if required..."
result=$(./update_komodo.sh $branch)
if [[ $result = "updated" ]]; then
echo "[$branch] Updated to latest"
updated_chain=$(echo "${ac_json}" | jq -r .[$i].ac_name)
echo "[$updated_chain] Stopping ..."
komodo-cli -ac_name=$updated_chain stop > /dev/null 2>&1
daemon_stopped "${updated_chain}"
echo "[$updated_chain] Stopped."
elif [[ $result = "update_failed" ]]; then
echo -e "\033[1;31m [$branch] ABORTING!!! failed to update please build manually using ~/komodo/zcutil/build.sh to see what problem is! Help Human! \033[0m"
exit 1
else
echo "[$branch] No update required"
fi
elif [[ $master_updated = 1 ]]; then
updated_chain=$(echo "${ac_json}" | jq -r .[$i].ac_name)
echo "[$updated_chain] Stopping ..."
komodo-cli -ac_name=$updated_chain stop > /dev/null 2>&1
daemon_stopped "${updated_chain}"
echo "[$updated_chain] Stopped."
fi
i=$(( $i +1 ))
done
# Start KMD
echo "[KMD] : Starting KMD"
$HOME/StakedNotary/komodo/master/komodod -stakednotary=1 -pubkey=$pubkey > /dev/null 2>&1 &
# Start assets
if [[ $(./assetchains $1) = "finished" ]]; then
echo "Started Assetchains"
else
echo -e "\033[1;31m Starting Assetchains Failed: help human! \033[0m"
exit 1
fi
# Validate Address on KMD + AC, will poll deamon until started then check if address is imported, if not import it.
echo "[KMD] : Waiting for KMD daemon to start..."
./validateaddress.sh KMD
validateaddress=$(komodo-cli validateaddress $Radd 2> /dev/null)
outcome=$(echo $?)
if [[ ${outcome} -eq 1 ]]; then
echo -e "\033[1;31m Starting KMD Failed: help human! \033[0m"
exit 1
fi
abort=0
./listassetchains.py | while read chain; do
# Move our auto generated coins file to the iguana coins dir
chmod +x "$chain"_7776
mv "$chain"_7776 iguana/coins
echo "[$chain] : Waiting for $chain daemon to start..."
./validateaddress.sh $chain
validateaddress=$(komodo-cli -ac_name=$chain validateaddress $Radd 2> /dev/null)
outcome=$(echo $?)
if [[ ${outcome} -eq 1 ]]; then
echo -e "\033[1;31m Starting $chain Failed: help human! \033[0m"
echo "abort=1" > abort
fi
done
source abort 2> /dev/null
rm abort 2> /dev/null
if [[ $abort -eq 1 ]]; then
exit 1
fi
cd ~/SuperNET
returnstr=$(git pull)
cd /home/$USER/StakedNotary
if [[ $returnstr = "Already up-to-date." ]]; then
echo "No Iguana update detected"
else
rm iguana/iguana
fi
if [[ ! -f iguana/iguana ]]; then
echo "Building iguana"
./build_iguana
pkill -15 iguana
fi
echo "Checking chains are in sync..."
abort=0
checksync KMD
outcome=$(echo $?)
if [[ $outcome = 0 ]]; then
abort=1
fi
for row in $(echo "${ac_json}" | jq -r '.[].ac_name'); do
checksync $row
outcome=$(echo $?)
if [[ $outcome = 0 ]]; then
abort=1
fi
done
iguanajson=$(cat staked.json | jq -c '.' )
newiguanajson=$(komodo/master/komodo-cli getiguanajson | jq -c '.')
if [ "$iguanajson" != "$newiguanajson" ]; then
echo $newiguanajson > staked.json
pkill -15 iguana
sleep 2
fi
if [[ $abort -eq 0 ]]; then
echo -e "\033[1;32m ALL CHAINS SYNC'd Starting Iguana if it needs starting then adding new chains for dPoW... \033[0m"
else
echo -e "\033[1;31m Something went wrong, please check error messages above requiring human help and manually rectify them before starting iguana! \033[0m"
exit 1
fi
./start_iguana.sh