BESU-146 - check if success and return errorResponse otherwise (#424)

Signed-off-by: Anthony Buckle <anthonybuckle@gmail.com>

Co-authored-by: CJ Hare <CjHare@users.noreply.github.com>
pull/431/head
anthonybuckle 5 years ago committed by GitHub
parent bfc40be32e
commit 30fa565dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGas.java
  2. 20
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthEstimateGasTest.java

@ -84,8 +84,10 @@ public class EthEstimateGas implements JsonRpcMethod {
private Function<TransactionSimulatorResult, JsonRpcResponse> gasEstimateResponse( private Function<TransactionSimulatorResult, JsonRpcResponse> gasEstimateResponse(
final JsonRpcRequestContext request) { final JsonRpcRequestContext request) {
return result -> return result ->
new JsonRpcSuccessResponse( result.isSuccessful()
request.getRequest().getId(), Quantity.create(result.getGasEstimate())); ? new JsonRpcSuccessResponse(
request.getRequest().getId(), Quantity.create(result.getGasEstimate()))
: null;
} }
private JsonRpcErrorResponse errorResponse(final JsonRpcRequestContext request) { private JsonRpcErrorResponse errorResponse(final JsonRpcRequestContext request) {

@ -83,9 +83,9 @@ public class EthEstimateGasTest {
} }
@Test @Test
public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResult() { public void shouldReturnGasEstimateWhenTransientTransactionProcessorReturnsResultSuccess() {
final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter()); final JsonRpcRequestContext request = ethEstimateGasRequest(callParameter());
mockTransientProcessorResultGasEstimate(1L); mockTransientProcessorResultGasEstimate(1L, true);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(1L)); final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(1L));
@ -93,11 +93,25 @@ public class EthEstimateGasTest {
.isEqualToComparingFieldByField(expectedResponse); .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); final TransactionSimulatorResult result = mock(TransactionSimulatorResult.class);
when(result.getGasEstimate()).thenReturn(gasEstimate); when(result.getGasEstimate()).thenReturn(gasEstimate);
when(transactionSimulator.process(eq(modifiedCallParameter()), eq(1L))) when(transactionSimulator.process(eq(modifiedCallParameter()), eq(1L)))
.thenReturn(Optional.of(result)); .thenReturn(Optional.of(result));
when(result.isSuccessful()).thenReturn(isSuccessful);
} }
private JsonCallParameter callParameter() { private JsonCallParameter callParameter() {

Loading…
Cancel
Save