From c05becee08c28d0482ce8338a0a2ccd7ba033634 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Tue, 10 Aug 2021 15:29:24 +0530 Subject: [PATCH 1/5] remove redundant denom --- x/bank/keeper/keeper.go | 4 ++-- x/bank/types/key.go | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index cc8ded240136..150d200cf0ad 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -218,7 +218,7 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { // false otherwise. func (k BaseKeeper) GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool) { store := ctx.KVStore(k.storeKey) - store = prefix.NewStore(store, types.DenomMetadataKey(denom)) + store = prefix.NewStore(store, types.DenomMetadataPrefix) bz := store.Get([]byte(denom)) if bz == nil { @@ -265,7 +265,7 @@ func (k BaseKeeper) IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metad // SetDenomMetaData sets the denominations metadata func (k BaseKeeper) SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata) { store := ctx.KVStore(k.storeKey) - denomMetaDataStore := prefix.NewStore(store, types.DenomMetadataKey(denomMetaData.Base)) + denomMetaDataStore := prefix.NewStore(store, types.DenomMetadataPrefix) m := k.cdc.MustMarshal(&denomMetaData) denomMetaDataStore.Set([]byte(denomMetaData.Base), m) diff --git a/x/bank/types/key.go b/x/bank/types/key.go index e3b6aa87afa3..dd01a4d49065 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -31,12 +31,6 @@ var ( BalancesPrefix = []byte{0x02} ) -// DenomMetadataKey returns the denomination metadata key. -func DenomMetadataKey(denom string) []byte { - d := []byte(denom) - return append(DenomMetadataPrefix, d...) -} - // AddressAndDenomFromBalancesStore returns an account address and denom from a balances prefix // store. The key must not contain the prefix BalancesPrefix as the prefix store // iterator discards the actual prefix. From 52ddb628c0e655a66454ab640dc7174497da606f Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Tue, 10 Aug 2021 20:39:27 +0530 Subject: [PATCH 2/5] add migrations --- x/bank/migrations/v043/keys.go | 11 ++++- x/bank/migrations/v044/store.go | 26 +++++++++- x/bank/migrations/v044/store_test.go | 74 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/x/bank/migrations/v043/keys.go b/x/bank/migrations/v043/keys.go index 01a4aab2ab51..ef24217ef0f3 100644 --- a/x/bank/migrations/v043/keys.go +++ b/x/bank/migrations/v043/keys.go @@ -12,8 +12,9 @@ const ( ) var ( - SupplyKey = []byte{0x00} - BalancesPrefix = []byte{0x02} + SupplyKey = []byte{0x00} + BalancesPrefix = []byte{0x02} + DenomMetadataPrefix = []byte{0x1} ErrInvalidKey = errors.New("invalid key") ) @@ -36,3 +37,9 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) { return key[1 : bound+1], nil } + +// DenomMetadataKey returns the denomination metadata key. +func DenomMetadataKey(denom string) []byte { + d := []byte(denom) + return append(DenomMetadataPrefix, d...) +} diff --git a/x/bank/migrations/v044/store.go b/x/bank/migrations/v044/store.go index faaa07a9068a..1f4b85a08683 100644 --- a/x/bank/migrations/v044/store.go +++ b/x/bank/migrations/v044/store.go @@ -14,9 +14,15 @@ import ( // // - Migrate coin storage to save only amount. // - Add an additional reverse index from denomination to address. +// - Remove redundant denom from denom metadata store key. func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { store := ctx.KVStore(storeKey) - return addDenomReverseIndex(store, cdc) + err := addDenomReverseIndex(store, cdc) + if err != nil { + return err + } + + return migrateDenomMetadata(store, cdc) } func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { @@ -64,3 +70,21 @@ func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { return nil } + +func migrateDenomMetadata(store sdk.KVStore, cdc codec.BinaryCodec) error { + oldDenomMetaDataStore := prefix.NewStore(store, v043.DenomMetadataPrefix) + + oldDenomMetaDataIter := oldDenomMetaDataStore.Iterator(nil, nil) + defer oldDenomMetaDataIter.Close() + + 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]...) + + store.Set(newKey, oldDenomMetaDataIter.Value()) + oldDenomMetaDataStore.Delete(oldDenomMetaDataIter.Key()) + } + + return nil +} diff --git a/x/bank/migrations/v044/store_test.go b/x/bank/migrations/v044/store_test.go index 61028e389440..37c268d3d702 100644 --- a/x/bank/migrations/v044/store_test.go +++ b/x/bank/migrations/v044/store_test.go @@ -52,3 +52,77 @@ func TestMigrateStore(t *testing.T) { require.NotNil(t, bz) } } + +func TestMigrateDenomMetaData(t *testing.T) { + encCfg := simapp.MakeTestEncodingConfig() + bankKey := sdk.NewKVStoreKey("bank") + ctx := testutil.DefaultContext(bankKey, sdk.NewTransientStoreKey("transient_test")) + store := ctx.KVStore(bankKey) + metaData := []types.Metadata{ + { + Name: "Cosmos Hub Atom", + Symbol: "ATOM", + Description: "The native staking token of the Cosmos Hub.", + DenomUnits: []*types.DenomUnit{ + {"uatom", uint32(0), []string{"microatom"}}, + {"matom", uint32(3), []string{"milliatom"}}, + {"atom", uint32(6), nil}, + }, + Base: "uatom", + Display: "atom", + }, + { + Name: "Token", + Symbol: "TOKEN", + Description: "The native staking token of the Token Hub.", + DenomUnits: []*types.DenomUnit{ + {"1token", uint32(5), []string{"decitoken"}}, + {"2token", uint32(4), []string{"centitoken"}}, + {"3token", uint32(7), []string{"dekatoken"}}, + }, + Base: "utoken", + Display: "token", + }, + } + denomMetadataStore := prefix.NewStore(store, v043.DenomMetadataPrefix) + + for i := range []int{0, 1} { + key := append(v043.DenomMetadataPrefix, []byte(metaData[i].Base)...) + key = append(key, []byte(metaData[i].Base)...) + bz, err := encCfg.Codec.Marshal(&metaData[i]) + require.NoError(t, err) + denomMetadataStore.Set(key, bz) + } + + require.NoError(t, v044.MigrateStore(ctx, bankKey, encCfg.Codec)) + + denomMetadataStore = prefix.NewStore(store, v043.DenomMetadataPrefix) + denomMetadataIter := denomMetadataStore.Iterator(nil, nil) + defer denomMetadataIter.Close() + for i := 0; denomMetadataIter.Valid(); denomMetadataIter.Next() { + var result types.Metadata + newKey := denomMetadataIter.Key() + + // make sure old entry is deleted + oldKey := append(newKey, newKey[1:]...) + bz := denomMetadataStore.Get(oldKey) + require.Nil(t, bz) + + require.Equal(t, string(newKey)[1:], metaData[i].Base) + bz = denomMetadataStore.Get(denomMetadataIter.Key()) + require.NotNil(t, bz) + err := encCfg.Codec.Unmarshal(bz, &result) + require.NoError(t, err) + assertMetaDataEqual(t, metaData[i], result) + i++ + } +} + +func assertMetaDataEqual(t *testing.T, expected, actual types.Metadata) { + require.Equal(t, expected.GetBase(), actual.GetBase()) + require.Equal(t, expected.GetDisplay(), actual.GetDisplay()) + require.Equal(t, expected.GetDescription(), actual.GetDescription()) + require.Equal(t, expected.GetDenomUnits()[1].GetDenom(), actual.GetDenomUnits()[1].GetDenom()) + require.Equal(t, expected.GetDenomUnits()[1].GetExponent(), actual.GetDenomUnits()[1].GetExponent()) + require.Equal(t, expected.GetDenomUnits()[1].GetAliases(), actual.GetDenomUnits()[1].GetAliases()) +} From 6bbbf15f87cb804bc97842f1e1242495609f536f Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Tue, 10 Aug 2021 20:42:09 +0530 Subject: [PATCH 3/5] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94efc4d653d1..568f1af6a4f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#9611](https://github.com/cosmos/cosmos-sdk/pull/9611) Introduce a new index to act as a reverse index between a denomination and address allowing to query for token holders of a specific denomination. `DenomOwners` is updated to use the new reverse index. * (x/bank) [\#9832] (https://github.com/cosmos/cosmos-sdk/pull/9832) Account balance is stored as `sdk.Int` rather than `sdk.Coin`. +* (x/bank) [\#9890] (https://github.com/cosmos/cosmos-sdk/pull/9890) Remove redundant denom from metadata store key. ## [v0.43.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0-rc0) - 2021-06-25 From 3b5556a1bab6d908d66615444d90457f3443ad95 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Tue, 10 Aug 2021 21:42:54 +0530 Subject: [PATCH 4/5] fix lint error --- x/bank/migrations/v044/store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/bank/migrations/v044/store.go b/x/bank/migrations/v044/store.go index 1f4b85a08683..371514851711 100644 --- a/x/bank/migrations/v044/store.go +++ b/x/bank/migrations/v044/store.go @@ -22,7 +22,7 @@ func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) return err } - return migrateDenomMetadata(store, cdc) + return migrateDenomMetadata(store) } func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { @@ -71,7 +71,7 @@ func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { return nil } -func migrateDenomMetadata(store sdk.KVStore, cdc codec.BinaryCodec) error { +func migrateDenomMetadata(store sdk.KVStore) error { oldDenomMetaDataStore := prefix.NewStore(store, v043.DenomMetadataPrefix) oldDenomMetaDataIter := oldDenomMetaDataStore.Iterator(nil, nil) From bcc60c7e8667dc262a1be97bd39d8bdf36a80ca2 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Wed, 11 Aug 2021 10:47:55 +0530 Subject: [PATCH 5/5] update changelog --- CHANGELOG.md | 2 +- x/bank/migrations/v044/store.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df7fc6557352..1b2131d33a01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -110,7 +110,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#9611](https://github.com/cosmos/cosmos-sdk/pull/9611) Introduce a new index to act as a reverse index between a denomination and address allowing to query for token holders of a specific denomination. `DenomOwners` is updated to use the new reverse index. * (x/bank) [\#9832] (https://github.com/cosmos/cosmos-sdk/pull/9832) Account balance is stored as `sdk.Int` rather than `sdk.Coin`. -* (x/bank) [\#9890] (https://github.com/cosmos/cosmos-sdk/pull/9890) Remove redundant denom from metadata store key. +* (x/bank) [\#9890] (https://github.com/cosmos/cosmos-sdk/pull/9890) Remove duplicate denom from denom metadata key. ## [v0.43.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0-rc0) - 2021-06-25 diff --git a/x/bank/migrations/v044/store.go b/x/bank/migrations/v044/store.go index 371514851711..b43404573121 100644 --- a/x/bank/migrations/v044/store.go +++ b/x/bank/migrations/v044/store.go @@ -14,7 +14,7 @@ import ( // // - Migrate coin storage to save only amount. // - Add an additional reverse index from denomination to address. -// - Remove redundant denom from denom metadata store key. +// - Remove duplicate denom from denom metadata store key. func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { store := ctx.KVStore(storeKey) err := addDenomReverseIndex(store, cdc)