-
Notifications
You must be signed in to change notification settings - Fork 153
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
feat: new command for inspecting state-root mismatches #3589
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e913a3b
scaffolding for better state validation
lemmih a2f97f4
Merge branch 'main' into lemmih/hoist-forest-cli-state-diff
lemmih c940455
print state diffs
lemmih c95f3bd
Merge branch 'main' into lemmih/hoist-forest-cli-state-diff
lemmih 993bf93
remove previous diff command
lemmih 13bc46b
document the 'forest-tool archive diff' command
lemmih bcb912a
add changelog entries
lemmih 325f147
Merge branch 'main' into lemmih/hoist-forest-cli-state-diff
ruseinov 5f31ddc
add instructions for how to use `forest-tool archive diff`
lemmih cca9ee6
describe how to create a forest.car.zst file
lemmih 9330b99
document the depth argument
lemmih 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,14 @@ use crate::cid_collections::CidHashSet; | |
use crate::cli_shared::{snapshot, snapshot::TrustedVendor}; | ||
use crate::db::car::ManyCar; | ||
use crate::db::car::{AnyCar, RandomAccessFileReader}; | ||
use crate::interpreter::VMTrace; | ||
use crate::ipld::{stream_graph, unordered_stream_graph}; | ||
use crate::networks::{calibnet, mainnet, ChainConfig, NetworkChain}; | ||
use crate::shim::address::CurrentNetwork; | ||
use crate::shim::clock::{ChainEpoch, EPOCHS_IN_DAY, EPOCH_DURATION_SECONDS}; | ||
use crate::shim::fvm_shared_latest::address::Network; | ||
use crate::shim::machine::MultiEngine; | ||
use crate::state_manager::{apply_block_messages, NO_CALLBACK}; | ||
use anyhow::{bail, Context as _}; | ||
use chrono::NaiveDateTime; | ||
use clap::Subcommand; | ||
|
@@ -81,6 +86,18 @@ pub enum ArchiveCommands { | |
#[arg(long, default_value_t = false)] | ||
force: bool, | ||
}, | ||
/// Show the difference between the canonical and computed state of a | ||
/// tipset. | ||
Diff { | ||
/// Snapshot input paths. Supports `.car`, `.car.zst`, and `.forest.car.zst`. | ||
#[arg(required = true)] | ||
snapshot_files: Vec<PathBuf>, | ||
/// Selected epoch to validate. | ||
#[arg(long)] | ||
epoch: ChainEpoch, | ||
#[arg(long)] | ||
depth: Option<u64>, | ||
Comment on lines
+100
to
+101
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. We could document this as well. 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. Done. |
||
}, | ||
} | ||
|
||
impl ArchiveCommands { | ||
|
@@ -124,6 +141,11 @@ impl ArchiveCommands { | |
output_path, | ||
force, | ||
} => merge_snapshots(snapshot_files, output_path, force).await, | ||
Self::Diff { | ||
snapshot_files, | ||
epoch, | ||
depth, | ||
} => show_tipset_diff(snapshot_files, epoch, depth).await, | ||
} | ||
} | ||
} | ||
|
@@ -440,6 +462,85 @@ async fn merge_snapshots( | |
Ok(()) | ||
} | ||
|
||
/// Compute the tree of actor states for a given epoch and compare it to the | ||
/// expected result (as encoded in the blockchain). Differences are printed | ||
/// using the diff format (red for the blockchain state, green for the computed | ||
/// state). | ||
async fn show_tipset_diff( | ||
snapshot_files: Vec<PathBuf>, | ||
epoch: ChainEpoch, | ||
depth: Option<u64>, | ||
) -> anyhow::Result<()> { | ||
use colored::*; | ||
|
||
let store = Arc::new(ManyCar::try_from(snapshot_files)?); | ||
|
||
let heaviest_tipset = Arc::new(store.heaviest_tipset()?); | ||
if heaviest_tipset.epoch() <= epoch { | ||
anyhow::bail!( | ||
"Highest epoch must be at least 1 greater than the target epoch. \ | ||
Highest epoch = {}, target epoch = {}.", | ||
heaviest_tipset.epoch(), | ||
epoch | ||
) | ||
} | ||
|
||
let genesis = heaviest_tipset.genesis(&store)?; | ||
let network = NetworkChain::from_genesis_or_devnet_placeholder(genesis.cid()); | ||
|
||
let timestamp = genesis.timestamp(); | ||
let chain_index = ChainIndex::new(Arc::clone(&store)); | ||
let chain_config = ChainConfig::from_chain(&network); | ||
if chain_config.is_testnet() { | ||
CurrentNetwork::set_global(Network::Testnet); | ||
} | ||
let beacon = Arc::new(chain_config.get_beacon_schedule(timestamp)); | ||
let tipset = chain_index.tipset_by_height( | ||
epoch, | ||
Arc::clone(&heaviest_tipset), | ||
ResolveNullTipset::TakeOlder, | ||
)?; | ||
|
||
let child_tipset = chain_index.tipset_by_height( | ||
epoch + 1, | ||
Arc::clone(&heaviest_tipset), | ||
ResolveNullTipset::TakeNewer, | ||
)?; | ||
|
||
let (state_root, _) = apply_block_messages( | ||
timestamp, | ||
Arc::new(chain_index), | ||
Arc::new(chain_config), | ||
beacon, | ||
&MultiEngine::default(), | ||
tipset, | ||
NO_CALLBACK, | ||
VMTrace::NotTraced, | ||
)?; | ||
|
||
if child_tipset.parent_state() != &state_root { | ||
println!( | ||
"{}", | ||
format!("- Expected state hash: {}", child_tipset.parent_state()).red() | ||
); | ||
println!( | ||
"{}", | ||
format!("+ Computed state hash: {}", state_root).green() | ||
); | ||
|
||
crate::statediff::print_state_diff( | ||
&store, | ||
&state_root, | ||
child_tipset.parent_state(), | ||
depth, | ||
)?; | ||
} else { | ||
println!("Computed state matches expected state."); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
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.
For completeness, let's write how to perform this conversion.
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.
Done.