Skip to content

Commit

Permalink
Use correct bit length constant for sdk.Dec (#11332)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <marbar3778@yahoo.com>
(cherry picked from commit 3d3cf25)

# Conflicts:
#	go.sum
  • Loading branch information
julienrbrt authored and mergify-bot committed Mar 8, 2022
1 parent a0951a6 commit 279a1e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,13 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
<<<<<<< HEAD
github.com/zondax/hid v0.9.0 h1:eiT3P6vNxAEVxXMw66eZUAAnU2zD33JBkfG/EnfAKl8=
=======
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
>>>>>>> 3d3cf2554 (Use correct bit length constant for sdk.Dec (#11332))
github.com/zondax/hid v0.9.0/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
8 changes: 4 additions & 4 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
32 changes: 29 additions & 3 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 279a1e8

Please sign in to comment.