Skip to content

Commit

Permalink
Add --allow-downgrade CLI arg. If set it allows the downgrade and upd…
Browse files Browse the repository at this point in the history
…ates the Besu version in the metadata file to the downgraded version.

Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
  • Loading branch information
matthew1001 committed Dec 20, 2023
1 parent e2714de commit deb1d75
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
11 changes: 11 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ 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 @@ -3387,6 +3393,11 @@ public Path getDataPath() {
public int getDatabaseVersion() {
return dataStorageOptions.toDomainObject().getDataStorageFormat().getDatabaseVersion();
}

@Override
public boolean getDowngradeAllowed() {
return allowDowngrade;
}
}

private void instantiateSignatureAlgorithmFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public Path getStoragePath() {
public Path getDataPath() {
return dataPath;
}

@Override
public boolean getDowngradeAllowed() {
return false; // TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public Path getDataPath() {
public int getDatabaseVersion() {
return 2;
}

@Override
public boolean getDowngradeAllowed() {
return false;
}
})
.withMetricsSystem(new NoOpMetricsSystem())
.build();
Expand Down
2 changes: 2 additions & 0 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,7 @@ dependencyManagement {
dependency 'tech.pegasys.discovery:discovery:22.2.0'

dependency 'com.github.oshi:oshi-core:6.4.1'

dependency 'org.apache.maven:maven-artifact:3.9.6'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface BesuConfiguration extends BesuService {
default int getDatabaseVersion() {
return 1;
}

boolean getDowngradeAllowed();
}
2 changes: 1 addition & 1 deletion plugins/rocksdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {

implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8'
implementation 'org.apache.maven:maven-artifact:3.9.6'
implementation 'org.apache.maven:maven-artifact'
implementation 'com.google.guava:guava'
implementation 'info.picocli:picocli'
implementation 'io.opentelemetry:opentelemetry-api'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private int readDatabaseVersion(final BesuConfiguration commonConfiguration) thr
databaseVersion = dbMetaData.getVersion();
besuVersion = dbMetaData.getBesuVersion();
LOG.info(
"Existing database detected at {}. DB version {}. Installed version {}. Compacting database...",
"Existing database detected at {}. DB version {}. Besu version {}. Compacting database...",
dataDir,
databaseVersion,
besuVersion);
Expand All @@ -280,15 +280,29 @@ private int readDatabaseVersion(final BesuConfiguration commonConfiguration) thr
if (versionComparison == 0) {
// Versions match - no-op
} else if (versionComparison < 0) {
final String message =
"Besu version "
+ installedVersion
+ " is lower than version "
+ dbBesuVersion
+ " that last updated the database."
+ ". Specify --downgrade to use different version.";
LOG.error(message);
throw new StorageException(message);
if (commonConfiguration.getDowngradeAllowed()) {
LOG.warn(
"Besu version {} is lower than version {} that last wrote to the database. Allowing startup because --allow-downgrade has been enabled.",
installedVersion,
dbBesuVersion);
// We've allowed startup at an older version of Besu. Since the version in the metadata
// file records the latest version of
// Besu to write to the database we'll update the metadata version to this
// downgraded-version. This avoids the need after a successful
// downgrade to keep specifying --allow-downgrade on every startup.
writeDatabaseMetadata(
databaseVersion, Optional.of(commonConfiguration.getBesuVersion()), dataDir);
} else {
final String message =
"Besu version "
+ installedVersion
+ " is lower than version "
+ dbBesuVersion
+ " that last updated the database."
+ ". Specify --allow-downgrade to allow Besu to start at the lower version (warning - this may have unrecoverable effects on the node database).";
LOG.error(message);
throw new StorageException(message);
}
} else if (versionComparison > 0) {
LOG.info(
"Besu version {} is higher than version {} that last updated the DB. Updating DB metadata.",
Expand All @@ -297,6 +311,12 @@ private int readDatabaseVersion(final BesuConfiguration commonConfiguration) thr
writeDatabaseMetadata(
databaseVersion, Optional.of(commonConfiguration.getBesuVersion()), dataDir);
}
} else {
// No besu version information was found in the metadata file, so update it with the current
// version and carry on
LOG.info("Adding Besu version to metadata file.");
writeDatabaseMetadata(
databaseVersion, Optional.of(commonConfiguration.getBesuVersion()), dataDir);
}
} else {
databaseVersion = commonConfiguration.getDatabaseVersion();
Expand Down

0 comments on commit deb1d75

Please sign in to comment.