This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster
executable file
·273 lines (235 loc) · 9.82 KB
/
cluster
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
#!/usr/bin/env bash
dp_error_flag=0
# Check if jq is installed
if [[ "$1" == "polybft" ]] && ! command -v jq >/dev/null 2>&1; then
echo "jq is not installed."
echo "Manual installation instructions: Visit https://jqlang.github.io/jq/ for more information."
dp_error_flag=1
fi
# Check if curl is installed
if [[ "$1" == "polybft" ]] && ! command -v curl >/dev/null 2>&1; then
echo "curl is not installed."
echo "Manual installation instructions: Visit https://everything.curl.dev/get/ for more information."
dp_error_flag=1
fi
# Check if docker-compose is installed
if [[ "$2" == "--docker" ]] && ! command -v docker-compose >/dev/null 2>&1; then
echo "docker-compose is not installed."
echo "Manual installation instructions: Visit https://docs.docker.com/compose/install/ for more information."
dp_error_flag=1
fi
# Stop script if any of the dependencies have failed
if [[ "$dp_error_flag" -eq 1 ]]; then
echo "Missing dependencies. Please install them and run the script again."
exit 1
fi
function showhelp(){
echo "Usage: cluster {consensus} [{command}] [{flags}]"
echo "Consensus:"
echo " ibft Start Supernets test environment locally with ibft consensus"
echo " polybft Start Supernets test environment locally with polybft consensus"
echo "Commands:"
echo " stop Stop the running environment"
echo " destroy Destroy the running environment"
echo " write-logs Writes STDOUT and STDERR output to log file. Not applicable when using --docker flag."
echo "Flags:"
echo " --docker Run using Docker (requires docker-compose)."
echo " --help Display this help information"
echo "Examples:"
echo " cluster polybft -- Run the script with the polybft consensus"
echo " cluster polybft --docker -- Run the script with the polybft consensus using docker"
echo " cluster polybft stop -- Stop the running environment"
}
function initIbftConsensus() {
echo "Running with ibft consensus"
./polygon-edge secrets init --insecure --data-dir test-chain- --num 4
node1_id=$(./polygon-edge secrets output --data-dir test-chain-1 | grep Node | head -n 1 | awk -F ' ' '{print $4}')
node2_id=$(./polygon-edge secrets output --data-dir test-chain-2 | grep Node | head -n 1 | awk -F ' ' '{print $4}')
genesis_params="--consensus ibft --ibft-validators-prefix-path test-chain- \
--bootnode /ip4/127.0.0.1/tcp/30301/p2p/$node1_id \
--bootnode /ip4/127.0.0.1/tcp/30302/p2p/$node2_id"
}
function initPolybftConsensus() {
echo "Running with polybft consensus"
genesis_params="--consensus polybft"
address1=$(./polygon-edge polybft-secrets --insecure --data-dir test-chain-1 | grep Public | head -n 1 | awk -F ' ' '{print $5}')
address2=$(./polygon-edge polybft-secrets --insecure --data-dir test-chain-2 | grep Public | head -n 1 | awk -F ' ' '{print $5}')
address3=$(./polygon-edge polybft-secrets --insecure --data-dir test-chain-3 | grep Public | head -n 1 | awk -F ' ' '{print $5}')
address4=$(./polygon-edge polybft-secrets --insecure --data-dir test-chain-4 | grep Public | head -n 1 | awk -F ' ' '{print $5}')
}
function createGenesis() {
./polygon-edge genesis $genesis_params \
--block-gas-limit 10000000 \
--premine 0x85da99c8a7c2c95964c8efd687e95e632fc533d6:1000000000000000000000 \
--premine 0x0000000000000000000000000000000000000000 \
--epoch-size 10 \
--reward-wallet 0xDEADBEEF:1000000 \
--native-token-config "Polygon:MATIC:18:true:$address1" \
--burn-contract 0:0x0000000000000000000000000000000000000000 \
--proxy-contracts-admin 0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed
}
function initRootchain() {
echo "Initializing rootchain"
if [ "$1" == "write-logs" ]; then
echo "Writing rootchain server logs to the file..."
./polygon-edge rootchain server 2>&1 | tee ./rootchain-server.log &
else
./polygon-edge rootchain server >/dev/null &
fi
set +e
while true; do
if curl -sSf -o /dev/null http://127.0.0.1:8545; then
break
fi
sleep 1
done
set -e
proxyContractsAdmin=0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed
./polygon-edge polybft stake-manager-deploy \
--jsonrpc http://127.0.0.1:8545 \
--proxy-contracts-admin ${proxyContractsAdmin} \
--test
stakeManagerAddr=$(cat genesis.json | jq -r '.params.engine.polybft.bridge.stakeManagerAddr')
stakeToken=$(cat genesis.json | jq -r '.params.engine.polybft.bridge.stakeTokenAddr')
./polygon-edge rootchain deploy \
--stake-manager ${stakeManagerAddr} \
--stake-token ${stakeToken} \
--proxy-contracts-admin ${proxyContractsAdmin} \
--test
customSupernetManagerAddr=$(cat genesis.json | jq -r '.params.engine.polybft.bridge.customSupernetManagerAddr')
supernetID=$(cat genesis.json | jq -r '.params.engine.polybft.supernetID')
./polygon-edge rootchain fund \
--stake-token ${stakeToken} \
--mint \
--addresses ${address1},${address2},${address3},${address4} \
--amounts 1000000000000000000000000,1000000000000000000000000,1000000000000000000000000,1000000000000000000000000
./polygon-edge polybft whitelist-validators \
--addresses ${address1},${address2},${address3},${address4} \
--supernet-manager ${customSupernetManagerAddr} \
--private-key aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d \
--jsonrpc http://127.0.0.1:8545
counter=1
while [ $counter -le 4 ]; do
echo "Registering validator: ${counter}"
./polygon-edge polybft register-validator \
--supernet-manager ${customSupernetManagerAddr} \
--data-dir test-chain-${counter} \
--jsonrpc http://127.0.0.1:8545
./polygon-edge polybft stake \
--data-dir test-chain-${counter} \
--amount 1000000000000000000000000 \
--supernet-id ${supernetID} \
--stake-manager ${stakeManagerAddr} \
--stake-token ${stakeToken} \
--jsonrpc http://127.0.0.1:8545
((counter++))
done
./polygon-edge polybft supernet \
--private-key aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d \
--supernet-manager ${customSupernetManagerAddr} \
--stake-manager ${stakeManagerAddr} \
--finalize-genesis-set \
--enable-staking \
--jsonrpc http://127.0.0.1:8545
}
function startServerFromBinary() {
if [ "$1" == "write-logs" ]; then
echo "Writing validators logs to the files..."
./polygon-edge server --data-dir ./test-chain-1 --chain genesis.json \
--grpc-address :10000 --libp2p :30301 --jsonrpc :10002 --relayer \
--num-block-confirmations 2 --seal --log-level DEBUG 2>&1 | tee ./validator-1.log &
./polygon-edge server --data-dir ./test-chain-2 --chain genesis.json \
--grpc-address :20000 --libp2p :30302 --jsonrpc :20002 \
--num-block-confirmations 2 --seal --log-level DEBUG 2>&1 | tee ./validator-2.log &
./polygon-edge server --data-dir ./test-chain-3 --chain genesis.json \
--grpc-address :30000 --libp2p :30303 --jsonrpc :30002 \
--num-block-confirmations 2 --seal --log-level DEBUG 2>&1 | tee ./validator-3.log &
./polygon-edge server --data-dir ./test-chain-4 --chain genesis.json \
--grpc-address :40000 --libp2p :30304 --jsonrpc :40002 \
--num-block-confirmations 2 --seal --log-level DEBUG 2>&1 | tee ./validator-4.log &
wait
else
./polygon-edge server --data-dir ./test-chain-1 --chain genesis.json \
--grpc-address :10000 --libp2p :30301 --jsonrpc :10002 --relayer \
--num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-2 --chain genesis.json \
--grpc-address :20000 --libp2p :30302 --jsonrpc :20002 \
--num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-3 --chain genesis.json \
--grpc-address :30000 --libp2p :30303 --jsonrpc :30002 \
--num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-4 --chain genesis.json \
--grpc-address :40000 --libp2p :30304 --jsonrpc :40002 \
--num-block-confirmations 2 --seal --log-level DEBUG &
wait
fi
}
function startServerFromDockerCompose() {
if [ "$1" != "polybft" ]; then
export EDGE_CONSENSUS="$1"
fi
docker-compose -f ./docker/local/docker-compose.yml up -d --build
}
function destroyDockerEnvironment() {
docker-compose -f ./docker/local/docker-compose.yml down -v
}
function stopDockerEnvironment() {
docker-compose -f ./docker/local/docker-compose.yml stop
}
set -e
# Show help if help flag is entered or no arguments are provided
if [[ "$1" == "--help" ]] || [[ $# -eq 0 ]]; then
showhelp
exit 0
fi
# Reset test-dirs
rm -rf test-chain-*
rm -f genesis.json
# Build binary
go build -o polygon-edge .
# If --docker flag is set run docker environment otherwise run from binary
case "$2" in
"--docker")
# cluster {consensus} --docker destroy
if [ "$3" == "destroy" ]; then
destroyDockerEnvironment
echo "Docker $1 environment destroyed!"
exit 0
# cluster {consensus} --docker stop
elif [ "$3" == "stop" ]; then
stopDockerEnvironment
echo "Docker $1 environment stopped!"
exit 0
fi
# cluster {consensus} --docker
echo "Running $1 docker environment..."
startServerFromDockerCompose $1
echo "Docker $1 environment deployed."
exit 0
;;
# cluster {consensus}
*)
echo "Running $1 environment from local binary..."
# Initialize ibft or polybft consensus
if [ "$1" == "ibft" ]; then
# Initialize ibft consensus
initIbftConsensus
# Create genesis file and start the server from binary
createGenesis
startServerFromBinary $2
exit 0
elif [ "$1" == "polybft" ]; then
# Initialize polybft consensus
initPolybftConsensus
# Create genesis file and start the server from binary
createGenesis
initRootchain $2
startServerFromBinary $2
exit 0
else
echo "Unsupported consensus mode. Supported modes are: ibft and polybft."
showhelp
exit 1
fi
;;
esac