From 840d15897833d1ef1ec6ca7abc056bee79029422 Mon Sep 17 00:00:00 2001 From: babadro Date: Tue, 4 Apr 2023 07:59:17 +0200 Subject: [PATCH 1/9] Binary format for HistoricalInfoKey --- x/staking/keeper/historical_info_test.go | 8 ++++---- x/staking/types/keys.go | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index 9709c5440b5..f3b68ceec48 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -136,9 +136,9 @@ func (s *KeeperTestSuite) TestGetAllHistoricalInfo() { testutil.NewValidator(s.T(), addrVals[1], PKs[1]), } - header1 := cmtproto.Header{ChainID: "HelloChain", Height: 10} - header2 := cmtproto.Header{ChainID: "HelloChain", Height: 11} - header3 := cmtproto.Header{ChainID: "HelloChain", Height: 12} + header1 := cmtproto.Header{ChainID: "HelloChain", Height: 9} + header2 := cmtproto.Header{ChainID: "HelloChain", Height: 10} + header3 := cmtproto.Header{ChainID: "HelloChain", Height: 11} hist1 := stakingtypes.HistoricalInfo{Header: header1, Valset: valSet} hist2 := stakingtypes.HistoricalInfo{Header: header2, Valset: valSet} @@ -147,7 +147,7 @@ func (s *KeeperTestSuite) TestGetAllHistoricalInfo() { expHistInfos := []stakingtypes.HistoricalInfo{hist1, hist2, hist3} for i, hi := range expHistInfos { - keeper.SetHistoricalInfo(ctx, int64(10+i), &hi) //nolint:gosec // G601: Implicit memory aliasing in for loop. + keeper.SetHistoricalInfo(ctx, int64(9+i), &hi) //nolint:gosec // G601: Implicit memory aliasing in for loop. } infos := keeper.GetAllHistoricalInfo(ctx) diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index f8f642af5c9..d197d58d1d3 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "fmt" - "strconv" "time" "cosmossdk.io/math" @@ -376,5 +375,7 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr // GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. func GetHistoricalInfoKey(height int64) []byte { - return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) + heightBytes := make([]byte, 8) + binary.BigEndian.PutUint64(heightBytes, uint64(height)) + return append(HistoricalInfoKey, heightBytes...) } From d5b1c5b425af05e58d91eaf39f0012d8aa7de36d Mon Sep 17 00:00:00 2001 From: babadro Date: Wed, 5 Apr 2023 06:51:35 +0200 Subject: [PATCH 2/9] Modify changelog file. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc3843c0a30..5681849d666 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -158,6 +158,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Client Breaking Changes +* (x/staking) [#15701](https://github.com/cosmos/cosmos-sdk/pull/15701) `HistoricalInfoKey` now has a binary format. * (grpc-web) [#14652](https://github.com/cosmos/cosmos-sdk/pull/14652) Use same port for gRPC-Web and the API server. ### CLI Breaking Changes @@ -314,6 +315,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### State Machine Breaking +* (x/staking) [#15701](https://github.com/cosmos/cosmos-sdk/pull/15701) The `HistoricalInfoKey` has been updated to use a binary format. * (baseapp, x/auth/posthandler) [#13940](https://github.com/cosmos/cosmos-sdk/pull/13940) Update `PostHandler` to receive the `runTx` success boolean. * (store) [#14378](https://github.com/cosmos/cosmos-sdk/pull/14378) The `CacheKV` store is thread-safe again, which includes improved iteration and deletion logic. Iteration is on a strictly isolated view now, which is breaking from previous behavior. * (x/bank) [#14538](https://github.com/cosmos/cosmos-sdk/pull/14538) Validate denom in bank balances GRPC queries. From d6f0053fd750eb09203e08568c90abc41c863772 Mon Sep 17 00:00:00 2001 From: babadro Date: Fri, 7 Apr 2023 09:07:30 +0200 Subject: [PATCH 3/9] test migrations --- x/staking/keeper/migrations.go | 6 ++ x/staking/migrations/v5/migrations_test.go | 75 ++++++++++++++++++++++ x/staking/migrations/v5/store.go | 47 ++++++++++++++ x/staking/module.go | 5 +- x/staking/types/keys_test.go | 19 ++++++ 5 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 x/staking/migrations/v5/migrations_test.go create mode 100644 x/staking/migrations/v5/store.go diff --git a/x/staking/keeper/migrations.go b/x/staking/keeper/migrations.go index b34a7993bfc..26dd511e219 100644 --- a/x/staking/keeper/migrations.go +++ b/x/staking/keeper/migrations.go @@ -6,6 +6,7 @@ import ( v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" v3 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v3" v4 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v4" + v5 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v5" ) // Migrator is a struct for handling in-place store migrations. @@ -36,3 +37,8 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error { func (m Migrator) Migrate3to4(ctx sdk.Context) error { return v4.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace) } + +// Migrate4to5 migrates x/staking state from consensus version 4 to 5. +func (m Migrator) Migrate4to5(ctx sdk.Context) error { + return v5.MigrateStore(ctx, m.keeper.storeKey) +} diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go new file mode 100644 index 00000000000..1938e4d77b5 --- /dev/null +++ b/x/staking/migrations/v5/migrations_test.go @@ -0,0 +1,75 @@ +package v5_test + +import ( + "math" + "math/rand" + "strconv" + "testing" + "time" + + storetypes "cosmossdk.io/store/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/testutil" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + v5 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v5" + stackingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" +) + +func TestHistoricalKeysMigration(t *testing.T) { + storeKey := storetypes.NewKVStoreKey("staking") + tKey := storetypes.NewTransientStoreKey("transient_test") + ctx := testutil.DefaultContext(storeKey, tKey) + store := ctx.KVStore(storeKey) + + type testCase struct { + oldKey, newKey []byte + historicalInfo []byte + } + + testCases := make(map[int64]testCase) + + // edge cases + testCases[0], testCases[1], testCases[math.MaxInt32] = testCase{}, testCase{}, testCase{} + + // random cases + seed := time.Now().UnixNano() + r := rand.New(rand.NewSource(seed)) + for i := 0; i < 10; i++ { + height := r.Intn(math.MaxInt32-2) + 2 + + testCases[int64(height)] = testCase{} + } + + cdc := moduletestutil.MakeTestEncodingConfig().Codec + for height := range testCases { + testCases[height] = testCase{ + oldKey: getOldHistoricalInfoKey(height), + newKey: stackingtypes.GetHistoricalInfoKey(height), + historicalInfo: cdc.MustMarshal(createHistoricalInfo(height, "testChainID")), + } + } + + // populate store using old key format + for _, tc := range testCases { + store.Set(tc.oldKey, tc.historicalInfo) + } + + // migrate store to new key format + require.NoErrorf(t, v5.MigrateStore(ctx, storeKey), "v5.MigrateStore failed, seed: %d", seed) + + // check results + for _, tc := range testCases { + require.Nilf(t, store.Get(tc.oldKey), "old key should be deleted, seed: %d", seed) + require.NotNilf(t, store.Get(tc.newKey), "new key should be created, seed: %d", seed) + require.Equalf(t, tc.historicalInfo, store.Get(tc.newKey), "seed: %d", seed) + } +} + +func getOldHistoricalInfoKey(height int64) []byte { + return append(stackingtypes.HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) +} + +func createHistoricalInfo(height int64, chainID string) *stackingtypes.HistoricalInfo { + return &stackingtypes.HistoricalInfo{Header: cmtproto.Header{ChainID: chainID, Height: height}} +} diff --git a/x/staking/migrations/v5/store.go b/x/staking/migrations/v5/store.go new file mode 100644 index 00000000000..3831bb796c3 --- /dev/null +++ b/x/staking/migrations/v5/store.go @@ -0,0 +1,47 @@ +package v5 + +import ( + "fmt" + "strconv" + + "cosmossdk.io/log" + "cosmossdk.io/store/prefix" + storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +// MigrateStore performs in-place store migrations from v4 to v5. +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey) error { + store := ctx.KVStore(storeKey) + return migrateHistoricalInfoKeys(store, ctx.Logger()) +} + +// migrateHistoricalInfoKeys migrate HistoricalInfo keys to binary format +func migrateHistoricalInfoKeys(store storetypes.KVStore, logger log.Logger) error { + // old key is of format: + // prefix (0x50) || heightBytes (string representation of height in 10 base) + // new key is of format: + // prefix (0x50) || heightBytes (byte array representation using big-endian byte order) + oldStore := prefix.NewStore(store, types.HistoricalInfoKey) + + oldStoreIter := oldStore.Iterator(nil, nil) + defer sdk.LogDeferred(logger, func() error { return oldStoreIter.Close() }) + + for ; oldStoreIter.Valid(); oldStoreIter.Next() { + strHeight := oldStoreIter.Key() + + intHeight, err := strconv.ParseInt(string(strHeight), 10, 64) + if err != nil { + return fmt.Errorf("can't parse height from key %q to int64: %v", strHeight, err) + } + + newStoreKey := types.GetHistoricalInfoKey(intHeight) + + // Set new key on store. Values don't change. + store.Set(newStoreKey, oldStoreIter.Value()) + oldStore.Delete(oldStoreIter.Key()) + } + + return nil +} diff --git a/x/staking/module.go b/x/staking/module.go index b58a7ee191d..5e93989da5d 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -34,7 +34,7 @@ import ( ) const ( - consensusVersion uint64 = 4 + consensusVersion uint64 = 5 ) var ( @@ -165,6 +165,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) } + if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5); err != nil { + panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err)) + } } // InitGenesis performs genesis initialization for the staking module. diff --git a/x/staking/types/keys_test.go b/x/staking/types/keys_test.go index e5b96e48444..9c462cfed8f 100644 --- a/x/staking/types/keys_test.go +++ b/x/staking/types/keys_test.go @@ -8,6 +8,7 @@ import ( "time" "cosmossdk.io/math" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" @@ -129,3 +130,21 @@ func TestTestGetValidatorQueueKeyOrder(t *testing.T) { require.Equal(t, -1, bytes.Compare(keyB, endKey)) // keyB <= endKey require.Equal(t, 1, bytes.Compare(keyC, endKey)) // keyB >= endKey } + +func TestGetHistoricalInfoKey(t *testing.T) { + type args struct { + height int64 + } + tests := []struct { + name string + args args + want []byte + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, types.GetHistoricalInfoKey(tt.args.height), "GetHistoricalInfoKey(%v)", tt.args.height) + }) + } +} From c3135383a0beb19f8ab7d9bf33b099ada815810a Mon Sep 17 00:00:00 2001 From: babadro Date: Sat, 8 Apr 2023 07:14:44 +0200 Subject: [PATCH 4/9] unit tests --- x/staking/migrations/v5/migrations_test.go | 8 ++------ x/staking/types/keys_test.go | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go index 1938e4d77b5..f1ac2c37c15 100644 --- a/x/staking/migrations/v5/migrations_test.go +++ b/x/staking/migrations/v5/migrations_test.go @@ -3,7 +3,6 @@ package v5_test import ( "math" "math/rand" - "strconv" "testing" "time" @@ -11,6 +10,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/testutil" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v5 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v5" stackingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" @@ -44,7 +44,7 @@ func TestHistoricalKeysMigration(t *testing.T) { cdc := moduletestutil.MakeTestEncodingConfig().Codec for height := range testCases { testCases[height] = testCase{ - oldKey: getOldHistoricalInfoKey(height), + oldKey: v1.GetHistoricalInfoKey(height), newKey: stackingtypes.GetHistoricalInfoKey(height), historicalInfo: cdc.MustMarshal(createHistoricalInfo(height, "testChainID")), } @@ -66,10 +66,6 @@ func TestHistoricalKeysMigration(t *testing.T) { } } -func getOldHistoricalInfoKey(height int64) []byte { - return append(stackingtypes.HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) -} - func createHistoricalInfo(height int64, chainID string) *stackingtypes.HistoricalInfo { return &stackingtypes.HistoricalInfo{Header: cmtproto.Header{ChainID: chainID, Height: height}} } diff --git a/x/staking/types/keys_test.go b/x/staking/types/keys_test.go index 9c462cfed8f..ce3c55e4540 100644 --- a/x/staking/types/keys_test.go +++ b/x/staking/types/keys_test.go @@ -3,12 +3,13 @@ package types_test import ( "bytes" "encoding/hex" + math2 "math" "math/big" + "strconv" "testing" "time" "cosmossdk.io/math" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" @@ -132,19 +133,19 @@ func TestTestGetValidatorQueueKeyOrder(t *testing.T) { } func TestGetHistoricalInfoKey(t *testing.T) { - type args struct { - height int64 - } tests := []struct { - name string - args args - want []byte + height int64 + want []byte }{ - // TODO: Add test cases. + {0, append(types.HistoricalInfoKey, []byte{0, 0, 0, 0, 0, 0, 0, 0}...)}, + {1, append(types.HistoricalInfoKey, []byte{0, 0, 0, 0, 0, 0, 0, 1}...)}, + {2, append(types.HistoricalInfoKey, []byte{0, 0, 0, 0, 0, 0, 0, 2}...)}, + {514, append(types.HistoricalInfoKey, []byte{0, 0, 0, 0, 0, 0, 2, 2}...)}, + {math2.MaxInt64, append(types.HistoricalInfoKey, []byte{127, 255, 255, 255, 255, 255, 255, 255}...)}, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equalf(t, tt.want, types.GetHistoricalInfoKey(tt.args.height), "GetHistoricalInfoKey(%v)", tt.args.height) + t.Run(strconv.FormatInt(tt.height, 10), func(t *testing.T) { + require.Equal(t, tt.want, types.GetHistoricalInfoKey(tt.height)) }) } } From 49ec5431ee5d07fb16af3b64271d2fb5f91427c3 Mon Sep 17 00:00:00 2001 From: babadro Date: Sun, 9 Apr 2023 06:50:24 +0200 Subject: [PATCH 5/9] fix staking migration to v2 --- x/staking/migrations/v2/keys.go | 11 +++++++++++ x/staking/migrations/v2/store_test.go | 2 +- x/staking/migrations/v5/migrations_test.go | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/x/staking/migrations/v2/keys.go b/x/staking/migrations/v2/keys.go index 439a823206a..39a9c08a607 100644 --- a/x/staking/migrations/v2/keys.go +++ b/x/staking/migrations/v2/keys.go @@ -1,6 +1,17 @@ package v2 +import "strconv" + const ( // ModuleName is the name of the module ModuleName = "staking" ) + +var ( + HistoricalInfoKey = []byte{0x50} // prefix for the historical info +) + +// GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. +func GetHistoricalInfoKey(height int64) []byte { + return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) +} diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 31c8016b1bb..b9416ad4f80 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -114,7 +114,7 @@ func TestStoreMigration(t *testing.T) { { "HistoricalInfoKey", v1.GetHistoricalInfoKey(4), - types.GetHistoricalInfoKey(4), + v2.GetHistoricalInfoKey(4), }, } diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go index f1ac2c37c15..2f8f59cb7cd 100644 --- a/x/staking/migrations/v5/migrations_test.go +++ b/x/staking/migrations/v5/migrations_test.go @@ -10,7 +10,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/testutil" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" + v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" v5 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v5" stackingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" @@ -44,7 +44,7 @@ func TestHistoricalKeysMigration(t *testing.T) { cdc := moduletestutil.MakeTestEncodingConfig().Codec for height := range testCases { testCases[height] = testCase{ - oldKey: v1.GetHistoricalInfoKey(height), + oldKey: v2.GetHistoricalInfoKey(height), newKey: stackingtypes.GetHistoricalInfoKey(height), historicalInfo: cdc.MustMarshal(createHistoricalInfo(height, "testChainID")), } From 1a018c9d590b6cf72371d11374ab2c829a43cf59 Mon Sep 17 00:00:00 2001 From: babadro Date: Sun, 9 Apr 2023 07:14:20 +0200 Subject: [PATCH 6/9] fix DeterministicIterations test for HistoricalInfo --- tests/integration/staking/keeper/determinstic_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index 042882bf421..ded82a8be2d 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -605,7 +605,7 @@ func TestGRPCHistoricalInfo(t *testing.T) { Height: height, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.HistoricalInfo, 1930, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.HistoricalInfo, 1945, false) } func TestGRPCDelegatorValidators(t *testing.T) { From 8e7a416f9cd0e2de63c0e20bfcc7520dc907521e Mon Sep 17 00:00:00 2001 From: babadro Date: Tue, 11 Apr 2023 06:31:53 +0200 Subject: [PATCH 7/9] fix changelog: move note to unreleased changes section --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5681849d666..98797652f7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### State Machine Breaking +* (x/staking) [#15701](https://github.com/cosmos/cosmos-sdk/pull/15701) The `HistoricalInfoKey` has been updated to use a binary format. * (x/feegrant) [#14294](https://github.com/cosmos/cosmos-sdk/pull/14294) Moved the logic of rejecting duplicate grant from `msg_server` to `keeper` method. * (x/staking) [#14590](https://github.com/cosmos/cosmos-sdk/pull/14590) `MsgUndelegateResponse` now includes undelegated amount. `x/staking` module's `keeper.Undelegate` now returns 3 values (completionTime,undelegateAmount,error) instead of 2. @@ -315,7 +316,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### State Machine Breaking -* (x/staking) [#15701](https://github.com/cosmos/cosmos-sdk/pull/15701) The `HistoricalInfoKey` has been updated to use a binary format. * (baseapp, x/auth/posthandler) [#13940](https://github.com/cosmos/cosmos-sdk/pull/13940) Update `PostHandler` to receive the `runTx` success boolean. * (store) [#14378](https://github.com/cosmos/cosmos-sdk/pull/14378) The `CacheKV` store is thread-safe again, which includes improved iteration and deletion logic. Iteration is on a strictly isolated view now, which is breaking from previous behavior. * (x/bank) [#14538](https://github.com/cosmos/cosmos-sdk/pull/14538) Validate denom in bank balances GRPC queries. From 0dea3f8c3faf84fde9b4039052631d3fdb5eb282 Mon Sep 17 00:00:00 2001 From: babadro Date: Tue, 11 Apr 2023 06:46:16 +0200 Subject: [PATCH 8/9] create duplicate key in migration package --- x/staking/migrations/v5/keys.go | 12 ++++++++++++ x/staking/migrations/v5/migrations_test.go | 2 +- x/staking/migrations/v5/store.go | 5 ++--- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 x/staking/migrations/v5/keys.go diff --git a/x/staking/migrations/v5/keys.go b/x/staking/migrations/v5/keys.go new file mode 100644 index 00000000000..3725301f77d --- /dev/null +++ b/x/staking/migrations/v5/keys.go @@ -0,0 +1,12 @@ +package v5 + +import "encoding/binary" + +var HistoricalInfoKey = []byte{0x50} // prefix for the historical info + +// GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. +func GetHistoricalInfoKey(height int64) []byte { + heightBytes := make([]byte, 8) + binary.BigEndian.PutUint64(heightBytes, uint64(height)) + return append(HistoricalInfoKey, heightBytes...) +} diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go index 2f8f59cb7cd..67607ffa4e8 100644 --- a/x/staking/migrations/v5/migrations_test.go +++ b/x/staking/migrations/v5/migrations_test.go @@ -45,7 +45,7 @@ func TestHistoricalKeysMigration(t *testing.T) { for height := range testCases { testCases[height] = testCase{ oldKey: v2.GetHistoricalInfoKey(height), - newKey: stackingtypes.GetHistoricalInfoKey(height), + newKey: v5.GetHistoricalInfoKey(height), historicalInfo: cdc.MustMarshal(createHistoricalInfo(height, "testChainID")), } } diff --git a/x/staking/migrations/v5/store.go b/x/staking/migrations/v5/store.go index 3831bb796c3..4ab2eadece2 100644 --- a/x/staking/migrations/v5/store.go +++ b/x/staking/migrations/v5/store.go @@ -8,7 +8,6 @@ import ( "cosmossdk.io/store/prefix" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking/types" ) // MigrateStore performs in-place store migrations from v4 to v5. @@ -23,7 +22,7 @@ func migrateHistoricalInfoKeys(store storetypes.KVStore, logger log.Logger) erro // prefix (0x50) || heightBytes (string representation of height in 10 base) // new key is of format: // prefix (0x50) || heightBytes (byte array representation using big-endian byte order) - oldStore := prefix.NewStore(store, types.HistoricalInfoKey) + oldStore := prefix.NewStore(store, HistoricalInfoKey) oldStoreIter := oldStore.Iterator(nil, nil) defer sdk.LogDeferred(logger, func() error { return oldStoreIter.Close() }) @@ -36,7 +35,7 @@ func migrateHistoricalInfoKeys(store storetypes.KVStore, logger log.Logger) erro return fmt.Errorf("can't parse height from key %q to int64: %v", strHeight, err) } - newStoreKey := types.GetHistoricalInfoKey(intHeight) + newStoreKey := GetHistoricalInfoKey(intHeight) // Set new key on store. Values don't change. store.Set(newStoreKey, oldStoreIter.Value()) From 7ea12e4961876375336fca976e76731ae81cd77a Mon Sep 17 00:00:00 2001 From: babadro Date: Thu, 13 Apr 2023 06:21:53 +0200 Subject: [PATCH 9/9] lint fix --- x/staking/migrations/v2/keys.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/staking/migrations/v2/keys.go b/x/staking/migrations/v2/keys.go index 39a9c08a607..57ccb0aebee 100644 --- a/x/staking/migrations/v2/keys.go +++ b/x/staking/migrations/v2/keys.go @@ -7,9 +7,7 @@ const ( ModuleName = "staking" ) -var ( - HistoricalInfoKey = []byte{0x50} // prefix for the historical info -) +var HistoricalInfoKey = []byte{0x50} // prefix for the historical info // GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. func GetHistoricalInfoKey(height int64) []byte {