From 30fa565dca87f0021b4e223cb4279c619a4cf032 Mon Sep 17 00:00:00 2001 From: anthonybuckle Date: Wed, 26 Feb 2020 01:14:18 -0330 Subject: [PATCH] BESU-146 - check if success and return errorResponse otherwise (#424) Signed-off-by: Anthony Buckle Co-authored-by: CJ Hare --- .../internal/methods/EthEstimateGas.java | 6 ++++-- .../internal/methods/EthEstimateGasTest.java | 20 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGas.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGas.java index ff4c15c034..fb9a7d4e63 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGas.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGas.java @@ -84,8 +84,10 @@ public class EthEstimateGas implements JsonRpcMethod { private Function gasEstimateResponse( final JsonRpcRequestContext request) { return result -> - new JsonRpcSuccessResponse( - request.getRequest().getId(), Quantity.create(result.getGasEstimate())); + result.isSuccessful() + ? new JsonRpcSuccessResponse( + request.getRequest().getId(), Quantity.create(result.getGasEstimate())) + : null; } private JsonRpcErrorResponse errorResponse(final JsonRpcRequestContext request) { diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGasTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGasTest.java index 37c7eb9450..c166972397 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGasTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGasTest.java @@ -83,9 +83,9 @@ public class EthEstimateGasTest { } @Test - public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResult() { + public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResultSuccess() { final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter()); - mockTransientProcessorResultGasEstimate(1L); + mockTransientProcessorResultGasEstimate(1L, true); final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(1L)); @@ -93,11 +93,25 @@ public class EthEstimateGasTest { .isEqualToComparingFieldByField(expectedResponse); } - private void mockTransientProcessorResultGasEstimate(final long gasEstimate) { + @Test + public void shouldReturnGasEstimateErrorWhenTransientTransactionProcessorReturnsResultFailure() { + final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter()); + mockTransientProcessorResultGasEstimate(1L, false); + + final JsonRpcResponse expectedResponse = + new JsonRpcErrorResponse(null, JsonRpcError.INTERNAL_ERROR); + + Assertions.assertThat(method.response(request)) + .isEqualToComparingFieldByField(expectedResponse); + } + + private void mockTransientProcessorResultGasEstimate( + final long gasEstimate, final boolean isSuccessful) { final TransactionSimulatorResult result = mock(TransactionSimulatorResult.class); when(result.getGasEstimate()).thenReturn(gasEstimate); when(transactionSimulator.process(eq(modifiedCallParameter()), eq(1L))) .thenReturn(Optional.of(result)); + when(result.isSuccessful()).thenReturn(isSuccessful); } private JsonCallParameter callParameter() {