Skip to content

Commit

Permalink
perf: add AmountOfNoValidation for DecCoins (#18440)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Nov 10, 2023
1 parent 89296cc commit b7fb292
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit b7fb292

Please sign in to comment.