Skip to content

Commit

Permalink
option with default
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Apr 16, 2024
1 parent ef89bbe commit 1670d17
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
35 changes: 16 additions & 19 deletions modules/apps/27-interchain-accounts/host/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

// Migrator is a struct for handling in-place state migrations.
type Migrator struct {
keeper *Keeper
keeper *Keeper
useDefaultParams bool
}

// NewMigrator returns Migrator instance for the state migration.
Expand All @@ -18,29 +19,25 @@ func NewMigrator(k *Keeper) Migrator {
}
}

// NewMigratorWithDefaultParams returns a Migrator instance for state migration,
// initialized with default parameters.
func NewMigratorWithDefaultParams(k *Keeper) Migrator {
migrator := NewMigrator(k)
migrator.useDefaultParams = true
return migrator
}

// MigrateParams migrates the host submodule's parameters from the x/params to self store.
func (m Migrator) MigrateParams(ctx sdk.Context) error {
if m.keeper != nil {
defaultParams := types.DefaultParams()
var params types.Params
ps := &params
for _, pair := range ps.ParamSetPairs() {
if !m.keeper.legacySubspace.Has(ctx, pair.Key) {
var value interface{}
switch string(pair.Key) {
case "HostEnabled":
value = defaultParams.HostEnabled
case "AllowMessages":
value = defaultParams.AllowMessages
default:
continue
}
m.keeper.legacySubspace.Set(ctx, pair.Key, value)
if m.useDefaultParams {
params = types.DefaultParams()
} else {
m.keeper.legacySubspace.GetParamSet(ctx, &params)
if err := params.Validate(); err != nil {
return err
}
m.keeper.legacySubspace.Get(ctx, pair.Key, pair.Value)
}
if err := params.Validate(); err != nil {
return err
}
m.keeper.SetParams(ctx, params)
m.keeper.Logger(ctx).Info("successfully migrated ica/host submodule to self-manage params")
Expand Down
19 changes: 14 additions & 5 deletions modules/apps/27-interchain-accounts/host/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

func (suite *KeeperTestSuite) TestMigratorMigrateParams() {
testCases := []struct {
msg string
malleate func()
expectedParams icahosttypes.Params
msg string
malleate func()
expectedParams icahosttypes.Params
useDefaultParams bool
}{
{
"success: default params",
Expand All @@ -21,11 +22,13 @@ func (suite *KeeperTestSuite) TestMigratorMigrateParams() {
subspace.SetParamSet(suite.chainA.GetContext(), &params) // set params
},
icahosttypes.DefaultParams(),
false,
},
{
"success: no params",
"success: no legacy params",
func() {},
icahosttypes.DefaultParams(),
true,
},
}

Expand All @@ -37,7 +40,13 @@ func (suite *KeeperTestSuite) TestMigratorMigrateParams() {

tc.malleate() // explicitly set params

migrator := icahostkeeper.NewMigrator(&suite.chainA.GetSimApp().ICAHostKeeper)
var migrator icahostkeeper.Migrator
k := suite.chainA.GetSimApp().ICAHostKeeper
if tc.useDefaultParams {
migrator = icahostkeeper.NewMigratorWithDefaultParams(&k)
} else {
migrator = icahostkeeper.NewMigrator(&k)
}
err := migrator.MigrateParams(suite.chainA.GetContext())
suite.Require().NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
panic(fmt.Errorf("failed to migrate interchainaccounts app from version 1 to 2 (channel capabilities owned by controller submodule check): %v", err))
}

hostMigrator := hostkeeper.NewMigrator(am.hostKeeper)
hostMigrator := hostkeeper.NewMigratorWithDefaultParams(am.hostKeeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, func(ctx sdk.Context) error {
if err := hostMigrator.MigrateParams(ctx); err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions modules/apps/27-interchain-accounts/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,4 @@ type PortKeeper interface {
// ParamSubspace defines the expected Subspace interface for module parameters.
type ParamSubspace interface {
GetParamSet(ctx sdk.Context, ps paramtypes.ParamSet)
Has(ctx sdk.Context, key []byte) bool
Get(ctx sdk.Context, key []byte, ptr interface{})
Set(ctx sdk.Context, key []byte, value interface{})
}

0 comments on commit 1670d17

Please sign in to comment.