fix: Make MulDecTruncate() return nil DecCoins when multiplier is 0 (#10687)

## Description
The `DecCoins.MulDecTruncate` function is supposed to panic instead of returning `nil` when multiplier `d` is zero. This fixes that behavior and updates the related test `decCoinTestSuite.TestDecCoins_MulDecTruncate` to expect a panic when multiplier `d` is zero.

Closes: #9790 

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
evancholmberg 2022-01-12 13:26:09 -05:00 committed by GitHub
parent b0190834c3
commit 08db28711b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 5 deletions

View File

@ -189,6 +189,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#10593](https://github.com/cosmos/cosmos-sdk/pull/10593) Update swagger-ui to v4.1.0 to fix xss vulnerability.
* [\#10674](https://github.com/cosmos/cosmos-sdk/pull/10674) Fix issue with `Error.Wrap` and `Error.Wrapf` usage with `errors.Is`.
* [\#10897](https://github.com/cosmos/cosmos-sdk/pull/10897) Fix: set a non-zero value on gas overflow.
* [#9790](https://github.com/cosmos/cosmos-sdk/pull/10687) Fix behavior of `DecCoins.MulDecTruncate`.
### State Machine Breaking

View File

@ -373,12 +373,15 @@ func (coins DecCoins) MulDec(d Dec) DecCoins {
}
// MulDecTruncate multiplies all the decimal coins by a decimal, truncating. It
// panics if d is zero.
// returns nil DecCoins if d is zero.
//
// CONTRACT: No zero coins will be returned.
func (coins DecCoins) MulDecTruncate(d Dec) DecCoins {
var res DecCoins
if d.IsZero() {
return DecCoins{}
}
var res DecCoins
for _, coin := range coins {
product := DecCoin{
Denom: coin.Denom,

View File

@ -880,12 +880,10 @@ func (s *decCoinTestSuite) TestDecCoins_MulDecTruncate() {
}{
{"No Coins", sdk.DecCoins{}, sdk.NewDec(1), sdk.DecCoins(nil), false},
// TODO - Fix test - Function comment documentation for MulDecTruncate says if multiplier d is zero, it should panic.
// However, that is not the observed behaviour. Currently nil is returned.
{"Multiple coins - zero multiplier", sdk.DecCoins{
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(10, 3)},
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(30, 2)},
}, sdk.NewDec(0), sdk.DecCoins(nil), false},
}, sdk.NewDec(0), sdk.DecCoins{}, false},
{"Multiple coins - positive multiplier", sdk.DecCoins{
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},