Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
CLI refactoring
Browse files Browse the repository at this point in the history
Stop using Guava's HostAndPort and HostSpecifier and just use Strings.
  • Loading branch information
shemnon committed Jan 30, 2019
1 parent f64aad3 commit c5e7faa
Showing 1 changed file with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
import java.util.stream.Stream;

import com.google.common.io.Resources;
import com.google.common.net.HostAndPort;
import com.google.common.net.HostSpecifier;
import io.vertx.core.Vertx;
import io.vertx.core.json.DecodeException;
import org.apache.logging.log4j.Level;
Expand Down Expand Up @@ -225,8 +223,7 @@ public static class RpcApisConversionException extends Exception {
description = "Host for p2p peers discovery to listen on (default: ${DEFAULT-VALUE})",
arity = "1"
)
private final HostSpecifier p2pHost =
HostSpecifier.fromValid(autoDiscoverDefaultIP().getHostAddress());
private final String p2pHost = autoDiscoverDefaultIP().getHostAddress();

@Option(
names = {"--p2p-port"},
Expand Down Expand Up @@ -257,8 +254,7 @@ public static class RpcApisConversionException extends Exception {
description = "Host for HTTP JSON-RPC to listen on (default: ${DEFAULT-VALUE})",
arity = "1"
)
private final HostSpecifier rpcHttpHost =
HostSpecifier.fromValid(autoDiscoverDefaultIP().getHostAddress());
private final String rpcHttpHost = autoDiscoverDefaultIP().getHostAddress();

@Option(
names = {"--rpc-http-port"},
Expand Down Expand Up @@ -299,8 +295,7 @@ public static class RpcApisConversionException extends Exception {
description = "Host for WebSocket JSON-RPC to listen on (default: ${DEFAULT-VALUE})",
arity = "1"
)
private final HostSpecifier rpcWsHost =
HostSpecifier.fromValid(autoDiscoverDefaultIP().getHostAddress());
private final String rpcWsHost = autoDiscoverDefaultIP().getHostAddress();

@Option(
names = {"--rpc-ws-port"},
Expand Down Expand Up @@ -356,8 +351,7 @@ private Long configureRefreshDelay(final Long refreshDelay) {
description = "Host for the metrics exporter to listen on (default: ${DEFAULT-VALUE})",
arity = "1"
)
private final HostSpecifier metricsHost =
HostSpecifier.fromValid(autoDiscoverDefaultIP().getHostAddress());
private final String metricsHost = autoDiscoverDefaultIP().getHostAddress();

@Option(
names = {"--metrics-port"},
Expand All @@ -380,8 +374,7 @@ private Long configureRefreshDelay(final Long refreshDelay) {
description = "Host of the Prometheus Push Gateway for push mode (default: ${DEFAULT-VALUE})",
arity = "1"
)
private final HostSpecifier metricsPushHost =
HostSpecifier.fromValid(autoDiscoverDefaultIP().getHostAddress());
private final String metricsPushHost = autoDiscoverDefaultIP().getHostAddress();

@Option(
names = {"--metrics-push-port"},
Expand Down Expand Up @@ -517,6 +510,7 @@ public void parse(
commandLine = new CommandLine(this);

commandLine.setCaseInsensitiveEnumValuesAllowed(true);
// commandLine.setToggleBooleanFlags(false);

standaloneCommands = new StandaloneCommand();

Expand All @@ -533,8 +527,6 @@ public void parse(

commandLine.registerConverter(Address.class, Address::fromHexString);
commandLine.registerConverter(BytesValue.class, BytesValue::fromHexString);
commandLine.registerConverter(HostAndPort.class, HostAndPort::fromString);
commandLine.registerConverter(HostSpecifier.class, HostSpecifier::from);
commandLine.registerConverter(Level.class, Level::valueOf);
commandLine.registerConverter(SyncMode.class, SyncMode::fromString);
commandLine.registerConverter(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg)));
Expand Down Expand Up @@ -594,7 +586,8 @@ public void run() {
peerDiscoveryEnabled,
ethNetworkConfig.getBootNodes(),
maxPeers,
HostAndPort.fromParts(p2pHost.toString(), p2pPort),
p2pHost,
p2pPort,
jsonRpcConfiguration(),
webSocketConfiguration(),
metricsConfiguration(),
Expand All @@ -612,7 +605,7 @@ private void ensureAllBootnodesAreInWhitelist(
try {
PermissioningConfigurationValidator.areAllBootnodesAreInWhitelist(
ethNetworkConfig, permissioningConfiguration);
} catch (Exception e) {
} catch (final Exception e) {
throw new ParameterException(new CommandLine(this), e.getMessage());
}
}
Expand Down Expand Up @@ -654,7 +647,7 @@ private JsonRpcConfiguration jsonRpcConfiguration() {

final JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault();
jsonRpcConfiguration.setEnabled(isRpcHttpEnabled);
jsonRpcConfiguration.setHost(rpcHttpHost.toString());
jsonRpcConfiguration.setHost(rpcHttpHost);
jsonRpcConfiguration.setPort(rpcHttpPort);
jsonRpcConfiguration.setCorsAllowedDomains(rpcHttpCorsAllowedOrigins);
jsonRpcConfiguration.setRpcApis(rpcHttpApis);
Expand All @@ -678,7 +671,7 @@ private WebSocketConfiguration webSocketConfiguration() {

final WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault();
webSocketConfiguration.setEnabled(isRpcWsEnabled);
webSocketConfiguration.setHost(rpcWsHost.toString());
webSocketConfiguration.setHost(rpcWsHost);
webSocketConfiguration.setPort(rpcWsPort);
webSocketConfiguration.setRpcApis(rpcWsApis);
webSocketConfiguration.setRefreshDelay(rpcWsRefreshDelay);
Expand Down Expand Up @@ -713,10 +706,10 @@ MetricsConfiguration metricsConfiguration() {

final MetricsConfiguration metricsConfiguration = createDefault();
metricsConfiguration.setEnabled(isMetricsEnabled);
metricsConfiguration.setHost(metricsHost.toString());
metricsConfiguration.setHost(metricsHost);
metricsConfiguration.setPort(metricsPort);
metricsConfiguration.setPushEnabled(isMetricsPushEnabled);
metricsConfiguration.setPushHost(metricsPushHost.toString());
metricsConfiguration.setPushHost(metricsPushHost);
metricsConfiguration.setPushPort(metricsPushPort);
metricsConfiguration.setPushInterval(metricsPushInterval);
metricsConfiguration.setPrometheusJob(metricsPrometheusJob);
Expand All @@ -730,12 +723,10 @@ private PermissioningConfiguration permissioningConfiguration() {
return PermissioningConfiguration.createDefault();
}

final PermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfigurationFromToml(
DefaultCommandValues.PERMISSIONING_CONFIG_LOCATION,
permissionsNodesEnabled,
permissionsAccountsEnabled);
return permissioningConfiguration;
return PermissioningConfigurationBuilder.permissioningConfigurationFromToml(
DefaultCommandValues.PERMISSIONING_CONFIG_LOCATION,
permissionsNodesEnabled,
permissionsAccountsEnabled);
}

private PrivacyParameters orionConfiguration() {
Expand Down Expand Up @@ -770,7 +761,8 @@ private void synchronize(
final boolean peerDiscoveryEnabled,
final Collection<?> bootstrapNodes,
final int maxPeers,
final HostAndPort discoveryHostAndPort,
final String discoveryHost,
final int discoveryPort,
final JsonRpcConfiguration jsonRpcConfiguration,
final WebSocketConfiguration webSocketConfiguration,
final MetricsConfiguration metricsConfiguration,
Expand All @@ -785,8 +777,8 @@ private void synchronize(
.p2pEnabled(p2pEnabled)
.discovery(peerDiscoveryEnabled)
.bootstrapPeers(bootstrapNodes)
.discoveryHost(discoveryHostAndPort.getHost())
.discoveryPort(discoveryHostAndPort.getPort())
.discoveryHost(discoveryHost)
.discoveryPort(discoveryPort)
.maxPeers(maxPeers)
.jsonRpcConfiguration(jsonRpcConfiguration)
.webSocketConfiguration(webSocketConfiguration)
Expand Down Expand Up @@ -834,7 +826,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) {

// custom genesis file use comes with specific default values for the genesis file itself
// but also for the network id and the bootnodes list.
File genesisFile = genesisFile();
final File genesisFile = genesisFile();
if (genesisFile != null) {

//noinspection ConstantConditions network is not always null but injected by PicoCLI if used
Expand All @@ -860,13 +852,13 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) {
// stage.
// If no chain id is found in the genesis as it's an optional, we use mainnet network id.
try {
GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisConfig());
final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisConfig());
builder.setNetworkId(
genesisConfigFile
.getConfigOptions()
.getChainId()
.orElse(EthNetworkConfig.getNetworkConfig(MAINNET).getNetworkId()));
} catch (DecodeException e) {
} catch (final DecodeException e) {
throw new ParameterException(
new CommandLine(this),
String.format("Unable to parse genesis file %s.", genesisFile),
Expand Down

0 comments on commit c5e7faa

Please sign in to comment.