diff --git a/pkgs/id/crates/id-sync/src/federation/slices/b07_conflict_report.rs b/pkgs/id/crates/id-sync/src/federation/slices/b07_conflict_report.rs index e36e913d32..967819b9b8 100644 --- a/pkgs/id/crates/id-sync/src/federation/slices/b07_conflict_report.rs +++ b/pkgs/id/crates/id-sync/src/federation/slices/b07_conflict_report.rs @@ -17,17 +17,19 @@ //! its canonical bytes) and appends `ReportCreated` to the audit log //! in the same step. An object has more than one head exactly when //! it has an open report, and that report names exactly its heads. -//! 2. **No silent resolution.** A version that joins two or more of a -//! conflicted object's heads is a resolution. It is refused unless -//! its signed header names the conflict it resolves: the -//! [`ConflictKey`] of exactly the heads it joins. A header that names -//! any other key, or names one without joining heads, is refused too. -//! The key depends only on the conflicting versions, never on when or -//! from where a replica saw them, so one signed resolution means the -//! same thing on every replica that holds the same versions, while -//! each replica's report stays its own durable record. Because two -//! heads always come with an open report, a resolution is always a -//! new signed event that comes after a report covering what it joins. +//! 2. **No silent resolution.** A version with two or more parents is a +//! merge. It is refused unless its signed header names the conflict +//! it resolves, the [`ConflictKey`] of exactly its parents, and its +//! parents are pairwise concurrent (none descends from another). A +//! version with fewer than two parents must name no conflict. These +//! checks read only the signed version and the fixed ancestry of its +//! parents, never the store's current heads, so every replica that +//! holds the parents makes the same decision whatever order versions +//! arrived in; each replica's report stays its own durable record. +//! Report first still holds: when the later of two concurrent +//! versions is applied, each of their branches leaves a head, so the +//! store has already reported the divergence before any merge of +//! them is recorded. //! 3. **Reports are immutable.** When a conflicted object's heads //! change but stay plural, a new report supersedes the old one, //! whose status becomes `Superseded`; when they collapse to one, the @@ -404,20 +406,21 @@ pub enum IngestError { MalformedVersion, /// A parent belongs to a different object. ForeignParent(EventId), - /// The version joins heads of a conflicted object without naming - /// the conflict it resolves. + /// The version merges two or more parents without naming the + /// conflict it resolves. ResolutionRequired { - /// The object's open report. - open: ReportId, - /// The key the version would have to name. + /// The key the version would have to name: that of its parents. key: ConflictKey, }, /// The version names a conflict it does not resolve: not the key of - /// exactly the heads it joins, or named without joining heads. + /// exactly its parents, or named with fewer than two parents. NotAResolution { /// The key the version named. named: ConflictKey, }, + /// A merge names a parent that descends from another of its + /// parents, so it resolves nothing between them. + RedundantParents, } /// The key of a set of conflicting versions: SHA-256 of @@ -587,6 +590,7 @@ impl ConflictStore { body_len: header.body.len() as u64, }; self.check_parents(&version)?; + Self::check_merge(&version)?; if version .parents .iter() @@ -642,22 +646,9 @@ impl ConflictStore { .get(&version.object_id) .cloned() .unwrap_or_default(); - let joined: Vec = version - .parents - .iter() - .filter(|p| state.heads.contains(*p)) - .copied() - .collect(); - match (state.open, version.resolves) { - (Some(_), Some(named)) if joined.len() >= 2 && named == conflict_key(&joined) => {} - (Some(open), None) if joined.len() >= 2 => { - return Err(IngestError::ResolutionRequired { - open, - key: conflict_key(&joined), - }); - } - (_, Some(named)) => return Err(IngestError::NotAResolution { named }), - _ => {} + Self::check_merge(&version)?; + if version.parents.len() >= 2 && !self.pairwise_concurrent(&version.parents) { + return Err(IngestError::RedundantParents); } let object_id = version.object_id; @@ -741,6 +732,28 @@ impl ConflictStore { report_id } + /// The checks on a version's own signed header: a merge names the + /// key of its parents, anything else names none. + fn check_merge(version: &VersionRef) -> Result<(), IngestError> { + let key = conflict_key(&version.parents); + match (version.parents.len() >= 2, version.resolves) { + (true, Some(named)) if named == key => Ok(()), + (true, None) => Err(IngestError::ResolutionRequired { key }), + (_, Some(named)) => Err(IngestError::NotAResolution { named }), + (false, None) => Ok(()), + } + } + + /// Whether no parent descends from another. + fn pairwise_concurrent(&self, parents: &[EventId]) -> bool { + parents.iter().all(|parent| { + let ancestry = self.ancestry(*parent); + parents + .iter() + .all(|other| other == parent || !ancestry.contains(other)) + }) + } + /// Every stored version `event_id` descends from, itself included. fn ancestry(&self, event_id: EventId) -> BTreeSet { let mut seen = BTreeSet::new(); diff --git a/pkgs/id/crates/id-sync/tests/federation_b07.rs b/pkgs/id/crates/id-sync/tests/federation_b07.rs index c20e7a6824..2e2dc9d1ac 100644 --- a/pkgs/id/crates/id-sync/tests/federation_b07.rs +++ b/pkgs/id/crates/id-sync/tests/federation_b07.rs @@ -212,7 +212,7 @@ fn step_strategy() -> impl Strategy { ( 0_u8..3, 0..SIGNERS.len(), - 0_u8..5, + 0_u8..6, 0_u8..4, 0..5_usize, any::(), @@ -256,16 +256,23 @@ fn run( .copied() .into_iter() .collect(), - _ => heads.iter().rev().take(2).copied().collect(), + 4 => heads.iter().rev().take(2).copied().collect(), + // A version and a head: concurrent or not, by chance. + _ => all + .get(usize::from(step.body) % all.len().max(1)) + .copied() + .into_iter() + .chain(heads.first().copied()) + .collect(), }; - let open = store.open_report(&object_id); - let joined: Vec = parents - .iter() - .filter(|p| heads.contains(p)) - .copied() + let parents: Vec = parents + .into_iter() + .collect::>() + .into_iter() .collect(); + let open = store.open_report(&object_id); let resolves = match step.resolves { - 1 => Some(key_of(&joined)), + 1 => Some(key_of(&parents)), 2 => Some(key_of(&heads)), 3 => Some([0x5A; 32]), _ => None, @@ -275,14 +282,17 @@ fn run( // Predict. let conflicted = heads.len() > 1; - let expect_err = match (open, resolves) { - (Some(_), Some(r)) if joined.len() >= 2 && r == key_of(&joined) => None, + let merge_key = key_of(&parents); + let concurrent = parents.iter().all(|p| { + let ancestry = reference.ancestry(&object_id, *p); + parents.iter().all(|q| q == p || !ancestry.contains(q)) + }); + let expect_err = match (parents.len() >= 2, resolves) { + (true, Some(r)) if r == merge_key && !concurrent => Some(IngestError::RedundantParents), + (true, Some(r)) if r == merge_key => None, + (true, None) => Some(IngestError::ResolutionRequired { key: merge_key }), (_, Some(r)) => Some(IngestError::NotAResolution { named: r }), - (Some(o), None) if joined.len() >= 2 => Some(IngestError::ResolutionRequired { - open: o, - key: key_of(&joined), - }), - _ => None, + (false, None) => None, }; assert_eq!(conflicted, open.is_some(), "open report iff diverged"); @@ -409,38 +419,27 @@ fn check_audit_order(store: &ConflictStore) { created.insert(*report_id); } AuditEntry::Resolved { report_id, .. } => assert!(created.contains(report_id)), - AuditEntry::Recorded { event_id, .. } => { + AuditEntry::Recorded { + object_id, + event_id, + } => { let version = store.version(event_id).unwrap(); if let Some(named) = version.resolves { - // The next entry closes a report that was created - // earlier and that holds exactly what was resolved. - let closed = match audit.get(i + 1).copied() { - Some( - AuditEntry::Resolved { report_id, .. } - | AuditEntry::ReportCreated { - supersedes: Some(report_id), - .. - }, - ) => report_id, - other => panic!("resolution followed by {other:?}"), - }; - assert!(created.contains(&closed), "resolution before its report"); - let covered: BTreeSet = store - .report(&closed) - .unwrap() - .report - .versions - .iter() - .map(|v| v.event_id) - .collect(); - let joined: Vec = version - .parents - .iter() - .filter(|p| covered.contains(*p)) - .copied() - .collect(); - assert!(joined.len() >= 2); - assert_eq!(key_of(&joined), named); + // A merge names exactly its parents, comes after a + // report on its object, and changes the report. + assert!(version.parents.len() >= 2); + assert_eq!(key_of(&version.parents), named); + assert!( + audit[..i].iter().any(|e| matches!( + e, + AuditEntry::ReportCreated { object_id: o, .. } if o == object_id + )), + "merge before any report on its object" + ); + assert!(matches!( + audit.get(i + 1), + Some(AuditEntry::Resolved { .. } | AuditEntry::ReportCreated { .. }) + )); } } AuditEntry::Held { .. } | AuditEntry::Refused { .. } => {} @@ -516,7 +515,7 @@ fn divergence_reports_versions_signers_sources_and_disputed_data() { let silent = envelope(2, event(4), doc, None, &[event(2), event(3)], b"merged"); assert_eq!( store.ingest(IngressPath::Remote, &silent, "peer-c", NOW), - Err(IngestError::ResolutionRequired { open: id, key }) + Err(IngestError::ResolutionRequired { key }) ); let wrong = envelope( 2, @@ -532,6 +531,20 @@ fn divergence_reports_versions_signers_sources_and_disputed_data() { ); assert_eq!(store.open_report(&doc), Some(id)); + // A merge of a version and its own ancestor resolves nothing. + let redundant = envelope( + 2, + event(7), + doc, + Some(key_of(&[event(1), event(2)])), + &[event(1), event(2)], + b"merged", + ); + assert_eq!( + store.ingest(IngressPath::Remote, &redundant, "peer-c", NOW), + Err(IngestError::RedundantParents) + ); + let resolution = envelope( 2, event(6),