Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat/arm64' into jdk/jdk11
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/build.gradle
#	framework/src/main/java/org/tron/core/db/Manager.java
#	plugins/build.gradle
  • Loading branch information
halibobo1205 committed Jun 4, 2024
2 parents e933eff + db56a8e commit a33dada
Show file tree
Hide file tree
Showing 51 changed files with 772 additions and 114 deletions.
8 changes: 8 additions & 0 deletions actuator/src/main/java/org/tron/core/actuator/VMActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.tron.core.utils.TransactionUtil;
import org.tron.core.vm.EnergyCost;
import org.tron.core.vm.LogInfoTriggerParser;
import org.tron.core.vm.Op;
import org.tron.core.vm.OperationRegistry;
import org.tron.core.vm.VM;
import org.tron.core.vm.VMConstant;
Expand Down Expand Up @@ -188,6 +189,13 @@ public void execute(Object object) throws ContractExeException {
VM.play(program, OperationRegistry.getTable());
result = program.getResult();

if (VMConfig.allowEnergyAdjustment()) {
// If the last op consumed too much execution time, the CPU time limit for the whole tx can be exceeded.
// This is not fair for other txs in the same block.
// So when allowFairEnergyAdjustment is on, the CPU time limit will be checked at the end of tx execution.
program.checkCPUTimeLimit(Op.getNameOf(program.getLastOp()) + "(TX_LAST_OP)");
}

if (TrxType.TRX_CONTRACT_CREATION_TYPE == trxType && !result.isRevert()) {
byte[] code = program.getResult().getHReturn();
if (code.length != 0 && VMConfig.allowTvmLondon() && code[0] == (byte) 0xEF) {
Expand Down
35 changes: 34 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tron.core.utils;

import static org.tron.core.Constant.CREATE_ACCOUNT_TRANSACTION_MAX_BYTE_SIZE;
import static org.tron.core.Constant.CREATE_ACCOUNT_TRANSACTION_MIN_BYTE_SIZE;
import static org.tron.core.Constant.DYNAMIC_ENERGY_INCREASE_FACTOR_RANGE;
import static org.tron.core.Constant.DYNAMIC_ENERGY_MAX_FACTOR_RANGE;
import static org.tron.core.config.Parameter.ChainConstant.ONE_YEAR_BLOCK_NUMBERS;
Expand Down Expand Up @@ -748,6 +750,35 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_ENERGY_ADJUSTMENT: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_5)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_ENERGY_ADJUSTMENT]");
}
if (dynamicPropertiesStore.getAllowEnergyAdjustment() == 1) {
throw new ContractValidateException(
"[ALLOW_ENERGY_ADJUSTMENT] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_ENERGY_ADJUSTMENT] is only allowed to be 1");
}
break;
}
case MAX_CREATE_ACCOUNT_TX_SIZE: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_5)) {
throw new ContractValidateException(
"Bad chain parameter id [MAX_CREATE_ACCOUNT_TX_SIZE]");
}
if (value < CREATE_ACCOUNT_TRANSACTION_MIN_BYTE_SIZE
|| value > CREATE_ACCOUNT_TRANSACTION_MAX_BYTE_SIZE) {
throw new ContractValidateException(
"This value[MAX_CREATE_ACCOUNT_TX_SIZE] is only allowed to be greater than or equal "
+ "to " + CREATE_ACCOUNT_TRANSACTION_MIN_BYTE_SIZE + " and less than or equal to "
+ CREATE_ACCOUNT_TRANSACTION_MAX_BYTE_SIZE + "!");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -824,7 +855,9 @@ public enum ProposalType { // current value, value range
ALLOW_TVM_SHANGHAI(76), // 0, 1
ALLOW_CANCEL_ALL_UNFREEZE_V2(77), // 0, 1
MAX_DELEGATE_LOCK_PERIOD(78), // (86400, 10512000]
ALLOW_OLD_REWARD_OPT(79); // 0, 1
ALLOW_OLD_REWARD_OPT(79), // 0, 1
ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1
MAX_CREATE_ACCOUNT_TX_SIZE(82); // [500, 10000]

private long code;

Expand Down
31 changes: 30 additions & 1 deletion actuator/src/main/java/org/tron/core/vm/EnergyCost.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,19 @@ public static long getSuicideCost(Program ignored) {
return SUICIDE;
}

public static long getSuicideCost2(Program program) {
DataWord inheritorAddress = program.getStack().peek();
if (isDeadAccount(program, inheritorAddress)) {
return getSuicideCost(program) + NEW_ACCT_CALL;
}
return getSuicideCost(program);
}

public static long getBalanceCost(Program ignored) {
return BALANCE;
}

public static long getFreezeCost(Program program) {

Stack stack = program.getStack();
DataWord receiverAddressWord = stack.get(stack.size() - 3);
if (isDeadAccount(program, receiverAddressWord)) {
Expand Down Expand Up @@ -306,7 +313,27 @@ public static long getUnDelegateResourceCost(Program ignored) {
}

public static long getVoteWitnessCost(Program program) {
Stack stack = program.getStack();
long oldMemSize = program.getMemSize();
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
DataWord amountArrayOffset = stack.get(stack.size() - 2);
DataWord witnessArrayLength = stack.get(stack.size() - 3).clone();
DataWord witnessArrayOffset = stack.get(stack.size() - 4);

DataWord wordSize = new DataWord(DataWord.WORD_SIZE);

amountArrayLength.mul(wordSize);
BigInteger amountArrayMemoryNeeded = memNeeded(amountArrayOffset, amountArrayLength);

witnessArrayLength.mul(wordSize);
BigInteger witnessArrayMemoryNeeded = memNeeded(witnessArrayOffset, witnessArrayLength);

return VOTE_WITNESS + calcMemEnergy(oldMemSize,
(amountArrayMemoryNeeded.compareTo(witnessArrayMemoryNeeded) > 0
? amountArrayMemoryNeeded : witnessArrayMemoryNeeded), 0, Op.VOTEWITNESS);
}

public static long getVoteWitnessCost2(Program program) {
Stack stack = program.getStack();
long oldMemSize = program.getMemSize();
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
Expand All @@ -317,9 +344,11 @@ public static long getVoteWitnessCost(Program program) {
DataWord wordSize = new DataWord(DataWord.WORD_SIZE);

amountArrayLength.mul(wordSize);
amountArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
BigInteger amountArrayMemoryNeeded = memNeeded(amountArrayOffset, amountArrayLength);

witnessArrayLength.mul(wordSize);
witnessArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
BigInteger witnessArrayMemoryNeeded = memNeeded(witnessArrayOffset, witnessArrayLength);

return VOTE_WITNESS + calcMemEnergy(oldMemSize,
Expand Down
17 changes: 17 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public static JumpTable getTable() {
adjustMemOperations(table);
}

if (VMConfig.allowEnergyAdjustment()) {
adjustForFairEnergy(table);
}

return table;
}

Expand Down Expand Up @@ -635,4 +639,17 @@ public static void appendShangHaiOperations(JumpTable table) {
OperationActions::push0Action,
proposal));
}

public static void adjustForFairEnergy(JumpTable table) {
table.set(new Operation(
Op.VOTEWITNESS, 4, 1,
EnergyCost::getVoteWitnessCost2,
OperationActions::voteWitnessAction,
VMConfig::allowTvmVote));

table.set(new Operation(
Op.SUICIDE, 1, 0,
EnergyCost::getSuicideCost2,
OperationActions::suicideAction));
}
}
7 changes: 6 additions & 1 deletion actuator/src/main/java/org/tron/core/vm/VMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,6 +20,7 @@
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Commons;
import org.tron.common.utils.DecodeUtil;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.vm.config.VMConfig;
Expand All @@ -34,6 +34,11 @@ public final class VMUtils {
private VMUtils() {
}

public static int getAddressSize() {
return VMConfig.allowEnergyAdjustment() ?
Constant.TRON_ADDRESS_SIZE : Constant.STANDARD_ADDRESS_SIZE;
}

public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor());
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment());
}
}
}
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 @@ -49,6 +49,8 @@ public class VMConfig {

private static boolean ALLOW_TVM_SHANGHAI = false;

private static boolean ALLOW_ENERGY_ADJUSTMENT = false;

private VMConfig() {
}

Expand Down Expand Up @@ -136,6 +138,10 @@ public static void initAllowTvmShangHai(long allow) {
ALLOW_TVM_SHANGHAI = allow == 1;
}

public static void initAllowEnergyAdjustment(long allow) {
ALLOW_ENERGY_ADJUSTMENT = allow == 1;
}

public static boolean getEnergyLimitHardFork() {
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
}
Expand Down Expand Up @@ -211,4 +217,8 @@ public static long getDynamicEnergyMaxFactor() {
public static boolean allowTvmShanghai() {
return ALLOW_TVM_SHANGHAI;
}

public static boolean allowEnergyAdjustment() {
return ALLOW_ENERGY_ADJUSTMENT;
}
}
7 changes: 6 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 @@ -276,6 +276,10 @@ public void setLastOp(byte op) {
this.lastOp = op;
}

public byte getLastOp() {
return this.lastOp;
}

/**
* Returns the last fully executed OP.
*/
Expand Down Expand Up @@ -458,7 +462,8 @@ public void suicide(DataWord obtainerAddress) {
InternalTransaction internalTx = addInternalTx(null, owner, obtainer, balance, null,
"suicide", nonce, getContractState().getAccount(owner).getAssetMapV2());

if (FastByteComparisons.compareTo(owner, 0, 20, obtainer, 0, 20) == 0) {
int ADDRESS_SIZE = VMUtils.getAddressSize();
if (FastByteComparisons.compareTo(owner, 0, ADDRESS_SIZE, obtainer, 0, ADDRESS_SIZE) == 0) {
// if owner == obtainer just zeroing account according to Yellow Paper
getContractState().addBalance(owner, -balance);
byte[] blackHoleAddress = getContractState().getBlackHoleAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.util.stream.Collectors;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.AbstractComparator;
import org.rocksdb.BlockBasedTableConfig;
import org.rocksdb.BloomFilter;
import org.rocksdb.Checkpoint;
import org.rocksdb.DirectComparator;
import org.rocksdb.InfoLogLevel;
import org.rocksdb.Logger;
import org.rocksdb.Options;
Expand Down Expand Up @@ -60,11 +60,11 @@ public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
private static final String KEY_ENGINE = "ENGINE";
private static final String ROCKSDB = "ROCKSDB";
private DirectComparator comparator;
private AbstractComparator comparator;
private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB);

public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings,
DirectComparator comparator) {
AbstractComparator comparator) {
this.dataBaseName = name;
this.parentPath = parentPath;
this.comparator = comparator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.tron.common.utils;

import org.rocksdb.AbstractComparator;
import org.rocksdb.ComparatorOptions;
import org.rocksdb.DirectSlice;
import org.rocksdb.util.DirectBytewiseComparator;
import org.tron.core.capsule.utils.MarketUtils;

public class MarketOrderPriceComparatorForRockDB extends DirectBytewiseComparator {
import java.nio.ByteBuffer;

public class MarketOrderPriceComparatorForRockDB extends AbstractComparator {

public MarketOrderPriceComparatorForRockDB(final ComparatorOptions copt) {
super(copt);
Expand All @@ -17,21 +19,16 @@ public String name() {
}

@Override
public int compare(final DirectSlice a, final DirectSlice b) {
public int compare(final ByteBuffer a, final ByteBuffer b) {
return MarketUtils.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b));
}

/**
* DirectSlice.data().array will throw UnsupportedOperationException.
* */
public byte[] convertDataToBytes(DirectSlice directSlice) {
int capacity = directSlice.data().capacity();
byte[] bytes = new byte[capacity];

for (int i = 0; i < capacity; i++) {
bytes[i] = directSlice.get(i);
}

public byte[] convertDataToBytes(ByteBuffer buf) {
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
return bytes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.tron.common.utils.StringUtil.encode58Check;
import static org.tron.common.utils.WalletUtil.checkPermissionOperations;
import static org.tron.core.Constant.MAX_CONTRACT_RESULT_SIZE;
import static org.tron.core.exception.P2pException.TypeEnum.PROTOBUF_ERROR;

import com.google.common.primitives.Bytes;
Expand Down Expand Up @@ -114,6 +115,9 @@ public class TransactionCapsule implements ProtoCapsule<Transaction> {
@Getter
@Setter
private boolean isTransactionCreate = false;
@Getter
@Setter
private boolean isInBlock = false;

public byte[] getOwnerAddress() {
if (this.ownerAddress == null) {
Expand Down Expand Up @@ -730,6 +734,15 @@ public long getResultSerializedSize() {
return size;
}

public long getResultSizeWithMaxContractRet() {
long size = 0;
for (Result result : this.transaction.getRetList()) {
size += result.toBuilder().clearContractRet().build().getSerializedSize()
+ MAX_CONTRACT_RESULT_SIZE;
}
return size;
}

@Override
public Transaction getInstance() {
return this.transaction;
Expand Down Expand Up @@ -842,4 +855,18 @@ public BalanceContract.TransferContract getTransferContract() {
return null;
}
}

public void removeRedundantRet() {
Transaction tx = this.getInstance();
List<Result> tmpList = new ArrayList<>(tx.getRetList());
int contractCount = tx.getRawData().getContractCount();
if (tx.getRetCount() > contractCount && contractCount > 0) {
Transaction.Builder transactionBuilder = tx.toBuilder().clearRet();
for (int i = 0; i < contractCount; i++) {
Result result = tmpList.get(i);
transactionBuilder.addRet(result);
}
this.transaction = transactionBuilder.build();
}
}
}
Loading

0 comments on commit a33dada

Please sign in to comment.