aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/sync.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 04:00:43 +0000
committertv <tv@krebsco.de>2026-11-29 04:00:43 +0000
commit2ae7562083f5f96490f90e9398762cf6a024d51e (patch)
tree5e23c6bda5682e39413fe557348023cc4204d487 /src/document/sync.rs
parentf497cf6ef14de8204bc1edec078116593d791b97 (diff)
document: a unit's name, which the swarm passes on by
document::UnitId: the name of a unit of a document as its kind has it, an Automerge change's hash. What the swarm announces, forwards along trees, tells a peer it refused and drops is named so (the dissemination, relay, refusals, AppState::drop_taken), and the session is told so what a peer gets from another; a change's hash it is where the document is asked, and on the wire.
Diffstat (limited to 'src/document/sync.rs')
-rw-r--r--src/document/sync.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/document/sync.rs b/src/document/sync.rs
index 2de8e60..e1caaef 100644
--- a/src/document/sync.rs
+++ b/src/document/sync.rs
@@ -761,12 +761,15 @@ impl SyncSession {
/// The peer has `changes`, or will without us (another parent in their writer's
/// tree brings them, `swarm::tree::forward`): counted as sent, they don't go to it,
/// unless it asks for them (`resend_if_needed`).
- pub(crate) fn known_to_peer(&mut self, changes: &[crate::document::ChangeHash], now: Instant) {
+ pub(crate) fn known_to_peer(&mut self, changes: &[crate::document::UnitId], now: Instant) {
self.known_elsewhere
.retain(|_, at| now.saturating_duration_since(*at) < KNOWN_ELSEWHERE_KEPT);
- for hash in changes {
- if self.state.sent_hashes.insert(*hash) {
- self.known_elsewhere.insert(*hash, now);
+ for hash in changes
+ .iter()
+ .map(|unit| crate::document::ChangeHash::from(*unit))
+ {
+ if self.state.sent_hashes.insert(hash) {
+ self.known_elsewhere.insert(hash, now);
}
}
}
@@ -779,11 +782,14 @@ impl SyncSession {
pub(crate) fn with_predecessors(
&mut self,
doc: &Document,
- changes: &[crate::document::ChangeHash],
+ changes: &[crate::document::UnitId],
now: Instant,
) {
- for hash in changes {
- let Some(change) = doc.get_change_by_hash(hash) else {
+ for hash in changes
+ .iter()
+ .map(|unit| crate::document::ChangeHash::from(*unit))
+ {
+ let Some(change) = doc.get_change_by_hash(&hash) else {
continue;
};
for dep in change.deps() {
@@ -1275,6 +1281,10 @@ mod model;
mod tests {
use super::*;
+ fn units(hashes: &[crate::document::ChangeHash]) -> Vec<crate::document::UnitId> {
+ hashes.iter().copied().map(Into::into).collect()
+ }
+
/// After a loss, the changes the peer lacks go in one message, not one per round
/// trip (`answer_for_everything_since_shared`).
#[test]
@@ -1540,7 +1550,7 @@ mod tests {
let (now, bytes) = (Instant::now(), SyncBytes::default());
for i in 0..100 {
change(&mut ours, |doc| doc.put(ROOT, "key", i).unwrap());
- a.known_to_peer(&ours.get_heads(), now);
+ a.known_to_peer(&units(&ours.get_heads()), now);
}
theirs.merge(&mut ours.clone()).unwrap();
assert_eq!(a.sent_hashes_len(), 100);
@@ -1559,7 +1569,7 @@ mod tests {
// Older heads than those, said later (a STATUS overtaken): nothing changes.
change(&mut ours, |doc| doc.put(ROOT, "key", 100).unwrap());
- a.known_to_peer(&ours.get_heads(), now);
+ a.known_to_peer(&units(&ours.get_heads()), now);
let older = theirs.get_heads();
theirs.merge(&mut ours.clone()).unwrap();
assert_eq!(a.heard_heads(&ours, &theirs.get_heads()), 1);
@@ -2093,7 +2103,7 @@ mod tests {
let (now, bytes) = (Instant::now(), SyncBytes::default());
change(&mut ours, |doc| doc.put(ROOT, "elsewhere", 2).unwrap());
- a.known_to_peer(&ours.get_heads(), Instant::now());
+ a.known_to_peer(&units(&ours.get_heads()), Instant::now());
let message = a.generate(&ours, now, bytes).expect("our heads moved");
a.send_succeeded(a.id());
let carried = decode(&message).unwrap().change_bytes();
@@ -2122,9 +2132,9 @@ mod tests {
let (now, bytes) = (Instant::now(), SyncBytes::default());
change(&mut ours, |doc| doc.put(ROOT, "elsewhere", 2).unwrap());
- a.known_to_peer(&ours.get_heads(), now);
+ a.known_to_peer(&units(&ours.get_heads()), now);
change(&mut ours, |doc| doc.put(ROOT, "along", 3).unwrap());
- a.with_predecessors(&ours, &ours.get_heads(), now);
+ a.with_predecessors(&ours, &units(&ours.get_heads()), now);
let message = a.generate(&ours, now, bytes).expect("our heads moved");
b.receive(&mut theirs, decode(&message).unwrap()).unwrap();
assert_eq!(theirs.get_heads(), ours.get_heads(), "both in one message");