Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return rpc error in eth_getLogs on unknown block hash #8051

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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;
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();

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");
Comment on lines +412 to +420
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertThrows is used in other parts of the codebase to assert exceptions

    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
}