Repository navigation
feat: refine contract deployment transaction validation #6945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import org.tron.common.runtime.InternalTransaction.TrxType; | ||
| import org.tron.common.runtime.ProgramResult; | ||
| import org.tron.common.runtime.vm.DataWord; | ||
| import org.tron.common.utils.ForkController; | ||
| import org.tron.common.utils.StorageUtils; | ||
| import org.tron.common.utils.StringUtil; | ||
| import org.tron.common.utils.WalletUtil; | ||
|
|
@@ -33,6 +34,7 @@ | |
| import org.tron.core.capsule.BlockCapsule; | ||
| import org.tron.core.capsule.ContractCapsule; | ||
| import org.tron.core.capsule.ReceiptCapsule; | ||
| import org.tron.core.config.Parameter; | ||
| import org.tron.core.db.EnergyProcessor; | ||
| import org.tron.core.db.TransactionContext; | ||
| import org.tron.core.exception.ContractExeException; | ||
|
|
@@ -217,6 +219,9 @@ public void execute(Object object) throws ContractExeException { | |
| } else { | ||
| result.spendEnergy(saveCodeEnergy); | ||
| if (VMConfig.allowTvmConstantinople()) { | ||
| CreateSmartContract createContract = | ||
| ContractCapsule.getSmartContractFromTransaction(trx); | ||
| checkContractHashFields(createContract.getNewContract()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is placing this check immediately before saveCode intentional so that the restriction is applied at the code-persistence boundary, while the preceding contract validation flow keeps its existing behavior?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The check is intentionally performed when the deployment reaches the code-persistence stage. If either field is populated after activation, execution is stopped before the deployed code is saved, while earlier validation behavior remains unchanged. |
||
| rootRepository.saveCode(program.getContractAddress().getNoLeadZeroesData(), code); | ||
| } | ||
| } | ||
|
|
@@ -330,6 +335,7 @@ private void create() | |
| if (contract == null) { | ||
| throw new ContractValidateException("Cannot get CreateSmartContract from transaction"); | ||
| } | ||
|
|
||
| SmartContract newSmartContract; | ||
| if (VMConfig.allowTvmCompatibleEvm()) { | ||
| newSmartContract = contract.getNewContract().toBuilder().setVersion(1).build(); | ||
|
|
@@ -341,11 +347,7 @@ private void create() | |
| throw new ContractValidateException("OwnerAddress is not equals OriginAddress"); | ||
| } | ||
|
|
||
| byte[] contractName = newSmartContract.getName().getBytes(); | ||
|
|
||
| if (contractName.length > VMConstant.CONTRACT_NAME_LENGTH) { | ||
| throw new ContractValidateException("contractName's length cannot be greater than 32"); | ||
| } | ||
| checkContractNameLength(contract.getNewContract()); | ||
|
|
||
| long percent = contract.getNewContract().getConsumeUserResourcePercent(); | ||
| if (percent < 0 || percent > VMConstant.ONE_HUNDRED) { | ||
|
|
@@ -455,6 +457,22 @@ private void create() | |
|
|
||
| } | ||
|
|
||
| static void checkContractHashFields(SmartContract contract) { | ||
| if (!contract.getCodeHash().isEmpty() || !contract.getTrxHash().isEmpty()) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Covering both fields with a single || and relying on proto3 isEmpty() correctly handles unset vs. empty — normal deployments are untouched.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly. In proto3, an unset bytes field is exposed as ByteString.EMPTY, so isEmpty() treats unset and explicitly empty values consistently. Only a non-empty code_hash or trx_hash reaches the activation-gated restriction, leaving normal contract deployments unchanged. |
||
| MUtil.checkCPUTimeForContractHashFields(); | ||
| } | ||
| } | ||
|
|
||
| static void checkContractNameLength(SmartContract contract) throws ContractValidateException { | ||
| int contractNameLength = | ||
| ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2) | ||
| ? contract.getNameBytes().size() | ||
| : contract.getName().getBytes().length; | ||
| if (contractNameLength > VMConstant.CONTRACT_NAME_LENGTH) { | ||
| throw new ContractValidateException("contractName's length cannot be greater than 32"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ** | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,12 @@ public static boolean isNotNullOrEmpty(String str) { | |
| return !isNullOrEmpty(str); | ||
| } | ||
|
|
||
| public static void checkCPUTimeForContractHashFields() { | ||
| if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the activation check intentionally kept inside this helper so that the call site can remain unconditional once the persistence path is reached, with the helper becoming a no-op before activation?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. Keeping the activation decision in the helper centralizes the fork-dependent behavior. Before activation it returns normally, and after activation it applies the new restriction. |
||
| throw new OutOfTimeException("CPU timeout for contract hash fields"); | ||
| } | ||
| } | ||
|
|
||
| public static void checkCPUTime() { | ||
| if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_7_1)) { | ||
| throw new OutOfTimeException("CPU timeout for 0x0a executing"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.tron.core.actuator; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.protobuf.ByteString; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.tron.common.utils.ForkController; | ||
| import org.tron.core.config.Parameter.ForkBlockVersionEnum; | ||
| import org.tron.core.vm.program.Program.OutOfTimeException; | ||
| import org.tron.protos.contract.SmartContractOuterClass.SmartContract; | ||
|
|
||
| public class ContractHashValidationTest { | ||
|
|
||
| @Test | ||
| public void acceptsHashFieldsBeforeActivation() { | ||
| SmartContract contract = SmartContract.newBuilder() | ||
| .setCodeHash(ByteString.copyFromUtf8("code")) | ||
| .setTrxHash(ByteString.copyFromUtf8("transaction")) | ||
| .build(); | ||
|
|
||
| runWithActivation(false, () -> VMActuator.checkContractHashFields(contract)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsCodeHashAfterActivation() { | ||
| SmartContract contract = SmartContract.newBuilder() | ||
| .setCodeHash(ByteString.copyFromUtf8("code")) | ||
| .build(); | ||
|
|
||
| OutOfTimeException exception = assertThrows(OutOfTimeException.class, | ||
| () -> runWithActivation(true, () -> VMActuator.checkContractHashFields(contract))); | ||
|
|
||
| assertEquals("CPU timeout for contract hash fields", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsTransactionHashAfterActivation() { | ||
| SmartContract contract = SmartContract.newBuilder() | ||
| .setTrxHash(ByteString.copyFromUtf8("transaction")) | ||
| .build(); | ||
|
|
||
| OutOfTimeException exception = assertThrows(OutOfTimeException.class, | ||
| () -> runWithActivation(true, () -> VMActuator.checkContractHashFields(contract))); | ||
|
|
||
| assertEquals("CPU timeout for contract hash fields", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void acceptsEmptyHashFieldsAfterActivation() { | ||
| runWithActivation(true, | ||
| () -> VMActuator.checkContractHashFields(SmartContract.getDefaultInstance())); | ||
| } | ||
|
|
||
| private void runWithActivation(boolean activated, Runnable action) { | ||
| ForkController controller = mock(ForkController.class); | ||
| when(controller.pass(ForkBlockVersionEnum.VERSION_4_8_2_2)).thenReturn(activated); | ||
| try (MockedStatic<ForkController> controllerMock = Mockito.mockStatic(ForkController.class)) { | ||
| controllerMock.when(ForkController::instance).thenReturn(controller); | ||
| action.run(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.tron.core.actuator; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.function.ThrowingRunnable; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.tron.common.utils.ForkController; | ||
| import org.tron.core.config.Parameter.ForkBlockVersionEnum; | ||
| import org.tron.core.exception.ContractValidateException; | ||
| import org.tron.protos.contract.SmartContractOuterClass.SmartContract; | ||
|
|
||
| public class ContractNameValidationTest { | ||
|
|
||
| @Test | ||
| public void acceptsThirtyTwoByteNameAfterActivation() throws Throwable { | ||
| SmartContract contract = contractWithName("12345678901234567890123456789012"); | ||
|
|
||
| runWithActivation(true, () -> VMActuator.checkContractNameLength(contract)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsThirtyThreeByteNameAfterActivation() { | ||
| SmartContract contract = contractWithName("123456789012345678901234567890123"); | ||
|
|
||
| ContractValidateException exception = assertThrows(ContractValidateException.class, | ||
| () -> runWithActivation(true, () -> VMActuator.checkContractNameLength(contract))); | ||
|
|
||
| assertEquals("contractName's length cannot be greater than 32", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void countsMultibyteNameUsingProtobufBytesAfterActivation() { | ||
| SmartContract contract = contractWithName("合合合合合合合合合合合"); | ||
| assertEquals(33, contract.getNameBytes().size()); | ||
|
|
||
| assertThrows(ContractValidateException.class, | ||
| () -> runWithActivation(true, () -> VMActuator.checkContractNameLength(contract))); | ||
| } | ||
|
|
||
| @Test | ||
| public void preservesNameValidationBeforeActivation() { | ||
| SmartContract contract = contractWithName("123456789012345678901234567890123"); | ||
|
|
||
| assertThrows(ContractValidateException.class, | ||
| () -> runWithActivation(false, () -> VMActuator.checkContractNameLength(contract))); | ||
| } | ||
|
|
||
| private SmartContract contractWithName(String name) { | ||
| return SmartContract.newBuilder().setName(name).build(); | ||
| } | ||
|
|
||
| private void runWithActivation(boolean activated, ThrowingRunnable action) throws Throwable { | ||
| ForkController controller = mock(ForkController.class); | ||
| when(controller.pass(ForkBlockVersionEnum.VERSION_4_8_2_2)).thenReturn(activated); | ||
| try (MockedStatic<ForkController> controllerMock = Mockito.mockStatic(ForkController.class)) { | ||
| controllerMock.when(ForkController::instance).thenReturn(controller); | ||
| action.run(); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: since
create()already unpacked thisCreateSmartContractfromtrx, does callinggetSmartContractFromTransactionagain here add any measurable cost on the deployment path?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking. We do not have a separate microbenchmark for this parsing step, but it occurs only once when a contract deployment reaches the reaches" and is about to persist its code, rather than inside an execution loop.
Reading it from the original transaction also keeps the check tied to the fields actually supplied by the caller and avoids retaining additional state between the the validation and execution phases. Compared with contract initialization and code persistence writing, the additional protobuf unpacking cost is expected to be negligible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed explanation, nothing further from me on this line.