Accept to use default port values if not in use. (#1673)

* Accept to use default port values if not in use.

Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net>
pull/1684/head
Abdelhamid Bakhta 4 years ago committed by GitHub
parent 5241747ba4
commit c4ef564825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 30
      besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java

@ -2366,21 +2366,12 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
}
private void checkPortClash() {
// List of port parameters
final List<Integer> ports =
asList(
p2pPort,
graphQLHttpPort,
rpcHttpPort,
rpcWsPort,
metricsPort,
metricsPushPort,
stratumPort);
ports.stream()
getEffectivePorts().stream()
.filter(Objects::nonNull)
.filter(port -> port > 0)
.forEach(
port -> {
if (port != 0 && !allocatedPorts.add(port)) {
if (!allocatedPorts.add(port)) {
throw new ParameterException(
commandLine,
"Port number '"
@ -2390,6 +2381,37 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
});
}
/**
* * Gets the list of effective ports (ports that are enabled).
*
* @return The list of effective ports
*/
private List<Integer> getEffectivePorts() {
final List<Integer> effectivePorts = new ArrayList<>();
addPortIfEnabled(effectivePorts, p2pPort, p2pEnabled);
addPortIfEnabled(effectivePorts, graphQLHttpPort, isGraphQLHttpEnabled);
addPortIfEnabled(effectivePorts, rpcHttpPort, isRpcHttpEnabled);
addPortIfEnabled(effectivePorts, rpcWsPort, isRpcWsEnabled);
addPortIfEnabled(effectivePorts, metricsPort, isMetricsEnabled);
addPortIfEnabled(effectivePorts, metricsPushPort, isMetricsPushEnabled);
addPortIfEnabled(effectivePorts, stratumPort, iStratumMiningEnabled);
return effectivePorts;
}
/**
* Adds port in the passed list only if enabled.
*
* @param ports The list of ports
* @param port The port value
* @param enabled true if enabled, false otherwise
*/
private void addPortIfEnabled(
final List<Integer> ports, final Integer port, final boolean enabled) {
if (enabled) {
ports.add(port);
}
}
private void checkGoQuorumCompatibilityConfig(final EthNetworkConfig ethNetworkConfig) {
if (isGoQuorumCompatibilityMode) {
final GenesisConfigOptions genesisConfigOptions = readGenesisConfigOptions();

@ -3853,7 +3853,12 @@ public class BesuCommandTest extends CommandTestAbstract {
@Test
public void assertThatDuplicatePortSpecifiedFails() {
parseCommand("--p2p-port=9", "--rpc-http-port=10", "--rpc-ws-port=10");
parseCommand(
"--p2p-port=9",
"--rpc-http-enabled",
"--rpc-http-port=10",
"--rpc-ws-port=10",
"--rpc-ws-enabled");
assertThat(commandErrorOutput.toString())
.contains("Port number '10' has been specified multiple times.");
}
@ -3999,4 +4004,27 @@ public class BesuCommandTest extends CommandTestAbstract {
assertThat(commandErrorOutput.toString())
.contains("GoQuorum compatibility mode (enabled) cannot be used on Mainnet");
}
@Test
public void assertThatCheckPortClashAcceptsAsExpected() throws Exception {
// use WS port for HTTP
final int port = 8546;
parseCommand("--rpc-http-enabled", "--rpc-http-port", String.valueOf(port));
verify(mockRunnerBuilder).jsonRpcConfiguration(jsonRpcConfigArgumentCaptor.capture());
verify(mockRunnerBuilder).build();
assertThat(jsonRpcConfigArgumentCaptor.getValue().getPort()).isEqualTo(port);
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString()).isEmpty();
}
@Test
public void assertThatCheckPortClashRejectsAsExpected() throws Exception {
// use WS port for HTTP
final int port = 8546;
parseCommand("--rpc-http-enabled", "--rpc-http-port", String.valueOf(port), "--rpc-ws-enabled");
assertThat(commandOutput.toString()).isEmpty();
assertThat(commandErrorOutput.toString())
.contains(
"Port number '8546' has been specified multiple times. Please review the supplied configuration.");
}
}

Loading…
Cancel
Save