fix: decrement types.Dec max length to keep decimal bits in DecimalPrecisionBits (backport #11772) (#11805)
This commit is contained in:
parent
c405df4567
commit
ad4ad43f02
|
@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* [\#11772](https://github.com/cosmos/cosmos-sdk/pull/11772) Limit types.Dec length to avoid overflow.
|
||||
|
||||
## [v0.45.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.4) - 2022-04-25
|
||||
|
||||
### Bug Fixes
|
||||
|
|
|
@ -751,6 +751,7 @@ func (s *coinTestSuite) TestParseCoins() {
|
|||
{"10atom10", true, sdk.Coins{{"atom10", sdk.NewInt(10)}}},
|
||||
{"200transfer/channelToA/uatom", true, sdk.Coins{{"transfer/channelToA/uatom", sdk.NewInt(200)}}},
|
||||
{"50ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", true, sdk.Coins{{"ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", sdk.NewInt(50)}}},
|
||||
{"120000000000000000000000000000000000000000000000000000000000000000000000000000btc", false, nil},
|
||||
}
|
||||
|
||||
for tcIndex, tc := range cases {
|
||||
|
|
|
@ -22,11 +22,16 @@ const (
|
|||
// number of decimal places
|
||||
Precision = 18
|
||||
|
||||
// bytes required to represent the above precision
|
||||
// Ceiling[Log2[999 999 999 999 999 999]]
|
||||
// bits required to represent the above precision
|
||||
// Ceiling[Log2[10^Precision - 1]]
|
||||
DecimalPrecisionBits = 60
|
||||
|
||||
maxDecBitLen = maxBitLen + DecimalPrecisionBits
|
||||
// decimalTruncateBits is the minimum number of bits removed
|
||||
// by a truncate operation. It is equal to
|
||||
// Floor[Log2[10^Precision - 1]].
|
||||
decimalTruncateBits = DecimalPrecisionBits - 1
|
||||
|
||||
maxDecBitLen = maxBitLen + decimalTruncateBits
|
||||
|
||||
// max number of iterations in ApproxRoot function
|
||||
maxApproxRootIterations = 100
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
@ -33,10 +34,10 @@ func (s *decimalTestSuite) TestNewDecFromStr() {
|
|||
largeBigInt, ok := new(big.Int).SetString("3144605511029693144278234343371835", 10)
|
||||
s.Require().True(ok)
|
||||
|
||||
largerBigInt, ok := new(big.Int).SetString("88888888888888888888888888888888888888888888888888888888888888888888844444440", 10)
|
||||
largerBigInt, ok := new(big.Int).SetString("8888888888888888888888888888888888888888888888888888888888888888888844444440", 10)
|
||||
s.Require().True(ok)
|
||||
|
||||
largestBigInt, ok := new(big.Int).SetString("133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
largestBigInt, ok := new(big.Int).SetString("33499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
s.Require().True(ok)
|
||||
|
||||
tests := []struct {
|
||||
|
@ -63,8 +64,8 @@ func (s *decimalTestSuite) TestNewDecFromStr() {
|
|||
{"foobar", true, sdk.Dec{}},
|
||||
{"0.foobar", true, sdk.Dec{}},
|
||||
{"0.foobar.", true, sdk.Dec{}},
|
||||
{"88888888888888888888888888888888888888888888888888888888888888888888844444440", false, sdk.NewDecFromBigInt(largerBigInt)},
|
||||
{"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535", false, sdk.NewDecFromBigIntWithPrec(largestBigInt, 18)},
|
||||
{"8888888888888888888888888888888888888888888888888888888888888888888844444440", false, sdk.NewDecFromBigInt(largerBigInt)},
|
||||
{"33499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535", false, sdk.NewDecFromBigIntWithPrec(largestBigInt, 18)},
|
||||
{"133499189745056880149688856635597007162669032647290798121690100488888732861291", true, sdk.Dec{}},
|
||||
}
|
||||
|
||||
|
@ -449,10 +450,14 @@ func (s *decimalTestSuite) TestDecSortableBytes() {
|
|||
}
|
||||
|
||||
func (s *decimalTestSuite) TestDecEncoding() {
|
||||
largestBigInt, ok := new(big.Int).SetString("133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
largestBigInt, ok := new(big.Int).SetString("33499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
s.Require().True(ok)
|
||||
|
||||
smallestBigInt, ok := new(big.Int).SetString("-133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
smallestBigInt, ok := new(big.Int).SetString("-33499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535", 10)
|
||||
s.Require().True(ok)
|
||||
|
||||
const maxDecBitLen = 315
|
||||
maxInt, ok := new(big.Int).SetString(strings.Repeat("1", maxDecBitLen), 2)
|
||||
s.Require().True(ok)
|
||||
|
||||
testCases := []struct {
|
||||
|
@ -492,15 +497,21 @@ func (s *decimalTestSuite) TestDecEncoding() {
|
|||
},
|
||||
{
|
||||
sdk.NewDecFromBigIntWithPrec(largestBigInt, 18),
|
||||
"313333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335",
|
||||
"\"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"",
|
||||
"\"133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n",
|
||||
"3333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335",
|
||||
"\"33499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"",
|
||||
"\"33499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n",
|
||||
},
|
||||
{
|
||||
sdk.NewDecFromBigIntWithPrec(smallestBigInt, 18),
|
||||
"2D313333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335",
|
||||
"\"-133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"",
|
||||
"\"-133499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n",
|
||||
"2D3333343939313839373435303536383830313439363838383536363335353937303037313632363639303332363437323930373938313231363930313030343838383838373332383631323930303334333736343335313330343333353335",
|
||||
"\"-33499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"",
|
||||
"\"-33499189745056880149688856635597007162669032647290798121690100488888732861290.034376435130433535\"\n",
|
||||
},
|
||||
{
|
||||
sdk.NewDecFromBigIntWithPrec(maxInt, 18),
|
||||
"3636373439353934383732353238343430303734383434343238333137373938353033353831333334353136333233363435333939303630383435303530323434343434333636343330363435303137313838323137353635323136373637",
|
||||
"\"66749594872528440074844428317798503581334516323645399060845050244444366430645.017188217565216767\"",
|
||||
"\"66749594872528440074844428317798503581334516323645399060845050244444366430645.017188217565216767\"\n",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue