From 7efeb826e09d654ae716aabcb248241a8c5f27e6 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 7 Nov 2022 11:32:56 +0100 Subject: [PATCH] refactor: rename migrations from `v42`, `v43`, `v{SDK}` to `v1`, `v2`, `v{Consensus}` (#13772) * chore: rename migrations from v42, v43 to v1, v2 * group migration should be in #13742 * updates --- x/auth/keeper/migrations.go | 8 ++-- x/auth/migrations/{v042 => v1}/types.go | 2 +- x/auth/migrations/{v043 => v2}/store.go | 4 +- x/auth/migrations/{v043 => v2}/store_test.go | 5 +- x/auth/migrations/{v046 => v3}/store.go | 2 +- x/auth/migrations/{v046 => v3}/store_test.go | 5 +- x/auth/migrations/v4/migrate.go | 4 -- x/auth/migrations/v4/migrator_test.go | 3 +- x/auth/module.go | 4 +- x/authz/keeper/migrations.go | 4 +- x/authz/migrations/{v046 => v2}/keys.go | 2 +- x/authz/migrations/{v046 => v2}/keys_test.go | 2 +- x/authz/migrations/{v046 => v2}/store.go | 2 +- x/authz/migrations/{v046 => v2}/store_test.go | 14 +++--- x/authz/module/module.go | 3 +- x/bank/migrations/v1/types.go | 8 ++-- x/bank/migrations/v2/store.go | 4 +- x/distribution/keeper/migrations.go | 4 +- x/distribution/keeper/params_test.go | 3 +- x/distribution/migrations/v043/store.go | 24 ---------- .../migrations/{v042 => v1}/types.go | 36 +++++++------- .../migrations/{v043 => v2}/helpers.go | 12 ++--- x/distribution/migrations/v2/store.go | 24 ++++++++++ .../migrations/{v043 => v2}/store_test.go | 26 +++++----- x/feegrant/keeper/migrations.go | 4 +- x/feegrant/migrations/{v046 => v2}/keys.go | 2 +- x/feegrant/migrations/{v046 => v2}/store.go | 2 +- .../migrations/{v046 => v2}/store_test.go | 18 +++---- x/feegrant/module/module.go | 3 +- x/gov/migrations/v1/types.go | 4 +- x/gov/module.go | 6 +-- x/slashing/keeper/migrations.go | 4 +- x/slashing/migrations/v043/store.go | 21 -------- x/slashing/migrations/{v042 => v1}/types.go | 4 +- x/slashing/migrations/v2/store.go | 21 ++++++++ .../migrations/{v043 => v2}/store_test.go | 14 +++--- x/staking/migrations/v1/types.go | 48 +++++++++---------- x/staking/migrations/v2/store.go | 22 ++++----- x/upgrade/module.go | 3 +- 39 files changed, 191 insertions(+), 190 deletions(-) rename x/auth/migrations/{v042 => v1}/types.go (79%) rename x/auth/migrations/{v043 => v2}/store.go (99%) rename x/auth/migrations/{v043 => v2}/store_test.go (99%) rename x/auth/migrations/{v046 => v3}/store.go (98%) rename x/auth/migrations/{v046 => v3}/store_test.go (96%) rename x/authz/migrations/{v046 => v2}/keys.go (99%) rename x/authz/migrations/{v046 => v2}/keys_test.go (98%) rename x/authz/migrations/{v046 => v2}/store.go (99%) rename x/authz/migrations/{v046 => v2}/store_test.go (85%) delete mode 100644 x/distribution/migrations/v043/store.go rename x/distribution/migrations/{v042 => v1}/types.go (88%) rename x/distribution/migrations/{v043 => v2}/helpers.go (89%) create mode 100644 x/distribution/migrations/v2/store.go rename x/distribution/migrations/{v043 => v2}/store_test.go (72%) rename x/feegrant/migrations/{v046 => v2}/keys.go (99%) rename x/feegrant/migrations/{v046 => v2}/store.go (98%) rename x/feegrant/migrations/{v046 => v2}/store_test.go (78%) delete mode 100644 x/slashing/migrations/v043/store.go rename x/slashing/migrations/{v042 => v1}/types.go (95%) create mode 100644 x/slashing/migrations/v2/store.go rename x/slashing/migrations/{v043 => v2}/store_test.go (78%) diff --git a/x/auth/keeper/migrations.go b/x/auth/keeper/migrations.go index 33eaf4c516..16c7ffabb2 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -5,8 +5,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/exported" - v043 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v043" - v046 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v046" + v2 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v2" + v3 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v3" v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -28,7 +28,7 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error { var iterErr error m.keeper.IterateAccounts(ctx, func(account types.AccountI) (stop bool) { - wb, err := v043.MigrateAccount(ctx, account, m.queryServer) + wb, err := v2.MigrateAccount(ctx, account, m.queryServer) if err != nil { iterErr = err return true @@ -48,7 +48,7 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error { // Migrate2to3 migrates from consensus version 2 to version 3. Specifically, for each account // we index the account's ID to their address. func (m Migrator) Migrate2to3(ctx sdk.Context) error { - return v046.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) + return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) } // Migrate3to4 migrates the x/auth module state from the consensus version 3 to diff --git a/x/auth/migrations/v042/types.go b/x/auth/migrations/v1/types.go similarity index 79% rename from x/auth/migrations/v042/types.go rename to x/auth/migrations/v1/types.go index ecc3dc7b3c..6aa7e99667 100644 --- a/x/auth/migrations/v042/types.go +++ b/x/auth/migrations/v1/types.go @@ -1,4 +1,4 @@ -package v042 +package v1 const ( ModuleName = "auth" diff --git a/x/auth/migrations/v043/store.go b/x/auth/migrations/v2/store.go similarity index 99% rename from x/auth/migrations/v043/store.go rename to x/auth/migrations/v2/store.go index e448c63162..d1b33350bc 100644 --- a/x/auth/migrations/v043/store.go +++ b/x/auth/migrations/v2/store.go @@ -1,4 +1,4 @@ -// Package v043 creates in-place store migrations for fixing tracking +// Package v2 creates in-place store migrations for fixing tracking // delegations with vesting accounts. // ref: https://github.com/cosmos/cosmos-sdk/issues/8601 // ref: https://github.com/cosmos/cosmos-sdk/issues/8812 @@ -15,7 +15,7 @@ // https://github.com/cosmos/cosmos-sdk/issues/9070 // The preferred solution is to use inter-module communication (ADR-033), and // this file will be refactored to use ADR-033 once it's ready. -package v043 +package v2 import ( "errors" diff --git a/x/auth/migrations/v043/store_test.go b/x/auth/migrations/v2/store_test.go similarity index 99% rename from x/auth/migrations/v043/store_test.go rename to x/auth/migrations/v2/store_test.go index 546ee34a6f..f9cd5f200b 100644 --- a/x/auth/migrations/v043/store_test.go +++ b/x/auth/migrations/v2/store_test.go @@ -1,4 +1,4 @@ -package v043_test +package v2_test import ( "fmt" @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/keeper" + v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4" authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -42,7 +43,7 @@ func TestMigrateVestingAccounts(t *testing.T) { encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}) cdc := encCfg.Codec - storeKey := sdk.NewKVStoreKey(v4.ModuleName) + storeKey := sdk.NewKVStoreKey(v1.ModuleName) tKey := sdk.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) diff --git a/x/auth/migrations/v046/store.go b/x/auth/migrations/v3/store.go similarity index 98% rename from x/auth/migrations/v046/store.go rename to x/auth/migrations/v3/store.go index 8cb7c15bd6..8e93a87e0b 100644 --- a/x/auth/migrations/v046/store.go +++ b/x/auth/migrations/v3/store.go @@ -1,4 +1,4 @@ -package v046 +package v3 import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/auth/migrations/v046/store_test.go b/x/auth/migrations/v3/store_test.go similarity index 96% rename from x/auth/migrations/v046/store_test.go rename to x/auth/migrations/v3/store_test.go index c5f59c2eb2..d47d7ce2f7 100644 --- a/x/auth/migrations/v046/store_test.go +++ b/x/auth/migrations/v3/store_test.go @@ -1,4 +1,4 @@ -package v046_test +package v3_test import ( "math/rand" @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/keeper" + v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4" authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -38,7 +39,7 @@ func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) { encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}) cdc := encCfg.Codec - storeKey := sdk.NewKVStoreKey(v4.ModuleName) + storeKey := sdk.NewKVStoreKey(v1.ModuleName) tKey := sdk.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) diff --git a/x/auth/migrations/v4/migrate.go b/x/auth/migrations/v4/migrate.go index 3b0d0ce8cf..93b8658408 100644 --- a/x/auth/migrations/v4/migrate.go +++ b/x/auth/migrations/v4/migrate.go @@ -7,10 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -const ( - ModuleName = "auth" -) - var ParamsKey = []byte{0x01} // Migrate migrates the x/auth module state from the consensus version 3 to diff --git a/x/auth/migrations/v4/migrator_test.go b/x/auth/migrations/v4/migrator_test.go index 57a9245a81..e3dd3b1d55 100644 --- a/x/auth/migrations/v4/migrator_test.go +++ b/x/auth/migrations/v4/migrator_test.go @@ -10,6 +10,7 @@ import ( moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/exported" + v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -30,7 +31,7 @@ func TestMigrate(t *testing.T) { encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}) cdc := encCfg.Codec - storeKey := sdk.NewKVStoreKey(v4.ModuleName) + storeKey := sdk.NewKVStoreKey(v1.ModuleName) tKey := sdk.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) diff --git a/x/auth/module.go b/x/auth/module.go index 52064974f9..8cab7194d4 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -142,11 +142,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer(), am.legacySubspace) if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) } if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) } if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { diff --git a/x/authz/keeper/migrations.go b/x/authz/keeper/migrations.go index 17b11e1039..a4411d0919 100644 --- a/x/authz/keeper/migrations.go +++ b/x/authz/keeper/migrations.go @@ -2,7 +2,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - v046 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v046" + v2 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v2" ) // Migrator is a struct for handling in-place store migrations. @@ -17,5 +17,5 @@ func NewMigrator(keeper Keeper) Migrator { // Migrate1to2 migrates from version 1 to 2. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v046.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) + return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) } diff --git a/x/authz/migrations/v046/keys.go b/x/authz/migrations/v2/keys.go similarity index 99% rename from x/authz/migrations/v046/keys.go rename to x/authz/migrations/v2/keys.go index 8754185a7b..fceced95f1 100644 --- a/x/authz/migrations/v046/keys.go +++ b/x/authz/migrations/v2/keys.go @@ -1,4 +1,4 @@ -package v046 +package v2 import ( "time" diff --git a/x/authz/migrations/v046/keys_test.go b/x/authz/migrations/v2/keys_test.go similarity index 98% rename from x/authz/migrations/v046/keys_test.go rename to x/authz/migrations/v2/keys_test.go index 9a73edbc32..7553b01427 100644 --- a/x/authz/migrations/v046/keys_test.go +++ b/x/authz/migrations/v2/keys_test.go @@ -1,4 +1,4 @@ -package v046 +package v2 import ( "testing" diff --git a/x/authz/migrations/v046/store.go b/x/authz/migrations/v2/store.go similarity index 99% rename from x/authz/migrations/v046/store.go rename to x/authz/migrations/v2/store.go index 16837e1b3c..198556ec32 100644 --- a/x/authz/migrations/v046/store.go +++ b/x/authz/migrations/v2/store.go @@ -1,4 +1,4 @@ -package v046 +package v2 import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/authz/migrations/v046/store_test.go b/x/authz/migrations/v2/store_test.go similarity index 85% rename from x/authz/migrations/v046/store_test.go rename to x/authz/migrations/v2/store_test.go index 5d3241f452..52fbf4f08e 100644 --- a/x/authz/migrations/v046/store_test.go +++ b/x/authz/migrations/v2/store_test.go @@ -1,4 +1,4 @@ -package v046_test +package v2_test import ( "testing" @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/authz" - v046 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v046" + v2 "github.com/cosmos/cosmos-sdk/x/authz/migrations/v2" authztestutil "github.com/cosmos/cosmos-sdk/x/authz/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" @@ -101,13 +101,13 @@ func TestMigration(t *testing.T) { for _, g := range grants { grant := g.authorization() - store.Set(v046.GrantStoreKey(g.grantee, g.granter, g.msgType), cdc.MustMarshal(&grant)) + store.Set(v2.GrantStoreKey(g.grantee, g.granter, g.msgType), cdc.MustMarshal(&grant)) } ctx = ctx.WithBlockTime(ctx.BlockTime().Add(1 * time.Hour)) - require.NoError(t, v046.MigrateStore(ctx, authzKey, cdc)) + require.NoError(t, v2.MigrateStore(ctx, authzKey, cdc)) - require.NotNil(t, store.Get(v046.GrantStoreKey(grantee1, granter2, genericMsgType))) - require.NotNil(t, store.Get(v046.GrantStoreKey(grantee1, granter1, sendMsgType))) - require.Nil(t, store.Get(v046.GrantStoreKey(grantee2, granter2, genericMsgType))) + require.NotNil(t, store.Get(v2.GrantStoreKey(grantee1, granter2, genericMsgType))) + require.NotNil(t, store.Get(v2.GrantStoreKey(grantee1, granter1, sendMsgType))) + require.Nil(t, store.Get(v2.GrantStoreKey(grantee2, granter2, genericMsgType))) } diff --git a/x/authz/module/module.go b/x/authz/module/module.go index f65a747d82..6e30bcbca6 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -3,6 +3,7 @@ package authz import ( "context" "encoding/json" + "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -52,7 +53,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { m := keeper.NewMigrator(am.keeper) err := cfg.RegisterMigration(authz.ModuleName, 1, m.Migrate1to2) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", authz.ModuleName, err)) } } diff --git a/x/bank/migrations/v1/types.go b/x/bank/migrations/v1/types.go index 1860775896..a584be926d 100644 --- a/x/bank/migrations/v1/types.go +++ b/x/bank/migrations/v1/types.go @@ -4,7 +4,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/golang/protobuf/proto" ) @@ -40,9 +40,9 @@ func DenomMetadataKey(denom string) []byte { // store. The key must not contain the perfix BalancesPrefix as the prefix store // iterator discards the actual prefix. func AddressFromBalancesStore(key []byte) sdk.AccAddress { - kv.AssertKeyAtLeastLength(key, 1+v042auth.AddrLen) - addr := key[:v042auth.AddrLen] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 1+v1auth.AddrLen) + addr := key[:v1auth.AddrLen] + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.AccAddress(addr) } diff --git a/x/bank/migrations/v2/store.go b/x/bank/migrations/v2/store.go index 7b9331bdfb..50042a61c0 100644 --- a/x/bank/migrations/v2/store.go +++ b/x/bank/migrations/v2/store.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/prefix" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" v1 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v1" "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/tendermint/tendermint/libs/log" @@ -63,7 +63,7 @@ func migrateBalanceKeys(store sdk.KVStore, logger log.Logger) { for ; oldStoreIter.Valid(); oldStoreIter.Next() { addr := v1.AddressFromBalancesStore(oldStoreIter.Key()) - denom := oldStoreIter.Key()[v042auth.AddrLen:] + denom := oldStoreIter.Key()[v1auth.AddrLen:] newStoreKey := types.CreatePrefixedAccountStoreKey(addr, denom) // Set new key on store. Values don't change. diff --git a/x/distribution/keeper/migrations.go b/x/distribution/keeper/migrations.go index 1ea8c0f160..c60846669f 100644 --- a/x/distribution/keeper/migrations.go +++ b/x/distribution/keeper/migrations.go @@ -3,7 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/distribution/exported" - v043 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v043" + v2 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v2" v3 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v3" ) @@ -20,7 +20,7 @@ func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator { // Migrate1to2 migrates from version 1 to 2. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v043.MigrateStore(ctx, m.keeper.storeKey) + return v2.MigrateStore(ctx, m.keeper.storeKey) } // Migrate2to3 migrates the x/distribution module state from the consensus diff --git a/x/distribution/keeper/params_test.go b/x/distribution/keeper/params_test.go index fc6c7248a5..46c433d41d 100644 --- a/x/distribution/keeper/params_test.go +++ b/x/distribution/keeper/params_test.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/keeper" distrtestutil "github.com/cosmos/cosmos-sdk/x/distribution/testutil" "github.com/cosmos/cosmos-sdk/x/distribution/types" - disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -19,7 +18,7 @@ import ( func TestParams(t *testing.T) { ctrl := gomock.NewController(t) - key := sdk.NewKVStoreKey(disttypes.StoreKey) + key := sdk.NewKVStoreKey(types.StoreKey) testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{}) ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Height: 1}) diff --git a/x/distribution/migrations/v043/store.go b/x/distribution/migrations/v043/store.go deleted file mode 100644 index 12fa534f9b..0000000000 --- a/x/distribution/migrations/v043/store.go +++ /dev/null @@ -1,24 +0,0 @@ -package v043 - -import ( - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" - v042distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v042" -) - -// MigrateStore performs in-place store migrations from v0.40 to v0.43. The -// migration includes: -// -// - Change addresses to be length-prefixed. -func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { - store := ctx.KVStore(storeKey) - MigratePrefixAddress(store, v042distribution.ValidatorOutstandingRewardsPrefix) - MigratePrefixAddress(store, v042distribution.DelegatorWithdrawAddrPrefix) - MigratePrefixAddressAddress(store, v042distribution.DelegatorStartingInfoPrefix) - MigratePrefixAddressBytes(store, v042distribution.ValidatorHistoricalRewardsPrefix) - MigratePrefixAddress(store, v042distribution.ValidatorCurrentRewardsPrefix) - MigratePrefixAddress(store, v042distribution.ValidatorAccumulatedCommissionPrefix) - MigratePrefixAddressBytes(store, v042distribution.ValidatorSlashEventPrefix) - - return nil -} diff --git a/x/distribution/migrations/v042/types.go b/x/distribution/migrations/v1/types.go similarity index 88% rename from x/distribution/migrations/v042/types.go rename to x/distribution/migrations/v1/types.go index dc5cc6c746..987a1fc29e 100644 --- a/x/distribution/migrations/v042/types.go +++ b/x/distribution/migrations/v1/types.go @@ -5,7 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" ) const ( @@ -59,7 +59,7 @@ var ( func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.ValAddress(addr) } @@ -67,29 +67,29 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.AccAddress(addr) } // gets the addresses from a delegator starting info key func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { - kv.AssertKeyAtLeastLength(key, 2+v042auth.AddrLen) - addr := key[1 : 1+v042auth.AddrLen] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen) + addr := key[1 : 1+v1auth.AddrLen] + kv.AssertKeyLength(addr, v1auth.AddrLen) valAddr = sdk.ValAddress(addr) - addr = key[1+v042auth.AddrLen:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + addr = key[1+v1auth.AddrLen:] + kv.AssertKeyLength(addr, v1auth.AddrLen) delAddr = sdk.AccAddress(addr) return } // gets the address & period from a validator's historical rewards key func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { - kv.AssertKeyAtLeastLength(key, 2+v042auth.AddrLen) - addr := key[1 : 1+v042auth.AddrLen] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen) + addr := key[1 : 1+v1auth.AddrLen] + kv.AssertKeyLength(addr, v1auth.AddrLen) valAddr = sdk.ValAddress(addr) - b := key[1+v042auth.AddrLen:] + b := key[1+v1auth.AddrLen:] kv.AssertKeyLength(addr, 8) period = binary.LittleEndian.Uint64(b) return @@ -99,7 +99,7 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.ValAddress(addr) } @@ -107,17 +107,17 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.ValAddress(addr) } // gets the height from a validator's slash event key func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { - kv.AssertKeyAtLeastLength(key, 2+v042auth.AddrLen) - addr := key[1 : 1+v042auth.AddrLen] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen) + addr := key[1 : 1+v1auth.AddrLen] + kv.AssertKeyLength(addr, v1auth.AddrLen) valAddr = sdk.ValAddress(addr) - startB := 1 + v042auth.AddrLen + startB := 1 + v1auth.AddrLen kv.AssertKeyAtLeastLength(key, startB+9) b := key[startB : startB+8] // the next 8 bytes represent the height height = binary.BigEndian.Uint64(b) diff --git a/x/distribution/migrations/v043/helpers.go b/x/distribution/migrations/v2/helpers.go similarity index 89% rename from x/distribution/migrations/v043/helpers.go rename to x/distribution/migrations/v2/helpers.go index 0e846a72ff..fb9ccfbe73 100644 --- a/x/distribution/migrations/v043/helpers.go +++ b/x/distribution/migrations/v2/helpers.go @@ -1,10 +1,10 @@ -package v043 +package v2 import ( "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" ) // MigratePrefixAddress is a helper function that migrates all keys of format: @@ -39,8 +39,8 @@ func MigratePrefixAddressBytes(store sdk.KVStore, prefixBz []byte) { defer oldStoreIter.Close() for ; oldStoreIter.Valid(); oldStoreIter.Next() { - addr := oldStoreIter.Key()[:v042auth.AddrLen] - endBz := oldStoreIter.Key()[v042auth.AddrLen:] + addr := oldStoreIter.Key()[:v1auth.AddrLen] + endBz := oldStoreIter.Key()[v1auth.AddrLen:] newStoreKey := append(append(prefixBz, address.MustLengthPrefix(addr)...), endBz...) // Set new key on store. Values don't change. @@ -60,8 +60,8 @@ func MigratePrefixAddressAddress(store sdk.KVStore, prefixBz []byte) { defer oldStoreIter.Close() for ; oldStoreIter.Valid(); oldStoreIter.Next() { - addr1 := oldStoreIter.Key()[:v042auth.AddrLen] - addr2 := oldStoreIter.Key()[v042auth.AddrLen:] + addr1 := oldStoreIter.Key()[:v1auth.AddrLen] + addr2 := oldStoreIter.Key()[v1auth.AddrLen:] newStoreKey := append(append(prefixBz, address.MustLengthPrefix(addr1)...), address.MustLengthPrefix(addr2)...) // Set new key on store. Values don't change. diff --git a/x/distribution/migrations/v2/store.go b/x/distribution/migrations/v2/store.go new file mode 100644 index 0000000000..dfecbc4098 --- /dev/null +++ b/x/distribution/migrations/v2/store.go @@ -0,0 +1,24 @@ +package v2 + +import ( + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + v1 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v1" +) + +// MigrateStore performs in-place store migrations from v0.40 to v0.43. The +// migration includes: +// +// - Change addresses to be length-prefixed. +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { + store := ctx.KVStore(storeKey) + MigratePrefixAddress(store, v1.ValidatorOutstandingRewardsPrefix) + MigratePrefixAddress(store, v1.DelegatorWithdrawAddrPrefix) + MigratePrefixAddressAddress(store, v1.DelegatorStartingInfoPrefix) + MigratePrefixAddressBytes(store, v1.ValidatorHistoricalRewardsPrefix) + MigratePrefixAddress(store, v1.ValidatorCurrentRewardsPrefix) + MigratePrefixAddress(store, v1.ValidatorAccumulatedCommissionPrefix) + MigratePrefixAddressBytes(store, v1.ValidatorSlashEventPrefix) + + return nil +} diff --git a/x/distribution/migrations/v043/store_test.go b/x/distribution/migrations/v2/store_test.go similarity index 72% rename from x/distribution/migrations/v043/store_test.go rename to x/distribution/migrations/v2/store_test.go index 9c055f72e5..b67d194842 100644 --- a/x/distribution/migrations/v043/store_test.go +++ b/x/distribution/migrations/v2/store_test.go @@ -1,4 +1,4 @@ -package v043_test +package v2_test import ( "bytes" @@ -9,8 +9,8 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - v042distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v042" - v043distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v043" + v1 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v1" + v2 "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v2" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -32,47 +32,47 @@ func TestStoreMigration(t *testing.T) { }{ { "FeePoolKey", - v042distribution.FeePoolKey, + v1.FeePoolKey, types.FeePoolKey, }, { "ProposerKey", - v042distribution.ProposerKey, + v1.ProposerKey, types.ProposerKey, }, { "ValidatorOutstandingRewards", - v042distribution.GetValidatorOutstandingRewardsKey(valAddr), + v1.GetValidatorOutstandingRewardsKey(valAddr), types.GetValidatorOutstandingRewardsKey(valAddr), }, { "DelegatorWithdrawAddr", - v042distribution.GetDelegatorWithdrawAddrKey(addr2), + v1.GetDelegatorWithdrawAddrKey(addr2), types.GetDelegatorWithdrawAddrKey(addr2), }, { "DelegatorStartingInfo", - v042distribution.GetDelegatorStartingInfoKey(valAddr, addr2), + v1.GetDelegatorStartingInfoKey(valAddr, addr2), types.GetDelegatorStartingInfoKey(valAddr, addr2), }, { "ValidatorHistoricalRewards", - v042distribution.GetValidatorHistoricalRewardsKey(valAddr, 6), + v1.GetValidatorHistoricalRewardsKey(valAddr, 6), types.GetValidatorHistoricalRewardsKey(valAddr, 6), }, { "ValidatorCurrentRewards", - v042distribution.GetValidatorCurrentRewardsKey(valAddr), + v1.GetValidatorCurrentRewardsKey(valAddr), types.GetValidatorCurrentRewardsKey(valAddr), }, { "ValidatorAccumulatedCommission", - v042distribution.GetValidatorAccumulatedCommissionKey(valAddr), + v1.GetValidatorAccumulatedCommissionKey(valAddr), types.GetValidatorAccumulatedCommissionKey(valAddr), }, { "ValidatorSlashEvent", - v042distribution.GetValidatorSlashEventKey(valAddr, 6, 8), + v1.GetValidatorSlashEventKey(valAddr, 6, 8), types.GetValidatorSlashEventKey(valAddr, 6, 8), }, } @@ -83,7 +83,7 @@ func TestStoreMigration(t *testing.T) { } // Run migrations. - err := v043distribution.MigrateStore(ctx, distributionKey) + err := v2.MigrateStore(ctx, distributionKey) require.NoError(t, err) // Make sure the new keys are set and old keys are deleted. diff --git a/x/feegrant/keeper/migrations.go b/x/feegrant/keeper/migrations.go index 67c3e489cf..7299b67fc4 100644 --- a/x/feegrant/keeper/migrations.go +++ b/x/feegrant/keeper/migrations.go @@ -2,7 +2,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - v046 "github.com/cosmos/cosmos-sdk/x/feegrant/migrations/v046" + v2 "github.com/cosmos/cosmos-sdk/x/feegrant/migrations/v2" ) // Migrator is a struct for handling in-place store migrations. @@ -17,5 +17,5 @@ func NewMigrator(keeper Keeper) Migrator { // Migrate1to2 migrates from version 1 to 2. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v046.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) + return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) } diff --git a/x/feegrant/migrations/v046/keys.go b/x/feegrant/migrations/v2/keys.go similarity index 99% rename from x/feegrant/migrations/v046/keys.go rename to x/feegrant/migrations/v2/keys.go index 226595e126..ae748c6491 100644 --- a/x/feegrant/migrations/v046/keys.go +++ b/x/feegrant/migrations/v2/keys.go @@ -1,4 +1,4 @@ -package v046 +package v2 import ( "time" diff --git a/x/feegrant/migrations/v046/store.go b/x/feegrant/migrations/v2/store.go similarity index 98% rename from x/feegrant/migrations/v046/store.go rename to x/feegrant/migrations/v2/store.go index f3d49312ca..53cde4e6fd 100644 --- a/x/feegrant/migrations/v046/store.go +++ b/x/feegrant/migrations/v2/store.go @@ -1,4 +1,4 @@ -package v046 +package v2 import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/feegrant/migrations/v046/store_test.go b/x/feegrant/migrations/v2/store_test.go similarity index 78% rename from x/feegrant/migrations/v046/store_test.go rename to x/feegrant/migrations/v2/store_test.go index eadf8c33ca..746cf03865 100644 --- a/x/feegrant/migrations/v046/store_test.go +++ b/x/feegrant/migrations/v2/store_test.go @@ -1,4 +1,4 @@ -package v046_test +package v2_test import ( "testing" @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/feegrant" - v046 "github.com/cosmos/cosmos-sdk/x/feegrant/migrations/v046" + v2 "github.com/cosmos/cosmos-sdk/x/feegrant/migrations/v2" feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" "github.com/stretchr/testify/require" ) @@ -19,7 +19,7 @@ func TestMigration(t *testing.T) { var cdc codec.Codec depinject.Inject(feegranttestutil.AppConfig, &cdc) - feegrantKey := sdk.NewKVStoreKey(v046.ModuleName) + feegrantKey := sdk.NewKVStoreKey(v2.ModuleName) ctx := testutil.DefaultContext(feegrantKey, sdk.NewTransientStoreKey("transient_test")) granter1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) grantee1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) @@ -72,15 +72,15 @@ func TestMigration(t *testing.T) { bz, err := cdc.Marshal(&newGrant) require.NoError(t, err) - store.Set(v046.FeeAllowanceKey(grant.granter, grant.grantee), bz) + store.Set(v2.FeeAllowanceKey(grant.granter, grant.grantee), bz) } ctx = ctx.WithBlockTime(now.Add(30 * time.Hour)) - require.NoError(t, v046.MigrateStore(ctx, feegrantKey, cdc)) + require.NoError(t, v2.MigrateStore(ctx, feegrantKey, cdc)) store = ctx.KVStore(feegrantKey) - require.NotNil(t, store.Get(v046.FeeAllowanceKey(granter1, grantee1))) - require.Nil(t, store.Get(v046.FeeAllowanceKey(granter2, grantee2))) - require.NotNil(t, store.Get(v046.FeeAllowanceKey(granter1, grantee2))) - require.Nil(t, store.Get(v046.FeeAllowanceKey(granter2, grantee1))) + require.NotNil(t, store.Get(v2.FeeAllowanceKey(granter1, grantee1))) + require.Nil(t, store.Get(v2.FeeAllowanceKey(granter2, grantee2))) + require.NotNil(t, store.Get(v2.FeeAllowanceKey(granter1, grantee2))) + require.Nil(t, store.Get(v2.FeeAllowanceKey(granter2, grantee1))) } diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index a91d243869..bf60e2e5bf 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -3,6 +3,7 @@ package module import ( "context" "encoding/json" + "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -56,7 +57,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { m := keeper.NewMigrator(am.keeper) err := cfg.RegisterMigration(feegrant.ModuleName, 1, m.Migrate1to2) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/feegrant from version 1 to 2: %v", err)) } } diff --git a/x/gov/migrations/v1/types.go b/x/gov/migrations/v1/types.go index 48c5a75bf0..5f94a341a3 100644 --- a/x/gov/migrations/v1/types.go +++ b/x/gov/migrations/v1/types.go @@ -9,7 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" ) const ( @@ -154,7 +154,7 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { } func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { - kv.AssertKeyLength(key[1:], 8+v042auth.AddrLen) + kv.AssertKeyLength(key[1:], 8+v1auth.AddrLen) kv.AssertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) diff --git a/x/gov/module.go b/x/gov/module.go index c6f54f1631..e8b051c5bf 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -280,15 +280,15 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { m := keeper.NewMigrator(am.keeper, am.legacySubspace) err := cfg.RegisterMigration(govtypes.ModuleName, 1, m.Migrate1to2) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/gov from version 1 to 2: %v", err)) } err = cfg.RegisterMigration(govtypes.ModuleName, 2, m.Migrate2to3) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/gov from version 2 to 3: %v", err)) } err = cfg.RegisterMigration(govtypes.ModuleName, 3, m.Migrate3to4) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/gov from version 3 to 4: %v", err)) } } diff --git a/x/slashing/keeper/migrations.go b/x/slashing/keeper/migrations.go index 07ded76095..fca5255e6f 100644 --- a/x/slashing/keeper/migrations.go +++ b/x/slashing/keeper/migrations.go @@ -3,7 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/exported" - v043 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v043" + v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2" v3 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v3" ) @@ -20,7 +20,7 @@ func NewMigrator(keeper Keeper, ss exported.Subspace) Migrator { // Migrate1to2 migrates from version 1 to 2. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v043.MigrateStore(ctx, m.keeper.storeKey) + return v2.MigrateStore(ctx, m.keeper.storeKey) } // Migrate2to3 migrates the x/slashing module state from the consensus diff --git a/x/slashing/migrations/v043/store.go b/x/slashing/migrations/v043/store.go deleted file mode 100644 index d9d44664c1..0000000000 --- a/x/slashing/migrations/v043/store.go +++ /dev/null @@ -1,21 +0,0 @@ -package v043 - -import ( - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" - v043distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v043" - v042slashing "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v042" -) - -// MigrateStore performs in-place store migrations from v0.40 to v0.43. The -// migration includes: -// -// - Change addresses to be length-prefixed. -func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { - store := ctx.KVStore(storeKey) - v043distribution.MigratePrefixAddress(store, v042slashing.ValidatorSigningInfoKeyPrefix) - v043distribution.MigratePrefixAddressBytes(store, v042slashing.ValidatorMissedBlockBitArrayKeyPrefix) - v043distribution.MigratePrefixAddress(store, v042slashing.AddrPubkeyRelationKeyPrefix) - - return nil -} diff --git a/x/slashing/migrations/v042/types.go b/x/slashing/migrations/v1/types.go similarity index 95% rename from x/slashing/migrations/v042/types.go rename to x/slashing/migrations/v1/types.go index 0089afe732..8267a6d836 100644 --- a/x/slashing/migrations/v042/types.go +++ b/x/slashing/migrations/v1/types.go @@ -7,7 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" ) const ( @@ -47,7 +47,7 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - kv.AssertKeyLength(addr, v042auth.AddrLen) + kv.AssertKeyLength(addr, v1auth.AddrLen) return sdk.ConsAddress(addr) } diff --git a/x/slashing/migrations/v2/store.go b/x/slashing/migrations/v2/store.go new file mode 100644 index 0000000000..2445a62903 --- /dev/null +++ b/x/slashing/migrations/v2/store.go @@ -0,0 +1,21 @@ +package v2 + +import ( + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + v2distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v2" + v1 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v1" +) + +// MigrateStore performs in-place store migrations from v0.40 to v0.43. The +// migration includes: +// +// - Change addresses to be length-prefixed. +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { + store := ctx.KVStore(storeKey) + v2distribution.MigratePrefixAddress(store, v1.ValidatorSigningInfoKeyPrefix) + v2distribution.MigratePrefixAddressBytes(store, v1.ValidatorMissedBlockBitArrayKeyPrefix) + v2distribution.MigratePrefixAddress(store, v1.AddrPubkeyRelationKeyPrefix) + + return nil +} diff --git a/x/slashing/migrations/v043/store_test.go b/x/slashing/migrations/v2/store_test.go similarity index 78% rename from x/slashing/migrations/v043/store_test.go rename to x/slashing/migrations/v2/store_test.go index 17aa3387c2..d6bd71501f 100644 --- a/x/slashing/migrations/v043/store_test.go +++ b/x/slashing/migrations/v2/store_test.go @@ -1,4 +1,4 @@ -package v043_test +package v2_test import ( "bytes" @@ -9,8 +9,8 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v042" - v043slashing "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v043" + v1 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v1" + v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -31,17 +31,17 @@ func TestStoreMigration(t *testing.T) { }{ { "ValidatorSigningInfoKey", - v040slashing.ValidatorSigningInfoKey(consAddr), + v1.ValidatorSigningInfoKey(consAddr), types.ValidatorSigningInfoKey(consAddr), }, { "ValidatorMissedBlockBitArrayKey", - v040slashing.ValidatorMissedBlockBitArrayKey(consAddr, 2), + v1.ValidatorMissedBlockBitArrayKey(consAddr, 2), types.ValidatorMissedBlockBitArrayKey(consAddr, 2), }, { "AddrPubkeyRelationKey", - v040slashing.AddrPubkeyRelationKey(consAddr), + v1.AddrPubkeyRelationKey(consAddr), types.AddrPubkeyRelationKey(consAddr), }, } @@ -52,7 +52,7 @@ func TestStoreMigration(t *testing.T) { } // Run migrations. - err := v043slashing.MigrateStore(ctx, slashingKey) + err := v2.MigrateStore(ctx, slashingKey) require.NoError(t, err) // Make sure the new keys are set and old keys are deleted. diff --git a/x/staking/migrations/v1/types.go b/x/staking/migrations/v1/types.go index 3800d997af..97302ac66d 100644 --- a/x/staking/migrations/v1/types.go +++ b/x/staking/migrations/v1/types.go @@ -9,7 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -90,7 +90,7 @@ func GetValidatorsByPowerIndexKey(validator types.Validator) []byte { powerBytesLen := len(powerBytes) // 8 // key is of format prefix || powerbytes || addrBytes - key := make([]byte, 1+powerBytesLen+v042auth.AddrLen) + key := make([]byte, 1+powerBytesLen+v1auth.AddrLen) key[0] = ValidatorsByPowerIndexKey[0] copy(key[1:powerBytesLen+1], powerBytes) @@ -117,11 +117,11 @@ func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { // GetREDKey returns a key prefix for indexing a redelegation from a delegator // and source validator to a destination validator. func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - key := make([]byte, 1+v042auth.AddrLen*3) + key := make([]byte, 1+v1auth.AddrLen*3) - copy(key[0:v042auth.AddrLen+1], GetREDsKey(delAddr.Bytes())) - copy(key[v042auth.AddrLen+1:2*v042auth.AddrLen+1], valSrcAddr.Bytes()) - copy(key[2*v042auth.AddrLen+1:3*v042auth.AddrLen+1], valDstAddr.Bytes()) + copy(key[0:v1auth.AddrLen+1], GetREDsKey(delAddr.Bytes())) + copy(key[v1auth.AddrLen+1:2*v1auth.AddrLen+1], valSrcAddr.Bytes()) + copy(key[2*v1auth.AddrLen+1:3*v1auth.AddrLen+1], valDstAddr.Bytes()) return key } @@ -133,10 +133,10 @@ func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V offset := len(REDSFromValsSrcKey) // key is of the form REDSFromValsSrcKey || delAddr || valDstAddr - key := make([]byte, len(REDSFromValsSrcKey)+2*v042auth.AddrLen) + key := make([]byte, len(REDSFromValsSrcKey)+2*v1auth.AddrLen) copy(key[0:offset], REDSFromValsSrcKey) - copy(key[offset:offset+v042auth.AddrLen], delAddr.Bytes()) - copy(key[offset+v042auth.AddrLen:offset+2*v042auth.AddrLen], valDstAddr.Bytes()) + copy(key[offset:offset+v1auth.AddrLen], delAddr.Bytes()) + copy(key[offset+v1auth.AddrLen:offset+2*v1auth.AddrLen], valDstAddr.Bytes()) return key } @@ -148,10 +148,10 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V offset := len(REDSToValsDstKey) // key is of the form REDSToValsDstKey || delAddr || valSrcAddr - key := make([]byte, len(REDSToValsDstKey)+2*v042auth.AddrLen) + key := make([]byte, len(REDSToValsDstKey)+2*v1auth.AddrLen) copy(key[0:offset], REDSToValsDstKey) - copy(key[offset:offset+v042auth.AddrLen], delAddr.Bytes()) - copy(key[offset+v042auth.AddrLen:offset+2*v042auth.AddrLen], valSrcAddr.Bytes()) + copy(key[offset:offset+v1auth.AddrLen], delAddr.Bytes()) + copy(key[offset+v1auth.AddrLen:offset+2*v1auth.AddrLen], valSrcAddr.Bytes()) return key } @@ -159,11 +159,11 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V // GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte - kv.AssertKeyLength(indexKey, 3*v042auth.AddrLen+1) + kv.AssertKeyLength(indexKey, 3*v1auth.AddrLen+1) - valSrcAddr := indexKey[1 : v042auth.AddrLen+1] - delAddr := indexKey[v042auth.AddrLen+1 : 2*v042auth.AddrLen+1] - valDstAddr := indexKey[2*v042auth.AddrLen+1 : 3*v042auth.AddrLen+1] + valSrcAddr := indexKey[1 : v1auth.AddrLen+1] + delAddr := indexKey[v1auth.AddrLen+1 : 2*v1auth.AddrLen+1] + valDstAddr := indexKey[2*v1auth.AddrLen+1 : 3*v1auth.AddrLen+1] return GetREDKey(delAddr, valSrcAddr, valDstAddr) } @@ -171,11 +171,11 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte - kv.AssertKeyLength(indexKey, 3*v042auth.AddrLen+1) + kv.AssertKeyLength(indexKey, 3*v1auth.AddrLen+1) - valDstAddr := indexKey[1 : v042auth.AddrLen+1] - delAddr := indexKey[v042auth.AddrLen+1 : 2*v042auth.AddrLen+1] - valSrcAddr := indexKey[2*v042auth.AddrLen+1 : 3*v042auth.AddrLen+1] + valDstAddr := indexKey[1 : v1auth.AddrLen+1] + delAddr := indexKey[v1auth.AddrLen+1 : 2*v1auth.AddrLen+1] + valSrcAddr := indexKey[2*v1auth.AddrLen+1 : 3*v1auth.AddrLen+1] return GetREDKey(delAddr, valSrcAddr, valDstAddr) } @@ -259,11 +259,11 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes - kv.AssertKeyLength(addrs, 2*v042auth.AddrLen) + kv.AssertKeyLength(addrs, 2*v1auth.AddrLen) - kv.AssertKeyAtLeastLength(addrs, v042auth.AddrLen+1) - valAddr := addrs[:v042auth.AddrLen] - delAddr := addrs[v042auth.AddrLen:] + kv.AssertKeyAtLeastLength(addrs, v1auth.AddrLen+1) + valAddr := addrs[:v1auth.AddrLen] + delAddr := addrs[v1auth.AddrLen:] return GetUBDKey(delAddr, valAddr) } diff --git a/x/staking/migrations/v2/store.go b/x/staking/migrations/v2/store.go index 2e1476fb00..a376190df1 100644 --- a/x/staking/migrations/v2/store.go +++ b/x/staking/migrations/v2/store.go @@ -5,8 +5,8 @@ import ( storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" - v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" - v043distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v043" + v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1" + v2distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v2" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -22,9 +22,9 @@ func migratePrefixAddressAddressAddress(store sdk.KVStore, prefixBz []byte) { defer oldStoreIter.Close() for ; oldStoreIter.Valid(); oldStoreIter.Next() { - addr1 := oldStoreIter.Key()[:v042auth.AddrLen] - addr2 := oldStoreIter.Key()[v042auth.AddrLen : 2*v042auth.AddrLen] - addr3 := oldStoreIter.Key()[2*v042auth.AddrLen:] + addr1 := oldStoreIter.Key()[:v1auth.AddrLen] + addr2 := oldStoreIter.Key()[v1auth.AddrLen : 2*v1auth.AddrLen] + addr3 := oldStoreIter.Key()[2*v1auth.AddrLen:] newStoreKey := append(append(append( prefixBz, address.MustLengthPrefix(addr1)...), address.MustLengthPrefix(addr2)...), address.MustLengthPrefix(addr3)..., @@ -62,15 +62,15 @@ func migrateValidatorsByPowerIndexKey(store sdk.KVStore) { func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { store := ctx.KVStore(storeKey) - v043distribution.MigratePrefixAddress(store, v1.LastValidatorPowerKey) + v2distribution.MigratePrefixAddress(store, v1.LastValidatorPowerKey) - v043distribution.MigratePrefixAddress(store, v1.ValidatorsKey) - v043distribution.MigratePrefixAddress(store, v1.ValidatorsByConsAddrKey) + v2distribution.MigratePrefixAddress(store, v1.ValidatorsKey) + v2distribution.MigratePrefixAddress(store, v1.ValidatorsByConsAddrKey) migrateValidatorsByPowerIndexKey(store) - v043distribution.MigratePrefixAddressAddress(store, v1.DelegationKey) - v043distribution.MigratePrefixAddressAddress(store, v1.UnbondingDelegationKey) - v043distribution.MigratePrefixAddressAddress(store, v1.UnbondingDelegationByValIndexKey) + v2distribution.MigratePrefixAddressAddress(store, v1.DelegationKey) + v2distribution.MigratePrefixAddressAddress(store, v1.UnbondingDelegationKey) + v2distribution.MigratePrefixAddressAddress(store, v1.UnbondingDelegationByValIndexKey) migratePrefixAddressAddressAddress(store, v1.RedelegationKey) migratePrefixAddressAddressAddress(store, v1.RedelegationByValSrcIndexKey) migratePrefixAddressAddressAddress(store, v1.RedelegationByValDstIndexKey) diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 9aa94e3bb6..505214ac8e 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -3,6 +3,7 @@ package upgrade import ( "context" "encoding/json" + "fmt" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" @@ -104,7 +105,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { m := keeper.NewMigrator(am.keeper) err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2) if err != nil { - panic(err) + panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) } }