From cc946d2f1f514ca1977c1f097cc4ea53f346dd93 Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:56:45 +0200 Subject: [PATCH] Fix migrations bug discovered with manual tests (#8998) * Fix migrations bugs discovered with manual tests * Update slashing version * Move supply interface to legacy * Fix lint * Fix test --- x/bank/legacy/v040/migrate.go | 16 ++++++++-------- x/bank/legacy/v040/types.go | 31 +++++++++++++++++++++++++++++++ x/bank/legacy/v043/store.go | 12 ++++++++++-- x/bank/legacy/v043/store_test.go | 9 ++++++--- x/bank/module.go | 4 ++++ x/slashing/module.go | 2 +- 6 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 x/bank/legacy/v040/types.go diff --git a/x/bank/legacy/v040/migrate.go b/x/bank/legacy/v040/migrate.go index ab2e596f57..1c8d270e40 100644 --- a/x/bank/legacy/v040/migrate.go +++ b/x/bank/legacy/v040/migrate.go @@ -4,7 +4,7 @@ import ( v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v039" v036supply "github.com/cosmos/cosmos-sdk/x/bank/legacy/v036" v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v038" - v040bank "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/cosmos-sdk/x/bank/types" ) // Migrate accepts exported v0.39 x/auth and v0.38 x/bank genesis state and @@ -17,22 +17,22 @@ func Migrate( bankGenState v038bank.GenesisState, authGenState v039auth.GenesisState, supplyGenState v036supply.GenesisState, -) *v040bank.GenesisState { - balances := make([]v040bank.Balance, len(authGenState.Accounts)) +) *types.GenesisState { + balances := make([]types.Balance, len(authGenState.Accounts)) for i, acc := range authGenState.Accounts { - balances[i] = v040bank.Balance{ + balances[i] = types.Balance{ Address: acc.GetAddress().String(), Coins: acc.GetCoins(), } } - return &v040bank.GenesisState{ - Params: v040bank.Params{ - SendEnabled: []*v040bank.SendEnabled{}, + return &types.GenesisState{ + Params: types.Params{ + SendEnabled: []*types.SendEnabled{}, DefaultSendEnabled: bankGenState.SendEnabled, }, Balances: balances, Supply: supplyGenState.Supply, - DenomMetadata: []v040bank.Metadata{}, + DenomMetadata: []types.Metadata{}, } } diff --git a/x/bank/legacy/v040/types.go b/x/bank/legacy/v040/types.go new file mode 100644 index 0000000000..11fdf1dfe8 --- /dev/null +++ b/x/bank/legacy/v040/types.go @@ -0,0 +1,31 @@ +package v040 + +import ( + "github.com/golang/protobuf/proto" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// SupplyI defines an inflationary supply interface for modules that handle +// token supply. +// It is copy-pasted from: +// https://github.com/cosmos/cosmos-sdk/blob/v042.3/x/bank/exported/exported.go +// where we stripped off the unnecessary methods. +// +// It is used in the migration script, because we save this interface as an Any +// in the supply state. +// +// Deprecated. +type SupplyI interface { + proto.Message +} + +// RegisterInterfaces registers interfaces required for the v0.40 migrations. +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterInterface( + "cosmos.bank.v1beta1.SupplyI", + (*SupplyI)(nil), + &types.Supply{}, + ) +} diff --git a/x/bank/legacy/v043/store.go b/x/bank/legacy/v043/store.go index 8f7dbc7b35..6dbc3ceab9 100644 --- a/x/bank/legacy/v043/store.go +++ b/x/bank/legacy/v043/store.go @@ -14,8 +14,8 @@ import ( // ref: https://github.com/cosmos/cosmos-sdk/issues/7092 func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error { // Old supply was stored as a single blob under the SupplyKey. - var oldSupply types.Supply - err := cdc.UnmarshalBinaryBare(store.Get(v040bank.SupplyKey), &oldSupply) + var oldSupplyI v040bank.SupplyI + err := cdc.UnmarshalInterface(store.Get(v040bank.SupplyKey), &oldSupplyI) if err != nil { return err } @@ -23,8 +23,16 @@ func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error { // We delete the single key holding the whole blob. store.Delete(v040bank.SupplyKey) + if oldSupplyI == nil { + return nil + } + // We add a new key for each denom supplyStore := prefix.NewStore(store, types.SupplyKey) + + // We're sure that SupplyI is a Supply struct, there's no other + // implementation. + oldSupply := oldSupplyI.(*types.Supply) for i := range oldSupply.Total { coin := oldSupply.Total[i] coinBz, err := cdc.MarshalBinaryBare(&coin) diff --git a/x/bank/legacy/v043/store_test.go b/x/bank/legacy/v043/store_test.go index ff4b3c3c08..145db59f16 100644 --- a/x/bank/legacy/v043/store_test.go +++ b/x/bank/legacy/v043/store_test.go @@ -25,11 +25,14 @@ func TestSupplyMigration(t *testing.T) { oldBarCoin := sdk.NewCoin("bar", sdk.NewInt(200)) // Old supply was stored as a single blob under the `SupplyKey`. - oldSupply := types.Supply{Total: sdk.NewCoins(oldFooCoin, oldBarCoin)} - store.Set(v040bank.SupplyKey, encCfg.Marshaler.MustMarshalBinaryBare(&oldSupply)) + var oldSupply v040bank.SupplyI + oldSupply = &types.Supply{Total: sdk.NewCoins(oldFooCoin, oldBarCoin)} + oldSupplyBz, err := encCfg.Marshaler.MarshalInterface(oldSupply) + require.NoError(t, err) + store.Set(v040bank.SupplyKey, oldSupplyBz) // Run migration. - err := v043bank.MigrateStore(ctx, bankKey, encCfg.Marshaler) + err = v043bank.MigrateStore(ctx, bankKey, encCfg.Marshaler) require.NoError(t, err) // New supply is indexed by denom. diff --git a/x/bank/module.go b/x/bank/module.go index daf393d299..26effe98a0 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -22,6 +22,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/client/cli" "github.com/cosmos/cosmos-sdk/x/bank/client/rest" "github.com/cosmos/cosmos-sdk/x/bank/keeper" + v040 "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040" "github.com/cosmos/cosmos-sdk/x/bank/simulation" "github.com/cosmos/cosmos-sdk/x/bank/types" ) @@ -84,6 +85,9 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { // RegisterInterfaces registers interfaces and implementations of the bank module. func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { types.RegisterInterfaces(registry) + + // Register legacy interfaces for migration scripts. + v040.RegisterInterfaces(registry) } // AppModule implements an application module for the bank module. diff --git a/x/slashing/module.go b/x/slashing/module.go index 69e96b49d6..22a627f433 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -161,7 +161,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json } // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 1 } +func (AppModule) ConsensusVersion() uint64 { return 2 } // BeginBlock returns the begin blocker for the slashing module. func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {