Skip to content

Commit

Permalink
Revise and fix cluster script when ran in docker mode (0xPolygon#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Jun 1, 2023
1 parent fdc3467 commit 3b827b8
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 40 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ compile-core-contracts:
generate-smart-contract-bindings:
go run ./consensus/polybft/contractsapi/artifacts-gen/main.go
go run ./consensus/polybft/contractsapi/bindings-gen/main.go

.PHONY: run-docker
run-docker:
./scripts/cluster polybft --docker

.PHONY: stop-docker
stop-docker:
./scripts/cluster polybft --docker stop

.PHONY: destroy-docker
destroy-docker:
./scripts/cluster polybft --docker destroy
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

if err != nil {
return err
return fmt.Errorf("faield to get deployer key: %w", err)
}

txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(params.jsonRPC))
Expand All @@ -124,7 +124,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
txn := &ethgo.Transaction{To: &deployerAddr, Value: ethgo.Ether(1)}

if _, err = txRelayer.SendTransactionLocal(txn); err != nil {
return err
return fmt.Errorf("faield to send local transaction: %w", err)
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
// write updated chain configuration
chainConfig.Params.Engine[polybft.ConsensusName] = consensusConfig

if err := helper.WriteGenesisConfigToDisk(chainConfig, params.genesisPath); err != nil {
if err = helper.WriteGenesisConfigToDisk(chainConfig, params.genesisPath); err != nil {
return fmt.Errorf("failed to save chain configuration bridge data: %w", err)
}

Expand Down
84 changes: 78 additions & 6 deletions docker/local/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
version: '3.9'

services:
## INITIALIZE GENESIS AND SECRETS
## RUN ROOTCHAIN
rootchain:
image: ghcr.io/0xpolygon/go-ethereum-console:latest
container_name: polygon-edge-rootchain
command: [
"--dev",
"--dev.period", "2",
"--datadir", "/eth1data",
"--ipcpath", "/eth1data/geth.ipc",
"--http",
"--http.addr", "0.0.0.0",
"--http.vhosts", "*",
"--http.api", "eth,net,web3,debug",
"--ws",
"--ws.addr", "0.0.0.0"
]
ports:
- '8545:8545'
volumes:
- eth1data:/eth1data
networks:
- polygon-edge-docker

## INITIALIZE GENESIS AND SECRETS, ETC.
init:
build:
context: ../../
dockerfile: docker/local/Dockerfile
image: local/polygon-edge
container_name: polygon-edge-bootstrapper
command: [ "init", "${EDGE_CONSENSUS:-ibft}" ]
command: [ "init", "${EDGE_CONSENSUS:-polybft}" ]
depends_on:
rootchain:
condition: service_started
volumes:
- data:/data
networks:
Expand All @@ -18,10 +44,21 @@ services:
node-1:
image: local/polygon-edge
container_name: polygon-edge-validator-1
command: ["server", "--data-dir", "/data/data-1", "--chain", "/data/genesis.json", "--grpc-address", "0.0.0.0:9632", "--libp2p", "0.0.0.0:1478", "--jsonrpc", "0.0.0.0:8545", "--prometheus", "0.0.0.0:5001", "--seal"]
command: [
"server",
"--data-dir", "/data/data-1",
"--chain", "/data/genesis.json",
"--grpc-address", "0.0.0.0:9632",
"--libp2p", "0.0.0.0:1478",
"--jsonrpc", "0.0.0.0:8545",
"--prometheus", "0.0.0.0:5001",
"--seal"
]
depends_on:
init:
condition: service_completed_successfully
rootchain:
condition: service_started
ports:
- '10000:9632'
- '10002:8545'
Expand All @@ -35,10 +72,22 @@ services:
node-2:
image: local/polygon-edge
container_name: polygon-edge-validator-2
command: ["server", "--data-dir", "/data/data-2", "--chain", "/data/genesis.json", "--grpc-address", "0.0.0.0:9632", "--libp2p", "0.0.0.0:1478", "--jsonrpc", "0.0.0.0:8545", "--prometheus", "0.0.0.0:5001", "--seal"]
command: [
"server",
"--data-dir",
"/data/data-2",
"--chain", "/data/genesis.json",
"--grpc-address", "0.0.0.0:9632",
"--libp2p", "0.0.0.0:1478",
"--jsonrpc", "0.0.0.0:8545",
"--prometheus", "0.0.0.0:5001",
"--seal"
]
depends_on:
init:
condition: service_completed_successfully
rootchain:
condition: service_started
ports:
- '20000:9632'
- '20002:8545'
Expand All @@ -52,10 +101,21 @@ services:
node-3:
image: local/polygon-edge
container_name: polygon-edge-validator-3
command: ["server", "--data-dir", "/data/data-3", "--chain", "/data/genesis.json", "--grpc-address", "0.0.0.0:9632", "--libp2p", "0.0.0.0:1478", "--jsonrpc", "0.0.0.0:8545", "--prometheus", "0.0.0.0:5001", "--seal"]
command: [
"server",
"--data-dir", "/data/data-3",
"--chain", "/data/genesis.json",
"--grpc-address", "0.0.0.0:9632",
"--libp2p", "0.0.0.0:1478",
"--jsonrpc", "0.0.0.0:8545",
"--prometheus", "0.0.0.0:5001",
"--seal"
]
depends_on:
init:
condition: service_completed_successfully
rootchain:
condition: service_started
ports:
- '30000:9632'
- '30002:8545'
Expand All @@ -69,10 +129,21 @@ services:
node-4:
image: local/polygon-edge
container_name: polygon-edge-validator-4
command: ["server", "--data-dir", "/data/data-4", "--chain", "/data/genesis.json", "--grpc-address", "0.0.0.0:9632", "--libp2p", "0.0.0.0:1478", "--jsonrpc", "0.0.0.0:8545", "--prometheus", "0.0.0.0:5001", "--seal"]
command: [
"server",
"--data-dir", "/data/data-4",
"--chain", "/data/genesis.json",
"--grpc-address", "0.0.0.0:9632",
"--libp2p", "0.0.0.0:1478",
"--jsonrpc", "0.0.0.0:8545",
"--prometheus", "0.0.0.0:5001",
"--seal"
]
depends_on:
init:
condition: service_completed_successfully
rootchain:
condition: service_started
ports:
- '40000:9632'
- '40002:8545'
Expand All @@ -90,4 +161,5 @@ networks:

volumes:
data:
eth1data:
genesis:
111 changes: 90 additions & 21 deletions docker/local/polygon-edge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,120 @@ CHAIN_CUSTOM_OPTIONS=$(tr "\n" " " << EOL
--epoch-size 10
--chain-id 51001
--name polygon-edge-docker
--burn-contract 0:0x0000000000000000000000000000000000000000
--premine 0x228466F2C715CbEC05dEAbfAc040ce3619d7CF0B:0xD3C21BCECCEDA1000000
--premine 0xca48694ebcB2548dF5030372BE4dAad694ef174e:0xD3C21BCECCEDA1000000
EOL
)

case "$1" in

"init")
case "$2" in
"ibft")
if [ -f "$GENESIS_PATH" ]; then
echo "Secrets have already been generated."
else
echo "Generating secrets..."
secrets=$("$POLYGON_EDGE_BIN" secrets init --insecure --num 4 --data-dir /data/data- --json)
echo "Secrets have been successfully generated"
echo "Generating IBFT Genesis file..."
cd /data && /polygon-edge/polygon-edge genesis $CHAIN_CUSTOM_OPTIONS \
--dir genesis.json \
--consensus ibft \
--ibft-validators-prefix-path data- \
--bootnode "/dns4/node-1/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[0] | .node_id')" \
--bootnode "/dns4/node-2/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[1] | .node_id')"
fi
"ibft")
if [ -f "$GENESIS_PATH" ]; then
echo "Secrets have already been generated."
else
echo "Generating IBFT secrets..."
secrets=$("$POLYGON_EDGE_BIN" secrets init --insecure --num 4 --data-dir /data/data- --json)
echo "Secrets have been successfully generated"

