-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(staking)!: binary format for HistoricalInfoKey #15701
Merged
mergify
merged 14 commits into
cosmos:main
from
babadro:11463-binary-historical-info-key
Apr 13, 2023
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
840d158
Binary format for HistoricalInfoKey
babadro d5b1c5b
Modify changelog file.
babadro d6f0053
test migrations
babadro c313538
unit tests
babadro 49ec543
fix staking migration to v2
babadro 1a018c9
fix DeterministicIterations test for HistoricalInfo
babadro 8e7a416
fix changelog: move note to unreleased changes section
babadro 0dea3f8
create duplicate key in migration package
babadro b792c92
Merge branch 'main' into 11463-binary-historical-info-key
babadro ac04fc4
Merge branch 'main' into 11463-binary-historical-info-key
babadro 7e428e4
Merge branch 'main' into 11463-binary-historical-info-key
julienrbrt 157b765
Merge branch 'main' into 11463-binary-historical-info-key
julienrbrt e5edb6e
Merge branch 'main' into 11463-binary-historical-info-key
babadro 7ea12e4
lint fix
babadro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package v5_test | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"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" | ||
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" | ||
) | ||
|
||
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: v2.GetHistoricalInfoKey(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 createHistoricalInfo(height int64, chainID string) *stackingtypes.HistoricalInfo { | ||
return &stackingtypes.HistoricalInfo{Header: cmtproto.Header{ChainID: chainID, Height: height}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
alexanderbez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Set new key on store. Values don't change. | ||
store.Set(newStoreKey, oldStoreIter.Value()) | ||
oldStore.Delete(oldStoreIter.Key()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in the wrong section. Move it up to the unreleased section above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed