-
Notifications
You must be signed in to change notification settings - Fork 30
Add chainstate storage version mismatch check #1098
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29d1855
Add version mismatch check
azarovh c118f5b
Clean up directory and retry
azarovh cc521f8
Add clean_data command
azarovh 3128926
Minor fixes
azarovh 9c4a229
Check magic bytes and chain type
azarovh f9127b3
Move storage checks to a mod
azarovh 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) 2023 RBB S.r.l | ||
// opensource@mintlayer.org | ||
// SPDX-License-Identifier: MIT | ||
// Licensed under the MIT License; | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE | ||
// | ||
// 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. | ||
|
||
use chainstate::StorageCompatibilityCheckError; | ||
use chainstate_storage::{BlockchainStorageRead, BlockchainStorageWrite}; | ||
use common::chain::ChainConfig; | ||
use utils::ensure; | ||
|
||
const CHAINSTATE_STORAGE_VERSION_UNINITIALIZED: u32 = 0; | ||
const CHAINSTATE_STORAGE_VERSION_V1: u32 = 1; | ||
const CURRENT_CHAINSTATE_STORAGE_VERSION: u32 = CHAINSTATE_STORAGE_VERSION_V1; | ||
|
||
pub fn check_storage_compatibility<B: 'static + storage::Backend>( | ||
storage: &mut chainstate_storage::Store<B>, | ||
chain_config: &ChainConfig, | ||
) -> Result<(), StorageCompatibilityCheckError> { | ||
check_storage_version(storage)?; | ||
check_magic_bytes(storage, chain_config)?; | ||
check_chain_type(storage, chain_config)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn check_storage_version<B: 'static + storage::Backend>( | ||
storage: &mut chainstate_storage::Store<B>, | ||
) -> Result<(), StorageCompatibilityCheckError> { | ||
let storage_version = storage | ||
.get_storage_version() | ||
.map_err(StorageCompatibilityCheckError::StorageError)?; | ||
|
||
if storage_version == CHAINSTATE_STORAGE_VERSION_UNINITIALIZED { | ||
storage | ||
.set_storage_version(CURRENT_CHAINSTATE_STORAGE_VERSION) | ||
.map_err(StorageCompatibilityCheckError::StorageError)?; | ||
} else { | ||
ensure!( | ||
storage_version == CURRENT_CHAINSTATE_STORAGE_VERSION, | ||
StorageCompatibilityCheckError::ChainstateStorageVersionMismatch( | ||
storage_version, | ||
CURRENT_CHAINSTATE_STORAGE_VERSION | ||
) | ||
); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn check_magic_bytes<B: 'static + storage::Backend>( | ||
storage: &mut chainstate_storage::Store<B>, | ||
chain_config: &ChainConfig, | ||
) -> Result<(), StorageCompatibilityCheckError> { | ||
let storage_magic_bytes = storage | ||
.get_magic_bytes() | ||
.map_err(StorageCompatibilityCheckError::StorageError)?; | ||
let chain_config_magic_bytes = chain_config.magic_bytes(); | ||
|
||
match storage_magic_bytes { | ||
Some(storage_magic_bytes) => ensure!( | ||
&storage_magic_bytes == chain_config_magic_bytes, | ||
StorageCompatibilityCheckError::ChainConfigMagicBytesMismatch( | ||
storage_magic_bytes, | ||
chain_config_magic_bytes.to_owned() | ||
) | ||
), | ||
None => storage | ||
.set_magic_bytes(chain_config_magic_bytes) | ||
.map_err(StorageCompatibilityCheckError::StorageError)?, | ||
}; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn check_chain_type<B: 'static + storage::Backend>( | ||
storage: &mut chainstate_storage::Store<B>, | ||
chain_config: &ChainConfig, | ||
) -> Result<(), StorageCompatibilityCheckError> { | ||
let storage_chain_type = | ||
storage.get_chain_type().map_err(StorageCompatibilityCheckError::StorageError)?; | ||
let chain_config_type = chain_config.chain_type().name(); | ||
|
||
match storage_chain_type { | ||
Some(storage_chain_type) => ensure!( | ||
storage_chain_type == chain_config_type, | ||
StorageCompatibilityCheckError::ChainTypeMismatch( | ||
storage_chain_type, | ||
chain_config_type.to_owned() | ||
) | ||
), | ||
None => storage | ||
.set_chain_type(chain_config_type) | ||
.map_err(StorageCompatibilityCheckError::StorageError)?, | ||
}; | ||
|
||
Ok(()) | ||
} |
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
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
Oops, something went wrong.
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.