Skip to content
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

fix: Add check for empty inline initcode for contract creation #10463

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.hedera.node.app.service.mono.utils.EntityIdUtils.contractIdFromEvmAddress;
import static com.hederahashgraph.api.proto.java.ContractCreateTransactionBody.InitcodeSourceCase.INITCODE;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.AUTORENEW_DURATION_NOT_IN_RANGE;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_BYTECODE_EMPTY;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_FILE_EMPTY;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_GAS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_VALUE;
Expand Down Expand Up @@ -349,6 +350,7 @@ public ResponseCodeEnum validate(final TransactionBody contractCreateTxn) {

Bytes prepareCodeWithConstructorArguments(final ContractCreateTransactionBody op) {
if (op.getInitcodeSourceCase() == INITCODE) {
validateFalse(op.getInitcode().isEmpty(), CONTRACT_BYTECODE_EMPTY);
return Bytes.wrap(ByteStringUtils.unwrapUnsafelyIfPossible(op.getInitcode()));
} else {
final var bytecodeSrc = op.getFileID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.hedera.node.app.service.mono.sigs.utils.ImmutableKeyUtils.IMMUTABILITY_SENTINEL_KEY;
import static com.hedera.node.app.service.mono.store.contracts.precompile.HTSTestsUtil.create1ContractAddress;
import static com.hedera.test.utils.TxnUtils.assertFailsWith;
import static com.hederahashgraph.api.proto.java.ContractCreateTransactionBody.InitcodeSourceCase.INITCODE;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.AUTORENEW_DURATION_NOT_IN_RANGE;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_GAS;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_NEGATIVE_VALUE;
Expand Down Expand Up @@ -1259,6 +1260,17 @@ void throwsErrorOnInvalidBytecode() {
assertEquals("ERROR_DECODING_BYTESTRING", exception.getMessage());
}

@Test
void throwsErrorOnEmptyInitcode() {
given(transactionBody.getInitcodeSourceCase()).willReturn(INITCODE);
given(transactionBody.getInitcode()).willReturn(ByteString.EMPTY);
// when:
Exception exception = assertThrows(
InvalidTransactionException.class, () -> subject.prepareCodeWithConstructorArguments(transactionBody));
// then:
assertEquals("CONTRACT_BYTECODE_EMPTY", exception.getMessage());
}

@Test
void throwsErrorOnUnfetchableBytecode() {
given(transactionBody.getFileID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.node.app.service.contract.impl.infra;

import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_BYTECODE_EMPTY;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_DELETED;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_FILE_EMPTY;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_NEGATIVE_GAS;
Expand Down Expand Up @@ -295,6 +296,7 @@ private void assertValidCreation(@NonNull final ContractCreateTransactionBody bo

private Bytes initcodeFor(@NonNull final ContractCreateTransactionBody body) {
if (body.hasInitcode()) {
validateTrue(body.initcode().length() > 0, CONTRACT_BYTECODE_EMPTY);
return body.initcode();
} else {
final var initcode = fileStore.getFileLeaf(body.fileIDOrElse(FileID.DEFAULT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.hedera.hapi.node.base.ResponseCodeEnum.AUTORENEW_DURATION_NOT_IN_RANGE;
import static com.hedera.hapi.node.base.ResponseCodeEnum.BAD_ENCODING;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_BYTECODE_EMPTY;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_FILE_EMPTY;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_NEGATIVE_GAS;
import static com.hedera.hapi.node.base.ResponseCodeEnum.CONTRACT_NEGATIVE_VALUE;
Expand Down Expand Up @@ -366,6 +367,17 @@ void fromHapiCreationValidatesInitcodeNotEmpty() {
.autoRenewPeriod(SOME_DURATION));
}

@Test
void fromHapiCreationValidatesInlineInitcodeNotEmpty() {
assertCreateFailsWith(CONTRACT_BYTECODE_EMPTY, b -> b.memo(SOME_MEMO)
.adminKey(AN_ED25519_KEY)
.initcode(Bytes.EMPTY)
.autoRenewAccountId(NON_SYSTEM_ACCOUNT_ID)
.gas(DEFAULT_CONTRACTS_CONFIG.maxGasPerSec())
.proxyAccountID(AccountID.DEFAULT)
.autoRenewPeriod(SOME_DURATION));
}

@Test
void fromHapiCreationTranslatesHexParsingException() {
given(fileStore.getFileLeaf(INITCODE_FILE_ID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static com.hedera.services.bdd.suites.contract.Utils.FunctionType.FUNCTION;
import static com.hedera.services.bdd.suites.contract.Utils.getABIFor;
import static com.hedera.services.bdd.suites.contract.hapi.ContractUpdateSuite.ADMIN_KEY;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_BYTECODE_EMPTY;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_REVERT_EXECUTED;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.ERROR_DECODING_BYTESTRING;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INSUFFICIENT_GAS;
Expand Down Expand Up @@ -114,6 +115,8 @@ public class ContractCreateSuite extends HapiSuite {
public static final String EMPTY_CONSTRUCTOR_CONTRACT = "EmptyConstructor";
public static final String PARENT_INFO = "parentInfo";
private static final String PAYER = "payer";
private static final String PATTERN = "0.0.%d";

private static final Logger log = LogManager.getLogger(ContractCreateSuite.class);

public static void main(String... args) {
Expand Down Expand Up @@ -142,7 +145,8 @@ public List<HapiSpec> getSpecsInSuite() {
blockTimestampChangesWithinFewSeconds(),
contractWithAutoRenewNeedSignatures(),
newAccountsCanUsePureContractIdKey(),
createContractWithStakingFields());
createContractWithStakingFields(),
disallowCreationsOfEmptyInitCode());
}

@Override
Expand Down Expand Up @@ -221,6 +225,21 @@ private HapiSpec insufficientPayerBalanceUponCreation() {
.hasPrecheck(INSUFFICIENT_PAYER_BALANCE));
}

@HapiTest
private HapiSpec disallowCreationsOfEmptyInitCode() {
final var contract = "EmptyContract";
return defaultHapiSpec("allowCreationsOfEmptyContract")
.given(
newKeyNamed(ADMIN_KEY),
contractCreate(contract)
.adminKey(ADMIN_KEY)
.entityMemo("Empty Contract")
.inlineInitCode(ByteString.EMPTY)
.hasKnownStatus(CONTRACT_BYTECODE_EMPTY))
.when()
.then();
}

@HapiTest
HapiSpec cannotSendToNonExistentAccount() {
final var contract = "Multipurpose";
Expand Down
Loading