From 758e8e202894a805b11addf643d33b815f522074 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 2 Aug 2022 20:41:41 +0200 Subject: [PATCH] feat: Add convenience method for constructing key to access account's balance for a given denom (backport #12674) (#12745) * feat: Add convenience method for constructing key to access account's balance for a given denom (#12674) This PR adds a convenience method for constructing the key necessary to query for the account's balance of a given denom. I ran into this issue since we are using ABCI query now to perform balance requests because we are also requesting merkle proofs for the returned balance [here](https://github.com/celestiaorg/celestia-node/pull/911/files#diff-0ee31f5a7bd88e9f758e6bebdf3ee36365519e55a451098d9638c39afe5eac42R144). It would be nice to have a definitive convenience method for constructing the key. [Ref.](github.com/celestiaorg/celestia-node/pull/911) (cherry picked from commit a1777a87b65fad74732cfe1a4c27683dcffffbfe) # Conflicts: # CHANGELOG.md # x/bank/legacy/v043/store.go # x/bank/types/keys.go * Update CHANGELOG.md * fix conflict Co-authored-by: rene <41963722+renaynay@users.noreply.github.com> Co-authored-by: Marko --- CHANGELOG.md | 1 + x/bank/legacy/v043/store.go | 2 +- x/bank/legacy/v043/store_test.go | 4 ++-- x/bank/types/key.go | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ebdf1237341..4975bb3adae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/params) [#12724](https://github.com/cosmos/cosmos-sdk/pull/12724) Add `GetParamSetIfExists` function to params `Subspace` to prevent panics on breaking changes. * [#12668](https://github.com/cosmos/cosmos-sdk/pull/12668) Add `authz_msg_index` event attribute to message events emitted when executing via `MsgExec` through `x/authz`. * [#12697](https://github.com/cosmos/cosmos-sdk/pull/12697) Upgrade IAVL to v0.19.0 with fast index and error propagation. NOTE: first start will take a while to propagate into new model. +* (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. ### Bug Fixes diff --git a/x/bank/legacy/v043/store.go b/x/bank/legacy/v043/store.go index c4924f17df2d..aa8f8632986b 100644 --- a/x/bank/legacy/v043/store.go +++ b/x/bank/legacy/v043/store.go @@ -61,7 +61,7 @@ func migrateBalanceKeys(store sdk.KVStore) { for ; oldStoreIter.Valid(); oldStoreIter.Next() { addr := v040bank.AddressFromBalancesStore(oldStoreIter.Key()) denom := oldStoreIter.Key()[v040auth.AddrLen:] - newStoreKey := append(types.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/legacy/v043/store_test.go b/x/bank/legacy/v043/store_test.go index 2b6af2c6ed9b..8d6474de8b83 100644 --- a/x/bank/legacy/v043/store_test.go +++ b/x/bank/legacy/v043/store_test.go @@ -90,13 +90,13 @@ func TestBalanceKeysMigration(t *testing.T) { err = v043bank.MigrateStore(ctx, bankKey, encCfg.Marshaler) 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 e91a38970e8b..de8fa7110c9a 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -57,3 +57,9 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) { func CreateAccountBalancesPrefix(addr []byte) []byte { return append(BalancesPrefix, address.MustLengthPrefix(addr)...) } + +// 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...) +}