From efe41772608304b4a3831b9025731343a3da10fd Mon Sep 17 00:00:00 2001 From: Venkatesh Mankena Date: Mon, 14 Jun 2021 10:28:47 +0530 Subject: [PATCH 1/5] Handle MAX_INT_256 Change maxBitLen of sdk.Int to handle max Erc20 value. --- types/int.go | 6 +++--- types/int_internal_test.go | 20 +++++++++++++++++++- types/int_test.go | 24 ++++++++++++------------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/types/int.go b/types/int.go index 1a013bc0549e..0708cda58112 100644 --- a/types/int.go +++ b/types/int.go @@ -9,7 +9,7 @@ import ( "math/big" ) -const maxBitLen = 255 +const maxBitLen = 256 func newIntegerFromString(s string) (*big.Int, bool) { return new(big.Int).SetString(s, 0) @@ -69,9 +69,9 @@ func unmarshalText(i *big.Int, text string) error { var _ CustomProtobufType = (*Int)(nil) -// Int wraps big.Int with a 256 bit range bound +// Int wraps big.Int with a 257 bit range bound // Checks overflow, underflow and division by zero -// Exists in range from -(2^255 - 1) to 2^255 - 1 +// Exists in range from -(2^256 - 1) to 2^256 - 1 type Int struct { i *big.Int } diff --git a/types/int_internal_test.go b/types/int_internal_test.go index c6b039948e69..77d357bbf750 100644 --- a/types/int_internal_test.go +++ b/types/int_internal_test.go @@ -63,7 +63,7 @@ func (s *internalIntTestSuite) TestEncodingRandom() { } func (s *internalIntTestSuite) TestSerializationOverflow() { - bx, _ := new(big.Int).SetString("91888242871839275229946405745257275988696311157297823662689937894645226298583", 10) + bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639936", 10) x := Int{bx} y := new(Int) @@ -80,6 +80,24 @@ func (s *internalIntTestSuite) TestSerializationOverflow() { s.Require().Error(y.UnmarshalJSON(bz)) } +func (s *internalIntTestSuite) TestDeserializeMaxERC20() { + bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639935", 10) + x := Int{bx} + y := new(Int) + + bz, err := x.Marshal() + s.Require().NoError(err) + + // require deserialization to be successful + s.Require().NoError(y.Unmarshal(bz)) + + // require JSON deserialization to succeed + bz, err = x.MarshalJSON() + s.Require().NoError(err) + + s.Require().NoError(y.UnmarshalJSON(bz)) +} + func (s *internalIntTestSuite) TestImmutabilityArithInt() { size := 500 diff --git a/types/int_test.go b/types/int_test.go index e992d67566f9..359c4859b6dc 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -40,16 +40,16 @@ func (s *intTestSuite) TestFromUint64() { } func (s *intTestSuite) TestIntPanic() { - // Max Int = 2^255-1 = 5.789e+76 - // Min Int = -(2^255-1) = -5.789e+76 - s.Require().NotPanics(func() { sdk.NewIntWithDecimal(1, 76) }) - i1 := sdk.NewIntWithDecimal(1, 76) - s.Require().NotPanics(func() { sdk.NewIntWithDecimal(2, 76) }) - i2 := sdk.NewIntWithDecimal(2, 76) - s.Require().NotPanics(func() { sdk.NewIntWithDecimal(3, 76) }) - i3 := sdk.NewIntWithDecimal(3, 76) - - s.Require().Panics(func() { sdk.NewIntWithDecimal(6, 76) }) + // Max Int = 2^256-1 = 1.1579209e+77 + // Min Int = -(2^256-1) = -1.1579209e+77 + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(4, 76) }) + i1 := sdk.NewIntWithDecimal(4, 76) + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(5, 76) }) + i2 := sdk.NewIntWithDecimal(5, 76) + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(6, 76) }) + i3 := sdk.NewIntWithDecimal(6, 76) + + s.Require().Panics(func() { sdk.NewIntWithDecimal(2, 77) }) s.Require().Panics(func() { sdk.NewIntWithDecimal(9, 80) }) // Overflow check @@ -69,7 +69,7 @@ func (s *intTestSuite) TestIntPanic() { s.Require().Panics(func() { i2.Neg().Mul(i2.Neg()) }) s.Require().Panics(func() { i3.Neg().Mul(i3.Neg()) }) - // Underflow check + // // Underflow check i3n := i3.Neg() s.Require().NotPanics(func() { i3n.Sub(i1) }) s.Require().NotPanics(func() { i3n.Sub(i2) }) @@ -84,7 +84,7 @@ func (s *intTestSuite) TestIntPanic() { s.Require().Panics(func() { i3.Mul(i3.Neg()) }) // Bound check - intmax := sdk.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1))) + intmax := sdk.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))) intmin := intmax.Neg() s.Require().NotPanics(func() { intmax.Add(sdk.ZeroInt()) }) s.Require().NotPanics(func() { intmin.Sub(sdk.ZeroInt()) }) From adf73cad5f25c7576a82c20a85c4176073501a20 Mon Sep 17 00:00:00 2001 From: Venkatesh Mankena Date: Mon, 14 Jun 2021 11:14:22 +0530 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63e42a722333..e388eb8c7637 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,6 +152,7 @@ if input key is empty, or input data contains empty key. * (x/bank) [\#8434](https://github.com/cosmos/cosmos-sdk/pull/8434) Fix legacy REST API `GET /bank/total` and `GET /bank/total/{denom}` in swagger * (x/slashing) [\#8427](https://github.com/cosmos/cosmos-sdk/pull/8427) Fix query signing infos command * (x/bank) [\#9229](https://github.com/cosmos/cosmos-sdk/pull/9229) Now zero coin balances cannot be added to balances & supply stores. If any denom becomes zero corresponding key gets deleted from store. +* (sdk types) [\#9511](https://github.com/cosmos/cosmos-sdk/pull/9511) Change maxBitLen of sdk.Int to handle max Erc20 value. ### Deprecated From 94b91e5cdaa5dad4a5027e8dc5a6f35968852fcb Mon Sep 17 00:00:00 2001 From: Venkatesh Mankena Date: Mon, 14 Jun 2021 13:56:47 +0530 Subject: [PATCH 3/5] Use maxBitLen instead of hardcoded value in sdk.Dec overflow checks. --- types/decimal.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 47de0618bc83..40fd7ea320f3 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -222,7 +222,7 @@ func (d Dec) BigInt() *big.Int { func (d Dec) Add(d2 Dec) Dec { res := new(big.Int).Add(d.i, d2.i) - if res.BitLen() > 255+DecimalPrecisionBits { + if res.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{res} @@ -232,7 +232,7 @@ func (d Dec) Add(d2 Dec) Dec { func (d Dec) Sub(d2 Dec) Dec { res := new(big.Int).Sub(d.i, d2.i) - if res.BitLen() > 255+DecimalPrecisionBits { + if res.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{res} @@ -243,7 +243,7 @@ func (d Dec) Mul(d2 Dec) Dec { mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndRound(mul) - if chopped.BitLen() > 255+DecimalPrecisionBits { + if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{chopped} @@ -254,7 +254,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec { mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndTruncate(mul) - if chopped.BitLen() > 255+DecimalPrecisionBits { + if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{chopped} @@ -264,7 +264,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec { func (d Dec) MulInt(i Int) Dec { mul := new(big.Int).Mul(d.i, i.i) - if mul.BitLen() > 255+DecimalPrecisionBits { + if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{mul} @@ -274,7 +274,7 @@ func (d Dec) MulInt(i Int) Dec { func (d Dec) MulInt64(i int64) Dec { mul := new(big.Int).Mul(d.i, big.NewInt(i)) - if mul.BitLen() > 255+DecimalPrecisionBits { + if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{mul} @@ -289,7 +289,7 @@ func (d Dec) Quo(d2 Dec) Dec { quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRound(quo) - if chopped.BitLen() > 255+DecimalPrecisionBits { + if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{chopped} @@ -304,7 +304,7 @@ func (d Dec) QuoTruncate(d2 Dec) Dec { quo := mul.Quo(mul, d2.i) chopped := chopPrecisionAndTruncate(quo) - if chopped.BitLen() > 255+DecimalPrecisionBits { + if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{chopped} @@ -319,7 +319,7 @@ func (d Dec) QuoRoundUp(d2 Dec) Dec { quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRoundUp(quo) - if chopped.BitLen() > 255+DecimalPrecisionBits { + if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { panic("Int overflow") } return Dec{chopped} From 3f284ead8f5e087f8804a3552aa13019d2d5dfb5 Mon Sep 17 00:00:00 2001 From: Albert Chon Date: Mon, 14 Jun 2021 12:47:54 -0400 Subject: [PATCH 4/5] Update CHANGELOG.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e388eb8c7637..8f3dd465086c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,7 +152,7 @@ if input key is empty, or input data contains empty key. * (x/bank) [\#8434](https://github.com/cosmos/cosmos-sdk/pull/8434) Fix legacy REST API `GET /bank/total` and `GET /bank/total/{denom}` in swagger * (x/slashing) [\#8427](https://github.com/cosmos/cosmos-sdk/pull/8427) Fix query signing infos command * (x/bank) [\#9229](https://github.com/cosmos/cosmos-sdk/pull/9229) Now zero coin balances cannot be added to balances & supply stores. If any denom becomes zero corresponding key gets deleted from store. -* (sdk types) [\#9511](https://github.com/cosmos/cosmos-sdk/pull/9511) Change maxBitLen of sdk.Int to handle max Erc20 value. +* (types) [\#9511](https://github.com/cosmos/cosmos-sdk/pull/9511) Change `maxBitLen` of `sdk.Int` and `sdk.Dec` to handle max ERC20 value. ### Deprecated From 161c1112496cd81a62895e5a9b1786262b936fbf Mon Sep 17 00:00:00 2001 From: Venkatesh Mankena Date: Wed, 16 Jun 2021 08:54:39 +0530 Subject: [PATCH 5/5] constant for maxDecBitLen --- types/decimal.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 40fd7ea320f3..a6d2868969a1 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -26,6 +26,8 @@ const ( // Ceiling[Log2[999 999 999 999 999 999]] DecimalPrecisionBits = 60 + maxDecBitLen = maxBitLen + DecimalPrecisionBits + // max number of iterations in ApproxRoot function maxApproxRootIterations = 100 ) @@ -222,7 +224,7 @@ func (d Dec) BigInt() *big.Int { func (d Dec) Add(d2 Dec) Dec { res := new(big.Int).Add(d.i, d2.i) - if res.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if res.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{res} @@ -232,7 +234,7 @@ func (d Dec) Add(d2 Dec) Dec { func (d Dec) Sub(d2 Dec) Dec { res := new(big.Int).Sub(d.i, d2.i) - if res.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if res.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{res} @@ -243,7 +245,7 @@ func (d Dec) Mul(d2 Dec) Dec { mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndRound(mul) - if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if chopped.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{chopped} @@ -254,7 +256,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec { mul := new(big.Int).Mul(d.i, d2.i) chopped := chopPrecisionAndTruncate(mul) - if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if chopped.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{chopped} @@ -264,7 +266,7 @@ func (d Dec) MulTruncate(d2 Dec) Dec { func (d Dec) MulInt(i Int) Dec { mul := new(big.Int).Mul(d.i, i.i) - if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if mul.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{mul} @@ -274,7 +276,7 @@ func (d Dec) MulInt(i Int) Dec { func (d Dec) MulInt64(i int64) Dec { mul := new(big.Int).Mul(d.i, big.NewInt(i)) - if mul.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if mul.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{mul} @@ -289,7 +291,7 @@ func (d Dec) Quo(d2 Dec) Dec { quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRound(quo) - if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if chopped.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{chopped} @@ -304,7 +306,7 @@ func (d Dec) QuoTruncate(d2 Dec) Dec { quo := mul.Quo(mul, d2.i) chopped := chopPrecisionAndTruncate(quo) - if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if chopped.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{chopped} @@ -319,7 +321,7 @@ func (d Dec) QuoRoundUp(d2 Dec) Dec { quo := new(big.Int).Quo(mul, d2.i) chopped := chopPrecisionAndRoundUp(quo) - if chopped.BitLen() > (maxBitLen + DecimalPrecisionBits) { + if chopped.BitLen() > maxDecBitLen { panic("Int overflow") } return Dec{chopped}