Skip to content

Commit

Permalink
Merge pull request #192 from neutron-org/fix/upgrade
Browse files Browse the repository at this point in the history
Fix: upgrade fix
  • Loading branch information
pr0n00gler authored Apr 4, 2023
2 parents 624af11 + a6aac56 commit b24c6e1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
48 changes: 46 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"strings"

"github.com/neutron-org/neutron/app/upgrades"
v3 "github.com/neutron-org/neutron/app/upgrades/v3"
"github.com/neutron-org/neutron/x/cron"

"github.com/CosmWasm/wasmd/x/wasm"
Expand Down Expand Up @@ -80,8 +82,6 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v4/modules/core/keeper"
"github.com/cosmos/interchain-security/legacy_ibc_testing/core"
ibctesting "github.com/cosmos/interchain-security/legacy_ibc_testing/testing"
cronkeeper "github.com/neutron-org/neutron/x/cron/keeper"
crontypes "github.com/neutron-org/neutron/x/cron/types"
"github.com/spf13/cast"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
Expand All @@ -90,6 +90,9 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

cronkeeper "github.com/neutron-org/neutron/x/cron/keeper"
crontypes "github.com/neutron-org/neutron/x/cron/types"

"github.com/neutron-org/neutron/x/tokenfactory"
tokenfactorykeeper "github.com/neutron-org/neutron/x/tokenfactory/keeper"
tokenfactorytypes "github.com/neutron-org/neutron/x/tokenfactory/types"
Expand Down Expand Up @@ -169,6 +172,8 @@ func GetEnabledProposals() []wasm.ProposalType {
}

var (
Upgrades = []upgrades.Upgrade{v3.Upgrade}

// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string

Expand Down Expand Up @@ -258,6 +263,8 @@ type App struct {
appCodec codec.Codec
interfaceRegistry types.InterfaceRegistry

configurator module.Configurator

encodingConfig appparams.EncodingConfig

invCheckPeriod uint
Expand Down Expand Up @@ -644,6 +651,8 @@ func New(
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.

app.setupUpgradeStoreLoaders()

app.mm = module.NewManager(
auth.NewAppModule(appCodec, app.AccountKeeper, nil),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
Expand Down Expand Up @@ -771,8 +780,11 @@ func New(

app.mm.RegisterInvariants(&app.CrisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.configurator = module.NewConfigurator(app.AppCodec(), app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()))

app.setupUpgradeHandlers()

// create the simulation manager and define the order of the modules for deterministic simulations
app.sm = module.NewSimulationManager(
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
Expand Down Expand Up @@ -857,6 +869,38 @@ func New(
return app
}

func (app *App) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

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

for _, upgrade := range Upgrades {
if upgradeInfo.Name == upgrade.UpgradeName {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgrade.StoreUpgrades))
}
}
}

func (app *App) setupUpgradeHandlers() {
for _, upgrade := range Upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
upgrade.UpgradeName,
upgrade.CreateUpgradeHandler(
app.mm,
app.configurator,
app.InterchainQueriesKeeper,
app.CronKeeper,
app.TokenFactoryKeeper,
),
)
}
}

// Name returns the name of the App
func (app *App) Name() string { return app.BaseApp.Name() }

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

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

cronkeeper "github.com/neutron-org/neutron/x/cron/keeper"
icqkeeper "github.com/neutron-org/neutron/x/interchainqueries/keeper"
tokenfactorykeeper "github.com/neutron-org/neutron/x/tokenfactory/keeper"
)

// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal
// must have written, in order for the state migration to go smoothly.
// An upgrade must implement this struct, and then set it in the app.go.
// The app.go will then define the handler.
type Upgrade struct {
// Upgrade version name, for the upgrade handler, e.g. `v7`
UpgradeName string

// CreateUpgradeHandler defines the function that creates an upgrade handler
CreateUpgradeHandler func(*module.Manager, module.Configurator, icqkeeper.Keeper, cronkeeper.Keeper, *tokenfactorykeeper.Keeper) upgradetypes.UpgradeHandler

// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades store.StoreUpgrades
}
23 changes: 23 additions & 0 deletions app/upgrades/v3/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v3

import (
store "github.com/cosmos/cosmos-sdk/store/types"
ccvprovider "github.com/cosmos/interchain-security/x/ccv/provider/types"

"github.com/neutron-org/neutron/app/upgrades"
)

const (
// UpgradeName defines the on-chain upgrades name.
UpgradeName = "v0.3.0"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{
ccvprovider.ModuleName,
},
},
}
39 changes: 39 additions & 0 deletions app/upgrades/v3/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package v3

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

cronkeeper "github.com/neutron-org/neutron/x/cron/keeper"
crontypes "github.com/neutron-org/neutron/x/cron/types"
icqkeeper "github.com/neutron-org/neutron/x/interchainqueries/keeper"
icqtypes "github.com/neutron-org/neutron/x/interchainqueries/types"
tokenfactorykeeper "github.com/neutron-org/neutron/x/tokenfactory/keeper"
tokenfactorytypes "github.com/neutron-org/neutron/x/tokenfactory/types"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
icqKeeper icqkeeper.Keeper,
cronKeeper cronkeeper.Keeper,
tokenfactoryKeeper *tokenfactorykeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting module migrations...")

// todo: FIXME
icqKeeper.SetParams(ctx, icqtypes.DefaultParams())
cronKeeper.SetParams(ctx, crontypes.DefaultParams())
tokenfactoryKeeper.SetParams(ctx, tokenfactorytypes.DefaultParams())

vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

ctx.Logger().Info("Upgrade complete")
return vm, err
}
}

0 comments on commit b24c6e1

Please sign in to comment.