Skip to content
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

fix: x/bank/044 migrateDenomMetadata #10239

Merged
merged 8 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* `SaveOfflineKey`
* Take `keyring.Record` instead of `Info` as first argument in:
* `MkConsKeyOutput`
* `MkValKeyOutput`
* `MkValKeyOutput`
* `MkAccKeyOutput`
* [\#10077](https://github.com/cosmos/cosmos-sdk/pull/10077) Remove telemetry on `GasKV` and `CacheKV` store Get/Set operations, significantly improving their performance.
* [\#10022](https://github.com/cosmos/cosmos-sdk/pull/10022) `AuthKeeper` interface in `x/auth` now includes a function `HasAccount`.
Expand Down Expand Up @@ -139,6 +139,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (server) [#10016](https://github.com/cosmos/cosmos-sdk/issues/10016) Fix marshaling of index-events into server config file.
* (x/feegrant) [\#10049](https://github.com/cosmos/cosmos-sdk/issues/10049) Fixed the error message when `period` or `period-limit` flag is not set on a feegrant grant transaction.
* [\#10184](https://github.com/cosmos/cosmos-sdk/pull/10184) Fixed CLI tx commands to no longer explicitly require the chain-id flag as this value can come from a user config.
* [\#10239](https://github.com/cosmos/cosmos-sdk/pull/10239) Fixed x/bank/044 migrateDenomMetadata.


### State Machine Breaking
Expand Down
7 changes: 5 additions & 2 deletions x/bank/migrations/v044/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var (
)

func CreateAddressDenomPrefix(denom string) []byte {
key := append(DenomAddressPrefix, []byte(denom)...)
return append(key, 0)
key := make([]byte, len(DenomAddressPrefix)+len(denom)+1)
copy(key, DenomAddressPrefix)
copy(key[len(DenomAddressPrefix):], denom)
// key[last] = 0 - not needed, because this is the default
return key
}
9 changes: 6 additions & 3 deletions x/bank/migrations/v044/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ func migrateDenomMetadata(store sdk.KVStore) error {

for ; oldDenomMetaDataIter.Valid(); oldDenomMetaDataIter.Next() {
oldKey := oldDenomMetaDataIter.Key()
// old key: prefix_bytes | denom_bytes | denom_bytes
newKey := append(types.DenomMetadataPrefix, oldKey[:len(oldKey)/2+1]...)
l := len(oldKey)/2 + 1

var newKey = make([]byte, len(types.DenomMetadataPrefix)+l)
copy(newKey, types.DenomMetadataPrefix)
// old key: prefix_bytes | denom_bytes | denom_bytes
newKey = append(newKey, oldKey[:l]...)
store.Set(newKey, oldDenomMetaDataIter.Value())
oldDenomMetaDataStore.Delete(oldDenomMetaDataIter.Key())
oldDenomMetaDataStore.Delete(oldKey)
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions x/bank/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func CreateAccountBalancesPrefix(addr []byte) []byte {
// CreateDenomAddressPrefix creates a prefix for a reverse index of denomination
// to account balance for that denomination.
func CreateDenomAddressPrefix(denom string) []byte {
key := append(DenomAddressPrefix, []byte(denom)...)
return append(key, 0)
// we add a "zero" byte at the end - null byte terminator, to allow prefix denom prefix
// scan. Setting it is not needed (key[last] = 0) - because this is the default.
key := make([]byte, len(DenomAddressPrefix)+len(denom)+1)
copy(key, DenomAddressPrefix)
copy(key[len(DenomAddressPrefix):], denom)
return key
}
12 changes: 12 additions & 0 deletions x/bank/types/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ func TestAddressFromBalancesStore(t *testing.T) {
})
}
}

func TestCreateDenomAddressPrefix(t *testing.T) {
require := require.New(t)

key := types.CreateDenomAddressPrefix("")
require.Len(key, len(types.DenomAddressPrefix)+1)
require.Equal(append(types.DenomAddressPrefix, 0), key)

key = types.CreateDenomAddressPrefix("abc")
require.Len(key, len(types.DenomAddressPrefix)+4)
require.Equal(append(types.DenomAddressPrefix, 'a', 'b', 'c', 0), key)
}