Skip to content
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased Provenance]

* nothing
### Improvments

* [#616](https://github.com/provenance-io/cosmos-sdk/pull/616) Provenance: Improve performance of the `GetAllBalances` and `GetAccountsBalances` keeper methods.

---

Expand Down
19 changes: 9 additions & 10 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,33 @@ func (k BaseViewKeeper) Logger() log.Logger {
// GetAllBalances returns all the account balances for the given account address.
func (k BaseViewKeeper) GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
balances := sdk.NewCoins()
// Prov customization can be removed with a version that has https://github.com/cosmos/cosmos-sdk/pull/24148.
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 {
idx, ok := mapAddressToBalancesIdx[addr.String()]
if ok {
// address is already on the set of accounts balances
balances[idx].Coins = balances[idx].Coins.Add(balance)
balances[idx].Coins.Sort()
addrStr := addr.String()
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: addr.String(),
Address: addrStr,
Coins: sdk.NewCoins(balance),
}
balances = append(balances, accountBalance)
mapAddressToBalancesIdx[addr.String()] = len(balances) - 1
return false
})

Expand Down