Skip to content

Commit

Permalink
Allow for plugins to include txs during block creation
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Jan 30, 2025
1 parent f79e274 commit b7f23c3
Show file tree
Hide file tree
Showing 26 changed files with 1,033 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,42 @@
*/
package org.hyperledger.besu.services;

import static com.google.common.base.Preconditions.checkState;

import org.hyperledger.besu.plugin.data.ProcessableBlockHeader;
import org.hyperledger.besu.plugin.services.TransactionSelectionService;
import org.hyperledger.besu.plugin.services.txselection.BlockTransactionSelectionService;
import org.hyperledger.besu.plugin.services.txselection.PluginTransactionSelector;
import org.hyperledger.besu.plugin.services.txselection.PluginTransactionSelectorFactory;

import java.util.Optional;
import org.hyperledger.besu.plugin.services.txselection.SelectorsStateManager;

/** The Transaction Selection service implementation. */
public class TransactionSelectionServiceImpl implements TransactionSelectionService {

/** Default Constructor. */
public TransactionSelectionServiceImpl() {}

private Optional<PluginTransactionSelectorFactory> factory = Optional.empty();
private PluginTransactionSelectorFactory factory = PluginTransactionSelectorFactory.NO_OP_FACTORY;

@Override
public PluginTransactionSelector createPluginTransactionSelector(
final SelectorsStateManager selectorsStateManager) {
return factory.create(selectorsStateManager);
}

@Override
public PluginTransactionSelector createPluginTransactionSelector() {
return factory
.map(PluginTransactionSelectorFactory::create)
.orElse(PluginTransactionSelector.ACCEPT_ALL);
public void selectPendingTransactions(
final BlockTransactionSelectionService selectionService,
final ProcessableBlockHeader pendingBlockHeader) {
factory.selectPendingTransactions(selectionService, pendingBlockHeader);
}

@Override
public void registerPluginTransactionSelectorFactory(
final PluginTransactionSelectorFactory pluginTransactionSelectorFactory) {
factory = Optional.ofNullable(pluginTransactionSelectorFactory);
checkState(
factory == PluginTransactionSelectorFactory.NO_OP_FACTORY,
"PluginTransactionSelectorFactory was already registered");
factory = pluginTransactionSelectorFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.plugin.services.tracer.BlockAwareOperationTracer;
import org.hyperledger.besu.plugin.services.txselection.PluginTransactionSelector;
import org.hyperledger.besu.plugin.services.txselection.SelectorsStateManager;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -216,13 +216,16 @@ protected BlockCreationResult createBlock(

throwIfStopped();

final PluginTransactionSelector pluginTransactionSelector =
miningConfiguration.getTransactionSelectionService().createPluginTransactionSelector();
final var selectorsStateManager = new SelectorsStateManager();
final var pluginTransactionSelector =
miningConfiguration
.getTransactionSelectionService()
.createPluginTransactionSelector(selectorsStateManager);
final var operationTracer = pluginTransactionSelector.getOperationTracer();
pluginTransactionSelector
.getOperationTracer()
.traceStartBlock(processableBlockHeader, miningBeneficiary);

final BlockAwareOperationTracer operationTracer =
pluginTransactionSelector.getOperationTracer();

operationTracer.traceStartBlock(processableBlockHeader, miningBeneficiary);
timings.register("preTxsSelection");
final TransactionSelectionResults transactionResults =
selectTransactions(
Expand All @@ -232,6 +235,7 @@ protected BlockCreationResult createBlock(
miningBeneficiary,
newProtocolSpec,
pluginTransactionSelector,
selectorsStateManager,
parentHeader);
transactionResults.logSelectionStats();
timings.register("txsSelection");
Expand Down Expand Up @@ -363,6 +367,7 @@ private TransactionSelectionResults selectTransactions(
final Address miningBeneficiary,
final ProtocolSpec protocolSpec,
final PluginTransactionSelector pluginTransactionSelector,
final SelectorsStateManager selectorsStateManager,
final BlockHeader parentHeader)
throws RuntimeException {
final MainnetTransactionProcessor transactionProcessor = protocolSpec.getTransactionProcessor();
Expand Down Expand Up @@ -392,7 +397,8 @@ private TransactionSelectionResults selectTransactions(
protocolSpec.getGasLimitCalculator(),
protocolSpec.getBlockHashProcessor(),
pluginTransactionSelector,
ethScheduler);
ethScheduler,
selectorsStateManager);

if (transactions.isPresent()) {
return selector.evaluateTransactions(transactions.get());
Expand Down
Loading

0 comments on commit b7f23c3

Please sign in to comment.