diff options
Diffstat (limited to 'src/document/changes.rs')
| -rw-r--r-- | src/document/changes.rs | 102 |
1 files changed, 99 insertions, 3 deletions
diff --git a/src/document/changes.rs b/src/document/changes.rs index 426c5d2..a40f4e4 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -314,6 +314,8 @@ struct Made { insert: bool, /// The object it made, if it made one. makes: Option<automerge::ObjType>, + /// It put a value or made an object: what a transaction names as replaced. + holds: bool, } impl Made { @@ -327,6 +329,10 @@ impl Made { automerge::legacy::OpType::Make(kind) => Some(*kind), _ => None, }, + holds: matches!( + op.action, + automerge::legacy::OpType::Put(_) | automerge::legacy::OpType::Make(_) + ), } } } @@ -388,8 +394,10 @@ impl Written { /// * Every operation it names was made in its history, or before in the change /// itself, and is what it is named as: the object written into an object of /// 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.) + /// place that put a value or made an object. (One naming an operation elsewhere + /// marks an unrelated value gone, and the document then gives back another + /// change under this one's hash; one naming a deletion or an increment does the + /// same, or panics as it is given back.) /// * Every operation is one Automerge's transactions write ([`unwritten`] says /// which), and a mark ends at its begin or at an element after the begin's in /// the text. (Automerge takes others, and then can't give the change back, or @@ -571,7 +579,7 @@ impl Written { let at_its_place = there.contains(&exid(id)) || (element == Some(id)) || made(self, id).is_some_and(|made| { - !made.insert && made.obj == op.obj && made.key == op.key + made.holds && !made.insert && made.obj == op.obj && made.key == op.key }); if !at_its_place { let OpId(counter, by) = id; @@ -1546,6 +1554,94 @@ mod tests { } } + /// What an operation replaces put a value or made an object, as a transaction names + /// it: a counter incremented and put over names its put alone, a key put again + /// after its deletion names nothing. One naming a deletion Automerge takes, and + /// then gives back another change under its hash, or panics giving it back. + #[test] + fn only_what_holds_a_value_is_replaced() { + use crate::document::Transaction; + use automerge::legacy::{ElementId, Key, OpId}; + use automerge::{ObjType, ScalarValue}; + let mut theirs = Document::new(); + let list = change(&mut theirs, |doc| { + doc.put(ROOT, "k", 1).unwrap(); + doc.put(ROOT, "count", ScalarValue::counter(0)).unwrap(); + doc.put(ROOT, "other", ScalarValue::counter(0)).unwrap(); + let list = doc.put_object(ROOT, "list", ObjType::List).unwrap(); + doc.insert(&list, 0, "a").unwrap(); + doc.insert(&list, 1, "b").unwrap(); + list + }); + let first = theirs.get_last_local_change().unwrap(); + change(&mut theirs, |doc| { + doc.delete(ROOT, "k").unwrap(); + doc.delete(ROOT, "count").unwrap(); + doc.delete(&list, 0).unwrap(); + }); + let deleted = theirs.get_last_local_change().unwrap(); + // The first change's operations: 4 the list's "a", 5 its "b"; the second's: 0 + // the deletion of "k", 1 of "count", 2 of the list's "a". + let actor = first.actor_id().clone(); + let made = + |change: &Change, index: u64| OpId(change.start_op().get() + index, actor.clone()); + let mut ours = Document::new(); + ours.apply_changes([first.clone(), deleted.clone()]) + .unwrap(); + fn on(doc: &Document, write: impl FnOnce(&mut Transaction<'_>)) -> Change { + let mut fork = doc.fork(); + change(&mut fork, write); + fork.get_last_local_change().unwrap() + } + let put_again = on(&ours, |doc| doc.put(ROOT, "k", 2).unwrap()); + let put_over = on(&ours, |doc| doc.put(&list, 0, "B").unwrap()); + let delete = on(&ours, |doc| doc.delete(&list, 0).unwrap()); + let increment = on(&ours, |doc| doc.increment(ROOT, "other", 1).unwrap()); + let counted_over = on(&ours, |doc| { + doc.increment(ROOT, "other", 1).unwrap(); + doc.increment(ROOT, "other", 1).unwrap(); + doc.put(ROOT, "other", 5).unwrap(); + }); + for honest in [&put_again, &put_over, &delete, &increment, &counted_over] { + assert_eq!( + Written::default().check(&ours, honest, &honest.decode()), + None, + "{:?}", + honest.decode().operations + ); + } + let at_a = Key::Seq(ElementId::Id(made(&first, 4))); + let crafted = [ + // A key put again, naming its deletion. + crafted(&put_again, |change| { + change.operations[0].pred = vec![made(&deleted, 0)].into() + }), + // The same at a list's element, put over and deleted. + crafted(&put_over, |change| { + change.operations[0].key = at_a.clone(); + change.operations[0].pred = vec![made(&deleted, 2)].into(); + }), + crafted(&delete, |change| { + change.operations[0].key = at_a.clone(); + change.operations[0].pred = vec![made(&deleted, 2)].into(); + }), + // A counter incremented, naming its deletion. + crafted(&increment, |change| { + change.operations[0].key = Key::Map("count".into()); + change.operations[0].pred = vec![made(&deleted, 1)].into(); + }), + // Put over, naming an increment. + crafted(&counted_over, |change| { + let increment = OpId(change.start_op.get(), change.actor_id.clone()); + change.operations[2].pred = vec![increment].into(); + }), + ]; + for crafted in crafted { + let why = Written::default().check(&ours, &crafted, &crafted.decode()); + assert!(why.is_some(), "{:?}", crafted.decode().operations); + } + } + /// 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. |
