Skip to content

Commit

Permalink
Fix tests from ContractCreateSuite (#8713)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Gatsanoga <[email protected]>
  • Loading branch information
MiroslavGatsanoga authored Sep 19, 2023
1 parent 198d278 commit d1c5a53
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public void createContract(final long number, final long parentNumber, @Nullable
dispatchAndMarkCreation(
number,
synthAccountCreationFromHapi(
ContractID.newBuilder().contractNum(number).build(), evmAddress, impliedContractCreation));
ContractID.newBuilder().contractNum(number).build(), evmAddress, impliedContractCreation),
null);
}

/**
Expand All @@ -221,7 +222,8 @@ public void createContract(
dispatchAndMarkCreation(
number,
synthAccountCreationFromHapi(
ContractID.newBuilder().contractNum(number).build(), evmAddress, body));
ContractID.newBuilder().contractNum(number).build(), evmAddress, body),
body.autoRenewAccountId());
}

/**
Expand Down Expand Up @@ -289,7 +291,10 @@ public int getOriginalSlotsUsed(final long contractNumber) {
return 0;
}

private void dispatchAndMarkCreation(final long number, @NonNull final CryptoCreateTransactionBody body) {
private void dispatchAndMarkCreation(
final long number,
@NonNull final CryptoCreateTransactionBody body,
@Nullable final AccountID autoRenewAccountId) {
final var recordBuilder = context.dispatchChildTransaction(
TransactionBody.newBuilder().cryptoCreateAccount(body).build(),
CryptoCreateRecordBuilder.class,
Expand All @@ -302,6 +307,7 @@ private void dispatchAndMarkCreation(final long number, @NonNull final CryptoCre
// Then use the TokenService API to mark the created account as a contract
final var tokenServiceApi = context.serviceApi(TokenServiceApi.class);
final var accountId = AccountID.newBuilder().accountNum(number).build();
tokenServiceApi.markAsContract(accountId);

tokenServiceApi.markAsContract(accountId, autoRenewAccountId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public void handle(@NonNull final HandleContext context) throws HandleException
.contractCallResult(outcome.result())
.contractID(outcome.recipientIdIfCalled())
.status(outcome.status());

if (!outcome.isSuccess()) {
throw new HandleException(outcome.status());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void handle(@NonNull final HandleContext context) throws HandleException
.contractCreateResult(outcome.result())
.contractID(outcome.recipientIdIfCreated())
.status(outcome.status());

if (!outcome.isSuccess()) {
throw new HandleException(outcome.status());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.pbjLogsFrom;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.tuweniToPbjBytes;
import static java.util.Objects.requireNonNull;
import static org.hyperledger.besu.evm.frame.ExceptionalHaltReason.INSUFFICIENT_GAS;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.ContractID;
import com.hedera.hapi.node.base.ResponseCodeEnum;
import com.hedera.hapi.node.contract.ContractFunctionResult;
import com.hedera.hapi.streams.ContractStateChanges;
import com.hedera.node.app.hapi.utils.ethereum.EthTxData;
import com.hedera.node.app.service.contract.impl.exec.failure.CustomExceptionalHaltReason;
import com.hedera.node.app.service.contract.impl.state.RootProxyWorldUpdater;
import com.hedera.node.app.service.contract.impl.state.StorageAccesses;
import com.hedera.node.app.service.contract.impl.utils.ConversionUtils;
Expand Down Expand Up @@ -82,9 +84,9 @@ public ContractFunctionResult asProtoResultOf(@NonNull final RootProxyWorldUpdat
public ContractFunctionResult asProtoResultOf(
@Nullable final EthTxData ethTxData, @NonNull final RootProxyWorldUpdater updater) {
if (haltReason != null) {
throw new AssertionError("Not implemented");
return null;
} else if (revertReason != null) {
throw new AssertionError("Not implemented");
return null;
} else {
return withMaybeEthFields(asSuccessResultForCommitted(updater), ethTxData);
}
Expand Down Expand Up @@ -113,9 +115,19 @@ public ContractFunctionResult asQueryResultOf() {
*/
public ResponseCodeEnum finalStatus() {
if (haltReason != null) {
throw new AssertionError("Not implemented");
if (haltReason.equals(CustomExceptionalHaltReason.MISSING_ADDRESS.toString())) {
return ResponseCodeEnum.INVALID_SOLIDITY_ADDRESS;
} else if (haltReason.equals(INSUFFICIENT_GAS.toString())) {
return ResponseCodeEnum.INSUFFICIENT_GAS;
} else {
throw new AssertionError("Not implemented");
}
} else if (revertReason != null) {
throw new AssertionError("Not implemented");
if (revertReason.length() == 0) {
return ResponseCodeEnum.CONTRACT_REVERT_EXECUTED;
} else {
throw new AssertionError("Not implemented");
}
} else {
return SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void createContractWithParentDispatchesAsExpectedThenMarksCreated() {
.dispatchChildTransaction(
eq(synthTxn), eq(CryptoCreateRecordBuilder.class), any(Predicate.class), eq(A_NEW_ACCOUNT_ID));
verify(tokenServiceApi)
.markAsContract(AccountID.newBuilder().accountNum(666L).build());
.markAsContract(AccountID.newBuilder().accountNum(666L).build(), null);
}

@Test
Expand Down Expand Up @@ -271,7 +271,7 @@ void createContractWithBodyDispatchesThenMarksAsContract() {
.dispatchChildTransaction(
eq(synthTxn), eq(CryptoCreateRecordBuilder.class), any(Predicate.class), eq(A_NEW_ACCOUNT_ID));
verify(tokenServiceApi)
.markAsContract(AccountID.newBuilder().accountNum(666L).build());
.markAsContract(AccountID.newBuilder().accountNum(666L).build(), NON_SYSTEM_ACCOUNT_ID);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.given;

import com.hedera.hapi.node.base.ResponseCodeEnum;
import com.hedera.node.app.service.contract.impl.exec.failure.CustomExceptionalHaltReason;
import com.hedera.node.app.service.contract.impl.exec.utils.FrameUtils;
import com.hedera.node.app.service.contract.impl.hevm.HederaEvmTransactionResult;
import com.hedera.node.app.service.contract.impl.infra.StorageAccessTracker;
Expand Down Expand Up @@ -84,11 +86,27 @@ void setUp() {
@Test
void finalStatusFromHaltNotImplemented() {
given(frame.getGasPrice()).willReturn(WEI_NETWORK_GAS_PRICE);
given(frame.getExceptionalHaltReason()).willReturn(Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
given(frame.getExceptionalHaltReason()).willReturn(Optional.of(ExceptionalHaltReason.INVALID_OPERATION));
final var subject = HederaEvmTransactionResult.failureFrom(GAS_LIMIT / 2, SENDER_ID, frame);
assertThrows(AssertionError.class, subject::finalStatus);
}

@Test
void finalStatusFromInsufficientGasHaltImplemented() {
given(frame.getGasPrice()).willReturn(WEI_NETWORK_GAS_PRICE);
given(frame.getExceptionalHaltReason()).willReturn(Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
final var subject = HederaEvmTransactionResult.failureFrom(GAS_LIMIT / 2, SENDER_ID, frame);
assertEquals(ResponseCodeEnum.INSUFFICIENT_GAS, subject.finalStatus());
}

@Test
void finalStatusFromMissingAddressHaltImplemented() {
given(frame.getGasPrice()).willReturn(WEI_NETWORK_GAS_PRICE);
given(frame.getExceptionalHaltReason()).willReturn(Optional.of(CustomExceptionalHaltReason.MISSING_ADDRESS));
final var subject = HederaEvmTransactionResult.failureFrom(GAS_LIMIT / 2, SENDER_ID, frame);
assertEquals(ResponseCodeEnum.INVALID_SOLIDITY_ADDRESS, subject.finalStatus());
}

@Test
void finalStatusFromRevertNotImplemented() {
given(frame.getGasPrice()).willReturn(WEI_NETWORK_GAS_PRICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ public void assertValidStakingElection(
* {@inheritDoc}
*/
@Override
public void markAsContract(@NonNull final AccountID accountId) {
public void markAsContract(@NonNull final AccountID accountId, @Nullable AccountID autoRenewAccountId) {
requireNonNull(accountId);
final var accountAsContract = requireNonNull(store.get(accountId))
.copyBuilder()
.smartContract(true)
.autoRenewAccountId(autoRenewAccountId)
.build();
store.put(accountAsContract);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void createsExpectedContractWithAliasIfSet() {
accountStore.put(Account.newBuilder().accountId(CONTRACT_ACCOUNT_ID).build());

assertNull(accountStore.getContractById(CONTRACT_ID_BY_NUM));
subject.markAsContract(CONTRACT_ACCOUNT_ID);
subject.markAsContract(CONTRACT_ACCOUNT_ID, null);

assertEquals(1, accountStore.sizeOfAccountState());
assertNotNull(accountStore.getContractById(CONTRACT_ID_BY_NUM));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void assertValidStakingElection(
* Marks an account as a contract.
*
*/
void markAsContract(@NonNull AccountID accountId);
void markAsContract(@NonNull AccountID accountId, @Nullable AccountID autoRenewAccountId);

/**
* Finalizes a hollow account as a contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private HapiSpec insufficientPayerBalanceUponCreation() {
.hasPrecheck(INSUFFICIENT_PAYER_BALANCE));
}

@HapiTest
HapiSpec cannotSendToNonExistentAccount() {
final var contract = "Multipurpose";
Object[] donationArgs = new Object[] {666666L, "Hey, Ma!"};
Expand Down Expand Up @@ -374,6 +375,7 @@ private HapiSpec rejectsInvalidBytecode() {
.then(contractCreate(contract).hasKnownStatus(ERROR_DECODING_BYTESTRING));
}

@HapiTest
private HapiSpec revertsNonzeroBalance() {
return defaultHapiSpec("RevertsNonzeroBalance")
.given(uploadInitCode(EMPTY_CONSTRUCTOR_CONTRACT))
Expand Down Expand Up @@ -571,6 +573,7 @@ HapiSpec newAccountsCanUsePureContractIdKey() {
}));
}

@HapiTest
HapiSpec contractWithAutoRenewNeedSignatures() {
final var contract = "CreateTrivial";
final var autoRenewAccount = "autoRenewAccount";
Expand Down

0 comments on commit d1c5a53

Please sign in to comment.