Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pending block header to TransactionEvaluationContext #7483

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
Loading