Skip to content

Commit e82620f

Browse files
Merge pull request tronprotocol#6098 from halibobo1205/477/Math_to_strictMath_for_pow
feat(math): migrate `pow` operation from java.lang.Math to java.lang.StrictMath
2 parents 4a28e3d + 3278ced commit e82620f

File tree

25 files changed

+224
-526
lines changed

25 files changed

+224
-526
lines changed

actuator/src/main/java/org/tron/core/actuator/ExchangeTransactionActuator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public boolean execute(Object object) throws ContractExeException {
6666
long tokenQuant = exchangeTransactionContract.getQuant();
6767

6868
byte[] anotherTokenID;
69-
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant);
69+
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant,
70+
dynamicStore.allowStrictMath());
7071

7172
if (Arrays.equals(tokenID, firstTokenID)) {
7273
anotherTokenID = secondTokenID;
@@ -205,7 +206,8 @@ public boolean validate() throws ContractValidateException {
205206
}
206207
}
207208

208-
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant);
209+
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant,
210+
dynamicStore.allowStrictMath());
209211
if (anotherTokenQuant < tokenExpected) {
210212
throw new ContractValidateException("token required must greater than expected");
211213
}

actuator/src/main/java/org/tron/core/utils/ProposalUtil.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
779779
}
780780
break;
781781
}
782+
case ALLOW_STRICT_MATH: {
783+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_7)) {
784+
throw new ContractValidateException(
785+
"Bad chain parameter id [ALLOW_STRICT_MATH]");
786+
}
787+
if (dynamicPropertiesStore.allowStrictMath()) {
788+
throw new ContractValidateException(
789+
"[ALLOW_STRICT_MATH] has been valid, no need to propose again");
790+
}
791+
if (value != 1) {
792+
throw new ContractValidateException(
793+
"This value[ALLOW_STRICT_MATH] is only allowed to be 1");
794+
}
795+
break;
796+
}
782797
default:
783798
break;
784799
}
@@ -857,7 +872,8 @@ public enum ProposalType { // current value, value range
857872
MAX_DELEGATE_LOCK_PERIOD(78), // (86400, 10512000]
858873
ALLOW_OLD_REWARD_OPT(79), // 0, 1
859874
ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1
860-
MAX_CREATE_ACCOUNT_TX_SIZE(82); // [500, 10000]
875+
MAX_CREATE_ACCOUNT_TX_SIZE(82), // [500, 10000]
876+
ALLOW_STRICT_MATH(87); // 0, 1
861877

862878
private long code;
863879

actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static void load(StoreFactory storeFactory) {
4040
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
4141
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
4242
VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment());
43+
VMConfig.initAllowStrictMath(ds.getAllowStrictMath());
4344
}
4445
}
4546
}

actuator/src/main/java/org/tron/core/vm/config/VMConfig.java

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class VMConfig {
5151

5252
private static boolean ALLOW_ENERGY_ADJUSTMENT = false;
5353

54+
private static boolean ALLOW_STRICT_MATH = false;
55+
5456
private VMConfig() {
5557
}
5658

@@ -142,6 +144,10 @@ public static void initAllowEnergyAdjustment(long allow) {
142144
ALLOW_ENERGY_ADJUSTMENT = allow == 1;
143145
}
144146

147+
public static void initAllowStrictMath(long allow) {
148+
ALLOW_STRICT_MATH = allow == 1;
149+
}
150+
145151
public static boolean getEnergyLimitHardFork() {
146152
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
147153
}
@@ -221,4 +227,8 @@ public static boolean allowTvmShanghai() {
221227
public static boolean allowEnergyAdjustment() {
222228
return ALLOW_ENERGY_ADJUSTMENT;
223229
}
230+
231+
public static boolean allowStrictMath() {
232+
return ALLOW_STRICT_MATH;
233+
}
224234
}

actuator/src/main/java/org/tron/core/vm/program/Program.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,8 @@ public long updateContextContractFactor() {
22302230
contractState.getDynamicPropertiesStore().getCurrentCycleNumber(),
22312231
VMConfig.getDynamicEnergyThreshold(),
22322232
VMConfig.getDynamicEnergyIncreaseFactor(),
2233-
VMConfig.getDynamicEnergyMaxFactor())) {
2233+
VMConfig.getDynamicEnergyMaxFactor(),
2234+
VMConfig.allowStrictMath())) {
22342235
contractState.updateContractState(getContextAddress(), contractStateCapsule
22352236
);
22362237
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.tron.common.math;
2+
3+
/**
4+
* This class is deprecated and should not be used in new code,
5+
* for cross-platform consistency, please use {@link StrictMathWrapper} instead,
6+
* especially for floating-point calculations.
7+
*/
8+
@Deprecated
9+
public class Maths {
10+
11+
/**
12+
* Returns the value of the first argument raised to the power of the second argument.
13+
* @param a the base.
14+
* @param b the exponent.
15+
* @return the value {@code a}<sup>{@code b}</sup>.
16+
*/
17+
public static double pow(double a, double b, boolean useStrictMath) {
18+
return useStrictMath ? StrictMathWrapper.pow(a, b) : MathWrapper.pow(a, b);
19+
}
20+
}

chainbase/src/main/java/org/tron/core/capsule/ContractStateCapsule.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.google.protobuf.InvalidProtocolBufferException;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.tron.common.math.Maths;
89
import org.tron.core.store.DynamicPropertiesStore;
910
import org.tron.protos.contract.SmartContractOuterClass;
1011
import org.tron.protos.contract.SmartContractOuterClass.ContractState;
@@ -77,12 +78,13 @@ public boolean catchUpToCycle(DynamicPropertiesStore dps) {
7778
dps.getCurrentCycleNumber(),
7879
dps.getDynamicEnergyThreshold(),
7980
dps.getDynamicEnergyIncreaseFactor(),
80-
dps.getDynamicEnergyMaxFactor()
81+
dps.getDynamicEnergyMaxFactor(),
82+
dps.allowStrictMath()
8183
);
8284
}
8385

