Skip to content

Commit

Permalink
better logic in upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vuong177 committed Mar 23, 2022
1 parent 054e902 commit 02e72ff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 113 deletions.
69 changes: 32 additions & 37 deletions app/app.go
Expand Up @@ -16,6 +16,10 @@ import (
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"

"github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
lupercalia "github.com/CosmosContracts/juno/app/upgrade"
"github.com/CosmosContracts/juno/docs"
"github.com/CosmosContracts/juno/x/mint"
mintkeeper "github.com/CosmosContracts/juno/x/mint/keeper"
Expand Down Expand Up @@ -101,12 +105,8 @@ import (
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
tmjson "github.com/tendermint/tendermint/libs/json"

"github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/prometheus/client_golang/prometheus"
tmjson "github.com/tendermint/tendermint/libs/json"

// this line is used by starport scaffolding # stargate/app/moduleImport

Expand Down Expand Up @@ -398,10 +398,6 @@ func New(
)
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, nil)

// upgrade handlers
cfg := module.NewConfigurator(appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.RegisterUpgradeHandlers(cfg)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
Expand Down Expand Up @@ -685,6 +681,12 @@ func New(
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

// upgrade handlers for lupercalia
app.setupUpgradeStoreLoaders()
cfg := module.NewConfigurator(appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.RegisterUpgradeHandlers(cfg, ICAModule)


if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down Expand Up @@ -828,35 +830,28 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
}

// RegisterUpgradeHandlers returns upgrade handlers
func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler("lupercalia", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// force an update of validator min commission
// we already did this for moneta
// but validators could have snuck in changes in the
// interim
// and via state sync to post-moneta
validators := app.StakingKeeper.GetAllValidators(ctx)
// hard code this because we don't want
// a) a fork or
// b) immediate reaction with additional gov props
minCommissionRate := sdk.NewDecWithPrec(5, 2)
for _, v := range validators {
if v.Commission.Rate.LT(minCommissionRate) {
if v.Commission.MaxRate.LT(minCommissionRate) {
v.Commission.MaxRate = minCommissionRate
}

v.Commission.Rate = minCommissionRate
v.Commission.UpdateTime = ctx.BlockHeader().Time

// call the before-modification hook since we're about to update the commission
app.StakingKeeper.BeforeValidatorModified(ctx, v.GetOperator())

app.StakingKeeper.SetValidator(ctx, v)
}
func (app *App) RegisterUpgradeHandlers(cfg module.Configurator, ica ica.AppModule) {
bankBaseKeeper, _ := app.BankKeeper.(bankkeeper.BaseKeeper)
app.UpgradeKeeper.SetUpgradeHandler(
"lupercalia",
lupercalia.CreateUpgradeHandler(app.mm, cfg, &bankBaseKeeper, ica)
)
}

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 upgradeInfo.Name == "lupercalia" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := store.StoreUpgrades{
Added: []string{icahosttypes.StoreKey, icacontrollerkeeper.StoreKey},
}
return app.mm.RunMigrations(ctx, cfg, vm)
})

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

// GetMaccPerms returns a copy of the module account permissions
Expand Down
118 changes: 42 additions & 76 deletions app/upgrade/upgrade_handler.go
@@ -1,100 +1,66 @@
package upgrade

import (
"fmt"

juno "github.com/CosmosContracts/juno/app"
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"
icamodule "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts"
icacontrollertypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
"github.com/tendermint/tendermint/libs/os"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

// Thank you, cosmos hub team.
// Juno team, we will need to add cw related messages here to ensure maximum interchain intercourse.
func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, bank *bankkeeper.BaseKeeper, icaModule icamodule.AppModule, app juno.App, loadLatest bool) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
app.UpgradeKeeper.SetUpgradeHandler(
"lupercalia",
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {

fromVM[icatypes.ModuleName] = icaModule.ConsensusVersion()
// create ICS27 Controller submodule params
controllerParams := icacontrollertypes.Params{}
// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
"cosmwasm.wasm.v1.MsgInstantiate",
"cosmwasm.wasm.v1.MsgExecute",
"cosmwasm.wasm.v1.MsgStoreCode",
"cosmwasm.wasm.v1.MsgMigrateContract",
"cosmwasm.wasm.v1.UpdateAdmin",
"cosmwasm.wasm.v1.MsgClearAdmin",
"/cosmos.authz.v1beta1.MsgExec",
"/cosmos.authz.v1beta1.MsgGrant",
"/cosmos.authz.v1beta1.MsgRevoke",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.bank.v1beta1.MsgMultiSend",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
"/cosmos.gov.v1beta1.MsgVoteWeighted",
"/cosmos.gov.v1beta1.MsgSubmitProposal",
"/cosmos.gov.v1beta1.MsgDeposit",
"/cosmos.gov.v1beta1.MsgVote",
"/cosmos.staking.v1beta1.MsgEditValidator",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgCreateValidator",
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount",
"/ibc.applications.transfer.v1.MsgTransfer",
},
}

ctx.Logger().Info("start to init interchainaccount module...")

// initialize ICS27 module
icaModule.InitModule(ctx, controllerParams, hostParams)

ctx.Logger().Info("start to run module migrations...")

return mm.RunMigrations(ctx, configurator, fromVM)
func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, bank *bankkeeper.BaseKeeper, icaModule icamodule.AppModule) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
fromVM[icatypes.ModuleName] = icaModule.ConsensusVersion()
// create ICS27 Controller submodule params
controllerParams := icacontrollertypes.Params{}
// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
"cosmwasm.wasm.v1.MsgInstantiate",
"cosmwasm.wasm.v1.MsgExecute",
"cosmwasm.wasm.v1.MsgStoreCode",
"cosmwasm.wasm.v1.MsgMigrateContract",
"cosmwasm.wasm.v1.UpdateAdmin",
"cosmwasm.wasm.v1.MsgClearAdmin",
"/cosmos.authz.v1beta1.MsgExec",
"/cosmos.authz.v1beta1.MsgGrant",
"/cosmos.authz.v1beta1.MsgRevoke",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.bank.v1beta1.MsgMultiSend",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
"/cosmos.gov.v1beta1.MsgVoteWeighted",
"/cosmos.gov.v1beta1.MsgSubmitProposal",
"/cosmos.gov.v1beta1.MsgDeposit",
"/cosmos.gov.v1beta1.MsgVote",
"/cosmos.staking.v1beta1.MsgEditValidator",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgCreateValidator",
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount",
"/ibc.applications.transfer.v1.MsgTransfer",
},
)

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

if upgradeInfo.Name == "lupercalia" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{
Added: []string{icahosttypes.StoreKey},
}
ctx.Logger().Info("start to init interchainaccount module...")

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

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
os.Exit(fmt.Sprintf("failed to load latest version: %s", err))
}
}
// initialize ICS27 module
icaModule.InitModule(ctx, controllerParams, hostParams)

return mm.RunMigrations(ctx, configurator, vm)
ctx.Logger().Info("start to run module migrations...")

return mm.RunMigrations(ctx, configurator, fromVM)
}
}

0 comments on commit 02e72ff

Please sign in to comment.