Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

### Improvements

* (x/bank) [#24148](https://github.com/cosmos/cosmos-sdk/pull/24148) Improve performance of the `GetAllBalances` and `GetAccountsBalances` keeper methods.

### Bug Fixes

* (x/auth) [#23741](https://github.com/cosmos/cosmos-sdk/pull/23741) Support legacy global AccountNumber.
Expand Down
15 changes: 6 additions & 9 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,34 @@ func (k BaseViewKeeper) HasBalance(ctx context.Context, addr sdk.AccAddress, amt
func (k BaseViewKeeper) GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
balances := sdk.NewCoins()
k.IterateAccountBalances(ctx, addr, func(balance sdk.Coin) bool {
balances = balances.Add(balance)
balances = append(balances, balance)
return false
})

return balances.Sort()
return balances
}

// GetAccountsBalances returns all the accounts balances from the store.
func (k BaseViewKeeper) GetAccountsBalances(ctx context.Context) []types.Balance {
balances := make([]types.Balance, 0)
mapAddressToBalancesIdx := make(map[string]int)

k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
addrStr, err := k.addrCdc.BytesToString(addr)
if err != nil {
panic(err)
}
idx, ok := mapAddressToBalancesIdx[addrStr]
if ok {
// address is already on the set of accounts balances
balances[idx].Coins = balances[idx].Coins.Add(balance)
balances[idx].Coins.Sort()
if len(balances) > 0 && balances[len(balances)-1].Address == addrStr {
// Same address as last entry = add the coin to it.
balances[len(balances)-1].Coins = append(balances[len(balances)-1].Coins, balance)
return false
}

// New address = new entry.
accountBalance := types.Balance{
Address: addrStr,
Coins: sdk.NewCoins(balance),
}
balances = append(balances, accountBalance)
mapAddressToBalancesIdx[addrStr] = len(balances) - 1
return false
})

Expand Down
Loading