Skip to content

Commit

Permalink
fix(dec): fix error.
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Jun 16, 2022
1 parent 82b79c0 commit b1096c3
Showing 1 changed file with 15 additions and 7 deletions.
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

0 comments on commit b1096c3

Please sign in to comment.