Skip to content

Commit

Permalink
Use PendingTransaction interface in block creation classes (#8254)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Feb 5, 2025
1 parent 630a8f7 commit a818793
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ public interface PendingTransaction {
* @return timestamp
*/
long getAddedAt();

/**
* Return the estimated amount memory that this pending transaction occupies
*
* @return the estimated memory size
*/
int memorySize();

/**
* Formats a string with detailed information about the pending transaction for debug purposes
*
* @return a string
*/
String toTraceLog();
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private TransactionSelectionResult evaluateTransaction(

final WorldUpdater txWorldStateUpdater = blockWorldStateUpdater.updater();
final TransactionProcessingResult processingResult =
processTransaction(pendingTransaction, txWorldStateUpdater);
processTransaction(evaluationContext.getTransaction(), txWorldStateUpdater);

var postProcessingSelectionResult = evaluatePostProcessing(evaluationContext, processingResult);

Expand Down Expand Up @@ -370,20 +370,20 @@ private TransactionSelectionResult evaluatePostProcessing(
/**
* Processes a transaction
*
* @param pendingTransaction The transaction to be processed.
* @param transaction The transaction to be processed.
* @param worldStateUpdater The world state updater.
* @return The result of the transaction processing.
*/
private TransactionProcessingResult processTransaction(
final PendingTransaction pendingTransaction, final WorldUpdater worldStateUpdater) {
final Transaction transaction, final WorldUpdater worldStateUpdater) {
final BlockHashLookup blockHashLookup =
blockSelectionContext
.blockHashProcessor()
.createBlockHashLookup(blockchain, blockSelectionContext.pendingBlockHeader());
return transactionProcessor.processTransaction(
worldStateUpdater,
blockSelectionContext.pendingBlockHeader(),
pendingTransaction.getTransaction(),
transaction,
blockSelectionContext.miningBeneficiary(),
operationTracer,
blockHashLookup,
Expand Down Expand Up @@ -528,7 +528,7 @@ private boolean transactionTookTooLong(
.setMessage(
"Transaction {} is too late for inclusion, with result {}, evaluated in {} that is over the max limit of {}ms"
+ ", {}")
.addArgument(evaluationContext.getPendingTransaction()::getHash)
.addArgument(evaluationContext.getTransaction()::getHash)
.addArgument(selectionResult)
.addArgument(evaluationTimer)
.addArgument(blockTxsSelectionMaxTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
*/
package org.hyperledger.besu.ethereum.blockcreation.txselection;

import org.hyperledger.besu.datatypes.PendingTransaction;
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> {
implements org.hyperledger.besu.plugin.services.txselection.TransactionEvaluationContext {
private final ProcessableBlockHeader pendingBlockHeader;
private final PendingTransaction pendingTransaction;
private final Stopwatch evaluationTimer;
Expand All @@ -44,7 +43,9 @@ public TransactionEvaluationContext(
}

public Transaction getTransaction() {
return pendingTransaction.getTransaction();
// ToDo: can we avoid this cast? either by always using the interface
// or moving our Transaction implementation in the datatypes package
return (Transaction) pendingTransaction.getTransaction();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hyperledger.besu.ethereum.blockcreation.txselection.BlockSelectionContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionEvaluationContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionSelectionResults;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;

Expand Down Expand Up @@ -47,7 +46,7 @@ public MinPriorityFeePerGasTransactionSelector(final BlockSelectionContext conte
public TransactionSelectionResult evaluateTransactionPreProcessing(
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResults transactionSelectionResults) {
if (isPriorityFeePriceBelowMinimum(evaluationContext.getPendingTransaction())) {
if (isPriorityFeePriceBelowMinimum(evaluationContext)) {
return TransactionSelectionResult.PRIORITY_FEE_PER_GAS_BELOW_CURRENT_MIN;
}
return TransactionSelectionResult.SELECTED;
Expand All @@ -56,17 +55,18 @@ public TransactionSelectionResult evaluateTransactionPreProcessing(
/**
* Checks if the priority fee price is below the minimum.
*
* @param pendingTransaction The transaction to check.
* @param evaluationContext The current selection session data.
* @return boolean. Returns true if the minimum priority fee price is below the minimum, false
* otherwise.
*/
private boolean isPriorityFeePriceBelowMinimum(final PendingTransaction pendingTransaction) {
private boolean isPriorityFeePriceBelowMinimum(
final TransactionEvaluationContext evaluationContext) {
// Priority txs are exempt from this check
if (pendingTransaction.hasPriority()) {
if (evaluationContext.getPendingTransaction().hasPriority()) {
return false;
}
Wei priorityFeePerGas =
pendingTransaction
evaluationContext
.getTransaction()
.getEffectivePriorityFeePerGas(context.pendingBlockHeader().getBaseFee());
return priorityFeePerGas.lessThan(context.miningConfiguration().getMinPriorityFeePerGas());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.hyperledger.besu.ethereum.blockcreation.txselection.BlockSelectionContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionEvaluationContext;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionSelectionResults;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;

Expand Down Expand Up @@ -71,7 +70,7 @@ public TransactionSelectionResult evaluateTransactionPostProcessing(
*/
private boolean transactionCurrentPriceBelowMin(
final TransactionEvaluationContext evaluationContext) {
final PendingTransaction pendingTransaction = evaluationContext.getPendingTransaction();
final var pendingTransaction = evaluationContext.getPendingTransaction();
// Priority txs are exempt from this check
if (!pendingTransaction.hasPriority()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,7 @@ public void transactionSelectionPluginShouldWork_PreProcessing() {
new PluginTransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final TransactionEvaluationContext<? extends PendingTransaction>
evaluationContext) {
final TransactionEvaluationContext evaluationContext) {
if (evaluationContext
.getPendingTransaction()
.getTransaction()
Expand All @@ -670,8 +669,7 @@ public TransactionSelectionResult evaluateTransactionPreProcessing(

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final TransactionEvaluationContext<? extends PendingTransaction>
evaluationContext,
final TransactionEvaluationContext evaluationContext,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
return SELECTED;
Expand Down Expand Up @@ -727,15 +725,13 @@ public void transactionSelectionPluginShouldWork_PostProcessing() {
new PluginTransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
final TransactionEvaluationContext<? extends PendingTransaction>
evaluationContext) {
final TransactionEvaluationContext evaluationContext) {
return SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
final TransactionEvaluationContext<? extends PendingTransaction>
evaluationContext,
final TransactionEvaluationContext evaluationContext,
final org.hyperledger.besu.plugin.data.TransactionProcessingResult
processingResult) {
// the transaction with max gas +1 should fail
Expand Down Expand Up @@ -808,7 +804,7 @@ public void transactionSelectionPluginShouldBeNotifiedWhenTransactionSelectionCo
selector.buildTransactionListForBlock();

@SuppressWarnings("unchecked")
ArgumentCaptor<TransactionEvaluationContext<PendingTransaction>> argumentCaptor =
ArgumentCaptor<TransactionEvaluationContext> argumentCaptor =
ArgumentCaptor.forClass(TransactionEvaluationContext.class);

// selected transaction must be notified to the selector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public long getAddedAt() {
return addedAt;
}

@Override
public int memorySize() {
if (memorySize == NOT_INITIALIZED) {
memorySize = computeMemorySize();
Expand Down Expand Up @@ -291,6 +292,7 @@ public String toString() {
+ '}';
}

@Override
public String toTraceLog() {
return "{sequence: "
+ sequence
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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 = 'FEieWer0x6AdCmvf02G7yGQxS5JRxsIYrRDpqsNgQ+0='
knownHash = 'U/zVfjqq/stLY920xHh1N26KU+KoAdgEiV2nPWIFRIs='
}
check.dependsOn('checkAPIChanges')

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

import static org.hyperledger.besu.plugin.data.TransactionSelectionResult.SELECTED;

import org.hyperledger.besu.datatypes.PendingTransaction;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.TransactionProcessingResult;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;
Expand All @@ -30,13 +29,13 @@ public interface PluginTransactionSelector {
new PluginTransactionSelector() {
@Override
public TransactionSelectionResult evaluateTransactionPreProcessing(
TransactionEvaluationContext<? extends PendingTransaction> evaluationContext) {
TransactionEvaluationContext evaluationContext) {
return SELECTED;
}

@Override
public TransactionSelectionResult evaluateTransactionPostProcessing(
TransactionEvaluationContext<? extends PendingTransaction> evaluationContext,
TransactionEvaluationContext evaluationContext,
TransactionProcessingResult processingResult) {
return SELECTED;
}
Expand All @@ -60,7 +59,7 @@ default BlockAwareOperationTracer getOperationTracer() {
* @return TransactionSelectionResult that indicates whether to include the transaction
*/
TransactionSelectionResult evaluateTransactionPreProcessing(
TransactionEvaluationContext<? extends PendingTransaction> evaluationContext);
TransactionEvaluationContext evaluationContext);

/**
* Method called to decide whether a processed transaction is added to a block. The result can
Expand All @@ -71,8 +70,7 @@ TransactionSelectionResult evaluateTransactionPreProcessing(
* @return TransactionSelectionResult that indicates whether to include the transaction
*/
TransactionSelectionResult evaluateTransactionPostProcessing(
TransactionEvaluationContext<? extends PendingTransaction> evaluationContext,
TransactionProcessingResult processingResult);
TransactionEvaluationContext evaluationContext, TransactionProcessingResult processingResult);

/**
* Method called when a transaction is selected to be added to a block.
Expand All @@ -81,7 +79,7 @@ TransactionSelectionResult evaluateTransactionPostProcessing(
* @param processingResult The result of processing the selected transaction.
*/
default void onTransactionSelected(
final TransactionEvaluationContext<? extends PendingTransaction> evaluationContext,
final TransactionEvaluationContext evaluationContext,
final TransactionProcessingResult processingResult) {}

/**
Expand All @@ -91,6 +89,6 @@ default void onTransactionSelected(
* @param transactionSelectionResult The transaction selection result
*/
default void onTransactionNotSelected(
final TransactionEvaluationContext<? extends PendingTransaction> evaluationContext,
final TransactionEvaluationContext evaluationContext,
final TransactionSelectionResult transactionSelectionResult) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
/**
* This interface defines the context for evaluating a transaction. It provides methods to get the
* pending transaction, the evaluation timer, and the transaction gas price.
*
* @param <PT> the type of the pending transaction
*/
public interface TransactionEvaluationContext<PT extends PendingTransaction> {
public interface TransactionEvaluationContext {

/**
* Gets the pending block header
Expand All @@ -40,7 +38,7 @@ public interface TransactionEvaluationContext<PT extends PendingTransaction> {
*
* @return the pending transaction
*/
PT getPendingTransaction();
PendingTransaction getPendingTransaction();

/**
* Gets the stopwatch used for timing the evaluation.
Expand Down

0 comments on commit a818793

Please sign in to comment.