Skip to content

Commit 8d898b4

Browse files
feat: refine contract deployment transaction validation (#6945)
Co-authored-by: Asuka <yanghang8612@163.com>
1 parent e6ccd38 commit 8d898b4

4 files changed

Lines changed: 161 additions & 5 deletions

File tree

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

Lines changed: 23 additions & 5 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;
@@ -218,6 +220,9 @@ public void execute(Object object) throws ContractExeException {
218220
} else {
219221
result.spendEnergy(saveCodeEnergy);
220222
if (VMConfig.allowTvmConstantinople()) {
223+
CreateSmartContract createContract =
224+
ContractCapsule.getSmartContractFromTransaction(trx);
225+
checkContractHashFields(createContract.getNewContract());
221226
rootRepository.saveCode(program.getContractAddress().getNoLeadZeroesData(), code);
222227
}
223228
}
@@ -331,6 +336,7 @@ private void create()
331336
if (contract == null) {
332337
throw new ContractValidateException("Cannot get CreateSmartContract from transaction");
333338
}
339+
334340
SmartContract newSmartContract;
335341
if (VMConfig.allowTvmCompatibleEvm()) {
336342
newSmartContract = contract.getNewContract().toBuilder().setVersion(1).build();
@@ -342,11 +348,7 @@ private void create()
342348
throw new ContractValidateException("OwnerAddress is not equals OriginAddress");
343349
}
344350

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

351353
long percent = contract.getNewContract().getConsumeUserResourcePercent();
352354
if (percent < 0 || percent > VMConstant.ONE_HUNDRED) {
@@ -456,6 +458,22 @@ private void create()
456458

457459
}
458460

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+
459477
/**
460478
* **
461479
*/

‎actuator/src/main/java/org/tron/core/vm/utils/MUtil.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public static boolean isNotNullOrEmpty(String str) {
5959
return !isNullOrEmpty(str);
6060
}
6161

62+
public static void checkCPUTimeForContractHashFields() {
63+
if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)) {
64+
throw new OutOfTimeException("CPU timeout for contract hash fields");
65+
}
66+
}
67+
6268
public static void checkCPUTime() {
6369
if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_7_1)) {
6470
throw new OutOfTimeException("CPU timeout for 0x0a executing");
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.tron.core.actuator;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import com.google.protobuf.ByteString;
9+
import org.junit.Test;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.Mockito;
12+
import org.tron.common.utils.ForkController;
13+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
14+
import org.tron.core.vm.program.Program.OutOfTimeException;
15+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
16+
17+
public class ContractHashValidationTest {
18+
19+
@Test
20+
public void acceptsHashFieldsBeforeActivation() {
21+
SmartContract contract = SmartContract.newBuilder()
22+
.setCodeHash(ByteString.copyFromUtf8("code"))
23+
.setTrxHash(ByteString.copyFromUtf8("transaction"))
24+
.build();
25+
26+
runWithActivation(false, () -> VMActuator.checkContractHashFields(contract));
27+
}
28+
29+
@Test
30+
public void rejectsCodeHashAfterActivation() {
31+
SmartContract contract = SmartContract.newBuilder()
32+
.setCodeHash(ByteString.copyFromUtf8("code"))
33+
.build();
34+
35+
OutOfTimeException exception = assertThrows(OutOfTimeException.class,
36+
() -> runWithActivation(true, () -> VMActuator.checkContractHashFields(contract)));
37+
38+
assertEquals("CPU timeout for contract hash fields", exception.getMessage());
39+
}
40+
41+
@Test
42+
public void rejectsTransactionHashAfterActivation() {
43+
SmartContract contract = SmartContract.newBuilder()
44+
.setTrxHash(ByteString.copyFromUtf8("transaction"))
45+
.build();
46+
47+
OutOfTimeException exception = assertThrows(OutOfTimeException.class,
48+
() -> runWithActivation(true, () -> VMActuator.checkContractHashFields(contract)));
49+
50+
assertEquals("CPU timeout for contract hash fields", exception.getMessage());
51+
}
52+
53+
@Test
54+
public void acceptsEmptyHashFieldsAfterActivation() {
55+
runWithActivation(true,
56+
() -> VMActuator.checkContractHashFields(SmartContract.getDefaultInstance()));
57+
}
58+
59+
private void runWithActivation(boolean activated, Runnable action) {
60+
ForkController controller = mock(ForkController.class);
61+
when(controller.pass(ForkBlockVersionEnum.VERSION_4_8_2_2)).thenReturn(activated);
62+
try (MockedStatic<ForkController> controllerMock = Mockito.mockStatic(ForkController.class)) {
63+
controllerMock.when(ForkController::instance).thenReturn(controller);
64+
action.run();
65+
}
66+
}
67+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.tron.core.actuator;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import org.junit.Test;
9+
import org.junit.function.ThrowingRunnable;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.Mockito;
12+
import org.tron.common.utils.ForkController;
13+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
14+
import org.tron.core.exception.ContractValidateException;
15+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
16+
17+
public class ContractNameValidationTest {
18+
19+
@Test
20+
public void acceptsThirtyTwoByteNameAfterActivation() throws Throwable {
21+
SmartContract contract = contractWithName("12345678901234567890123456789012");
22+
23+
runWithActivation(true, () -> VMActuator.checkContractNameLength(contract));
24+
}
25+
26+
@Test
27+
public void rejectsThirtyThreeByteNameAfterActivation() {
28+
SmartContract contract = contractWithName("123456789012345678901234567890123");
29+
30+
ContractValidateException exception = assertThrows(ContractValidateException.class,
31+
() -> runWithActivation(true, () -> VMActuator.checkContractNameLength(contract)));
32+
33+
assertEquals("contractName's length cannot be greater than 32", exception.getMessage());
34+
}
35+
36+
@Test
37+
public void countsMultibyteNameUsingProtobufBytesAfterActivation() {
38+
SmartContract contract = contractWithName("合合合合合合合合合合合");
39+
assertEquals(33, contract.getNameBytes().size());
40+
41+
assertThrows(ContractValidateException.class,
42+
() -> runWithActivation(true, () -> VMActuator.checkContractNameLength(contract)));
43+
}
44+
45+
@Test
46+
public void preservesNameValidationBeforeActivation() {
47+
SmartContract contract = contractWithName("123456789012345678901234567890123");
48+
49+
assertThrows(ContractValidateException.class,
50+
() -> runWithActivation(false, () -> VMActuator.checkContractNameLength(contract)));
51+
}
52+
53+
private SmartContract contractWithName(String name) {
54+
return SmartContract.newBuilder().setName(name).build();
55+
}
56+
57+
private void runWithActivation(boolean activated, ThrowingRunnable action) throws Throwable {
58+
ForkController controller = mock(ForkController.class);
59+
when(controller.pass(ForkBlockVersionEnum.VERSION_4_8_2_2)).thenReturn(activated);
60+
try (MockedStatic<ForkController> controllerMock = Mockito.mockStatic(ForkController.class)) {
61+
controllerMock.when(ForkController::instance).thenReturn(controller);
62+
action.run();
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)