diff --git a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java index 1af54573ebe..c2dcbb2d2b2 100644 --- a/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java +++ b/rskj-core/src/main/java/co/rsk/rpc/modules/eth/EthModule.java @@ -146,7 +146,7 @@ public String call(CallArgumentsParam argsParam, BlockIdentifierParam bnOrId) { } else { res = callConstant(args, block); } - handleTransactionRevert(res); + handleTransactionRevertIfHappens(res); hReturn = HexUtils.toUnformattedJsonHex(res.getHReturn()); return hReturn; @@ -181,7 +181,7 @@ public String estimateGas(CallArgumentsParam args, @Nonnull BlockIdentifierParam ); ProgramResult res = executor.getResult(); - handleTransactionRevert(res); + handleTransactionRevertIfHappens(res); estimation = internalEstimateGas(executor.getResult()); @@ -348,7 +348,7 @@ private ProgramResult callConstantWithState(CallArguments args, Block executionB ); } - private void handleTransactionRevert(ProgramResult res) { + private void handleTransactionRevertIfHappens(ProgramResult res) { if (res.isRevert()) { Pair programRevert = decodeProgramRevert(res); String revertReason = programRevert.getLeft(); diff --git a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java index 77c4f73a2d2..07b1db1adad 100644 --- a/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java +++ b/rskj-core/src/test/java/co/rsk/rpc/modules/eth/EthModuleTest.java @@ -605,6 +605,49 @@ void whenExecuteEstimateGasWithDataParameter_callExecutorWithData() { assertArrayEquals(HexUtils.strHexOrStrNumberToByteArray(args.getData()), dataCaptor.getValue()); } + @Test + void testwhenExecuteEstimateGasWithDataParameter_callExecutorWithData() { + CallArguments args = new CallArguments(); + Block block = mock(Block.class); + ExecutionBlockRetriever.Result blockResult = mock(ExecutionBlockRetriever.Result.class); + when(blockResult.getBlock()).thenReturn(block); + ExecutionBlockRetriever retriever = mock(ExecutionBlockRetriever.class); + when(retriever.retrieveExecutionBlock("latest")).thenReturn(blockResult); + Blockchain blockchain = mock(Blockchain.class); + + ProgramResult executorResult = mock(ProgramResult.class); + when(executorResult.isRevert()).thenReturn(true); + TransactionExecutor transactionExecutor = mock(TransactionExecutor.class); + when(transactionExecutor.getResult()) + .thenReturn(executorResult); + + ReversibleTransactionExecutor reversibleTransactionExecutor = mock(ReversibleTransactionExecutor.class); + when(reversibleTransactionExecutor.estimateGas(eq(block), any(), any(), any(), any(), any(), any(), any(), any())) + .thenReturn(transactionExecutor); + + EthModule eth = new EthModule( + null, + Constants.REGTEST_CHAIN_ID, + blockchain, + null, + reversibleTransactionExecutor, + retriever, + mock(RepositoryLocator.class), + null, + null, + new BridgeSupportFactory( + null, null, null, signatureCache), + config.getGasEstimationCap(), + config.getCallGasCap()); + + CallArgumentsParam callArgumentsParam = TransactionFactoryHelper.toCallArgumentsParam(args); + + RskJsonRpcRequestException exception = assertThrows(RskJsonRpcRequestException.class, () -> { + eth.estimateGas(callArgumentsParam, new BlockIdentifierParam("latest")); + }); + assertThat(exception.getMessage(), Matchers.containsString("transaction reverted")); + } + @Test void whenExecuteEstimateGasWithInputParameter_callExecutorWithInput() { CallArguments args = new CallArguments();