Skip to content

Commit

Permalink
Merge pull request #6103 from tronprotocol/release_v4.7.7
Browse files Browse the repository at this point in the history
Release v4.7.7 merge to master
  • Loading branch information
CodeNinjaEvan authored Nov 29, 2024
2 parents 4d0ad61 + eafada1 commit f4da4fa
Show file tree
Hide file tree
Showing 39 changed files with 427 additions and 546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public boolean execute(Object object) throws ContractExeException {
long tokenQuant = exchangeTransactionContract.getQuant();

byte[] anotherTokenID;
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant);
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant,
dynamicStore.allowStrictMath());

if (Arrays.equals(tokenID, firstTokenID)) {
anotherTokenID = secondTokenID;
Expand Down Expand Up @@ -205,7 +206,8 @@ public boolean validate() throws ContractValidateException {
}
}

long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant);
long anotherTokenQuant = exchangeCapsule.transaction(tokenID, tokenQuant,
dynamicStore.allowStrictMath());
if (anotherTokenQuant < tokenExpected) {
throw new ContractValidateException("token required must greater than expected");
}
Expand Down
18 changes: 17 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_STRICT_MATH: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_7)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_STRICT_MATH]");
}
if (dynamicPropertiesStore.allowStrictMath()) {
throw new ContractValidateException(
"[ALLOW_STRICT_MATH] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_STRICT_MATH] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -857,7 +872,8 @@ public enum ProposalType { // current value, value range
MAX_DELEGATE_LOCK_PERIOD(78), // (86400, 10512000]
ALLOW_OLD_REWARD_OPT(79), // 0, 1
ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1
MAX_CREATE_ACCOUNT_TX_SIZE(82); // [500, 10000]
MAX_CREATE_ACCOUNT_TX_SIZE(82), // [500, 10000]
ALLOW_STRICT_MATH(87); // 0, 1

private long code;

Expand Down
3 changes: 3 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,9 @@ public static void exeCall(Program program, DataWord adjustedCallEnergy,
PrecompiledContracts.PrecompiledContract contract =
PrecompiledContracts.getContractForAddress(codeAddress);
if (contract != null) {
if (program.isConstantCall()) {
contract = PrecompiledContracts.getOptimizedContractForConstant(contract);
}
program.callToPrecompiledAddress(msg, contract);
} else {
program.callToAddress(msg);
Expand Down
10 changes: 10 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;

import com.google.protobuf.ByteString;

import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
Expand Down Expand Up @@ -194,6 +196,14 @@ public class PrecompiledContracts {
private static final DataWord blake2FAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000020009");

public static PrecompiledContract getOptimizedContractForConstant(PrecompiledContract contract) {
try {
Constructor<?> constructor = contract.getClass().getDeclaredConstructor();
return (PrecompiledContracts.PrecompiledContract) constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static PrecompiledContract getContractForAddress(DataWord address) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment());
VMConfig.initAllowStrictMath(ds.getAllowStrictMath());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/config/VMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class VMConfig {

private static boolean ALLOW_ENERGY_ADJUSTMENT = false;

private static boolean ALLOW_STRICT_MATH = false;

private VMConfig() {
}

Expand Down Expand Up @@ -142,6 +144,10 @@ public static void initAllowEnergyAdjustment(long allow) {
ALLOW_ENERGY_ADJUSTMENT = allow == 1;
}

public static void initAllowStrictMath(long allow) {
ALLOW_STRICT_MATH = allow == 1;
}

public static boolean getEnergyLimitHardFork() {
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
}
Expand Down Expand Up @@ -221,4 +227,8 @@ public static boolean allowTvmShanghai() {
public static boolean allowEnergyAdjustment() {
return ALLOW_ENERGY_ADJUSTMENT;
}

public static boolean allowStrictMath() {
return ALLOW_STRICT_MATH;
}
}
3 changes: 2 additions & 1 deletion actuator/src/main/java/org/tron/core/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,8 @@ public long updateContextContractFactor() {
contractState.getDynamicPropertiesStore().getCurrentCycleNumber(),
VMConfig.getDynamicEnergyThreshold(),
VMConfig.getDynamicEnergyIncreaseFactor(),
VMConfig.getDynamicEnergyMaxFactor())) {
VMConfig.getDynamicEnergyMaxFactor(),
VMConfig.allowStrictMath())) {
contractState.updateContractState(getContextAddress(), contractStateCapsule
);
}
Expand Down
20 changes: 20 additions & 0 deletions chainbase/src/main/java/org/tron/common/math/Maths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.tron.common.math;

/**
* This class is deprecated and should not be used in new code,
* for cross-platform consistency, please use {@link StrictMathWrapper} instead,
* especially for floating-point calculations.
*/
@Deprecated
public class Maths {

/**
* Returns the value of the first argument raised to the power of the second argument.
* @param a the base.
* @param b the exponent.
* @return the value {@code a}<sup>{@code b}</sup>.
*/
public static double pow(double a, double b, boolean useStrictMath) {
return useStrictMath ? StrictMathWrapper.pow(a, b) : MathWrapper.pow(a, b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.math.Maths;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.protos.contract.SmartContractOuterClass;
import org.tron.protos.contract.SmartContractOuterClass.ContractState;
Expand Down Expand Up @@ -77,12 +78,13 @@ public boolean catchUpToCycle(DynamicPropertiesStore dps) {
dps.getCurrentCycleNumber(),
dps.getDynamicEnergyThreshold(),
dps.getDynamicEnergyIncreaseFactor(),
dps.getDynamicEnergyMaxFactor()
dps.getDynamicEnergyMaxFactor(),
dps.allowStrictMath()
);
}

public boolean catchUpToCycle(
long newCycle, long threshold, long increaseFactor, long maxFactor
long newCycle, long threshold, long increaseFactor, long maxFactor, boolean useStrictMath
) {
long lastCycle = getUpdateCycle();

Expand Down Expand Up @@ -119,9 +121,9 @@ public boolean catchUpToCycle(
}

// Calc the decrease percent (decrease factor [75% ~ 100%])
double decreasePercent = Math.pow(
double decreasePercent = Maths.pow(
1 - (double) increaseFactor / DYNAMIC_ENERGY_DECREASE_DIVISION / precisionFactor,
cycleCount
cycleCount, useStrictMath
);

// Decrease to this cycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public byte[] createDbKey() {
return calculateDbKey(getID());
}

public long transaction(byte[] sellTokenID, long sellTokenQuant) {
public long transaction(byte[] sellTokenID, long sellTokenQuant, boolean useStrictMath) {
long supply = 1_000_000_000_000_000_000L;
ExchangeProcessor processor = new ExchangeProcessor(supply);
ExchangeProcessor processor = new ExchangeProcessor(supply, useStrictMath);

long buyTokenQuant = 0;
long firstTokenBalance = this.exchange.getFirstTokenBalance();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package org.tron.core.capsule;

import lombok.extern.slf4j.Slf4j;
import org.tron.common.math.Maths;

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

private long supply;
private final boolean useStrictMath;

public ExchangeProcessor(long supply) {
public ExchangeProcessor(long supply, boolean useStrictMath) {
this.supply = supply;
this.useStrictMath = useStrictMath;
}

private long exchangeToSupply(long balance, long quant) {
logger.debug("balance: " + balance);
long newBalance = balance + quant;
logger.debug("balance + quant: " + newBalance);

double issuedSupply = -supply * (1.0 - Math.pow(1.0 + (double) quant / newBalance, 0.0005));
double issuedSupply = -supply * (1.0
- Maths.pow(1.0 + (double) quant / newBalance, 0.0005, this.useStrictMath));
logger.debug("issuedSupply: " + issuedSupply);
long out = (long) issuedSupply;
supply += out;
Expand All @@ -27,8 +31,8 @@ private long exchangeToSupply(long balance, long quant) {
private long exchangeFromSupply(long balance, long supplyQuant) {
supply -= supplyQuant;

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

return (long) exchangeBalance;
Expand Down
Loading

0 comments on commit f4da4fa

Please sign in to comment.