Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
  • Loading branch information
matthew1001 committed Dec 20, 2023
1 parent 4520865 commit 64cc64a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Set Ethereum Classic mainnet activation block for Spiral network upgrade [#6267](https://github.com/hyperledger/besu/pull/6267)
- Add custom genesis file name to config overview if specified [#6297](https://github.com/hyperledger/besu/pull/6297)
- Update Gradle plugins and replace unmaintained License Gradle Plugin with the actively maintained Gradle License Report [#6275](https://github.com/hyperledger/besu/pull/6275)
- Protection against accidental downgrade of Besu version to avoid possible data corruption problems. Downgrade can be enabled with `--allow-downgrade` [6307](https://github.com/hyperledger/besu/pull/6307)

### Bug fixes

Expand Down
8 changes: 1 addition & 7 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,6 @@ private InetAddress autoDiscoverDefaultIP() {
"Minimum number of peers required before starting fast sync. Has only effect on PoW networks. (default: ${DEFAULT-VALUE})")
private final Integer fastSyncMinPeerCount = FAST_SYNC_MIN_PEER_COUNT;

@Option(
names = {"--allow-downgrade"},
description =
"Allow an older version of Besu to start if it detects that a more recent version last wrote to the database. Warning - this could result in unrecoverable changes to the database so should only be used if a backup of the data has been taken before the downgrade. (default: ${DEFAULT-VALUE})")
private final Boolean allowDowngrade = false;

@Option(
names = {"--network"},
paramLabel = MANDATORY_NETWORK_FORMAT_HELP,
Expand Down Expand Up @@ -3396,7 +3390,7 @@ public int getDatabaseVersion() {

@Override
public boolean getDowngradeAllowed() {
return allowDowngrade;
return dataStorageOptions.toDomainObject().getAllowDowngrade();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public class DataStorageOptions implements CLIOptions<DataStorageConfiguration>
arity = "1")
private Long bonsaiMaxLayersToLoad = DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD;

@Option(
names = {"--allow-downgrade"},
description =
"Allow an older version of Besu to start if it detects that a more recent version last wrote to the database. Warning - this could result in unrecoverable changes to the database so should only be used if a backup of the data has been taken before the downgrade. (default: ${DEFAULT-VALUE})")
private Boolean allowDowngrade = false;

@CommandLine.ArgGroup(validate = false)
private final DataStorageOptions.Unstable unstableOptions = new Unstable();

Expand Down Expand Up @@ -121,6 +127,7 @@ static DataStorageOptions fromConfig(final DataStorageConfiguration domainObject
final DataStorageOptions dataStorageOptions = DataStorageOptions.create();
dataStorageOptions.dataStorageFormat = domainObject.getDataStorageFormat();
dataStorageOptions.bonsaiMaxLayersToLoad = domainObject.getBonsaiMaxLayersToLoad();
dataStorageOptions.allowDowngrade = domainObject.getAllowDowngrade();
dataStorageOptions.unstableOptions.bonsaiTrieLogPruningEnabled =
domainObject.getUnstable().getBonsaiTrieLogPruningEnabled();
dataStorageOptions.unstableOptions.bonsaiTrieLogRetentionThreshold =
Expand All @@ -136,6 +143,7 @@ public DataStorageConfiguration toDomainObject() {
return ImmutableDataStorageConfiguration.builder()
.dataStorageFormat(dataStorageFormat)
.bonsaiMaxLayersToLoad(bonsaiMaxLayersToLoad)
.allowDowngrade(allowDowngrade)
.unstable(
ImmutableDataStorageConfiguration.Unstable.builder()
.bonsaiTrieLogPruningEnabled(unstableOptions.bonsaiTrieLogPruningEnabled)
Expand Down
25 changes: 13 additions & 12 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,19 @@ public void parsesInvalidBonsaiTrieLimitBackLayersOption() {
"Invalid value for option '--bonsai-maximum-back-layers-to-load': 'ten' is not a long");
}

@Test
public void parsesValidAllowDowngradeOption() {
parseCommand("--allow-dowgrade", "true");
verify(mockControllerBuilder)
.dataStorageConfiguration(dataStorageConfigurationArgumentCaptor.capture());

final DataStorageConfiguration dataStorageConfiguration =
dataStorageConfigurationArgumentCaptor.getValue();
assertThat(dataStorageConfiguration.getAllowDowngrade()).isEqualTo(Boolean.valueOf("true"));
assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void dnsEnabledOptionIsParsedCorrectly() {
final TestBesuCommand besuCommand = parseCommand("--Xdns-enabled", "true");
Expand Down Expand Up @@ -5412,16 +5425,4 @@ public void cacheLastBlocksOptionShouldWork() {
assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void parsesValidAllowDowngradeOption() {
parseCommand("--allow-dowgrade", "false");
verify(mockControllerBuilder).synchronizerConfiguration(syncConfigurationCaptor.capture());

final SynchronizerConfiguration syncConfig = syncConfigurationCaptor.getValue();
assertThat(syncConfig.getSyncMode()).isEqualTo(SyncMode.FAST);
assertThat(syncConfig.getFastSyncMinimumPeerCount()).isEqualTo(11);
assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface DataStorageConfiguration {

Long getBonsaiMaxLayersToLoad();

Boolean getAllowDowngrade();

@Value.Default
default Unstable getUnstable() {
return Unstable.DEFAULT;
Expand Down

0 comments on commit 64cc64a

Please sign in to comment.