Skip to content

Commit

Permalink
String param handling.
Browse files Browse the repository at this point in the history
Signed-off-by: mark-terry <mark.terry@consensys.net>
  • Loading branch information
mark-terry committed Oct 12, 2022
1 parent d9ebbdb commit 76a2ca7
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -58,10 +59,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
blockHash, filter.getLogsQuery(), requestContext::isAlive))
.orElseGet(
() -> {
final long fromBlockNumber =
filter.getFromBlock().getNumber().orElse(blockchain.headBlockNumber());
final long toBlockNumber =
filter.getToBlock().getNumber().orElse(blockchain.headBlockNumber());
final long fromBlockNumber = interpretBlockParam(filter.getFromBlock());
final long toBlockNumber = interpretBlockParam(filter.getToBlock());

return blockchain.matchingLogs(
fromBlockNumber,
toBlockNumber,
Expand All @@ -72,4 +72,26 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), new LogsResult(matchingLogs));
}

private long interpretBlockParam(final BlockParameter block) {
if (block.isFinalized()) {
return blockchain
.finalizedBlockHeader()
.orElseThrow(() -> new IllegalArgumentException("Finalized block not found."))
.getNumber();
} else if (block.isLatest()) {
return blockchain.headBlockNumber();
} else if (block.isPending()) {
// Pending not implemented, returns latest
return blockchain.headBlockNumber();
} else if (block.isSafe()) {
return blockchain
.safeBlockHeader()
.orElseThrow(() -> new IllegalArgumentException("Safe block not found."))
.getNumber();
} else {
// Alternate cases (numeric input or "earliest")
return block.getNumber().get();
}
}
}

0 comments on commit 76a2ca7

Please sign in to comment.