Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: add topos docker local stack (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan authored and atanmarko committed Sep 7, 2023
1 parent 1c41a3b commit 509c0f4
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Docker_build_push

on:
push:
branches: [develop]
pull_request:
branches: [develop]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
docker:
name: Build and push docker image to GitHub Container Registry
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4
with:
short-length: 7

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Compile contracts
run: cd core-contracts && npm install && npm run compile

- name: Push to GitHub Container Registry
uses: docker/build-push-action@v3
with:
context: .
push: true
file: ./docker/local-topos/Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: |
type=registry,ref=ghcr.io/${{ github.repository }}:build-cache-${{ env.GITHUB_REF_SLUG_URL }}-${{ github.workflow }}
type=registry,ref=ghcr.io/${{ github.repository }}:build-cache-develop-${{ github.workflow }}
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:build-cache-${{ env.GITHUB_REF_SLUG_URL }}-${{ github.workflow }},mode=max
4 changes: 4 additions & 0 deletions docker/local-topos/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CHAIN_ID=
NUMBER_OF_NODES=
BOOTNODE_DOMAIN_NAME=node-1
TOPOS_MESSAGING_PROTOCOL_CONTRACTS_VERSION=main
29 changes: 29 additions & 0 deletions docker/local-topos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM golang:1.18-alpine AS builder

RUN apk add git

WORKDIR /polygon-edge

ADD go.mod go.sum ./
RUN go mod download

COPY . .

RUN go run ./consensus/polybft/contractsapi/artifacts-gen/main.go

RUN go build -o polygon-edge main.go

FROM alpine:latest AS runner

RUN apk --no-cache add ca-certificates jq

WORKDIR /polygon-edge

COPY --from=builder /polygon-edge/polygon-edge ./
COPY ./docker/local/polygon-edge.sh ./
COPY ./core-contracts/artifacts ./core-contracts/artifacts

# Expose json-rpc, libp2p and grpc ports
EXPOSE 8545 9632 1478 5001

ENTRYPOINT ["./polygon-edge.sh"]
116 changes: 116 additions & 0 deletions docker/local-topos/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
version: '3.9'

services:
contracts-init:
image: ghcr.io/toposware/topos-smart-contracts:${TOPOS_MESSAGING_PROTOCOL_CONTRACTS_VERSION}
container_name: contracts-init
volumes:
- contracts:/usr/src/app/brownie/build/contracts

## INITIALIZE GENESIS AND SECRETS
init:
build:
context: ../../
dockerfile: docker/local/Dockerfile
image: local/polygon-edge
container_name: polygon-edge-bootstrapper
command: [ "init", "${EDGE_CONSENSUS:-ibft}" ]
environment:
- CHAIN_ID=${CHAIN_ID}
volumes:
- data:/data
- contracts:/contracts
depends_on:
- contracts-init

## RUN NODES
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"]
depends_on:
init:
condition: service_completed_successfully
ports:
- '10000:9632'
- '10002:8545'
- '10003:5001'
volumes:
- data:/data
networks:
- polygon-edge-docker
restart: on-failure

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"]
depends_on:
init:
condition: service_completed_successfully
ports:
- '20000:9632'
- '20002:8545'
- '20003:5001'
volumes:
- data:/data
networks:
- polygon-edge-docker
restart: on-failure

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"]
depends_on:
init:
condition: service_completed_successfully
ports:
- '30000:9632'
- '30002:8545'
- '30003:5001'
volumes:
- data:/data
networks:
- polygon-edge-docker
restart: on-failure

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"]
depends_on:
init:
condition: service_completed_successfully
ports:
- '40000:9632'
- '40002:8545'
- '40003:5001'
volumes:
- data:/data
networks:
- polygon-edge-docker
restart: on-failure

contracts:
image: ghcr.io/toposware/topos-smart-contracts:${TOPOS_MESSAGING_PROTOCOL_CONTRACTS_VERSION}
container_name: contracts
init: true
environment:
- PRIVATE_KEY=${PRIVATE_KEY}
- TOKEN_DEPLOYER_SALT=${TOKEN_DEPLOYER_SALT}
- TOPOS_CORE_SALT=${TOPOS_CORE_SALT}
- TOPOS_CORE_PROXY_SALT=${TOPOS_CORE_PROXY_SALT}
- SUBNET_REGISTRATOR_SALT=${SUBNET_REGISTRATOR_SALT}
command: bash -c "
sleep 15 &&
npm run deploy2:topos-msg-protocol http://node-1:8545 $$(cat /data/data-1/consensus/validator.key)
"
volumes:
- data:/data
depends_on:
- node-1
volumes:
contracts:
data:
96 changes: 96 additions & 0 deletions docker/local-topos/polygon-edge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh

set -e

POLYGON_EDGE_BIN=./polygon-edge
CONTRACTS_PATH=/contracts
GENESIS_PATH=/data/genesis/genesis.json
CHAIN_ID="${CHAIN_ID:-100}" # 100 is Edge's default value
NUMBER_OF_NODES="${NUMBER_OF_NODES:-4}" # Number of subnet nodes in the consensus
BOOTNODE_DOMAIN_NAME="${BOOTNODE_DOMAIN_NAME:-node-1}"
CHAIN_CUSTOM_OPTIONS=$(tr "\n" " " << EOL
--block-gas-limit 10000000
--epoch-size 10
--chain-id $CHAIN_ID
--name polygon-edge-docker
--premine 0x228466F2C715CbEC05dEAbfAc040ce3619d7CF0B:0xD3C21BCECCEDA1000000
--premine 0xca48694ebcB2548dF5030372BE4dAad694ef174e:0xD3C21BCECCEDA1000000
--premine 0x4AAb25B4fAd0Beaac466050f3A7142A502f4Cf0a:1000000000000000000000
EOL
)

case "$1" in

"init")
data_dir="data-"
if [ "$NUMBER_OF_NODES" -eq "1" ]; then
data_dir="data-1"
fi

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 "$NUMBER_OF_NODES" --data-dir "$data_dir" --json)
echo "Secrets have been successfully generated"

