Skip to content

Commit

Permalink
Dockernet work (#61)
Browse files Browse the repository at this point in the history
* dockernet work

* resolve merge conflict

* elys, band, go-relayer, hermes, price-feeder docker image build

* start elys, band and relayer

* configure price feeder

* further update on dockernet

* resolve epoch module test
  • Loading branch information
jelysn authored May 3, 2023
1 parent c7f1a66 commit 8451802
Show file tree
Hide file tree
Showing 26 changed files with 1,088 additions and 23 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ release/
.vscode/
.DS_Store
docs/diagrams
build
build
dockernet/state
dockernet/logs
dockernet/upgrades/binaries
dockernet/upgrades/cosmovisor
dockernet/temp.sh
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# syntax = docker/dockerfile:1

ARG GO_VERSION="1.19"
ARG RUNNER_IMAGE="alpine:3.16"

FROM golang:${GO_VERSION}-alpine as builder

WORKDIR /opt
RUN apk add --no-cache make git gcc musl-dev openssl-dev linux-headers

COPY go.mod .
COPY go.sum .

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go mod download

# Copy the remaining files
COPY . .

RUN LINK_STATICALLY=true make build

# Add to a distroless container
FROM ${RUNNER_IMAGE}

COPY --from=builder /opt/build/elysd /usr/local/bin/elysd
RUN apk add bash vim sudo dasel \
&& addgroup -g 1000 elys \
&& adduser -S -h /home/elys -D elys -u 1000 -G elys

RUN mkdir -p /etc/sudoers.d \
&& echo '%wheel ALL=(ALL) ALL' > /etc/sudoers.d/wheel \
&& echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers \
&& adduser elys wheel

USER 1000
ENV HOME /home/elys
WORKDIR $HOME

EXPOSE 26657 26656 1317 9090

CMD ["elysd", "start"]
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ COMMIT:=$(shell git log -1 --format='%H')
VERSION:=$(shell git describe --tags --match 'v*' --abbrev=8 | sed 's/-g/-/' | sed 's/-[0-9]*-/-/')
GOFLAGS:=""
GOTAGS:=ledger

BUILDDIR ?= $(CURDIR)/build
DOCKER := $(shell which docker)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf:1.7.0
DOCKERNET_HOME=./dockernet
DOCKERNET_COMPOSE_FILE=$(DOCKERNET_HOME)/docker-compose.yml

ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=$(NAME) \
-X github.com/cosmos/cosmos-sdk/version.AppName=$(NAME) \
-X github.com/cosmos/cosmos-sdk/version.ServerName=$(BINARY) \
Expand Down Expand Up @@ -92,4 +99,20 @@ help: Makefile

.PHONY: help

.DEFAULT_GOAL := install
.DEFAULT_GOAL := install

build-docker:
@bash $(DOCKERNET_HOME)/build.sh -${build} ${BUILDDIR}

start-docker: build-docker
@bash $(DOCKERNET_HOME)/start_network.sh

clean-docker:
@docker-compose -f $(DOCKERNET_COMPOSE_FILE) stop
@docker-compose -f $(DOCKERNET_COMPOSE_FILE) down
rm -rf $(DOCKERNET_HOME)/state
docker image prune -a

stop-docker:
@bash $(DOCKERNET_HOME)/pkill.sh
docker-compose -f $(DOCKERNET_COMPOSE_FILE) down --remove-orphans
31 changes: 31 additions & 0 deletions dockernet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Build docker images locally

```
make build-docker build=ebrhf
```

The command to build `elys`, `band`, `go-relayer`, `hermes`, `price-feeder`.

Note: ensure price-feeder repo is cloned under elys repository.

# Build daemon on build directory

The configuration files and genesis files are generated with the binaries built on local and should ensure that correct versions of `bandd`, `elysd`, `hermes`, `price-feeder`, `rly` daemons are put on `build` directory.

# Starting dockernet

```
make start-docker
```

# Checking logs from docker

`dockernet/logs` keep the logs from multiple binaries on docker containers.

# Checking the configuration files for dockernet (volumes shared with dockernet)

`dockernet/state` keep the directories for node home directories, price feeder config and relayer configuration.

# Interacting with dockernet

The commands on `dockernet/tests` could be executed to interact with dockernet.
44 changes: 44 additions & 0 deletions dockernet/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -eu
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source ${SCRIPT_DIR}/config.sh

BUILDDIR="$2"
mkdir -p $BUILDDIR


build_local_and_docker() {
module="$1"
title=$(printf "$module" | awk '{ print toupper($0) }')

echo "Building $title Docker... "
if [[ "$module" == "elys" ]]; then
image=Dockerfile
else
image=dockernet/dockerfiles/Dockerfile.$module
fi

DOCKER_BUILDKIT=1 docker build --tag elyszone:$module -f $image . | true
docker_build_succeeded=${PIPESTATUS[0]}

if [[ "$docker_build_succeeded" == "0" ]]; then
echo "Done"
else
echo "Failed"
fi
return $docker_build_succeeded
}

# build docker images and local binaries
while getopts ebrhf flag; do
case "${flag}" in
e) build_local_and_docker elys ;;
f) build_local_and_docker feeder ;;
b) build_local_and_docker band ;;
r) build_local_and_docker relayer ;;
h) echo "Building Hermes Docker... ";
docker build --tag elyszone:hermes -f dockernet/dockerfiles/Dockerfile.hermes . ;
echo "Done" ;;
esac
done
173 changes: 173 additions & 0 deletions dockernet/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/bin/bash

