Skip to content

Commit

Permalink
Refactor and extend PluginTransactionPoolValidatorService
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Feb 28, 2024
1 parent 647750c commit db73383
Show file tree
Hide file tree
Showing 34 changed files with 133 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,18 @@
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;
import org.hyperledger.besu.plugin.services.PluginTransactionValidatorService;
import org.hyperledger.besu.plugin.services.PluginTransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
import org.hyperledger.besu.plugin.services.SecurityModuleService;
import org.hyperledger.besu.plugin.services.StorageService;
import org.hyperledger.besu.plugin.services.TransactionSelectionService;
import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBPlugin;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidatorFactory;
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuEventsImpl;
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.PermissioningServiceImpl;
import org.hyperledger.besu.services.PicoCLIOptionsImpl;
import org.hyperledger.besu.services.PluginTransactionValidatorServiceImpl;
import org.hyperledger.besu.services.PluginTransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
import org.hyperledger.besu.services.StorageServiceImpl;
Expand All @@ -71,7 +70,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -105,7 +103,8 @@ private BesuPluginContextImpl buildPluginContext(
besuPluginContext.addService(
TransactionSelectionService.class, transactionSelectionServiceImpl);
besuPluginContext.addService(
PluginTransactionValidatorService.class, new PluginTransactionValidatorServiceImpl());
PluginTransactionPoolValidatorService.class,
new PluginTransactionPoolValidatorServiceImpl());
final Path pluginsPath;
final String pluginDir = System.getProperty("besu.plugins.dir");
if (pluginDir == null || pluginDir.isEmpty()) {
Expand Down Expand Up @@ -205,9 +204,6 @@ public void startNode(final BesuNode node) {

final int maxPeers = 25;

final PluginTransactionValidatorFactory pluginTransactionValidatorFactory =
getPluginTransactionValidatorFactory(besuPluginContext);

final DataStorageConfiguration dataStorageConfiguration =
node.getDataStorageConfiguration() == null
? DataStorageConfiguration.DEFAULT_BONSAI_CONFIG
Expand All @@ -234,8 +230,7 @@ public void startNode(final BesuNode node) {
.maxPeers(maxPeers)
.maxRemotelyInitiatedPeers(15)
.networkConfiguration(node.getNetworkingConfiguration())
.randomPeerPriority(false)
.pluginTransactionValidatorFactory(pluginTransactionValidatorFactory);
.randomPeerPriority(false);

node.getGenesisConfig()
.map(GenesisConfigFile::fromConfig)
Expand Down Expand Up @@ -345,11 +340,4 @@ public void startConsoleCapture() {
public String getConsoleContents() {
throw new RuntimeException("Console contents can only be captured in process execution");
}

private PluginTransactionValidatorFactory getPluginTransactionValidatorFactory(
final BesuPluginContextImpl besuPluginContext) {
final Optional<PluginTransactionValidatorService> txValidatorService =
besuPluginContext.getService(PluginTransactionValidatorService.class);
return txValidatorService.map(PluginTransactionValidatorService::get).orElse(null);
}
}
21 changes: 7 additions & 14 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.PermissioningService;
import org.hyperledger.besu.plugin.services.PicoCLIOptions;
import org.hyperledger.besu.plugin.services.PluginTransactionValidatorService;
import org.hyperledger.besu.plugin.services.PluginTransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.PrivacyPluginService;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
import org.hyperledger.besu.plugin.services.SecurityModuleService;
Expand All @@ -174,14 +174,13 @@
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
import org.hyperledger.besu.plugin.services.storage.PrivacyKeyValueStorageFactory;
import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBPlugin;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidatorFactory;
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuEventsImpl;
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.BlockchainServiceImpl;
import org.hyperledger.besu.services.PermissioningServiceImpl;
import org.hyperledger.besu.services.PicoCLIOptionsImpl;
import org.hyperledger.besu.services.PluginTransactionValidatorServiceImpl;
import org.hyperledger.besu.services.PluginTransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.PrivacyPluginServiceImpl;
import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
Expand Down Expand Up @@ -370,7 +369,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
P2PDiscoveryOptionGroup p2PDiscoveryOptionGroup = new P2PDiscoveryOptionGroup();

private final TransactionSelectionServiceImpl transactionSelectionServiceImpl;
private final PluginTransactionValidatorServiceImpl transactionValidatorServiceImpl;
private final PluginTransactionPoolValidatorServiceImpl transactionValidatorServiceImpl;
private final BlockchainServiceImpl blockchainServiceImpl;

static class P2PDiscoveryOptionGroup {
Expand Down Expand Up @@ -956,7 +955,7 @@ public BesuCommand(
new PkiBlockCreationConfigurationProvider(),
new RpcEndpointServiceImpl(),
new TransactionSelectionServiceImpl(),
new PluginTransactionValidatorServiceImpl(),
new PluginTransactionPoolValidatorServiceImpl(),
new BlockchainServiceImpl());
}

Expand Down Expand Up @@ -998,7 +997,7 @@ protected BesuCommand(
final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider,
final RpcEndpointServiceImpl rpcEndpointServiceImpl,
final TransactionSelectionServiceImpl transactionSelectionServiceImpl,
final PluginTransactionValidatorServiceImpl transactionValidatorServiceImpl,
final PluginTransactionPoolValidatorServiceImpl transactionValidatorServiceImpl,
final BlockchainServiceImpl blockchainServiceImpl) {
this.besuComponent = besuComponent;
this.logger = besuComponent.getBesuCommandLogger();
Expand Down Expand Up @@ -1210,7 +1209,7 @@ private void preparePlugins() {
besuPluginContext.addService(
TransactionSelectionService.class, transactionSelectionServiceImpl);
besuPluginContext.addService(
PluginTransactionValidatorService.class, transactionValidatorServiceImpl);
PluginTransactionPoolValidatorService.class, transactionValidatorServiceImpl);
besuPluginContext.addService(BlockchainService.class, blockchainServiceImpl);

// register built-in plugins
Expand Down Expand Up @@ -1791,7 +1790,6 @@ public BesuControllerBuilder getControllerBuilder() {
.synchronizerConfiguration(buildSyncConfig())
.ethProtocolConfiguration(unstableEthProtocolOptions.toDomainObject())
.networkConfiguration(unstableNetworkingOptions.toDomainObject())
.pluginTransactionValidatorFactory(getPluginTransactionValidatorFactory())
.dataDirectory(dataDir())
.dataStorageConfiguration(getDataStorageConfiguration())
.miningParameters(getMiningParameters())
Expand Down Expand Up @@ -1822,12 +1820,6 @@ public BesuControllerBuilder getControllerBuilder() {
.cacheLastBlocks(numberOfblocksToCache);
}

private PluginTransactionValidatorFactory getPluginTransactionValidatorFactory() {
final Optional<PluginTransactionValidatorService> txSValidatorService =
besuPluginContext.getService(PluginTransactionValidatorService.class);
return txSValidatorService.map(PluginTransactionValidatorService::get).orElse(null);
}

private JsonRpcConfiguration createEngineJsonRpcConfiguration(
final Integer engineListenPort, final List<String> allowCallsFrom) {
jsonRpcHttpOptions.checkDependencies(logger, commandLine);
Expand Down Expand Up @@ -2094,6 +2086,7 @@ private SynchronizerConfiguration buildSyncConfig() {
}

private TransactionPoolConfiguration buildTransactionPoolConfiguration() {
transactionPoolOptions.setPluginTransactionValidatorService(transactionValidatorServiceImpl);
final var txPoolConf = transactionPoolOptions.toDomainObject();
final var txPoolConfBuilder =
ImmutableTransactionPoolConfiguration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.plugin.services.PluginTransactionPoolValidatorService;
import org.hyperledger.besu.util.number.Fraction;
import org.hyperledger.besu.util.number.Percentage;

Expand Down Expand Up @@ -57,6 +58,8 @@ public class TransactionPoolOptions implements CLIOptions<TransactionPoolConfigu
private static final String TX_POOL_PRIORITY_SENDERS = "--tx-pool-priority-senders";
private static final String TX_POOL_MIN_GAS_PRICE = "--tx-pool-min-gas-price";

private PluginTransactionPoolValidatorService pluginTransactionPoolValidatorService;

@CommandLine.Option(
names = {TX_POOL_IMPLEMENTATION},
paramLabel = "<Enum>",
Expand Down Expand Up @@ -252,6 +255,11 @@ public static TransactionPoolOptions create() {
return new TransactionPoolOptions();
}

public void setPluginTransactionValidatorService(
final PluginTransactionPoolValidatorService pluginTransactionPoolValidatorService) {
this.pluginTransactionPoolValidatorService = pluginTransactionPoolValidatorService;
}

/**
* Create Transaction Pool Options from Transaction Pool Configuration.
*
Expand All @@ -277,6 +285,7 @@ public static TransactionPoolOptions fromConfig(final TransactionPoolConfigurati
config.getTxPoolLimitByAccountPercentage();
options.sequencedOptions.txPoolMaxSize = config.getTxPoolMaxSize();
options.sequencedOptions.pendingTxRetentionPeriod = config.getPendingTxRetentionPeriod();
options.pluginTransactionPoolValidatorService = config.getPluginTransactionValidatorService();
options.unstableOptions.txMessageKeepAliveSeconds =
config.getUnstable().getTxMessageKeepAliveSeconds();
options.unstableOptions.eth65TrxAnnouncedBufferingPeriod =
Expand Down Expand Up @@ -331,6 +340,7 @@ public TransactionPoolConfiguration toDomainObject() {
.txPoolLimitByAccountPercentage(sequencedOptions.txPoolLimitByAccountPercentage)
.txPoolMaxSize(sequencedOptions.txPoolMaxSize)
.pendingTxRetentionPeriod(sequencedOptions.pendingTxRetentionPeriod)
.pluginTransactionValidatorService(pluginTransactionPoolValidatorService)
.unstable(
ImmutableTransactionPoolConfiguration.Unstable.builder()
.txMessageKeepAliveSeconds(unstableOptions.txMessageKeepAliveSeconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.permissioning.NodeMessagePermissioningProvider;
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidatorFactory;

import java.io.Closeable;
import java.math.BigInteger;
Expand Down Expand Up @@ -189,8 +188,6 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
/** the Dagger configured context that can provide dependencies */
protected Optional<BesuComponent> besuComponent = Optional.empty();

private PluginTransactionValidatorFactory pluginTransactionValidatorFactory;

private int numberOfBlocksToCache = 0;

/**
Expand Down Expand Up @@ -532,18 +529,6 @@ public BesuControllerBuilder randomPeerPriority(final Boolean randomPeerPriority
return this;
}

/**
* sets the pluginTransactionValidatorFactory
*
* @param pluginTransactionValidatorFactory factory that creates plugin transaction Validators
* @return the besu controller builder
*/
public BesuControllerBuilder pluginTransactionValidatorFactory(
final PluginTransactionValidatorFactory pluginTransactionValidatorFactory) {
this.pluginTransactionValidatorFactory = pluginTransactionValidatorFactory;
return this;
}

/**
* Build besu controller.
*
Expand Down Expand Up @@ -697,7 +682,6 @@ public BesuController build() {
metricsSystem,
syncState,
transactionPoolConfiguration,
pluginTransactionValidatorFactory,
besuComponent.map(BesuComponent::getBlobCache).orElse(new BlobCache()),
miningParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
*/
package org.hyperledger.besu.services;

import org.hyperledger.besu.plugin.services.PluginTransactionValidatorService;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionValidatorFactory;
import org.hyperledger.besu.plugin.services.PluginTransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolTransactionValidator;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolValidatorFactory;

import java.util.Optional;

/** The Transaction Validation service implementation. */
public class PluginTransactionValidatorServiceImpl implements PluginTransactionValidatorService {
public class PluginTransactionPoolValidatorServiceImpl
implements PluginTransactionPoolValidatorService {

private PluginTransactionValidatorFactory factory;
private Optional<PluginTransactionPoolValidatorFactory> factory;

@Override
public PluginTransactionValidatorFactory get() {
return factory;
public PluginTransactionPoolTransactionValidator createTransactionValidator() {
return factory
.map(PluginTransactionPoolValidatorFactory::createTransactionValidator)
.orElse(PluginTransactionPoolTransactionValidator.VALIDATE_ALL);
}

@Override
public void registerTransactionValidatorFactory(
final PluginTransactionValidatorFactory transactionValidatorFactory) {
factory = transactionValidatorFactory;
final PluginTransactionPoolValidatorFactory transactionValidatorFactory) {
factory = Optional.ofNullable(transactionValidatorFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.BlockchainServiceImpl;
import org.hyperledger.besu.services.PermissioningServiceImpl;
import org.hyperledger.besu.services.PluginTransactionValidatorServiceImpl;
import org.hyperledger.besu.services.PluginTransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.PrivacyPluginServiceImpl;
import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
Expand Down Expand Up @@ -294,8 +294,6 @@ public void initMocks() throws Exception {
when(mockControllerBuilder.maxPeers(anyInt())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.maxRemotelyInitiatedPeers(anyInt()))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.pluginTransactionValidatorFactory(any()))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.besuComponent(any(BesuComponent.class)))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.cacheLastBlocks(any())).thenReturn(mockControllerBuilder);
Expand Down Expand Up @@ -571,7 +569,7 @@ public static class TestBesuCommand extends BesuCommand {
pkiBlockCreationConfigProvider,
rpcEndpointServiceImpl,
new TransactionSelectionServiceImpl(),
new PluginTransactionValidatorServiceImpl(),
new PluginTransactionPoolValidatorServiceImpl(),
new BlockchainServiceImpl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public void setUp() {
new NoOpMetricsSystem(),
syncState,
txPoolConfig,
null,
new BlobCache(),
MiningParameters.newDefault());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ private TransactionPool createTransactionPool() {
mock(TransactionBroadcaster.class),
ethContext,
new TransactionPoolMetrics(metricsSystem),
conf,
null);
conf);
transactionPool.setEnabled();
return transactionPool;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ private TransactionPool createTransactionPool() {
mock(TransactionBroadcaster.class),
cliqueEthContext,
new TransactionPoolMetrics(metricsSystem),
conf,
null);
conf);

transactionPool.setEnabled();
return transactionPool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ private static ControllerAndState createControllerAndFinalState(
mock(TransactionBroadcaster.class),
ethContext,
new TransactionPoolMetrics(metricsSystem),
poolConf,
null);
poolConf);

transactionPool.setEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public BlockHeaderValidator.Builder createBlockHeaderRuleset(
mock(TransactionBroadcaster.class),
ethContext,
new TransactionPoolMetrics(metricsSystem),
poolConf,
null);
poolConf);

transactionPool.setEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ public void setUp() {
mock(TransactionBroadcaster.class),
ethContext,
new TransactionPoolMetrics(metricsSystem),
poolConf,
null);
poolConf);

this.transactionPool.setEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,7 @@ private static ControllerAndState createControllerAndFinalState(
mock(TransactionBroadcaster.class),
ethContext,
new TransactionPoolMetrics(metricsSystem),
poolConf,
null);
poolConf);

transactionPool.setEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public void setUp() {
batchAddedListener,
ethContext,
new TransactionPoolMetrics(metricsSystem),
TransactionPoolConfiguration.DEFAULT,
null);
TransactionPoolConfiguration.DEFAULT);
transactionPool.setEnabled();
final BlockchainQueries blockchainQueries =
new BlockchainQueries(blockchain, protocolContext.getWorldStateArchive());
Expand Down
Loading

0 comments on commit db73383

Please sign in to comment.