-
Notifications
You must be signed in to change notification settings - Fork 1k
Restrict downgrade #6307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Restrict downgrade #6307
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
32cb5fc
Add Besu version to DB metadata. Check for downgrades and reject if v…
matthew1001 723f268
Add --allow-downgrade CLI arg. If set it allows the downgrade and upd…
matthew1001 494c8d3
Update gradle verification XML
matthew1001 4520865
Add and update tests
matthew1001 14014fa
Refactoring
matthew1001 96f5080
Remove versioning from RocksDB, now in separate VERSION_DATADATA.json
matthew1001 4c4b66b
Tidy up and tests for the new class
matthew1001 ff03349
Move downgrade logic into VersionMetadata as BesuCommand is already v…
matthew1001 ee7a6ee
Add more tests
matthew1001 30f395d
Merge branch 'main' into restrict-downgrade
matthew1001 fb9e035
Refactor the naming of the option to version-compatibility-protection
matthew1001 95370ab
Remove remaining references to allow-downgrade
matthew1001 383a7ba
Rename test
matthew1001 719a174
Update comments
matthew1001 32bae87
Merge branch 'main' into restrict-downgrade
matthew1001 1526cd7
Metadata verification update
matthew1001 555ad6f
gradle fix
matthew1001 bc25ba7
Enable version downgrade protection by default for non-named networks
matthew1001 5d4db2a
Fix default logic
matthew1001 f2344d0
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core…
matthew1001 8a4ecb2
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core…
matthew1001 a728e49
Merge branch 'main' into restrict-downgrade
matthew1001 b828a37
mock-maker-inline no longer needed
matthew1001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright Hyperledger Besu contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.core; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class VersionMetadata { | ||
| private static final Logger LOG = LoggerFactory.getLogger(VersionMetadata.class); | ||
|
|
||
| /** Represents an unknown Besu version in the version metadata file */ | ||
| public static final String BESU_VERSION_UNKNOWN = "UNKNOWN"; | ||
|
|
||
| private static final String METADATA_FILENAME = "VERSION_METADATA.json"; | ||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
| private final String besuVersion; | ||
|
|
||
| /** | ||
| * Get the version of Besu that is running. | ||
| * | ||
| * @return the version of Besu | ||
| */ | ||
| public static String getRuntimeVersion() { | ||
| return VersionMetadata.class.getPackage().getImplementationVersion(); | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public VersionMetadata(@JsonProperty("besuVersion") final String besuVersion) { | ||
| this.besuVersion = besuVersion; | ||
| } | ||
|
|
||
| public String getBesuVersion() { | ||
| return besuVersion; | ||
| } | ||
|
|
||
| public static VersionMetadata lookUpFrom(final Path dataDir) throws IOException { | ||
| LOG.info("Lookup version metadata file in data directory: {}", dataDir.toString()); | ||
matthew1001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return resolveVersionMetadata(getDefaultMetadataFile(dataDir)); | ||
| } | ||
|
|
||
| public void writeToDirectory(final Path dataDir) throws IOException { | ||
| MAPPER.writeValue(getDefaultMetadataFile(dataDir), this); | ||
| } | ||
|
|
||
| private static File getDefaultMetadataFile(final Path dataDir) { | ||
| return dataDir.resolve(METADATA_FILENAME).toFile(); | ||
| } | ||
|
|
||
| private static VersionMetadata resolveVersionMetadata(final File metadataFile) | ||
| throws IOException { | ||
| VersionMetadata versionMetadata; | ||
| try { | ||
| versionMetadata = MAPPER.readValue(metadataFile, VersionMetadata.class); | ||
| LOG.info("Existing version data detected. Besu version {}", versionMetadata.besuVersion); | ||
| } catch (FileNotFoundException fnfe) { | ||
| versionMetadata = new VersionMetadata(BESU_VERSION_UNKNOWN); | ||
| } catch (JsonProcessingException jpe) { | ||
| throw new IllegalStateException( | ||
| java.lang.String.format("Invalid metadata file %s", metadataFile.getAbsolutePath()), jpe); | ||
matthew1001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return versionMetadata; | ||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/VersionMetadataTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright Hyperledger Besu contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.hyperledger.besu.ethereum.core; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class VersionMetadataTest { | ||
| @TempDir public Path temporaryFolder; | ||
|
|
||
| @Test | ||
| void getVersion() { | ||
| final VersionMetadata versionMetadata = new VersionMetadata("23.10.2"); | ||
| assertThat(versionMetadata).isNotNull(); | ||
| assertThat(versionMetadata.getBesuVersion()).isEqualTo("23.10.2"); | ||
| } | ||
|
|
||
| @Test | ||
| void metaFileShouldContain() throws Exception { | ||
| final Path tempDataDir = | ||
| createAndWrite("data", "VERSION_METADATA.json", "{\"besuVersion\":\"23.10.3\"}"); | ||
|
|
||
| final VersionMetadata versionMetadata = VersionMetadata.lookUpFrom(tempDataDir); | ||
| assertThat(versionMetadata).isNotNull(); | ||
| assertThat(versionMetadata.getBesuVersion()).isEqualTo("23.10.3"); | ||
| } | ||
|
|
||
| private Path createAndWrite(final String dir, final String file, final String content) | ||
| throws IOException { | ||
| return createAndWrite(temporaryFolder, dir, file, content); | ||
| } | ||
|
|
||
| private Path createAndWrite( | ||
| final Path temporaryFolder, final String dir, final String file, final String content) | ||
| throws IOException { | ||
| final Path tmpDir = temporaryFolder.resolve(dir); | ||
| Files.createDirectories(tmpDir); | ||
| createAndWrite(tmpDir.resolve(file), content); | ||
| return tmpDir; | ||
| } | ||
|
|
||
| private void createAndWrite(final Path path, final String content) throws IOException { | ||
| path.toFile().createNewFile(); | ||
| Files.writeString(path, content); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.