pub mod store;
mod weight;
use crate::blocks::Tipset;
use crate::cid_collections::CidHashSet;
use crate::db::car::forest;
use crate::ipld::stream_chain;
use crate::utils::io::{AsyncWriterWithChecksum, Checksum};
use crate::utils::stream::par_buffer;
use anyhow::Context as _;
use digest::Digest;
use fvm_ipld_blockstore::Blockstore;
use std::sync::Arc;
use tokio::io::{AsyncWrite, AsyncWriteExt, BufWriter};
pub use self::{store::*, weight::*};
pub async fn export<D: Digest>(
db: Arc<impl Blockstore + Send + Sync + 'static>,
tipset: &Tipset,
lookup_depth: ChainEpochDelta,
writer: impl AsyncWrite + Unpin,
seen: CidHashSet,
skip_checksum: bool,
) -> anyhow::Result<Option<digest::Output<D>>, Error> {
let stateroot_lookup_limit = tipset.epoch() - lookup_depth;
let roots = tipset.key().to_cids();
let mut writer = AsyncWriterWithChecksum::<D, _>::new(BufWriter::new(writer), !skip_checksum);
let blocks = par_buffer(
1024,
stream_chain(
Arc::clone(&db),
tipset.clone().chain_owned(Arc::clone(&db)),
stateroot_lookup_limit,
)
.with_seen(seen),
);
let frames = forest::Encoder::compress_stream_default(blocks);
forest::Encoder::write(&mut writer, roots, frames).await?;
writer.flush().await.context("failed to flush")?;
let digest = writer.finalize().map_err(|e| Error::Other(e.to_string()))?;
Ok(digest)
}