From 02453f64d535ecef0eabfcfe856e476ad2bec6b9 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 13 Oct 2023 11:08:41 +0800 Subject: [PATCH] fix(x/bank): miss keypair of SendEnabled to restore legacy param set before migration this change was introduced in https://github.com/cosmos/cosmos-sdk/pull/11977/files, but accidently get removed in https://github.com/cosmos/cosmos-sdk/pull/12630/files, since bank send_enabled refactor before params migration --- CHANGELOG.md | 1 + x/bank/exported/exported.go | 1 + x/bank/keeper/migrations.go | 4 ++++ x/bank/migrations/v4/store_test.go | 2 ++ x/bank/types/params_legacy.go | 38 +++++++++++++++++++++++++++++- 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eca4b873b9cf..3bd9c4f94e6f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (config) [#17649](https://github.com/cosmos/cosmos-sdk/pull/17649) Fix `mempool.max-txs` configuration is invalid in `app.config`. * (mempool) [#17668](https://github.com/cosmos/cosmos-sdk/pull/17668) Fix `PriorityNonceIterator.Next()` nil pointer ref for min priority at the end of iteration. * (x/auth) [#17902](https://github.com/cosmos/cosmos-sdk/pull/17902) Remove tip posthandler. +* (x/bank) [#18107](https://github.com/cosmos/cosmos-sdk/pull/18107) Add missing keypair of SendEnabled to restore legacy param set before migration. ### Client Breaking Changes diff --git a/x/bank/exported/exported.go b/x/bank/exported/exported.go index 7ab9136e896d6..9a518025968c7 100644 --- a/x/bank/exported/exported.go +++ b/x/bank/exported/exported.go @@ -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{}) } ) diff --git a/x/bank/keeper/migrations.go b/x/bank/keeper/migrations.go index 2e2d0ee7ad7ad..32880532c7f42 100644 --- a/x/bank/keeper/migrations.go +++ b/x/bank/keeper/migrations.go @@ -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. @@ -31,5 +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 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) } diff --git a/x/bank/migrations/v4/store_test.go b/x/bank/migrations/v4/store_test.go index b9d2c035c25b3..537d2a39c8547 100644 --- a/x/bank/migrations/v4/store_test.go +++ b/x/bank/migrations/v4/store_test.go @@ -25,6 +25,8 @@ 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 diff --git a/x/bank/types/params_legacy.go b/x/bank/types/params_legacy.go index 668358b4df7a3..d88d152189384 100644 --- a/x/bank/types/params_legacy.go +++ b/x/bank/types/params_legacy.go @@ -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 @@ -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), 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) +}