diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/screen.rs | 16 | ||||
| -rw-r--r-- | src/document/sync.rs | 44 | ||||
| -rw-r--r-- | src/swarm/sync.rs | 1 |
3 files changed, 60 insertions, 1 deletions
diff --git a/src/access/screen.rs b/src/access/screen.rs index b6fb8fd..e37f2b9 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -283,6 +283,21 @@ impl Waiting { self.0.len() } + /// What the changes waiting build on that `doc` lacks, but for those waiting + /// themselves: what to ask the peers for (`SyncSession::lacks`). + pub(crate) fn lacking(&self, doc: &Document) -> Vec<ChangeHash> { + let waiting: HashSet<ChangeHash> = self.0.iter().map(Change::hash).collect(); + let mut lacking: Vec<ChangeHash> = self + .0 + .iter() + .flat_map(|change| change.deps().iter().copied()) + .filter(|dep| !waiting.contains(dep) && !crate::document::changes::holds(doc, dep)) + .collect(); + lacking.sort_unstable(); + lacking.dedup(); + lacking + } + /// The changes waiting, oldest first. #[cfg(test)] pub(crate) fn changes(&self) -> &[Change] { @@ -1460,6 +1475,7 @@ mod tests { let screened = screen(&mut ours, last, &mut waiting); assert_eq!(screened.applied, 0); assert_eq!(waiting.len(), 1); + assert_eq!(waiting.lacking(&ours), middle, "what it lacks, to ask for"); // The rest: all in, the waiting one too. let screened = screen(&mut ours, received(&theirs, &[]), &mut waiting); assert_eq!(screened.applied, 4); diff --git a/src/document/sync.rs b/src/document/sync.rs index e9afe06..db4eed7 100644 --- a/src/document/sync.rs +++ b/src/document/sync.rs @@ -208,6 +208,9 @@ pub(crate) struct SyncSession { /// The heads the peer said last, in the newest of its messages so far (one that /// comes late doesn't count: `older_keeps_newer`). said_heads: Option<Vec<crate::document::ChangeHash>>, + /// What changes waiting in screening build on and we lack (`access::screen`): asked + /// for in our next message (`lacks`). + lacking: Vec<crate::document::ChangeHash>, /// The wait before answering a peer that offers again only changes we refused /// (`refused_again`). @@ -266,6 +269,7 @@ impl SyncSession { known_elsewhere: std::collections::HashMap::new(), assumed_lately: std::collections::VecDeque::new(), said_heads: None, + lacking: Vec::new(), refused_backoff: Backoff::new(REFUSED_WAIT, REFUSED_WAIT_MAX), refusals_told: None, reply: None, @@ -295,7 +299,16 @@ impl SyncSession { self.state.last_sent_heads.clone(), self.state.have_responded, ); - let message = doc.generate_sync_message(&mut self.state); + let message = doc + .generate_sync_message(&mut self.state) + .map(|mut message| { + for hash in &self.lacking { + if !message.need.contains(hash) && !crate::document::changes::holds(doc, hash) { + message.need.push(*hash); + } + } + message + }); let in_step = self.state.their_heads.as_ref() == Some(&doc.get_heads()); let sent: Vec<_> = self .state @@ -904,6 +917,14 @@ impl SyncSession { before - self.state.sent_hashes.len() } + /// `lacking`, what changes waiting in screening build on and we lack + /// (`Waiting::lacking`): our next message asks for them (`need`), as Automerge asks + /// for what the changes it holds back lack. Screening keeps them from Automerge, + /// which then asks for the peer's heads alone. + pub(crate) fn lacks(&mut self, lacking: Vec<crate::document::ChangeHash>) { + self.lacking = lacking; + } + /// The peer has heads we lack (its keepalive said so): ask for what they need, /// even though Automerge thinks a message is still in flight. pub(crate) fn pull_from(&mut self, heads: Vec<crate::document::ChangeHash>) { @@ -1554,6 +1575,27 @@ mod tests { assert_eq!(decode(&ask).unwrap().0.need, vec![lost]); } + /// What changes held back in screening lack is asked for (`lacks`), as Automerge + /// asks for what the changes it holds back lack. + #[test] + fn a_message_asks_for_what_changes_held_back_lack() { + use crate::document::testing::change; + use automerge::{ROOT, transaction::Transactable}; + let (mut ours, mut theirs) = (Document::new(), Document::new()); + change(&mut ours, |doc| doc.put(ROOT, "early", 1).unwrap()); + let (_, mut b) = in_step(&mut ours, &mut theirs); + change(&mut ours, |doc| doc.put(ROOT, "lacking", 2).unwrap()); + let lacking = ours.get_heads(); + change(&mut ours, |doc| doc.put(ROOT, "later", 3).unwrap()); + b.pull_from(ours.get_heads()); + b.lacks(lacking.clone()); + let ask = b + .generate(&theirs, Instant::now(), SyncBytes::default()) + .expect("an ask"); + let need = decode(&ask).unwrap().0.need; + assert!(lacking.iter().all(|hash| need.contains(hash))); + } + /// Automerge, as we rely on it: a message taken back for a delta leaves Automerge as /// if it hadn't been generated (`withdraw_for_delta` restores what it had sent): the /// peer opened, our answer was the delta's message, so our heads never went. Once diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs index 528fe47..5f30440 100644 --- a/src/swarm/sync.rs +++ b/src/swarm/sync.rs @@ -706,6 +706,7 @@ pub(crate) async fn handle_sync( if let Some(actor) = outcome.fresh_actor { remake.add_own_actor(actor); } + sync.lacks(screening.lock_or_recover().waiting.lacking(doc)); } sync.receive_logged(doc, message, log) })(); |
