Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: miss keypair of SendEnabled to restore legacy param set before migration #194

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (x/gov) [#17780](https://github.com/cosmos/cosmos-sdk/pull/17780) Recover panics and turn them into errors when executing x/gov proposals.
* (x/bank) [#](https://github.com/crypto-org-chain/cosmos-sdk/pull/) Add miss keypair of SendEnabled to restore legacy param set before migration.
mmsqe marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes

Expand Down
8 changes: 7 additions & 1 deletion x/bank/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
v2 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v2"
v3 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v3"
v4 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v4"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -31,5 +32,10 @@ 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 {
return v4.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc)
var oldParams types.Params
m.legacySubspace.GetParamSet(ctx, &oldParams)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
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)
}
7 changes: 1 addition & 6 deletions x/bank/migrations/v4/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -16,11 +15,8 @@ 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, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, currParams types.Params, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
var currParams types.Params
legacySubspace.GetParamSet(ctx, &currParams)

if err := currParams.Validate(); err != nil {
yihuang marked this conversation as resolved.
Show resolved Hide resolved
return err
}
Expand All @@ -29,7 +25,6 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace
if err != nil {
return err
}

store.Set(ParamsKey, bz)

return nil
Expand Down
38 changes: 37 additions & 1 deletion x/bank/types/params_legacy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package types

import paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
import (
fmt "fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

var (
// KeySendEnabled is store's key for SendEnabled Params
Expand All @@ -18,6 +23,37 @@ func ParamKeyTable() paramtypes.KeyTable {
// Deprecated: ParamSetPairs implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeySendEnabled, &p.SendEnabled, validateSendEnabledParams),
yihuang marked this conversation as resolved.
Show resolved Hide resolved
paramtypes.NewParamSetPair(KeyDefaultSendEnabled, &p.DefaultSendEnabled, validateIsBool),
}
}

// SendEnabledParams is a collection of parameters indicating if a coin denom is enabled for sending
type SendEnabledParams []*SendEnabled

func validateSendEnabledParams(i interface{}) error {
params, ok := i.([]*SendEnabled)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// ensure each denom is only registered one time.
registered := make(map[string]bool)
for _, p := range params {
if _, exists := registered[p.Denom]; exists {
return fmt.Errorf("duplicate send enabled parameter found: '%s'", p.Denom)
}
if err := validateSendEnabled(*p); err != nil {
return err
}
registered[p.Denom] = true
}
return nil
}

func validateSendEnabled(i interface{}) error {
param, ok := i.(SendEnabled)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return sdk.ValidateDenom(param.Denom)
}
Loading