From 2ae7562083f5f96490f90e9398762cf6a024d51e Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 04:00:43 +0000 Subject: 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. --- src/swarm/dissemination.rs | 10 +++++----- src/swarm/flood.rs | 6 +++--- src/swarm/plumtree.rs | 14 +++++++------- src/swarm/refusals.rs | 15 ++++++++++----- src/swarm/relay.rs | 4 +++- src/swarm/sync.rs | 10 +++++----- src/swarm/tree.rs | 8 ++++---- 7 files changed, 37 insertions(+), 30 deletions(-) (limited to 'src/swarm') diff --git a/src/swarm/dissemination.rs b/src/swarm/dissemination.rs index 4c15d0f..11d9295 100644 --- a/src/swarm/dissemination.rs +++ b/src/swarm/dissemination.rs @@ -11,7 +11,7 @@ use async_trait::async_trait; use tracing::trace; -use crate::document::ChangeHash; +use crate::document::UnitId; use crate::state::AppState; use crate::swarm::tree::Plan; @@ -34,7 +34,7 @@ pub(crate) trait Dissemination: Send + Sync + 'static { state: &AppState, writer: u64, from: &str, - changes: &[ChangeHash], + changes: &[UnitId], ) -> Option; /// What a sync message from `peer` says about how well this works (`/admin/stats`): @@ -46,7 +46,7 @@ pub(crate) trait Dissemination: Send + Sync + 'static { peer: &str, writer: Option, carried: bool, - authors: &[(String, Vec)], + authors: &[(String, Vec)], ); /// `peer` asks us to send it `writer`'s changes at once (`eager`, a GRAFT) or to @@ -59,7 +59,7 @@ pub(crate) trait Dissemination: Send + Sync + 'static { /// `peer` told us of `heads` we lack (a STATUS: an announcement, or its keepalive's), /// for a way that minds which neighbour has changes first (PlumTree). By default /// nothing. - fn announced(&self, peer: &str, heads: &[ChangeHash]) { + fn announced(&self, peer: &str, heads: &[UnitId]) { let _ = (peer, heads); } @@ -83,7 +83,7 @@ pub(crate) async fn pass_on( state: &AppState, writer: u64, from: &str, - changes: &[ChangeHash], + changes: &[UnitId], mut plan: Plan, ) -> Plan { if !changes.is_empty() { diff --git a/src/swarm/flood.rs b/src/swarm/flood.rs index 39cb096..2883f80 100644 --- a/src/swarm/flood.rs +++ b/src/swarm/flood.rs @@ -8,7 +8,7 @@ use async_trait::async_trait; -use crate::document::ChangeHash; +use crate::document::UnitId; use crate::state::AppState; use crate::swarm::dissemination::{Dissemination, pass_on}; use crate::swarm::topology; @@ -41,7 +41,7 @@ impl Dissemination for Flood { state: &AppState, writer: u64, from: &str, - changes: &[ChangeHash], + changes: &[UnitId], ) -> Option { let plan = self.plan(state, writer).await?; Some(pass_on(state, writer, from, changes, plan).await) @@ -55,7 +55,7 @@ impl Dissemination for Flood { peer: &str, writer: Option, carried: bool, - authors: &[(String, Vec)], + authors: &[(String, Vec)], ) { let _ = peer; if writer.is_some() && carried && authors.is_empty() { diff --git a/src/swarm/plumtree.rs b/src/swarm/plumtree.rs index 9560b9f..340fa68 100644 --- a/src/swarm/plumtree.rs +++ b/src/swarm/plumtree.rs @@ -45,7 +45,7 @@ use async_trait::async_trait; use tokio::time::Instant; use tracing::debug; -use crate::document::ChangeHash; +use crate::document::UnitId; use crate::locks::MutexExt; use crate::state::AppState; use crate::swarm::dissemination::{Dissemination, pass_on}; @@ -99,7 +99,7 @@ const SETTLE_UNKNOWN: Duration = Duration::from_secs(2); #[derive(Default)] pub(crate) struct PlumTree { lazy: Mutex>, - announced: Mutex>, + announced: Mutex>, ahead_times: Mutex>, /// When we last grafted each link (`settling`). grafted: Mutex>, @@ -124,7 +124,7 @@ impl PlumTree { /// pushes in a row: its link is the faster, by at least an announcement's delay /// (`relay::ANNOUNCE_DELAY`), which a push over it doesn't wait. The changes are /// forgotten as announced. - fn ahead(&self, peer: &str, authors: &[(String, Vec)]) -> Option { + fn ahead(&self, peer: &str, authors: &[(String, Vec)]) -> Option { let now = Instant::now(); let told: Vec<(String, Instant)> = { let mut announced = self.announced.lock_or_recover(); @@ -186,7 +186,7 @@ impl PlumTree { } /// The changes, come another way, are forgotten as announced. - fn forget_announced(&self, authors: &[(String, Vec)]) { + fn forget_announced(&self, authors: &[(String, Vec)]) { let mut announced = self.announced.lock_or_recover(); for change in authors.iter().flat_map(|(_, changes)| changes) { announced.remove(change); @@ -241,7 +241,7 @@ impl Dissemination for PlumTree { state: &AppState, writer: u64, from: &str, - changes: &[ChangeHash], + changes: &[UnitId], ) -> Option { let plan = self.plan(state, writer).await?; Some(pass_on(state, writer, from, changes, plan).await) @@ -262,7 +262,7 @@ impl Dissemination for PlumTree { peer: &str, writer: Option, carried: bool, - authors: &[(String, Vec)], + authors: &[(String, Vec)], ) { match writer { Some(writer) if carried && authors.is_empty() => { @@ -304,7 +304,7 @@ impl Dissemination for PlumTree { } /// The first to tell us of each head is the one noticed. - fn announced(&self, peer: &str, heads: &[ChangeHash]) { + fn announced(&self, peer: &str, heads: &[UnitId]) { let mut announced = self.announced.lock_or_recover(); if announced.len() + heads.len() > MAX_ANNOUNCED { announced.clear(); diff --git a/src/swarm/refusals.rs b/src/swarm/refusals.rs index 652f9bc..bdc727e 100644 --- a/src/swarm/refusals.rs +++ b/src/swarm/refusals.rs @@ -18,7 +18,7 @@ use std::time::Duration; use tracing::{debug, warn}; -use crate::document::ChangeHash; +use crate::document::UnitId; use crate::document::replica::Epoch; use crate::state::AppState; use crate::transport::{ParsedMessage, RefusalsMessage}; @@ -34,13 +34,13 @@ pub(crate) async fn tell( peer: &str, epoch: Epoch, epoch_seq: u32, - mut changes: Vec, + mut changes: Vec, ) { changes.truncate(MAX_LISTED); let message = ParsedMessage::Refusals(RefusalsMessage { epoch, epoch_seq, - changes, + changes: changes.into_iter().map(Into::into).collect(), }); if let Err(error) = state.transport.send_message(peer, &message).await { // Told again with the next refusal. @@ -53,7 +53,12 @@ pub(crate) async fn tell( pub(crate) async fn received(state: &AppState, peer: &str, message: RefusalsMessage) { let doc = std::sync::Arc::clone(&state.doc); let screening = std::sync::Arc::clone(&state.screening); - let changes: Vec = message.changes.into_iter().take(MAX_LISTED).collect(); + let changes: Vec = message + .changes + .into_iter() + .take(MAX_LISTED) + .map(Into::into) + .collect(); let judged = tokio::task::spawn_blocking(move || { use crate::locks::MutexExt; let replica = doc.blocking_read(); @@ -76,7 +81,7 @@ pub(crate) async fn received(state: &AppState, peer: &str, message: RefusalsMess let mut refused = None; let mut taken = Vec::new(); for hash in changes { - match crate::access::rules::judge_held(&replica.doc, replica.epoch, &hash) { + match crate::access::rules::judge_held(&replica.doc, replica.epoch, &hash.into()) { Some(Err(why)) => { refused = Some((hash, why)); break; diff --git a/src/swarm/relay.rs b/src/swarm/relay.rs index 54749b0..560a41e 100644 --- a/src/swarm/relay.rs +++ b/src/swarm/relay.rs @@ -153,7 +153,7 @@ pub(crate) struct Pending { pub(crate) async fn announce( state: &AppState, from: &str, - writers: &[(u64, Vec)], + writers: &[(u64, Vec)], unauthored: bool, ) { if !state.initial_sync_done.load(Ordering::Acquire) { @@ -343,6 +343,8 @@ pub(crate) async fn pull_after_grace(state: &AppState, peer: &str, remote: &tran crate::document::changes::missing(&replica.doc, remote.heads.listed()) }; if !missing.is_empty() { + let missing: Vec = + missing.into_iter().map(Into::into).collect(); state.dissemination().announced(peer, &missing); let grace = state .lock_rtt() diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs index 006df9d..bb99547 100644 --- a/src/swarm/sync.rs +++ b/src/swarm/sync.rs @@ -484,9 +484,9 @@ pub(crate) async fn handle_sync( bool, bool, bool, - (Vec<(String, Vec)>, bool), + (Vec<(String, Vec)>, bool), Option, - Option<(u32, Vec)>, + Option<(u32, Vec)>, ); tokio::task::spawn_blocking(move || -> Result> { let (payload_len, message) = @@ -658,7 +658,7 @@ pub(crate) async fn handle_sync( } // Caught up: our other neighbours may lack what the source had. if changed || just_caught_up { - let writers: Vec<(u64, Vec)> = authors + let writers: Vec<(u64, Vec)> = authors .into_iter() .map(|(author, changes)| (crate::swarm::topology::pair_key(&author), changes)) .collect(); @@ -856,7 +856,7 @@ async fn download_and_apply_delta( async fn own_changes( state: &AppState, seen_heads: &mut Vec, -) -> Option> { +) -> Option> { let replica = state.doc.read().await; let before = std::mem::replace(seen_heads, replica.heads().to_vec()); if !crate::document::changes::holds_all(&replica.doc, &before) { @@ -867,7 +867,7 @@ async fn own_changes( .authors .into_iter() .find(|(author, _)| author == state.local_id()) - .map(|(_, hashes)| hashes) + .map(|(_, hashes)| hashes.into_iter().map(Into::into).collect()) } pub(crate) async fn run_sync_worker(state: AppState, mut rx: mpsc::UnboundedReceiver) { diff --git a/src/swarm/tree.rs b/src/swarm/tree.rs index 4d5afd8..0dd4e31 100644 --- a/src/swarm/tree.rs +++ b/src/swarm/tree.rs @@ -388,7 +388,7 @@ pub(crate) async fn note_arrival( peer: &str, writer: Option, carried: bool, - authors: &[(String, Vec)], + authors: &[(String, Vec)], ) { match writer { Some(writer) if carried => { @@ -663,7 +663,7 @@ pub(crate) async fn forward( state: &AppState, writer: u64, from: &str, - changes: &[crate::document::ChangeHash], + changes: &[crate::document::UnitId], ) -> Option { let plan = plan(state, writer).await?; Some(crate::swarm::dissemination::pass_on(state, writer, from, changes, plan).await) @@ -688,7 +688,7 @@ impl crate::swarm::dissemination::Dissemination for WritersTrees { state: &AppState, writer: u64, from: &str, - changes: &[crate::document::ChangeHash], + changes: &[crate::document::UnitId], ) -> Option { forward(state, writer, from, changes).await } @@ -699,7 +699,7 @@ impl crate::swarm::dissemination::Dissemination for WritersTrees { peer: &str, writer: Option, carried: bool, - authors: &[(String, Vec)], + authors: &[(String, Vec)], ) { note_arrival(state, peer, writer, carried, authors).await; } -- cgit v1.3.1