Skip to content

Commit 243b693

Browse files
authored
Merge pull request tronprotocol#6956 from tronprotocol/master
chore(branch): merge master into develop
2 parents 57b7b04 + d5c3d1d commit 243b693

23 files changed

Lines changed: 826 additions & 111 deletions

File tree

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tron.common.runtime.InternalTransaction.TrxType;
2626
import org.tron.common.runtime.ProgramResult;
2727
import org.tron.common.runtime.vm.DataWord;
28+
import org.tron.common.utils.ForkController;
2829
import org.tron.common.utils.StorageUtils;
2930
import org.tron.common.utils.StringUtil;
3031
import org.tron.common.utils.WalletUtil;
@@ -33,6 +34,7 @@
3334
import org.tron.core.capsule.BlockCapsule;
3435
import org.tron.core.capsule.ContractCapsule;
3536
import org.tron.core.capsule.ReceiptCapsule;
37+
import org.tron.core.config.Parameter;
3638
import org.tron.core.db.EnergyProcessor;
3739
import org.tron.core.db.TransactionContext;
3840
import org.tron.core.exception.ContractExeException;
@@ -189,7 +191,8 @@ public void execute(Object object) throws ContractExeException {
189191
throw e;
190192
}
191193

192-
VM.play(program, OperationRegistry.getTable());
194+
// Prepare the table once for this execution and all nested calls.
195+
VM.play(program, OperationRegistry.prepareAndGetTable(isConstantCall));
193196
result = program.getResult();
194197

