diff --git a/CHANGELOG.md b/CHANGELOG.md index ef334703528..65bd5a0d03b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release - Cache last n blocks by using a new Besu flag --cache-last-blocks=n +- Optimize performances of RPC method Eth_feeHistory ### Breaking Changes diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java index 240cdaeb655..ffe3b96b05a 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistory.java @@ -149,33 +149,30 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) { final Optional>> maybeRewards = maybeRewardPercentiles.map( - rewardPercentiles -> - blockHeaders.stream() - .parallel() - .map( - blockHeader -> { - final RewardCacheKey key = - new RewardCacheKey(blockHeader.getBlockHash(), rewardPercentiles); - return Optional.ofNullable(cache.getIfPresent(key)) - .or( - () -> { - Optional block = - blockchain.getBlockByHash(blockHeader.getBlockHash()); - return block.map( - b -> { - List rewards = - computeRewards( - rewardPercentiles.stream() - .sorted() - .collect(toUnmodifiableList()), - b); - cache.put(key, rewards); - return rewards; - }); - }); - }) - .flatMap(Optional::stream) - .collect(toUnmodifiableList())); + rewardPercentiles -> { + var sortedPercentiles = rewardPercentiles.stream().sorted().toList(); + return blockHeaders.stream() + .parallel() + .map( + blockHeader -> { + final RewardCacheKey key = + new RewardCacheKey(blockHeader.getBlockHash(), rewardPercentiles); + return Optional.ofNullable(cache.getIfPresent(key)) + .or( + () -> { + Optional block = + blockchain.getBlockByHash(blockHeader.getBlockHash()); + return block.map( + b -> { + List rewards = computeRewards(sortedPercentiles, b); + cache.put(key, rewards); + return rewards; + }); + }); + }) + .flatMap(Optional::stream) + .toList(); + }); return new JsonRpcSuccessResponse( requestId,