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

Add in equivalence tests for direct calls #7812

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 @@ -165,8 +166,7 @@ public SubscriptionResponse subscribeToTopicAndRetrieveMessages(

public CryptoAllowancesResponse getAccountCryptoAllowance(String accountId) {
log.debug("Verify account '{}''s crypto allowance is returned by Mirror Node", accountId);
return callRestEndpoint(
"/accounts/{accountId}/allowances/crypto", CryptoAllowancesResponse.class, accountId);
return callRestEndpoint("/accounts/{accountId}/allowances/crypto", CryptoAllowancesResponse.class, accountId);
}

public CryptoAllowancesResponse getAccountCryptoAllowanceBySpender(String accountId, String spenderId) {
Expand Down Expand Up @@ -210,8 +210,13 @@ public ContractResultsResponse getContractResultsById(String contractId) {

public ContractResult getContractResultByTransactionId(String transactionId) {
log.debug("Verify contract result '{}' is returned by Mirror Node", 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/{transactionId}", ContractResult.class, transactionId);
"/contracts/results/{contractId}/actions", ContractActionsResponse.class, transactionId);
}

public NetworkExchangeRateSetResponse getExchangeRates() {
Expand Down Expand Up @@ -248,8 +253,7 @@ public NetworkStakeResponse getNetworkStake() {

public Nft getNftInfo(String tokenId, long serialNumber) {
log.debug("Verify serial number '{}' for token '{}' is returned by Mirror Node", serialNumber, tokenId);
return callRestEndpoint(
"/tokens/{tokenId}/nfts/{serialNumber}", Nft.class, tokenId, serialNumber);
return callRestEndpoint("/tokens/{tokenId}/nfts/{serialNumber}", Nft.class, tokenId, serialNumber);
}

public NftTransactionHistory getNftTransactions(TokenId tokenId, Long serialNumber) {
Expand Down Expand Up @@ -290,10 +294,7 @@ public TokenRelationshipResponse getTokenRelationships(AccountId accountId, Toke
accountId,
tokenId);
return callRestEndpoint(
"/accounts/{accountId}/tokens?token.id={tokenId}",
TokenRelationshipResponse.class,
accountId,
tokenId);
"/accounts/{accountId}/tokens?token.id={tokenId}", TokenRelationshipResponse.class, accountId, tokenId);
}

public AccountBalanceTransactions getAccountDetailsUsingAlias(@NonNull AccountId accountId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@

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.ContractId;
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.tuweni.bytes.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -48,7 +57,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 +73,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 +97,58 @@ 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 ContractId to,
final DeployedContract deployedContract,
final String method,
final byte[] data,
final Hbar amount) {
if (NodeNameEnum.MIRROR.equals(node)) {
try {
var contractCallRequestBody = ModelBuilder.contractCallRequest()
.data(Strings.encode(ByteBuffer.wrap(data)))
.to(
to != null
? to.toSolidityAddress()
: 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));
} catch (Exception e) {
return new GeneralContractExecutionResponse(e.getMessage());
}
} else {
try {
final var gas = contractClient
.getSdkClient()
.getAcceptanceTestProperties()
.getFeatureProperties()
.getMaxContractFunctionGas();

var contractId = to != null ? to : deployedContract.contractId();
final var result = contractClient.executeContract(contractId, gas, method, data, amount);
final var txId = result.networkTransactionResponse().getTransactionIdStringNoCheckSum();
final var errorMessage = extractInternalCallErrorMessage(txId);
return new GeneralContractExecutionResponse(txId, result.networkTransactionResponse(), errorMessage);
} catch (Exception e) {
return new GeneralContractExecutionResponse(extractTransactionId(e.getMessage()), e.getMessage());
}
}
}

Expand All @@ -115,4 +169,32 @@ 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;
}

private static String extractTransactionId(String message) {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+)@(\\d+)\\.(\\d+)");
Matcher matcher = pattern.matcher(message);
if (matcher.find()) {
return matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3);
} else {
return "Not found";
}
}
}
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 GeneralContractExecutionResponse(String errorMessage) {
this(null, null, errorMessage, null);
}

public GeneralContractExecutionResponse(String transactionId, String errorMessage) {
this(transactionId, null, errorMessage, null);
}
}
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 All @@ -48,6 +49,7 @@
import lombok.CustomLog;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.tuweni.bytes.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -191,8 +193,24 @@ 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 ContractId to, final Hbar amount) {
return networkAdapter.contractsCall(node, false, from, to, null, "", Bytes.EMPTY.toArrayUnsafe(), amount);
}

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, null, getContract(contractResource), method, data, amount);
}

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