BOOTNODE_ID=$(echo $secrets | jq -r '.[0] | .node_id')
BOOTNODE_ADDRESS=$(echo $secrets | jq -r '.[0] | .address')

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- \
--validator-set-size=$NUMBER_OF_NODES \
--bootnode /dns4/"$BOOTNODE_DOMAIN_NAME"/tcp/1478/p2p/$BOOTNODE_ID \
--premine=$BOOTNODE_ADDRESS:1000000000000000000000
fi
;;

"polybft")
if [ -f "$GENESIS_PATH" ]; then
echo "Secrets have already been generated."
else
echo "Generating PolyBFT secrets..."
secrets=$("$POLYGON_EDGE_BIN" polybft-secrets init --insecure --num "$NUMBER_OF_NODES" --data-dir "$data_dir" --json)
echo "Secrets have been successfully generated"

BOOTNODE_ID=$(echo $secrets | jq -r '.[0] | .node_id')
BOOTNODE_ADDRESS=$(echo $secrets | jq -r '.[0] | .address')

echo "Generating manifest..."
"$POLYGON_EDGE_BIN" manifest --path /data/manifest.json --validators-path /data --validators-prefix "$data_dir"

echo "Generating PolyBFT Genesis file..."
"$POLYGON_EDGE_BIN" genesis $CHAIN_CUSTOM_OPTIONS \
--dir /data/genesis.json \
--consensus polybft \
--manifest /data/manifest.json \
--validator-set-size=$NUMBER_OF_NODES \
--bootnode /dns4/"$BOOTNODE_DOMAIN_NAME"/tcp/1478/p2p/$BOOTNODE_ID \
--premine=$BOOTNODE_ADDRESS:1000000000000000000000
fi
;;

*)
echo "Predeploying ConstAddressDeployer contract..."
CONST_ADDRESS_DEPLOYER_ADDRESS=0x0000000000000000000000000000000000001110
"$POLYGON_EDGE_BIN" genesis predeploy \
--chain "$GENESIS_PATH" \
--artifacts-path "$CONTRACTS_PATH"/ConstAddressDeployer.json \
--predeploy-address "$CONST_ADDRESS_DEPLOYER_ADDRESS"
echo "ConstAddressDeployer has been successfully predeployed!"
;;
esac
;;

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

esac

0 comments on commit 509c0f4

Please sign in to comment.