-
Notifications
You must be signed in to change notification settings - Fork 188
feat: forest-dev export-state-tree #6885
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
+176
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30263b6
feat: forest-dev export-state-tree
hanabi1224 701ccc2
Merge branch 'main' into hm/export-state-tree-cmd
hanabi1224 dd259a0
update markdown gen script
hanabi1224 8e1f60a
Update src/ipld/util.rs
hanabi1224 344abad
Merge branch 'main' into hm/export-state-tree-cmd
hanabi1224 f33f28d
Update util.rs
hanabi1224 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use crate::{ | ||
| chain::{ChainStore, index::ResolveNullTipset}, | ||
| cli_shared::{chain_path, read_config}, | ||
| daemon::db_util::load_all_forest_cars, | ||
| db::{ | ||
| CAR_DB_DIR_NAME, | ||
| car::ManyCar, | ||
| car::forest::FOREST_CAR_FILE_EXTENSION, | ||
| db_engine::{db_root, open_db}, | ||
| }, | ||
| genesis::read_genesis_header, | ||
| ipld::IpldStream, | ||
| networks::{ChainConfig, NetworkChain}, | ||
| shim::{clock::ChainEpoch, executor::Receipt}, | ||
| }; | ||
| use anyhow::Context as _; | ||
| use clap::Args; | ||
| use itertools::Itertools; | ||
| use std::{ | ||
| path::{Path, PathBuf}, | ||
| sync::Arc, | ||
| }; | ||
| use tokio::io::AsyncWriteExt as _; | ||
|
|
||
| /// Exports N consecutive parent state trees(together with messages, message receipts and events) of the tipset at the given epoch | ||
| #[derive(Debug, Args)] | ||
| pub struct ExportStateTreeCommand { | ||
| /// Filecoin network chain (e.g., calibnet, mainnet) | ||
| #[arg(long, required = true)] | ||
| chain: NetworkChain, | ||
| /// Optional path to the database folder | ||
| #[arg(long)] | ||
| db: Option<PathBuf>, | ||
| /// The maximum tipset epoch to export state tree from (Exclusive) | ||
| #[arg(long)] | ||
| from: ChainEpoch, | ||
| /// The minimum tipset epoch to export state tree from (Inclusive) | ||
| #[arg(long)] | ||
| to: ChainEpoch, | ||
| /// The path to the output `ForestCAR` file | ||
| #[arg(short, long)] | ||
| output: Option<PathBuf>, | ||
| } | ||
|
|
||
| impl ExportStateTreeCommand { | ||
| pub async fn run(self) -> anyhow::Result<()> { | ||
| let Self { | ||
| chain, | ||
| db, | ||
| from, | ||
| to, | ||
| output, | ||
| } = self; | ||
| let output = output.unwrap_or_else(|| { | ||
| Path::new(&format!( | ||
| "statetree_{chain}_{to}_{from}{FOREST_CAR_FILE_EXTENSION}" | ||
| )) | ||
| .to_owned() | ||
| }); | ||
| let db_root_path = if let Some(db) = db { | ||
| db | ||
| } else { | ||
| let (_, config) = read_config(None, Some(chain.clone()))?; | ||
| db_root(&chain_path(&config))? | ||
| }; | ||
| let forest_car_db_dir = db_root_path.join(CAR_DB_DIR_NAME); | ||
| let db: Arc<ManyCar<crate::db::parity_db::ParityDb>> = | ||
| Arc::new(ManyCar::new(open_db(db_root_path, &Default::default())?)); | ||
| load_all_forest_cars(&db, &forest_car_db_dir)?; | ||
|
|
||
| let chain_config = Arc::new(ChainConfig::from_chain(&chain)); | ||
| let genesis_header = | ||
| read_genesis_header(None, chain_config.genesis_bytes(&db).await?.as_deref(), &db) | ||
| .await?; | ||
| let chain_store = Arc::new(ChainStore::new( | ||
| db.clone(), | ||
| db.clone(), | ||
| db.clone(), | ||
| chain_config, | ||
| genesis_header, | ||
| )?); | ||
|
|
||
| let start_ts = chain_store.chain_index().tipset_by_height( | ||
| from, | ||
| chain_store.heaviest_tipset(), | ||
| ResolveNullTipset::TakeNewer, | ||
| )?; | ||
|
|
||
| let mut ipld_roots = vec![]; | ||
| for (child, ts) in start_ts | ||
| .chain(&db) | ||
| .tuple_windows() | ||
| .take_while(|(_, parent)| parent.epoch() >= to) | ||
| { | ||
| ipld_roots.extend([*child.parent_state(), *child.parent_message_receipts()]); | ||
| ipld_roots.extend(ts.block_headers().iter().map(|h| h.messages)); | ||
| let receipts = Receipt::get_receipts(&db, *child.parent_message_receipts()) | ||
| .with_context(|| { | ||
| format!( | ||
| "failed to get receipts, root: {}, epoch: {}, tipset key: {}", | ||
| *child.parent_message_receipts(), | ||
| ts.epoch(), | ||
| ts.key(), | ||
| ) | ||
| })?; | ||
| ipld_roots.extend(receipts.into_iter().filter_map(|r| r.events_root())); | ||
| } | ||
| let roots = nunny::vec![ipld_roots.first().cloned().context("no ipld roots found")?]; | ||
| let stream = IpldStream::new(db, ipld_roots.clone()); | ||
| let frames = crate::db::car::forest::Encoder::compress_stream_default(stream); | ||
| let tmp = | ||
| tempfile::NamedTempFile::new_in(output.parent().unwrap_or_else(|| Path::new(".")))? | ||
| .into_temp_path(); | ||
| let mut writer = tokio::io::BufWriter::new(tokio::fs::File::create(&tmp).await?); | ||
| crate::db::car::forest::Encoder::write(&mut writer, roots, frames).await?; | ||
| writer.flush().await?; | ||
| tmp.persist(output)?; | ||
|
|
||
| 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
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.