Skip to content

Commit 7811240

Browse files
committed
Merge branch 'develop' into feature/support_domain
2 parents 686e31e + 0757279 commit 7811240

46 files changed

Lines changed: 865 additions & 915 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ private void create()
398398
byte[] ops = newSmartContract.getBytecode().toByteArray();
399399
rootInternalTx = new InternalTransaction(trx, trxType);
400400

401-
long maxCpuTimeOfOneTx = rootRepository.getDynamicPropertiesStore()
402-
.getMaxCpuTimeOfOneTx() * VMConstant.ONE_THOUSAND;
403-
long thisTxCPULimitInUs = (long) (maxCpuTimeOfOneTx * getCpuLimitInUsRatio());
401+
long thisTxCPULimitInUs = calculateCpuLimitInUs(isConstantCall,
402+
rootRepository.getDynamicPropertiesStore().getMaxCpuTimeOfOneTx(),
403+
getCpuLimitInUsRatio(), CommonParameter.getInstance().getConstantCallTimeoutMs());
404404
long vmStartInUs = System.nanoTime() / VMConstant.ONE_THOUSAND;
405405
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;
406406
ProgramInvoke programInvoke = ProgramInvokeFactory
@@ -512,10 +512,9 @@ private void call()
512512
energyLimit = getTotalEnergyLimit(creator, caller, contract, feeLimit, callValue);
513513
}
514514

515-
long maxCpuTimeOfOneTx = rootRepository.getDynamicPropertiesStore()
516-
.getMaxCpuTimeOfOneTx() * VMConstant.ONE_THOUSAND;
517-
long thisTxCPULimitInUs =
518-
(long) (maxCpuTimeOfOneTx * getCpuLimitInUsRatio());
515+
long thisTxCPULimitInUs = calculateCpuLimitInUs(isConstantCall,
516+
rootRepository.getDynamicPropertiesStore().getMaxCpuTimeOfOneTx(),
517+
getCpuLimitInUsRatio(), CommonParameter.getInstance().getConstantCallTimeoutMs());
519518
long vmStartInUs = System.nanoTime() / VMConstant.ONE_THOUSAND;
520519
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;
521520
ProgramInvoke programInvoke = ProgramInvokeFactory
@@ -692,6 +691,14 @@ private double getCpuLimitInUsRatio() {
692691
return cpuLimitRatio;
693692
}
694693