rm -f /data/genesis.json

echo "Generating IBFT Genesis file..."
"$POLYGON_EDGE_BIN" genesis $CHAIN_CUSTOM_OPTIONS \
--dir /data/genesis.json \
--consensus ibft \
--ibft-validators-prefix-path data- \
--bootnode "/dns4/node-1/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[0] | .node_id')" \
--bootnode "/dns4/node-2/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[1] | .node_id')" \
--bootnode "/dns4/node-3/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[2] | .node_id')" \
--bootnode "/dns4/node-4/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[3] | .node_id')"
fi
;;
"polybft")
echo "Generating PolyBFT secrets..."
secrets=$("$POLYGON_EDGE_BIN" polybft-secrets init --insecure --num 4 --data-dir /data/data- --json)
echo "Secrets have been successfully generated"

echo "Generating PolyBFT Genesis file..."
rm -f /data/genesis.json

echo "Generating PolyBFT genesis file..."
"$POLYGON_EDGE_BIN" genesis $CHAIN_CUSTOM_OPTIONS \
--dir /data/genesis.json \
--consensus polybft \
--validators-path /data \
--validators-path /data \
--validators-prefix data- \
--reward-wallet 0xDEADBEEF:1000000 \
--bootnode "/dns4/node-1/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[0] | .node_id')" \
--bootnode "/dns4/node-2/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[1] | .node_id')"
--bootnode "/dns4/node-2/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[1] | .node_id')" \
--bootnode "/dns4/node-3/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[2] | .node_id')" \
--bootnode "/dns4/node-4/tcp/1478/p2p/$(echo "$secrets" | jq -r '.[3] | .node_id')"

