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

07193 Add in equivalence tests for internal calls #7901

Draft
wants to merge 6 commits into
base: 7193-add-in-equivalence-tests-for-system-accounts
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ protected InputStream getResourceAsStream(String resourcePath) throws IOExceptio
}

protected byte[] encodeDataToByteArray(ContractResource resource, SelectorInterface method, Object... args) {
return encodeDataToByteArray(resource, method.getSelector(), args);
}

protected byte[] encodeDataToByteArray(ContractResource resource, String method, Object... args) {
String json;
try (var in = getResourceAsStream(resource.getPath())) {
json = getAbiFunctionAsJsonString(readCompiledArtifact(in), method.getSelector());
json = getAbiFunctionAsJsonString(readCompiledArtifact(in), method);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hedera.mirror.rest.model.AccountBalanceTransactions;
import com.hedera.mirror.rest.model.AccountInfo;
import com.hedera.mirror.rest.model.BlocksResponse;
import com.hedera.mirror.rest.model.ContractActionsResponse;
import com.hedera.mirror.rest.model.ContractCallRequest;
import com.hedera.mirror.rest.model.ContractCallResponse;
import com.hedera.mirror.rest.model.ContractResponse;
Expand Down Expand Up @@ -214,6 +215,11 @@ public ContractResult getContractResultByTransactionId(String transactionId) {
return callRestEndpoint("/contracts/results/{transactionId}", ContractResult.class, transactionId);
}

public ContractActionsResponse getContractActions(String transactionId) {
log.debug("Verify contract actions '{}' is returned by Mirror Node", transactionId);
return callRestEndpoint("/contracts/results/{contractId}/actions", ContractActionsResponse.class, transactionId);
}

public NetworkExchangeRateSetResponse getExchangeRates() {
log.debug("Get exchange rates by Mirror Node");
return callRestEndpoint("/network/exchangerate", NetworkExchangeRateSetResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@

package com.hedera.mirror.test.e2e.acceptance.client;

import static com.hedera.mirror.rest.model.ContractAction.ResultDataTypeEnum.OUTPUT;
import static com.hedera.mirror.test.e2e.acceptance.util.TestUtil.hexToAscii;
import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;

import com.esaulpaugh.headlong.abi.TupleType;
import com.esaulpaugh.headlong.util.Strings;
import com.hedera.hashgraph.sdk.ContractFunctionResult;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.mirror.rest.model.ContractCallResponse;
import com.hedera.mirror.test.e2e.acceptance.client.ContractClient.NodeNameEnum;
import com.hedera.mirror.test.e2e.acceptance.response.GeneralContractExecutionResponse;
import com.hedera.mirror.test.e2e.acceptance.steps.AbstractFeature.DeployedContract;
import com.hedera.mirror.test.e2e.acceptance.steps.AbstractFeature.SelectorInterface;
import com.hedera.mirror.test.e2e.acceptance.util.ContractCallResponseWrapper;
import com.hedera.mirror.test.e2e.acceptance.util.ModelBuilder;
import jakarta.inject.Named;
import java.nio.ByteBuffer;
import org.apache.commons.lang3.StringUtils;
import org.apache.tuweni.bytes.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -48,7 +54,7 @@ public class NetworkAdapter extends EncoderDecoderFacade {
public static final TupleType BIG_INTEGER_TUPLE = TupleType.parse(UINT256);
public static final TupleType BYTES_TUPLE = TupleType.parse(BYTES);

public ContractCallResponse contractsCall(
public ContractCallResponseWrapper contractsCall(
final NodeNameEnum node,
boolean isEstimate,
final String from,
Expand All @@ -64,11 +70,11 @@ public ContractCallResponse contractsCall(
.from(from.isEmpty() ? contractClient.getClientAddress() : from)
.to(deployedContract.contractId().toSolidityAddress());

return mirrorClient.contractsCall(contractCallRequestBody);
return ContractCallResponseWrapper.of(mirrorClient.contractsCall(contractCallRequestBody));
} catch (Exception e) {
ContractCallResponse contractCallResponse = new ContractCallResponse();
contractCallResponse.setResult(e.getMessage());
return contractCallResponse;
return ContractCallResponseWrapper.of(contractCallResponse);
}
} else {
final var gas = contractClient
Expand All @@ -88,13 +94,45 @@ public ContractCallResponse contractsCall(
if (e instanceof PrecheckStatusException pse) {
final var exceptionReason = pse.status.toString();
contractCallResponse.setResult(exceptionReason);
return contractCallResponse;
return ContractCallResponseWrapper.of(contractCallResponse);
}

contractCallResponse.setResult(e.getMessage());
}

return contractCallResponse;
return ContractCallResponseWrapper.of(contractCallResponse);
}
}

public GeneralContractExecutionResponse contractsCall(
final NodeNameEnum node,
boolean isEstimate,
final String from,
final DeployedContract deployedContract,
final String method,
final byte[] data,
final Hbar amount) {
if (NodeNameEnum.MIRROR.equals(node)) {
var contractCallRequestBody = ModelBuilder.contractCallRequest()
.data(Strings.encode(ByteBuffer.wrap(data)))
.to(deployedContract.contractId().toSolidityAddress())
.from(from.isEmpty() ? contractClient.getClientAddress() : from)
.estimate(isEstimate)
.value(amount != null ? amount.toTinybars() : 0);

var response = mirrorClient.contractsCall(contractCallRequestBody);
return new GeneralContractExecutionResponse(ContractCallResponseWrapper.of(response));
} else {
final var gas = contractClient
.getSdkClient()
.getAcceptanceTestProperties()
.getFeatureProperties()
.getMaxContractFunctionGas();

final var result = contractClient.executeContract(deployedContract.contractId(), gas, method, data, amount);
final var txId = result.networkTransactionResponse().getTransactionIdStringNoCheckSum();
final var errorMessage = extractInternalCallErrorMessage(txId);
return new GeneralContractExecutionResponse(txId, result.networkTransactionResponse(), errorMessage);
}
}

Expand All @@ -115,4 +153,22 @@ private ContractCallResponse convertConsensusResponse(
}
return contractCallResponse;
}

private String extractInternalCallErrorMessage(String transactionId) throws IllegalArgumentException {
var transactions = mirrorClient.getTransactions(transactionId).getTransactions();
if (transactions == null || transactions.size() < 2) {
var actions = mirrorClient.getContractActions(transactionId).getActions();
if (actions == null || actions.size() < 2) {
throw new IllegalArgumentException("The actions list must contain at least two elements.");
}
if (actions.get(1).getResultDataType() != OUTPUT) {
String hexString = actions.get(1).getResultData();
return hexToAscii(hexString.replace("0x", ""));
}
return null;
}

var resultMessage = transactions.get(1).getResult();
return resultMessage.equalsIgnoreCase("success") ? null : resultMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.test.e2e.acceptance.response;

import com.hedera.mirror.test.e2e.acceptance.util.ContractCallResponseWrapper;

/**
* This is a class that represents a response either from the consensus node (with initialized transactionId,
* networkResponse, errorMessage) or from the mirror node (with initialized contractCallResponse).
* This is needed for the NetworkAdapter logic.
*/
public record GeneralContractExecutionResponse(
String transactionId,
NetworkTransactionResponse networkResponse,
String errorMessage,
ContractCallResponseWrapper contractCallResponse) {
public GeneralContractExecutionResponse(ContractCallResponseWrapper contractCallResponse) {
this(null, null, null, contractCallResponse);
}

public GeneralContractExecutionResponse(
String transactionId, NetworkTransactionResponse networkResponse, String errorMessage) {
this(transactionId, networkResponse, errorMessage, null);
}

public String getMirrorNodeResponseAsString() {
return contractCallResponse.getResult().substring(2);
}

public byte[] getMirrorNodeResponseAsBytes() {
return contractCallResponse.getResultAsBytes().toArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.hedera.mirror.test.e2e.acceptance.client.MirrorNodeClient;
import com.hedera.mirror.test.e2e.acceptance.client.NetworkAdapter;
import com.hedera.mirror.test.e2e.acceptance.props.CompiledSolidityArtifact;
import com.hedera.mirror.test.e2e.acceptance.response.GeneralContractExecutionResponse;
import com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse;
import com.hedera.mirror.test.e2e.acceptance.util.ContractCallResponseWrapper;
import com.hedera.mirror.test.e2e.acceptance.util.ModelBuilder;
Expand Down Expand Up @@ -191,8 +192,18 @@ protected ContractCallResponseWrapper callContract(
final SelectorInterface method,
final String data,
final TupleType returnTupleType) {
return ContractCallResponseWrapper.of(networkAdapter.contractsCall(
node, false, from, getContract(contractResource), method, data, returnTupleType));
return networkAdapter.contractsCall(
node, false, from, getContract(contractResource), method, data, returnTupleType);
}

protected GeneralContractExecutionResponse callContract(
final NodeNameEnum node,
final String from,
final ContractResource contractResource,
final String method,
final byte[] data,
final Hbar amount) {
return networkAdapter.contractsCall(node, false, from, getContract(contractResource), method, data, amount);
}

protected ContractCallResponseWrapper estimateContract(String data, String contractAddress) {
Expand Down