Skip to content

Commit 726250d

Browse files
committed
fix: return error response for genesis block in debug_traceBlockByNumber
Genesis block (block 0) has no transactions to trace. Instead of returning null, return a JSON-RPC error matching the debug trace spec. Relates to #10115 Signed-off-by: Vivek Singh Solanki <viveksolanki0509@gmail.com>
1 parent cd44ab0 commit 726250d

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams;
2525
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
2626
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
27+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
2728
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
2829
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult;
2930
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
@@ -88,6 +89,11 @@ protected BlockParameter blockParameter(final JsonRpcRequestContext request) {
8889
protected Object resultByBlockNumber(
8990
final JsonRpcRequestContext request, final long blockNumber) {
9091

92+
if (blockNumber == 0L) {
93+
return new JsonRpcErrorResponse(
94+
request.getRequest().getId(), RpcErrorType.GENESIS_BLOCK_NOT_TRACEABLE);
95+
}
96+
9197
final TraceOptions traceOptions;
9298
try {
9399
traceOptions =

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/RpcErrorType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public enum RpcErrorType implements RpcMethodError {
210210
// Debug failures
211211
BLOCK_NOT_FOUND(-32000, "Block not found"),
212212
PARENT_BLOCK_NOT_FOUND(-32000, "Parent block not found"),
213+
GENESIS_BLOCK_NOT_TRACEABLE(-32000, "genesis is not traceable"),
213214

214215
// Permissioning/Account allowlist errors
215216
ACCOUNT_ALLOWLIST_NOT_ENABLED(-32000, "Account allowlist has not been enabled"),

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
2727
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
2828
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
29+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
2930
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
3031
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
32+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
3133
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.OpCodeLoggerTracerResult;
3234
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
3335
import org.hyperledger.besu.ethereum.chain.Blockchain;
@@ -117,6 +119,19 @@ private Collection<OpCodeLoggerTracerResult> getResult(final JsonRpcSuccessRespo
117119
return (Collection<OpCodeLoggerTracerResult>) response.getResult();
118120
}
119121

122+
@Test
123+
public void shouldReturnErrorForGenesisBlock() {
124+
final Object[] params = new Object[] {"0x0"};
125+
final JsonRpcRequestContext request =
126+
new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceBlockByNumber", params));
127+
128+
final JsonRpcResponse jsonRpcResponse = debugTraceBlockByNumber.response(request);
129+
assertThat(jsonRpcResponse).isInstanceOf(JsonRpcErrorResponse.class);
130+
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) jsonRpcResponse;
131+
assertThat(errorResponse.getErrorType())
132+
.isEqualByComparingTo(RpcErrorType.GENESIS_BLOCK_NOT_TRACEABLE);
133+
}
134+
120135
@Test
121136
public void shouldHandleInvalidParametersGracefully() {
122137
final Object[] invalidParams = new Object[] {"invalid-block-number"};

0 commit comments

Comments
 (0)