Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ private List<Wei> calculateRewards(
*/
private List<Wei> boundRewards(final List<Wei> rewards, final Wei nextBaseFee) {
final Wei lowerBoundGasPrice = blockchainQueries.gasPriceLowerBound();
final Wei lowerBoundPriorityFee = lowerBoundGasPrice.subtract(nextBaseFee);
// prevent unsigned underflow when nextBaseFee exceeds lower bound
final Wei lowerBoundPriorityFee =
lowerBoundGasPrice.greaterThan(nextBaseFee)
? lowerBoundGasPrice.subtract(nextBaseFee)
: Wei.ZERO;
final Wei minPriorityFee = miningCoordinator.getMinPriorityFeePerGas();
final Wei forcedMinPriorityFee = UInt256s.max(minPriorityFee, lowerBoundPriorityFee);
final Wei lowerBound =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,64 @@ public void shouldApplyLowerBoundRewardsCorrectly() {
assertThat(rewards).isEqualTo(expectedBoundedRewards);
}

@Test
public void shouldNotUnderflowWhenNextBaseFeeExceedsGasPriceLowerBound() {
// gasPriceLowerBound = 10, nextBaseFee = 50: lowerBoundPriorityFee = ZERO (not negative).
// forcedMinPriorityFee = max(minPriorityFee=1, 0) = 1 → lowerBound=1, upperBound=2.
List<Double> rewardPercentiles =
Arrays.asList(0.0, 5.0, 10.0, 27.50, 31.0, 59.0, 60.0, 61.0, 100.0);

Block block = mock(Block.class);
Blockchain blockchain = mockBlockchainTransactionsWithPriorityFee(block);

ApiConfiguration apiConfiguration =
ImmutableApiConfiguration.builder()
.isGasAndPriorityFeeLimitingEnabled(true)
.lowerBoundGasAndPriorityFeeCoefficient(100L)
.upperBoundGasAndPriorityFeeCoefficient(200L)
.build();

final var blockchainQueries = mockBlockchainQueries(blockchain, Wei.of(10));
when(miningCoordinator.getMinPriorityFeePerGas()).thenReturn(Wei.ONE);

EthFeeHistory ethFeeHistory =
new EthFeeHistory(null, blockchainQueries, miningCoordinator, apiConfiguration);

List<Wei> rewards = ethFeeHistory.computeRewards(rewardPercentiles, block, Wei.of(50));

List<Wei> expectedBoundedRewards = Stream.of(1, 1, 2, 2, 2, 2, 2, 2, 2).map(Wei::of).toList();
assertThat(rewards).isEqualTo(expectedBoundedRewards);
}

@Test
public void shouldReturnZeroLowerBoundPriorityFeeWhenNextBaseFeeEqualsGasPriceLowerBound() {
// Equal case: nextBaseFee == lowerBoundGasPrice → lowerBoundPriorityFee must be ZERO,
// not a wraparound. forcedMinPriorityFee = max(1, 0) = 1 → lowerBound=1, upperBound=2.
List<Double> rewardPercentiles =
Arrays.asList(0.0, 5.0, 10.0, 27.50, 31.0, 59.0, 60.0, 61.0, 100.0);

Block block = mock(Block.class);
Blockchain blockchain = mockBlockchainTransactionsWithPriorityFee(block);

ApiConfiguration apiConfiguration =
ImmutableApiConfiguration.builder()
.isGasAndPriorityFeeLimitingEnabled(true)
.lowerBoundGasAndPriorityFeeCoefficient(100L)
.upperBoundGasAndPriorityFeeCoefficient(200L)
.build();

final var blockchainQueries = mockBlockchainQueries(blockchain, Wei.of(50));
when(miningCoordinator.getMinPriorityFeePerGas()).thenReturn(Wei.ONE);

EthFeeHistory ethFeeHistory =
new EthFeeHistory(null, blockchainQueries, miningCoordinator, apiConfiguration);

List<Wei> rewards = ethFeeHistory.computeRewards(rewardPercentiles, block, Wei.of(50));

List<Wei> expectedBoundedRewards = Stream.of(1, 1, 2, 2, 2, 2, 2, 2, 2).map(Wei::of).toList();
assertThat(rewards).isEqualTo(expectedBoundedRewards);
}

private Blockchain mockBlockchainTransactionsWithPriorityFee(final Block block) {
final Blockchain blockchain = mock(Blockchain.class);

Expand Down
Loading