Skip to content

Commit

Permalink
Add pending block header to TransactionEvaluationContext (hyperledger…
Browse files Browse the repository at this point in the history
…#7483)

Add pending block header to TransactionEvaluationContext plugin API, so PluginTransactionSelector can access info of the pending block.

---
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Co-authored-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Ade Lucas <ade.lucas@consensys.net>
  • Loading branch information
fab-10 authored and cloudspores committed Aug 20, 2024
1 parent 072e875 commit b18273f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public record BlockSelectionContext(
GasCalculator gasCalculator,
GasLimitCalculator gasLimitCalculator,
BlockHashProcessor blockHashProcessor,
ProcessableBlockHeader processableBlockHeader,
ProcessableBlockHeader pendingBlockHeader,
FeeMarket feeMarket,
Wei blobGasPrice,
Address miningBeneficiary,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ private TransactionEvaluationContext createTransactionEvaluationContext(
.getTransactionPriceCalculator()
.price(
pendingTransaction.getTransaction(),
blockSelectionContext.processableBlockHeader().getBaseFee());
blockSelectionContext.pendingBlockHeader().getBaseFee());

return new TransactionEvaluationContext(
blockSelectionContext.pendingBlockHeader(),
pendingTransaction,
Stopwatch.createStarted(),
transactionGasPriceInBlock,
Expand Down Expand Up @@ -330,10 +331,10 @@ private TransactionSelectionResult evaluatePostProcessing(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
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;
Expand All @@ -44,6 +47,11 @@ public Transaction getTransaction() {
return pendingTransaction.getTransaction();
}

@Override
public ProcessableBlockHeader getPendingBlockHeader() {
return pendingBlockHeader;
}

@Override
public PendingTransaction getPendingTransaction() {
return pendingTransaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private boolean transactionTooLargeForBlock(
final TransactionSelectionResults transactionSelectionResults) {

return transaction.getGasLimit()
> context.processableBlockHeader().getGasLimit()
> context.pendingBlockHeader().getGasLimit()
- transactionSelectionResults.getCumulativeGasUsed();
}

Expand All @@ -104,7 +104,7 @@ private boolean transactionTooLargeForBlock(
*/
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;
Expand All @@ -129,7 +129,7 @@ private boolean blockOccupancyAboveThreshold(
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private boolean isPriorityFeePriceBelowMinimum(final PendingTransaction pendingT
Wei priorityFeePerGas =
pendingTransaction
.getTransaction()
.getEffectivePriorityFeePerGas(context.processableBlockHeader().getBaseFee());
.getEffectivePriorityFeePerGas(context.pendingBlockHeader().getBaseFee());
return priorityFeePerGas.lessThan(context.miningParameters().getMinPriorityFeePerGas());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,24 @@
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() {
MiningParameters miningParameters =
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);
}

Expand Down Expand Up @@ -100,6 +97,6 @@ private TransactionEvaluationContext mockTransactionEvaluationContext(
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ void notBlobTransactionsAreSelectedWithoutAnyCheck() {

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);
Expand All @@ -94,7 +96,9 @@ void firstBlobTransactionIsSelected() {

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);

Expand All @@ -112,7 +116,9 @@ void returnsBlobsFullWhenMaxNumberOfBlobsAlreadyPresent() {

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);

Expand All @@ -132,7 +138,9 @@ void returnsTooLargeForRemainingBlobGas() {

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);

Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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;

Expand All @@ -27,6 +28,13 @@
*/
public interface TransactionEvaluationContext<PT extends PendingTransaction> {

/**
* Gets the pending block header
*
* @return the pending block header
*/
ProcessableBlockHeader getPendingBlockHeader();

/**
* Gets the pending transaction.
*
Expand Down

0 comments on commit b18273f

Please sign in to comment.