Skip to content
/ besu Public
forked from hyperledger/besu

Commit

Permalink
Refactor and extend PluginTransactionPoolValidatorService (hyperledge…
Browse files Browse the repository at this point in the history
…r#6636)

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: amsmota <antonio.mota@citi.com>
  • Loading branch information
fab-10 authored and amsmota committed Apr 16, 2024
1 parent 3a82cf2 commit aa5a829
Show file tree
Hide file tree
Showing 41 changed files with 178 additions and 189 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Update commons-compress to 1.26.0 [#6648](https://github.com/hyperledger/besu/pull/6648)
- Add blob transaction support to `eth_call` [#6661](https://github.com/hyperledger/besu/pull/6661)
- Add blobs to `eth_feeHistory` [#6679](https://github.com/hyperledger/besu/pull/6679)
- Refactor and extend `TransactionPoolValidatorService` [#6636](https://github.com/hyperledger/besu/pull/6636)

### Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@
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.RpcEndpointService;
import org.hyperledger.besu.plugin.services.SecurityModuleService;
import org.hyperledger.besu.plugin.services.StorageService;
import org.hyperledger.besu.plugin.services.TransactionPoolValidatorService;
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.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
import org.hyperledger.besu.services.StorageServiceImpl;
import org.hyperledger.besu.services.TransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.TransactionSelectionServiceImpl;

import java.io.File;
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,7 @@ private BesuPluginContextImpl buildPluginContext(
besuPluginContext.addService(
TransactionSelectionService.class, transactionSelectionServiceImpl);
besuPluginContext.addService(
PluginTransactionValidatorService.class, new PluginTransactionValidatorServiceImpl());
TransactionPoolValidatorService.class, new TransactionPoolValidatorServiceImpl());
final Path pluginsPath;
final String pluginDir = System.getProperty("besu.plugins.dir");
if (pluginDir == null || pluginDir.isEmpty()) {
Expand Down Expand Up @@ -205,8 +203,6 @@ public void startNode(final BesuNode node) {

final int maxPeers = 25;

final PluginTransactionValidatorFactory pluginTransactionValidatorFactory =
getPluginTransactionValidatorFactory(besuPluginContext);
builder
.synchronizerConfiguration(new SynchronizerConfiguration.Builder().build())
.dataDirectory(node.homeDirectory())
Expand All @@ -228,8 +224,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 @@ -339,11 +334,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,12 +160,12 @@
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.PrivacyPluginService;
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.TraceService;
import org.hyperledger.besu.plugin.services.TransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.TransactionSelectionService;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
Expand All @@ -174,19 +174,18 @@
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.PrivacyPluginServiceImpl;
import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
import org.hyperledger.besu.services.StorageServiceImpl;
import org.hyperledger.besu.services.TraceServiceImpl;
import org.hyperledger.besu.services.TransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.TransactionSelectionServiceImpl;
import org.hyperledger.besu.services.kvstore.InMemoryStoragePlugin;
import org.hyperledger.besu.util.InvalidConfigurationException;
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 TransactionPoolValidatorServiceImpl 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 TransactionPoolValidatorServiceImpl(),
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 TransactionPoolValidatorServiceImpl 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);
TransactionPoolValidatorService.class, transactionValidatorServiceImpl);
besuPluginContext.addService(BlockchainService.class, blockchainServiceImpl);

// register built-in plugins
Expand Down Expand Up @@ -1812,7 +1811,6 @@ public BesuControllerBuilder getControllerBuilder() {
.synchronizerConfiguration(buildSyncConfig())
.ethProtocolConfiguration(unstableEthProtocolOptions.toDomainObject())
.networkConfiguration(unstableNetworkingOptions.toDomainObject())
.pluginTransactionValidatorFactory(getPluginTransactionValidatorFactory())
.dataDirectory(dataDir())
.dataStorageConfiguration(getDataStorageConfiguration())
.miningParameters(getMiningParameters())
Expand Down Expand Up @@ -1843,12 +1841,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 @@ -2115,6 +2107,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.TransactionPoolValidatorService;
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 TransactionPoolValidatorService transactionPoolValidatorService;

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

/**
* Set the plugin txpool validator service
*
* @param transactionPoolValidatorService the plugin txpool validator service
*/
public void setPluginTransactionValidatorService(
final TransactionPoolValidatorService transactionPoolValidatorService) {
this.transactionPoolValidatorService = transactionPoolValidatorService;
}

/**
* Create Transaction Pool Options from Transaction Pool Configuration.
*
Expand All @@ -277,6 +290,7 @@ public static TransactionPoolOptions fromConfig(final TransactionPoolConfigurati
config.getTxPoolLimitByAccountPercentage();
options.sequencedOptions.txPoolMaxSize = config.getTxPoolMaxSize();
options.sequencedOptions.pendingTxRetentionPeriod = config.getPendingTxRetentionPeriod();
options.transactionPoolValidatorService = config.getTransactionPoolValidatorService();
options.unstableOptions.txMessageKeepAliveSeconds =
config.getUnstable().getTxMessageKeepAliveSeconds();
options.unstableOptions.eth65TrxAnnouncedBufferingPeriod =
Expand Down Expand Up @@ -331,6 +345,7 @@ public TransactionPoolConfiguration toDomainObject() {
.txPoolLimitByAccountPercentage(sequencedOptions.txPoolLimitByAccountPercentage)
.txPoolMaxSize(sequencedOptions.txPoolMaxSize)
.pendingTxRetentionPeriod(sequencedOptions.pendingTxRetentionPeriod)
.transactionPoolValidatorService(transactionPoolValidatorService)
.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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.services;

import org.hyperledger.besu.plugin.services.TransactionPoolValidatorService;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolValidator;
import org.hyperledger.besu.plugin.services.txvalidator.PluginTransactionPoolValidatorFactory;

import java.util.Optional;

/** The Transaction pool validator service implementation. */
public class TransactionPoolValidatorServiceImpl implements TransactionPoolValidatorService {

private Optional<PluginTransactionPoolValidatorFactory> factory = Optional.empty();

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

@Override
public void registerPluginTransactionValidatorFactory(
final PluginTransactionPoolValidatorFactory pluginTransactionPoolValidatorFactory) {
factory = Optional.ofNullable(pluginTransactionPoolValidatorFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@
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.PrivacyPluginServiceImpl;
import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import org.hyperledger.besu.services.SecurityModuleServiceImpl;
import org.hyperledger.besu.services.StorageServiceImpl;
import org.hyperledger.besu.services.TransactionPoolValidatorServiceImpl;
import org.hyperledger.besu.services.TransactionSelectionServiceImpl;
import org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage;

Expand Down Expand Up @@ -302,8 +302,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 @@ -579,7 +577,7 @@ public static class TestBesuCommand extends BesuCommand {
pkiBlockCreationConfigProvider,
rpcEndpointServiceImpl,
new TransactionSelectionServiceImpl(),
new PluginTransactionValidatorServiceImpl(),
new TransactionPoolValidatorServiceImpl(),
new BlockchainServiceImpl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,9 @@ protected TransactionPoolOptions optionsFromDomainObject(
protected TransactionPoolOptions getOptionsFromBesuCommand(final TestBesuCommand besuCommand) {
return besuCommand.getTransactionPoolOptions();
}

@Override
protected String[] getNonOptionFields() {
return new String[] {"transactionPoolValidatorService"};
}
}
Loading

0 comments on commit aa5a829

Please sign in to comment.