Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -3291,4 +3283,14 @@ public static List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ public static void checkMultiOptionDependencies(
}
}

public static void failIfOptionDoesntMeetRequirement(
final CommandLine commandLine,
final String errorMessage,
final boolean requirement,
final List<String> 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<String> dependentOptionsNames) {
return commandLine.getCommandSpec().options().stream()
Expand Down
29 changes: 7 additions & 22 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}
}