diff options
Diffstat (limited to 'src/remake/delta.rs')
| -rw-r--r-- | src/remake/delta.rs | 89 |
1 files changed, 79 insertions, 10 deletions
diff --git a/src/remake/delta.rs b/src/remake/delta.rs index 327249b..c3688cb 100644 --- a/src/remake/delta.rs +++ b/src/remake/delta.rs @@ -138,9 +138,9 @@ pub(crate) fn replay_newest( .map(|path| { let at = newest(&written, path).map_or(0, |(at, _)| at); // Under a strict policy, the user of the change it goes as: the last by - // history that touched it (`last_written`). + // history that wrote it (`last_written`). let user = if strict { - last_written(&written, path).and_then(|made| written[made].1.clone()) + last_written(src, &changes, &written, path).and_then(|made| written[made].1.clone()) } else { writer(&written, path) }; @@ -212,7 +212,7 @@ pub(crate) fn replay_newest( } _ => { let goes = if strict { - last_written(&written, &path).map_or(0, |made| made as i64) + last_written(src, &changes, &written, &path).map_or(0, |made| made as i64) } else { at }; @@ -638,16 +638,30 @@ fn touched(doc: &Document, changes: &[ChangeHash]) -> Vec<Touched> { .collect() } -/// Where, among the changes `written` is of (`touched`, in their order), the last that -/// touched `path`, a path above it or one below it, is. -fn last_written(written: &[Touched], path: &[String]) -> Option<usize> { - written.iter().rposition(|(_, _, paths)| { - paths - .iter() - .any(|other| other.starts_with(path) || path.starts_with(other)) +/// Where, among `changes` (in `src`; `written` is theirs, `touched`, in their order), the +/// last that wrote at `path` or below it, or replaced what was at a path above it, is: a +/// map one made on the way, where it found none, is no write of that change's at `path`. +fn last_written( + src: &Document, + changes: &[ChangeHash], + written: &[Touched], + path: &[String], +) -> Option<usize> { + (0..written.len()).rev().find(|&made| { + written[made].2.iter().any(|other| { + other.starts_with(path) + || path.starts_with(other) && found_at(src, &changes[made], other) + }) }) } +/// Whether the change `hash` (in `doc`) found something at `path` where it was made. +fn found_at(doc: &Document, hash: &ChangeHash, path: &[String]) -> bool { + doc.get_change_by_hash(hash) + .and_then(|change| At::new(doc, change.deps())) + .is_some_and(|before| matches!(store::read_held(&before, path), Ok(Some(_)))) +} + /// The time and user of the newest of `changes` that touched `path`, a path above it or /// one below it (of two at once, the user named last in order). fn newest(changes: &[Touched], path: &[String]) -> Option<(i64, Option<String>)> { @@ -1442,6 +1456,61 @@ mod tests { assert_eq!(written, [vec![path("config")], vec![path("notes")]]); } + /// Under a strict policy, a map made on the way is no write of the change that made it + /// at what another wrote below it: two users writing their first entries at once, each + /// making the map above them, each have theirs replayed as their own. + #[test] + fn maps_made_on_the_way_name_no_user() { + use crate::access::Right; + let path = |text: &str| -> Vec<String> { text.split('/').map(str::to_owned).collect() }; + let key = ed25519_dalek::SigningKey::from_bytes(&[7; 32]); + let mut old = strict_with(&key); + change(&mut old, |doc| { + for (user, entry) in [("alice", "servers/eu"), ("bob", "servers/us")] { + let rights = crate::access::registry::entry( + "root", + 1, + &[(path(entry), &[Right::Write][..])], + ); + store::write_path(doc, &path(&format!("_continuum/users/{user}")), &rights) + .unwrap(); + } + }); + let base = old.get_heads(); + let mut replayed = old.clone().with_actor(automerge::ActorId::random()); + let mut bob = old.clone().with_actor(automerge::ActorId::random()); + for (doc, user, entry, at) in [ + (&mut old, "alice", "servers/eu", 10), + (&mut bob, "bob", "servers/us", 20), + ] { + let mut transaction = doc.transaction(); + store::write_path(&mut transaction, &path(entry), &json!(1)).unwrap(); + transaction.commit_with( + automerge::transaction::CommitOptions::default() + .with_time(at) + .with_message(format!("User: {user}")), + ); + } + old.merge(&mut bob).unwrap(); + let stats = replay_newest( + &old, + &base, + &old.get_heads(), + &mut replayed, + crate::document::replica::Epoch::GENESIS, + "node", + &Translator::default(), + Superseded::Drop, + ); + assert!(stats.quarantined.is_empty(), "{:?}", stats.quarantined); + for entry in ["servers/eu", "servers/us"] { + assert_eq!( + store::read_path(&replayed, &path(entry)).unwrap(), + Some(json!(1)) + ); + } + } + /// The first group refused: those after it still replayed, still the node's (a /// fork, taking the refused one back, has no author of its own). #[test] |
