forked from bnb-chain/node-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsc_cluster.sh
More file actions
359 lines (328 loc) · 16 KB
/
bsc_cluster.sh
File metadata and controls
359 lines (328 loc) · 16 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
# Exit script on error
set -e
basedir=$(
cd $(dirname $0)
pwd
)
workspace=${basedir}
source ${workspace}/.env
size=$((BSC_CLUSTER_SIZE))
stateScheme="hash"
dbEngine="leveldb"
gcmode="full"
sleepBeforeStart=15
sleepAfterStart=10
# stop geth client
function exit_previous() {
ValIdx=$1
ps -ef | grep geth$ValIdx | grep config |awk '{print $2}' | xargs -r kill
sleep ${sleepBeforeStart}
}
# Check if validator is a file or directory and handle accordingly
function handle_validator() {
local validator_path=$1
local index=$2
local target_dir=$3
if [ -f "${validator_path}" ]; then
# It's a file containing the operator address
echo "Validator ${index} is a file, copying operator address file"
cp ${validator_path} ${target_dir}/
elif [ -d "${validator_path}" ]; then
# It's a directory containing the keystore
echo "Validator ${index} is a directory, copying keystore directory"
cp -r ${validator_path} ${workspace}/.local/
else
echo "Error: Validator ${index} is neither a file nor a directory"
exit 1
fi
}
function create_validator() {
rm -rf ${workspace}/.local
mkdir -p ${workspace}/.local
for ((i = 0; i < size; i++)); do
# Handle validator based on whether it's a file or directory
handle_validator "${workspace}/keys/validator${i}" ${i} "${workspace}/.local/validator${i}"
cp -r ${workspace}/keys/consensus${i} ${workspace}/.local/
cp -r ${workspace}/keys/bls${i} ${workspace}/.local/
done
}
function prepare_bsc_client() {
if [ ${useLatestBscClient} = true ]; then
if [ ! -f "${workspace}/bsc/Makefile" ]; then
cd ${workspace}
git clone https://github.com/bnb-chain/bsc.git
fi
cd ${workspace}/bsc && git pull && make geth && mv -f ${workspace}/bsc/build/bin/geth ${workspace}/bin/
fi
}
# reset genesis, but keep edited genesis-template.json
function reset_genesis() {
if [ ! -f "${workspace}/genesis/genesis-template.json" ]; then
cd ${workspace} && git submodule update --init --recursive genesis
cd ${workspace}/genesis && git reset --hard ${GENESIS_COMMIT}
cd ${workspace}/genesis
cp genesis-template.json genesis-template.json.bk
cp scripts/init_holders.template scripts/init_holders.template.bk
git stash
cd ${workspace} && git submodule update --remote --recursive genesis && cd ${workspace}/genesis
git reset --hard ${GENESIS_COMMIT}
mv genesis-template.json.bk genesis-template.json
mv scripts/init_holders.template.bk scripts/init_holders.template
poetry install --no-root
npm install
rm -rf lib/forge-std
forge install --no-git foundry-rs/forge-std@v1.7.3
cd lib/forge-std/lib
rm -rf ds-test
git clone https://github.com/dapphub/ds-test
fi
}
function prepare_config() {
rm -f ${workspace}/genesis/validators.conf
passedHardforkTime=$(expr $(date +%s) + ${PASSED_FORK_DELAY})
echo "passedHardforkTime "${passedHardforkTime} > ${workspace}/.local/hardforkTime.txt
initHolders=${INIT_HOLDER}
for ((i = 0; i < size; i++)); do
# Check if validator is a file or directory and extract operator address accordingly
if [ -f "${workspace}/keys/validator${i}" ]; then
# Read operator address from the file
operator_addr=$(cat ${workspace}/keys/validator${i})
elif [ -d "${workspace}/keys/validator${i}" ]; then
# Extract operator address from keystore
for f in ${workspace}/keys/validator${i}/keystore/*; do
operator_addr="0x$(cat ${f} | jq -r .address)"
done
else
echo "Error: Validator ${i} is neither a file nor a directory"
exit 1
fi
initHolders=${initHolders}","${operator_addr}
fee_addr=${operator_addr}
for f in ${workspace}/.local/consensus${i}/keystore/*; do
cons_addr="0x$(cat ${f} | jq -r .address)"
done
targetDir=${workspace}/.local/node${i}
mkdir -p ${targetDir} && cd ${targetDir}
cp ${workspace}/keys/password.txt ./
cp ${workspace}/.local/hardforkTime.txt ./
bbcfee_addrs=${fee_addr}
powers="0x000001d1a94a2000" #2000000000000
mv ${workspace}/.local/bls${i}/bls ./ && rm -rf ${workspace}/.local/bls${i}
vote_addr=0x$(cat ./bls/keystore/*json | jq .pubkey | sed 's/"//g')
echo "${cons_addr},${bbcfee_addrs},${fee_addr},${powers},${vote_addr}" >> ${workspace}/genesis/validators.conf
if [ ${EnableSentryNode} = true ]; then
mkdir -p ${workspace}/.local/sentry${i}
fi
done
if [ ${EnableFullNode} = true ]; then
mkdir -p ${workspace}/.local/fullnode0
fi
rm -f ${workspace}/.local/hardforkTime.txt
cd ${workspace}/genesis/
git checkout HEAD contracts
sed -i -e 's/alreadyInit = true;/turnLength = 16;alreadyInit = true;/' ${workspace}/genesis/contracts/BSCValidatorSet.sol
sed -i -e 's/public onlyCoinbase onlyZeroGasPrice {/public onlyCoinbase onlyZeroGasPrice {if (block.number < 300) return;/' ${workspace}/genesis/contracts/BSCValidatorSet.sol
poetry run python -m scripts.generate generate-validators
poetry run python -m scripts.generate generate-init-holders "${initHolders}"
poetry run python -m scripts.generate dev \
--dev-chain-id "${CHAIN_ID}" \
--init-burn-ratio "1000" \
--init-felony-slash-scope "60" \
--breathe-block-interval "10 minutes" \
--block-interval "3 seconds" \
--stake-hub-protector "${INIT_HOLDER}" \
--unbond-period "2 minutes" \
--downtime-jail-time "2 minutes" \
--felony-jail-time "3 minutes" \
--misdemeanor-threshold "50" \
--felony-threshold "150" \
--init-voting-period "2 minutes / BLOCK_INTERVAL" \
--init-min-period-after-quorum "uint64(1 minutes / BLOCK_INTERVAL)" \
--governor-protector "${INIT_HOLDER}" \
--init-minimal-delay "1 minutes" \
--token-recover-portal-protector "${INIT_HOLDER}"
}
function initNetwork() {
cd ${workspace}
for ((i = 0; i < size; i++)); do
mkdir ${workspace}/.local/node${i}/geth
cp ${workspace}/keys/validator-nodekey${i} ${workspace}/.local/node${i}/geth/nodekey
# Handle validator based on whether it's a file or directory
if [ -d "${workspace}/.local/validator${i}" ]; then
# Old format: validator is a directory with keystore
mv ${workspace}/.local/validator${i}/keystore ${workspace}/.local/node${i}/ && rm -rf ${workspace}/.local/validator${i}
else
# New format: validator is a file with operator address
# In this case, we don't need to move anything to node directory
rm -rf ${workspace}/.local/validator${i}
fi
mv ${workspace}/.local/consensus${i}/keystore ${workspace}/.local/node${i}/ && rm -rf ${workspace}/.local/consensus${i}
if [ ${EnableSentryNode} = true ]; then
mkdir ${workspace}/.local/sentry${i}/geth
cp ${workspace}/keys/sentry-nodekey${i} ${workspace}/.local/sentry${i}/geth/nodekey
fi
done
if [ ${EnableFullNode} = true ]; then
mkdir ${workspace}/.local/fullnode0/geth
cp ${workspace}/keys/fullnode-nodekey0 ${workspace}/.local/fullnode0/geth/nodekey
fi
init_extra_args=""
if [ ${EnableSentryNode} = true ]; then
init_extra_args="--init.sentrynode-size ${size} --init.sentrynode-ports 30411"
fi
if [ ${EnableFullNode} = true ]; then
init_extra_args="${init_extra_args} --init.fullnode-size 1 --init.fullnode-ports 30511"
fi
if [ "${RegisterNodeID}" = true ]; then
if [ "${EnableSentryNode}" = true ]; then
init_extra_args="${init_extra_args} --init.evn-sentry-register"
else
init_extra_args="${init_extra_args} --init.evn-validator-register"
fi
fi
if [ "${EnableEVNWhitelist}" = true ]; then
if [ "${EnableSentryNode}" = true ]; then
init_extra_args="${init_extra_args} --init.evn-sentry-whitelist"
else
init_extra_args="${init_extra_args} --init.evn-validator-whitelist"
fi
fi
${workspace}/bin/geth init-network --init.dir ${workspace}/.local --init.size=${size} --config ${workspace}/config.toml ${init_extra_args} ${workspace}/genesis/genesis.json
rm -f ${workspace}/*bsc.log*
for ((i = 0; i < size; i++)); do
sed -i -e '/"<nil>"/d' ${workspace}/.local/node${i}/config.toml
# init genesis
initLog=${workspace}/.local/node${i}/init.log
if [ $i -eq 0 ] ; then
${workspace}/bin/geth --datadir ${workspace}/.local/node${i} init --state.scheme ${stateScheme} --db.engine ${dbEngine} ${workspace}/genesis/genesis.json > "${initLog}" 2>&1
elif [ $i -eq 1 ] ; then
${workspace}/bin/geth --datadir ${workspace}/.local/node${i} init --state.scheme path --db.engine pebble --multidatabase ${workspace}/genesis/genesis.json > "${initLog}" 2>&1
else
${workspace}/bin/geth --datadir ${workspace}/.local/node${i} init --state.scheme path --db.engine pebble ${workspace}/genesis/genesis.json > "${initLog}" 2>&1
fi
rm -f ${workspace}/.local/node${i}/*bsc.log*
if [ ${EnableSentryNode} = true ]; then
sed -i -e '/"<nil>"/d' ${workspace}/.local/sentry${i}/config.toml
initLog=${workspace}/.local/sentry${i}/init.log
${workspace}/bin/geth --datadir ${workspace}/.local/sentry${i} init --state.scheme path --db.engine pebble ${workspace}/genesis/genesis.json > "${initLog}" 2>&1
rm -f ${workspace}/.local/sentry${i}/*bsc.log*
fi
done
if [ ${EnableFullNode} = true ]; then
sed -i -e '/"<nil>"/d' ${workspace}/.local/fullnode0/config.toml
sed -i -e 's/EnableEVNFeatures = true/EnableEVNFeatures = false/g' ${workspace}/.local/fullnode0/config.toml
initLog=${workspace}/.local/fullnode0/init.log
${workspace}/bin/geth --datadir ${workspace}/.local/fullnode0 init --state.scheme path --db.engine pebble ${workspace}/genesis/genesis.json > "${initLog}" 2>&1
rm -f ${workspace}/.local/fullnode0/*bsc.log*
fi
}
function native_start() {
PassedForkTime=`cat ${workspace}/.local/node0/hardforkTime.txt|grep passedHardforkTime|awk -F" " '{print $NF}'`
LastHardforkTime=$(expr ${PassedForkTime} + ${LAST_FORK_MORE_DELAY})
rialtoHash=`cat ${workspace}/.local/node0/init.log|grep "database=chaindata"|awk -F"=" '{print $NF}'|awk -F'"' '{print $1}'`
ValIdx=$1
for ((i = 0; i < size; i++));do
if [ ! -z $ValIdx ] && [ $i -ne $ValIdx ]; then
continue
fi
for j in ${workspace}/.local/node${i}/keystore/*;do
cons_addr="0x$(cat ${j} | jq -r .address)"
done
HTTPPort=$((8545 + i*2))
WSPort=${HTTPPort}
MetricsPort=$((6060 + i*2))
PProfPort=$((7060 + i*2))
# geth may be replaced
cp ${workspace}/bin/geth ${workspace}/.local/node${i}/geth${i}
# update `config` in genesis.json
# ${workspace}/.local/node${i}/geth${i} dumpgenesis --datadir ${workspace}/.local/node${i} | jq . > ${workspace}/.local/node${i}/genesis.json
# run BSC node
nohup ${workspace}/.local/node${i}/geth${i} --config ${workspace}/.local/node${i}/config.toml \
--mine --vote --password ${workspace}/.local/node${i}/password.txt --unlock ${cons_addr} --miner.etherbase ${cons_addr} --blspassword ${workspace}/.local/node${i}/password.txt \
--datadir ${workspace}/.local/node${i} \
--nodekey ${workspace}/.local/node${i}/geth/nodekey \
--rpc.allow-unprotected-txs --allow-insecure-unlock \
--ws.addr 0.0.0.0 --ws.port ${WSPort} --http.addr 0.0.0.0 --http.port ${HTTPPort} --http.corsdomain "*" \
--metrics --metrics.addr localhost --metrics.port ${MetricsPort} --metrics.expensive \
--pprof --pprof.addr localhost --pprof.port ${PProfPort} \
--gcmode ${gcmode} --syncmode full --monitor.maliciousvote \
--override.passedforktime ${PassedForkTime} --override.lorentz ${PassedForkTime} --override.maxwell ${LastHardforkTime} \
--override.immutabilitythreshold ${FullImmutabilityThreshold} --override.breatheblockinterval ${BreatheBlockInterval} \
--override.minforblobrequest ${MinBlocksForBlobRequests} --override.defaultextrareserve ${DefaultExtraReserveForBlobRequests} \
>> ${workspace}/.local/node${i}/bsc-node.log 2>&1 &
if [ ${EnableSentryNode} = true ]; then
cp ${workspace}/bin/geth ${workspace}/.local/sentry${i}/geth${i}
nohup ${workspace}/.local/sentry${i}/geth${i} --config ${workspace}/.local/sentry${i}/config.toml \
--datadir ${workspace}/.local/sentry${i} \
--nodekey ${workspace}/.local/sentry${i}/geth/nodekey \
--rpc.allow-unprotected-txs --allow-insecure-unlock \
--ws.addr 0.0.0.0 --ws.port $((WSPort+1)) --http.addr 0.0.0.0 --http.port $((HTTPPort+1)) --http.corsdomain "*" \
--metrics --metrics.addr localhost --metrics.port $((MetricsPort+1)) --metrics.expensive \
--pprof --pprof.addr localhost --pprof.port $((PProfPort+1)) \
--gcmode ${gcmode} --syncmode full --monitor.maliciousvote \
--override.passedforktime ${PassedForkTime} --override.lorentz ${PassedForkTime} --override.maxwell ${LastHardforkTime} \
--override.immutabilitythreshold ${FullImmutabilityThreshold} --override.breatheblockinterval ${BreatheBlockInterval} \
--override.minforblobrequest ${MinBlocksForBlobRequests} --override.defaultextrareserve ${DefaultExtraReserveForBlobRequests} \
>> ${workspace}/.local/sentry${i}/bsc-node.log 2>&1 &
fi
done
if [ ${EnableFullNode} = true ]; then
cp ${workspace}/bin/geth ${workspace}/.local/fullnode0/geth0
nohup ${workspace}/.local/fullnode0/geth0 --config ${workspace}/.local/fullnode0/config.toml \
--datadir ${workspace}/.local/fullnode0 \
--nodekey ${workspace}/.local/fullnode0/geth/nodekey \
--rpc.allow-unprotected-txs --allow-insecure-unlock \
--ws.addr 0.0.0.0 --ws.port $((8645)) --http.addr 0.0.0.0 --http.port $((8645)) --http.corsdomain "*" \
--metrics --metrics.addr localhost --metrics.port $((6160)) --metrics.expensive \
--pprof --pprof.addr localhost --pprof.port $((7160)) \
--gcmode ${gcmode} --syncmode full --monitor.maliciousvote \
--override.immutabilitythreshold ${FullImmutabilityThreshold} --override.breatheblockinterval ${BreatheBlockInterval} \
--override.minforblobrequest ${MinBlocksForBlobRequests} --override.defaultextrareserve ${DefaultExtraReserveForBlobRequests} \
>> ${workspace}/.local/fullnode0/bsc-node.log 2>&1 &
fi
sleep ${sleepAfterStart}
}
function register_stakehub(){
if [ -f "${workspace}/keys/validator${i}" ];then
exit 1
fi
# wait feynman enable
sleep 45
for ((i = 0; i < size; i++));do
${workspace}/create-validator/create-validator --operator-key-dir ${workspace}/keys/validator${i} --consensus-key-dir ${workspace}/keys/consensus${i} --vote-key-dir ${workspace}/keys/bls${i} \
--password-path ${workspace}/keys/password.txt --amount 20001 --validator-desc Val${i} --rpc-url ${RPC_URL}
done
}
CMD=$1
ValidatorIdx=$2
case ${CMD} in
reset)
exit_previous
create_validator
prepare_bsc_client
reset_genesis
prepare_config
initNetwork
native_start
register_stakehub
;;
stop)
exit_previous $ValidatorIdx
;;
start)
native_start $ValidatorIdx
;;
restart)
exit_previous $ValidatorIdx
native_start $ValidatorIdx
;;
regen-genesis)
create_validator
reset_genesis
prepare_config
;;
*)
echo "Usage: bsc_cluster.sh | reset | stop [vidx]| start [vidx]| restart [vidx] | regen-genesis"
;;
esac