Skip to content

Commit

Permalink
Fix invalid error codes on some JSON RPC apis (hyperledger#2138)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
  • Loading branch information
matkt authored May 8, 2021
1 parent 9c404d9 commit 1e2411c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

- Fixed issue in discv5 where nonce was incorrectly reused. [\#2075](https://github.com/hyperledger/besu/pull/2075)
- Fixed issues in debug_standardTraceBadBlockToFile and debug_standardTraceBlockToFile. [\#2120](https://github.com/hyperledger/besu/pull/2120)

- Fixed invalid error code in several JSON RPC methods when the requested block is not in the range. [\#2138](https://github.com/hyperledger/besu/pull/2138)

## 21.1.5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ protected Object handleParamTypes(final JsonRpcRequestContext requestContext) {
result = pendingResult(requestContext);
} else if (blockParameterOrBlockHash.isNumeric() || blockParameterOrBlockHash.isEarliest()) {
OptionalLong blockNumber = blockParameterOrBlockHash.getNumber();
if (blockNumber.isEmpty()
|| blockNumber.getAsLong() < 0
|| blockNumber.getAsLong() > getBlockchainQueries().headBlockNumber()) {
if (blockNumber.isEmpty() || blockNumber.getAsLong() < 0) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
} else if (blockNumber.getAsLong() > getBlockchainQueries().headBlockNumber()) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.BLOCK_NOT_FOUND);
}

result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.BLOCK_NOT_FOUND;

import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
Expand Down Expand Up @@ -84,7 +86,7 @@ protected Object resultByBlockHash(final JsonRpcRequestContext request, final Ha
new JsonRpcErrorResponse(
request.getRequest().getId(),
JsonRpcErrorConverter.convertTransactionInvalidReason(reason))))
.orElse(errorResponse(request, JsonRpcError.INTERNAL_ERROR));
.orElse(errorResponse(request, BLOCK_NOT_FOUND));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INTERNAL_ERROR;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.BLOCK_NOT_FOUND;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -102,7 +102,7 @@ public void shouldThrowInvalidJsonRpcParametersExceptionWhenMissingToField() {
@Test
public void shouldReturnInternalErrorWhenProcessorReturnsEmpty() {
final JsonRpcRequestContext request = ethCallRequest(callParameter(), "latest");
final JsonRpcResponse expectedResponse = new JsonRpcErrorResponse(null, INTERNAL_ERROR);
final JsonRpcResponse expectedResponse = new JsonRpcErrorResponse(null, BLOCK_NOT_FOUND);

when(blockchainQueries.getBlockchain()).thenReturn(blockchain);
when(blockchainQueries.getBlockchain().getChainHead()).thenReturn(chainHead);
Expand Down Expand Up @@ -189,6 +189,18 @@ public void shouldUseCorrectBlockNumberWhenSpecified() {
verify(transactionSimulator).process(any(), any(), any(), any());
}

@Test
public void shouldReturnBlockNotFoundWhenInvalidBlockNumberSpecified() {
final JsonRpcRequestContext request = ethCallRequest(callParameter(), Quantity.create(33L));
when(blockchainQueries.headBlockNumber()).thenReturn(14L);
final JsonRpcResponse expectedResponse = new JsonRpcErrorResponse(null, BLOCK_NOT_FOUND);

JsonRpcResponse response = method.response(request);
assertThat(response).usingRecursiveComparison().isEqualTo(expectedResponse);

verify(blockchainQueries).headBlockNumber();
}

private JsonCallParameter callParameter() {
return new JsonCallParameter(
Address.fromHexString("0x0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"jsonrpc": "2.0",
"id": 28,
"error": {
"code": -32602,
"message": "Invalid params"
"code": -32000,
"message": "Block not found"
}
},
"statusCode": 400
"statusCode": 200
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"jsonrpc": "2.0",
"id": 13,
"error": {
"code": -32602,
"message": "Invalid params"
"code": -32000,
"message": "Block not found"
}
},
"statusCode": 400
"statusCode": 200
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"jsonrpc": "2.0",
"id": 28,
"error": {
"code": -32602,
"message": "Invalid params"
"code": -32000,
"message": "Block not found"
}
},
"statusCode": 400
"statusCode": 200
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"jsonrpc": "2.0",
"id": 337,
"error": {
"code": -32602,
"message": "Invalid params"
"code": -32000,
"message": "Block not found"
}
},
"statusCode": 400
"statusCode": 200
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"jsonrpc": "2.0",
"id": 487,
"error": {
"code": -32602,
"message": "Invalid params"
"code": -32000,
"message": "Block not found"
}
},
"statusCode": 400
"statusCode": 200
}

0 comments on commit 1e2411c

Please sign in to comment.