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 7f539e33ff1..cccc5c4af7d 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -2005,13 +2005,13 @@ private void issueOptionWarnings() { CommandLineUtils.failIfOptionDoesntMeetRequirement( commandLine, "--fast-sync-min-peers can't be used with FULL sync-mode", - !SyncMode.isFullSync(getDefaultSyncModeIfNotSet(syncMode)), + !SyncMode.isFullSync(getDefaultSyncModeIfNotSet()), singletonList("--fast-sync-min-peers")); CommandLineUtils.failIfOptionDoesntMeetRequirement( commandLine, "--Xcheckpoint-post-merge-enabled can only be used with X_CHECKPOINT sync-mode", - SyncMode.X_CHECKPOINT.equals(getDefaultSyncModeIfNotSet(syncMode)), + SyncMode.X_CHECKPOINT.equals(getDefaultSyncModeIfNotSet()), singletonList("--Xcheckpoint-post-merge-enabled")); if (!securityModuleName.equals(DEFAULT_SECURITY_MODULE) @@ -2033,7 +2033,7 @@ private void issueOptionWarnings() { private void configure() throws Exception { checkPortClash(); checkIfRequiredPortsAreAvailable(); - syncMode = getDefaultSyncModeIfNotSet(syncMode); + syncMode = getDefaultSyncModeIfNotSet(); ethNetworkConfig = updateNetworkConfig(network); @@ -2167,7 +2167,8 @@ public BesuController buildController() { public BesuControllerBuilder getControllerBuilder() { final KeyValueStorageProvider storageProvider = keyValueStorageProvider(keyValueStorageName); return controllerBuilderFactory - .fromEthNetworkConfig(updateNetworkConfig(network), genesisConfigOverrides, syncMode) + .fromEthNetworkConfig( + updateNetworkConfig(network), genesisConfigOverrides, getDefaultSyncModeIfNotSet()) .synchronizerConfiguration(buildSyncConfig()) .ethProtocolConfiguration(unstableEthProtocolOptions.toDomainObject()) .dataDirectory(dataDir()) @@ -3436,7 +3437,7 @@ public static List getJDKEnabledProtocols() { } } - private SyncMode getDefaultSyncModeIfNotSet(final SyncMode syncMode) { + private SyncMode getDefaultSyncModeIfNotSet() { return Optional.ofNullable(syncMode) .orElse( genesisFile == null diff --git a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java index b6421b84ad2..6a0b5592d27 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java @@ -19,6 +19,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.isNotNull; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -487,4 +488,13 @@ private void createDbDirectory(final boolean createDataFiles) throws IOException assertThat(success).isTrue(); } } + + @Test + public void blocksImportWithNoSyncModeDoesNotRaiseNPE() throws IOException { + final File fileToImport = temp.newFile("blocks.file"); + parseCommand( + BLOCK_SUBCOMMAND_NAME, BLOCK_IMPORT_SUBCOMMAND_NAME, "--from", fileToImport.getPath()); + + verify(mockControllerBuilderFactory).fromEthNetworkConfig(any(), any(), isNotNull()); + } }