From 246b92f475ea42cd8f944a536053a4c620b22e6d Mon Sep 17 00:00:00 2001 From: Justin Florentine Date: Wed, 14 Aug 2024 15:45:17 -0400 Subject: [PATCH] Eliminates dependency from Controller on BesuComponent Signed-off-by: Justin Florentine --- .../dsl/node/ThreadBesuNodeRunner.java | 11 ++--- .../org/hyperledger/besu/cli/BesuCommand.java | 46 +++++++++---------- .../subcommands/blocks/BlocksSubCommand.java | 4 +- .../storage/TrieLogSubCommand.java | 2 +- .../besu/components/BesuCommandModule.java | 12 +++-- .../besu/components/BesuComponent.java | 1 - .../components/BesuPluginContextModule.java | 17 +++++-- .../besu/cli/CommandTestAbstract.java | 41 ++++++++--------- 8 files changed, 68 insertions(+), 66 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java index 7e8d435c721..acc0b380f9b 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java @@ -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; @@ -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; @@ -387,7 +387,6 @@ public EthNetworkConfig provideEthNetworkConfig( } @Provides - @Named("besuPluginContext") public BesuPluginContextImpl providePluginContext( final StorageServiceImpl storageService, final SecurityModuleServiceImpl securityModuleService, @@ -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; } diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index a0e99db69fa..1f2c9d51120 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -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; @@ -865,13 +864,9 @@ static class PrivacyOptionGroup { private BesuController besuController; private BesuConfigurationImpl pluginCommonConfiguration; - private BesuComponent besuComponent; private final Supplier 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; @@ -879,7 +874,6 @@ static class PrivacyOptionGroup { /** * Besu command constructor. * - * @param besuComponent BesuComponent which acts as our application context * @param rlpBlockImporter RlpBlockImporter supplier * @param jsonBlockImporterFactory instance of {@code Function} * @param rlpBlockExporterFactory instance of {@code Function} @@ -887,18 +881,18 @@ static class PrivacyOptionGroup { * @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, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, final RunnerBuilder runnerBuilder, final BesuController.Builder controllerBuilder, final BesuPluginContextImpl besuPluginContext, - final Map environment) { + final Map environment, + final Logger commandLogger) { this( - besuComponent, rlpBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -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} * @param rlpBlockExporterFactory instance of {@code Function} @@ -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, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -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; @@ -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; @@ -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); } @@ -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()) @@ -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(); } diff --git a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommand.java index 3d1630013bf..9a0a453768b 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommand.java @@ -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()) @@ -374,7 +374,7 @@ public void run() { private BesuController createBesuController() { return parentCommand .parentCommand - .getControllerBuilder() + .setupControllerBuilder() .miningParameters(MiningParameters.newDefault()) .build(); } diff --git a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/storage/TrieLogSubCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/storage/TrieLogSubCommand.java index e054bfb615b..6fe6d058b3f 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/storage/TrieLogSubCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/storage/TrieLogSubCommand.java @@ -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(); diff --git a/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java b/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java index 6ce62b518e8..86aa8c75949 100644 --- a/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java +++ b/besu/src/main/java/org/hyperledger/besu/components/BesuCommandModule.java @@ -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; @@ -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; } diff --git a/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java b/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java index ba31fccb2bb..9f810a6dc6e 100644 --- a/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java +++ b/besu/src/main/java/org/hyperledger/besu/components/BesuComponent.java @@ -75,7 +75,6 @@ public interface BesuComponent { * * @return BesuPluginContextImpl */ - @Named("besuPluginContext") BesuPluginContextImpl getBesuPluginContext(); /** diff --git a/besu/src/main/java/org/hyperledger/besu/components/BesuPluginContextModule.java b/besu/src/main/java/org/hyperledger/besu/components/BesuPluginContextModule.java index 8ebae32bb2a..d62ab702244 100644 --- a/besu/src/main/java/org/hyperledger/besu/components/BesuPluginContextModule.java +++ b/besu/src/main/java/org/hyperledger/besu/components/BesuPluginContextModule.java @@ -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; @@ -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; } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index f6fa2e47984..3dd9641b487 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -229,9 +229,6 @@ public abstract class CommandTestAbstract { @Mock protected Logger mockLogger; - @Mock(lenient = true) - protected BesuComponent mockBesuComponent; - @Captor protected ArgumentCaptor> bytesCollectionCollector; @Captor protected ArgumentCaptor pathArgumentCaptor; @Captor protected ArgumentCaptor stringArgumentCaptor; @@ -384,8 +381,6 @@ public void initMocks() throws Exception { lenient() .when(mockBesuPluginContext.getService(TransactionSelectionService.class)) .thenReturn(Optional.of(txSelectionService)); - - when(mockBesuComponent.getBesuCommandLogger()).thenReturn(mockLogger); } @BeforeEach @@ -470,7 +465,6 @@ private TestBesuCommand getTestBesuCommand(final TestType testType) { switch (testType) { case REQUIRED_OPTION: return new TestBesuCommandWithRequiredOption( - mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -480,10 +474,10 @@ private TestBesuCommand getTestBesuCommand(final TestType testType) { environment, storageService, securityModuleService, - privacyPluginService); + privacyPluginService, + mockLogger); case PORT_CHECK: return new TestBesuCommand( - mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -493,10 +487,10 @@ private TestBesuCommand getTestBesuCommand(final TestType testType) { environment, storageService, securityModuleService, - privacyPluginService); + privacyPluginService, + mockLogger); default: return new TestBesuCommandWithoutPortCheck( - mockBesuComponent, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, @@ -506,7 +500,8 @@ private TestBesuCommand getTestBesuCommand(final TestType testType) { environment, storageService, securityModuleService, - privacyPluginService); + privacyPluginService, + mockLogger); } } @@ -536,7 +531,6 @@ public static class TestBesuCommand extends BesuCommand { private Vertx vertx; TestBesuCommand( - final BesuComponent besuComponent, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -546,9 +540,9 @@ public static class TestBesuCommand extends BesuCommand { final Map environment, final StorageServiceImpl storageService, final SecurityModuleServiceImpl securityModuleService, - final PrivacyPluginServiceImpl privacyPluginService) { + final PrivacyPluginServiceImpl privacyPluginService, + final Logger commandLogger) { super( - besuComponent, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -564,7 +558,8 @@ public static class TestBesuCommand extends BesuCommand { new TransactionSelectionServiceImpl(), new TransactionPoolValidatorServiceImpl(), new TransactionSimulationServiceImpl(), - new BlockchainServiceImpl()); + new BlockchainServiceImpl(), + commandLogger); } @Override @@ -635,7 +630,6 @@ public static class TestBesuCommandWithRequiredOption extends TestBesuCommand { private final Boolean acceptTermsAndConditions = false; TestBesuCommandWithRequiredOption( - final BesuComponent besuComponent, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -645,9 +639,9 @@ public static class TestBesuCommandWithRequiredOption extends TestBesuCommand { final Map environment, final StorageServiceImpl storageService, final SecurityModuleServiceImpl securityModuleService, - final PrivacyPluginServiceImpl privacyPluginService) { + final PrivacyPluginServiceImpl privacyPluginService, + final Logger commandLogger) { super( - besuComponent, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -657,7 +651,8 @@ public static class TestBesuCommandWithRequiredOption extends TestBesuCommand { environment, storageService, securityModuleService, - privacyPluginService); + privacyPluginService, + commandLogger); } public Boolean getAcceptTermsAndConditions() { @@ -669,7 +664,6 @@ public Boolean getAcceptTermsAndConditions() { public static class TestBesuCommandWithoutPortCheck extends TestBesuCommand { TestBesuCommandWithoutPortCheck( - final BesuComponent context, final Supplier mockBlockImporter, final Function jsonBlockImporterFactory, final Function rlpBlockExporterFactory, @@ -679,9 +673,9 @@ public static class TestBesuCommandWithoutPortCheck extends TestBesuCommand { final Map environment, final StorageServiceImpl storageService, final SecurityModuleServiceImpl securityModuleService, - final PrivacyPluginServiceImpl privacyPluginService) { + final PrivacyPluginServiceImpl privacyPluginService, + final Logger commandLogger) { super( - context, mockBlockImporter, jsonBlockImporterFactory, rlpBlockExporterFactory, @@ -691,7 +685,8 @@ public static class TestBesuCommandWithoutPortCheck extends TestBesuCommand { environment, storageService, securityModuleService, - privacyPluginService); + privacyPluginService, + commandLogger); } @Override