Skip to content

Commit 32035e7

Browse files
committed
Return error response for unknown transaction hash in debug_traceTransaction
Previously, debug_traceTransaction returned a success response with null result when the transaction hash was not found. Now it returns a proper JSON-RPC error response with code -32000 and message Transaction not found, aligning with the debug RPC endpoints specification. Refs: besu-eth#10115 Signed-off-by: Vivek Singh Solanki <viveksolanki0509@gmail.com>
1 parent 8f6a654 commit 32035e7

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceTransaction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams;
2323
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
2424
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer;
25+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
2526
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
2627
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
2728
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
@@ -91,7 +92,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
9192
return new JsonRpcSuccessResponse(
9293
requestContext.getRequest().getId(), debugResult.getResult());
9394
} else {
94-
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), null);
95+
return new JsonRpcErrorResponse(
96+
requestContext.getRequest().getId(), RpcErrorType.TRANSACTION_NOT_FOUND);
9597
}
9698
}
9799

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceTransactionTest.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
2929
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
3030
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer;
31+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
3132
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
3233
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
34+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
3335
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.OpCodeLoggerTracerResult;
3436
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.StructLog;
3537
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
@@ -181,48 +183,16 @@ public void shouldTraceTheTransactionUsingTheTransactionTracer() {
181183

182184
@Test
183185
public void shouldNotTraceTheTransactionIfNotFound() {
184-
final Map<String, Boolean> map = new HashMap<>();
185-
map.put("disableStorage", true);
186-
final Object[] params = new Object[] {transactionHash, map};
186+
final Object[] params = new Object[] {transactionHash};
187187
final JsonRpcRequestContext request =
188188
new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceTransaction", params));
189-
final TransactionProcessingResult result = mock(TransactionProcessingResult.class);
190189

191-
final TraceFrame traceFrame =
192-
TraceFrame.builder()
193-
.setPc(12)
194-
.setOpcode("NONE")
195-
.setOpcodeNumber(Integer.MAX_VALUE)
196-
.setGasRemaining(45L)
197-
.setGasCost(OptionalLong.of(56L))
198-
.setGasRefund(0L)
199-
.setDepth(2)
200-
.setRecipient(null)
201-
.setValue(Wei.ZERO)
202-
.setInputData(Bytes.EMPTY)
203-
.setOutputData(Bytes.EMPTY)
204-
.setWorldUpdater(null)
205-
.setRevertReason(Optional.of(Bytes.fromHexString("0x1122334455667788")))
206-
.setStackItemsProduced(0)
207-
.setVirtualOperation(false)
208-
.build();
209-
final List<TraceFrame> traceFrames = Collections.singletonList(traceFrame);
210-
final TransactionTrace transactionTrace =
211-
new TransactionTrace(transaction, result, traceFrames);
212-
when(transaction.getGasLimit()).thenReturn(100L);
213-
when(result.getGasRemaining()).thenReturn(27L);
214-
when(result.getOutput()).thenReturn(Bytes.fromHexString("1234"));
215-
when(blockchainQueries.headBlockNumber()).thenReturn(12L);
216190
when(blockchainQueries.transactionByHash(transactionHash)).thenReturn(Optional.empty());
217-
when(transactionTracer.traceTransaction(
218-
any(Tracer.TraceableState.class),
219-
eq(blockHash),
220-
eq(transactionHash),
221-
any(DebugOperationTracer.class)))
222-
.thenReturn(Optional.of(transactionTrace));
223-
final JsonRpcSuccessResponse response =
224-
(JsonRpcSuccessResponse) debugTraceTransaction.response(request);
225191

226-
assertThat(response.getResult()).isNull();
192+
final JsonRpcResponse response = debugTraceTransaction.response(request);
193+
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class);
194+
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
195+
assertThat(errorResponse.getErrorType())
196+
.isEqualByComparingTo(RpcErrorType.TRANSACTION_NOT_FOUND);
227197
}
228198
}

0 commit comments

Comments
 (0)