-
Notifications
You must be signed in to change notification settings - Fork 188
tool: forest-dev export-tipset-lookup #6827
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
4 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
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,114 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use crate::{ | ||
| chain::ChainStore, | ||
| cli_shared::{chain_path, read_config}, | ||
| daemon::db_util::load_all_forest_cars, | ||
| db::{ | ||
| CAR_DB_DIR_NAME, MemoryDB, | ||
| car::ManyCar, | ||
| db_engine::{db_root, open_db}, | ||
| }, | ||
| genesis::read_genesis_header, | ||
| networks::{ChainConfig, NetworkChain}, | ||
| shim::clock::ChainEpoch, | ||
| }; | ||
| use clap::Args; | ||
| use fil_actors_shared::fvm_ipld_amt::Amt; | ||
| use human_repr::HumanCount; | ||
| use std::{num::NonZeroUsize, path::PathBuf, sync::Arc, time::Instant}; | ||
|
|
||
| /// Exports epoch to tipset key mapping AMT as a `ForestCAR` file for a given epoch range. | ||
| /// The exported AMT can be used to quickly look up the tipset key for a given epoch without traversing the chain, | ||
| /// which is useful for tools that need to access historical tipsets frequently. | ||
| #[derive(Debug, Args)] | ||
| pub struct ExportTipsetLookupCommand { | ||
| /// Filecoin network chain (e.g., calibnet, mainnet) | ||
| #[arg(long, required = true)] | ||
| chain: NetworkChain, | ||
| /// Optional path to the database folder | ||
| #[arg(long)] | ||
| db: Option<PathBuf>, | ||
| /// Start epoch (inclusive). Defaults to the current chain head | ||
| #[arg(long)] | ||
| from: Option<ChainEpoch>, | ||
| /// End epoch (inclusive). | ||
| #[arg(long, default_value = "0")] | ||
| to: ChainEpoch, | ||
| /// Every N epochs to skip when exporting the AMT. Defaults to 1 (export every epoch) | ||
| #[arg(long, default_value = "1")] | ||
| skip_length: NonZeroUsize, | ||
| /// The path to the output `ForestCAR` file | ||
| #[arg(short, long)] | ||
| output: PathBuf, | ||
| } | ||
|
|
||
| impl ExportTipsetLookupCommand { | ||
| pub async fn run(self) -> anyhow::Result<()> { | ||
| let Self { | ||
| chain, | ||
| db, | ||
| from, | ||
| to, | ||
| skip_length, | ||
| output, | ||
| } = self; | ||
| let skip_length = skip_length.get() as i64; | ||
| 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::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 head = chain_store.heaviest_tipset(); | ||
|
|
||
| let amt_db = Arc::new(MemoryDB::default()); | ||
| let mut amt = Amt::new(&amt_db); | ||
| let start = Instant::now(); | ||
| for ts in head.chain(chain_store.blockstore()) { | ||
| if let Some(from) = from | ||
| && ts.epoch() > from | ||
| { | ||
| continue; | ||
| } | ||
| if ts.epoch() < to { | ||
| break; | ||
| } | ||
| if ts.epoch() % skip_length != 0 { | ||
| continue; | ||
| } | ||
| amt.set(ts.epoch() as u64, ts.key().clone())?; | ||
| } | ||
| let root = amt.flush()?; | ||
| println!( | ||
| "Exported tipset lookup AMT with root CID: {root}, len: {}, size: {}, took {}", | ||
| amt_db.blockstore_len(), | ||
| amt_db.blockstore_size_bytes().human_count_bytes(), | ||
| humantime::format_duration(start.elapsed()) | ||
| ); | ||
| amt_db | ||
| .export_forest_car_with_roots( | ||
| nunny::vec![root], | ||
| &mut tokio::fs::File::create(output).await?, | ||
| ) | ||
| .await?; | ||
| 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
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.