Skip to content

Commit

Permalink
Return rpc error in eth_getLogs on unknown block hash
Browse files Browse the repository at this point in the history
Signed-off-by: jonson <[email protected]>
  • Loading branch information
jonson committed Dec 19, 2024
1 parent 8c451c1 commit 7020bfd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -905,14 +907,17 @@ private List<LogWithMetadata> matchingLogsCached(
public List<LogWithMetadata> matchingLogs(
final Hash blockHash, final LogsQuery query, final Supplier<Boolean> isQueryAlive) {
try {
final Optional<BlockHeader> 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<TransactionReceipt> receipts = getReceipts(blockHash, isQueryAlive);
final List<Transaction> 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();
Expand All @@ -939,6 +944,8 @@ public List<LogWithMetadata> matchingLogs(
.flatMap(Collection::stream)
.filter(query::matches)
.collect(Collectors.toList());
} catch (final InvalidJsonRpcParameters e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.assertThrows;

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;
Expand Down Expand Up @@ -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<LogWithMetadata> logs =
queries.matchingLogs(Hash.ZERO, new LogsQuery.Builder().build(), () -> true);
assertThat(logs).isEmpty();

var exception =
assertThrows(
InvalidJsonRpcParameters.class,
() -> {
queries.matchingLogs(Hash.ZERO, new LogsQuery.Builder().build(), () -> true);
});

assertThat(exception.getMessage()).isEqualTo("Unknown block hash");
assertThat(exception.getRpcErrorType()).isEqualTo(RpcErrorType.BLOCK_NOT_FOUND);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7020bfd

Please sign in to comment.