Skip to content

Commit

Permalink
fix: add github action for upgrade testing (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuyenthevinh2000 authored Feb 17, 2023
1 parent 529971a commit 980cfa5
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 13 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,27 @@ jobs:
run: |
./contrib/localnet/localnet_liveness.sh 100 3 30 localhost
if: env.GIT_DIFF
upgrade-test:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v3
- uses: actions/[email protected]
with:
go-version: 1.18
- uses: technote-space/get-diff-action@v4
id: git_diff
with:
PATTERNS: |
app/upgrades/**/*.go
- name: echo diff
run: echo '${{ env.GIT_DIFF }}'
- name: start localnet
run: |
make clean localnet-start-upgrade
if: env.GIT_DIFF
- name: test upgrade
run: |
chmod +x -R ./contrib/updates
./contrib/updates/upgrade-test.sh
if: env.GIT_DIFF
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Features
* (build) [#118](https://github.com/classic-terra/core/pull/118) localnet for Apple Silicon

### Features
* (build) [#101](https://github.com/classic-terra/core/pull/101) Upgrade test
* (ante) [#103](https://github.com/classic-terra/core/pull/103) Add burn tax split logic
* (ante) [#107](https://github.com/classic-terra/core/pull/107) Burn Tax Whitelist
* (build) [#118](https://github.com/classic-terra/core/pull/118) localnet for Apple Silicon

### Improvements
* (build) [#93](https://github.com/classic-terra/core/pull/93) Use golang 1.18 and fix ad-hoc security vulnerabilities
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 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=.

# See https://github.com/CosmWasm/wasmvm/releases
ENV LIBWASMVM_VERSION=0.16.6
Expand All @@ -16,7 +17,7 @@ RUN apk add git cmake
# RUN apk add libusb-dev linux-headers

WORKDIR /code
COPY . /code/
COPY ${source} /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
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ localnet-start: localnet-stop build-linux
classic-terra/terrad-env testnet --v 4 -o . --starting-ip-address 192.168.10.2 --keyring-backend=test ; fi
docker-compose up -d

localnet-start-upgrade: localnet-upgrade-stop build-linux
$(MAKE) -C contrib/updates build-cosmovisor-linux BUILDDIR=$(BUILDDIR)
$(if $(shell $(DOCKER) inspect -f '{{ .Id }}' classic-terra/terrad-upgrade-env 2>/dev/null),$(info found image classic-terra/terrad-upgrade-env),$(MAKE) -C contrib/localnet terrad-upgrade-env)
bash contrib/updates/prepare_cosmovisor.sh $(BUILDDIR)
docker-compose -f ./contrib/updates/docker-compose.yml up -d

localnet-upgrade-stop:
docker-compose -f ./contrib/updates/docker-compose.yml down

localnet-stop:
docker-compose down

Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ func (app *TerraApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abc
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}

Expand Down
7 changes: 5 additions & 2 deletions contrib/localnet/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
all: terrad-env
all: terrad-env terrad-upgrade-env

terrad-env:
docker build --platform linux/amd64 --no-cache --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --tag classic-terra/terrad-env terrad-env

.PHONY: all terrad-env
terrad-upgrade-env:
docker build --platform linux/amd64 --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --build-arg entrypoint=cosmovisor-entrypoint.sh --tag classic-terra/terrad-upgrade-env terrad-env

.PHONY: all terrad-env terrad-upgrade-env
3 changes: 2 additions & 1 deletion contrib/localnet/terrad-env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RUN apt-get update && \

ARG UID=1000
ARG GID=1000
ARG entrypoint=entrypoint.sh

USER ${UID}:${GID}
VOLUME /terrad
Expand All @@ -15,4 +16,4 @@ ENTRYPOINT ["/usr/bin/entrypoint.sh"]
CMD ["start", "--log_format", "plain"]
STOPSIGNAL SIGTERM

COPY entrypoint.sh /usr/bin/entrypoint.sh
COPY ${entrypoint} /usr/bin/entrypoint.sh
25 changes: 25 additions & 0 deletions contrib/localnet/terrad-env/cosmovisor-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env sh

BINARY=/terrad/${BINARY:-cosmovisor}
ID=${ID:-0}
LOG=${LOG:-terrad.log}

if ! [ -f "${BINARY}" ]; then
echo "The binary $(basename "${BINARY}") cannot be found. Please add the binary to the shared folder. Please use the BINARY environment variable if the name of the binary is not 'terrad'"
exit 1
fi

BINARY_CHECK="$(file "$BINARY" | grep 'ELF 64-bit LSB executable, x86-64')"

if [ -z "${BINARY_CHECK}" ]; then
echo "Binary needs to be OS linux, ARCH amd64"
exit 1
fi

export TERRADHOME="/terrad/node${ID}/terrad"

if [ -d "$(dirname "${TERRADHOME}"/"${LOG}")" ]; then
"${BINARY}" run "$@" --home "${TERRADHOME}" | tee "${TERRADHOME}/${LOG}"
else
"${BINARY}" run "$@" --home "${TERRADHOME}"
fi
8 changes: 8 additions & 0 deletions contrib/updates/Dockerfile.cosmovisor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.18-alpine3.17

RUN set -eux; apk add --no-cache ca-certificates build-base;

# make cosmovisor statically linked
RUN go install -ldflags '-w -s -linkmode=external -extldflags "-Wl,-z,muldefs -static"' -trimpath cosmossdk.io/tools/cosmovisor/cmd/cosmovisor@latest

ENTRYPOINT [ "/bin/sh" ]
10 changes: 10 additions & 0 deletions contrib/updates/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all: build-cosmovisor-linux

build-cosmovisor-linux:
mkdir -p $(BUILDDIR)
docker build --platform linux/amd64 --no-cache --tag classic-terra/terraclassic.cosmovisor-binary --file Dockerfile.cosmovisor .
docker create --platform linux/amd64 --name temp classic-terra/terraclassic.cosmovisor-binary:latest
docker cp temp:/go/bin/cosmovisor $(BUILDDIR)/
docker rm temp

.PHONY: all build-cosmovisor-linux
83 changes: 83 additions & 0 deletions contrib/updates/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
version: '3'

services:
terradnode0:
container_name: terradnode0
image: "classic-terra/terrad-upgrade-env"
ports:
- "9090:9090"
- "26656-26657:26656-26657"
environment:
- ID=0
- LOG=$${LOG:-terrad.log}
- DAEMON_HOME=/terrad/node0/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.2

terradnode1:
container_name: terradnode1
image: "classic-terra/terrad-upgrade-env"
ports:
- "9091:9090"
- "26659-26660:26656-26657"
environment:
- ID=1
- LOG=$${LOG:-terrad.log}
- DAEMON_HOME=/terrad/node1/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.3

terradnode2:
container_name: terradnode2
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=2
- LOG=$${LOG:-terrad.log}
- DAEMON_HOME=/terrad/node2/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
ports:
- "9092:9090"
- "26661-26662:26656-26657"
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.4

terradnode3:
container_name: terradnode3
image: "classic-terra/terrad-upgrade-env"
environment:
- ID=3
- LOG=$${LOG:-terrad.log}
- DAEMON_HOME=/terrad/node3/terrad
- DAEMON_NAME=terrad
- DAEMON_RESTART_AFTER_UPGRADE=true
ports:
- "9093:9090"
- "26663-26664:26656-26657"
volumes:
- ../../build:/terrad:Z
networks:
localnet:
ipv4_address: 192.168.10.5

networks:
localnet:
driver: bridge
ipam:
driver: default
config:
-
subnet: 192.168.10.0/16
67 changes: 67 additions & 0 deletions contrib/updates/prepare_cosmovisor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# this bash will prepare cosmosvisor to the build folder so that it can perform upgrade
# this script is supposed to be run by Makefile

# These fields should be fetched automatically in the future
# Need to do more upgrade to see upgrade patterns
OLD_VERSION=v1.0.5
SOFTWARE_UPGRADE_NAME=$(ls -td -- ./app/upgrades/* | head -n 1 | cut -d'/' -f4)
BUILDDIR=$1

# check if BUILDDIR is set
if [ -z "$BUILDDIR" ]; then
echo "BUILDDIR is not set"
exit 1
fi

# install old version of terrad

## check if _build/classic-${OLD_VERSION} exists
if [ ! -d "_build/core-${OLD_VERSION:1}" ]; then
mkdir _build
wget -c "https://github.com/classic-terra/core/archive/refs/tags/${OLD_VERSION}.zip" -O _build/${OLD_VERSION}.zip
unzip _build/${OLD_VERSION}.zip -d _build
fi

## check if $BUILDDIR/old/terrad exists
if [ ! -f "$BUILDDIR/old/terrad" ]; then
mkdir -p $BUILDDIR/old
docker build --platform linux/amd64 --no-cache --build-arg source=./_build/core-${OLD_VERSION:1}/ --tag classic-terra/terraclassic.terrad-binary.old .
docker create --platform linux/amd64 --name old-temp classic-terra/terraclassic.terrad-binary.old:latest
docker cp old-temp:/usr/local/bin/terrad $BUILDDIR/old/
docker rm old-temp
fi

# prepare cosmovisor config in four nodes
if [ ! -f "$BUILDDIR/node0/terrad/config/genesis.json" ]; then docker run --rm \
--user $(id -u):$(id -g) \
-v $BUILDDIR:/terrad:Z \
-v /etc/group:/etc/group:ro \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/shadow:/etc/shadow:ro \
--entrypoint /terrad/old/terrad \
classic-terra/terrad-upgrade-env testnet --v 4 -o . --starting-ip-address 192.168.10.2 --keyring-backend=test --home /terrad;
fi

for i in {0..3}; do
CURRENT=$BUILDDIR/node$i/terrad

# change gov params voting_period
jq '.app_state.gov.voting_params.voting_period = "50s"' $CURRENT/config/genesis.json > $CURRENT/config/genesis.json.tmp && mv $CURRENT/config/genesis.json.tmp $CURRENT/config/genesis.json

docker run --rm \
--user $(id -u):$(id -g) \
-v $BUILDDIR:/terrad:Z \
-v /etc/group:/etc/group:ro \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/shadow:/etc/shadow:ro \
-e DAEMON_HOME=/terrad/node$i/terrad \
-e DAEMON_NAME=terrad \
-e DAEMON_RESTART_AFTER_UPGRADE=true \
--entrypoint /terrad/cosmovisor \
classic-terra/terrad-upgrade-env init /terrad/old/terrad
mkdir -p $CURRENT/cosmovisor/upgrades/$SOFTWARE_UPGRADE_NAME/bin
cp $BUILDDIR/terrad $CURRENT/cosmovisor/upgrades/$SOFTWARE_UPGRADE_NAME/bin
touch $CURRENT/cosmovisor/upgrades/$SOFTWARE_UPGRADE_NAME/upgrade-info.json
done
61 changes: 61 additions & 0 deletions contrib/updates/upgrade-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# should make this auto fetch upgrade name from app upgrades once many upgrades have been done
SOFTWARE_UPGRADE_NAME=$(ls -td -- ./app/upgrades/* | head -n 1 | cut -d'/' -f4)
NODE1_HOME=node1/terrad
BINARY_OLD="docker exec terradnode1 ./terrad"

# sleep to wait for localnet to come up
sleep 10

# 100 block from now
STATUS_INFO=($($BINARY_OLD status --home $NODE1_HOME | jq -r '.NodeInfo.network,.SyncInfo.latest_block_height'))
CHAIN_ID=${STATUS_INFO[0]}
UPGRADE_HEIGHT=$((STATUS_INFO[1] + 20))

$BINARY_OLD tx gov submit-proposal software-upgrade "$SOFTWARE_UPGRADE_NAME" --upgrade-height $UPGRADE_HEIGHT --upgrade-info "temp" --title "upgrade" --description "upgrade" --from node1 --keyring-backend test --chain-id $CHAIN_ID --home $NODE1_HOME -y

sleep 5

$BINARY_OLD tx gov deposit 1 "20000000uluna" --from node1 --keyring-backend test --chain-id $CHAIN_ID --home $NODE1_HOME -y

sleep 5

# loop from 0 to 3
for i in {0..3}; do
# check if docker for node i is running
if [[ $(docker ps -a | grep terradnode$i | wc -l) -eq 1 ]]; then
$BINARY_OLD tx gov vote 1 yes --from node$i --keyring-backend test --chain-id $CHAIN_ID --home "node$i/terrad" -y
sleep 5
fi
done

# keep track of block_height
while true; do
BLOCK_HEIGHT=$($BINARY_OLD status --home $NODE1_HOME | jq '.SyncInfo.latest_block_height' -r)
if [[ $BLOCK_HEIGHT -ge $UPGRADE_HEIGHT ]]; then
# assuming running only 1 terrad
echo "UPGRADE REACHED, CONTINUING NEW CHAIN"
break
else
$BINARY_OLD q gov proposal 1 --output=json --home $NODE1_HOME | jq ".status"
echo "BLOCK_HEIGHT = $BLOCK_HEIGHT"
sleep 10
fi
done

sleep 10

# check all nodes are online after upgrade
for i in {0..3}; do
if [[ $(docker ps -a | grep terradnode$i | wc -l) -eq 1 ]]; then
docker exec terradnode$i ./terrad status --home "node$i/terrad"
if [[ "${PIPESTATUS[0]}" != "0" ]]; then
echo "node$i is not online"
exit 1
fi
else
echo "terradnode$i is not running"
exit 1
fi
done
Loading

0 comments on commit 980cfa5

Please sign in to comment.