694+
static long calculateCpuLimitInUs(boolean isConstantCall, long maxCpuTimeOfOneTxMs,
695+
double cpuLimitInUsRatio, long constantCallTimeoutMs) {
696+
if (isConstantCall && constantCallTimeoutMs > 0L) {
697+
return constantCallTimeoutMs * VMConstant.ONE_THOUSAND;
698+
}
699+
return (long) (maxCpuTimeOfOneTxMs * VMConstant.ONE_THOUSAND * cpuLimitInUsRatio);
700+
}
701+
695702
public long getTotalEnergyLimitWithFixRatio(AccountCapsule creator, AccountCapsule caller,
696703
TriggerSmartContract contract, long feeLimit, long callValue)
697704
throws ContractValidateException {

actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static java.util.Arrays.copyOfRange;
44
import static org.tron.common.math.Maths.max;
55
import static org.tron.common.math.Maths.min;
6+
import static org.tron.common.math.StrictMathWrapper.multiplyExact;
7+
import static org.tron.common.math.StrictMathWrapper.subtractExact;
68
import static org.tron.common.runtime.vm.DataWord.WORD_SIZE;
79
import static org.tron.common.utils.BIUtil.addSafely;
810
import static org.tron.common.utils.BIUtil.isLessThan;
@@ -427,6 +429,14 @@ private static byte[] extractBytes(byte[] data, int offset, int len) {
427429
return Arrays.copyOfRange(data, offset, offset + len);
428430
}
429431

432+
private static boolean isValidAbiEncoding(byte[] data, int headerWords, int itemWords) {
433+
if (data == null || data.length % WORD_SIZE != 0) {
434+
return false;
435+
}
436+
long tail = subtractExact(data.length, multiplyExact(headerWords, WORD_SIZE));
437+
return tail > 0 && tail % multiplyExact(itemWords, WORD_SIZE) == 0;
438+
}
439+
430440
public abstract static class PrecompiledContract {
431441

432442
protected static final byte[] DATA_FALSE = new byte[WORD_SIZE];
@@ -1020,6 +1030,8 @@ public static class ValidateMultiSign extends PrecompiledContract {
10201030

10211031
private static final int ENGERYPERSIGN = 1500;
10221032
private static final int MAX_SIZE = 5;
1033+
private static final int ABI_HEADER_WORDS = 5;
1034+
private static final int ABI_ITEM_WORDS = 5;
10231035

10241036

10251037
@Override
@@ -1031,6 +1043,10 @@ public long getEnergyForData(byte[] data) {
10311043

10321044
@Override
10331045
public Pair<Boolean, byte[]> execute(byte[] rawData) {
1046+
if (VMConfig.allowTvmOsaka()
1047+
&& !isValidAbiEncoding(rawData, ABI_HEADER_WORDS, ABI_ITEM_WORDS)) {
1048+
return Pair.of(false, EMPTY_BYTE_ARRAY);
1049+
}
10341050
DataWord[] words = DataWord.parseArray(rawData);
10351051
byte[] address = words[0].toTronAddress();
10361052
int permissionId = words[1].intValueSafe();
@@ -1103,6 +1119,8 @@ public static class BatchValidateSign extends PrecompiledContract {
11031119
private static final String workersName = "validate-sign-contract";
11041120
private static final int ENGERYPERSIGN = 1500;
11051121
private static final int MAX_SIZE = 16;
1122+
private static final int ABI_HEADER_WORDS = 5;
1123+
private static final int ABI_ITEM_WORDS = 6;
11061124

11071125
static {
11081126
workers = ExecutorServiceManager.newFixedThreadPool(workersName,
@@ -1130,6 +1148,10 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
11301148

11311149
private Pair<Boolean, byte[]> doExecute(byte[] data)
11321150
throws InterruptedException, ExecutionException {
1151+
if (VMConfig.allowTvmOsaka()
1152+
&& !isValidAbiEncoding(data, ABI_HEADER_WORDS, ABI_ITEM_WORDS)) {
1153+
return Pair.of(false, EMPTY_BYTE_ARRAY);
1154+
}
11331155
DataWord[] words = DataWord.parseArray(data);
11341156
byte[] hash = words[0].getData();
11351157

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.tron.core.actuator;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class VMActuatorTest {
8+
9+
@Test
10+
public void testConstantCallUsesConfiguredTimeoutVerbatim() {
11+
assertEquals(123_000L, VMActuator.calculateCpuLimitInUs(true, 80L, 5.0, 123L));
12+
}
13+
14+
@Test
15+
public void testConstantCallWithoutConfiguredTimeoutUsesNetworkDeadline() {
16+
assertEquals(400_000L, VMActuator.calculateCpuLimitInUs(true, 80L, 5.0, 0L));
17+
}
18+
19+
@Test
20+
public void testNonConstantCallIgnoresConfiguredTimeout() {
21+
assertEquals(400_000L, VMActuator.calculateCpuLimitInUs(false, 80L, 5.0, 123L));
22+
}
23+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.tron.core.capsule;
1717

18+
import static org.tron.core.exception.BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED;
19+
1820
import com.google.common.primitives.Longs;
1921
import com.google.protobuf.ByteString;
2022
import com.google.protobuf.CodedInputStream;
@@ -37,6 +39,7 @@
3739
import org.tron.common.utils.Time;
3840
import org.tron.core.capsule.utils.MerkleTree;
3941
import org.tron.core.config.Parameter.ChainConstant;
42+
import org.tron.core.exception.BadBlockException;
4043
import org.tron.core.exception.BadItemException;
4144
import org.tron.core.exception.ValidateSignatureException;
4245
import org.tron.core.store.AccountStore;
@@ -49,6 +52,7 @@
4952
public class BlockCapsule implements ProtoCapsule<Block> {
5053

5154
public boolean generatedByMyself = false;
55+
private volatile boolean merkleValidated = false;
5256
@Getter
5357
@Setter
5458
private TransactionRetCapsule result;
@@ -225,6 +229,19 @@ public Sha256Hash calcMerkleRoot() {
225229
return MerkleTree.getInstance().createTree(ids).getRoot().getHash();
226230
}
227231

232+
public void validateMerkleRoot() throws BadBlockException {
233+
if (merkleValidated) {
234+
return;
235+
}
236+
Sha256Hash actual = calcMerkleRoot();
237+
if (!actual.equals(getMerkleRoot())) {
238+
throw new BadBlockException(CALC_MERKLE_ROOT_FAILED,
239+
String.format("merkle root mismatch for block %d: expected %s, actual %s",
240+
getNum(), getMerkleRoot(), actual));
241+
}
242+
merkleValidated = true;
243+
}
244+
228245
public void setMerkleRoot() {
229246
BlockHeader.raw blockHeaderRaw =
230247
this.block.getBlockHeader().getRawData().toBuilder()

common/src/main/java/org/tron/common/config/DbBackupConfig.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import lombok.Setter;
1010
import org.slf4j.bridge.SLF4JBridgeHandler;
1111
import org.tron.common.args.GenesisBlock;
12-
import org.tron.common.config.DbBackupConfig;
1312
import org.tron.common.cron.CronExpression;
1413
import org.tron.common.logsfilter.EventPluginConfig;
1514
import org.tron.common.logsfilter.FilterQuery;
@@ -71,6 +70,18 @@ public class CommonParameter {
7170
@Getter
7271
@Setter
7372
public double maxTimeRatio = calcMaxTimeRatio();
73+
/**
74+
* Max TVM execution time (ms) for constant calls — covers
75+
* triggerconstantcontract, triggersmartcontract dispatched to view/pure
76+
* functions, estimateenergy, eth_call, eth_estimateGas, and any other
77+
* RPC routed through Wallet#callConstantContract. 0 = use the same
78+
* deadline as block processing (current behaviour). When operators set
79+
* this in config the value must be positive and fit VM deadline conversion;
80+
* validated at config-load in VmConfig.
81+
*/
82+
@Getter
83+
@Setter
84+
public long constantCallTimeoutMs = 0L;
7485
@Getter
7586
@Setter
7687
public boolean saveInternalTx;
@@ -413,8 +424,6 @@ public class CommonParameter {
413424
@Setter
414425
public double rateLimiterDisconnect; // clearParam: 1.0
415426
@Getter
416-
public DbBackupConfig dbBackupConfig;
417-
@Getter
418427
public RocksDbSettings rocksDBCustomSettings;
419428
@Getter
420429
public GenesisBlock genesisBlock;
@@ -492,6 +501,9 @@ public class CommonParameter {
492501
public long pendingTransactionTimeout;
493502
@Getter
494503
@Setter
504+
public int maxTrxCacheSize;
505+
@Getter
506+
@Setter
495507
public boolean nodeMetricsEnable = false;
496508
@Getter
497509
@Setter

common/src/main/java/org/tron/core/config/Parameter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public class NetConstants {
102102
public static final int MSG_CACHE_DURATION_IN_BLOCKS = 5;
103103
public static final int MAX_BLOCK_FETCH_PER_PEER = 100;
104104
public static final int MAX_TRX_FETCH_PER_PEER = 1000;
105+
public static final int MAX_SYNC_CHAIN_IDS = 30;
105106
}
106107

107108
public class DatabaseConstants {

common/src/main/java/org/tron/core/config/args/NodeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class NodeConfig {
8585
private ChannelConfig channel = new ChannelConfig();
8686
private int maxTransactionPendingSize = 2000;
8787
private long pendingTransactionTimeout = 60000;
88+
private int maxTrxCacheSize = 50_000;
8889
private int agreeNodeCount = 0;
8990
private boolean openHistoryQueryWhenLiteFN = false;
9091
private boolean unsolidifiedBlockCheck = false;
@@ -498,6 +499,11 @@ private void postProcess() {
498499
if (dynamicConfig.checkInterval <= 0) {
499500
dynamicConfig.checkInterval = 600;
500501
}
502+
503+
// maxTrxCacheSize: minimum 2000
504+
if (maxTrxCacheSize < 2000) {
505+
maxTrxCacheSize = 2000;
506+
}
501507
}
502508

503509
// ===========================================================================

common/src/main/java/org/tron/core/config/args/StorageConfig.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class StorageConfig {
2525
private TransHistoryConfig transHistory = new TransHistoryConfig();
2626
private boolean needToUpdateAsset = true;
2727
private DbSettingsConfig dbSettings = new DbSettingsConfig();
28-
private BackupConfig backup = new BackupConfig();
2928
private BalanceConfig balance = new BalanceConfig();
3029
private CheckpointConfig checkpoint = new CheckpointConfig();
3130
private SnapshotConfig snapshot = new SnapshotConfig();
@@ -129,16 +128,6 @@ void postProcess() {
129128
}
130129
}
131130

132-
@Getter
133-
@Setter
134-
public static class BackupConfig {
135-
private boolean enable = false;
136-
private String propPath = "prop.properties";
137-
private String bak1path = "bak1/database/";
138-
private String bak2path = "bak2/database/";
139-
private int frequency = 10000;
140-
}
141-
142131
@Getter
143132
@Setter
144133
public static class BalanceConfig {

0 commit comments

Comments
 (0)