diff --git a/pkgs/id/src/hosted_export.rs b/pkgs/id/src/hosted_export.rs index d6eba4b07c..381e3150a5 100644 --- a/pkgs/id/src/hosted_export.rs +++ b/pkgs/id/src/hosted_export.rs @@ -33,8 +33,8 @@ //! The enumeration of a live per-user store (the slice-3 DO scaffold) //! and R2 blob fetching: `build_export` takes the object/blob sets as //! inputs, so the DO/R2 wiring is a follow-on that feeds it. Ingest -//! itself (§6.2 step 4) follows the platform's normal rules and is -//! likewise downstream of a verified import. +//! itself (§6.2 step 4) lives in [`crate::hosted_ingest`], downstream +//! of a verified import. use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey}; use serde::{Deserialize, Serialize}; diff --git a/pkgs/id/src/hosted_ingest.rs b/pkgs/id/src/hosted_ingest.rs new file mode 100644 index 0000000000..0927a2d72f --- /dev/null +++ b/pkgs/id/src/hosted_ingest.rs @@ -0,0 +1,567 @@ +//! Hosted import ingest (spec §6.2 step 4; tracker #2852 slice 4b, +//! #13158). +//! +//! Applies a verified hosted export at the importing (self-hosted) +//! node. This is the step downstream of [`crate::hosted_export::verify_import`]: +//! nothing here runs until verification has passed, and the +//! [`VerifiedImport`] it produced must be handed in — a token that +//! names a different export is refused before anything is written. +//! +//! Ingest is not a privileged write path. Each verified object goes +//! through the event store's normal ingest ([`EventStore::ingest`]), +//! the same verification, fork, and waiting rules any peer's event +//! faces (#2030: validity at ingest, counting at view), so a +//! hosted-origin object is indistinguishable from a self-hosted one +//! once stored. Each verified blob is placed into the node's blob +//! store under the hash of its bytes — the BLAKE3 hash a +//! `document.put` header cites — so citations resolve locally. +//! +//! Every entry is accounted for in the returned [`ImportReport`]: +//! applied, already present, or refused with the rule that refused +//! it. A refused entry never blocks the entries after it, and no +//! entry is silently dropped (#3810's scope requirement). +//! +//! Objects are offered to the store in the export's canonical +//! (hash) order, which is not chain order: an object whose +//! dependencies have not landed yet is held by the store's waiting +//! pool and released as the rest of the export arrives. An object +//! still waiting after the whole export has been offered is +//! reported refused under the waiting rule — the export does not +//! carry something it depends on. The store keeps its held row, so +//! a later import that carries the dependency releases it. + +use iroh_blobs::Hash as BlobHash; +use iroh_blobs::api::Store as BlobStore; + +use crate::chain; +use crate::event_store::{EventStore, Ingest, Refusal, StoreError}; +use crate::hosted_export::{ExportBlob, ExportObject, HostedExport, VerifiedImport}; + +/// What ingest did with one entry of the export. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum EntryOutcome { + /// The entry was not in the target and is stored now. + Applied, + /// The entry was already in the target; nothing changed. + AlreadyPresent, + /// The entry was refused. `rule` names the rule that refused it, + /// in the refusing layer's own terms. + Refused { + /// The rule that refused the entry. + rule: String, + }, +} + +/// One object's line in an [`ImportReport`], named by its export +/// hash (SHA-256 of its bytes, hex). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ObjectEntry { + /// The object's export hash, hex. + pub hash: String, + /// What ingest did with it. + pub outcome: EntryOutcome, +} + +/// One blob's line in an [`ImportReport`], named by its export +/// hash (SHA-256 of its bytes, hex). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct BlobEntry { + /// The blob's export hash, hex. + pub hash: String, + /// What ingest did with it. + pub outcome: EntryOutcome, +} + +/// The per-entry account of one import: every object and every blob +/// of the verified export, named and dispositioned, in the export's +/// canonical order. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ImportReport { + /// The binding the export belongs to. + pub binding_id: String, + /// One line per object, in export order. + pub objects: Vec, + /// One line per blob, in export order. + pub blobs: Vec, +} + +impl ImportReport { + /// How many entries (objects and blobs) were applied. + #[must_use] + pub fn applied(&self) -> usize { + self.count_where(EntryOutcome::Applied) + } + + /// How many entries were already present. + #[must_use] + pub fn already_present(&self) -> usize { + self.count_where(EntryOutcome::AlreadyPresent) + } + + /// How many entries were refused. + #[must_use] + pub fn refused(&self) -> usize { + self.objects + .iter() + .map(|entry| &entry.outcome) + .chain(self.blobs.iter().map(|entry| &entry.outcome)) + .filter(|outcome| matches!(outcome, EntryOutcome::Refused { .. })) + .count() + } + + /// True when nothing was refused: the whole export is in the + /// target now (applied by this import or already present). + #[must_use] + pub fn is_complete(&self) -> bool { + self.refused() == 0 + } + + fn count_where(&self, wanted: EntryOutcome) -> usize { + self.objects + .iter() + .map(|entry| &entry.outcome) + .chain(self.blobs.iter().map(|entry| &entry.outcome)) + .filter(|outcome| **outcome == wanted) + .count() + } +} + +/// Why an import did not run at all. A per-entry refusal is not an +/// error: it is a line in the [`ImportReport`]. +#[derive(Debug)] +pub enum ImportError { + /// The [`VerifiedImport`] handed in does not name this export: + /// it was minted by verifying something else. Nothing was + /// ingested. + VerificationMismatch, + /// The target event store failed (a redb or corruption error, + /// not a judgement on an entry). + Store(StoreError), +} + +impl std::fmt::Display for ImportError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::VerificationMismatch => { + write!(f, "the verified import does not name this export") + } + Self::Store(err) => write!(f, "event store error: {err}"), + } + } +} + +impl std::error::Error for ImportError {} + +impl From for ImportError { + fn from(err: StoreError) -> Self { + Self::Store(err) + } +} + +/// The rule a store [`Refusal`] names, in the store's own terms. +fn refusal_rule(refusal: &Refusal) -> String { + match refusal { + Refusal::Malformed(message) => format!("malformed: {message}"), + Refusal::Frozen => "frozen: the event is above its author's fork point".to_owned(), + Refusal::WaitingFull => { + "waiting: the author already holds as many waiting events as the caps allow".to_owned() + } + } +} + +/// One offer of one object to the store, before the waiting pool's +/// final disposition is known. +enum Step { + /// The store decided the object for good. + Done(EntryOutcome), + /// The store holds the object pending a dependency. + Waiting, +} + +fn offer(store: &mut EventStore, object: &ExportObject, now_ms: u64) -> Result { + match store.ingest(&object.bytes, now_ms)? { + // Accepted applies the event; Voided stores it without + // applying (it still counts as present); a Fork stores the + // headers as the proof. In all three the object is in the + // store now, under exactly the rules a peer's event faces. + Ingest::Accepted { .. } | Ingest::Voided { .. } | Ingest::Fork { .. } => { + Ok(Step::Done(EntryOutcome::Applied)) + } + Ingest::Duplicate { .. } => Ok(Step::Done(EntryOutcome::AlreadyPresent)), + Ingest::Refused(refusal) => Ok(Step::Done(EntryOutcome::Refused { + rule: refusal_rule(&refusal), + })), + Ingest::Waiting { .. } => Ok(Step::Waiting), + } +} + +/// The final disposition of an object the store held as waiting: +/// by the end of the import its dependencies have either landed +/// (the store holds it now) or they are not in the export at all. +fn settle_waiting( + store: &mut EventStore, + object: &ExportObject, + now_ms: u64, +) -> Result { + if let Ok(event) = chain::decode(&object.bytes) { + let author = *event.header.verifying_key.as_bytes(); + if let Some(stored) = store.event(&author, event.header.seq_num)? + && stored.id == event.id + { + return Ok(EntryOutcome::Applied); + } + } + match offer(store, object, now_ms)? { + Step::Done(outcome) => Ok(outcome), + Step::Waiting => Ok(EntryOutcome::Refused { + rule: "waiting: a dependency of this object is not present \ + and the export does not carry it" + .to_owned(), + }), + } +} + +/// Place one blob in the node's blob store, under the BLAKE3 hash +/// of its bytes — the hash a `document.put` header cites. +async fn place_blob(blobs: &BlobStore, blob: &ExportBlob) -> EntryOutcome { + let hash = BlobHash::new(&blob.bytes); + match blobs.has(hash).await { + Ok(true) => EntryOutcome::AlreadyPresent, + Ok(false) => match blobs.add_bytes(blob.bytes.clone()).await { + Ok(added) if added.hash == hash => EntryOutcome::Applied, + Ok(added) => EntryOutcome::Refused { + rule: format!( + "blob store stored the blob under {} instead of {}", + added.hash, hash + ), + }, + Err(err) => EntryOutcome::Refused { + rule: format!("blob store error: {err}"), + }, + }, + Err(err) => EntryOutcome::Refused { + rule: format!("blob store error: {err}"), + }, + } +} + +/// Ingest a verified export into the target node (§6.2 step 4). +/// +/// `verified` is the token [`crate::hosted_export::verify_import`] +/// minted for this export; it must name the export's binding, key, +/// and entry counts. `now_ms` is the importing node's clock, handed +/// to the event store like any other ingest's. +/// +/// # Errors +/// +/// Returns [`ImportError::VerificationMismatch`] — ingesting +/// nothing — when the token does not name this export, and +/// [`ImportError::Store`] when the event store itself fails. A +/// refusal of an individual entry is reported in the returned +/// [`ImportReport`], never as an error. +pub async fn ingest_import( + store: &mut EventStore, + blobs: &BlobStore, + export: &HostedExport, + verified: &VerifiedImport, + now_ms: u64, +) -> Result { + if verified.binding_id != export.manifest.binding_id + || verified.iroh_id != export.manifest.iroh_id + || verified.object_count != export.objects.len() as u64 + || verified.blob_count != export.blobs.len() as u64 + { + return Err(ImportError::VerificationMismatch); + } + + let mut objects = Vec::with_capacity(export.objects.len()); + let mut waiting: Vec = Vec::new(); + for object in &export.objects { + let outcome = match offer(store, object, now_ms)? { + Step::Done(outcome) => outcome, + Step::Waiting => { + waiting.push(objects.len()); + EntryOutcome::AlreadyPresent // placeholder, settled below + } + }; + objects.push(ObjectEntry { + hash: object.hash.clone(), + outcome, + }); + } + for index in waiting { + objects[index].outcome = settle_waiting(store, &export.objects[index], now_ms)?; + } + + let mut blob_entries = Vec::with_capacity(export.blobs.len()); + for blob in &export.blobs { + blob_entries.push(BlobEntry { + hash: blob.hash.clone(), + outcome: place_blob(blobs, blob).await, + }); + } + + Ok(ImportReport { + binding_id: export.manifest.binding_id.clone(), + objects, + blobs: blob_entries, + }) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +mod tests { + use super::*; + + use ed25519_dalek::SigningKey as DalekKey; + use iroh_blobs::store::mem::MemStore; + use p2panda_core::SigningKey; + + use crate::chain::{self, ChainExt, Event}; + use crate::hosted_binding::HostedBindingService; + use crate::hosted_export::{build_export, verify_import}; + use id_core::waiting::WaitingLimits; + use id_store::documents::{DOCUMENT_KIND, DocumentRef}; + + const SUB: &str = "google-subject-1"; + const CREATED: &str = "2026-11-13T00:00:00Z"; + const SEED: u8 = 21; + + fn dalek_key(seed: u8) -> DalekKey { + DalekKey::from_bytes(&[seed; 32]) + } + + fn p2p_key(seed: u8) -> SigningKey { + SigningKey::from_bytes(&[seed; 32]) + } + + fn ext(kind: &str) -> ChainExt { + ChainExt { + kind: kind.to_owned(), + group: None, + after: Vec::new(), + tag: None, + document: None, + removes: None, + note: None, + link: None, + checkpoint: None, + rotation: None, + chat: None, + anchor: None, + } + } + + fn open_store() -> (tempfile::TempDir, EventStore) { + let dir = tempfile::tempdir().unwrap(); + let store = EventStore::open(dir.path(), WaitingLimits::default(), 0).unwrap(); + (dir, store) + } + + fn blob_store() -> BlobStore { + let store: BlobStore = MemStore::new().into(); + store + } + + /// A three-event chain by the binding's root key: two content + /// events and a `document.put` citing one blob. Returns the + /// events and the blob bytes. + fn source_chain() -> (Vec, Vec) { + let key = p2p_key(SEED); + let content = b"the document the export carries".to_vec(); + let first = chain::sign(&key, 0, None, None, ext("message")).unwrap(); + let second = chain::sign(&key, 1, Some(first.id), None, ext("message")).unwrap(); + let mut doc_ext = ext(DOCUMENT_KIND); + doc_ext.document = Some(DocumentRef::for_bytes("text/plain", &content).unwrap()); + let third = chain::sign(&key, 2, Some(second.id), None, doc_ext).unwrap(); + (vec![first, second, third], content) + } + + /// Build a verified export over `events` from a fresh binding, + /// as the hosted service would produce it. When `blob_bytes` is + /// given, the export carries that one blob and the chain's + /// `document.put` event cites it. + fn make_export(events: &[Event], blob_bytes: Option<&[u8]>) -> HostedExport { + let mut service = HostedBindingService::with_seed("hosted.example", [9u8; 32]); + service.signup_account(SUB, None, "free").unwrap(); + let binding = service + .complete_browser_ceremony(SUB, &dalek_key(SEED), None) + .unwrap(); + let blob = blob_bytes.map(|bytes| ExportBlob::new(bytes.to_vec())); + let objects: Vec = events + .iter() + .map(|event| { + let refs = match (&blob, event.header.extensions.kind.as_str()) { + (Some(blob), DOCUMENT_KIND) => vec![blob.hash.clone()], + _ => Vec::new(), + }; + ExportObject::new(&dalek_key(SEED), event.bytes.clone(), refs) + }) + .collect(); + build_export( + &service, + &binding, + Vec::new(), + objects, + blob.into_iter().collect(), + Some(&dalek_key(SEED)), + "export-1", + CREATED, + ) + } + + #[tokio::test] + async fn round_trip_lands_every_object_and_blob() { + let (events, content) = source_chain(); + // The source node holds the chain first: export is what a + // store's contents become. + let (_src_dir, mut source) = open_store(); + for event in &events { + assert!(matches!( + source.ingest(&event.bytes, 0).unwrap(), + Ingest::Accepted { .. } + )); + } + let export = make_export(&events, Some(&content)); + let verified = verify_import(&export, false).unwrap(); + + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let report = ingest_import(&mut target, &blobs, &export, &verified, 0) + .await + .unwrap(); + assert!(report.is_complete(), "report: {report:?}"); + assert_eq!(report.applied(), 4); // 3 objects + 1 blob + assert_eq!(report.objects.len(), 3); + assert_eq!(report.blobs.len(), 1); + + // The target's object set is the export's set: every object + // is stored, byte-identical, at its author's sequence. + let author = *p2p_key(SEED).verifying_key().as_bytes(); + for event in &events { + let stored = target + .event(&author, event.header.seq_num) + .unwrap() + .expect("ingested event is stored"); + assert_eq!(stored.id, event.id); + assert_eq!(stored.bytes, event.bytes); + } + // The blob is retrievable by the hash the document.put + // header cites, so the citation resolves locally. + let cited = BlobHash::new(&content); + let fetched = blobs.get_bytes(cited).await.unwrap(); + assert_eq!(&fetched[..], &content[..]); + } + + #[tokio::test] + async fn reingest_reports_everything_already_present() { + let (events, content) = source_chain(); + let export = make_export(&events, Some(&content)); + let verified = verify_import(&export, false).unwrap(); + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let first = ingest_import(&mut target, &blobs, &export, &verified, 0) + .await + .unwrap(); + assert!(first.is_complete()); + let second = ingest_import(&mut target, &blobs, &export, &verified, 0) + .await + .unwrap(); + assert!(second.is_complete(), "report: {second:?}"); + assert_eq!(second.applied(), 0); + assert_eq!(second.already_present(), 4); + } + + #[tokio::test] + async fn an_object_that_is_not_an_event_is_refused_and_named() { + // A wrapper-valid object whose bytes are not an event passes + // §6.2 verification (the export layer authenticates bytes, + // not eventhood) and is refused by the store's normal rules. + let mut service = HostedBindingService::with_seed("hosted.example", [9u8; 32]); + service.signup_account(SUB, None, "free").unwrap(); + let binding = service + .complete_browser_ceremony(SUB, &dalek_key(SEED), None) + .unwrap(); + let (events, content) = source_chain(); + let good = ExportObject::new(&dalek_key(SEED), events[0].bytes.clone(), Vec::new()); + let middle = ExportObject::new(&dalek_key(SEED), events[1].bytes.clone(), Vec::new()); + let bogus = ExportObject::new(&dalek_key(SEED), b"not an event".to_vec(), Vec::new()); + let bogus_hash = bogus.hash.clone(); + let blob = ExportBlob::new(content.clone()); + let citing = ExportObject::new( + &dalek_key(SEED), + events[2].bytes.clone(), + vec![blob.hash.clone()], + ); + let export = build_export( + &service, + &binding, + Vec::new(), + vec![good, middle, bogus, citing], + vec![blob], + Some(&dalek_key(SEED)), + "export-1", + CREATED, + ); + let verified = verify_import(&export, false).unwrap(); + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let report = ingest_import(&mut target, &blobs, &export, &verified, 0) + .await + .unwrap(); + assert_eq!(report.refused(), 1, "report: {report:?}"); + let refused: Vec<&ObjectEntry> = report + .objects + .iter() + .filter(|entry| matches!(entry.outcome, EntryOutcome::Refused { .. })) + .collect(); + assert_eq!(refused.len(), 1); + assert_eq!(refused[0].hash, bogus_hash); + match &refused[0].outcome { + EntryOutcome::Refused { rule } => { + assert!(rule.starts_with("malformed"), "rule: {rule}"); + } + other => panic!("expected a refusal, got {other:?}"), + } + // The refusal did not block the other entries. + assert_eq!(report.applied(), 4); // 3 objects + 1 blob + } + + #[tokio::test] + async fn an_object_missing_its_predecessor_is_refused_as_waiting() { + let (events, content) = source_chain(); + // Export only seq 1: its backlink names an event the export + // does not carry. + let export = make_export(&events[1..2], None); + let verified = verify_import(&export, false).unwrap(); + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let report = ingest_import(&mut target, &blobs, &export, &verified, 0) + .await + .unwrap(); + assert_eq!(report.objects.len(), 1); + match &report.objects[0].outcome { + EntryOutcome::Refused { rule } => { + assert!(rule.starts_with("waiting"), "rule: {rule}"); + } + other => panic!("expected a waiting refusal, got {other:?}"), + } + } + + #[tokio::test] + async fn a_token_for_another_export_is_refused_before_any_write() { + let (events, content) = source_chain(); + let export = make_export(&events, Some(&content)); + let mut wrong = verify_import(&export, false).unwrap(); + wrong.binding_id = "another-binding".to_owned(); + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let err = ingest_import(&mut target, &blobs, &export, &wrong, 0) + .await + .unwrap_err(); + assert!(matches!(err, ImportError::VerificationMismatch)); + // Nothing landed. + let author = *p2p_key(SEED).verifying_key().as_bytes(); + assert!(target.event(&author, 0).unwrap().is_none()); + } +} diff --git a/pkgs/id/src/lib.rs b/pkgs/id/src/lib.rs index 42afc8d98f..dce38c2b41 100644 --- a/pkgs/id/src/lib.rs +++ b/pkgs/id/src/lib.rs @@ -254,6 +254,7 @@ pub mod grants; pub mod groups; pub mod hosted_binding; pub mod hosted_export; +pub mod hosted_ingest; pub use id_events::idset; pub mod input_framing; pub use id_directory::invite;