echo "Deploying stake manager..."
"$POLYGON_EDGE_BIN" polybft stake-manager-deploy \
--jsonrpc http://rootchain:8545 \
--genesis /data/genesis.json \
--test

stakeManagerAddr=$(cat /data/genesis.json | jq -r '.params.engine.polybft.bridge.stakeManagerAddr')
stakeToken=$(cat /data/genesis.json | jq -r '.params.engine.polybft.bridge.stakeTokenAddr')

"$POLYGON_EDGE_BIN" rootchain deploy \
--stake-manager ${stakeManagerAddr} \
--json-rpc http://rootchain:8545 \
--genesis /data/genesis.json \
--test

customSupernetManagerAddr=$(cat /data/genesis.json | jq -r '.params.engine.polybft.bridge.customSupernetManagerAddr')
supernetID=$(cat /data/genesis.json | jq -r '.params.engine.polybft.supernetID')
addresses="$(echo "$secrets" | jq -r '.[0] | .address'),$(echo "$secrets" | jq -r '.[1] | .address'),$(echo "$secrets" | jq -r '.[2] | .address'),$(echo "$secrets" | jq -r '.[3] | .address')"

"$POLYGON_EDGE_BIN" rootchain fund \
--json-rpc http://rootchain:8545 \
--stake-token ${stakeToken} \
--mint \
--addresses ${addresses} \
--amounts 1000000000000000000000000,1000000000000000000000000,1000000000000000000000000,1000000000000000000000000

"$POLYGON_EDGE_BIN" polybft whitelist-validators \
--addresses ${addresses} \
--supernet-manager ${customSupernetManagerAddr} \
--private-key aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d \
--jsonrpc http://rootchain:8545

counter=1
while [ $counter -le 4 ]; do
echo "Registering validator: ${counter}"

"$POLYGON_EDGE_BIN" polybft register-validator \
--supernet-manager ${customSupernetManagerAddr} \
--data-dir /data/data-${counter} \
--jsonrpc http://rootchain:8545

"$POLYGON_EDGE_BIN" polybft stake \
--data-dir /data/data-${counter} \
--amount 1000000000000000000000000 \
--supernet-id ${supernetID} \
--stake-manager ${stakeManagerAddr} \
--stake-token ${stakeToken} \
--jsonrpc http://rootchain:8545

counter=$((counter + 1))
done

"$POLYGON_EDGE_BIN" polybft supernet \
--private-key aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d \
--supernet-manager ${customSupernetManagerAddr} \
--stake-manager ${stakeManagerAddr} \
--finalize-genesis-set \
--enable-staking \
--genesis /data/genesis.json \
--jsonrpc http://rootchain:8545
;;
esac
;;

*)
echo "Executing polygon-edge..."
exec "$POLYGON_EDGE_BIN" "$@"
;;

esac
16 changes: 6 additions & 10 deletions scripts/cluster
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,12 @@ function startServerFromBinary() {
}

function startServerFromDockerCompose() {
case "$1" in
"ibft")
docker-compose -f ./docker/local/docker-compose.yml up -d --build
;;
"polybft")
cd core-contracts && npm install && npm run compile && cd -
go run ./consensus/polybft/contractsapi/artifacts-gen/main.go
EDGE_CONSENSUS=polybft docker-compose -f ./docker/local/docker-compose.yml up -d --build
;;
esac
if [ "$1" != "polybft" ]
then
export EDGE_CONSENSUS="$1"
fi

docker-compose -f ./docker/local/docker-compose.yml up -d --build
}

function destroyDockerEnvironment() {
Expand Down

0 comments on commit 3b827b8

Please sign in to comment.