Skip to content

Commit

Permalink
Cherry-pick mono changes (#9010)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Tinker <[email protected]>
  • Loading branch information
tinker-michaelj authored Oct 3, 2023
1 parent d482914 commit ba63786
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/** FUTURE: This class will be moved to hedera-app-spi module in future PRs */
@Singleton
public class HederaNumbers {
public static final long FIRST_USER_ENTITY = 1001L;

public static final long NUM_RESERVED_SYSTEM_ENTITIES = 750L;
public static final long FIRST_POST_SYSTEM_FILE_ENTITY = 200L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.node.app.service.mono.txns.contract;

import static com.hedera.node.app.service.evm.utils.ValidationUtils.validateFalse;
import static com.hedera.node.app.service.mono.config.HederaNumbers.FIRST_USER_ENTITY;
import static com.hedera.node.app.service.mono.ledger.accounts.ContractCustomizer.fromHapiCreation;
import static com.hedera.node.app.service.mono.ledger.accounts.HederaAccountCustomizer.hasStakedId;
import static com.hedera.node.app.service.mono.state.EntityCreator.EMPTY_MEMO;
Expand All @@ -28,6 +29,7 @@
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_GAS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_VALUE;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_AUTORENEW_ACCOUNT;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_FILE_ID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_RENEWAL_PERIOD;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_STAKING_ID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.MAX_GAS_LIMIT_EXCEEDED;
Expand Down Expand Up @@ -350,6 +352,7 @@ Bytes prepareCodeWithConstructorArguments(final ContractCreateTransactionBody op
return Bytes.wrap(ByteStringUtils.unwrapUnsafelyIfPossible(op.getInitcode()));
} else {
final var bytecodeSrc = op.getFileID();
validateFalse(bytecodeSrc.getFileNum() < FIRST_USER_ENTITY, INVALID_FILE_ID);
final byte[] bytecode;
try {
bytecode = hfs.cat(bytecodeSrc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static com.hedera.node.app.hapi.utils.ByteStringUtils.wrapUnsafely;
import static com.hedera.node.app.service.evm.utils.ValidationUtils.validateFalse;
import static com.hedera.node.app.service.evm.utils.ValidationUtils.validateTrue;
import static com.hedera.node.app.service.mono.config.HederaNumbers.FIRST_USER_ENTITY;
import static com.hedera.node.app.service.mono.ledger.properties.AccountProperty.ETHEREUM_NONCE;
import static com.hedera.node.app.service.mono.utils.EntityNum.MISSING_NUM;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_ACCOUNT_ID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_ETHEREUM_TRANSACTION;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_FILE_ID;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.NEGATIVE_ALLOWANCE_AMOUNT;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.OK;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.WRONG_CHAIN_ID;
Expand Down Expand Up @@ -129,7 +131,11 @@ public void doStateTransition() {

@Override
public ResponseCodeEnum validateSemantics(final TxnAccessor accessor) {
if (accessor.getTxn().getEthereumTransaction().getMaxGasAllowance() < 0) {
final var ethTx = accessor.getTxn().getEthereumTransaction();
if (ethTx.hasCallData() && ethTx.getCallData().getFileNum() < FIRST_USER_ENTITY) {
return INVALID_FILE_ID;
}
if (ethTx.getMaxGasAllowance() < 0) {
return NEGATIVE_ALLOWANCE_AMOUNT;
}
final var ethTxData = spanMapAccessor.getEthTxDataMeta(accessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ void hasCorrectApplicability() {
assertFalse(subject.applicability().test(TransactionBody.getDefaultInstance()));
}

@Test
void rejectsSystemFileInitcode() {
givenValidTxnCtx();
final var fileId = FileID.newBuilder().setFileNum(159).build();
given(accessor.getTxn())
.willReturn(contractCreateTxn.toBuilder()
.setContractCreateInstance(contractCreateTxn.getContractCreateInstance().toBuilder()
.setFileID(fileId))
.build());
given(txnCtx.activePayer()).willReturn(ourAccount());
given(txnCtx.accessor()).willReturn(accessor);
given(accountStore.loadAccount(senderAccount.getId())).willReturn(senderAccount);

// when:
Exception exception = assertThrows(InvalidTransactionException.class, () -> subject.doStateTransition());

// then:
assertEquals("INVALID_FILE_ID", exception.getMessage());
}

@Test
void acceptsOkSyntax() {
givenValidTxnCtx(true, false, false, false, true, false);
Expand Down Expand Up @@ -1230,6 +1250,8 @@ void rejectSerializationFailed() {
void throwsErrorOnInvalidBytecode() {
given(hfs.cat(any())).willReturn(new byte[] {1, 2, 3, '\n'});
given(transactionBody.getConstructorParameters()).willReturn(ByteString.EMPTY);
given(transactionBody.getFileID())
.willReturn(FileID.newBuilder().setFileNum(1234L).build());
// when:
Exception exception = assertThrows(
InvalidTransactionException.class, () -> subject.prepareCodeWithConstructorArguments(transactionBody));
Expand All @@ -1239,6 +1261,8 @@ void throwsErrorOnInvalidBytecode() {

@Test
void throwsErrorOnUnfetchableBytecode() {
given(transactionBody.getFileID())
.willReturn(FileID.newBuilder().setFileNum(1234L).build());
given(hfs.cat(any()))
.willThrow(new IllegalArgumentException(TieredHederaFs.IllegalArgumentType.DELETED_FILE.toString()));
assertFailsWith(() -> subject.prepareCodeWithConstructorArguments(transactionBody), FILE_DELETED);
Expand Down

0 comments on commit ba63786

Please sign in to comment.