Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Oct 13, 2023
1 parent 43b2ca5 commit dcbb61e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions x/bank/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ type (
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSet(ctx sdk.Context, ps ParamSet)
Get(ctx sdk.Context, key []byte, ptr interface{})
}
)
10 changes: 4 additions & 6 deletions x/bank/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {

// Migrate3to4 migrates x/bank storage from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
var oldParams types.Params
m.legacySubspace.GetParamSet(ctx, &oldParams)
m.keeper.SetAllSendEnabled(ctx, oldParams.GetSendEnabled())
currParams := types.NewParams(oldParams.DefaultSendEnabled)
m.keeper.SetParams(ctx, currParams)
return v4.MigrateStore(ctx, m.keeper.storeKey, currParams, m.keeper.cdc)
var sendEnabled []*types.SendEnabled
m.legacySubspace.Get(ctx, types.KeySendEnabled, &sendEnabled)
m.keeper.SetAllSendEnabled(ctx, sendEnabled)
return v4.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc)
}
9 changes: 5 additions & 4 deletions x/bank/migrations/v4/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/exported"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand All @@ -15,16 +16,16 @@ var ParamsKey = []byte{0x05}
// version 4. Specifically, it takes the parameters that are currently stored
// and managed by the x/params module and stores them directly into the x/bank
// module state.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, currParams types.Params, cdc codec.BinaryCodec) error {
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
if err := currParams.Validate(); err != nil {
return err
}
var currParams types.Params
legacySubspace.GetParamSet(ctx, &currParams)

bz, err := cdc.Marshal(&currParams)
if err != nil {
return err
}

store.Set(ParamsKey, bz)

return nil
Expand Down
20 changes: 18 additions & 2 deletions x/bank/migrations/v4/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/exported"
v4 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v4"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
)

type mockSubspace struct {
ps types.Params
}

func newMockSubspace(ps types.Params) mockSubspace {
return mockSubspace{ps: ps}
}

func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
*ps.(*types.Params) = ms.ps
}

func (ms mockSubspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {}

func TestMigrate(t *testing.T) {
encCfg := moduletestutil.MakeTestEncodingConfig(bank.AppModuleBasic{})
cdc := encCfg.Codec
Expand All @@ -21,10 +36,11 @@ func TestMigrate(t *testing.T) {
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)

require.NoError(t, v4.MigrateStore(ctx, storeKey, types.DefaultParams(), cdc))
legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v4.MigrateStore(ctx, storeKey, legacySubspace, cdc))

var res types.Params
bz := store.Get(v4.ParamsKey)
require.NoError(t, cdc.Unmarshal(bz, &res))
require.Equal(t, types.DefaultParams(), res)
require.Equal(t, legacySubspace.ps, res)
}

0 comments on commit dcbb61e

Please sign in to comment.