Skip to content

Commit

Permalink
feat(upgrade): create package for register upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 6, 2023
1 parent b65bf92 commit ef21308
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,8 @@ func New(
app.ScopedICAControllerKeeper = scopedICAControllerKeeper
app.ScopedInterTxKeeper = scopedInterTxKeeper

app.setupUpgradeHandlers()

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down
37 changes: 37 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package app

import (
"fmt"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
v4 "github.com/okp4/okp4d/app/upgrades/v4"
)

func (app *App) setupUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler(
v4.UpgradeName,
v4.CreateUpgradeHandler(app.mm, app.configurator),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("failed to read upgrade info from disk: %w", err))
}

if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}

var storeUpgrades *storetypes.StoreUpgrades
switch upgradeInfo.Name {
case v4.UpgradeName:
storeUpgrades = v4.StoreUpgrades
}

if storeUpgrades != nil {
// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades))
}

}
26 changes: 26 additions & 0 deletions app/upgrades/v4/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v4

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const UpgradeName = "v4.0.0"

var StoreUpgrades = &storetypes.StoreUpgrades{
Added: []string{"logic"},
}

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", UpgradeName)

logger.Debug("running module migrations...")
return mm.RunMigrations(ctx, configurator, vm)
}
}

0 comments on commit ef21308

Please sign in to comment.