mirror of https://github.com/hyperledger/besu
[Cleanup] Remove Gas Budget Calculator (#2363)
Since there are no separate gas budgets for eip-1559 and non-eip-1559 txs, we can return to the simpler method of just comparing against the gas limit. This is a non-functional change. Signed-off-by: Ratan Rai Sur <ratan.r.sur@gmail.com>pull/2392/head
parent
51287459ab
commit
de6c10d17f
@ -1,45 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.core.fees; |
||||
|
||||
import org.hyperledger.besu.ethereum.core.Transaction; |
||||
|
||||
@FunctionalInterface |
||||
public interface TransactionGasBudgetCalculator { |
||||
|
||||
boolean hasBudget( |
||||
final Transaction transaction, |
||||
final long blockNumber, |
||||
final long gasLimit, |
||||
final long gasUsed); |
||||
|
||||
static TransactionGasBudgetCalculator frontier() { |
||||
return gasBudgetCalculator((blockNumber, gasLimit, ignored) -> gasLimit); |
||||
} |
||||
|
||||
static TransactionGasBudgetCalculator gasBudgetCalculator( |
||||
final BlockGasLimitCalculator blockGasLimitCalculator) { |
||||
return (transaction, blockNumber, gasLimit, gasUsed) -> { |
||||
final long remainingGasBudget = |
||||
blockGasLimitCalculator.apply(blockNumber, gasLimit, transaction) - gasUsed; |
||||
return Long.compareUnsigned(transaction.getGasLimit(), remainingGasBudget) <= 0; |
||||
}; |
||||
} |
||||
|
||||
@FunctionalInterface |
||||
interface BlockGasLimitCalculator { |
||||
long apply(final long blockNumber, final long gasLimit, Transaction transaction); |
||||
} |
||||
} |
@ -1,97 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.core.fees; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import org.hyperledger.besu.ethereum.core.Transaction; |
||||
import org.hyperledger.besu.plugin.data.TransactionType; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collection; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runners.Parameterized; |
||||
|
||||
@RunWith(Parameterized.class) |
||||
public class TransactionGasBudgetCalculatorTest { |
||||
|
||||
private static final TransactionGasBudgetCalculator FRONTIER_CALCULATOR = |
||||
TransactionGasBudgetCalculator.frontier(); |
||||
|
||||
private final TransactionGasBudgetCalculator gasBudgetCalculator; |
||||
private final boolean isEIP1559; |
||||
private final long transactionGasLimit; |
||||
private final long blockNumber; |
||||
private final long blockHeaderGasLimit; |
||||
private final long gasUsed; |
||||
private final boolean expectedHasBudget; |
||||
|
||||
public TransactionGasBudgetCalculatorTest( |
||||
final TransactionGasBudgetCalculator gasBudgetCalculator, |
||||
final boolean isEIP1559, |
||||
final long transactionGasLimit, |
||||
final long blockNumber, |
||||
final long blockHeaderGasLimit, |
||||
final long gasUsed, |
||||
final boolean expectedHasBudget) { |
||||
this.gasBudgetCalculator = gasBudgetCalculator; |
||||
this.isEIP1559 = isEIP1559; |
||||
this.transactionGasLimit = transactionGasLimit; |
||||
this.blockNumber = blockNumber; |
||||
this.blockHeaderGasLimit = blockHeaderGasLimit; |
||||
this.gasUsed = gasUsed; |
||||
this.expectedHasBudget = expectedHasBudget; |
||||
} |
||||
|
||||
@Parameterized.Parameters |
||||
public static Collection<Object[]> data() { |
||||
|
||||
return Arrays.asList( |
||||
new Object[][] { |
||||
{FRONTIER_CALCULATOR, false, 5L, 1L, 10L, 0L, true}, |
||||
{FRONTIER_CALCULATOR, false, 11L, 1L, 10L, 0L, false}, |
||||
{FRONTIER_CALCULATOR, false, 5L, 1L, 10L, 6L, false}, |
||||
{FRONTIER_CALCULATOR, true, 5L, 1L, 10L, 0L, true}, |
||||
{FRONTIER_CALCULATOR, true, 11L, 1L, 10L, 0L, false}, |
||||
{FRONTIER_CALCULATOR, true, 5L, 1L, 10L, 6L, false}, |
||||
}); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatCalculatorWorks() { |
||||
assertThat( |
||||
gasBudgetCalculator.hasBudget( |
||||
transaction(isEIP1559, transactionGasLimit), |
||||
blockNumber, |
||||
blockHeaderGasLimit, |
||||
gasUsed)) |
||||
.isEqualTo(expectedHasBudget); |
||||
} |
||||
|
||||
private Transaction transaction(final boolean isEIP1559, final long gasLimit) { |
||||
final Transaction transaction = mock(Transaction.class); |
||||
if (isEIP1559) { |
||||
when(transaction.getType()).thenReturn(TransactionType.EIP1559); |
||||
} else { |
||||
when(transaction.getType()).thenReturn(TransactionType.FRONTIER); |
||||
} |
||||
when(transaction.getGasLimit()).thenReturn(gasLimit); |
||||
return transaction; |
||||
} |
||||
} |
Loading…
Reference in new issue