195198
if (VMConfig.allowEnergyAdjustment()) {
@@ -217,6 +220,9 @@ public void execute(Object object) throws ContractExeException {
217220
} else {
218221
result.spendEnergy(saveCodeEnergy);
219222
if (VMConfig.allowTvmConstantinople()) {
223+
CreateSmartContract createContract =
224+
ContractCapsule.getSmartContractFromTransaction(trx);
225+
checkContractHashFields(createContract.getNewContract());
220226
rootRepository.saveCode(program.getContractAddress().getNoLeadZeroesData(), code);
221227
}
222228
}
@@ -330,6 +336,7 @@ private void create()
330336
if (contract == null) {
331337
throw new ContractValidateException("Cannot get CreateSmartContract from transaction");
332338
}
339+
333340
SmartContract newSmartContract;
334341
if (VMConfig.allowTvmCompatibleEvm()) {
335342
newSmartContract = contract.getNewContract().toBuilder().setVersion(1).build();
@@ -341,11 +348,7 @@ private void create()
341348
throw new ContractValidateException("OwnerAddress is not equals OriginAddress");
342349
}
343350

344-
byte[] contractName = newSmartContract.getName().getBytes();
345-
346-
if (contractName.length > VMConstant.CONTRACT_NAME_LENGTH) {
347-
throw new ContractValidateException("contractName's length cannot be greater than 32");
348-
}
351+
checkContractNameLength(contract.getNewContract());
349352

350353
long percent = contract.getNewContract().getConsumeUserResourcePercent();
351354
if (percent < 0 || percent > VMConstant.ONE_HUNDRED) {
@@ -455,6 +458,22 @@ private void create()
455458

456459
}
457460

461+
static void checkContractHashFields(SmartContract contract) {
462+
if (!contract.getCodeHash().isEmpty() || !contract.getTrxHash().isEmpty()) {
463+
MUtil.checkCPUTimeForContractHashFields();
464+
}
465+
}
466+
467+
static void checkContractNameLength(SmartContract contract) throws ContractValidateException {
468+
int contractNameLength =
469+
ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)
470+
? contract.getNameBytes().size()
471+
: contract.getName().getBytes().length;
472+
if (contractNameLength > VMConstant.CONTRACT_NAME_LENGTH) {
473+
throw new ContractValidateException("contractName's length cannot be greater than 32");
474+
}
475+
}
476+
458477
/**
459478
* **
460479
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ public void execute(Program program) {
5252
public boolean isEnabled() {
5353
return enabled.getAsBoolean();
5454
}
55+
56+
public Operation adjustCost(Function<Program, Long> newCost) {
57+
return new Operation(opcode, require, ret, newCost, action, enabled);
58+
}
59+
60+
public Operation adjustAction(Consumer<Program> newAction) {
61+
return new Operation(opcode, require, ret, cost, newAction, enabled);
62+
}
5563
}

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

Lines changed: 94 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,45 @@
77

88
public class OperationRegistry {
99

10+
private static final Operation DEFAULT_MLOAD = new Operation(
11+
Op.MLOAD, 1, 1, EnergyCost::getMloadCost, OperationActions::mLoadAction);
12+
13+
private static final Operation DEFAULT_MSTORE = new Operation(
14+
Op.MSTORE, 2, 0, EnergyCost::getMStoreCost, OperationActions::mStoreAction);
15+
16+
private static final Operation DEFAULT_MSTORE8 = new Operation(
17+
Op.MSTORE8, 2, 0, EnergyCost::getMStore8Cost, OperationActions::mStore8Action);
18+
19+
private static final Operation ADJUSTED_MLOAD =
20+
DEFAULT_MLOAD.adjustCost(EnergyCost::getMloadCost2);
21+
22+
private static final Operation ADJUSTED_MSTORE =
23+
DEFAULT_MSTORE.adjustCost(EnergyCost::getMStoreCost2);
24+
25+
private static final Operation ADJUSTED_MSTORE8 =
26+
DEFAULT_MSTORE8.adjustCost(EnergyCost::getMStore8Cost2);
27+
28+
private static final Operation DEFAULT_VOTEWITNESS = new Operation(
29+
Op.VOTEWITNESS, 4, 1, EnergyCost::getVoteWitnessCost,
30+
OperationActions::voteWitnessAction, VMConfig::allowTvmVote);
31+
32+
private static final Operation ADJUSTED_VOTEWITNESS =
33+
DEFAULT_VOTEWITNESS.adjustCost(EnergyCost::getVoteWitnessCost2);
34+
35+
private static final Operation OSAKA_VOTEWITNESS =
36+
DEFAULT_VOTEWITNESS.adjustCost(EnergyCost::getVoteWitnessCost3);
37+
38+
private static final Operation DEFAULT_SUICIDE = new Operation(
39+
Op.SUICIDE, 1, 0, EnergyCost::getSuicideCost, OperationActions::suicideAction);
40+
41+
private static final Operation ADJUSTED_SUICIDE =
42+
DEFAULT_SUICIDE.adjustCost(EnergyCost::getSuicideCost2);
43+
44+
private static final Operation RESTRICTED_SUICIDE =
45+
DEFAULT_SUICIDE.adjustCost(EnergyCost::getSuicideCost3)
46+
.adjustAction(OperationActions::suicideAction2);
47+
1048
public enum Version {
11-
TRON_V1_0,
12-
TRON_V1_1,
13-
TRON_V1_2,
14-
TRON_V1_3,
15-
TRON_V1_4,
1649
TRON_V1_5,
1750
// add more
1851
// TRON_V2,
@@ -21,13 +54,21 @@ public enum Version {
2154

2255
private static final Map<Version, JumpTable> tableMap = new HashMap<>();
2356

57+
// The newest version in use. Bump this when a newer operation set is added,
58+
// together with newLatestOperationSet() below.
59+
private static final Version LATEST_VERSION = Version.TRON_V1_5;
60+
2461
static {
25-
tableMap.put(Version.TRON_V1_0, newTronV10OperationSet());
26-
tableMap.put(Version.TRON_V1_1, newTronV11OperationSet());
27-
tableMap.put(Version.TRON_V1_2, newTronV12OperationSet());
28-
tableMap.put(Version.TRON_V1_3, newTronV13OperationSet());
29-
tableMap.put(Version.TRON_V1_4, newTronV14OperationSet());
30-
tableMap.put(Version.TRON_V1_5, newTronV15OperationSet());
62+
tableMap.put(LATEST_VERSION, newLatestOperationSet());
63+
}
64+
65+
// Constant calls get a dedicated instance of the newest table, isolated from
66+
// the shared consensus table above.
67+
private static final JumpTable CONSTANT_CALL_TABLE = newLatestOperationSet();
68+
69+
// The single place that decides which operation set is the newest.
70+
private static JumpTable newLatestOperationSet() {
71+
return newTronV15OperationSet();
3172
}
3273

3374
public static JumpTable newTronV10OperationSet() {
@@ -74,28 +115,22 @@ public static JumpTable newTronV15OperationSet() {
74115
// Just for warming up class to avoid out_of_time
75116
public static void init() {}
76117

77-
public static JumpTable getTable() {
78-
// always get the table which has the newest version
79-
JumpTable table = tableMap.get(Version.TRON_V1_5);
80-
81-
// next make the corresponding changes, exclude activating opcode
82-
if (VMConfig.allowHigherLimitForMaxCpuTimeOfOneTx()) {
83-
adjustMemOperations(table);
84-
}
85-
86-
if (VMConfig.allowEnergyAdjustment()) {
87-
adjustForFairEnergy(table);
88-
}
89-
90-
if (VMConfig.allowTvmSelfdestructRestriction()) {
91-
adjustSelfdestruct(table);
92-
}
118+
public static JumpTable prepareAndGetTable(boolean isConstantCall) {
119+
JumpTable table = getTable(isConstantCall);
120+
// Apply configuration-dependent changes once at the top level.
121+
adjustTable(table);
122+
return table;
123+
}
93124

94-
if (VMConfig.allowTvmOsaka()) {
95-
adjustVoteWitnessCost(table);
96-
}
125+
public static JumpTable getTable(boolean isConstantCall) {
126+
return isConstantCall ? CONSTANT_CALL_TABLE : tableMap.get(LATEST_VERSION);
127+
}
97128

98-
return table;
129+
private static void adjustTable(JumpTable table) {
130+
// Make the corresponding changes, excluding opcode activation.
131+
adjustMemOperations(table);
132+
adjustVoteWitness(table);
133+
adjustSelfdestruct(table);
99134
}
100135

101136
public static JumpTable newBaseOperationSet() {
@@ -331,20 +366,11 @@ public static JumpTable newBaseOperationSet() {
331366
EnergyCost::getBaseTierCost,
332367
OperationActions::popAction));
333368

334-
table.set(new Operation(
335-
Op.MLOAD, 1, 1,
336-
EnergyCost::getMloadCost,
337-
OperationActions::mLoadAction));
369+
table.set(DEFAULT_MLOAD);
338370

339-
table.set(new Operation(
340-
Op.MSTORE, 2, 0,
341-
EnergyCost::getMStoreCost,
342-
OperationActions::mStoreAction));
371+
table.set(DEFAULT_MSTORE);
343372

344-
table.set(new Operation(
345-
Op.MSTORE8, 2, 0,
346-
EnergyCost::getMStore8Cost,
347-
OperationActions::mStore8Action));
373+
table.set(DEFAULT_MSTORE8);
348374

349375
table.set(new Operation(
350376
Op.SLOAD, 1, 1,
@@ -449,10 +475,7 @@ public static JumpTable newBaseOperationSet() {
449475
EnergyCost::getRevertCost,
450476
OperationActions::revertAction));
451477

452-
table.set(new Operation(
453-
Op.SUICIDE, 1, 0,
454-
EnergyCost::getSuicideCost,
455-
OperationActions::suicideAction));
478+
table.set(DEFAULT_SUICIDE);
456479

457480
return table;
458481
}
@@ -570,11 +593,7 @@ public static void appendFreezeOperations(JumpTable table) {
570593
public static void appendVoteOperations(JumpTable table) {
571594
BooleanSupplier proposal = VMConfig::allowTvmVote;
572595

573-
table.set(new Operation(
574-
Op.VOTEWITNESS, 4, 1,
575-
EnergyCost::getVoteWitnessCost,
576-
OperationActions::voteWitnessAction,
577-
proposal));
596+
table.set(DEFAULT_VOTEWITNESS);
578597

579598
table.set(new Operation(
580599
Op.WITHDRAWREWARD, 0, 1,
@@ -593,23 +612,6 @@ public static void appendLondonOperations(JumpTable table) {
593612
proposal));
594613
}
595614

596-
public static void adjustMemOperations(JumpTable table) {
597-
table.set(new Operation(
598-
Op.MLOAD, 1, 1,
599-
EnergyCost::getMloadCost2,
600-
OperationActions::mLoadAction));
601-
602-
table.set(new Operation(
603-
Op.MSTORE, 2, 0,
604-
EnergyCost::getMStoreCost2,
605-
OperationActions::mStoreAction));
606-
607-
table.set(new Operation(
608-
Op.MSTORE8, 2, 0,
609-
EnergyCost::getMStore8Cost2,
610-
OperationActions::mStore8Action));
611-
}
612-
613615
public static void appendFreezeV2Operations(JumpTable table) {
614616
BooleanSupplier proposal = VMConfig::allowTvmFreezeV2;
615617

@@ -664,19 +666,6 @@ public static void appendShangHaiOperations(JumpTable table) {
664666
proposal));
665667
}
666668

667-
public static void adjustForFairEnergy(JumpTable table) {
668-
table.set(new Operation(
669-
Op.VOTEWITNESS, 4, 1,
670-
EnergyCost::getVoteWitnessCost2,
671-
OperationActions::voteWitnessAction,
672-
VMConfig::allowTvmVote));
673-
674-
table.set(new Operation(
675-
Op.SUICIDE, 1, 0,
676-
EnergyCost::getSuicideCost2,
677-
OperationActions::suicideAction));
678-
}
679-
680669
public static void appendCancunOperations(JumpTable table) {
681670
BooleanSupplier proposal = VMConfig::allowTvmCancun;
682671
BooleanSupplier tvmBlobProposal = VMConfig::allowTvmBlob;
@@ -722,18 +711,30 @@ public static void appendOsakaOperations(JumpTable table) {
722711
proposal));
723712
}
724713

725-
public static void adjustSelfdestruct(JumpTable table) {
726-
table.set(new Operation(
727-
Op.SUICIDE, 1, 0,
728-
EnergyCost::getSuicideCost3,
729-
OperationActions::suicideAction2));
714+
public static void adjustMemOperations(JumpTable table) {
715+
boolean adjusted = VMConfig.allowHigherLimitForMaxCpuTimeOfOneTx();
716+
table.set(adjusted ? ADJUSTED_MLOAD : DEFAULT_MLOAD);
717+
table.set(adjusted ? ADJUSTED_MSTORE : DEFAULT_MSTORE);
718+
table.set(adjusted ? ADJUSTED_MSTORE8 : DEFAULT_MSTORE8);
730719
}
731720

732-
public static void adjustVoteWitnessCost(JumpTable table) {
733-
table.set(new Operation(
734-
Op.VOTEWITNESS, 4, 1,
735-
EnergyCost::getVoteWitnessCost3,
736-
OperationActions::voteWitnessAction,
737-
VMConfig::allowTvmVote));
721+
public static void adjustVoteWitness(JumpTable table) {
722+
if (VMConfig.allowTvmOsaka()) {
723+
table.set(OSAKA_VOTEWITNESS);
724+
} else if (VMConfig.allowEnergyAdjustment()) {
725+
table.set(ADJUSTED_VOTEWITNESS);
726+
} else {
727+
table.set(DEFAULT_VOTEWITNESS);
728+
}
729+
}
730+
731+
public static void adjustSelfdestruct(JumpTable table) {
732+
if (VMConfig.allowTvmSelfdestructRestriction()) {
733+
table.set(RESTRICTED_SUICIDE);
734+
} else if (VMConfig.allowEnergyAdjustment()) {
735+
table.set(ADJUSTED_SUICIDE);
736+
} else {
737+
table.set(DEFAULT_SUICIDE);
738+
}
738739
}
739740
}

actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.tron.core.vm.VMConstant;
2020
import org.tron.core.vm.nativecontract.param.CancelAllUnfreezeV2Param;
2121
import org.tron.core.vm.repository.Repository;
22+
import org.tron.core.vm.utils.MUtil;
2223
import org.tron.protos.Protocol;
2324

2425
@Slf4j(topic = "VMProcessor")
@@ -39,6 +40,10 @@ public void validate(CancelAllUnfreezeV2Param param, Repository repo) throws Con
3940
throw new ContractValidateException(
4041
ACCOUNT_EXCEPTION_STR + readableOwnerAddress + NOT_EXIST_STR);
4142
}
43+
44+
if (accountCapsule.hasInvalidDelegatedV2()) {
45+
MUtil.checkCPUTimeForInvalidDelegatedV2Balance();
46+
}
4247
}
4348

4449
public Map<String, Long> execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {

actuator/src/main/java/org/tron/core/vm/nativecontract/FreezeBalanceV2Processor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.store.DynamicPropertiesStore;
1515
import org.tron.core.vm.nativecontract.param.FreezeBalanceV2Param;
1616
import org.tron.core.vm.repository.Repository;
17+
import org.tron.core.vm.utils.MUtil;
1718

1819
@Slf4j(topic = "VMProcessor")
1920
public class FreezeBalanceV2Processor {
@@ -63,6 +64,10 @@ public void validate(FreezeBalanceV2Param param, Repository repo) throws Contrac
6364
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
6465
}
6566
}
67+
68+
if (repo.isSelfDestructed(ownerAddress)) {
69+
MUtil.checkCPUTimeForFreezeV2AfterSelfDestruct();
70+
}
6671
}
6772

6873
public void execute(FreezeBalanceV2Param param, Repository repo) {

0 commit comments

Comments
 (0)