diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/document/changes.rs | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/src/document/changes.rs b/src/document/changes.rs index 11d0228..5569995 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -390,6 +390,10 @@ impl Written { /// that kind, the element of that list, the operation replaced one at that /// place. (One naming an operation elsewhere marks an unrelated value gone, and /// the document then gives back another change under this one's hash.) + /// * Nothing Automerge's transactions never write: a deletion that replaces + /// nothing; a value put at a list's head without being inserted; a mark ended + /// but by the operation after the one that began it. (Automerge takes those, and + /// then can't give the change back: its save doesn't load.) /// /// A change that passes is among those the next are checked against only once /// it is [`Written::taken`]. @@ -399,7 +403,7 @@ impl Written { change: &Change, expanded: &ExpandedChange, ) -> Option<String> { - use automerge::legacy::{ElementId, Key, ObjectId, OpId}; + use automerge::legacy::{ElementId, Key, ObjectId, OpId, OpType}; self.checked = None; let hash = change.hash(); let again = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { @@ -475,6 +479,30 @@ impl Written { .flatten() .find(|made| written.reaches(doc, &mut history, made.change, *counter)) }; + // What Automerge's transactions never write. + match &op.action { + OpType::Delete if op.pred.is_empty() => { + return Some("it deletes, replacing nothing".to_owned()); + } + OpType::Put(_) | OpType::Make(_) + if !op.insert && matches!(op.key, Key::Seq(ElementId::Head)) => + { + return Some( + "it puts a value at a list's head without inserting it".to_owned(), + ); + } + OpType::MarkEnd(_) + if !(index > 0 + && matches!( + expanded.operations[index - 1].action, + OpType::MarkBegin(..) + ) + && expanded.operations[index - 1].obj == op.obj) => + { + return Some("it ends a mark but right after the one that began it".to_owned()); + } + _ => {} + } // The object written into: one, of the kind the key is for. let in_list = matches!(op.key, Key::Seq(_)); let fits = |kind: automerge::ObjType| { @@ -1208,6 +1236,52 @@ mod tests { ); } + /// What Automerge's transactions never write: a deletion replacing nothing, a value + /// put at a list's head without inserting, a mark ended but right after its begin. + #[test] + fn what_transactions_never_write_is_refused() { + use automerge::legacy::{ElementId, Key, OpType}; + let mut theirs = Document::new(); + let list = change(&mut theirs, |doc| { + doc.put(ROOT, "k", 1).unwrap(); + let list = doc + .put_object(ROOT, "list", automerge::ObjType::List) + .unwrap(); + doc.insert(&list, 0, "a").unwrap(); + list + }); + let mut ours = Document::new(); + ours.apply_changes(theirs.get_changes(&[])).unwrap(); + change(&mut theirs, |doc| doc.delete(ROOT, "k").unwrap()); + let delete = theirs.get_last_local_change().unwrap(); + change(&mut theirs, |doc| doc.insert(&list, 0, "b").unwrap()); + let insert = theirs.get_last_local_change().unwrap(); + for honest in [&delete, &insert] { + assert_eq!( + Written::default().check(&ours, honest, &honest.decode()), + None + ); + } + let nothing_replaced = crafted(&delete, |change| { + change.operations[0].pred = Vec::new().into(); + }); + let at_head = crafted(&insert, |change| { + change.operations[0].insert = false; + change.operations[0].key = Key::Seq(ElementId::Head); + }); + // In the list, where the insert was: a mark's end nothing began. + let mark_end = crafted(&insert, |change| { + change.operations[0].action = OpType::MarkEnd(false); + }); + for crafted in [nothing_replaced, at_head, mark_end] { + assert!( + Written::default() + .check(&ours, &crafted, &crafted.decode()) + .is_some() + ); + } + } + /// A fork of a change the document holds, checked before an honest change of the /// same batch and refused after (as forged): it doesn't stand in for the change the /// document holds, and the honest change passes. |
