Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124//! Bounded public repository CAR exports and proof extraction.use super::{ RepositoryService, car::{car_writer, map_block_error, repository_blocks_car}, commit::map_repo_error,};use crate::{ database::RepoBlockStore, error::{Error, Result}, syntax,};use atrium_api::types::DataModel as AtriumDataModel;use atrium_repo::blockstore::{AsyncBlockStoreRead as _, AsyncBlockStoreWrite as _, SHA2_256};use cid::Cid;use std::collections::HashSet;pub(super) const MAX_BLOCKS_PER_REQUEST: usize = 1_000;
impl RepositoryService { /// Export the current signed repository as a bounded CAR v1 archive. /// /// # Errors /// /// Returns `RepoNotFound` for an unhosted or not-yet-initialized repository, /// fails closed if stored history or the signed graph is invalid, and /// bounds both full and incremental archives. pub async fn export_repo(&self, did: &str, since: Option<&str>) -> Result<Vec<u8>> { let head = self.sync_head(did)?; let mut blocks = RepoBlockStore::new(self.database.clone()); let mut repo = self.open_repo(&mut blocks, head.root, &head.rev).await?; if let Some(since) = since { syntax::tid(since)?; drop(repo); let changed = self.database.repo_blocks_after_revision(did, since)?; return repository_blocks_car(changed, head.root).await; } let mut bytes = Vec::new(); let mut car = car_writer(&mut bytes, [head.root]).await?; repo.export_into(&mut car).await.map_err(map_repo_error)?; drop(car); Ok(bytes) }
/// Export the signed MST proof and value blocks for one current record. /// /// # Errors /// /// Returns the canonical repository or record not-found error, or fails /// closed if the stored graph or signature is invalid. pub async fn export_record(&self, did: &str, collection: &str, rkey: &str) -> Result<Vec<u8>> { syntax::collection(collection)?; syntax::record_key(rkey)?; let head = self.sync_head(did)?; let mut blocks = RepoBlockStore::new(self.database.clone()); let mut repo = self.open_repo(&mut blocks, head.root, &head.rev).await?; let path = format!("{collection}/{rkey}"); if repo .get_raw::<AtriumDataModel>(&path) .await .map_err(map_repo_error)? .is_none() { return Err(Error::xrpc("RecordNotFound", "record does not exist")); } let mut bytes = Vec::new(); let mut car = car_writer(&mut bytes, [head.root]).await?; repo.extract_raw_into(&path, &mut car) .await .map_err(map_repo_error)?; drop(car); Ok(bytes) }
/// Export selected blocks from the current signed repository graph. /// /// # Errors /// /// Rejects empty or excessive requests, CIDs outside the repository's /// current reachable graph, and invalid stored repository state. pub async fn export_blocks(&self, did: &str, requested: &[Cid]) -> Result<Vec<u8>> { if requested.is_empty() { return Err(Error::InvalidRequest( "at least one block CID is required".to_owned(), )); } if requested.len() > MAX_BLOCKS_PER_REQUEST { return Err(Error::InvalidRequest(format!( "at most {MAX_BLOCKS_PER_REQUEST} block CIDs may be requested" ))); } let head = self.sync_head(did)?; let mut blocks = RepoBlockStore::new(self.database.clone()); let mut repo = self.open_repo(&mut blocks, head.root, &head.rev).await?; let reachable = repo .export() .await .map_err(map_repo_error)? .collect::<HashSet<_>>(); drop(repo); if let Some(cid) = requested.iter().find(|cid| !reachable.contains(cid)) { return Err(Error::xrpc( "BlockNotFound", format!("block {cid} is not in the current repository"), )); }
let mut bytes = Vec::new(); let mut car = car_writer(&mut bytes, []).await?; for cid in requested { let block = blocks.read_block(*cid).await.map_err(map_block_error)?; let written = car .write_block(cid.codec(), SHA2_256, &block) .await .map_err(map_block_error)?; if written != *cid { return Err(Error::Internal(format!( "ATrium CAR writer changed repository block CID {cid}" ))); } } drop(car); Ok(bytes) }}