forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # .github/workflows/artifacts.yml # .github/workflows/docker.yml # .github/workflows/nightly.yml # .github/workflows/release.yml # acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java # besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java # besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java # besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java # besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java # besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockMinerTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java # consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java # consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java # consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java # consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCoinbaseValidationRuleTest.java # consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCommitSealsValidationRuleTest.java # consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftValidatorsValidationRuleTest.java # consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftVanityDataValidationRuleTest.java # consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java # consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/round/IbftRoundIntegrationTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/blockcreation/BftBlockCreatorTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftBlockHeightManagerTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRoundTest.java # consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java # consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java # consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeReorgTest.java # consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java # consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/test/round/QbftRoundIntegrationTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftBlockHeaderValidationRulesetFactoryTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/headervalidationrules/QbftValidatorsValidationRuleTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/statemachine/QbftBlockHeightManagerTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/statemachine/QbftRoundTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/validation/ProposalPayloadValidatorTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/validation/ProposalValidatorTest.java # consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/validation/RoundChangeMessageValidatorTest.java # ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java # ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java # ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java # ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java # ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java # ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java # ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java # ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java # ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/bonsai/AbstractIsolationTests.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ethtaskutils/AbstractMessageTaskTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java # ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java # ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java # plugin-api/build.gradle
- Loading branch information
Showing
145 changed files
with
1,714 additions
and
1,355 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
135 changes: 135 additions & 0 deletions
135
.../src/main/java/org/hyperledger/besu/cli/subcommands/storage/RevertMetadataSubCommand.java
This file contains 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,135 @@ | ||
/* | ||
* 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.cli.subcommands.storage; | ||
|
||
import org.hyperledger.besu.cli.util.VersionProvider; | ||
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.OptionalInt; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.ParentCommand; | ||
|
||
/** The revert metadata to v1 subcommand. */ | ||
@Command( | ||
name = "revert-metadata", | ||
description = "Revert database metadata to previous format", | ||
mixinStandardHelpOptions = true, | ||
versionProvider = VersionProvider.class, | ||
subcommands = RevertMetadataSubCommand.v2ToV1.class) | ||
public class RevertMetadataSubCommand implements Runnable { | ||
private static final Logger LOG = LoggerFactory.getLogger(RevertMetadataSubCommand.class); | ||
private static final String METADATA_FILENAME = "DATABASE_METADATA.json"; | ||
private static final ObjectMapper MAPPER = | ||
new ObjectMapper() | ||
.registerModule(new Jdk8Module()) | ||
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT) | ||
.enable(SerializationFeature.INDENT_OUTPUT); | ||
|
||
@SuppressWarnings("unused") | ||
@ParentCommand | ||
private StorageSubCommand parentCommand; | ||
|
||
@SuppressWarnings("unused") | ||
@CommandLine.Spec | ||
private CommandLine.Model.CommandSpec spec; | ||
|
||
@Override | ||
public void run() { | ||
spec.commandLine().usage(System.out); | ||
} | ||
|
||
@Command( | ||
name = "v2-to-v1", | ||
description = "Revert a database metadata v2 format to v1 format", | ||
mixinStandardHelpOptions = true, | ||
versionProvider = VersionProvider.class) | ||
static class v2ToV1 implements Runnable { | ||
|
||
@SuppressWarnings("unused") | ||
@CommandLine.Spec | ||
private CommandLine.Model.CommandSpec spec; | ||
|
||
@SuppressWarnings("unused") | ||
@ParentCommand | ||
private RevertMetadataSubCommand parentCommand; | ||
|
||
@Override | ||
public void run() { | ||
|
||
final Path dataDir = parentCommand.parentCommand.besuCommand.dataDir(); | ||
|
||
final File dbMetadata = dataDir.resolve(METADATA_FILENAME).toFile(); | ||
if (!dbMetadata.exists()) { | ||
String errMsg = | ||
String.format( | ||
"Could not find database metadata file %s, check your data dir %s", | ||
dbMetadata, dataDir); | ||
LOG.error(errMsg); | ||
throw new IllegalArgumentException(errMsg); | ||
} | ||
try { | ||
final var root = MAPPER.readTree(dbMetadata); | ||
if (!root.has("v2")) { | ||
String errMsg = | ||
String.format("Database metadata file %s is not in v2 format", dbMetadata); | ||
LOG.error(errMsg); | ||
throw new IllegalArgumentException(errMsg); | ||
} | ||
|
||
final var v2Obj = root.get("v2"); | ||
if (!v2Obj.has("format")) { | ||
String errMsg = | ||
String.format( | ||
"Database metadata file %s is malformed, \"format\" field not found", dbMetadata); | ||
LOG.error(errMsg); | ||
throw new IllegalArgumentException(errMsg); | ||
} | ||
|
||
final var formatField = v2Obj.get("format").asText(); | ||
final OptionalInt maybePrivacyVersion = | ||
v2Obj.has("privacyVersion") | ||
? OptionalInt.of(v2Obj.get("privacyVersion").asInt()) | ||
: OptionalInt.empty(); | ||
|
||
final DataStorageFormat dataStorageFormat = DataStorageFormat.valueOf(formatField); | ||
final int v1Version = | ||
switch (dataStorageFormat) { | ||
case FOREST -> 1; | ||
case BONSAI -> 2; | ||
}; | ||
|
||
@JsonSerialize | ||
record V1(int version, OptionalInt privacyVersion) {} | ||
|
||
MAPPER.writeValue(dbMetadata, new V1(v1Version, maybePrivacyVersion)); | ||
LOG.info("Successfully reverted database metadata from v2 to v1 in {}", dbMetadata); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.