Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker local-testnet scripts #5998

Merged
merged 17 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion scripts/docker-testnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ script is provided. This can be done, by invoking the script or building the ima

```
./build.sh # (Optional) Can be ignored if you already have the images stored in the local registry.
./start.sh # Will start the local-testnet.
./setup.sh # Will setup the local-testnet.
./clean.sh # Will stop and remove the containers related to the local-testnet.

Optionally
./stop.sh # Will stop all the containers in the local-testnet.
./start.sh # Will start all stopped containers from the initial local-testnet.
```
152 changes: 93 additions & 59 deletions scripts/docker-testnet/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,105 +29,139 @@ createDockerNetwork() {
}

startSeedNode() {
local publishPortArgs=""
if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
publishPortArgs="-p $DOCKER_PUBLISH_PORT_RANGE:10000"
(( DOCKER_PUBLISH_PORT_RANGE++ ))
fi

docker run -d --name seednode \
-v ${TESTNETDIR}/seednode/config:/go/mx-chain-go/cmd/seednode/config \
--network ${DOCKER_NETWORK_NAME} \
$publishPortArgs \
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
local observerIdx=0
local publishPortArgs=""

# 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))

if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
publishPortArgs="-p $DOCKER_PUBLISH_PORT_RANGE:10000"
fi

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


(( IP_HOST_BYTE++ ))
(( DOCKER_PUBLISH_PORT_RANGE++ ))
((observerIdx++)) || true
done
done

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

docker run -d --name "observer${observerIdx}-${NETWORK_ADDRESS}.${IP_HOST_BYTE}-10200-shard${i}" \
if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
publishPortArgs="-p $DOCKER_PUBLISH_PORT_RANGE:10000"
fi

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


(( IP_HOST_BYTE++ ))
(( DOCKER_PUBLISH_PORT_RANGE++ ))
((observerIdx++)) || true
done
done

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

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

(( IP_HOST_BYTE++ ))
((observerIdx++)) || true
done
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
local validatorIdx=0
local publishPortArgs=""
# Example for loop with injected variables in Bash
for ((i = 0; i < SHARDCOUNT; i++)); do
for ((j = 0; j < SHARD_VALIDATORCOUNT; j++)); do

if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
publishPortArgs="-p $DOCKER_PUBLISH_PORT_RANGE:10000"
fi

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

(( IP_HOST_BYTE++ ))
(( DOCKER_PUBLISH_PORT_RANGE++ ))
((validatorIdx++)) || true
done
done

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

(( IP_HOST_BYTE++ ))
((validatorIdx++)) || true
done
done
if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
publishPortArgs="-p $DOCKER_PUBLISH_PORT_RANGE:10000"
fi

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

(( IP_HOST_BYTE++ ))
((validatorIdx++)) || true
(( IP_HOST_BYTE++ ))
(( DOCKER_PUBLISH_PORT_RANGE++ ))
((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
pushd $TESTNETDIR/proxy/config
cp config.toml config_edit.toml

if [ "$SHARD_OBSERVERCOUNT" -le 0 ]; then
generateProxyValidatorListDocker config_edit.toml
else
generateProxyObserverListDocker config_edit.toml
fi
# Truncate config.toml before the [[Observers]] list
sed -i -n '/\[\[Observers\]\]/q;p' config_edit.toml

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

echo "Updated configuration for the Proxy."
popd
mv config_edit.toml config.toml
echo "Updated configuration for the Proxy."
popd
}

generateProxyObserverListDocker() {
Expand Down
43 changes: 43 additions & 0 deletions scripts/docker-testnet/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

set -eux

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

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

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

cloneRepositories

prepareFolders

buildConfigGenerator

generateConfig

copyConfig

copySeednodeConfig
updateSeednodeConfig

copyNodeConfig
updateNodeConfig

createDockerNetwork

startSeedNode
startObservers
startValidators

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

47 changes: 11 additions & 36 deletions scripts/docker-testnet/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,17 @@

set -eux

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

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

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

cloneRepositories

prepareFolders

buildConfigGenerator

generateConfig

copyConfig

copySeednodeConfig
updateSeednodeConfig

copyNodeConfig
updateNodeConfig

createDockerNetwork

startSeedNode
startObservers
startValidators

if [ $USE_PROXY -eq 1 ]; then
buildProxyImage
prepareFolders_Proxy
copyProxyConfig
updateProxyConfigDocker
startProxyDocker
# Check if the file exists
if [ ! -f "$file_path" ]; then
echo "File $file_path not found."
exit 1
fi

# Read the file line by line
while IFS= read -r line; do
docker start $line
done < "$file_path"

rmdir ./tmp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line?

21 changes: 21 additions & 0 deletions scripts/docker-testnet/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -eux

# 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"

# Get the IDs of containers attached to the network
export CONTAINER_IDS=$(docker network inspect -f '{{range $k, $v := .Containers}}{{printf "%s\n" $k}}{{end}}' "$DOCKER_NETWORK_NAME")

mkdir -p ./tmp

# Stop each container
echo "Stopping containers..."
for CONTAINER_ID in $CONTAINER_IDS; do
docker stop "$CONTAINER_ID"
echo "$CONTAINER_ID" >> ./tmp/stopped_containers
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line?

8 changes: 8 additions & 0 deletions scripts/docker-testnet/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
# Don't change the subnet, unless you know what you are doing. Prone to errors.
export DOCKER_NETWORK_SUBNET="172.18.0.0/24"
export DOCKER_NETWORK_NAME="local-testnet"

# By default ports won't be published. If set to 1, all containers will port-forward to host network.
export DOCKER_PUBLISH_PORTS=1

if [[ "$DOCKER_PUBLISH_PORTS" -gt 0 ]]; then
export DOCKER_PUBLISH_PORT_RANGE=30000
fi

########################################################################


Expand Down
Loading