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

Corrected eth_getLogs default fromBlock value. #4513

Merged
merged 8 commits into from
Oct 26, 2022
Merged
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
### Additions and Improvements
- Updated jackson-databind library to version 2.13.4.2 addressing [CVE-2022-42003](https://nvd.nist.gov/vuln/detail/CVE-2022-42003)

### Bug Fixes
- Fixed default fromBlock value and improved parameter interpetation in eth_getLogs RPC handler [#4513](https://github.com/hyperledger/besu/pull/4513)

## 22.10.0-RC2

### Breaking Changes
Expand Down Expand Up @@ -36,8 +39,6 @@
- Avoid a cyclic reference while printing EngineExchangeTransitionConfigurationParameter [#4357](https://github.com/hyperledger/besu/pull/4357)
- Corrects treating a block as bad on internal error [#4512](https://github.com/hyperledger/besu/issues/4512)
- In GraphQL update scalar parsing to be variable friendly [#4522](https://github.com/hyperledger/besu/pull/4522)
- Initiate connection to maintained peers soon after startup. [#4469](https://github.com/hyperledger/besu/pull/4469)
- Update apache-commons-text to 1.10.0 to address CVE-2022-42889 [#4542](https://github.com/hyperledger/besu/pull/4542)
Comment on lines -39 to -40
Copy link
Contributor

Choose a reason for hiding this comment

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

Please readd these 2 entries

Copy link
Contributor Author

Choose a reason for hiding this comment

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


### Download Links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.LogWithMetadata;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EthGetLogs implements JsonRpcMethod {

private static final Logger LOG = LoggerFactory.getLogger(EthGetLogs.class);

private final BlockchainQueries blockchain;

public EthGetLogs(final BlockchainQueries blockchain) {
Expand All @@ -49,6 +56,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}

final AtomicReference<Exception> ex = new AtomicReference<>();
final List<LogWithMetadata> matchingLogs =
filter
.getBlockHash()
Expand All @@ -58,16 +66,40 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
blockHash, filter.getLogsQuery(), requestContext::isAlive))
.orElseGet(
() -> {
final long fromBlockNumber = filter.getFromBlock().getNumber().orElse(0L);
final long toBlockNumber =
filter.getToBlock().getNumber().orElse(blockchain.headBlockNumber());
final long fromBlockNumber;
final long toBlockNumber;
try {
fromBlockNumber =
filter
.getFromBlock()
.getBlockNumber(blockchain)
.orElseThrow(
() ->
new Exception("fromBlock not found: " + filter.getFromBlock()));
toBlockNumber =
filter
.getToBlock()
.getBlockNumber(blockchain)
.orElseThrow(
() -> new Exception("toBlock not found: " + filter.getToBlock()));
} catch (final Exception e) {
ex.set(e);
return Collections.emptyList();
}

return blockchain.matchingLogs(
fromBlockNumber,
toBlockNumber,
filter.getLogsQuery(),
requestContext::isAlive);
});

if (ex.get() != null) {
LOG.debug("eth_getLogs call failed: ", ex.get());
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INTERNAL_ERROR);
}

return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), new LogsResult(matchingLogs));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters;

import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -99,6 +101,22 @@ public boolean isNumeric() {
return this.type == BlockParameterType.NUMERIC;
}

public Optional<Long> getBlockNumber(final BlockchainQueries blockchain) {
if (this.isFinalized()) {
return blockchain.finalizedBlockHeader().map(ProcessableBlockHeader::getNumber);
} else if (this.isLatest()) {
return Optional.of(blockchain.headBlockNumber());
} else if (this.isPending()) {
// Pending not implemented, returns latest
return Optional.of(blockchain.headBlockNumber());
} else if (this.isSafe()) {
return blockchain.safeBlockHeader().map(ProcessableBlockHeader::getNumber);
} else {
// Alternate cases (numeric input or "earliest")
return this.getNumber();
}
}

@Override
public String toString() {
return "BlockParameter{" + "type=" + type + ", number=" + number + '}';
Expand Down
Loading