set -eu
DOCKERNET_HOME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

STATE=$DOCKERNET_HOME/state
LOGS=$DOCKERNET_HOME/logs
UPGRADES=$DOCKERNET_HOME/upgrades
SRC=$DOCKERNET_HOME/src
PEER_PORT=26656
DOCKER_COMPOSE="docker-compose -f $DOCKERNET_HOME/docker-compose.yml"

# Logs
ELYS_LOGS=$LOGS/elys.log
TX_LOGS=$DOCKERNET_HOME/logs/tx.log
KEYS_LOGS=$DOCKERNET_HOME/logs/keys.log

# List of other chains enabled
OTHER_CHAINS=(BAND)

# Sets up upgrade if {UPGRADE_NAME} is non-empty
UPGRADE_NAME=""
UPGRADE_OLD_COMMIT_HASH=""

# COIN TYPES
# Coin types can be found at https://github.com/satoshilabs/slips/blob/master/slip-0044.md
COSMOS_COIN_TYPE=118
BAND_COIN_TYPE=494
ETH_COIN_TYPE=60
TERRA_COIN_TYPE=330

# CHAIN PARAMS
BLOCK_TIME='1s'
UNBONDING_TIME="120s"
MAX_DEPOSIT_PERIOD="30s"
VOTING_PERIOD="30s"

VAL_TOKENS=5000000000000
STAKE_TOKENS=5000000000
ADMIN_TOKENS=1000000000

# CHAIN MNEMONICS
VAL_MNEMONIC_1="close soup mirror crew erode defy knock trigger gather eyebrow tent farm gym gloom base lemon sleep weekend rich forget diagram hurt prize fly"
VAL_MNEMONIC_2="turkey miss hurry unable embark hospital kangaroo nuclear outside term toy fall buffalo book opinion such moral meadow wing olive camp sad metal banner"
VAL_MNEMONIC_3="tenant neck ask season exist hill churn rice convince shock modify evidence armor track army street stay light program harvest now settle feed wheat"
VAL_MNEMONIC_4="tail forward era width glory magnet knock shiver cup broken turkey upgrade cigar story agent lake transfer misery sustain fragile parrot also air document"
VAL_MNEMONIC_5="crime lumber parrot enforce chimney turtle wing iron scissors jealous indicate peace empty game host protect juice submit motor cause second picture nuclear area"
VAL_MNEMONICS=(
"$VAL_MNEMONIC_1"
"$VAL_MNEMONIC_2"
"$VAL_MNEMONIC_3"
"$VAL_MNEMONIC_4"
"$VAL_MNEMONIC_5"
)
REV_MNEMONIC="tonight bonus finish chaos orchard plastic view nurse salad regret pause awake link bacon process core talent whale million hope luggage sauce card weasel"

# ELYS
ELYS_CHAIN_ID=ELYS
ELYS_NODE_PREFIX=elys
ELYS_NUM_NODES=3
ELYS_VAL_PREFIX=val
ELYS_ADDRESS_PREFIX=elys
ELYS_DENOM=uelys
ELYS_RPC_PORT=26657
ELYS_ADMIN_ACCT=admin

