Skip to content

Commit

Permalink
Bug Fix eth_feeHistory - Issue-hyperledger#4231 (hyperledger#5397)
Browse files Browse the repository at this point in the history
* fix eth_feeHistory

Signed-off-by: George Tebrean <george@web3labs.com>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: George Tebrean <george@web3labs.com>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
2 people authored and eum602 committed Nov 3, 2023
1 parent 7f038c1 commit 7d277e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Additions and Improvements

### Bug Fixes
- Fix eth_feeHistory response for the case in which blockCount is higher than highestBlock requested. [#5397](https://github.com/hyperledger/besu/pull/5397)

### Download Links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) {

final long oldestBlock = Math.max(0, resolvedHighestBlockNumber - (blockCount - 1));

final long lastBlock =
blockCount > resolvedHighestBlockNumber
? (resolvedHighestBlockNumber + 1)
: (oldestBlock + blockCount);

final List<BlockHeader> blockHeaders =
LongStream.range(oldestBlock, oldestBlock + blockCount)
LongStream.range(oldestBlock, lastBlock)
.mapToObj(blockchain::getBlockHeader)
.flatMap(Optional::stream)
.collect(toUnmodifiableList());
Expand Down Expand Up @@ -138,7 +143,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) {
final Optional<List<List<Wei>>> maybeRewards =
maybeRewardPercentiles.map(
rewardPercentiles ->
LongStream.range(oldestBlock, oldestBlock + blockCount)
LongStream.range(oldestBlock, lastBlock)
.mapToObj(blockchain::getBlockByNumber)
.flatMap(Optional::stream)
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,44 @@ public void doesntGoPastChainHeadWithHighBlockCount() {
assertThat(result.getReward()).isNull();
}

@Test
public void feeValuesAreInTheBlockCountAndHighestBlock() {
final ProtocolSpec londonSpec = mock(ProtocolSpec.class);
when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5));
when(protocolSchedule.getForNextBlockHeader(
eq(blockchain.getChainHeadHeader()),
eq(blockchain.getChainHeadHeader().getTimestamp())))
.thenReturn(londonSpec);
double[] percentile = new double[] {100.0};

final Object ninth =
((JsonRpcSuccessResponse) feeHistoryRequest(2, "9", percentile)).getResult();
assertFeeMetadataSize(ninth, 2);

final Object eighth =
((JsonRpcSuccessResponse) feeHistoryRequest(4, "8", percentile)).getResult();
assertFeeMetadataSize(eighth, 4);
}

@Test
public void feeValuesDontGoPastHighestBlock() {
final ProtocolSpec londonSpec = mock(ProtocolSpec.class);
when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5));
when(protocolSchedule.getForNextBlockHeader(
eq(blockchain.getChainHeadHeader()),
eq(blockchain.getChainHeadHeader().getTimestamp())))
.thenReturn(londonSpec);
double[] percentile = new double[] {100.0};

final Object second =
((JsonRpcSuccessResponse) feeHistoryRequest(4, "2", percentile)).getResult();
assertFeeMetadataSize(second, 3);

final Object third =
((JsonRpcSuccessResponse) feeHistoryRequest(11, "3", percentile)).getResult();
assertFeeMetadataSize(third, 4);
}

@Test
public void correctlyHandlesForkBlock() {
final ProtocolSpec londonSpec = mock(ProtocolSpec.class);
Expand Down Expand Up @@ -177,6 +215,14 @@ public void allZeroPercentilesForZeroBlock() {
assertThat(result.getReward()).isEqualTo(List.of(List.of("0x0")));
}

private void assertFeeMetadataSize(final Object feeObject, final int blockCount) {
assertThat(((ImmutableFeeHistoryResult) feeObject).getBaseFeePerGas().size())
.isEqualTo(blockCount + 1);
assertThat(((ImmutableFeeHistoryResult) feeObject).getReward().size()).isEqualTo(blockCount);
assertThat(((ImmutableFeeHistoryResult) feeObject).getGasUsedRatio().size())
.isEqualTo(blockCount);
}

private JsonRpcResponse feeHistoryRequest(final Object... params) {
return method.response(
new JsonRpcRequestContext(new JsonRpcRequest("2.0", "eth_feeHistory", params)));
Expand Down

0 comments on commit 7d277e8

Please sign in to comment.