aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 03:51:14 +0000
committertv <tv@krebsco.de>2026-11-29 03:51:14 +0000
commitfacc05316e2b350d7b7bbd7dfd0c013cc167f341 (patch)
tree91950b531d24cca374d9f77bf37944c1619797fb /src
parent66f5cefd1801e2421169adf27d676ea8190e61de (diff)
document: taking changes fetched as a blob is the document's
document::passing::take_bulk: the changes a peer offered as a blob, fetched, judged and applied with their signatures, and all the heads offered held after. swarm::sync keeps the fetch, the epoch check, and what is counted and noted of the peer.
Diffstat (limited to 'src')
-rw-r--r--src/document/passing.rs76
-rw-r--r--src/swarm/sync.rs52
2 files changed, 91 insertions, 37 deletions
diff --git a/src/document/passing.rs b/src/document/passing.rs
index 381b5c7..f745d42 100644
--- a/src/document/passing.rs
+++ b/src/document/passing.rs
@@ -370,3 +370,79 @@ pub(crate) fn settle(
level: level && session.have_all_theirs(&replica.doc),
}
}
+
+/// What taking changes fetched as a blob did (`take_bulk`).
+pub(crate) struct Bulk {
+ /// The document's heads before.
+ pub(crate) heads_before: Vec<ChangeHash>,
+ /// How many heads it has now, and whether they changed.
+ pub(crate) heads: usize,
+ pub(crate) changed: bool,
+ /// Where asked, and the heads changed: when each change that came was made.
+ pub(crate) times: Vec<i64>,
+}
+
+/// Takes `bytes`, the changes a peer offered as a blob up to the heads `to`, fetched,
+/// into the document `replica` holds, with the `signatures` that came for them: judged
+/// as a message's are (`take`), and all of `to` held after, or it fails. With the fresh
+/// actor the node writes as from now on, where they carried a change of its actor that
+/// it didn't make, failed or not.
+pub(crate) fn take_bulk(
+ replica: &mut Replica,
+ bytes: &[u8],
+ signatures: Vec<(ChangeHash, [u8; 64])>,
+ to: &[ChangeHash],
+ screening: &std::sync::Mutex<super::screen::Screening>,
+ timed: bool,
+) -> (Option<super::ActorId>, anyhow::Result<Bulk>) {
+ let heads_before = replica.heads().to_vec();
+ let fresh_actor = {
+ // Its signatures wait for its changes (`Signatures::offer`), each checked as they
+ // are judged (`screen`), whether this document has a policy yet or not.
+ let (epoch, epoch_seq) = (replica.epoch, replica.epoch_seq);
+ let replica = &mut *replica;
+ replica.signatures.offer(&replica.doc, signatures);
+ match super::screen::apply_bytes(
+ &mut replica.doc,
+ epoch,
+ epoch_seq,
+ bytes,
+ screening,
+ Some(&mut replica.signatures),
+ &mut super::PatchLog::inactive(),
+ ) {
+ Ok(outcome) => outcome.fresh_actor,
+ Err(error) => return (None, Err(error)),
+ }
+ };
+ replica.refresh_heads();
+ // Signatures waiting for what it brought (`Signatures::offer`).
+ {
+ let replica = &mut *replica;
+ replica.signatures.offer(&replica.doc, Vec::new());
+ }
+ let missing = super::changes::missing(&replica.doc, to).len();
+ if missing > 0 {
+ return (
+ fresh_actor,
+ Err(anyhow::anyhow!(
+ "{missing} of the offered heads are still missing after loading"
+ )),
+ );
+ }
+ let changed = replica.heads() != heads_before.as_slice();
+ let times = if changed && timed {
+ super::changes::commit_times(&replica.doc, &heads_before)
+ } else {
+ Vec::new()
+ };
+ (
+ fresh_actor,
+ Ok(Bulk {
+ heads: replica.heads().len(),
+ changed,
+ times,
+ heads_before,
+ }),
+ )
+}
diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs
index 18e5653..006df9d 100644
--- a/src/swarm/sync.rs
+++ b/src/swarm/sync.rs
@@ -812,47 +812,25 @@ async fn download_and_apply_delta(
if replica.epoch != epoch {
bail!("the epoch changed during the download");
}
- let heads_before = replica.heads().to_vec();
- {
- // Its signatures wait for its changes (`Signatures::offer`), each checked as
- // they are judged (`document::screen`), whether this document has a policy yet
- // or not.
- let epoch_seq = replica.epoch_seq;
- let replica = &mut *replica;
- replica.signatures.offer(&replica.doc, signatures);
- let outcome = crate::document::screen::apply_bytes(
- &mut replica.doc,
- epoch,
- epoch_seq,
- &bytes,
- &screening,
- Some(&mut replica.signatures),
- &mut crate::document::PatchLog::inactive(),
- )?;
- if let Some(actor) = outcome.fresh_actor {
- own_writers.add(actor);
- }
- }
- replica.refresh_heads();
- // Signatures waiting for what it brought (`Signatures::offer`).
- {
- let replica = &mut *replica;
- replica.signatures.offer(&replica.doc, Vec::new());
- }
- let missing = crate::document::changes::missing(&replica.doc, &spec.to).len();
- if missing > 0 {
- bail!("{missing} of the offered heads are still missing after loading");
+ let (fresh_actor, bulk) = crate::document::passing::take_bulk(
+ &mut replica,
+ &bytes,
+ signatures,
+ &spec.to,
+ &screening,
+ measure_propagation,
+ );
+ if let Some(actor) = fresh_actor {
+ own_writers.add(actor);
}
- stats.heads_seen(replica.heads().len());
- let changed = replica.heads() != heads_before.as_slice();
+ let bulk = bulk?;
+ stats.heads_seen(bulk.heads);
+ let changed = bulk.changed;
+ let heads_before = bulk.heads_before;
if changed && measure_propagation {
// As in `receive`; a delta goes along no writer's tree.
let now_ms = clock.now_millis().as_u64() as i64 - ahead_ms;
- let delays: Vec<i64> =
- crate::document::changes::commit_times(&replica.doc, &heads_before)
- .into_iter()
- .map(|time| now_ms - time)
- .collect();
+ let delays: Vec<i64> = bulk.times.into_iter().map(|time| now_ms - time).collect();
stats.off_tree(&delays);
stats.propagated(delays);
}