8486
public boolean catchUpToCycle(
85-
long newCycle, long threshold, long increaseFactor, long maxFactor
87+
long newCycle, long threshold, long increaseFactor, long maxFactor, boolean useStrictMath
8688
) {
8789
long lastCycle = getUpdateCycle();
8890

@@ -119,9 +121,9 @@ public boolean catchUpToCycle(
119121
}
120122

121123
// Calc the decrease percent (decrease factor [75% ~ 100%])
122-
double decreasePercent = Math.pow(
124+
double decreasePercent = Maths.pow(
123125
1 - (double) increaseFactor / DYNAMIC_ENERGY_DECREASE_DIVISION / precisionFactor,
124-
cycleCount
126+
cycleCount, useStrictMath
125127
);
126128

127129
// Decrease to this cycle

chainbase/src/main/java/org/tron/core/capsule/ExchangeCapsule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ public byte[] createDbKey() {
112112
return calculateDbKey(getID());
113113
}
114114

115-
public long transaction(byte[] sellTokenID, long sellTokenQuant) {
115+
public long transaction(byte[] sellTokenID, long sellTokenQuant, boolean useStrictMath) {
116116
long supply = 1_000_000_000_000_000_000L;
117-
ExchangeProcessor processor = new ExchangeProcessor(supply);
117+
ExchangeProcessor processor = new ExchangeProcessor(supply, useStrictMath);
118118

119119
long buyTokenQuant = 0;
120120
long firstTokenBalance = this.exchange.getFirstTokenBalance();

chainbase/src/main/java/org/tron/core/capsule/ExchangeProcessor.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package org.tron.core.capsule;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.tron.common.math.Maths;
45

56
@Slf4j(topic = "capsule")
67
public class ExchangeProcessor {
78

89
private long supply;
10+
private final boolean useStrictMath;
911

10-
public ExchangeProcessor(long supply) {
12+
public ExchangeProcessor(long supply, boolean useStrictMath) {
1113
this.supply = supply;
14+
this.useStrictMath = useStrictMath;
1215
}
1316

1417
private long exchangeToSupply(long balance, long quant) {
1518
logger.debug("balance: " + balance);
1619
long newBalance = balance + quant;
1720
logger.debug("balance + quant: " + newBalance);
1821

19-
double issuedSupply = -supply * (1.0 - Math.pow(1.0 + (double) quant / newBalance, 0.0005));
22+
double issuedSupply = -supply * (1.0
23+
- Maths.pow(1.0 + (double) quant / newBalance, 0.0005, this.useStrictMath));
2024
logger.debug("issuedSupply: " + issuedSupply);
2125
long out = (long) issuedSupply;
2226
supply += out;
@@ -27,8 +31,8 @@ private long exchangeToSupply(long balance, long quant) {
2731
private long exchangeFromSupply(long balance, long supplyQuant) {
2832
supply -= supplyQuant;
2933

30-
double exchangeBalance =
31-
balance * (Math.pow(1.0 + (double) supplyQuant / supply, 2000.0) - 1.0);
34+
double exchangeBalance = balance
35+
* (Maths.pow(1.0 + (double) supplyQuant / supply, 2000.0, this.useStrictMath) - 1.0);
3236
logger.debug("exchangeBalance: " + exchangeBalance);
3337

3438
return (long) exchangeBalance;

0 commit comments

Comments
 (0)