diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java index a673e1df7b1..118de400bff 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java @@ -23,6 +23,8 @@ import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.api.ApiConfiguration; import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; import org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.TransactionLocation; @@ -905,14 +907,17 @@ private List matchingLogsCached( public List matchingLogs( final Hash blockHash, final LogsQuery query, final Supplier isQueryAlive) { try { - final Optional blockHeader = getBlockHeader(blockHash, isQueryAlive); - if (blockHeader.isEmpty()) { - return Collections.emptyList(); - } + final BlockHeader blockHeader = + getBlockHeader(blockHash, isQueryAlive) + .orElseThrow( + () -> + new InvalidJsonRpcParameters( + "Unknown block hash", RpcErrorType.BLOCK_NOT_FOUND)); + // receipts and transactions should exist if the header exists, so throwing is ok. final List receipts = getReceipts(blockHash, isQueryAlive); final List transactions = getTransactions(blockHash, isQueryAlive); - final long number = blockHeader.get().getNumber(); + final long number = blockHeader.getNumber(); final boolean removed = getRemoved(blockHash, isQueryAlive); final AtomicInteger logIndexOffset = new AtomicInteger(); @@ -939,6 +944,8 @@ public List matchingLogs( .flatMap(Collection::stream) .filter(query::matches) .collect(Collectors.toList()); + } catch (final InvalidJsonRpcParameters e) { + throw e; } catch (final Exception e) { throw new RuntimeException(e); } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java index e9235f38d4b..9a7a5088f64 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java @@ -18,10 +18,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain; import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive; +import static org.junit.jupiter.api.Assertions.fail; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; @@ -402,12 +405,19 @@ public void logsShouldBeFlaggedAsRemovedWhenBlockIsNotInCanonicalChain() { } @Test - public void matchingLogsShouldReturnAnEmptyListWhenGivenAnInvalidBlockHash() { + public void matchingLogsShouldThrowWhenGivenAnInvalidBlockHash() { final BlockchainWithData data = setupBlockchain(3); final BlockchainQueries queries = data.blockchainQueries; - List logs = - queries.matchingLogs(Hash.ZERO, new LogsQuery.Builder().build(), () -> true); - assertThat(logs).isEmpty(); + + try { + queries.matchingLogs(Hash.ZERO, new LogsQuery.Builder().build(), () -> true); + } catch (final InvalidJsonRpcParameters e) { + assertThat(e.getMessage()).isEqualTo("Unknown block hash"); + assertThat(e.getRpcErrorType()).isEqualTo(RpcErrorType.BLOCK_NOT_FOUND); + return; + } + + fail("Expected InvalidJsonRpcParameters exception"); } @Test diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_getLogs_blockhash_missingBlockHash.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_getLogs_blockhash_missingBlockHash.json index e283d5a0038..57b61143803 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_getLogs_blockhash_missingBlockHash.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/eth/eth_getLogs_blockhash_missingBlockHash.json @@ -10,9 +10,12 @@ }] }, "response": { - "jsonrpc": "2.0", - "id": 406, - "result" : [ ] + "jsonrpc" : "2.0", + "id" : 406, + "error" : { + "code": -32000, + "message": "Block not found" + } }, "statusCode": 200 } \ No newline at end of file