forest/daemon/
db_util.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::blocks::Tipset;
5use crate::db::car::forest::{
6    FOREST_CAR_FILE_EXTENSION, TEMP_FOREST_CAR_FILE_EXTENSION, new_forest_car_temp_path_in,
7};
8use crate::db::car::{ForestCar, ManyCar};
9use crate::interpreter::VMTrace;
10use crate::networks::ChainConfig;
11use crate::rpc::sync::SnapshotProgressTracker;
12use crate::shim::clock::ChainEpoch;
13use crate::state_manager::{NO_CALLBACK, StateManager};
14use crate::utils::db::car_stream::CarStream;
15use crate::utils::io::EitherMmapOrRandomAccessFile;
16use crate::utils::net::{DownloadFileOption, download_to};
17use anyhow::{Context, bail};
18use futures::TryStreamExt;
19use serde::{Deserialize, Serialize};
20use std::{
21    ffi::OsStr,
22    fs,
23    path::{Path, PathBuf},
24    sync::Arc,
25    time,
26};
27use tokio::io::AsyncWriteExt;
28use tracing::{debug, info, warn};
29use url::Url;
30use walkdir::WalkDir;
31
32#[cfg(doc)]
33use crate::rpc::eth::types::EthHash;
34
35#[cfg(doc)]
36use crate::blocks::TipsetKey;
37
38#[cfg(doc)]
39use cid::Cid;
40
41/// Loads all `.forest.car.zst` snapshots and cleanup stale `.forest.car.zst.tmp` files.
42pub fn load_all_forest_cars_with_cleanup<T>(
43    store: &ManyCar<T>,
44    forest_car_db_dir: &Path,
45) -> anyhow::Result<()> {
46    load_all_forest_cars_internal(store, forest_car_db_dir, true)
47}
48
49/// Loads all `.forest.car.zst` snapshots
50pub fn load_all_forest_cars<T>(store: &ManyCar<T>, forest_car_db_dir: &Path) -> anyhow::Result<()> {
51    load_all_forest_cars_internal(store, forest_car_db_dir, false)
52}
53
54fn load_all_forest_cars_internal<T>(
55    store: &ManyCar<T>,
56    forest_car_db_dir: &Path,
57    cleanup: bool,
58) -> anyhow::Result<()> {
59    if !forest_car_db_dir.is_dir() {
60        fs::create_dir_all(forest_car_db_dir)?;
61    }
62    for file in WalkDir::new(forest_car_db_dir)
63        .max_depth(1)
64        .into_iter()
65        .filter_map(|e| {
66            e.ok().and_then(|e| {
67                if !e.file_type().is_dir() {
68                    Some(e.into_path())
69                } else {
70                    None
71                }
72            })
73        })
74    {
75        if let Some(filename) = file.file_name().and_then(OsStr::to_str) {
76            if filename.ends_with(FOREST_CAR_FILE_EXTENSION) {
77                let car = ForestCar::try_from(file.as_path())
78                    .with_context(|| format!("Error loading car DB at {}", file.display()))?;
79                store.read_only(car.into())?;
80                debug!("Loaded car DB at {}", file.display());
81            } else if cleanup && filename.ends_with(TEMP_FOREST_CAR_FILE_EXTENSION) {
82                // Only delete files that appear to be incomplete car DB files
83                match std::fs::remove_file(&file) {
84                    Ok(_) => {
85                        info!("Deleted temp car DB at {}", file.display());
86                    }
87                    Err(e) => {
88                        warn!("Failed to delete temp car DB at {}: {e}", file.display());
89                    }
90                }
91            }
92        }
93    }
94
95    tracing::info!("Loaded {} CARs", store.len());
96
97    Ok(())
98}
99
100#[derive(
101    Default,
102    PartialEq,
103    Eq,
104    Debug,
105    Clone,
106    Copy,
107    strum::Display,
108    strum::EnumString,
109    Serialize,
110    Deserialize,
111)]
112#[strum(serialize_all = "lowercase")]
113#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
114pub enum ImportMode {
115    #[default]
116    /// Hard link the snapshot and fallback to `Copy` if not applicable
117    Auto,
118    /// Copies the snapshot to the database directory.
119    Copy,
120    /// Moves the snapshot to the database directory (or copies and deletes the original).
121    Move,
122    /// Creates a symbolic link to the snapshot in the database directory.
123    Symlink,
124    /// Creates a symbolic link to the snapshot in the database directory.
125    Hardlink,
126}
127
128/// This function validates and stores the CAR binary from `from_path`(either local path or URL) into the `{DB_ROOT}/car_db/`
129/// (automatically trans-code into `.forest.car.zst` format when needed), and returns its final file path and the heaviest tipset.
130pub async fn import_chain_as_forest_car(
131    from_path: &Path,
132    forest_car_db_dir: &Path,
133    import_mode: ImportMode,
134    rpc_endpoint: Url,
135    f3_root: &Path,
136    chain_config: &ChainConfig,
137    snapshot_progress_tracker: &SnapshotProgressTracker,
138) -> anyhow::Result<(PathBuf, Tipset)> {
139    info!("Importing chain from snapshot at: {}", from_path.display());
140
141    let stopwatch = time::Instant::now();
142
143    let forest_car_db_path = forest_car_db_dir.join(format!(
144        "{}{FOREST_CAR_FILE_EXTENSION}",
145        chrono::Utc::now().timestamp_millis()
146    ));
147
148    let move_or_copy = |mode: ImportMode| {
149        let forest_car_db_path = forest_car_db_path.clone();
150        async move {
151            let downloaded_car_temp_path = new_forest_car_temp_path_in(forest_car_db_dir)?;
152            if let Ok(url) = Url::parse(&from_path.display().to_string()) {
153                download_to(
154                    &url,
155                    &downloaded_car_temp_path,
156                    DownloadFileOption::Resumable,
157                    snapshot_progress_tracker.create_callback(),
158                )
159                .await?;
160
161                snapshot_progress_tracker.completed();
162            } else {
163                snapshot_progress_tracker.not_required();
164                if ForestCar::is_valid(&EitherMmapOrRandomAccessFile::open(from_path)?) {
165                    move_or_copy_file(from_path, &downloaded_car_temp_path, mode)?;
166                } else {
167                    // For a local snapshot, we transcode directly instead of copying & transcoding.
168                    transcode_into_forest_car(from_path, &downloaded_car_temp_path).await?;
169                    if mode == ImportMode::Move {
170                        std::fs::remove_file(from_path).context("Error removing original file")?;
171                    }
172                }
173            }
174
175            if ForestCar::is_valid(&EitherMmapOrRandomAccessFile::open(
176                &downloaded_car_temp_path,
177            )?) {
178                downloaded_car_temp_path.persist(&forest_car_db_path)?;
179            } else {
180                // Use another temp file to make sure all final `.forest.car.zst` files are complete and valid.
181                let forest_car_db_temp_path = new_forest_car_temp_path_in(forest_car_db_dir)?;
182                transcode_into_forest_car(&downloaded_car_temp_path, &forest_car_db_temp_path)
183                    .await?;
184                forest_car_db_temp_path.persist(&forest_car_db_path)?;
185            }
186            anyhow::Ok(())
187        }
188    };
189
190    match import_mode {
191        ImportMode::Auto => {
192            if Url::parse(&from_path.display().to_string()).is_ok() {
193                // Fallback to move if from_path is url
194                move_or_copy(ImportMode::Move).await?;
195            } else if ForestCar::is_valid(&EitherMmapOrRandomAccessFile::open(from_path)?) {
196                tracing::info!(
197                    "Hardlinking {} to {}",
198                    from_path.display(),
199                    forest_car_db_path.display()
200                );
201                if std::fs::hard_link(from_path, &forest_car_db_path).is_err() {
202                    tracing::warn!("Error creating hardlink, fallback to copy");
203                    move_or_copy(ImportMode::Copy).await?;
204                }
205            } else {
206                tracing::warn!(
207                    "Snapshot file is not a valid forest.car.zst file, fallback to copy"
208                );
209                move_or_copy(ImportMode::Copy).await?;
210            }
211        }
212        ImportMode::Copy | ImportMode::Move => {
213            move_or_copy(import_mode).await?;
214        }
215        ImportMode::Symlink => {
216            let from_path = std::path::absolute(from_path)?;
217            if ForestCar::is_valid(&EitherMmapOrRandomAccessFile::open(&from_path)?) {
218                tracing::info!(
219                    "Symlinking {} to {}",
220                    from_path.display(),
221                    forest_car_db_path.display()
222                );
223                std::os::unix::fs::symlink(from_path, &forest_car_db_path)
224                    .context("Error creating symlink")?;
225            } else {
226                bail!("Snapshot file must be a valid forest.car.zst file");
227            }
228        }
229        ImportMode::Hardlink => {
230            if ForestCar::is_valid(&EitherMmapOrRandomAccessFile::open(from_path)?) {
231                tracing::info!(
232                    "Hardlinking {} to {}",
233                    from_path.display(),
234                    forest_car_db_path.display()
235                );
236                std::fs::hard_link(from_path, &forest_car_db_path)
237                    .context("Error creating hardlink")?;
238            } else {
239                bail!("Snapshot file must be a valid forest.car.zst file");
240            }
241        }
242    };
243
244    let forest_car = ForestCar::try_from(forest_car_db_path.as_path())?;
245
246    if let Some(f3_cid) = forest_car.metadata().as_ref().and_then(|m| m.f3_data) {
247        let mut f3_data = forest_car
248            .get_reader(f3_cid)?
249            .with_context(|| format!("f3 data not found, cid: {f3_cid}"))?;
250        let mut temp_f3_snap = tempfile::Builder::new()
251            .suffix(".f3snap.bin")
252            .tempfile_in(forest_car_db_dir)?;
253        {
254            let f = temp_f3_snap.as_file_mut();
255            std::io::copy(&mut f3_data, f)?;
256            f.sync_all()?;
257        }
258        if let Err(e) = crate::f3::import_f3_snapshot(
259            chain_config,
260            rpc_endpoint.to_string(),
261            f3_root.display().to_string(),
262            temp_f3_snap.path().display().to_string(),
263        ) {
264            // Do not make it a hard error if anything is wrong with F3 snapshot
265            tracing::error!("Failed to import F3 snapshot: {e}");
266        }
267    }
268
269    let ts = forest_car.heaviest_tipset()?;
270    info!(
271        "Imported snapshot in: {}s, heaviest tipset epoch: {}, key: {}",
272        stopwatch.elapsed().as_secs(),
273        ts.epoch(),
274        ts.key()
275    );
276
277    Ok((forest_car_db_path, ts))
278}
279
280fn move_or_copy_file(from: &Path, to: &Path, import_mode: ImportMode) -> anyhow::Result<()> {
281    match import_mode {
282        ImportMode::Move => {
283            tracing::info!("Moving {} to {}", from.display(), to.display());
284            if fs::rename(from, to).is_ok() {
285                Ok(())
286            } else {
287                fs::copy(from, to).context("Error copying file")?;
288                fs::remove_file(from).context("Error removing original file")?;
289                Ok(())
290            }
291        }
292        ImportMode::Copy => {
293            tracing::info!("Copying {} to {}", from.display(), to.display());
294            fs::copy(from, to).map(|_| ()).context("Error copying file")
295        }
296        m => {
297            bail!("{m} must be handled elsewhere");
298        }
299    }
300}
301
302async fn transcode_into_forest_car(from: &Path, to: &Path) -> anyhow::Result<()> {
303    tracing::info!(
304        from = %from.display(),
305        to = %to.display(),
306        "transcoding into forest car"
307    );
308    let car_stream = CarStream::new_from_path(from).await?;
309    let roots = car_stream.header_v1.roots.clone();
310
311    let mut writer = tokio::io::BufWriter::new(tokio::fs::File::create(to).await?);
312    let frames = crate::db::car::forest::Encoder::compress_stream_default(
313        car_stream.map_err(anyhow::Error::from),
314    );
315    crate::db::car::forest::Encoder::write(&mut writer, roots, frames).await?;
316    writer.shutdown().await?;
317
318    Ok(())
319}
320
321async fn process_ts<DB>(
322    ts: &Tipset,
323    state_manager: &Arc<StateManager<DB>>,
324    delegated_messages: &mut Vec<(crate::message::SignedMessage, u64)>,
325) -> anyhow::Result<()>
326where
327    DB: fvm_ipld_blockstore::Blockstore + Send + Sync + 'static,
328{
329    let epoch = ts.epoch();
330    let tsk = ts.key().clone();
331
332    let state_output = state_manager
333        .compute_tipset_state(Arc::new(ts.clone()), NO_CALLBACK, VMTrace::NotTraced)
334        .await?;
335    for events_root in state_output.events_roots.iter().flatten() {
336        tracing::trace!("Indexing events root @{epoch}: {events_root}");
337
338        state_manager.chain_store().put_index(events_root, &tsk)?;
339    }
340
341    delegated_messages.append(
342        &mut state_manager
343            .chain_store()
344            .headers_delegated_messages(ts.block_headers().iter())?,
345    );
346    tracing::trace!("Indexing tipset @{}: {}", epoch, &tsk);
347    state_manager.chain_store().put_tipset_key(&tsk)?;
348
349    Ok(())
350}
351
352pub enum RangeSpec {
353    To(ChainEpoch),
354    NumTipsets(usize),
355}
356
357impl std::fmt::Display for RangeSpec {
358    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
359        match self {
360            RangeSpec::To(epoch) => write!(f, "To epoch:      {}", epoch),
361            RangeSpec::NumTipsets(n) => write!(f, "Tipsets:       {}", n),
362        }
363    }
364}
365
366/// To support the Event RPC API, a new column has been added to parity-db to handle the mapping:
367/// - Events root [`Cid`] -> [`TipsetKey`].
368///
369/// Similarly, to support the Ethereum RPC API, another column has been introduced to map:
370/// - [`struct@EthHash`] -> [`TipsetKey`],
371/// - [`struct@EthHash`] -> Delegated message [`Cid`].
372///
373/// This function traverses the chain store and populates these columns accordingly.
374pub async fn backfill_db<DB>(
375    state_manager: &Arc<StateManager<DB>>,
376    head_ts: &Tipset,
377    spec: RangeSpec,
378) -> anyhow::Result<()>
379where
380    DB: fvm_ipld_blockstore::Blockstore + Send + Sync + 'static,
381{
382    tracing::info!("Starting index backfill...");
383
384    let mut delegated_messages = vec![];
385
386    let mut num_backfills = 0;
387
388    match spec {
389        RangeSpec::To(to_epoch) => {
390            for ts in head_ts
391                .clone()
392                .chain(&state_manager.chain_store().blockstore())
393                .take_while(|ts| ts.epoch() >= to_epoch)
394            {
395                process_ts(&ts, state_manager, &mut delegated_messages).await?;
396                num_backfills += 1;
397            }
398        }
399        RangeSpec::NumTipsets(n_tipsets) => {
400            for ts in head_ts
401                .clone()
402                .chain(&state_manager.chain_store().blockstore())
403                .take(n_tipsets)
404            {
405                process_ts(&ts, state_manager, &mut delegated_messages).await?;
406                num_backfills += 1;
407            }
408        }
409    }
410
411    state_manager
412        .chain_store()
413        .process_signed_messages(&delegated_messages)?;
414
415    tracing::info!("Total successful backfills: {}", num_backfills);
416
417    Ok(())
418}
419
420#[cfg(test)]
421mod test {
422    use super::*;
423
424    #[tokio::test]
425    async fn import_snapshot_from_file_valid() {
426        for import_mode in [ImportMode::Auto, ImportMode::Copy, ImportMode::Move] {
427            import_snapshot_from_file("test-snapshots/chain4.car", import_mode)
428                .await
429                .unwrap();
430        }
431
432        // Linking is not supported for raw CAR files.
433        for import_mode in [ImportMode::Symlink, ImportMode::Hardlink] {
434            import_snapshot_from_file("test-snapshots/chain4.car", import_mode)
435                .await
436                .unwrap_err();
437        }
438    }
439
440    #[tokio::test]
441    async fn import_snapshot_from_compressed_file_valid() {
442        for import_mode in [ImportMode::Auto, ImportMode::Copy, ImportMode::Move] {
443            import_snapshot_from_file("test-snapshots/chain4.car.zst", import_mode)
444                .await
445                .unwrap();
446        }
447
448        // Linking is not supported for raw CAR files.
449        for import_mode in [ImportMode::Symlink, ImportMode::Hardlink] {
450            import_snapshot_from_file("test-snapshots/chain4.car", import_mode)
451                .await
452                .unwrap_err();
453        }
454    }
455
456    #[tokio::test]
457    async fn import_snapshot_from_forest_car_valid() {
458        for import_mode in [
459            ImportMode::Auto,
460            ImportMode::Copy,
461            ImportMode::Move,
462            ImportMode::Symlink,
463            ImportMode::Hardlink,
464        ] {
465            import_snapshot_from_file("test-snapshots/chain4.forest.car.zst", import_mode)
466                .await
467                .unwrap();
468        }
469    }
470
471    #[tokio::test]
472    async fn import_snapshot_from_file_invalid() {
473        for import_mode in &[
474            ImportMode::Auto,
475            ImportMode::Copy,
476            ImportMode::Move,
477            ImportMode::Symlink,
478            ImportMode::Hardlink,
479        ] {
480            import_snapshot_from_file("Cargo.toml", *import_mode)
481                .await
482                .unwrap_err();
483        }
484    }
485
486    #[tokio::test]
487    async fn import_snapshot_from_file_not_found() {
488        for import_mode in &[
489            ImportMode::Auto,
490            ImportMode::Copy,
491            ImportMode::Move,
492            ImportMode::Symlink,
493            ImportMode::Hardlink,
494        ] {
495            import_snapshot_from_file("dummy.car", *import_mode)
496                .await
497                .unwrap_err();
498        }
499    }
500
501    #[tokio::test]
502    async fn import_snapshot_from_url_not_found() {
503        for import_mode in &[
504            ImportMode::Auto,
505            ImportMode::Copy,
506            ImportMode::Move,
507            ImportMode::Symlink,
508            ImportMode::Hardlink,
509        ] {
510            import_snapshot_from_file("https://forest.chainsafe.io/dummy.car", *import_mode)
511                .await
512                .unwrap_err();
513        }
514    }
515
516    async fn import_snapshot_from_file(
517        file_path: &str,
518        import_mode: ImportMode,
519    ) -> anyhow::Result<()> {
520        // Prevent modifications on the original file, e.g., deletion via `ImportMode::Move`.
521        let temp_file = tempfile::Builder::new().tempfile()?;
522        fs::copy(Path::new(file_path), temp_file.path())?;
523        let file_path = temp_file.path();
524
525        let temp_db_dir = tempfile::Builder::new().tempdir()?;
526
527        let (path, ts) = import_chain_as_forest_car(
528            file_path,
529            temp_db_dir.path(),
530            import_mode,
531            "http://127.0.0.1:2345/rpc/v1".parse().unwrap(),
532            Path::new("test"),
533            &ChainConfig::devnet(),
534            &SnapshotProgressTracker::default(),
535        )
536        .await?;
537        match import_mode {
538            ImportMode::Symlink => {
539                assert_eq!(
540                    std::path::absolute(path.read_link()?)?,
541                    std::path::absolute(file_path)?
542                );
543            }
544            ImportMode::Move => {
545                assert!(!file_path.exists());
546                assert!(path.is_file());
547            }
548            _ => {
549                assert!(file_path.is_file());
550                assert!(path.is_file());
551            }
552        }
553        assert!(ts.epoch() > 0);
554        Ok(())
555    }
556}