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
This commit is contained in:
Amaury 2021-03-29 13:56:45 +02:00 committed by GitHub
parent 823620c231
commit cc946d2f1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 14 deletions

View File

@ -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{},
}
}

View File

@ -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{},
)
}

View File

@ -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)

View File

@ -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.

View File

@ -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.

View File

@ -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) {