From 78eb351cf87d6b52ca2a2da7df8b004be48ff3e5 Mon Sep 17 00:00:00 2001 From: evancholmberg Date: Sun, 5 Dec 2021 18:34:31 -0500 Subject: [PATCH 1/6] Fix behavior of MulDecTruncate() --- types/dec_coin.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/dec_coin.go b/types/dec_coin.go index 74a5a1ae44f5..7139963cb647 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -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, From 573c8f44c488d7d3fb36d443af1c303a5f40adc1 Mon Sep 17 00:00:00 2001 From: evancholmberg Date: Mon, 6 Dec 2021 17:58:36 -0500 Subject: [PATCH 2/6] Update test to expect panic when multiplier d is 0 --- types/dec_coin_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index 2f35075d3c2e..ea55e2dec77d 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -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)}, From 2b5cc6eb430e7359f91e4e34f39cad88d92a6fee Mon Sep 17 00:00:00 2001 From: evancholmberg Date: Mon, 6 Dec 2021 19:00:04 -0500 Subject: [PATCH 3/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef67961d2a50..068a5a53fd98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10394](https://github.com/cosmos/cosmos-sdk/issues/10394) Fixes issue related to grpc-gateway of account balance by ibc-denom. * [#10593](https://github.com/cosmos/cosmos-sdk/pull/10593) Update swagger-ui to v4.1.0 to fix xss vulnerability. +* [#9790](https://github.com/cosmos/cosmos-sdk/pull/10687) Fix behavior of `DecCoins.MulDecTruncate`. ### State Machine Breaking From c01dfb432fd9bddb205caa0426ddf142a3f1a7b6 Mon Sep 17 00:00:00 2001 From: evancholmberg <49920219+evancholmberg@users.noreply.github.com> Date: Mon, 6 Dec 2021 19:07:56 -0500 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8cfff0e20d5..8ada0696a762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -167,6 +167,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10466](https://github.com/cosmos/cosmos-sdk/issues/10466) Fixes error with simulation tests when genesis start time is randomly created after the year 2262 * [\#10394](https://github.com/cosmos/cosmos-sdk/issues/10394) Fixes issue related to grpc-gateway of account balance by 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`. From a31d3eecb6a2c107001326d299c483ca9d78e556 Mon Sep 17 00:00:00 2001 From: evancholmberg Date: Wed, 15 Dec 2021 21:37:33 -0500 Subject: [PATCH 5/6] Return `nil `DecCoins` when `d` is zero --- types/dec_coin.go | 4 ++-- types/dec_coin_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/types/dec_coin.go b/types/dec_coin.go index 7139963cb647..bb32ef653a11 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -373,12 +373,12 @@ 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 { if d.IsZero() { - panic("invalid zero decimal") + return DecCoins{} } var res DecCoins diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index ea55e2dec77d..52f50d24fc22 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -883,7 +883,7 @@ func (s *decCoinTestSuite) TestDecCoins_MulDecTruncate() { {"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), true}, + }, sdk.NewDec(0), sdk.DecCoins{}, false}, {"Multiple coins - positive multiplier", sdk.DecCoins{ sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)}, From 62f3061820149db39537c1a6e598d50a5f0fcabf Mon Sep 17 00:00:00 2001 From: evancholmberg <49920219+evancholmberg@users.noreply.github.com> Date: Tue, 11 Jan 2022 17:33:43 -0500 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e17500879eb..54a6b6c18a68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -191,7 +191,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#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 * [\#10564](https://github.com/cosmos/cosmos-sdk/pull/10564) Fix bug when updating allowance inside AllowedMsgAllowance