Skip to content

Commit

Permalink
Sort only once rewardPercentiles instead of doing it for each block h…
Browse files Browse the repository at this point in the history
…eader (hyperledger#6035)

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>
  • Loading branch information
ahamlat authored and NickSneo committed Nov 12, 2023
1 parent 8ef5b24 commit fddab9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,33 +149,30 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) {

final Optional<List<List<Wei>>> 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> block =
blockchain.getBlockByHash(blockHeader.getBlockHash());
return block.map(
b -> {
List<Wei> 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> block =
blockchain.getBlockByHash(blockHeader.getBlockHash());
return block.map(
b -> {
List<Wei> rewards = computeRewards(sortedPercentiles, b);
cache.put(key, rewards);
return rewards;
});
});
})
.flatMap(Optional::stream)
.toList();
});

return new JsonRpcSuccessResponse(
requestId,
Expand Down

0 comments on commit fddab9e

Please sign in to comment.