Skip to content

Commit

Permalink
added script to create local-testnet with docker.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristure committed Mar 14, 2024
1 parent 222e53c commit 41d8908
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 3 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/build_local_tesnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Build local testnet

on:
pull_request:
branches: [ master, rc/* ]
types: [opened, ready_for_review]
push:
workflow_dispatch:

jobs:
build:
strategy:
matrix:
runs-on: [ubuntu-latest]
runs-on: ${{ matrix.runs-on }}
name: Build
steps:
- name: Set up Go 1.20.7
uses: actions/setup-go@v3
with:
go-version: 1.20.7
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Check out mx-deploy-go
uses: actions/checkout@v4
with:
repository: multiversx/mx-chain-deploy-go
path: mx-chain-deploy-go

- name: Check out mx-chain-proxy-go
uses: actions/checkout@v4
with:
repository: multiversx/mx-chain-proxy-go
path: mx-chain-proxy-go

- name: Build images
run: |
docker build -f docker/node/Dockerfile . -t node:dev
docker build -f docker/seednode/Dockerfile . -t seednode:dev
- name: Start localnet
id: generate-config
run: |
cd ${GITHUB_WORKSPACE}/scripts/docker-testnet
export TESTNETDIR=${GITHUB_WORKSPACE}/docker-testnet
export CI_RUN=1
./start.sh
echo "Check everything is alright. Remove once confirmed"
docker ps
sleep 1m
curl http://localhost:7950
4 changes: 1 addition & 3 deletions docker/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ RUN go build -v -ldflags="-X main.appVersion=$(git describe --tags --long --dirt
RUN cp /go/pkg/mod/github.com/multiversx/$(cat /go/mx-chain-go/go.mod | grep mx-chain-vm-v | sort -n | tail -n -1| awk -F '/' '{print$3}'| sed 's/ /@/g')/wasmer/libwasmer_linux_amd64.so /lib/libwasmer_linux_amd64.so
RUN cp /go/pkg/mod/github.com/multiversx/$(cat /go/mx-chain-go/go.mod | grep mx-chain-vm-go | sort -n | tail -n -1| awk -F '/' '{print$3}'| sed 's/ /@/g')/wasmer2/libvmexeccapi.so /lib/libvmexeccapi.so

WORKDIR /go/mx-chain-go/cmd/node

# ===== SECOND STAGE ======
FROM ubuntu:22.04
RUN apt-get update && apt-get upgrade -y
COPY --from=builder "/go/mx-chain-go/cmd/node" "/go/mx-chain-go/cmd/node/"
COPY --from=builder "/go/mx-chain-go/cmd/node/node" "/go/mx-chain-go/cmd/node/"
COPY --from=builder "/lib/libwasmer_linux_amd64.so" "/lib/libwasmer_linux_amd64.so"
COPY --from=builder "/lib/libvmexeccapi.so" "/lib/libvmexeccapi.so"
WORKDIR /go/mx-chain-go/cmd/node/
Expand Down
16 changes: 16 additions & 0 deletions scripts/docker-testnet/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

# Delete the entire testnet folder, which includes configuration, executables and logs.

export MULTIVERSXTESTNETSCRIPTSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

source "$MULTIVERSXTESTNETSCRIPTSDIR/variables.sh"

echo "Stopping all containers..."
docker stop $(docker ps -a -q)

echo "Removing all containers..."
docker container prune -f

echo "Removing $TESTNETDIR..."
rm -rf $TESTNETDIR
153 changes: 153 additions & 0 deletions scripts/docker-testnet/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env bash

startSeedNode() {
docker run -d --name seednode -v ${TESTNETDIR}/seednode/config:/go/mx-chain-go/cmd/seednode/config seednode:dev \
--rest-api-interface=0.0.0.0:10000
}

startObservers() {
local observerIdx=0
# Example for loop with injected variables in Bash
for ((i = 0; i < SHARDCOUNT; i++)); do
for ((j = 0; j < SHARD_OBSERVERCOUNT; j++)); do
# Your commands or code to be executed in each iteration
KEY_INDEX=$((TOTAL_NODECOUNT - observerIdx - 1))

docker run -d --name "observer${observerIdx}" \
-v $TESTNETDIR/node/config:/go/mx-chain-go/cmd/node/config \
node:dev \
--destination-shard-as-observer $i \
--rest-api-interface=0.0.0.0:10200 \
--config ./config/config_observer.toml \
--sk-index=${KEY_INDEX} \

((observerIdx++)) || true
done
done

for ((i = 0; i < META_OBSERVERCOUNT; i++)); do
KEY_INDEX=$((TOTAL_NODECOUNT - observerIdx - 1))

docker run -d --name "observer${observerIdx}" \
-v $TESTNETDIR/node/config:/go/mx-chain-go/cmd/node/config \
node:dev \
--destination-shard-as-observer "metachain" \
--rest-api-interface=0.0.0.0:10200 \
--config ./config/config_observer.toml \
--sk-index=${KEY_INDEX} \

((observerIdx++)) || true
done
}

startValidators() {
validatorIdx=0
# Example for loop with injected variables in Bash
for ((i = 0; i < SHARDCOUNT; i++)); do
for ((j = 0; j < SHARD_VALIDATORCOUNT; j++)); do

docker run -d --name "validator${validatorIdx}" \
-v $TESTNETDIR/node/config:/go/mx-chain-go/cmd/node/config \
node:dev \
--rest-api-interface=0.0.0.0:10200 \
--config ./config/config_validator.toml \
--sk-index=${validatorIdx} \

((validatorIdx++)) || true
done
done

for ((i = 0; i < META_VALIDATORCOUNT; i++)); do
docker run -d --name "validator${validatorIdx}" \
-v $TESTNETDIR/node/config:/go/mx-chain-go/cmd/node/config \
node:dev \
--rest-api-interface=0.0.0.0:10200 \
--config ./config/config_observer.toml \
--sk-index=${validatorIdx} \

((validatorIdx++)) || true
done
}

updateProxyConfigDocker() {
pushd $TESTNETDIR/proxy/config
cp config.toml config_edit.toml

# Truncate config.toml before the [[Observers]] list
sed -i -n '/\[\[Observers\]\]/q;p' config_edit.toml

if [ "$SHARD_OBSERVERCOUNT" -le 0 ]; then
generateProxyValidatorListDocker config_edit.toml
else
generateProxyObserverListDocker config_edit.toml
fi

cp config_edit.toml config.toml
rm config_edit.toml

echo "Updated configuration for the Proxy."
popd
}

generateProxyObserverListDocker() {
IP_BIT=3
OUTPUTFILE=$!


for ((i = 0; i < SHARDCOUNT; i++)); do
for ((j = 0; j < SHARD_OBSERVERCOUNT; j++)); do

echo "[[Observers]]" >> config_edit.toml
echo " ShardId = $i" >> config_edit.toml
echo " Address = \"http://172.17.0.${IP_BIT}:10200\"" >> config_edit.toml
echo ""$'\n' >> config_edit.toml

(( IP_BIT++ ))
done
done

for META_OBSERVER in $(seq $META_OBSERVERCOUNT); do
echo "[[Observers]]" >> config_edit.toml
echo " ShardId = $METASHARD_ID" >> config_edit.toml
echo " Address = \"http://172.17.0.${IP_BIT}:10200\"" >> config_edit.toml
echo ""$'\n' >> config_edit.toml

(( IP_BIT++ ))
done
}

generateProxyValidatorListDocker() {
IP_BIT=3
OUTPUTFILE=$!


for ((i = 0; i < SHARDCOUNT; i++)); do
for ((j = 0; j < SHARD_VALIDATORCOUNT; j++)); do

echo "[[Observers]]" >> config_edit.toml
echo " ShardId = $i" >> config_edit.toml
echo " Address = \"http://172.17.0.${IP_BIT}:10200\"" >> config_edit.toml
echo " Type = \"Validator\"" >> config_edit.toml
echo ""$'\n' >> config_edit.toml

(( IP_BIT++ ))
done
done

for META_OBSERVER in $(seq $META_VALIDATORCOUNT); do
echo "[[Observers]]" >> config_edit.toml
echo " ShardId = $METASHARD_ID" >> config_edit.toml
echo " Address = \"http://172.17.0.${IP_BIT}:10200\"" >> config_edit.toml
echo " Type = \"Validator\"" >> config_edit.toml
echo ""$'\n' >> config_edit.toml

(( IP_BIT++ ))
done
}

startProxyDocker() {
docker run -d --name "proxy" \
-p $PORT_PROXY:8080 \
-v $TESTNETDIR/proxy/config:/mx-chain-proxy-go/cmd/proxy/config \
multiversx/chain-proxy:v1.1.45-sp4
}
38 changes: 38 additions & 0 deletions scripts/docker-testnet/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -e

export DOCKERTESTNETDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

MULTIVERSXTESTNETSCRIPTSDIR="$(dirname "$DOCKERTESTNETDIR")/testnet"

source "$DOCKERTESTNETDIR/variables.sh"
source "$DOCKERTESTNETDIR/helpers.sh"
source "$MULTIVERSXTESTNETSCRIPTSDIR/include/config.sh"
source "$MULTIVERSXTESTNETSCRIPTSDIR/include/build.sh"

prepareFolders

buildConfigGenerator

generateConfig

copyConfig

copySeednodeConfig
updateSeednodeConfig

copyNodeConfig
updateNodeConfig

startSeedNode
startObservers
startValidators

if [ $USE_PROXY -eq 1 ]; then
prepareFolders_Proxy
copyProxyConfig
updateProxyConfigDocker
startProxyDocker
fi

Loading

0 comments on commit 41d8908

Please sign in to comment.