From a686713f7ad91195199818e963ce65552d63fb60 Mon Sep 17 00:00:00 2001 From: Lucas Saldanha Date: Tue, 31 May 2022 14:19:57 +1200 Subject: [PATCH] Added unit test for EthGetTransactionByHash (#3908) * Added more unit tests for EthGetTransactionByHash Signed-off-by: Lucas Saldanha --- .../methods/EthGetTransactionByHashTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionByHashTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionByHashTest.java index 28aff6a1e1..f6dcddf07c 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionByHashTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionByHashTest.java @@ -15,14 +15,22 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionCompleteResult; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPendingResult; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; import org.hyperledger.besu.ethereum.eth.transactions.sorter.AbstractPendingTransactionsSorter; import org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter; @@ -33,6 +41,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import org.apache.tuweni.bytes.Bytes; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,6 +51,9 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class EthGetTransactionByHashTest { + private static final String VALID_TRANSACTION = + "0xf86d0485174876e800830222e0945aae326516b4f8fe08074b7e972e40a713048d62880de0b6b3a7640000801ba05d4e7998757264daab67df2ce6f7e7a0ae36910778a406ca73898c9899a32b9ea0674700d5c3d1d27f2e6b4469957dfd1a1c49bf92383d80717afc84eb05695d5b"; + @Mock private BlockchainQueries blockchainQueries; private EthGetTransactionByHash method; private final String JSON_RPC_VERSION = "2.0"; @@ -59,6 +71,91 @@ public class EthGetTransactionByHashTest { assertThat(method.getName()).isEqualTo(ETH_METHOD); } + @Test + public void shouldReturnErrorResponseIfMissingRequiredParameter() { + final JsonRpcRequest request = new JsonRpcRequest("2.0", method.getName(), new Object[] {}); + final JsonRpcRequestContext context = new JsonRpcRequestContext(request); + + final JsonRpcErrorResponse expectedResponse = + new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS); + + final JsonRpcResponse actualResponse = method.response(context); + + assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse); + } + + @Test + public void shouldReturnNullResultWhenTransactionDoesNotExist() { + final String transactionHash = + "0xf9ef5f0cf02685711cdf687b72d4754901729b942f4ea7f956e7fb206cae2f9e"; + when(pendingTransactions.getTransactionByHash(eq(Hash.fromHexString(transactionHash)))) + .thenReturn(Optional.empty()); + when(blockchainQueries.transactionByHash(eq(Hash.fromHexString(transactionHash)))) + .thenReturn(Optional.empty()); + + final JsonRpcRequest request = + new JsonRpcRequest("2.0", method.getName(), new Object[] {transactionHash}); + final JsonRpcRequestContext context = new JsonRpcRequestContext(request); + + final JsonRpcSuccessResponse expectedResponse = + new JsonRpcSuccessResponse(request.getId(), null); + + final JsonRpcResponse actualResponse = method.response(context); + + assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse); + } + + @Test + public void shouldReturnPendingTransactionWhenTransactionExistsAndIsPending() { + final org.hyperledger.besu.ethereum.core.Transaction transaction = + org.hyperledger.besu.ethereum.core.Transaction.readFrom( + Bytes.fromHexString(VALID_TRANSACTION)); + + when(pendingTransactions.getTransactionByHash(eq(transaction.getHash()))) + .thenReturn(Optional.of(transaction)); + verifyNoInteractions(blockchainQueries); + + final JsonRpcRequest request = + new JsonRpcRequest( + "2.0", method.getName(), new Object[] {transaction.getHash().toHexString()}); + final JsonRpcRequestContext context = new JsonRpcRequestContext(request); + + final JsonRpcSuccessResponse expectedResponse = + new JsonRpcSuccessResponse(request.getId(), new TransactionPendingResult(transaction)); + + final JsonRpcResponse actualResponse = method.response(context); + + assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse); + } + + @Test + public void shouldReturnCompleteTransactionWhenTransactionExistsInBlockchain() { + final org.hyperledger.besu.ethereum.core.Transaction transaction = + org.hyperledger.besu.ethereum.core.Transaction.readFrom( + Bytes.fromHexString(VALID_TRANSACTION)); + final TransactionWithMetadata transactionWithMetadata = + new TransactionWithMetadata(transaction, 1, Optional.empty(), Hash.ZERO, 0); + + when(pendingTransactions.getTransactionByHash(eq(transaction.getHash()))) + .thenReturn(Optional.empty()); + verifyNoMoreInteractions(pendingTransactions); + when(blockchainQueries.transactionByHash(eq(transaction.getHash()))) + .thenReturn(Optional.of(transactionWithMetadata)); + + final JsonRpcRequest request = + new JsonRpcRequest( + "2.0", method.getName(), new Object[] {transaction.getHash().toHexString()}); + final JsonRpcRequestContext context = new JsonRpcRequestContext(request); + + final JsonRpcSuccessResponse expectedResponse = + new JsonRpcSuccessResponse( + request.getId(), new TransactionCompleteResult(transactionWithMetadata)); + + final JsonRpcResponse actualResponse = method.response(context); + + assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse); + } + @Test public void validateResultSpec() {