|
| 1 | +// Copyright 2019-2026 ChainSafe Systems |
| 2 | +// SPDX-License-Identifier: Apache-2.0, MIT |
| 3 | + |
| 4 | +//! Scanning through millions of epochs to find the genesis is quite |
| 5 | +//! slow. We use a list of known blocks to short-circuit the search. |
| 6 | +//! The blocks are hash-chained together and known blocks are guaranteed |
| 7 | +//! to have a known genesis. |
| 8 | +
|
| 9 | +use crate::{ |
| 10 | + blocks::{CachingBlockHeader, Tipset}, |
| 11 | + networks::NetworkChain, |
| 12 | + shim::clock::ChainEpoch, |
| 13 | +}; |
| 14 | +use ahash::HashMap; |
| 15 | +use anyhow::Context as _; |
| 16 | +use cid::Cid; |
| 17 | +use fvm_ipld_blockstore::Blockstore; |
| 18 | +use itertools::Itertools; |
| 19 | +use serde::{Deserialize, Serialize}; |
| 20 | +use serde_with::{DisplayFromStr, serde_as}; |
| 21 | +use std::sync::{LazyLock, OnceLock}; |
| 22 | + |
| 23 | +#[serde_as] |
| 24 | +#[derive(Serialize, Deserialize)] |
| 25 | +pub struct KnownBlocks { |
| 26 | + #[serde_as(as = "HashMap<_, DisplayFromStr>")] |
| 27 | + pub calibnet: HashMap<ChainEpoch, Cid>, |
| 28 | + #[serde_as(as = "HashMap<_, DisplayFromStr>")] |
| 29 | + pub mainnet: HashMap<ChainEpoch, Cid>, |
| 30 | +} |
| 31 | + |
| 32 | +pub static KNOWN_BLOCKS: LazyLock<KnownBlocks> = LazyLock::new(|| { |
| 33 | + serde_yaml::from_str(include_str!("../../build/known_blocks.yaml")).expect("infallible") |
| 34 | +}); |
| 35 | + |
| 36 | +pub fn known_tipsets( |
| 37 | + bs: &impl Blockstore, |
| 38 | + network: &NetworkChain, |
| 39 | +) -> anyhow::Result<&'static Vec<Tipset>> { |
| 40 | + static CACHE_CALIBNET: OnceLock<Vec<Tipset>> = OnceLock::new(); |
| 41 | + static CACHE_MAINNET: OnceLock<Vec<Tipset>> = OnceLock::new(); |
| 42 | + let (cache, known_blocks) = match network { |
| 43 | + NetworkChain::Calibnet => (&CACHE_CALIBNET, &KNOWN_BLOCKS.calibnet), |
| 44 | + NetworkChain::Mainnet => (&CACHE_MAINNET, &KNOWN_BLOCKS.mainnet), |
| 45 | + _ => anyhow::bail!("unsupported network {network}"), |
| 46 | + }; |
| 47 | + if let Some(v) = cache.get() { |
| 48 | + Ok(v) |
| 49 | + } else { |
| 50 | + let tipsets = known_blocks_to_known_tipsets(bs, known_blocks)?; |
| 51 | + _ = cache.set(tipsets); |
| 52 | + cache.get().context("infallible") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn known_blocks_to_known_tipsets( |
| 57 | + bs: &impl Blockstore, |
| 58 | + blocks: &HashMap<ChainEpoch, Cid>, |
| 59 | +) -> anyhow::Result<Vec<Tipset>> { |
| 60 | + let mut tipsets: Vec<Tipset> = blocks |
| 61 | + .values() |
| 62 | + .map(|&b| block_cid_to_required_parent_tipset(bs, b)) |
| 63 | + .try_collect()?; |
| 64 | + tipsets.sort_by_key(|ts| ts.epoch()); |
| 65 | + Ok(tipsets) |
| 66 | +} |
| 67 | + |
| 68 | +fn block_cid_to_parent_tipset(bs: &impl Blockstore, block: Cid) -> anyhow::Result<Option<Tipset>> { |
| 69 | + if let Some(block) = CachingBlockHeader::load(bs, block)? { |
| 70 | + Tipset::load(bs, &block.parents) |
| 71 | + } else { |
| 72 | + Ok(None) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn block_cid_to_required_parent_tipset(bs: &impl Blockstore, block: Cid) -> anyhow::Result<Tipset> { |
| 77 | + block_cid_to_parent_tipset(bs, block)? |
| 78 | + .with_context(|| format!("failed to load parent tipset of block {block}")) |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_known_blocks() { |
| 87 | + assert!(!KNOWN_BLOCKS.calibnet.is_empty()); |
| 88 | + assert!(!KNOWN_BLOCKS.mainnet.is_empty()); |
| 89 | + } |
| 90 | +} |
0 commit comments