From 4463fa0c0bee425461ff1dcdb2162fe21551ad11 Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Tue, 2 Jul 2019 09:22:43 +0200 Subject: [PATCH 1/3] split parse method - handleStandaloneCommand - addSubCommands - registerConverters - handleUnstableOptions - preparePlugins - parse --- .../pegasys/pantheon/cli/PantheonCommand.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java index fdc7bb9b1b..3806f57704 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java @@ -633,17 +633,24 @@ public void parse( final PantheonExceptionHandler exceptionHandler, final InputStream in, final String... args) { + commandLine = new CommandLine(this).setCaseInsensitiveEnumValuesAllowed(true); + handleStandaloneCommand(); + addSubCommands(resultHandler, in); + registerConverters(); + handleUnstableOptions(); + preparePlugins(); + parse(resultHandler, exceptionHandler, args); + } - commandLine = new CommandLine(this); - - commandLine.setCaseInsensitiveEnumValuesAllowed(true); - + private void handleStandaloneCommand() { standaloneCommands = new StandaloneCommand(); - if (isFullInstantiation()) { commandLine.addMixin("standaloneCommands", standaloneCommands); } + } + private void addSubCommands( + final AbstractParseResultHandler> resultHandler, final InputStream in) { commandLine.addSubcommand( BlocksSubCommand.COMMAND_NAME, new BlocksSubCommand(blockImporter, resultHandler.out())); commandLine.addSubcommand( @@ -655,7 +662,9 @@ public void parse( RLPSubCommand.COMMAND_NAME, new RLPSubCommand(resultHandler.out(), in)); commandLine.addSubcommand( OperatorSubCommand.COMMAND_NAME, new OperatorSubCommand(resultHandler.out())); + } + private void registerConverters() { commandLine.registerConverter(Address.class, Address::fromHexStringStrict); commandLine.registerConverter(BytesValue.class, BytesValue::fromHexString); commandLine.registerConverter(Level.class, Level::valueOf); @@ -663,13 +672,14 @@ public void parse( commandLine.registerConverter(UInt256.class, (arg) -> UInt256.of(new BigInteger(arg))); commandLine.registerConverter(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg))); commandLine.registerConverter(PositiveNumber.class, PositiveNumber::fromString); - final MetricCategoryConverter metricCategoryConverter = new MetricCategoryConverter(); metricCategoryConverter.addCategories(PantheonMetricCategory.class); metricCategoryConverter.addCategories(StandardMetricCategory.class); commandLine.registerConverter(MetricCategory.class, metricCategoryConverter); + } - // Add performance options + private void handleUnstableOptions() { + // Add unstable options UnstableOptionsSubCommand.createUnstableOptions( commandLine, ImmutableMap.of( @@ -681,10 +691,17 @@ public void parse( ethereumWireConfigurationBuilder, "TransactionPool", transactionPoolConfigurationBuilder)); + } + private void preparePlugins() { pantheonPluginContext.addService(PicoCLIOptions.class, new PicoCLIOptionsImpl(commandLine)); pantheonPluginContext.registerPlugins(pluginsDir()); + } + private void parse( + final AbstractParseResultHandler> resultHandler, + final PantheonExceptionHandler exceptionHandler, + final String... args) { // Create a handler that will search for a config file option and use it for // default values // and eventually it will run regular parsing of the remaining options. From afa0784ef8266993bebb3ba878474cecb5cfefa3 Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Tue, 2 Jul 2019 10:01:58 +0200 Subject: [PATCH 2/3] split run method -checkOptions - configure - controller - startPlugins - startSynchronization --- .../pegasys/pantheon/cli/PantheonCommand.java | 167 ++++++++++-------- 1 file changed, 98 insertions(+), 69 deletions(-) diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java index 3806f57704..611cb15bfe 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java @@ -568,6 +568,15 @@ void setBannedNodeIds(final List values) { arity = "1") private final Integer pendingTxRetentionPeriod = PendingTransactions.DEFAULT_TX_RETENTION_HOURS; + private EthNetworkConfig ethNetworkConfig; + private JsonRpcConfiguration jsonRpcConfiguration; + private GraphQLConfiguration graphQLConfiguration; + private WebSocketConfiguration webSocketConfiguration; + private MetricsConfiguration metricsConfiguration; + private Optional permissioningConfiguration; + private Collection staticNodes; + private PantheonController pantheonController; + // Inner class so we can get to loggingLevel. public class PantheonExceptionHandler extends CommandLine.AbstractHandler, PantheonExceptionHandler> @@ -634,22 +643,34 @@ public void parse( final InputStream in, final String... args) { commandLine = new CommandLine(this).setCaseInsensitiveEnumValuesAllowed(true); - handleStandaloneCommand(); - addSubCommands(resultHandler, in); - registerConverters(); - handleUnstableOptions(); - preparePlugins(); - parse(resultHandler, exceptionHandler, args); + handleStandaloneCommand() + .addSubCommands(resultHandler, in) + .registerConverters() + .handleUnstableOptions() + .preparePlugins() + .parse(resultHandler, exceptionHandler, args); } - private void handleStandaloneCommand() { + @Override + public void run() { + try { + prepareLogging(); + logger.info("Starting Pantheon version: {}", PantheonInfo.version()); + checkOptions().configure().controller().startPlugins().startSynchronization(); + } catch (final Exception e) { + throw new ParameterException(this.commandLine, e.getMessage(), e); + } + } + + private PantheonCommand handleStandaloneCommand() { standaloneCommands = new StandaloneCommand(); if (isFullInstantiation()) { commandLine.addMixin("standaloneCommands", standaloneCommands); } + return this; } - private void addSubCommands( + private PantheonCommand addSubCommands( final AbstractParseResultHandler> resultHandler, final InputStream in) { commandLine.addSubcommand( BlocksSubCommand.COMMAND_NAME, new BlocksSubCommand(blockImporter, resultHandler.out())); @@ -662,9 +683,10 @@ private void addSubCommands( RLPSubCommand.COMMAND_NAME, new RLPSubCommand(resultHandler.out(), in)); commandLine.addSubcommand( OperatorSubCommand.COMMAND_NAME, new OperatorSubCommand(resultHandler.out())); + return this; } - private void registerConverters() { + private PantheonCommand registerConverters() { commandLine.registerConverter(Address.class, Address::fromHexStringStrict); commandLine.registerConverter(BytesValue.class, BytesValue::fromHexString); commandLine.registerConverter(Level.class, Level::valueOf); @@ -676,9 +698,10 @@ private void registerConverters() { metricCategoryConverter.addCategories(PantheonMetricCategory.class); metricCategoryConverter.addCategories(StandardMetricCategory.class); commandLine.registerConverter(MetricCategory.class, metricCategoryConverter); + return this; } - private void handleUnstableOptions() { + private PantheonCommand handleUnstableOptions() { // Add unstable options UnstableOptionsSubCommand.createUnstableOptions( commandLine, @@ -691,14 +714,16 @@ private void handleUnstableOptions() { ethereumWireConfigurationBuilder, "TransactionPool", transactionPoolConfigurationBuilder)); + return this; } - private void preparePlugins() { + private PantheonCommand preparePlugins() { pantheonPluginContext.addService(PicoCLIOptions.class, new PicoCLIOptionsImpl(commandLine)); pantheonPluginContext.registerPlugins(pluginsDir()); + return this; } - private void parse( + private PantheonCommand parse( final AbstractParseResultHandler> resultHandler, final PantheonExceptionHandler exceptionHandler, final String... args) { @@ -709,18 +734,44 @@ private void parse( new ConfigOptionSearchAndRunHandler( resultHandler, exceptionHandler, CONFIG_FILE_OPTION_NAME, environment, isDocker); commandLine.parseWithHandlers(configParsingHandler, exceptionHandler, args); + return this; } - @Override - public void run() { + private PantheonCommand startSynchronization() { + synchronize( + pantheonController, + p2pEnabled, + peerDiscoveryEnabled, + ethNetworkConfig, + maxPeers, + p2pHost, + p2pPort, + graphQLConfiguration, + jsonRpcConfiguration, + webSocketConfiguration, + metricsConfiguration, + permissioningConfiguration, + staticNodes); + return this; + } + + private PantheonCommand startPlugins() { + pantheonPluginContext.addService( + PantheonEvents.class, + new PantheonEventsImpl((pantheonController.getProtocolManager().getBlockBroadcaster()))); + pantheonPluginContext.startPlugins(); + return this; + } + + private void prepareLogging() { // set log level per CLI flags if (logLevel != null) { System.out.println("Setting logging level to " + logLevel.name()); Configurator.setAllLevels("", logLevel); } + } - logger.info("Starting Pantheon version: {}", PantheonInfo.version()); - + private PantheonCommand checkOptions() { // Check that P2P options are able to work or send an error checkOptionDependencies( logger, @@ -756,59 +807,32 @@ public void run() { "Unable to mine without a valid coinbase. Either disable mining (remove --miner-enabled)" + "or specify the beneficiary of mining (via --miner-coinbase
)"); } + return this; + } - final EthNetworkConfig ethNetworkConfig = updateNetworkConfig(getNetwork()); - try { - final JsonRpcConfiguration jsonRpcConfiguration = jsonRpcConfiguration(); - final GraphQLConfiguration graphQLConfiguration = graphQLConfiguration(); - final WebSocketConfiguration webSocketConfiguration = webSocketConfiguration(); - final Optional permissioningConfiguration = - permissioningConfiguration(); - - final Collection staticNodes = loadStaticNodes(); - logger.info("Connecting to {} static nodes.", staticNodes.size()); - logger.trace("Static Nodes = {}", staticNodes); - - final List enodeURIs = - ethNetworkConfig.getBootNodes().stream() - .map(EnodeURL::toURI) - .collect(Collectors.toList()); - permissioningConfiguration - .flatMap(PermissioningConfiguration::getLocalConfig) - .ifPresent(p -> ensureAllNodesAreInWhitelist(enodeURIs, p)); - - permissioningConfiguration - .flatMap(PermissioningConfiguration::getLocalConfig) - .ifPresent( - p -> - ensureAllNodesAreInWhitelist( - staticNodes.stream().map(EnodeURL::toURI).collect(Collectors.toList()), p)); - - final PantheonController pantheonController = buildController(); - final MetricsConfiguration metricsConfiguration = metricsConfiguration(); - - pantheonPluginContext.addService( - PantheonEvents.class, - new PantheonEventsImpl((pantheonController.getProtocolManager().getBlockBroadcaster()))); - pantheonPluginContext.startPlugins(); - - synchronize( - pantheonController, - p2pEnabled, - peerDiscoveryEnabled, - ethNetworkConfig, - maxPeers, - p2pHost, - p2pPort, - graphQLConfiguration, - jsonRpcConfiguration, - webSocketConfiguration, - metricsConfiguration, - permissioningConfiguration, - staticNodes); - } catch (final Exception e) { - throw new ParameterException(this.commandLine, e.getMessage(), e); - } + private PantheonCommand configure() throws Exception { + ethNetworkConfig = updateNetworkConfig(getNetwork()); + jsonRpcConfiguration = jsonRpcConfiguration(); + graphQLConfiguration = graphQLConfiguration(); + webSocketConfiguration = webSocketConfiguration(); + permissioningConfiguration = permissioningConfiguration(); + staticNodes = loadStaticNodes(); + logger.info("Connecting to {} static nodes.", staticNodes.size()); + logger.trace("Static Nodes = {}", staticNodes); + final List enodeURIs = + ethNetworkConfig.getBootNodes().stream().map(EnodeURL::toURI).collect(Collectors.toList()); + permissioningConfiguration + .flatMap(PermissioningConfiguration::getLocalConfig) + .ifPresent(p -> ensureAllNodesAreInWhitelist(enodeURIs, p)); + + permissioningConfiguration + .flatMap(PermissioningConfiguration::getLocalConfig) + .ifPresent( + p -> + ensureAllNodesAreInWhitelist( + staticNodes.stream().map(EnodeURL::toURI).collect(Collectors.toList()), p)); + metricsConfiguration = metricsConfiguration(); + return this; } private NetworkName getNetwork() { @@ -828,7 +852,12 @@ private void ensureAllNodesAreInWhitelist( } } - PantheonController buildController() { + private PantheonCommand controller() { + pantheonController = buildController(); + return this; + } + + PantheonController buildController() { try { return controllerBuilderFactory .fromEthNetworkConfig(updateNetworkConfig(getNetwork())) From 00f6d72fb08cc5570ec436e7cf354fc547abc5a2 Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Tue, 2 Jul 2019 10:32:45 +0200 Subject: [PATCH 3/3] Update PantheonCommand.java --- .../main/java/tech/pegasys/pantheon/cli/PantheonCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java index 611cb15bfe..e278ec878d 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java @@ -857,7 +857,7 @@ private PantheonCommand controller() { return this; } - PantheonController buildController() { + PantheonController buildController() { try { return controllerBuilderFactory .fromEthNetworkConfig(updateNetworkConfig(getNetwork()))