Skip to content

Commit 9dce227

Browse files
committed
Tidy up and tests for the new class
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
1 parent 96f5080 commit 9dce227

File tree

5 files changed

+104
-36
lines changed

5 files changed

+104
-36
lines changed

besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,12 +1466,13 @@ public void run() {
14661466
vertx = createVertx(createVertxOptions(metricsSystem.get()));
14671467

14681468
validateOptions();
1469+
1470+
performDowngradeCheck();
1471+
14691472
configure();
14701473
configureNativeLibs();
14711474
besuController = initController();
14721475

1473-
performDowngradeCheck();
1474-
14751476
besuPluginContext.beforeExternalServices();
14761477

14771478
final var runner = buildRunner();
@@ -1491,6 +1492,14 @@ public void run() {
14911492
}
14921493
}
14931494

1495+
/**
1496+
* This function is designed to protect a Besu instance from being unintentionally started at a lower
1497+
* version than the previous instance. Doing so could cause unexpected data corruption (depending on
1498+
* the storage provider that is in use), so this check prompts the user if a downgrade is detected
1499+
* and requires them to opt-in by setting --allow-downgrade. The --allow-downgrade flag only needs
1500+
* to be passed in once, and then the version information is updated to the lower version number, meaning
1501+
* future restarts will pass the check.
1502+
*/
14941503
private void performDowngradeCheck() throws IOException {
14951504
final VersionMetadata versionMetaData = VersionMetadata.lookUpFrom(dataDir());
14961505
if (versionMetaData.getBesuVersion().equals(VersionMetadata.BESU_VERSION_UNKNOWN)) {
@@ -1528,7 +1537,7 @@ private void performDowngradeCheck() throws IOException {
15281537
+ " is lower than version "
15291538
+ metadataVersion
15301539
+ " that last started."
1531-
+ ". Specify --allow-downgrade to allow Besu to start at the lower version (warning - this may have unrecoverable effects on the database).";
1540+
+ "Specify --allow-downgrade to allow Besu to start at the lower version (warning - this may have unrecoverable effects on the database).";
15321541
logger.error(message);
15331542
throw new StorageException(message);
15341543
}
@@ -3432,6 +3441,16 @@ String getLogLevel() {
34323441
return loggingLevelOption.getLogLevel();
34333442
}
34343443

3444+
/**
3445+
* Returns the flag indicating that downgrades are allowed.
3446+
*
3447+
* @return true if downgrades are allowed, otherwise false
3448+
*/
3449+
@VisibleForTesting
3450+
public Boolean getAllowDowngrade() {
3451+
return allowDowngrade;
3452+
}
3453+
34353454
private class BesuCommandConfigurationService implements BesuConfiguration {
34363455

34373456
@Override

besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,19 +1989,6 @@ public void parsesInvalidBonsaiTrieLimitBackLayersOption() {
19891989
"Invalid value for option '--bonsai-maximum-back-layers-to-load': 'ten' is not a long");
19901990
}
19911991

1992-
@Test
1993-
public void parsesValidAllowDowngradeOption() {
1994-
parseCommand("--allow-dowgrade", "true");
1995-
verify(mockControllerBuilder)
1996-
.dataStorageConfiguration(dataStorageConfigurationArgumentCaptor.capture());
1997-
1998-
final DataStorageConfiguration dataStorageConfiguration =
1999-
dataStorageConfigurationArgumentCaptor.getValue();
2000-
assertThat(dataStorageConfiguration.getAllowDowngrade()).isEqualTo(Boolean.valueOf("true"));
2001-
assertThat(commandOutput.toString(UTF_8)).isEmpty();
2002-
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
2003-
}
2004-
20051992
@Test
20061993
public void dnsEnabledOptionIsParsedCorrectly() {
20071994
final TestBesuCommand besuCommand = parseCommand("--Xdns-enabled", "true");
@@ -2010,6 +1997,13 @@ public void dnsEnabledOptionIsParsedCorrectly() {
20101997
assertThat(besuCommand.getEnodeDnsConfiguration().updateEnabled()).isFalse();
20111998
}
20121999

2000+
@Test
2001+
public void allowDowngradeTrueOptionIsParsedCorrectly() {
2002+
final TestBesuCommand besuCommand = parseCommand("--allow-downgrade", "true");
2003+
2004+
assertThat(besuCommand.getAllowDowngrade()).isTrue();
2005+
}
2006+
20132007
@Test
20142008
public void dnsUpdateEnabledOptionIsParsedCorrectly() {
20152009
final TestBesuCommand besuCommand =

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys AG.
2+
* Copyright Hyperledger Besu contributors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -29,7 +29,7 @@
2929
public class VersionMetadata {
3030
private static final Logger LOG = LoggerFactory.getLogger(VersionMetadata.class);
3131

32-
/** Represents an unknown Besu version in the database metadata file */
32+
/** Represents an unknown Besu version in the version metadata file */
3333
public static final String BESU_VERSION_UNKNOWN = "UNKNOWN";
3434

3535
private static final String METADATA_FILENAME = "VERSION_METADATA.json";
@@ -56,7 +56,7 @@ public String getBesuVersion() {
5656

5757
public static VersionMetadata lookUpFrom(final Path dataDir) throws IOException {
5858
LOG.info("Lookup version metadata file in data directory: {}", dataDir.toString());
59-
return resolveDatabaseMetadata(getDefaultMetadataFile(dataDir));
59+
return resolveVersionMetadata(getDefaultMetadataFile(dataDir));
6060
}
6161

6262
public void writeToDirectory(final Path dataDir) throws IOException {
@@ -67,18 +67,18 @@ private static File getDefaultMetadataFile(final Path dataDir) {
6767
return dataDir.resolve(METADATA_FILENAME).toFile();
6868
}
6969

70-
private static VersionMetadata resolveDatabaseMetadata(final File metadataFile)
70+
private static VersionMetadata resolveVersionMetadata(final File metadataFile)
7171
throws IOException {
72-
VersionMetadata databaseMetadata;
72+
VersionMetadata versionMetadata;
7373
try {
74-
databaseMetadata = MAPPER.readValue(metadataFile, VersionMetadata.class);
75-
LOG.info("Existing version data detected. Besu version {}", databaseMetadata.besuVersion);
74+
versionMetadata = MAPPER.readValue(metadataFile, VersionMetadata.class);
75+
LOG.info("Existing version data detected. Besu version {}", versionMetadata.besuVersion);
7676
} catch (FileNotFoundException fnfe) {
77-
databaseMetadata = new VersionMetadata(BESU_VERSION_UNKNOWN);
77+
versionMetadata = new VersionMetadata(BESU_VERSION_UNKNOWN);
7878
} catch (JsonProcessingException jpe) {
7979
throw new IllegalStateException(
8080
java.lang.String.format("Invalid metadata file %s", metadataFile.getAbsolutePath()), jpe);
8181
}
82-
return databaseMetadata;
82+
return versionMetadata;
8383
}
8484
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Hyperledger Besu contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package org.hyperledger.besu.ethereum.core;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.io.TempDir;
26+
27+
class VersionMetadataTest {
28+
@TempDir public Path temporaryFolder;
29+
30+
@Test
31+
void getVersion() {
32+
final VersionMetadata versionMetadata = new VersionMetadata("23.10.2");
33+
assertThat(versionMetadata).isNotNull();
34+
assertThat(versionMetadata.getBesuVersion()).isEqualTo("23.10.2");
35+
}
36+
37+
@Test
38+
void metaFileShouldContain() throws Exception {
39+
final Path tempDataDir =
40+
createAndWrite("data", "VERSION_METADATA.json", "{\"besuVersion\":\"23.10.3\"}");
41+
42+
final VersionMetadata versionMetadata = VersionMetadata.lookUpFrom(tempDataDir);
43+
assertThat(versionMetadata).isNotNull();
44+
assertThat(versionMetadata.getBesuVersion()).isEqualTo("23.10.3");
45+
}
46+
47+
private Path createAndWrite(final String dir, final String file, final String content)
48+
throws IOException {
49+
return createAndWrite(temporaryFolder, dir, file, content);
50+
}
51+
52+
private Path createAndWrite(
53+
final Path temporaryFolder, final String dir, final String file, final String content)
54+
throws IOException {
55+
final Path tmpDir = temporaryFolder.resolve(dir);
56+
Files.createDirectories(tmpDir);
57+
createAndWrite(tmpDir.resolve(file), content);
58+
return tmpDir;
59+
}
60+
61+
private void createAndWrite(final Path path, final String content) throws IOException {
62+
path.toFile().createNewFile();
63+
Files.writeString(path, content);
64+
}
65+
}

plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BesuService.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,4 @@
2121
* All services that can be resolved via {@link BesuContext#getService(Class)} must implement {@link
2222
* BesuService}
2323
*/
24-
public interface BesuService {
25-
26-
/**
27-
* Get the version of Besu that is running.
28-
*
29-
* @return the version of Besu
30-
*/
31-
default String getBesuVersion() {
32-
return BesuService.class.getPackage().getImplementationVersion();
33-
}
34-
}
24+
public interface BesuService {}

0 commit comments

Comments
 (0)