# Binaries are contigent on whether we're doing an upgrade or not
if [[ "$UPGRADE_NAME" == "" ]]; then
ELYS_BINARY="$DOCKERNET_HOME/../build/elysd"
else
if [[ "${NEW_BINARY:-false}" == "false" ]]; then
ELYS_BINARY="$UPGRADES/binaries/elysd1"
else
ELYS_BINARY="$UPGRADES/binaries/elysd2"
fi
fi
ELYS_MAIN_CMD="$ELYS_BINARY --home $DOCKERNET_HOME/state/${ELYS_NODE_PREFIX}1"

# BAND
BAND_CHAIN_ID=BAND
BAND_NODE_PREFIX=band
BAND_NUM_NODES=1
BAND_BINARY="$DOCKERNET_HOME/../build/bandd"
BAND_VAL_PREFIX=gval
BAND_REV_ACCT=grev1
BAND_ADDRESS_PREFIX=band
BAND_DENOM=uband
BAND_RPC_PORT=26557
BAND_MAIN_CMD="$BAND_BINARY --home $DOCKERNET_HOME/state/${BAND_NODE_PREFIX}1"

# RELAYER
RELAYER_CMD="$DOCKERNET_HOME/../build/relayer --home $STATE/relayer"
RELAYER_BAND_EXEC="$DOCKER_COMPOSE run --rm relayer-band"

RELAYER_ELYS_ACCT=rly1
RELAYER_BAND_ACCT=rly2
RELAYER_ACCTS=($RELAYER_BAND_ACCT)

RELAYER_BAND_MNEMONIC="fiction perfect rapid steel bundle giant blade grain eagle wing cannon fever must humble dance kitchen lazy episode museum faith off notable rate flavor"
RELAYER_MNEMONICS=(
"$RELAYER_BAND_MNEMONIC"
)

PRICE_FEEDER_ELYS_ACCT=feeder
PRICE_FEEDER_ACCTS=($PRICE_FEEDER_ELYS_ACCT)
PRICE_FEEDER_MNEMONIC="surprise pear reject sail fresh west equal tragic rain divorce direct roast piece canyon rival amateur leaf poet admit frequent point measure vapor abstract"
PRICE_FEEDER_MNEMONICS=(
"$PRICE_FEEDER_MNEMONIC"
)

ELYS_ADDRESS() {
# After an upgrade, the keys query can sometimes print migration info,
# so we need to filter by valid addresses using the prefix
$ELYS_MAIN_CMD keys show ${ELYS_VAL_PREFIX}1 --keyring-backend test -a | grep $ELYS_ADDRESS_PREFIX
}
BAND_ADDRESS() {
$BAND_MAIN_CMD keys show ${BAND_VAL_PREFIX}1 --keyring-backend test -a | grep $BAND_ADDRESS_PREFIX
}

CSLEEP() {
for i in $(seq $1); do
sleep 1
printf "\r\t$(($1 - $i))s left..."
done
}

GET_VAR_VALUE() {
var_name="$1"
echo "${!var_name}"
}

WAIT_FOR_BLOCK() {
num_blocks="${2:-1}"
for i in $(seq $num_blocks); do
( tail -f -n0 $1 & ) | grep -q "INF executed block height="
done
}

WAIT_FOR_STRING() {
( tail -f -n0 $1 & ) | grep -q "$2"
}

WAIT_FOR_BALANCE_CHANGE() {
chain=$1
address=$2
denom=$3

max_blocks=30

main_cmd=$(GET_VAR_VALUE ${chain}_MAIN_CMD)
initial_balance=$($main_cmd q bank balances $address --denom $denom | grep amount)
for i in $(seq $max_blocks); do
new_balance=$($main_cmd q bank balances $address --denom $denom | grep amount)

if [[ "$new_balance" != "$initial_balance" ]]; then
break
fi

WAIT_FOR_BLOCK $ELYS_LOGS 1
done
}

GET_VAL_ADDR() {
chain=$1
val_index=$2

MAIN_CMD=$(GET_VAR_VALUE ${chain}_MAIN_CMD)
$MAIN_CMD q staking validators | grep ${chain}_${val_index} -A 5 | grep operator | awk '{print $2}'
}

TRIM_TX() {
grep -E "code:|txhash:" | sed 's/^/ /'
}
Loading

0 comments on commit 8451802

Please sign in to comment.