|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.mainnet; |
| 16 | + |
| 17 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 18 | + |
| 19 | +import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket; |
| 20 | +import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket; |
| 21 | +import org.hyperledger.besu.evm.gascalculator.OsakaGasCalculator; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.TestInstance; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.Arguments; |
| 31 | +import org.junit.jupiter.params.provider.MethodSource; |
| 32 | + |
| 33 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 34 | +class OsakaTargetingGasLimitCalculatorTest { |
| 35 | + private static final long TARGET_BLOB_GAS_PER_BLOCK_OSAKA = 0x120000; |
| 36 | + |
| 37 | + private final OsakaGasCalculator osakaGasCalculator = new OsakaGasCalculator(); |
| 38 | + private final BaseFeeMarket feeMarket = FeeMarket.cancunDefault(0L, Optional.empty()); |
| 39 | + |
| 40 | + @ParameterizedTest( |
| 41 | + name = "{index} - parent gas {0}, used gas {1}, parent base fee per gas {2}, new excess {3}") |
| 42 | + @MethodSource("osakaExcessBlobGasTestCases") |
| 43 | + public void shouldCalculateOsakaExcessBlobGasCorrectly( |
| 44 | + final long parentExcess, |
| 45 | + final long used, |
| 46 | + final long parentBaseFeePerGas, |
| 47 | + final long expected) { |
| 48 | + int maxBlobs = 10; |
| 49 | + int targetBlobs = 9; |
| 50 | + final OsakaTargetingGasLimitCalculator osakaTargetingGasLimitCalculator = |
| 51 | + new OsakaTargetingGasLimitCalculator( |
| 52 | + 0L, feeMarket, osakaGasCalculator, maxBlobs, targetBlobs); |
| 53 | + |
| 54 | + final long usedBlobGas = osakaGasCalculator.blobGasCost(used); |
| 55 | + assertThat( |
| 56 | + osakaTargetingGasLimitCalculator.computeExcessBlobGas( |
| 57 | + parentExcess, usedBlobGas, parentBaseFeePerGas)) |
| 58 | + .isEqualTo(expected); |
| 59 | + } |
| 60 | + |
| 61 | + Iterable<Arguments> osakaExcessBlobGasTestCases() { |
| 62 | + long targetGasPerBlock = TARGET_BLOB_GAS_PER_BLOCK_OSAKA; |
| 63 | + return List.of( |
| 64 | + // Case 1: Below target, should return 0 |
| 65 | + Arguments.of(0L, 0L, 0L, 0L), |
| 66 | + Arguments.of(targetGasPerBlock - 1, 0L, 0L, 0L), |
| 67 | + Arguments.of(0L, 8, 0L, 0L), // 8 blobs is below target (9) |
| 68 | + |
| 69 | + // Case 2: Above target, BLOB_BASE_COST * baseFee <= GAS_PER_BLOB * blobFee |
| 70 | + // This should use the formula: parentExcess + parentBlobGasUsed - target |
| 71 | + Arguments.of(targetGasPerBlock * 2, 10, 17L, 2490368L), |
| 72 | + |
| 73 | + // Case 3: Above target, BLOB_BASE_COST * baseFee > GAS_PER_BLOB * blobFee |
| 74 | + // This should use the formula: parentExcess + parentBlobGasUsed * (max - target) / max |
| 75 | + Arguments.of(targetGasPerBlock, 1, 0L, osakaGasCalculator.getBlobGasPerBlob()), |
| 76 | + Arguments.of(targetGasPerBlock, 10, 1L, 1310720L), |
| 77 | + Arguments.of(targetGasPerBlock, 10, 16L, 1310720L)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldUseCorrectBlobGasPerBlob() { |
| 82 | + // should use OsakaGasCalculator's blob gas per blob to calculate the gas limit |
| 83 | + final long blobGasPerBlob = osakaGasCalculator.getBlobGasPerBlob(); |
| 84 | + assertThat(blobGasPerBlob).isEqualTo(131072); // same as Cancun |
| 85 | + |
| 86 | + int maxBlobs = 10; |
| 87 | + int targetBlobs = 9; |
| 88 | + var osakaTargetingGasLimitCalculator = |
| 89 | + new OsakaTargetingGasLimitCalculator( |
| 90 | + 0L, feeMarket, osakaGasCalculator, maxBlobs, targetBlobs); |
| 91 | + |
| 92 | + // if maxBlobs = 10, then the gas limit would be 131072 * 10 = 1310720 |
| 93 | + assertThat(osakaTargetingGasLimitCalculator.currentBlobGasLimit()) |
| 94 | + .isEqualTo(blobGasPerBlob * maxBlobs); |
| 95 | + assertThat(osakaTargetingGasLimitCalculator.currentBlobGasLimit()).isEqualTo(1310720); |
| 96 | + assertThat(osakaTargetingGasLimitCalculator.getTargetBlobGasPerBlock()) |
| 97 | + .isEqualTo(blobGasPerBlob * targetBlobs); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testComputeExcessBlobGasWithDifferentConditions() { |
| 102 | + // Create a test instance with specific parameters |
| 103 | + int maxBlobs = 10; |
| 104 | + int targetBlobs = 9; |
| 105 | + var calculator = |
| 106 | + new OsakaTargetingGasLimitCalculator( |
| 107 | + 0L, feeMarket, osakaGasCalculator, maxBlobs, targetBlobs); |
| 108 | + assertThat(calculator.maxBlobsPerBlock).isEqualTo(maxBlobs); |
| 109 | + assertThat(calculator.targetBlobsPerBlock).isEqualTo(targetBlobs); |
| 110 | + |
| 111 | + long parentExcessBlobGas = calculator.getTargetBlobGasPerBlock(); |
| 112 | + long parentBlobGasUsed = osakaGasCalculator.getBlobGasPerBlob() * 2; |
| 113 | + |
| 114 | + // Test the condition where BLOB_BASE_COST * baseFee > GAS_PER_BLOB * blobFee |
| 115 | + // This should use the first formula |
| 116 | + long result1 = calculator.computeExcessBlobGas(parentExcessBlobGas, parentBlobGasUsed, 0L); |
| 117 | + // Expected value based on the test case |
| 118 | + assertThat(result1).isEqualTo(262144L); |
| 119 | + |
| 120 | + // Test with very high excess blob gas to trigger the second formula |
| 121 | + // When excess is high, blob fee will be high, potentially making BLOB_BASE_COST * baseFee <= |
| 122 | + // GAS_PER_BLOB * blobFee |
| 123 | + long highExcess = calculator.getTargetBlobGasPerBlock() * 10; |
| 124 | + long result2 = calculator.computeExcessBlobGas(highExcess, parentBlobGasUsed, 0L); |
| 125 | + // Expected value based on the test case |
| 126 | + assertThat(result2).isEqualTo(10878976L); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void dryRunDetector() { |
| 131 | + Assertions.assertThat(true) |
| 132 | + .withFailMessage("This test is here so gradle --dry-run executes this class") |
| 133 | + .isTrue(); |
| 134 | + } |
| 135 | +} |
0 commit comments