diff options
Diffstat (limited to 'src/document')
| -rw-r--r-- | src/document/sync.rs | 44 |
1 files changed, 43 insertions, 1 deletions
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 |
