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 6 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 @@ -170,6 +170,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
ibc-denom.
* [\#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`.
* [#9790](https://github.com/cosmos/cosmos-sdk/pull/10687) Fix behavior of `DecCoins.MulDecTruncate`.

### State Machine Breaking

Expand Down
5 changes: 4 additions & 1 deletion types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,11 @@ func (coins DecCoins) MulDec(d Dec) DecCoins {
//
// CONTRACT: No zero coins will be returned.
func (coins DecCoins) MulDecTruncate(d Dec) DecCoins {
var res DecCoins
if d.IsZero() {
panic("invalid zero decimal")
}

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(nil), true},

{"Multiple coins - positive multiplier", sdk.DecCoins{
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
Expand Down