From 76a2ca7ec443f2ea02a089a9e001716602abb1de Mon Sep 17 00:00:00 2001 From: mark-terry Date: Wed, 12 Oct 2022 12:52:15 +1000 Subject: [PATCH] String param handling. Signed-off-by: mark-terry --- .../jsonrpc/internal/methods/EthGetLogs.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetLogs.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetLogs.java index 1ba2b98ec574..f3c1fa36ed2c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetLogs.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetLogs.java @@ -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; @@ -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, @@ -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(); + } + } }