Skip to content

Commit

Permalink
Eliminates dependency from Controller on BesuComponent
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Florentine <justin+github@florentine.us>
  • Loading branch information
jflo committed Aug 14, 2024
1 parent 0e8b444 commit 246b92f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hyperledger.besu.cli.config.EthNetworkConfig;
import org.hyperledger.besu.cli.config.NetworkName;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.components.BesuPluginContextModule;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.controller.BesuControllerBuilder;
Expand Down Expand Up @@ -92,7 +93,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;
import javax.inject.Inject;
Expand Down Expand Up @@ -387,7 +387,6 @@ public EthNetworkConfig provideEthNetworkConfig(
}

@Provides
@Named("besuPluginContext")
public BesuPluginContextImpl providePluginContext(
final StorageServiceImpl storageService,
final SecurityModuleServiceImpl securityModuleService,
Expand Down Expand Up @@ -501,17 +500,17 @@ TransactionPoolConfiguration provideTransactionPoolConfiguration(
static class MockBesuCommandModule {

@Provides
BesuCommand provideBesuCommand(final AcceptanceTestBesuComponent component) {
BesuCommand provideBesuCommand(final BesuPluginContextImpl pluginContext) {
final BesuCommand besuCommand =
new BesuCommand(
component,
RlpBlockImporter::new,
JsonBlockImporter::new,
RlpBlockExporter::new,
new RunnerBuilder(),
new BesuController.Builder(),
Optional.ofNullable(component.getBesuPluginContext()).orElse(null),
System.getenv());
pluginContext,
System.getenv(),
LoggerFactory.getLogger(MockBesuCommandModule.class));
besuCommand.toCommandLine();
return besuCommand;
}
Expand Down
46 changes: 22 additions & 24 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
import org.hyperledger.besu.cli.util.CommandLineUtils;
import org.hyperledger.besu.cli.util.ConfigDefaultValueProviderStrategy;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.components.BesuComponent;
import org.hyperledger.besu.config.CheckpointConfigOptions;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
Expand Down Expand Up @@ -865,40 +864,35 @@ static class PrivacyOptionGroup {
private BesuController besuController;
private BesuConfigurationImpl pluginCommonConfiguration;

private BesuComponent besuComponent;
private final Supplier<ObservableMetricsSystem> metricsSystem =
Suppliers.memoize(
() ->
besuComponent == null || besuComponent.getObservableMetricsSystem() == null
? MetricsSystemFactory.create(metricsConfiguration())
: besuComponent.getObservableMetricsSystem());
Suppliers.memoize(() -> MetricsSystemFactory.create(metricsConfiguration()));

private Vertx vertx;
private EnodeDnsConfiguration enodeDnsConfiguration;
private KeyValueStorageProvider keyValueStorageProvider;

/**
* Besu command constructor.
*
* @param besuComponent BesuComponent which acts as our application context
* @param rlpBlockImporter RlpBlockImporter supplier
* @param jsonBlockImporterFactory instance of {@code Function<BesuController, JsonBlockImporter>}
* @param rlpBlockExporterFactory instance of {@code Function<Blockchain, RlpBlockExporter>}
* @param runnerBuilder instance of RunnerBuilder
* @param controllerBuilder instance of BesuController.Builder
* @param besuPluginContext instance of BesuPluginContextImpl
* @param environment Environment variables map
* @param commandLogger instance of Logger for outputting to the CLI
*/
public BesuCommand(
final BesuComponent besuComponent,
final Supplier<RlpBlockImporter> rlpBlockImporter,
final Function<BesuController, JsonBlockImporter> jsonBlockImporterFactory,
final Function<Blockchain, RlpBlockExporter> rlpBlockExporterFactory,
final RunnerBuilder runnerBuilder,
final BesuController.Builder controllerBuilder,
final BesuPluginContextImpl besuPluginContext,
final Map<String, String> environment) {
final Map<String, String> environment,
final Logger commandLogger) {
this(
besuComponent,
rlpBlockImporter,
jsonBlockImporterFactory,
rlpBlockExporterFactory,
Expand All @@ -914,13 +908,13 @@ public BesuCommand(
new TransactionSelectionServiceImpl(),
new TransactionPoolValidatorServiceImpl(),
new TransactionSimulationServiceImpl(),
new BlockchainServiceImpl());
new BlockchainServiceImpl(),
commandLogger);
}

/**
* Overloaded Besu command constructor visible for testing.
*
* @param besuComponent BesuComponent which acts as our application context
* @param rlpBlockImporter RlpBlockImporter supplier
* @param jsonBlockImporterFactory instance of {@code Function<BesuController, JsonBlockImporter>}
* @param rlpBlockExporterFactory instance of {@code Function<Blockchain, RlpBlockExporter>}
Expand All @@ -937,10 +931,10 @@ public BesuCommand(
* @param transactionValidatorServiceImpl instance of TransactionValidatorServiceImpl
* @param transactionSimulationServiceImpl instance of TransactionSimulationServiceImpl
* @param blockchainServiceImpl instance of BlockchainServiceImpl
* @param commandLogger instance of Logger for outputting to the CLI
*/
@VisibleForTesting
protected BesuCommand(
final BesuComponent besuComponent,
final Supplier<RlpBlockImporter> rlpBlockImporter,
final Function<BesuController, JsonBlockImporter> jsonBlockImporterFactory,
final Function<Blockchain, RlpBlockExporter> rlpBlockExporterFactory,
Expand All @@ -956,9 +950,10 @@ protected BesuCommand(
final TransactionSelectionServiceImpl transactionSelectionServiceImpl,
final TransactionPoolValidatorServiceImpl transactionValidatorServiceImpl,
final TransactionSimulationServiceImpl transactionSimulationServiceImpl,
final BlockchainServiceImpl blockchainServiceImpl) {
this.besuComponent = besuComponent;
this.logger = besuComponent.getBesuCommandLogger();
final BlockchainServiceImpl blockchainServiceImpl,
final Logger commandLogger) {

this.logger = commandLogger;
this.rlpBlockImporter = rlpBlockImporter;
this.rlpBlockExporterFactory = rlpBlockExporterFactory;
this.jsonBlockImporterFactory = jsonBlockImporterFactory;
Expand All @@ -970,8 +965,13 @@ protected BesuCommand(
this.securityModuleService = securityModuleService;
this.permissioningService = permissioningService;
this.privacyPluginService = privacyPluginService;
this.pluginCommonConfiguration = new BesuConfigurationImpl();
besuPluginContext.addService(BesuConfiguration.class, pluginCommonConfiguration);
if (besuPluginContext.getService(BesuConfigurationImpl.class).isPresent()) {
this.pluginCommonConfiguration =
besuPluginContext.getService(BesuConfigurationImpl.class).get();
} else {
this.pluginCommonConfiguration = new BesuConfigurationImpl();
besuPluginContext.addService(BesuConfiguration.class, this.pluginCommonConfiguration);
}
this.rpcEndpointServiceImpl = rpcEndpointServiceImpl;
this.transactionSelectionServiceImpl = transactionSelectionServiceImpl;
this.transactionValidatorServiceImpl = transactionValidatorServiceImpl;
Expand Down Expand Up @@ -1814,9 +1814,7 @@ private void ensureAllNodesAreInAllowlist(
*/
public BesuController buildController() {
try {
return this.besuComponent == null
? getControllerBuilder().build()
: getControllerBuilder().besuComponent(this.besuComponent).build();
return setupControllerBuilder().build();
} catch (final Exception e) {
throw new ExecutionException(this.commandLine, e.getMessage(), e);
}
Expand All @@ -1827,7 +1825,7 @@ public BesuController buildController() {
*
* @return instance of BesuControllerBuilder
*/
public BesuControllerBuilder getControllerBuilder() {
public BesuControllerBuilder setupControllerBuilder() {
pluginCommonConfiguration
.init(dataDir(), dataDir().resolve(DATABASE_PATH), getDataStorageConfiguration())
.withMiningParameters(miningParametersSupplier.get())
Expand Down Expand Up @@ -2793,7 +2791,7 @@ && getDataStorageConfiguration().getBonsaiLimitTrieLogsEnabled()) {
builder.setTxPoolImplementation(buildTransactionPoolConfiguration().getTxPoolImplementation());
builder.setWorldStateUpdateMode(unstableEvmOptions.toDomainObject().worldUpdaterMode());

builder.setPluginContext(besuComponent.getBesuPluginContext());
builder.setPluginContext(this.besuPluginContext);

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private BesuController createController() {
// Set some defaults
return parentCommand
.parentCommand
.getControllerBuilder()
.setupControllerBuilder()
// set to mainnet genesis block so validation rules won't reject it.
.clock(Clock.fixed(Instant.ofEpochSecond(startTime), ZoneOffset.UTC))
.miningParameters(getMiningParameters())
Expand Down Expand Up @@ -374,7 +374,7 @@ public void run() {
private BesuController createBesuController() {
return parentCommand
.parentCommand
.getControllerBuilder()
.setupControllerBuilder()
.miningParameters(MiningParameters.newDefault())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static BesuController createBesuController() {
// disable limit trie logs to avoid preloading during subcommand execution
return parentCommand
.besuCommand
.getControllerBuilder()
.setupControllerBuilder()
.dataStorageConfiguration(
ImmutableDataStorageConfiguration.copyOf(config).withBonsaiLimitTrieLogsEnabled(false))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
import org.hyperledger.besu.services.BesuPluginContextImpl;

import java.util.Optional;
import javax.inject.Named;
import javax.inject.Singleton;

Expand All @@ -42,17 +42,19 @@ public BesuCommandModule() {}

@Provides
@Singleton
BesuCommand provideBesuCommand(final BesuComponent besuComponent) {
BesuCommand provideBesuCommand(
final BesuPluginContextImpl pluginContext,
final @Named("besuCommandLogger") Logger commandLogger) {
final BesuCommand besuCommand =
new BesuCommand(
besuComponent,
RlpBlockImporter::new,
JsonBlockImporter::new,
RlpBlockExporter::new,
new RunnerBuilder(),
new BesuController.Builder(),
Optional.ofNullable(besuComponent.getBesuPluginContext()).orElse(null),
System.getenv());
pluginContext,
System.getenv(),
commandLogger);
besuCommand.toCommandLine();
return besuCommand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public interface BesuComponent {
*
* @return BesuPluginContextImpl
*/
@Named("besuPluginContext")
BesuPluginContextImpl getBesuPluginContext();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
*/
package org.hyperledger.besu.components;

import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuPluginContextImpl;

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
Expand All @@ -29,15 +30,23 @@ public class BesuPluginContextModule {
/** Default constructor. */
public BesuPluginContextModule() {}

@Provides
@Singleton
BesuConfigurationImpl provideBesuPluginConfig() {
return new BesuConfigurationImpl();
}

/**
* Creates a BesuPluginContextImpl, used for plugin service discovery.
*
* @param pluginConfig the BesuConfigurationImpl
* @return the BesuPluginContext
*/
@Provides
@Named("besuPluginContext")
@Singleton
public BesuPluginContextImpl provideBesuPluginContext() {
return new BesuPluginContextImpl();
public BesuPluginContextImpl provideBesuPluginContext(final BesuConfigurationImpl pluginConfig) {
BesuPluginContextImpl retval = new BesuPluginContextImpl();
retval.addService(BesuConfiguration.class, pluginConfig);
return retval;
}
}
Loading

0 comments on commit 246b92f

Please sign in to comment.