-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add storage consistency checks on CLI commands #8667
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,27 @@ | ||
| //! Contains common `reth` arguments | ||
|
|
||
| use clap::Parser; | ||
| use reth_beacon_consensus::EthBeaconConsensus; | ||
| use reth_config::{config::EtlConfig, Config}; | ||
| use reth_db::{init_db, open_db_read_only, DatabaseEnv}; | ||
| use reth_db_common::init::init_genesis; | ||
| use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader}; | ||
| use reth_evm::noop::NoopBlockExecutorProvider; | ||
| use reth_node_core::{ | ||
| args::{ | ||
| utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS}, | ||
| DatabaseArgs, DatadirArgs, | ||
| }, | ||
| dirs::{ChainPath, DataDirPath}, | ||
| }; | ||
| use reth_primitives::ChainSpec; | ||
| use reth_provider::{providers::StaticFileProvider, ProviderFactory}; | ||
| use reth_primitives::{stage::PipelineTarget, ChainSpec}; | ||
| use reth_provider::{ | ||
| providers::StaticFileProvider, HeaderSyncMode, ProviderFactory, StaticFileProviderFactory, | ||
| }; | ||
| use reth_stages::{sets::DefaultStages, Pipeline}; | ||
| use reth_static_file::StaticFileProducer; | ||
| use std::{path::PathBuf, sync::Arc}; | ||
| use tracing::{debug, info}; | ||
| use tracing::{debug, info, warn}; | ||
|
|
||
| /// Struct to hold config and datadir paths | ||
| #[derive(Debug, Parser)] | ||
|
|
@@ -77,14 +84,70 @@ impl EnvironmentArgs { | |
| ), | ||
| }; | ||
|
|
||
| let provider_factory = ProviderFactory::new(db, self.chain.clone(), sfp); | ||
| let provider_factory = self.create_provider_factory(&config, db, sfp)?; | ||
| if access.is_read_write() { | ||
| debug!(target: "reth::cli", chain=%self.chain.chain, genesis=?self.chain.genesis_hash(), "Initializing genesis"); | ||
| init_genesis(provider_factory.clone())?; | ||
| } | ||
|
|
||
| Ok(Environment { config, provider_factory, data_dir }) | ||
| } | ||
|
|
||
| /// Returns a [`ProviderFactory`] after executing consistency checks. | ||
| /// | ||
| /// If it's a read-write environment and an issue is found, it will attempt to heal (including a | ||
| /// pipeline unwind). Otherwise, it will print out an warning, advising the user to restart the | ||
| /// node to heal. | ||
| fn create_provider_factory( | ||
| &self, | ||
| config: &Config, | ||
| db: Arc<DatabaseEnv>, | ||
| static_file_provider: StaticFileProvider, | ||
| ) -> eyre::Result<ProviderFactory<Arc<DatabaseEnv>>> { | ||
| let has_receipt_pruning = config.prune.as_ref().map_or(false, |a| a.has_receipts_pruning()); | ||
| let factory = ProviderFactory::new(db, self.chain.clone(), static_file_provider); | ||
|
|
||
| info!(target: "reth::cli", "Verifying storage consistency."); | ||
|
|
||
| // Check for consistency between database and static files. | ||
| if let Some(unwind_target) = factory | ||
| .static_file_provider() | ||
| .check_consistency(&factory.provider()?, has_receipt_pruning)? | ||
| { | ||
| if factory.db_ref().is_read_only() { | ||
| warn!(target: "reth::cli", ?unwind_target, "Inconsistent storage. Restart node to heal."); | ||
| return Ok(factory) | ||
| } | ||
|
|
||
| let prune_modes = config.prune.clone().map(|prune| prune.segments).unwrap_or_default(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, should be fn prune_segments |
||
|
|
||
| // Highly unlikely to happen, and given its destructive nature, it's better to panic | ||
| // instead. | ||
| assert_ne!(unwind_target, PipelineTarget::Unwind(0), "A static file <> database inconsistency was found that would trigger an unwind to block 0"); | ||
|
|
||
| info!(target: "reth::cli", unwind_target = %unwind_target, "Executing an unwind after a failed storage consistency check."); | ||
|
|
||
| // Builds and executes an unwind-only pipeline | ||
| let mut pipeline = Pipeline::builder() | ||
| .add_stages(DefaultStages::new( | ||
| factory.clone(), | ||
| HeaderSyncMode::Continuous, | ||
| Arc::new(EthBeaconConsensus::new(self.chain.clone())), | ||
| NoopHeaderDownloader::default(), | ||
| NoopBodiesDownloader::default(), | ||
| NoopBlockExecutorProvider::default(), | ||
| config.stages.clone(), | ||
| prune_modes.clone(), | ||
| )) | ||
| .build(factory.clone(), StaticFileProducer::new(factory.clone(), prune_modes)); | ||
|
|
||
| // Move all applicable data from database to static files. | ||
| pipeline.move_to_static_files()?; | ||
| pipeline.unwind(unwind_target.unwind_target().expect("should exist"), None)?; | ||
| } | ||
|
|
||
| Ok(factory) | ||
| } | ||
| } | ||
|
|
||
| /// Environment built from [`EnvironmentArgs`]. | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a function of th prune type