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

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

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 @@ -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

Expand Down
7 changes: 5 additions & 2 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions types/dec_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down