Skip to content

Commit

Permalink
Terra operator docker refactor (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuyenthevinh2000 authored Mar 18, 2023
1 parent 272ec36 commit 74bf5c4
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 199 deletions.
32 changes: 0 additions & 32 deletions Dockerfile.terraclassic.terrad-binary

This file was deleted.

15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,18 @@ localnet-stop:
docker-compose down

.PHONY: localnet-start localnet-stop

###############################################################################
### Images ###
###############################################################################

build-operator-img-all: build-operator-img-core build-operator-img-node

build-operator-img-core:
docker-compose -f contrib/terra-operator/docker-compose.build.yml build core --no-cache

build-operator-img-node:
@if ! docker image inspect public.ecr.aws/p5q2r9h7/core:alpine3.17 &>/dev/null ; then make build-operator-img-core ; fi
docker-compose -f contrib/terra-operator/docker-compose.build.yml build node --no-cache

.PHONY: build-operator-img-all build-operator-img-core build-operator-img-node
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,10 @@ docker-compose up -d
```

#### Build from source
Its possible to use docker-compose to build the images from the go source code by running the following commands in sequence:

1) docker-compose -f docker-compose.node.yml -f docker-compose.build.yml build core --no-cache
2) docker-compose -f docker-compose.node.yml -f docker-compose.build.yml build node --no-cache

```sh
make build-all -f contrib/terra-operator/Makefile
```

## Resources

Expand Down
34 changes: 34 additions & 0 deletions contrib/terra-operator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ARG base_image=classic-terra/core
ARG version=latest

FROM ${base_image}:${version}

ARG chainid=test
ARG new_network=false
ARG enable_lcd=true
ARG validator_keyname=local
ARG validator_mnenomic="torch swamp cancel lift never october child harsh rib aspect luxury word peanut length bamboo hawk material vehicle glue above west random sketch author"
ARG validator_amount=1uluna
ARG validator_commission_rate=0.2
ARG validator_commission_rate_max=1
ARG validator_commission_rate_max_change=0.01
ARG validator_min_self_delegation=1

ENV CHAINID ${chainid}
ENV NEW_NETWORK ${new_network}
ENV ENABLE_LCD ${enable_lcd}
ENV VALIDATOR_KEYNAME ${validator_keyname}
ENV VALIDATOR_MNENOMIC ${validator_mnenomic}
ENV VALIDATOR_AMOUNT ${validator_amount}
ENV VALIDATOR_COMMISSION_RATE ${validator_commission_rate}
ENV VALIDATOR_COMMISSION_RATE_MAX ${validator_commission_rate_max}
ENV VALIDATOR_COMMISSION_RATE_MAX_CHANGE ${validator_commission_rate_max_change}
ENV VALIDATOR_MIN_SELF_DELEGATION ${validator_min_self_delegation}

COPY ./entrypoint.sh /entrypoint.sh
COPY ./keys-add.sh /keys-add.sh
COPY ./create-validator.sh /create-validator.sh
COPY ./test-node-setup.sh /test-node-setup.sh
COPY ./keys.json /keys.json

CMD ["/entrypoint.sh"]
58 changes: 58 additions & 0 deletions contrib/terra-operator/Dockerfile.core
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# docker build . -t cosmwasm/wasmd:latest
# docker run --rm -it cosmwasm/wasmd:latest /bin/sh
FROM golang:1.18-alpine3.17 AS go-builder
ARG source=.

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
# with some changes to support our toolchain, etc
RUN set -eux; apk add --no-cache ca-certificates build-base git;

RUN apk add git cmake
# NOTE: add these to run with LEDGER_ENABLED=true
# RUN apk add libusb-dev linux-headers

RUN git clone https://github.com/classic-terra/core.git && mv core /code
WORKDIR /code

# Install mimalloc
RUN git clone --depth 1 https://github.com/microsoft/mimalloc; cd mimalloc; mkdir build; cd build; cmake ..; make -j$(nproc); make install
ENV MIMALLOC_RESERVE_HUGE_OS_PAGES=4

# Cosmwasm - download correct libwasmvm version and verify checksum
RUN WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | cut -d ' ' -f 2) \
&& wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$(uname -m).a \
-O /lib/libwasmvm_muslc.a \
&& wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt \
&& sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep $(uname -m) | cut -d ' ' -f 1)

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN LEDGER_ENABLED=false BUILD_TAGS=muslc LDFLAGS="-linkmode=external -extldflags \"-L/code/mimalloc/build -lmimalloc -Wl,-z,muldefs -static\"" make build

FROM ubuntu:22.04

RUN DEBIAN_FRONTEND=noninteractive ; \
apt-get update ; \
apt-get install -y wget lz4 aria2 curl jq ip nmap; \
apt-get clean ; \
mkdir /terra ; \
groupadd -r terra ; \
useradd -r -g terra --home-dir=/terra terra ; \
chown -R terra:terra /terra

USER terra

WORKDIR /terra

COPY --from=go-builder /code/build/terrad /usr/local/bin/terrad

# rest server
EXPOSE 1317
# grpc
EXPOSE 9090
# tendermint p2p
EXPOSE 26656
# tendermint rpc
EXPOSE 26657

CMD ["/usr/local/bin/terrad", "version"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions contrib/terra-operator/docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.8'

services:
core:
image: public.ecr.aws/p5q2r9h7/core:alpine3.17
build:
context: ../..
dockerfile: Dockerfile
platforms:
- "linux/amd64"
labels:
- "description=Contains the terrad binary"
node:
image: public.ecr.aws/p5q2r9h7/node:alpine3.17
build:
context: .
dockerfile: Dockerfile
args:
base_image: public.ecr.aws/p5q2r9h7/core
version: alpine3.17
platforms:
- "linux/amd64"
labels:
- "description=Contains everything to run a full node"
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ version: '3.8'
services:
node:
container_name: node
image: terraclassic.terrad-node
image: public.ecr.aws/p5q2r9h7/node:alpine3.17
platform: linux/amd64
environment:
- CHAINID=columbus-5
- ENABLE_LCD=true
- ENABLE_UNSAFE_CORS=false
- CORS_ALLOWED_ORIGINS=[]
- CORS_ALLOWED_METHODS=["HEAD", "GET", "POST"]
- CORS_ALLOWED_HEADERS=["Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time"]
- CHAINID=test
- NEW_NETWORK=true
- ENABLE_LCD=true
- TERRAD_STARTUP_PARAMETERS=--x-crisis-skip-assert-invariants
- MINIMUM_GAS_PRICES=0.05uluna,0.104938usdr,0.15uusd,170.0ukrw
- MONIKER=rebel-docker-node
Expand Down
98 changes: 98 additions & 0 deletions contrib/terra-operator/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/sh

# Default to "data".
DATADIR="${DATADIR:-/terra/.terra/data}"
MONIKER="${MONIKER:-docker-node}"
ENABLE_LCD="${ENABLE_LCD:-true}"
MINIMUM_GAS_PRICES=${MINIMUM_GAS_PRICES-0.01133uluna,0.15uusd,0.104938usdr,169.77ukrw,428.571umnt,0.125ueur,0.98ucny,16.37ujpy,0.11ugbp,10.88uinr,0.19ucad,0.14uchf,0.19uaud,0.2usgd,4.62uthb,1.25usek,1.25unok,0.9udkk,2180.0uidr,7.6uphp,1.17uhkd}
SNAPSHOT_NAME="${SNAPSHOT_NAME}"
SNAPSHOT_BASE_URL="${SNAPSHOT_BASE_URL:-https://getsfo.quicksync.io}"

# Moniker will be updated by entrypoint.
terrad init --chain-id $CHAINID moniker

# Backup for templating
mv ~/.terra/config/config.toml ~/config.toml
mv ~/.terra/config/app.toml ~/app.toml

if [ "$CHAINID" = "columbus-5" ] ; then wget -O ~/.terra/config/genesis.json https://columbus-genesis.s3.ap-northeast-1.amazonaws.com/columbus-5-genesis.json; fi; \
if [ "$CHAINID" = "columbus-5" ] ; then wget -O ~/.terra/config/addrbook.json https://networks.mcontrol.ml/columbus/addrbook.json; fi; \
if [ "$CHAINID" = "rebel-1" ] ; then wget -O ~/.terra/config/genesis.json https://raw.githubusercontent.com/terra-rebels/classic-testnet/master/rebel-1/genesis.json; fi; \
if [ "$CHAINID" = "rebel-1" ] ; then wget -O ~/.terra/config/addrbook.json https://raw.githubusercontent.com/terra-rebels/classic-testnet/master/rebel-1/addrbook.json; fi; \
if [ "$CHAINID" = "rebel-2" ] ; then wget -O ~/.terra/config/genesis.json https://raw.githubusercontent.com/terra-rebels/classic-testnet/master/rebel-2/genesis.json; fi; \
if [ "$CHAINID" = "rebel-2" ] ; then wget -O ~/.terra/config/addrbook.json https://raw.githubusercontent.com/terra-rebels/classic-testnet/master/rebel-2/addrbook.json; fi;

# First sed gets the app.toml moved into place.
# app.toml updates
sed 's/minimum-gas-prices = "0uluna"/minimum-gas-prices = "'"$MINIMUM_GAS_PRICES"'"/g' ~/app.toml > ~/.terra/config/app.toml

# Needed to use awk to replace this multiline string.
if [ "$ENABLE_LCD" = true ] ; then
sed -i '0,/enable = false/s//enable = true/' ~/.terra/config/app.toml

fi

# config.toml updates

sed 's/moniker = "moniker"/moniker = "'"$MONIKER"'"/g' ~/config.toml > ~/.terra/config/config.toml
sed -i 's/laddr = "tcp:\/\/127.0.0.1:26657"/laddr = "tcp:\/\/0.0.0.0:26657"/g' ~/.terra/config/config.toml

if [ "$CHAINID" = "columbus-5" ] && [[ ! -z "$SNAPSHOT_NAME" ]] ; then
# Download the snapshot if data directory is empty.
res=$(find "$DATADIR" -name "*.db")
if [ "$res" ]; then
echo "data directory is NOT empty, skipping quicksync"
else
echo "starting snapshot download"
mkdir -p $DATADIR
cd $DATADIR
FILENAME="$SNAPSHOT_NAME"

# Download
aria2c -x5 $SNAPSHOT_BASE_URL/$FILENAME
# Extract
lz4 -d $FILENAME | tar xf -

# # cleanup
rm $FILENAME
fi
fi

# check if CHAINID is test
if [ "$NEW_NETWORK" = "true" ] ; then
# add new gentx
sh /test-node-setup.sh
fi

terrad start $TERRAD_STARTUP_PARAMETERS &

if [ "$NEW_NETWORK" = "false" ] ; then
#Wait for Terrad to catch up
while true
do
if ! (( $(echo $(terrad status) | awk -F '"catching_up":|},"ValidatorInfo"' '{print $2}') ));
then
break
fi
sleep 1
done

if [ ! -z "$VALIDATOR_AUTO_CONFIG" ] && [ "$VALIDATOR_AUTO_CONFIG" = "1" ]; then
if [ ! -z "$VALIDATOR_KEYNAME" ] && [ ! -z "$VALIDATOR_MNENOMIC" ] && [ ! -z "$VALIDATOR_PASSPHRASE" ] ; then
terrad keys add $VALIDATOR_KEYNAME --recover > ~/.terra/keys.log 2>&1 << EOF
$VALIDATOR_MNENOMIC
$VALIDATOR_PASSPHRASE
$VALIDATOR_PASSPHRASE
EOF
fi

if [ ! -z "$VALIDATOR_AMOUNT" ] && [ ! -z "$MONIKER" ] && [ ! -z "$VALIDATOR_PASSPHRASE" ] && [ ! -z "$VALIDATOR_KEYNAME" ] && [ ! -z "$VALIDATOR_KEYNAME" ] && [ ! -z "$VALIDATOR_COMMISSION_RATE" ] && [ ! -z "$VALIDATOR_COMMISSION_RATE_MAX" ] && [ ! -z "$VALIDATOR_COMMISSION_RATE_MAX_CHANGE" ] && [ ! -z "$VALIDATOR_MIN_SELF_DELEGATION" ] ; then
terrad tx staking create-validator --amount=$VALIDATOR_AMOUNT --pubkey=$(terrad tendermint show-validator) --moniker="$MONIKER" --chain-id=$CHAINID --from=$VALIDATOR_KEYNAME --commission-rate="$VALIDATOR_COMMISSION_RATE" --commission-max-rate="$VALIDATOR_COMMISSION_RATE_MAX" --commission-max-change-rate="$VALIDATOR_COMMISSION_RATE_MAX_CHANGE" --min-self-delegation="$VALIDATOR_MIN_SELF_DELEGATION" --gas=$VALIDATOR_GAS --gas-adjustment=$VALIDATOR_GAS_ADJUSTMENT --fees=$VALIDATOR_FEES > ~/.terra/validator.log 2>&1 << EOF
$VALIDATOR_PASSPHRASE
y
EOF
fi
fi
fi

wait
File renamed without changes.
24 changes: 24 additions & 0 deletions contrib/terra-operator/keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"keys": [
{
"keyring-keyname": "test",
"address": "terra1r9xyz4qgkktf4kycqnjj3tpsluaqxhzzqzke6x",
"mnemonic": "vintage emotion risk gun gadget siren quit weapon work dignity pudding lamp huge tube govern mosquito diary rookie card risk leg bridge velvet stick"
},
{
"keyring-keyname": "test1",
"address": "terra1ryyltrt8zvap3cgmv9mly7g87xgl99k3hnq6tv",
"mnemonic": "flat color utility today rent lake client hair victory pause text more connect lonely menu hope cup music armor critic license casino panic mirror"
},
{
"keyring-keyname": "test2",
"address": "terra1z2j9rsptplwap79k8t4dy05xhe9c3p0jqlseag",
"mnemonic": "mixed tourist quit copper robot panic rather record scare learn wing bicycle latin tape proud upper rocket scare tobacco thunder neither flat isolate humor"
},
{
"keyring-keyname": "test3",
"address": "terra15dpwzefn4zsktnnu0zl058p4kklsj4p05x560w",
"mnemonic": "permit lava scene secret ball lava iron result reunion purpose sea badge focus rug cradle human plastic rough stand swarm pipe diagram deliver faculty"
}
]
}
36 changes: 36 additions & 0 deletions contrib/terra-operator/test-node-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#/bin/sh

# KEY MANAGEMENT
KEYRING="test"

# Function updates the config based on a jq argument as a string
update_test_genesis () {
# EX: update_test_genesis '.consensus_params["block"]["max_gas"]="100000000"'
cat ~/.terra/config/genesis.json | jq --arg DENOM "$2" "$1" > ~/.terra/config/tmp_genesis.json && mv ~/.terra/config/tmp_genesis.json ~/.terra/config/genesis.json
}

# add keys, add balances
for i in $(seq 0 3); do
key=$(jq ".keys[$i] | tostring" /keys.json )
keyname=$(echo $key | jq -r 'fromjson | ."keyring-keyname"')
mnemonic=$(echo $key | jq -r 'fromjson | .mnemonic')
# Add new account
echo $mnemonic | terrad keys add $keyname --keyring-backend $KEYRING --recover --home ~/.terra
# Add initial balances
terrad add-genesis-account $keyname "1000000000000uluna" --keyring-backend $KEYRING --home ~/.terra
done

# Sign genesis transaction
terrad gentx test "1000000uluna" --keyring-backend $KEYRING --chain-id $CHAINID --home ~/.terra

update_test_genesis '.app_state["gov"]["voting_params"]["voting_period"] = "50s"'
update_test_genesis '.app_state["mint"]["params"]["mint_denom"]=$DENOM' uluna
update_test_genesis '.app_state["gov"]["deposit_params"]["min_deposit"]=[{"denom": $DENOM,"amount": "1000000"}]' uluna
update_test_genesis '.app_state["crisis"]["constant_fee"]={"denom": $DENOM,"amount": "1000"}' uluna
update_test_genesis '.app_state["staking"]["params"]["bond_denom"]=$DENOM' uluna

# Collect genesis tx
terrad collect-gentxs --home ~/.terra

# Run this to ensure everything worked and that the genesis file is setup correctly
terrad validate-genesis --home ~/.terra
19 changes: 0 additions & 19 deletions docker-compose.build.yml

This file was deleted.

Loading

0 comments on commit 74bf5c4

Please sign in to comment.