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 1 commit
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
Next Next commit
fix: x/bank/044 migrateDenomMetadata
  • Loading branch information
robert-zaremba committed Sep 27, 2021
commit 7d2ee9ff6630fab84b2a3e4f6ea39bfdb6843a12
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
7 changes: 5 additions & 2 deletions x/bank/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ 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)
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
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
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)
}