Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dec): fix error. #4458

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions common/src/main/java/org/tron/common/entity/Dec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
public final class Dec implements Comparable<Dec> {
// number of decimal places
public static final int 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]]
public static final int decimalPrecisionBits = 60;

// bits required to represent the above precision
// Ceiling[Log2[10^Precision - 1]]
private static final int decimalTruncateBits = decimalPrecisionBits - 1;

private static final int maxBitLen = 256;
private static final int maxDecBitLen = maxBitLen + decimalPrecisionBits;
// https://github.com/cosmos/cosmos-sdk/pull/11805
private static final int maxDecBitLen = maxBitLen + decimalTruncateBits;
// max number of iterations in ApproxRoot function
private static final int maxApproxRootIterations = 100;
// https://github.com/cosmos/cosmos-sdk/pull/12229
private static final int maxApproxRootIterations = 300;
private static final BigInteger precisionReuse = BigInteger.TEN.pow(precision);
private static final BigInteger fivePrecision = precisionReuse.divide(BigInteger.valueOf(2));
private static final BigInteger zeroInt = BigInteger.ZERO;
Expand Down Expand Up @@ -137,10 +145,10 @@ public static Dec newDec(String str) {
combinedStr.append('0');
}
BigInteger combined = new BigInteger(combinedStr.toString(), 10); // base 10

if (combined.bitLength() > maxBitLen) {
// https://github.com/cosmos/cosmos-sdk/pull/11332
if (combined.bitLength() > maxDecBitLen) {
throw new RuntimeException(String.format("decimal out of range; bitLen: got %d, max %d",
combined.bitLength(), maxBitLen));
combined.bitLength(), maxDecBitLen));
}
if (neg) {
combined = combined.negate();
Expand Down