-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(trie): Add helper sub-command #18301
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
15 commits
Select commit
Hold shift + click to select a range
5914fd9
WIP
mediocregopher 46f73f1
disable tx timeout
mediocregopher ebbd5c5
Add actual repairing logic
mediocregopher 6090e34
Depth-first
mediocregopher 59d08f5
progress indicator
mediocregopher 9ac75e7
better progress indicator
mediocregopher d2c7e7c
More progress improvements
mediocregopher 515e9f5
fmt and other simplifications
mediocregopher 4188229
Include validation that accounts with no storage have empty tries
mediocregopher 0bcbae8
Fix verify_empty_storages
mediocregopher 512c8f4
remove print
mediocregopher 09927ed
Lints
mediocregopher 7d0de2c
vocs fix
mediocregopher 9ef5eac
lint
mediocregopher e9d6e8e
Reword inconsistency warning
mediocregopher 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,163 @@ | ||
| use clap::Parser; | ||
| use reth_db_api::{ | ||
| cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}, | ||
| database::Database, | ||
| tables, | ||
| transaction::{DbTx, DbTxMut}, | ||
| }; | ||
| use reth_node_builder::NodeTypesWithDB; | ||
| use reth_provider::ProviderFactory; | ||
| use reth_trie::{ | ||
| verify::{Output, Verifier}, | ||
| Nibbles, | ||
| }; | ||
| use reth_trie_common::{StorageTrieEntry, StoredNibbles, StoredNibblesSubKey}; | ||
| use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; | ||
| use std::time::{Duration, Instant}; | ||
| use tracing::{info, warn}; | ||
|
|
||
| /// The arguments for the `reth db repair-trie` command | ||
| #[derive(Parser, Debug)] | ||
| pub struct Command { | ||
| /// Only show inconsistencies without making any repairs | ||
| #[arg(long)] | ||
| dry_run: bool, | ||
| } | ||
|
|
||
| impl Command { | ||
| /// Execute `db repair-trie` command | ||
| pub fn execute<N: NodeTypesWithDB>( | ||
| self, | ||
| provider_factory: ProviderFactory<N>, | ||
| ) -> eyre::Result<()> { | ||
| // Get a database transaction directly from the database | ||
| let db = provider_factory.db_ref(); | ||
| let mut tx = db.tx_mut()?; | ||
| tx.disable_long_read_transaction_safety(); | ||
|
|
||
| // Create the hashed cursor factory | ||
| let hashed_cursor_factory = DatabaseHashedCursorFactory::new(&tx); | ||
|
|
||
| // Create the trie cursor factory | ||
| let trie_cursor_factory = DatabaseTrieCursorFactory::new(&tx); | ||
|
|
||
| // Create the verifier | ||
| let verifier = Verifier::new(trie_cursor_factory, hashed_cursor_factory)?; | ||
|
|
||
| let mut account_trie_cursor = tx.cursor_write::<tables::AccountsTrie>()?; | ||
| let mut storage_trie_cursor = tx.cursor_dup_write::<tables::StoragesTrie>()?; | ||
|
|
||
| let mut inconsistent_nodes = 0; | ||
| let start_time = Instant::now(); | ||
| let mut last_progress_time = Instant::now(); | ||
|
|
||
| // Iterate over the verifier and repair inconsistencies | ||
| for output_result in verifier { | ||
| let output = output_result?; | ||
|
|
||
| if let Output::Progress(path) = output { | ||
| // Output progress every 5 seconds | ||
| if last_progress_time.elapsed() > Duration::from_secs(5) { | ||
| output_progress(path, start_time, inconsistent_nodes); | ||
| last_progress_time = Instant::now(); | ||
| } | ||
| continue | ||
| }; | ||
|
|
||
| warn!("Inconsistency found, will repair: {output:?}"); | ||
| inconsistent_nodes += 1; | ||
|
|
||
| if self.dry_run { | ||
| continue; | ||
| } | ||
|
|
||
| match output { | ||
| Output::AccountExtra(path, _node) => { | ||
| // Extra account node in trie, remove it | ||
| let nibbles = StoredNibbles(path); | ||
| if account_trie_cursor.seek_exact(nibbles)?.is_some() { | ||
| account_trie_cursor.delete_current()?; | ||
| } | ||
| } | ||
| Output::StorageExtra(account, path, _node) => { | ||
| // Extra storage node in trie, remove it | ||
| let nibbles = StoredNibblesSubKey(path); | ||
| if storage_trie_cursor | ||
| .seek_by_key_subkey(account, nibbles.clone())? | ||
| .filter(|e| e.nibbles == nibbles) | ||
| .is_some() | ||
| { | ||
| storage_trie_cursor.delete_current()?; | ||
| } | ||
| } | ||
| Output::AccountWrong { path, expected: node, .. } | | ||
| Output::AccountMissing(path, node) => { | ||
| // Wrong/missing account node value, upsert it | ||
| let nibbles = StoredNibbles(path); | ||
| account_trie_cursor.upsert(nibbles, &node)?; | ||
| } | ||
| Output::StorageWrong { account, path, expected: node, .. } | | ||
| Output::StorageMissing(account, path, node) => { | ||
| // Wrong/missing storage node value, upsert it | ||
| let nibbles = StoredNibblesSubKey(path); | ||
| let entry = StorageTrieEntry { nibbles, node }; | ||
| storage_trie_cursor.upsert(account, &entry)?; | ||
| } | ||
| Output::Progress(_) => { | ||
| unreachable!() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if inconsistent_nodes > 0 { | ||
| if self.dry_run { | ||
| info!("Found {} inconsistencies (dry run - no changes made)", inconsistent_nodes); | ||
| } else { | ||
| info!("Repaired {} inconsistencies", inconsistent_nodes); | ||
| tx.commit()?; | ||
| info!("Changes committed to database"); | ||
| } | ||
| } else { | ||
| info!("No inconsistencies found"); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Output progress information based on the last seen account path. | ||
| fn output_progress(last_account: Nibbles, start_time: Instant, inconsistent_nodes: u64) { | ||
| // Calculate percentage based on position in the trie path space | ||
| // For progress estimation, we'll use the first few nibbles as an approximation | ||
|
|
||
| // Convert the first 16 nibbles (8 bytes) to a u64 for progress calculation | ||
| let mut current_value: u64 = 0; | ||
| let nibbles_to_use = last_account.len().min(16); | ||
|
|
||
| for i in 0..nibbles_to_use { | ||
| current_value = (current_value << 4) | (last_account.get(i).unwrap_or(0) as u64); | ||
| } | ||
| // Shift left to fill remaining bits if we have fewer than 16 nibbles | ||
| if nibbles_to_use < 16 { | ||
| current_value <<= (16 - nibbles_to_use) * 4; | ||
| } | ||
|
|
||
| let progress_percent = current_value as f64 / u64::MAX as f64 * 100.0; | ||
| let progress_percent_str = format!("{progress_percent:.2}"); | ||
|
|
||
| // Calculate ETA based on current speed | ||
| let elapsed = start_time.elapsed(); | ||
| let elapsed_secs = elapsed.as_secs_f64(); | ||
|
|
||
| let estimated_total_time = | ||
| if progress_percent > 0.0 { elapsed_secs / (progress_percent / 100.0) } else { 0.0 }; | ||
| let remaining_time = estimated_total_time - elapsed_secs; | ||
| let eta_duration = Duration::from_secs(remaining_time as u64); | ||
|
|
||
| info!( | ||
| progress_percent = progress_percent_str, | ||
| eta = %humantime::format_duration(eta_duration), | ||
| inconsistent_nodes, | ||
| "Repairing trie tables", | ||
| ); | ||
| } |
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.
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.
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.
we could add a --dry-run arg and init this with readonly, but could do this as a followup