diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5eda29249e..30d96c47901a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## [Unreleased] + +### Improvements + +* (x/bank) [#12674](https://github.com/cosmos/cosmos-sdk/pull/12674) Add convenience function `CreatePrefixedAccountStoreKey()` to construct key to access account's balance for a given denom. + ## [v0.46.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0) - 2022-07-26 ### Features diff --git a/x/bank/migrations/v043/store.go b/x/bank/migrations/v043/store.go index d745f7807cf6..bd32ba69920f 100644 --- a/x/bank/migrations/v043/store.go +++ b/x/bank/migrations/v043/store.go @@ -62,7 +62,7 @@ func migrateBalanceKeys(store sdk.KVStore) { for ; oldStoreIter.Valid(); oldStoreIter.Next() { addr := v042bank.AddressFromBalancesStore(oldStoreIter.Key()) denom := oldStoreIter.Key()[v042auth.AddrLen:] - newStoreKey := append(CreateAccountBalancesPrefix(addr), denom...) + newStoreKey := types.CreatePrefixedAccountStoreKey(addr, denom) // Set new key on store. Values don't change. store.Set(newStoreKey, oldStoreIter.Value()) diff --git a/x/bank/migrations/v043/store_test.go b/x/bank/migrations/v043/store_test.go index 04117b3c3b32..6343d4d30eb4 100644 --- a/x/bank/migrations/v043/store_test.go +++ b/x/bank/migrations/v043/store_test.go @@ -90,13 +90,13 @@ func TestBalanceKeysMigration(t *testing.T) { err = v043bank.MigrateStore(ctx, bankKey, encCfg.Codec) require.NoError(t, err) - newKey := append(types.CreateAccountBalancesPrefix(addr), []byte(fooCoin.Denom)...) + newKey := types.CreatePrefixedAccountStoreKey(addr, []byte(fooCoin.Denom)) // -7 because we replaced "balances" with 0x02, // +1 because we added length-prefix to address. require.Equal(t, len(oldFooKey)-7+1, len(newKey)) require.Nil(t, store.Get(oldFooKey)) require.Equal(t, fooBz, store.Get(newKey)) - newKeyFooBar := append(types.CreateAccountBalancesPrefix(addr), []byte(fooBarCoin.Denom)...) + newKeyFooBar := types.CreatePrefixedAccountStoreKey(addr, []byte(fooBarCoin.Denom)) require.Nil(t, store.Get(newKeyFooBar)) // after migration zero balances pruned from store. } diff --git a/x/bank/types/key.go b/x/bank/types/key.go index 44decc3e7160..2517257d7520 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -52,6 +52,12 @@ func AddressAndDenomFromBalancesStore(key []byte) (sdk.AccAddress, string, error return key[1 : addrBound+1], string(key[addrBound+1:]), nil } +// CreatePrefixedAccountStoreKey returns the key for the given account and denomination. +// This method can be used when performing an ABCI query for the balance of an account. +func CreatePrefixedAccountStoreKey(addr []byte, denom []byte) []byte { + return append(CreateAccountBalancesPrefix(addr), denom...) +} + // CreateAccountBalancesPrefix creates the prefix for an account's balances. func CreateAccountBalancesPrefix(addr []byte) []byte { return append(BalancesPrefix, address.MustLengthPrefix(addr)...)