From 11bda928d8151e995c46d299274bc18db80b5c40 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 9 Mar 2022 13:26:35 +0100 Subject: [PATCH] fix(types): use correct bit length constant for sdk.Dec (backport #11332) (#11335) * Use correct bit length constant for sdk.Dec (#11332) Co-authored-by: Marko (cherry picked from commit 3d3cf25546bb851e38c49857ad2ae2db96ac69d0) # Conflicts: # go.sum * fix conflicts Co-authored-by: Julien Robert Co-authored-by: marbar3778 --- types/decimal.go | 8 ++++---- types/decimal_test.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index a6d286896..467a50335 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -179,8 +179,8 @@ func NewDecFromStr(str string) (Dec, error) { if !ok { return Dec{}, fmt.Errorf("failed to set decimal string: %s", combinedStr) } - if combined.BitLen() > maxBitLen { - return Dec{}, fmt.Errorf("decimal out of range; bitLen: got %d, max %d", combined.BitLen(), maxBitLen) + if combined.BitLen() > maxDecBitLen { + return Dec{}, fmt.Errorf("decimal out of range; bitLen: got %d, max %d", combined.BitLen(), maxDecBitLen) } if neg { combined = new(big.Int).Neg(combined) @@ -744,8 +744,8 @@ func (d *Dec) Unmarshal(data []byte) error { return err } - if d.i.BitLen() > maxBitLen { - return fmt.Errorf("decimal out of range; got: %d, max: %d", d.i.BitLen(), maxBitLen) + if d.i.BitLen() > maxDecBitLen { + return fmt.Errorf("decimal out of range; got: %d, max: %d", d.i.BitLen(), maxDecBitLen) } return nil diff --git a/types/decimal_test.go b/types/decimal_test.go index 16a1561cb..dd703ceec 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -30,8 +30,14 @@ func (s *decimalTestSuite) mustNewDecFromStr(str string) (d sdk.Dec) { } func (s *decimalTestSuite) TestNewDecFromStr() { - largeBigInt, success := new(big.Int).SetString("3144605511029693144278234343371835", 10) - s.Require().True(success) + largeBigInt, ok := new(big.Int).SetString("3144605511029693144278234343371835", 10) + s.Require().True(ok) + + largerBigInt, ok := new(big.Int).SetString("88888888888888888888888888888888888888888888888888888888888888888888844444440", 10) + s.Require().True(ok) + + largestBigInt, ok := new(big.Int).SetString("133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10) + s.Require().True(ok) tests := []struct { decimalStr string @@ -57,7 +63,9 @@ func (s *decimalTestSuite) TestNewDecFromStr() { {"foobar", true, sdk.Dec{}}, {"0.foobar", true, sdk.Dec{}}, {"0.foobar.", true, sdk.Dec{}}, - {"88888888888888888888888888888888888888888888888888888888888888888888844444440", true, sdk.Dec{}}, + {"88888888888888888888888888888888888888888888888888888888888888888888844444440", false, sdk.NewDecFromBigInt(largerBigInt)}, + {"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535", false, sdk.NewDecFromBigIntWithPrec(largestBigInt, 18)}, + {"133499189745056880149688856635597007162669032647290798121690100488888732861291", true, sdk.Dec{}}, } for tcIndex, tc := range tests { @@ -441,6 +449,12 @@ func (s *decimalTestSuite) TestDecSortableBytes() { } func (s *decimalTestSuite) TestDecEncoding() { + largestBigInt, ok := new(big.Int).SetString("133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10) + s.Require().True(ok) + + smallestBigInt, ok := new(big.Int).SetString("-133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10) + s.Require().True(ok) + testCases := []struct { input sdk.Dec rawBz string @@ -476,6 +490,18 @@ func (s *decimalTestSuite) TestDecEncoding() { "\"-1.414213562373095049\"", "\"-1.414213562373095049\"\n", }, + { + sdk.NewDecFromBigIntWithPrec(largestBigInt, 18), + "313333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335", + "\"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"", + "\"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n", + }, + { + sdk.NewDecFromBigIntWithPrec(smallestBigInt, 18), + "2D313333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335", + "\"-133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"", + "\"-133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n", + }, } for _, tc := range testCases {