aboutsummaryrefslogtreecommitdiffstats
path: root/src/document
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
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')
-rw-r--r--src/document/mod.rs33
-rw-r--r--src/document/passing.rs16
-rw-r--r--src/document/sync.rs34
3 files changed, 67 insertions, 16 deletions
diff --git a/src/document/mod.rs b/src/document/mod.rs
index 3872ea9..2e65bee 100644
--- a/src/document/mod.rs
+++ b/src/document/mod.rs
@@ -13,6 +13,39 @@ pub(crate) use automerge::{
ScalarValue,
};
+/// The name of a unit of a document, as its kind of document has it (`docs/document.md`,
+/// Passing on): what is announced, asked for, refused and counted is named so, above the
+/// seam. An Automerge document's units are its changes, named by their hashes.
+#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
+pub(crate) struct UnitId(pub(crate) [u8; 32]);
+
+impl From<ChangeHash> for UnitId {
+ fn from(hash: ChangeHash) -> Self {
+ UnitId(hash.0)
+ }
+}
+
+impl From<UnitId> for ChangeHash {
+ fn from(unit: UnitId) -> Self {
+ ChangeHash(unit.0)
+ }
+}
+
+impl std::fmt::Display for UnitId {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ for byte in self.0 {
+ write!(f, "{byte:02x}")?;
+ }
+ Ok(())
+ }
+}
+
+impl std::fmt::Debug for UnitId {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{self}")
+ }
+}
+
/// The version of Automerge this build makes documents with: a remake needs the same on
/// every node, and a federation the same encoding of a change (`remake::snapshot`).
pub(crate) fn engine_version() -> &'static str {
diff --git a/src/document/passing.rs b/src/document/passing.rs
index f745d42..10b888d 100644
--- a/src/document/passing.rs
+++ b/src/document/passing.rs
@@ -232,7 +232,7 @@ pub(crate) struct Taken {
/// It carried changes, all of them refused before (`screen::Outcome`).
pub(crate) refused_again: bool,
/// The changes that broke a rule, of those refused.
- pub(crate) roots: Vec<ChangeHash>,
+ pub(crate) roots: Vec<super::UnitId>,
/// Changes it carried forged under another node's actor.
pub(crate) forged: usize,
/// The fresh actor this node writes as from now on, where it carried a change of
@@ -280,7 +280,7 @@ pub(crate) fn take(
heads_before,
said,
refused_again: outcome.refused_again,
- roots: outcome.roots,
+ roots: outcome.roots.into_iter().map(Into::into).collect(),
forged: outcome.forged,
fresh_actor: outcome.fresh_actor,
};
@@ -305,7 +305,7 @@ pub(crate) struct Settled {
/// predecessors (the peer's heads aren't the document's yet), or had come before.
pub(crate) waits: Option<bool>,
/// Whose the changes it brought are, whether any is nobody's, and when each was made.
- pub(crate) authors: Vec<(String, Vec<ChangeHash>)>,
+ pub(crate) authors: Vec<(String, Vec<super::UnitId>)>,
pub(crate) unauthored: bool,
pub(crate) times: Vec<i64>,
/// Where asked: whether the document holds all the peer said it has.
@@ -354,7 +354,15 @@ pub(crate) fn settle(
let waits =
(carried && !changed).then(|| !super::changes::holds_all(&replica.doc, &taken.said));
let (authors, unauthored, times) = match since {
- Some(since) => (since.authors, since.unauthored, since.times),
+ Some(since) => (
+ since
+ .authors
+ .into_iter()
+ .map(|(author, hashes)| (author, hashes.into_iter().map(Into::into).collect()))
+ .collect(),
+ since.unauthored,
+ since.times,
+ ),
None => (Vec::new(), false, Vec::new()),
};
Settled {
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");