Better handler for --fast-sync-min-peers flag (#4481)

* Change validation for fast-sync-min-peers to allow it to be used with any other sync-mode rather than FULL sync
* Test to ensure fast-sync-min-peers can be used with any other sync-mode rather than FULL sync

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
pull/4499/head
Gabriel Camargo Fukushima 2 years ago committed by GitHub
parent aa849839b8
commit 9bc210b2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 14
      besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java
  3. 29
      besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java

@ -1929,11 +1929,10 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
"--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 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
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 class BesuCommand implements DefaultCommandValues, Runnable {
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);
}
}

@ -97,6 +97,20 @@ public class CommandLineUtils {
}
}
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()

@ -2293,28 +2293,6 @@ public class BesuCommandTest extends CommandTestAbstract {
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 class BesuCommandTest extends CommandTestAbstract {
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");
}
}

Loading…
Cancel
Save