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 bbcf800e3b4..743f18285b7 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -1929,11 +1929,10 @@ private void issueOptionWarnings() { "--Xminer-remote-sealers-limit", "--Xminer-remote-sealers-hashrate-ttl")); - CommandLineUtils.checkOptionDependencies( - logger, + CommandLineUtils.failIfOptionDoesntMeetRequirement( commandLine, - "--sync-mode", - SyncMode.isFullSync(syncMode), + "--fast-sync-min-peers can't be used with FULL sync-mode", + !SyncMode.isFullSync(getDefaultSyncModeIfNotSet(syncMode)), singletonList("--fast-sync-min-peers")); if (!securityModuleName.equals(DEFAULT_SECURITY_MODULE) @@ -1966,14 +1965,7 @@ private void issueOptionWarnings() { private void configure() throws Exception { checkPortClash(); - syncMode = - Optional.ofNullable(syncMode) - .orElse( - genesisFile == null - && !privacyOptionGroup.isPrivacyEnabled - && Optional.ofNullable(network).map(NetworkName::canFastSync).orElse(false) - ? SyncMode.FAST - : SyncMode.FULL); + syncMode = getDefaultSyncModeIfNotSet(syncMode); ethNetworkConfig = updateNetworkConfig(network); @@ -3291,4 +3283,14 @@ public static List getJDKEnabledProtocols() { throw new RuntimeException(e); } } + + private SyncMode getDefaultSyncModeIfNotSet(final SyncMode syncMode) { + return Optional.ofNullable(syncMode) + .orElse( + genesisFile == null + && !privacyOptionGroup.isPrivacyEnabled + && Optional.ofNullable(network).map(NetworkName::canFastSync).orElse(false) + ? SyncMode.FAST + : SyncMode.FULL); + } } diff --git a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java index 92b185224f6..a9eb0231a17 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java @@ -97,6 +97,20 @@ public static void checkMultiOptionDependencies( } } + public static void failIfOptionDoesntMeetRequirement( + final CommandLine commandLine, + final String errorMessage, + final boolean requirement, + final List dependentOptionsNames) { + if (!requirement) { + final String affectedOptions = getAffectedOptions(commandLine, dependentOptionsNames); + + if (!affectedOptions.isEmpty()) { + throw new CommandLine.ParameterException(commandLine, errorMessage); + } + } + } + private static String getAffectedOptions( final CommandLine commandLine, final List dependentOptionsNames) { return commandLine.getCommandSpec().options().stream() diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 87b1a612d65..cde38a84df9 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -2293,28 +2293,6 @@ public void privacyTlsOptionsRequiresPrivacyToBeEnabledToml() throws IOException assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } - @Test - public void fastSyncOptionsRequiresFastSyncModeToBeSet() { - parseCommand("--fast-sync-min-peers", "5"); - - verifyOptionsConstraintLoggerCall("--sync-mode", "--fast-sync-min-peers"); - - assertThat(commandOutput.toString(UTF_8)).isEmpty(); - assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); - } - - @Test - public void fastSyncOptionsRequiresFastSyncModeToBeSetToml() throws IOException { - final Path toml = createTempFile("toml", "fast-sync-min-peers=5\n"); - - parseCommand("--config-file", toml.toString()); - - verifyOptionsConstraintLoggerCall("--sync-mode", "--fast-sync-min-peers"); - - assertThat(commandOutput.toString(UTF_8)).isEmpty(); - assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); - } - @Test public void rpcApisPropertyWithInvalidEntryMustDisplayError() { parseCommand("--rpc-http-api", "BOB"); @@ -5350,4 +5328,11 @@ public void logsSuggestInstallingJemallocWhenEnvVarNotPresent() { verify(mockLogger) .info("jemalloc library not found, memory usage may be reduced by installing it"); } + + @Test + public void logWarnIfFastSyncMinPeersUsedWithFullSync() { + parseCommand("--sync-mode", "FULL", "--fast-sync-min-peers", "1"); + assertThat(commandErrorOutput.toString(UTF_8)) + .contains("--fast-sync-min-peers can't be used with FULL sync-mode"); + } }