Skip to content

Commit a1777a8

Browse files
authored
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)
1 parent 9f5ee97 commit a1777a8

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
307307
* (deps) Downgrade to Tendermint [v0.34.20-rc0](https://github.com/tendermint/tendermint/releases/tag/v0.34.20-rc0).
308308
* [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default.
309309
* [#12153](https://github.com/cosmos/cosmos-sdk/pull/12153) Add a new `NewSimulationManagerFromAppModules` constructor, to simplify simulation wiring.
310+
* (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.
310311

311312
### Bug Fixes
312313

x/bank/migrations/v2/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func migrateBalanceKeys(store sdk.KVStore) {
6363
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
6464
addr := v1.AddressFromBalancesStore(oldStoreIter.Key())
6565
denom := oldStoreIter.Key()[v042auth.AddrLen:]
66-
newStoreKey := append(CreateAccountBalancesPrefix(addr), denom...)
66+
newStoreKey := types.CreatePrefixedAccountStoreKey(addr, denom)
6767

6868
// Set new key on store. Values don't change.
6969
store.Set(newStoreKey, oldStoreIter.Value())

x/bank/migrations/v2/store_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ func TestBalanceKeysMigration(t *testing.T) {
9393
err = v2bank.MigrateStore(ctx, bankKey, encCfg.Codec)
9494
require.NoError(t, err)
9595

96-
newKey := append(types.CreateAccountBalancesPrefix(addr), []byte(fooCoin.Denom)...)
96+
newKey := types.CreatePrefixedAccountStoreKey(addr, []byte(fooCoin.Denom))
9797
// -7 because we replaced "balances" with 0x02,
9898
// +1 because we added length-prefix to address.
9999
require.Equal(t, len(oldFooKey)-7+1, len(newKey))
100100
require.Nil(t, store.Get(oldFooKey))
101101
require.Equal(t, fooBz, store.Get(newKey))
102102

103-
newKeyFooBar := append(types.CreateAccountBalancesPrefix(addr), []byte(fooBarCoin.Denom)...)
103+
newKeyFooBar := types.CreatePrefixedAccountStoreKey(addr, []byte(fooBarCoin.Denom))
104104
require.Nil(t, store.Get(newKeyFooBar)) // after migration zero balances pruned from store.
105105
}

x/bank/types/keys.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ func AddressAndDenomFromBalancesStore(key []byte) (sdk.AccAddress, string, error
6868
return key[1 : addrBound+1], string(key[addrBound+1:]), nil
6969
}
7070

71+
// CreatePrefixedAccountStoreKey returns the key for the given account and denomination.
72+
// This method can be used when performing an ABCI query for the balance of an account.
73+
func CreatePrefixedAccountStoreKey(addr []byte, denom []byte) []byte {
74+
return append(CreateAccountBalancesPrefix(addr), denom...)
75+
}
76+
7177
// CreateAccountBalancesPrefix creates the prefix for an account's balances.
7278
func CreateAccountBalancesPrefix(addr []byte) []byte {
7379
return append(BalancesPrefix, address.MustLengthPrefix(addr)...)

0 commit comments

Comments
 (0)