Skip to content

Commit cd04a42

Browse files
committed
feat: refine contract deployment transaction validation
1 parent f8b05d4 commit cd04a42

5 files changed

Lines changed: 164 additions & 7 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;
@@ -217,6 +219,9 @@ public void execute(Object object) throws ContractExeException {
217219
} else {
218220
result.spendEnergy(saveCodeEnergy);
219221
if (VMConfig.allowTvmConstantinople()) {
222+
CreateSmartContract createContract =
223+
ContractCapsule.getSmartContractFromTransaction(trx);
224+
checkContractHashFields(createContract.getNewContract());
220225
rootRepository.saveCode(program.getContractAddress().getNoLeadZeroesData(), code);
221226
}
222227
}
@@ -330,6 +335,7 @@ private void create()
330335
if (contract == null) {
331336
throw new ContractValidateException("Cannot get CreateSmartContract from transaction");
332337
}
338+
333339
SmartContract newSmartContract;
334340
if (VMConfig.allowTvmCompatibleEvm()) {
335341
newSmartContract = contract.getNewContract().toBuilder().setVersion(1).build();
@@ -341,11 +347,7 @@ private void create()
341347
throw new ContractValidateException("OwnerAddress is not equals OriginAddress");
342348
}
343349

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-
}
350+
checkContractNameLength(contract.getNewContract());
349351

350352
long percent = contract.getNewContract().getConsumeUserResourcePercent();
351353
if (percent < 0 || percent > VMConstant.ONE_HUNDRED) {
@@ -455,6 +457,22 @@ private void create()
455457

456458
}
457459

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

‎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");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public enum ForkBlockVersionEnum {
3030
VERSION_4_8_0_1(33, 1596780000000L, 70),
3131
VERSION_4_8_1(34, 1596780000000L, 80),
3232
VERSION_4_8_1_1(35, 1596780000000L, 70),
33-
VERSION_4_8_2(36, 1596780000000L, 80);
33+
VERSION_4_8_2(36, 1596780000000L, 80),
34+
VERSION_4_8_2_2(37, 1596780000000L, 70);
3435
// if add a version, modify BLOCK_VERSION simultaneously
3536

3637
@Getter
@@ -79,7 +80,7 @@ public class ChainConstant {
7980
public static final int SINGLE_REPEAT = 1;
8081
public static final int BLOCK_FILLED_SLOTS_NUMBER = 128;
8182
public static final int MAX_FROZEN_NUMBER = 1;
82-
public static final int BLOCK_VERSION = 36;
83+
public static final int BLOCK_VERSION = 37;
8384
public static final long FROZEN_PERIOD = 86_400_000L;
8485
public static final long DELEGATE_PERIOD = 3 * 86_400_000L;
8586
public static final long TRX_PRECISION = 1000_000L;
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)