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

Added Upgrade Scripts to Dockernet #307

Merged
merged 8 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ scripts/tests/validator_selection
scripts/tests/local_tests.bats
scripts-local/upgrades/binaries
scripts-local/upgrades/cosmovisor
scripts/upgrades/binaries
scripts/upgrades/cosmovisor
vue/*
# Test binary, built with `go test -c`
*.test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You can optionally pass build arguments to specify which binary to rebuild
2. `g` This will re-build the Gaia binary
3. `j` This will re-build the Juno binary
4. `j` This will re-build the Osmo binary
5. `j` This will re-build the Stargaze binary
5. `t` This will re-build the Stargaze binary
sampocs marked this conversation as resolved.
Show resolved Hide resolved
6. `r` This will re-build the Go Relayer binary
7. `h` This will re-build the Hermes binary
8. `i` This will re-build the ICQ binary
Expand Down
2 changes: 1 addition & 1 deletion scripts/init_chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ $MAIN_NODE_CMD collect-gentxs &> /dev/null
# wipe out the persistent peers for the main node (these are incorrectly autogenerated for each validator during collect-gentxs)
sed -i -E "s|persistent_peers = .*|persistent_peers = \"\"|g" $MAIN_CONFIG

# update chian-specific genesis settings
# update chain-specific genesis settings
if [ "$CHAIN_ID" == "$STRIDE_CHAIN_ID" ]; then
set_stride_genesis $MAIN_GENESIS
else
Expand Down
5 changes: 2 additions & 3 deletions scripts/register_host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ HOST_ZONE_NUM="$2"
CONNECTION="connection-$HOST_ZONE_NUM"
CHANNEL="channel-$HOST_ZONE_NUM"

MAIN_CMD=$(GET_VAR_VALUE ${CHAIN_ID}_MAIN_CMD)
VAL_PREFIX=$(GET_VAR_VALUE ${CHAIN_ID}_VAL_PREFIX)
IBC_DENOM=$(GET_VAR_VALUE IBC_${CHAIN_ID}_CHANNEL_${HOST_ZONE_NUM}_DENOM)
HOST_DENOM=$(GET_VAR_VALUE ${CHAIN_ID}_DENOM)
Expand All @@ -20,7 +19,7 @@ NUM_VALS=$(GET_VAR_VALUE ${CHAIN_ID}_NUM_NODES)
echo "$CHAIN_ID - Registering host zone..."
$STRIDE_MAIN_CMD tx stakeibc register-host-zone \
$CONNECTION $HOST_DENOM $ADDRESS_PREFIX $IBC_DENOM $CHANNEL 1 \
--gas 1000000 --from $STRIDE_ADMIN_ACCT --home $SCRIPT_DIR/state/stride1 -y | grep -E "code:|txhash:" | sed 's/^/ /'
--gas 1000000 --from $STRIDE_ADMIN_ACCT --home $SCRIPT_DIR/state/stride1 -y | TRIM_TX
sleep 4

echo "$CHAIN_ID - Registering validators..."
Expand All @@ -30,7 +29,7 @@ for (( i=1; i <= $NUM_VALS; i++ )); do
weight=${weights[$i]}

$STRIDE_MAIN_CMD tx stakeibc add-validator $CHAIN_ID ${VAL_PREFIX}${i} $delegate_val 10 $weight \
--from $STRIDE_ADMIN_ACCT -y | grep -E "code:|txhash:" | sed 's/^/ /'
--from $STRIDE_ADMIN_ACCT -y | TRIM_TX
sleep 4
done

Expand Down
25 changes: 23 additions & 2 deletions scripts/start_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@ set -eu
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source ${SCRIPT_DIR}/vars.sh

HOST_CHAINS=(GAIA JUNO OSMO STARS)

# cleanup any stale state
docker-compose down
make stop-docker
rm -rf $SCRIPT_DIR/state $SCRIPT_DIR/logs/*.log $SCRIPT_DIR/logs/temp
mkdir -p $SCRIPT_DIR/logs

HERMES_LOGS=$SCRIPT_DIR/logs/hermes.log
ICQ_LOGS=$SCRIPT_DIR/logs/icq.log

HOST_CHAINS=(GAIA JUNO OSMO STARS)
# If we're testing an upgrade, setup cosmovisor
if [[ "$UPGRADE_NAME" != "" ]]; then
printf "\n>>> UPGRADE ENABLED! ($UPGRADE_NAME)\n\n"

# Update binary #2 with the binary that was just compiled
mkdir -p $SCRIPT_DIR/upgrades/binaries
rm -f $SCRIPT_DIR/upgrades/binaries/strided2
cp $SCRIPT_DIR/../build/strided $SCRIPT_DIR/upgrades/binaries/strided2

# Build a cosmovisor image with the old binary and replace the stride docker image with a new one
# that has both binaries and is running cosmovisor
# The reason for having a separate cosmovisor image is so we can cache the building of cosmovisor and the old binary
echo "Building Cosmovisor..."
docker build -t stridezone:cosmovisor --build-arg old_commit_hash=$UPGRADE_OLD_COMMIT_HASH -f ${SCRIPT_DIR}/upgrades/Dockerfile.cosmovisor .

echo "Re-Building Stride with Upgrade Support..."
docker build -t stridezone:stride -f ${SCRIPT_DIR}/upgrades/Dockerfile.stride .

echo "Done"
fi

# Initialize the state for each chain
for chain_id in STRIDE ${HOST_CHAINS[@]}; do
Expand Down
2 changes: 1 addition & 1 deletion scripts/test-util/4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ source ${SCRIPT_DIR}/../vars.sh
$GAIA_MAIN_CMD q bank balances $GAIA_RECEIVER_ACCT

#claim stake
EPOCH=5
EPOCH=$($STRIDE_MAIN_CMD q records list-user-redemption-record | grep -Fiw 'epochNumber' | head -n 1 | grep -o -E '[0-9]+')
SENDER=stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7
$STRIDE_MAIN_CMD tx stakeibc claim-undelegated-tokens GAIA $EPOCH $(STRIDE_ADDRESS) --from ${STRIDE_VAL_PREFIX}1 -y

Expand Down
26 changes: 26 additions & 0 deletions scripts/upgrades/Dockerfile.cosmovisor
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# syntax = docker/dockerfile:1
FROM golang:1.18-alpine3.15 AS builder

ARG old_commit_hash
RUN test -n "$old_commit_hash"

WORKDIR /opt/

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

# Install cosmovisor
RUN git clone https://github.com/cosmos/cosmos-sdk \
&& cd cosmos-sdk \
&& git checkout cosmovisor/v1.1.0
RUN --mount=type=cache,target=/root/.cache/go-build cd /opt/cosmos-sdk && make cosmovisor

# Build the old binary
RUN git clone https://github.com/Stride-Labs/stride.git \
&& cd stride \
&& git checkout $old_commit_hash \
&& sed -i -E "s|stride1k8c2m5cn322akk5wy8lpt87dd2f4yh9azg7jlh|stride1u20df3trc2c2zdhm8qvh2hdjx9ewh00sv6eyy8|g" utils/utils.go \
&& env GOOS=linux GOARCH=amd64 go build -mod=readonly -trimpath -o /opt/build/ ./... \
&& mv /opt/build/strided /opt/build/strided1
RUN --mount=type=cache,target=/root/.cache/go-build cd /opt/stride && make build


24 changes: 24 additions & 0 deletions scripts/upgrades/Dockerfile.stride
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# syntax = docker/dockerfile:1
FROM alpine:3.15

ENV DAEMON_NAME=strided
ENV DAEMON_HOME=/home/stride/.stride
ENV DAEMON_RESTART_AFTER_UPGRADE=true
ENV COSMOVISOR_HOME=/home/stride/cosmovisor

RUN apk add --update vim bash \
&& addgroup -g 1000 stride \
&& adduser -S -h /home/stride -D stride -u 1000 -G stride

COPY --from=stridezone:cosmovisor /opt/cosmos-sdk/cosmovisor/cosmovisor /usr/local/bin/cosmovisor
COPY --from=stridezone:cosmovisor --chown=stride:stride /opt/build/strided1 ${COSMOVISOR_HOME}/genesis/bin/strided
COPY --from=stridezone:stride --chown=stride:stride /usr/local/bin/strided ${COSMOVISOR_HOME}/upgrades/${UPGRADE_NAME}/bin/strided

USER stride
WORKDIR /home/stride

EXPOSE 26657 26656 1317 9090

RUN echo "mv ${COSMOVISOR_HOME} ${DAEMON_HOME}/cosmovisor && cosmovisor run start" > start.sh

CMD ["bash", "start.sh" ]
36 changes: 36 additions & 0 deletions scripts/upgrades/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Testing Upgrades in Local Mode
## Run Instructions
* Before working on the upgrade logic, compile the original binary and place it in `scripts/upgrades/binaries/` named `strided1`
* **This binary should represent the code before the upgrade changes. You'll likely want to checkout to the main branch to compile this.**
```
git checkout {OLD_COMMIT_HASH}
make build-docker build=s
mkdir -p scripts/upgrades/binaries
cp build/strided scripts/upgrades/binaries/strided1
git checkout {UPDATED_BRANCH}
```
* Then switch the code back to the most recent version
* Enter the commit hash of the old binary (built above) as `UPGRADE_OLD_COMMIT_HASH` in `scripts/vars.sh`
* Enter upgrade name as `UPGRADE_NAME` in `scripts/vars.sh` and `PROPOSAL_NAME` in `scripts/submit_upgrade.sh`
* Optionally update timing parameters the upgrade height (`UPGRADE_HEIGHT` in `scripts/submit_upgrade.sh`)
* Then startup the chain as normal and rebuild stride
```
make start-docker build=s
```
* The startup script will:
* Compile the new binary
* Create the cosmosvisor file structure required for upgrades
* Rebuild and replace the stride docker image with an image that has both binaries and is running cosmosvisor
* This image pulls the new binary from the normal docker build that happens at the start of running this make command
* Once the chain is up and running, run the upgrade script to propose and vote on an upgrade
```
bash scripts/upgrades/submit_upgrade.sh
```
* To view the status of the proposal (and confirm it has passed), open up a new terminal window and run
```
bash scripts/upgrades/view_proposal_status.sh
```
* It will first print the time at which voting ends, and then continuously probe the status. You must see "PROPOSAL_STATUS_PASSED" before the upgrade height in order for the upgrade to go through.
* While the chain is running, now is a good time to test your pre-upgrade condition (i.e. some validation that indicates you are on the old binary). When doing so, use `scripts/upgrades/binaries/strided1`
* View the stride logs - you should notice an update occuring at the specified upgrade height.
* After the upgrade has occured, check a post-upgrade condition using `scripts/upgrades/binaries/strided2`
52 changes: 52 additions & 0 deletions scripts/upgrades/submit_upgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

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

UPGRADE_HEIGHT=250

printf "PROPOSAL\n"
$STRIDE_MAIN_CMD tx gov submit-proposal software-upgrade $UPGRADE_NAME \
--title $UPGRADE_NAME --description "version 2 description" \
--upgrade-height $UPGRADE_HEIGHT --from val1 -y | TRIM_TX

sleep 5
printf "\nPROPOSAL CONFIRMATION\n"
$STRIDE_MAIN_CMD query gov proposals

sleep 5
printf "\nDEPOSIT\n"
$STRIDE_MAIN_CMD tx gov deposit 1 10000001ustrd --from val1 -y | TRIM_TX

sleep 5
printf "\nDEPOSIT CONFIRMATION\n"
$STRIDE_MAIN_CMD query gov deposits 1

sleep 5
printf "\nVOTING\n"
$STRIDE_MAIN_CMD tx gov vote 1 yes --from val1 -y | TRIM_TX
$STRIDE_MAIN_CMD tx gov vote 1 yes --from val2 -y | TRIM_TX
$STRIDE_MAIN_CMD tx gov vote 1 yes --from val3 -y | TRIM_TX

sleep 5
printf "\nVOTE CONFIRMATION\n"
$STRIDE_MAIN_CMD query gov tally 1

printf "\nPROPOSAL STATUS\n"
while true; do
status=$($STRIDE_MAIN_CMD query gov proposal 1 | grep "status" | awk '{printf $2}')
if [[ "$status" == "PROPOSAL_STATUS_VOTING_PERIOD" ]]; then
echo "Proposal still in progress..."
sleep 5
elif [[ "$status" == "PROPOSAL_STATUS_PASSED" ]]; then
echo "Proposal passed!"
exit 0
elif [[ "$status" == "PROPOSAL_STATUS_REJECTED" ]]; then
echo "Proposal Failed!"
exit 1
else
echo "Unknown proposal status: $status"
exit 1
fi
done
16 changes: 15 additions & 1 deletion scripts/vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ STRIDE_LOGS=$LOGS/stride.log
TX_LOGS=$SCRIPT_DIR/logs/tx.log
KEYS_LOGS=$SCRIPT_DIR/logs/keys.log

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

# DENOMS
ATOM_DENOM='uatom'
JUNO_DENOM='ujuno'
Expand Down Expand Up @@ -71,12 +75,18 @@ ADMIN_TOKENS=1000000000
STRIDE_CHAIN_ID=STRIDE
STRIDE_NODE_PREFIX=stride
STRIDE_NUM_NODES=3
STRIDE_CMD="$SCRIPT_DIR/../build/strided"
STRIDE_VAL_PREFIX=val
STRIDE_DENOM=$STRD_DENOM
STRIDE_RPC_PORT=26657
STRIDE_ADMIN_ACCT=admin
STRIDE_ADMIN_ADDRESS=stride1u20df3trc2c2zdhm8qvh2hdjx9ewh00sv6eyy8

# Binaries are contigent on whether we're doing an upgrade or not
if [[ "$UPGRADE_NAME" == "" ]]; then
STRIDE_CMD="$SCRIPT_DIR/../build/strided"
else
STRIDE_CMD="$SCRIPT_DIR/upgrades/binaries/strided1"
fi
STRIDE_MAIN_CMD="$STRIDE_CMD --home $SCRIPT_DIR/state/${STRIDE_NODE_PREFIX}1"

STRIDE_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"
Expand Down Expand Up @@ -297,4 +307,8 @@ GET_ICA_ADDR() {
ica_type="$2" #delegation, fee, redemption, or withdrawal

$STRIDE_MAIN_CMD q stakeibc show-host-zone $chain_id | grep ${ica_type}Account -A 1 | grep address | awk '{print $2}'
}

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