Skip to content

Commit

Permalink
Fix migrate legacy params (#1729)
Browse files Browse the repository at this point in the history
* Fix migrate legacy params

* Better tests

* Handle upgrade from wasmd 33 chains
  • Loading branch information
alpe authored Nov 21, 2023
1 parent 9050b5f commit dffa321
Show file tree
Hide file tree
Showing 15 changed files with 1,742 additions and 108 deletions.
40 changes: 35 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"sort"
"strings"
"sync"

abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand All @@ -27,10 +28,12 @@ import (
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
transfer "github.com/cosmos/ibc-go/v8/modules/apps/transfer"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
Expand Down Expand Up @@ -240,6 +243,7 @@ type WasmApp struct {

// module configurator
configurator module.Configurator
once sync.Once
}

// NewWasmApp returns a reference to an initialized WasmApp.
Expand Down Expand Up @@ -920,6 +924,27 @@ func NewWasmApp(
return app
}

func (app *WasmApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
// when skipping sdk 47 for sdk 50, the upgrade handler is called too late in BaseApp
// this is a hack to ensure that the migration is executed when needed and not panics
app.once.Do(func() {
ctx := app.NewUncachedContext(false, tmproto.Header{})
if _, err := app.ConsensusParamsKeeper.Params(ctx, &consensusparamtypes.QueryParamsRequest{}); err != nil {
// prevents panic: consensus key is nil: collections: not found: key 'no_key' of type github.com/cosmos/gogoproto/tendermint.types.ConsensusParams
// sdk 47:
// Migrate Tendermint consensus parameters from x/params module to a dedicated x/consensus module.
// see https://github.com/cosmos/cosmos-sdk/blob/v0.47.0/simapp/upgrades.go#L66
baseAppLegacySS := app.GetSubspace(baseapp.Paramspace)
err := baseapp.MigrateParams(sdk.UnwrapSDKContext(ctx), baseAppLegacySS, app.ConsensusParamsKeeper.ParamsStore)
if err != nil {
panic(err)
}
}
})

return app.BaseApp.FinalizeBlock(req)
}

func (app *WasmApp) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey *storetypes.KVStoreKey) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
Expand Down Expand Up @@ -1174,10 +1199,15 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(govtypes.ModuleName)
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibcexported.ModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(icacontrollertypes.SubModuleName)

// register the IBC key tables for legacy param subspaces
keyTable := ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
paramsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
paramsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
paramsKeeper.Subspace(icacontrollertypes.SubModuleName).WithKeyTable(icacontrollertypes.ParamKeyTable())
paramsKeeper.Subspace(icahosttypes.SubModuleName).WithKeyTable(icahosttypes.ParamKeyTable())

paramsKeeper.Subspace(wasmtypes.ModuleName)
return paramsKeeper
}
69 changes: 47 additions & 22 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,49 @@ package app
import (
"fmt"

icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"

upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/baseapp"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/CosmWasm/wasmd/app/upgrades"
"github.com/CosmWasm/wasmd/app/upgrades/noop"
v050 "github.com/CosmWasm/wasmd/app/upgrades/v050"
v2 "github.com/CosmWasm/wasmd/x/wasm/migrations/v2"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

// Upgrades list of chain upgrades
var Upgrades = []upgrades.Upgrade{v050.Upgrade}

// RegisterUpgradeHandlers registers the chain upgrade handlers
func (app WasmApp) RegisterUpgradeHandlers() {
setupLegacyKeyTables(app.ParamsKeeper)
func (app *WasmApp) RegisterUpgradeHandlers() {
setupLegacyKeyTables(&app.ParamsKeeper)
if len(Upgrades) == 0 {
// always have a unique upgrade registered for the current version to test in system tests
Upgrades = append(Upgrades, noop.NewUpgrade(app.Version()))
}

keepers := upgrades.AppKeepers{AccountKeeper: app.AccountKeeper}
keepers := upgrades.AppKeepers{
AccountKeeper: &app.AccountKeeper,
ParamsKeeper: &app.ParamsKeeper,
ConsensusParamsKeeper: &app.ConsensusParamsKeeper,
CapabilityKeeper: app.CapabilityKeeper,
IBCKeeper: app.IBCKeeper,
Codec: app.appCodec,
GetStoreKey: app.GetKey,
}
app.GetStoreKeys()
// register all upgrade handlers
for _, upgrade := range Upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
Expand Down Expand Up @@ -62,23 +76,31 @@ func (app WasmApp) RegisterUpgradeHandlers() {
}
}

func setupLegacyKeyTables(k paramskeeper.Keeper) {
// Set param key table for params module migration
func setupLegacyKeyTables(k *paramskeeper.Keeper) {
for _, subspace := range k.GetSubspaces() {
subspace := subspace

var keyTable paramstypes.KeyTable
switch subspace.Name() {
// ibc types
case ibcexported.ModuleName:
keyTable = ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
case ibctransfertypes.ModuleName:
keyTable = ibctransfertypes.ParamKeyTable()
case icahosttypes.SubModuleName:
keyTable = icahosttypes.ParamKeyTable()
case icacontrollertypes.SubModuleName:
keyTable = icacontrollertypes.ParamKeyTable()
case authtypes.ModuleName:
keyTable = authtypes.ParamKeyTable() //nolint:staticcheck
case banktypes.ModuleName:
keyTable = banktypes.ParamKeyTable() //nolint:staticcheck
case stakingtypes.ModuleName:
keyTable = stakingtypes.ParamKeyTable() //nolint:staticcheck
case minttypes.ModuleName:
keyTable = minttypes.ParamKeyTable() //nolint:staticcheck
case distrtypes.ModuleName:
keyTable = distrtypes.ParamKeyTable() //nolint:staticcheck
case slashingtypes.ModuleName:
keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable() //nolint:staticcheck
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck
// wasm
case wasmtypes.ModuleName:
keyTable = v2.ParamKeyTable() //nolint:staticcheck
default:
continue
}
Expand All @@ -87,4 +109,7 @@ func setupLegacyKeyTables(k paramskeeper.Keeper) {
subspace.WithKeyTable(keyTable)
}
}
// sdk 47
k.Subspace(baseapp.Paramspace).
WithKeyTable(paramstypes.ConsensusParamsKeyTable())
}
14 changes: 13 additions & 1 deletion app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ package upgrades
import (
"context"

capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
)

type AppKeepers struct {
authkeeper.AccountKeeper
AccountKeeper *authkeeper.AccountKeeper
ParamsKeeper *paramskeeper.Keeper
ConsensusParamsKeeper *consensusparamkeeper.Keeper
Codec codec.Codec
GetStoreKey func(storeKey string) *storetypes.KVStoreKey
CapabilityKeeper *capabilitykeeper.Keeper
IBCKeeper *ibckeeper.Keeper
}
type ModuleManager interface {
RunMigrations(ctx context.Context, cfg module.Configurator, fromVM module.VersionMap) (module.VersionMap, error)
Expand Down
1 change: 1 addition & 0 deletions app/upgrades/v050/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func CreateUpgradeHandler(
configurator module.Configurator,
ak *upgrades.AppKeepers,
) upgradetypes.UpgradeHandler {
// sdk 47 to sdk 50
return func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return mm.RunMigrations(ctx, configurator, fromVM)
}
Expand Down
73 changes: 73 additions & 0 deletions app/upgrades/v050/upgrades.go_wasmd_33_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package v050

import (
"context"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
v6 "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/migrations/v6"
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
ibctmmigrations "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint/migrations"

storetypes "cosmossdk.io/store/types"
circuittypes "cosmossdk.io/x/circuit/types"
"cosmossdk.io/x/nft"
upgradetypes "cosmossdk.io/x/upgrade/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/group"

"github.com/CosmWasm/wasmd/app/upgrades"
)

// UpgradeName defines the on-chain upgrade name
const UpgradeName = "v0.50"

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: storetypes.StoreUpgrades{
Added: []string{
// SDK 46
group.ModuleName,
nft.ModuleName,
// SDK 47
crisistypes.ModuleName,
consensusparamtypes.ModuleName,
// SDK 50
circuittypes.ModuleName,
},
Deleted: []string{},
},
}

func CreateUpgradeHandler(
mm upgrades.ModuleManager,
configurator module.Configurator,
ak *upgrades.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
// ibc v6
// NOTE: The moduleName arg of v6.CreateUpgradeHandler refers to the auth module ScopedKeeper name to which the channel capability should be migrated from.
// This should be the same string value provided upon instantiation of the ScopedKeeper with app.CapabilityKeeper.ScopeToModule()
const moduleName = icacontrollertypes.SubModuleName
if err := v6.MigrateICS27ChannelCapability(sdkCtx, ak.Codec, ak.GetStoreKey(capabilitytypes.ModuleName),
ak.CapabilityKeeper, moduleName); err != nil {
return nil, err
}

// ibc v7
if _, err := ibctmmigrations.PruneExpiredConsensusStates(sdkCtx, ak.Codec, ak.IBCKeeper.ClientKeeper); err != nil {
return nil, err
}

// sdk 47
// consensus params migration is done in app.go FinalizeBlock function to prevent panic

// sdk 50
return mm.RunMigrations(ctx, configurator, fromVM)
}
}
2 changes: 1 addition & 1 deletion cmd/wasmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewRootCmd() *cobra.Command {
cfg.Seal()
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
// note, this is not necessary when using app wiring, as depinject can be directly used (see root_v2.go)
tempApp := app.NewWasmApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir()), []wasmkeeper.Option{})
tempApp := app.NewWasmApp(log.NewNopLogger(), dbm.NewMemDB(), nil, false, simtestutil.NewAppOptionsWithFlagHome(tempDir()), []wasmkeeper.Option{})
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
Expand Down
35 changes: 31 additions & 4 deletions x/wasm/keeper/migrations_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package keeper_test
import (
"testing"

"github.com/cometbft/cometbft/libs/rand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

upgradetypes "cosmossdk.io/x/upgrade/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/CosmWasm/wasmd/app"
v2 "github.com/CosmWasm/wasmd/x/wasm/migrations/v2"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

func TestModuleMigrations(t *testing.T) {
wasmApp := app.Setup(t)
myAddress := sdk.AccAddress(rand.Bytes(address.Len))

upgradeHandler := func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { //nolint:unparam
return wasmApp.ModuleManager.RunMigrations(ctx, wasmApp.Configurator(), fromVM)
Expand All @@ -30,15 +34,15 @@ func TestModuleMigrations(t *testing.T) {
"with legacy params migrated": {
startVersion: 1,
setup: func(ctx sdk.Context) {
params := types.Params{
CodeUploadAccess: types.AllowNobody,
InstantiateDefaultPermission: types.AccessTypeNobody,
params := v2.Params{
CodeUploadAccess: v2.AccessConfig{Permission: v2.AccessTypeNobody},
InstantiateDefaultPermission: v2.AccessTypeNobody,
}

// upgrade code shipped with v0.40
// https://github.com/CosmWasm/wasmd/blob/v0.40.0/app/upgrades.go#L66
sp, _ := wasmApp.ParamsKeeper.GetSubspace(types.ModuleName)
keyTable := types.ParamKeyTable()
keyTable := v2.ParamKeyTable()
if !sp.HasKeyTable() {
sp.WithKeyTable(keyTable)
}
Expand All @@ -50,6 +54,29 @@ func TestModuleMigrations(t *testing.T) {
InstantiateDefaultPermission: types.AccessTypeNobody,
},
},
"with legacy one address type replaced": {
startVersion: 1,
setup: func(ctx sdk.Context) {
params := v2.Params{
CodeUploadAccess: v2.AccessConfig{Permission: v2.AccessTypeOnlyAddress, Address: myAddress.String()},
InstantiateDefaultPermission: v2.AccessTypeNobody,
}

// upgrade code shipped with v0.40
// https://github.com/CosmWasm/wasmd/blob/v0.40.0/app/upgrades.go#L66
sp, _ := wasmApp.ParamsKeeper.GetSubspace(types.ModuleName)
keyTable := v2.ParamKeyTable()
if !sp.HasKeyTable() {
sp.WithKeyTable(keyTable)
}

sp.SetParamSet(ctx, &params)
},
exp: types.Params{
CodeUploadAccess: types.AccessTypeAnyOfAddresses.With(myAddress),
InstantiateDefaultPermission: types.AccessTypeNobody,
},
},
"fresh from genesis": {
startVersion: wasmApp.ModuleManager.GetVersionMap()[types.ModuleName], // latest
setup: func(ctx sdk.Context) {},
Expand Down
Loading

0 comments on commit dffa321

Please sign in to comment.