Skip to content

Commit

Permalink
Upgrade handler code-gen (#4031)
Browse files Browse the repository at this point in the history
* #3628

* #3632

* macos sed syntax

* fix ci token

* debug

* add token

* update E2E_UPGRADE_VERSION version on Makefile as well

* Update empty_upgrade_handler_gen.sh
  • Loading branch information
MasterPi-2124 authored Jan 18, 2023
1 parent 49dd541 commit 57d05f5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 70 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/gen-upgrade-on-manual-input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
name: On Trigger Gen Upgrade Handler

on:
release:
release:
types: [published]

workflow_dispatch:
inputs:
version:
description: 'Major vesrion to generate. Default value (v1) will ignore the action. Example format: v13'
description: "Major vesrion to generate. Default value (v1) will ignore the action. Example format: v13"
required: true
default: 'v1'
default: "v1"

jobs:
post_release:
Expand Down Expand Up @@ -45,14 +45,14 @@ jobs:
run: bash ./scripts/check_release.sh ${{ env.input }}

- name: Run post release script
if: env.MAJOR == 1 # 1 means vX of existing upgrade handler is smaller than A in tag vA.B.C
if: env.MAJOR == 1 # 1 means vX of existing upgrade handler is smaller than A in tag vA.B.C
run: bash ./scripts/empty_upgrade_handler_gen.sh ${{ env.input }}

- name: Create PR
if: env.MAJOR == 1
uses: peter-evans/create-pull-request@v4
with:
base: ${{ github.event.repository.default_branch }}
token: ${{ secrets.COMMIT_TO_BRANCH }}
title: "auto: code-gen upgrade handler ${{ env.input }}"
body: |
Update report
Expand All @@ -61,4 +61,8 @@ jobs:
labels: |
T:auto
C:e2e
C:app-wiring
V:state/breaking
C:app-wiring
base: ${{ github.event.repository.default_branch }}
branch: create-pull-request/upgrade-${{ env.input }}
delete-branch: true
154 changes: 90 additions & 64 deletions scripts/empty_upgrade_handler_gen.sh
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
#!/bin/bash

# 1) this script creates an empty directory in app/upgrades called "vX" where X is a previous version + 1 with an empty upgrade handler.
# 2) increases E2E_UPGRADE_VERSION in makefile by 1
# 3) adds new version to app.go
# 2) adds new version to app.go
# 3) increases E2E_UPGRADE_VERSION in makefile by 1
# 4) bumps up previous e2e-init version in tests/e2e/containers/config.go

# Also insures that all the imports make use of a current module version from go mod:
# (see: module=$(go mod edit -json | jq ".Module.Path") in this script)
# Github workflow which calls this script can be found here: osmosis/.github/workflows/auto-update-upgrade.yml

latest_version=0
for f in app/upgrades/*; do
s_f=(${f//// })
version=${s_f[2]}
num_version=${version//[!0-9]/}
if [[ $num_version -gt $latest_version ]]; then
LATEST_FILE=$f
latest_version=$num_version
fi
done
version_create=$1
new_file=./app/upgrades/${version_create}

mkdir $new_file
CONSTANTS_FILE=$new_file/constants.go
UPGRADES_FILE=$new_file/upgrades.go
touch $CONSTANTS_FILE
touch $UPGRADES_FILE
module=$(go mod edit -json | jq ".Module.Path")
module=${module%?}
path=${module%???}

bracks='"'
# set packages
echo -e "package ${version_create}\n" >> $CONSTANTS_FILE
echo -e "package ${version_create}\n" >> $UPGRADES_FILE

# imports
echo "import (" >> $CONSTANTS_FILE
echo "import (" >> $UPGRADES_FILE

# set imports for constants.go
echo -e "\t$module/app/upgrades$bracks\n" >> $CONSTANTS_FILE
echo -e '\tstore "github.com/cosmos/cosmos-sdk/store/types"' >> $CONSTANTS_FILE

# set imports for upgrades.go
echo -e '\tsdk "github.com/cosmos/cosmos-sdk/types"' >> $UPGRADES_FILE
echo -e '\t"github.com/cosmos/cosmos-sdk/types/module"' >> $UPGRADES_FILE
echo -e '\tupgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"\n' >> $UPGRADES_FILE
echo -e "\t$module/app/keepers$bracks" >> $UPGRADES_FILE
echo -e "\t$module/app/upgrades$bracks" >> $UPGRADES_FILE

# close import
echo ")" >> $UPGRADES_FILE
echo -e ")\n" >> $CONSTANTS_FILE
latest_version=0
for f in app/upgrades/*; do
s_f=(${f//// })
version=${s_f[2]}
num_version=${version//[!0-9]/}
if [[ $num_version -gt $latest_version ]]; then
LATEST_FILE=$f
latest_version=$num_version
fi
done
version_create=$1
new_file=./app/upgrades/${version_create}

mkdir $new_file
CONSTANTS_FILE=$new_file/constants.go
UPGRADES_FILE=$new_file/upgrades.go
touch $CONSTANTS_FILE
touch $UPGRADES_FILE

module=$(go mod edit -json | jq ".Module.Path")
module=${module%?}
path=${module%???}

bracks='"'
# set packages
echo -e "package ${version_create}\n" >> $CONSTANTS_FILE
echo -e "package ${version_create}\n" >> $UPGRADES_FILE

# imports
echo "import (" >> $CONSTANTS_FILE
echo "import (" >> $UPGRADES_FILE

# set imports for constants.go
echo -e "\t$module/app/upgrades$bracks\n" >> $CONSTANTS_FILE
echo -e '\tstore "github.com/cosmos/cosmos-sdk/store/types"' >> $CONSTANTS_FILE

# set imports for upgrades.go
echo -e '\tsdk "github.com/cosmos/cosmos-sdk/types"' >> $UPGRADES_FILE
echo -e '\t"github.com/cosmos/cosmos-sdk/types/module"' >> $UPGRADES_FILE
echo -e '\tupgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"\n' >> $UPGRADES_FILE
echo -e "\t$module/app/keepers$bracks" >> $UPGRADES_FILE
echo -e "\t$module/app/upgrades$bracks" >> $UPGRADES_FILE

# close import
echo ")" >> $UPGRADES_FILE
echo -e ")\n" >> $CONSTANTS_FILE

# constants.go logic
echo "// UpgradeName defines the on-chain upgrade name for the Osmosis $version_create upgrade." >> $CONSTANTS_FILE
echo "const UpgradeName = ${bracks}$version_create$bracks" >> $CONSTANTS_FILE
echo "
# constants.go logic
echo "// UpgradeName defines the on-chain upgrade name for the Osmosis $version_create upgrade." >> $CONSTANTS_FILE
echo "const UpgradeName = ${bracks}$version_create$bracks" >> $CONSTANTS_FILE
echo "
var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
Expand All @@ -68,8 +69,8 @@ var Upgrade = upgrades.Upgrade{
},
}" >> $CONSTANTS_FILE

# upgrades.go logic
echo "
# upgrades.go logic
echo "
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
Expand All @@ -81,15 +82,40 @@ func CreateUpgradeHandler(
}
}" >> $UPGRADES_FILE

# change app/app.go file
app_file=./app/app.go
UPGRADES_LINE=$(grep -F upgrades.Upgrade{ $app_file)
UPGRADES_LINE="${UPGRADES_LINE%?}, ${version_create}.Upgrade}"
sed -i "s|.*upgrades.Upgrade{.*|$UPGRADES_LINE|" $app_file
# change app/app.go file
app_file=./app/app.go
UPGRADES_LINE=$(grep -F upgrades.Upgrade{ $app_file)
UPGRADES_LINE="${UPGRADES_LINE%?}, ${version_create}.Upgrade}"
sed -i "s|.*upgrades.Upgrade{.*|$UPGRADES_LINE|" $app_file

PREV_IMPORT="v$latest_version $module/app/upgrades/v$latest_version$bracks"
NEW_IMPORT="$version_create $module/app/upgrades/$version_create$bracks"
sed -i "s|.*$PREV_IMPORT.*|\t$PREV_IMPORT\n\t$NEW_IMPORT|" $app_file

# change e2e version in makefile
sed -i "s/E2E_UPGRADE_VERSION := ${bracks}v$latest_version$bracks/E2E_UPGRADE_VERSION := ${bracks}$version_create$bracks/" ./Makefile

# bumps up prev e2e version
e2e_file=./tests/e2e/containers/config.go
PREV_OSMOSIS_DEV_TAG=$(curl -L -s 'https://registry.hub.docker.com/v2/repositories/osmolabs/osmosis-dev/tags?page=1&page_size=100' | jq -r '.results[] | .name | select(.|test("^(?:v|)[0-9]+\\.[0-9]+(?:$|\\.[0-9]+$)"))' | grep --max-count=1 "")
PREV_OSMOSIS_E2E_TAG=$(curl -L -s 'https://registry.hub.docker.com/v2/repositories/osmolabs/osmosis-e2e-init-chain/tags?page=1&page_size=100' | jq -r '.results[] | .name | select(.|test("^(?:v|)[0-9]+\\.[0-9]+(?:$|\\.[0-9]+$)"))' | grep --max-count=1 "")

PREV_IMPORT="v$latest_version $module/app/upgrades/v$latest_version$bracks"
NEW_IMPORT="$version_create $module/app/upgrades/$version_create$bracks"
sed -i "s|.*$PREV_IMPORT.*|\t$PREV_IMPORT\n\t$NEW_IMPORT|" $app_file
# previousVersionOsmoTag = PREV_OSMOSIS_DEV_TAG
if [[ $version_create == v$(($(echo $PREV_OSMOSIS_DEV_TAG | awk -F . '{print $1}')+1)) ]]; then
echo "Found previous osmosis-dev tag $PREV_OSMOSIS_DEV_TAG"
sed -i '/previousVersionOsmoTag/s/".*"/'"\"$PREV_OSMOSIS_DEV_TAG\""'/' $e2e_file
else
PREV_OSMOSIS_DEV_TAG=v$((${version_create:1}-1)).0.0
echo "Using pre-defined osmosis-dev tag: $PREV_OSMOSIS_DEV_TAG"
sed -i '/previousVersionOsmoTag/s/".*"/'"\"$PREV_OSMOSIS_DEV_TAG\""'/' $e2e_file
fi

# change e2e version in makefile
sed -i "s/E2E_UPGRADE_VERSION := ${bracks}v$latest_version$bracks/E2E_UPGRADE_VERSION := ${bracks}$version_create$bracks/" ./Makefile
# previousVersionInitTag = PREV_OSMOSIS_E2E_TAG
if [[ $version_create == v$(($(echo $PREV_OSMOSIS_E2E_TAG | awk -F . '{print $1}' | grep -Eo '[0-9]*')+1)) ]]; then
echo "Found previous osmosis-e2e-init-chain tag $PREV_OSMOSIS_E2E_TAG"
sed -i '/previousVersionInitTag/s/".*"/'"\"$PREV_OSMOSIS_E2E_TAG\""'/' $e2e_file
else
PREV_OSMOSIS_E2E_TAG=v$((${version_create:1}-1)).0.0
echo "Using pre-defined osmosis-e2e-init-chain tag: $PREV_OSMOSIS_E2E_TAG"
sed -i '/previousVersionInitTag/s/".*"/'"\"$PREV_OSMOSIS_E2E_TAG\""'/' $e2e_file
fi

0 comments on commit 57d05f5

Please sign in to comment.