|
61 | 61 | import java.util.Collections;
|
62 | 62 | import java.util.List;
|
63 | 63 | import java.util.Optional;
|
| 64 | +import java.util.stream.Collectors; |
| 65 | +import java.util.stream.IntStream; |
64 | 66 | import java.util.stream.Stream;
|
65 | 67 |
|
66 | 68 | import org.apache.tuweni.bytes.Bytes;
|
@@ -368,6 +370,48 @@ public void allZeroPercentilesForZeroBlock() {
|
368 | 370 | assertThat(result.getReward()).isEqualTo(List.of(List.of("0x0")));
|
369 | 371 | }
|
370 | 372 |
|
| 373 | + @Test |
| 374 | + public void assertMaximumPercentilesArraySize() { |
| 375 | + final ProtocolSpec londonSpec = mock(ProtocolSpec.class); |
| 376 | + when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5)); |
| 377 | + final BlockDataGenerator.BlockOptions blockOptions = BlockDataGenerator.BlockOptions.create(); |
| 378 | + blockOptions.hasTransactions(false); |
| 379 | + blockOptions.setParentHash(blockchain.getChainHeadHash()); |
| 380 | + blockOptions.setBlockNumber(11); |
| 381 | + final Block emptyBlock = gen.block(blockOptions); |
| 382 | + blockchain.appendBlock(emptyBlock, gen.receipts(emptyBlock)); |
| 383 | + when(protocolSchedule.getForNextBlockHeader( |
| 384 | + eq(blockchain.getChainHeadHeader()), |
| 385 | + eq(blockchain.getChainHeadHeader().getTimestamp()))) |
| 386 | + .thenReturn(londonSpec); |
| 387 | + |
| 388 | + double[] biglist = new double[500]; |
| 389 | + Arrays.fill(biglist, 1d); |
| 390 | + List<Double> oversizeRewardPercentiles = |
| 391 | + Arrays.stream(biglist).boxed().collect(Collectors.toList()); |
| 392 | + |
| 393 | + List<Double> maxRewardPercentiles = |
| 394 | + IntStream.rangeClosed(1, 100) |
| 395 | + .mapToDouble(i -> i / 100d) |
| 396 | + .boxed() |
| 397 | + .collect(Collectors.toList()); |
| 398 | + |
| 399 | + // assert we return no percentiles for array sizes > 100 |
| 400 | + final FeeHistory.FeeHistoryResult result = |
| 401 | + (FeeHistory.FeeHistoryResult) |
| 402 | + ((JsonRpcSuccessResponse) feeHistoryRequest("0x1", "latest", oversizeRewardPercentiles)) |
| 403 | + .getResult(); |
| 404 | + assertThat(result.getReward()).isNull(); |
| 405 | + |
| 406 | + // assert we will return percentiles for array sizes <= 100 |
| 407 | + final FeeHistory.FeeHistoryResult resultOk = |
| 408 | + (FeeHistory.FeeHistoryResult) |
| 409 | + ((JsonRpcSuccessResponse) feeHistoryRequest("0x1", "latest", maxRewardPercentiles)) |
| 410 | + .getResult(); |
| 411 | + assertThat(resultOk.getReward()).isNotNull(); |
| 412 | + assertThat(resultOk.getReward().get(0).size()).isEqualTo(100); |
| 413 | + } |
| 414 | + |
371 | 415 | private void assertFeeMetadataSize(final Object feeObject, final int blockCount) {
|
372 | 416 | assertThat(((ImmutableFeeHistoryResult) feeObject).getBaseFeePerGas().size())
|
373 | 417 | .isEqualTo(blockCount + 1);
|
|
0 commit comments