Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add AmountOfNoValidation for DecCoins #18440

Merged
merged 4 commits into from
Nov 10, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (types) [#18440](https://github.com/cosmos/cosmos-sdk/pull/18440) Add `AmountOfNoValidation` to `sdk.DecCoins`.
* (x/gov) [#18428](https://github.com/cosmos/cosmos-sdk/pull/18428) Extend governance config
* (x/gov) [#18189](https://github.com/cosmos/cosmos-sdk/pull/18189) Limit the accepted deposit coins for a proposal to the minimum proposal deposit denoms.
* (x/gov) [#18025](https://github.com/cosmos/cosmos-sdk/pull/18025) Improve `<appd> q gov proposer` by querying directly a proposal instead of tx events. It is an alias of `q gov proposal` as the proposer is a field of the proposal.
Expand Down
12 changes: 9 additions & 3 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,16 @@ func (coins DecCoins) Empty() bool {
return len(coins) == 0
}

// AmountOf returns the amount of a denom from deccoins
// AmountOf returns the amount of a denom from deccoins. It panics if the denom
// is invalid.
func (coins DecCoins) AmountOf(denom string) math.LegacyDec {
mustValidateDenom(denom)
return coins.AmountOfNoValidation(denom)
}

// AmountOfNoValidation returns the amount of a denom from deccoins without checking
// the correctness of the denom.
func (coins DecCoins) AmountOfNoValidation(denom string) math.LegacyDec {
switch len(coins) {
case 0:
return math.LegacyZeroDec()
Expand All @@ -472,11 +478,11 @@ func (coins DecCoins) AmountOf(denom string) math.LegacyDec {

switch {
case denom < coin.Denom:
return coins[:midIdx].AmountOf(denom)
return coins[:midIdx].AmountOfNoValidation(denom)
case denom == coin.Denom:
return coin.Amount
default:
return coins[midIdx+1:].AmountOf(denom)
return coins[midIdx+1:].AmountOfNoValidation(denom)
}
}
}
Expand Down