From 19e1a9aaf6f00eb79b70eff13e2d33963f377cf0 Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Tue, 20 Aug 2024 01:50:26 +0200 Subject: [PATCH] Add pending block header to TransactionEvaluationContext (#7483) Add pending block header to TransactionEvaluationContext plugin API, so PluginTransactionSelector can access info of the pending block. --- Signed-off-by: Fabio Di Fabio Co-authored-by: Usman Saleem --- CHANGELOG.md | 1 + .../txselection/BlockSelectionContext.java | 2 +- .../txselection/BlockTransactionSelector.java | 7 ++++--- .../TransactionEvaluationContext.java | 12 ++++++++++-- .../selectors/BlockSizeTransactionSelector.java | 6 +++--- ...MinPriorityFeePerGasTransactionSelector.java | 2 +- ...riorityFeePerGasTransactionSelectorTest.java | 17 +++++++---------- .../BlobSizeTransactionSelectorTest.java | 16 ++++++++++++---- plugin-api/build.gradle | 2 +- .../TransactionEvaluationContext.java | 8 ++++++++ 10 files changed, 48 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad5678b08..f04fcfb345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Additions and Improvements - Add 'inbound' field to admin_peers JSON-RPC Call [#7461](https://github.com/hyperledger/besu/pull/7461) +- Add pending block header to `TransactionEvaluationContext` plugin API [#7483](https://github.com/hyperledger/besu/pull/7483) ### Bug fixes - Fix tracing in precompiled contracts when halting for out of gas [#7318](https://github.com/hyperledger/besu/issues/7318) diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockSelectionContext.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockSelectionContext.java index a3dd13bd1b..f8bf6d50c5 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockSelectionContext.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockSelectionContext.java @@ -29,7 +29,7 @@ public record BlockSelectionContext( GasCalculator gasCalculator, GasLimitCalculator gasLimitCalculator, BlockHashProcessor blockHashProcessor, - ProcessableBlockHeader processableBlockHeader, + ProcessableBlockHeader pendingBlockHeader, FeeMarket feeMarket, Wei blobGasPrice, Address miningBeneficiary, diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockTransactionSelector.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockTransactionSelector.java index ed028767d6..3d56c1fc07 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockTransactionSelector.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/BlockTransactionSelector.java @@ -263,9 +263,10 @@ public class BlockTransactionSelector { .getTransactionPriceCalculator() .price( pendingTransaction.getTransaction(), - blockSelectionContext.processableBlockHeader().getBaseFee()); + blockSelectionContext.pendingBlockHeader().getBaseFee()); return new TransactionEvaluationContext( + blockSelectionContext.pendingBlockHeader(), pendingTransaction, Stopwatch.createStarted(), transactionGasPriceInBlock, @@ -330,10 +331,10 @@ public class BlockTransactionSelector { private TransactionProcessingResult processTransaction( final PendingTransaction pendingTransaction, final WorldUpdater worldStateUpdater) { final BlockHashLookup blockHashLookup = - new CachingBlockHashLookup(blockSelectionContext.processableBlockHeader(), blockchain); + new CachingBlockHashLookup(blockSelectionContext.pendingBlockHeader(), blockchain); return transactionProcessor.processTransaction( worldStateUpdater, - blockSelectionContext.processableBlockHeader(), + blockSelectionContext.pendingBlockHeader(), pendingTransaction.getTransaction(), blockSelectionContext.miningBeneficiary(), pluginOperationTracer, diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java index 0a17812b0f..7fce600a1a 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/TransactionEvaluationContext.java @@ -17,23 +17,26 @@ package org.hyperledger.besu.ethereum.blockcreation.txselection; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.core.Transaction; import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction; +import org.hyperledger.besu.plugin.data.ProcessableBlockHeader; import com.google.common.base.Stopwatch; public class TransactionEvaluationContext implements org.hyperledger.besu.plugin.services.txselection.TransactionEvaluationContext< PendingTransaction> { - private final org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction - pendingTransaction; + private final ProcessableBlockHeader pendingBlockHeader; + private final PendingTransaction pendingTransaction; private final Stopwatch evaluationTimer; private final Wei transactionGasPrice; private final Wei minGasPrice; public TransactionEvaluationContext( + final ProcessableBlockHeader pendingBlockHeader, final PendingTransaction pendingTransaction, final Stopwatch evaluationTimer, final Wei transactionGasPrice, final Wei minGasPrice) { + this.pendingBlockHeader = pendingBlockHeader; this.pendingTransaction = pendingTransaction; this.evaluationTimer = evaluationTimer; this.transactionGasPrice = transactionGasPrice; @@ -44,6 +47,11 @@ public class TransactionEvaluationContext return pendingTransaction.getTransaction(); } + @Override + public ProcessableBlockHeader getPendingBlockHeader() { + return pendingBlockHeader; + } + @Override public PendingTransaction getPendingTransaction() { return pendingTransaction; diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java index 793f3e9384..2877c4ce13 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlockSizeTransactionSelector.java @@ -91,7 +91,7 @@ public class BlockSizeTransactionSelector extends AbstractTransactionSelector { final TransactionSelectionResults transactionSelectionResults) { return transaction.getGasLimit() - > context.processableBlockHeader().getGasLimit() + > context.pendingBlockHeader().getGasLimit() - transactionSelectionResults.getCumulativeGasUsed(); } @@ -104,7 +104,7 @@ public class BlockSizeTransactionSelector extends AbstractTransactionSelector { */ private boolean blockOccupancyAboveThreshold( final TransactionSelectionResults transactionSelectionResults) { - final long gasAvailable = context.processableBlockHeader().getGasLimit(); + final long gasAvailable = context.pendingBlockHeader().getGasLimit(); final long gasUsed = transactionSelectionResults.getCumulativeGasUsed(); final long gasRemaining = gasAvailable - gasUsed; @@ -129,7 +129,7 @@ public class BlockSizeTransactionSelector extends AbstractTransactionSelector { * @return True if the block is full, false otherwise. */ private boolean blockFull(final TransactionSelectionResults transactionSelectionResults) { - final long gasAvailable = context.processableBlockHeader().getGasLimit(); + final long gasAvailable = context.pendingBlockHeader().getGasLimit(); final long gasUsed = transactionSelectionResults.getCumulativeGasUsed(); final long gasRemaining = gasAvailable - gasUsed; diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/MinPriorityFeePerGasTransactionSelector.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/MinPriorityFeePerGasTransactionSelector.java index fc32a5c08c..5783c7e500 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/MinPriorityFeePerGasTransactionSelector.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/MinPriorityFeePerGasTransactionSelector.java @@ -68,7 +68,7 @@ public class MinPriorityFeePerGasTransactionSelector extends AbstractTransaction Wei priorityFeePerGas = pendingTransaction .getTransaction() - .getEffectivePriorityFeePerGas(context.processableBlockHeader().getBaseFee()); + .getEffectivePriorityFeePerGas(context.pendingBlockHeader().getBaseFee()); return priorityFeePerGas.lessThan(context.miningParameters().getMinPriorityFeePerGas()); } diff --git a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/MinPriorityFeePerGasTransactionSelectorTest.java b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/MinPriorityFeePerGasTransactionSelectorTest.java index 8122dc3088..6f81f4df2c 100644 --- a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/MinPriorityFeePerGasTransactionSelectorTest.java +++ b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/MinPriorityFeePerGasTransactionSelectorTest.java @@ -33,11 +33,16 @@ import org.hyperledger.besu.plugin.data.TransactionSelectionResult; import com.google.common.base.Stopwatch; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) public class MinPriorityFeePerGasTransactionSelectorTest { private AbstractTransactionSelector transactionSelector; private final int minPriorityFeeParameter = 7; + @Mock private ProcessableBlockHeader pendingBlockHeader; @BeforeEach public void initialize() { @@ -45,15 +50,7 @@ public class MinPriorityFeePerGasTransactionSelectorTest { MiningParameters.newDefault().setMinPriorityFeePerGas(Wei.of(minPriorityFeeParameter)); BlockSelectionContext context = new BlockSelectionContext( - miningParameters, - null, - null, - null, - mock(ProcessableBlockHeader.class), - null, - null, - null, - null); + miningParameters, null, null, null, pendingBlockHeader, null, null, null, null); transactionSelector = new MinPriorityFeePerGasTransactionSelector(context); } @@ -100,6 +97,6 @@ public class MinPriorityFeePerGasTransactionSelectorTest { when(pendingTransaction.getTransaction()).thenReturn(transaction); when(transaction.getEffectivePriorityFeePerGas(any())).thenReturn(Wei.of(priorityFeePerGas)); return new TransactionEvaluationContext( - pendingTransaction, Stopwatch.createStarted(), Wei.ONE, Wei.ONE); + pendingBlockHeader, pendingTransaction, Stopwatch.createStarted(), Wei.ONE, Wei.ONE); } } diff --git a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelectorTest.java b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelectorTest.java index f52a5dfb46..123faf2d18 100644 --- a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelectorTest.java +++ b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/txselection/selectors/BlobSizeTransactionSelectorTest.java @@ -76,7 +76,9 @@ class BlobSizeTransactionSelectorTest { final var nonBlobTx = createEIP1559PendingTransaction(); - final var txEvaluationContext = new TransactionEvaluationContext(nonBlobTx, null, null, null); + final var txEvaluationContext = + new TransactionEvaluationContext( + blockSelectionContext.pendingBlockHeader(), nonBlobTx, null, null, null); final var result = selector.evaluateTransactionPreProcessing(txEvaluationContext, selectionResults); @@ -94,7 +96,9 @@ class BlobSizeTransactionSelectorTest { final var firstBlobTx = createBlobPendingTransaction(MAX_BLOBS); - final var txEvaluationContext = new TransactionEvaluationContext(firstBlobTx, null, null, null); + final var txEvaluationContext = + new TransactionEvaluationContext( + blockSelectionContext.pendingBlockHeader(), firstBlobTx, null, null, null); when(selectionResults.getCumulativeBlobGasUsed()).thenReturn(0L); @@ -112,7 +116,9 @@ class BlobSizeTransactionSelectorTest { final var firstBlobTx = createBlobPendingTransaction(1); - final var txEvaluationContext = new TransactionEvaluationContext(firstBlobTx, null, null, null); + final var txEvaluationContext = + new TransactionEvaluationContext( + blockSelectionContext.pendingBlockHeader(), firstBlobTx, null, null, null); when(selectionResults.getCumulativeBlobGasUsed()).thenReturn(MAX_BLOB_GAS); @@ -132,7 +138,9 @@ class BlobSizeTransactionSelectorTest { final var firstBlobTx = createBlobPendingTransaction(MAX_BLOBS); - final var txEvaluationContext = new TransactionEvaluationContext(firstBlobTx, null, null, null); + final var txEvaluationContext = + new TransactionEvaluationContext( + blockSelectionContext.pendingBlockHeader(), firstBlobTx, null, null, null); when(selectionResults.getCumulativeBlobGasUsed()).thenReturn(MAX_BLOB_GAS - 1); diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index 4440a4f069..af8da71a7f 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -70,7 +70,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = '6Hy3eaCpnxehyDO3smSAr1i2DsB2q/V37/m8POycikI=' + knownHash = '2tFIKwEd8T5I37ywbFnVcMwTR8HiiCC6gO1Chd3hZp8=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/TransactionEvaluationContext.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/TransactionEvaluationContext.java index 93eae92dc7..8938ef881b 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/TransactionEvaluationContext.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/txselection/TransactionEvaluationContext.java @@ -16,6 +16,7 @@ package org.hyperledger.besu.plugin.services.txselection; import org.hyperledger.besu.datatypes.PendingTransaction; import org.hyperledger.besu.datatypes.Wei; +import org.hyperledger.besu.plugin.data.ProcessableBlockHeader; import com.google.common.base.Stopwatch; @@ -27,6 +28,13 @@ import com.google.common.base.Stopwatch; */ public interface TransactionEvaluationContext { + /** + * Gets the pending block header + * + * @return the pending block header + */ + ProcessableBlockHeader getPendingBlockHeader(); + /** * Gets the pending transaction. *