From b501bbe41976769c7ea2f1976e26cd8ccb778515 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 04:54:24 +0000 Subject: remake: test helpers proposing and committing against the protocol For simulations of a coordinator that doesn't keep to the protocol: `testing::propose_again` proposes a remake's proposal again under a new ID, whatever is open, and `testing::commit_as` commits a remake at the node's heads now, whatever the votes and another decision, to a given epoch or to the epoch of the snapshot the node builds there. --- src/remake/api.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/remake/api.rs') diff --git a/src/remake/api.rs b/src/remake/api.rs index 5cd4db1..325f9ba 100644 --- a/src/remake/api.rs +++ b/src/remake/api.rs @@ -996,6 +996,72 @@ pub(crate) mod testing { into_json(abort_by_id(host, id.to_owned(), query, Some(user.to_owned())).await).await } + /// What a coordinator proposing against the protocol does: proposes remake `id`'s + /// proposal again, under a new ID, whatever is open. The new ID. + pub(crate) async fn propose_again(host: &H, id: &str) -> String { + use crate::remake::records; + let all = host.read_doc(records::load_all).await; + let (_, record) = all + .into_iter() + .find(|(other, _)| other == id) + .expect("the remake's record"); + let again = format!("{id}-again-{}", host.now_millis().as_u64()); + let written = again.clone(); + host.commit_doc(move |doc| records::write_proposal(doc, &written, &record.proposal)) + .await + .expect("the proposal written"); + again + } + + /// What a coordinator deciding against the protocol does: commits remake `id`, its own + /// and still undecided, at its heads now, whatever the votes and another decision, to + /// `to_epoch`, or, where none, to the epoch of the snapshot it builds there. The epoch + /// committed. + pub(crate) async fn commit_as( + host: &H, + id: &str, + to_epoch: Option, + ) -> crate::document::replica::Epoch { + use crate::remake::records::{self, Decision}; + let all = host.read_doc(records::load_all).await; + let (_, record) = all + .into_iter() + .find(|(other, _)| other == id) + .expect("the remake's record"); + let copy = host.read_doc(|doc| doc.clone()).await; + let base = copy.get_heads(); + #[allow(unused_mut)] + let mut request = record + .snapshot_request_at(id, base.clone()) + .expect("a snapshot request"); + #[cfg(feature = "federation")] + { + request.held = crate::remake::snapshot::Held(Some(host.holding())); + } + let (snapshot, _) = crate::remake::snapshot::build_detached(copy, request) + .await + .expect("a snapshot"); + let epoch = to_epoch.unwrap_or(snapshot.epoch); + let decision = Decision::Committed { + to_epoch: epoch.into(), + base_heads: record + .proposal + .base_heads + .is_none() + .then(|| records::encode_heads(&base)), + snapshot_bytes: snapshot.doc.save().len() as u64, + decided_at: host.now_millis(), + }; + if epoch == snapshot.epoch { + host.remake().store_prebuilt(id, snapshot); + } + let id = id.to_owned(); + host.commit_doc(move |doc| records::write_decision(doc, &id, &decision)) + .await + .expect("the decision written"); + epoch + } + /// `GET /admin/remakes/{id}`: the report, if the remake exists here. pub(crate) async fn report(host: &H, id: &str) -> Option { let report = load_report(host, id).await?; -- cgit v1.3.1