Skip to content

Commit

Permalink
Allow plugins to propose transactions during block creation (#8268)
Browse files Browse the repository at this point in the history
* Allow plugins to propose transactions during block creation

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>

* Apply suggestions from code review

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>

* Document AbstractStatefulTransactionSelector

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>

* Unit tests for BlockSizeTransactionSelectorTest

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>

---------

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 authored Feb 12, 2025
1 parent 0ad85ec commit 428a638
Show file tree
Hide file tree
Showing 28 changed files with 1,284 additions and 321 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fast Sync
### Additions and Improvements
- Add TLS/mTLS options and configure the GraphQL HTTP service[#7910](https://github.com/hyperledger/besu/pull/7910)
- Allow plugins to propose transactions during block creation [#8268](https://github.com/hyperledger/besu/pull/8268)
### Bug fixes
- Upgrade Netty to version 4.1.118 to fix CVE-2025-24970 [#8275](https://github.com/hyperledger/besu/pull/8275)
- Add missing RPC method `debug_accountRange` to `RpcMethod.java` and implemented its handler. [#8153](https://github.com/hyperledger/besu/issues/8153)
Expand Down
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 @@ -55,8 +55,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 @@ -213,11 +213,15 @@ protected BlockCreationResult createBlock(

throwIfStopped();

final PluginTransactionSelector pluginTransactionSelector =
miningConfiguration.getTransactionSelectionService().createPluginTransactionSelector();

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

operationTracer.traceStartBlock(processableBlockHeader, miningBeneficiary);
BlockProcessingContext blockProcessingContext =
Expand All @@ -240,6 +244,7 @@ protected BlockCreationResult createBlock(
miningBeneficiary,
newProtocolSpec,
pluginTransactionSelector,
selectorsStateManager,
parentHeader);
transactionResults.logSelectionStats();
timings.register("txsSelection");
Expand Down Expand Up @@ -362,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 @@ -391,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 428a638

Please sign in to comment.