diff --git a/pkgs/id/src/encryption/mod.rs b/pkgs/id/src/encryption/mod.rs index 588b7fbf56..5b040fab37 100644 --- a/pkgs/id/src/encryption/mod.rs +++ b/pkgs/id/src/encryption/mod.rs @@ -34,7 +34,7 @@ pub use ordering::{Envelope, Orderer, OrdererError}; pub use roster::Roster; pub use scheme::{ DATASCHEME, MAX_HELD_BYTES, MAX_HELD_MESSAGES, RECOVERY_STEPS, Received, Recovery, SCHEME_V1, - SchemeError, Session, key_bundle, key_of, negotiate, new_keys, registry, + SchemeError, Session, StalledDelivery, key_bundle, key_of, negotiate, new_keys, registry, }; #[cfg(test)] diff --git a/pkgs/id/src/encryption/ordering.rs b/pkgs/id/src/encryption/ordering.rs index 70858a3170..e06f36e2b5 100644 --- a/pkgs/id/src/encryption/ordering.rs +++ b/pkgs/id/src/encryption/ordering.rs @@ -9,6 +9,12 @@ //! delivered before the device is welcomed: a create or a welcome opens delivery, and earlier //! messages wait in the queue. //! +//! A message that waits only because a message it names has not been delivered is a stalled +//! delivery (#187): [`OrdererState::waiting`] reports it with the names it still lacks, and +//! [`super::scheme::Session::stalled_delivery`] surfaces that to callers. Nothing expires a wait +//! and nothing is dropped: the caller polls the surface and drives recovery (a welcome, a history +//! answer, a re-sync) until the missing messages arrive and the queue drains on its own. +//! //! Application messages are never named by other messages, so the adapter may leave one out //! without stalling anything. [`super::scheme`] does that for messages whose group secret the //! device does not hold. @@ -246,6 +252,28 @@ impl OrdererState { self.parent_list().len() } + /// The messages that still wait for a message they name, with the names they still lack, in + /// message ID order (the map's order). A recorded name that has since become ready is not + /// reported: what is reported is what the message still waits for, in the order the names + /// were recorded. This is the read side of the stalled-delivery surface (#187): the orderer + /// keeps no timer and drops nothing that waits, so a caller polls this and drives recovery. + pub fn waiting(&self) -> Vec<(Hash, Vec)> { + self.waiting + .iter() + .map(|(id, missing)| { + ( + *id, + missing + .iter() + .copied() + .filter(|named| !self.ready.contains(named)) + .collect::>(), + ) + }) + .filter(|(_, missing)| !missing.is_empty()) + .collect() + } + /// A control message is a head from the time it is delivered or published, until a control /// message names it. The messages it names stop being heads. fn observe(&mut self, envelope: &Envelope) { diff --git a/pkgs/id/src/encryption/scheme.rs b/pkgs/id/src/encryption/scheme.rs index f8916a8ce5..753b3f3f5a 100644 --- a/pkgs/id/src/encryption/scheme.rs +++ b/pkgs/id/src/encryption/scheme.rs @@ -101,6 +101,17 @@ pub enum Received { Withheld(Hash), } +/// A message that cannot be delivered yet because a message it names has not been delivered +/// (#187): the waiting message, and the named messages it still lacks. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct StalledDelivery { + /// The message that waits in the orderer. + pub message: Hash, + /// The messages `message` names that are not delivered yet, in the order the orderer + /// recorded them. + pub missing: Vec, +} + /// What the recovery procedure (N8) did. The CLI prints [`RECOVERY_STEPS`] next to it. #[derive(Clone, Debug)] pub struct Recovery { @@ -296,6 +307,27 @@ impl Session { roster_of(y) } + /// The messages stalled in delivery: each message the orderer holds because a message it + /// names has not been delivered, with the names it still lacks (#187). Entries are sorted + /// by message ID; the result is empty when nothing waits, and empty before the group is + /// created or joined on this device. + /// + /// This is the pre-join stall [`Session::request_history`] cannot see: a joiner missing + /// control messages from before its join waits here, not in the secret-held set. The + /// session carries no timer and drops nothing that waits, so the caller polls this surface + /// and drives the existing answer paths — a welcome, a history request, a re-sync — until + /// the missing messages arrive; the stall then drains on its own and this returns empty + /// again. + pub fn stalled_delivery(&self) -> Vec { + self.state.as_ref().map_or_else(Vec::new, |y| { + y.orderer + .waiting() + .into_iter() + .map(|(message, missing)| StalledDelivery { message, missing }) + .collect() + }) + } + /// Creates the group with the members `view` lists, as the group fold gives them. This device /// must be one of them. pub fn create(&mut self, view: &GroupView, rng: &Rng) -> Result { diff --git a/pkgs/id/src/encryption/tests.rs b/pkgs/id/src/encryption/tests.rs index e325f631fa..ce7a027b65 100644 --- a/pkgs/id/src/encryption/tests.rs +++ b/pkgs/id/src/encryption/tests.rs @@ -1565,6 +1565,90 @@ fn add_welcome_len(rotations: usize) -> usize { encode_cbor(&add).expect("encode the add").len() } +// --- stalled delivery (#187) --------------------------------------------------------------------- + +#[test] +fn test_stalled_delivery_is_empty_before_create_and_when_caught_up() { + let mut cast = Cast::new(&[1, 2]); + assert!( + cast.session(1).stalled_delivery().is_empty(), + "before create" + ); + assert!( + cast.session(2).stalled_delivery().is_empty(), + "before create" + ); + let create = cast.create(1, &[1, 2]); + cast.deliver(&create, &[2]); + let message = cast.send(1, "caught up"); + cast.deliver(&message, &[2]); + assert!(cast.session(2).stalled_delivery().is_empty(), "caught up"); +} + +#[test] +fn test_a_joiner_sees_the_control_message_it_missed_and_drains_when_it_arrives() { + let mut cast = Cast::new(&[1, 2, 3, 4]); + let create = cast.create(1, &[1, 2, 3]); + cast.deliver(&create, &[2, 3, 4]); + + let add = cast.add(1, 4); + cast.deliver(&add, &[2, 3]); + let update = cast.update(1); + cast.deliver(&update, &[2, 3]); + + // The joiner is handed the update before the add that welcomes it: the update names the + // add, so it waits in the orderer, and the stall names exactly the add. + cast.deliver(&update, &[4]); + assert_eq!( + cast.session(4).stalled_delivery(), + vec![StalledDelivery { + message: update.id(), + missing: vec![add.id()] + }], + ); + + // The add arrives: the stall drains and the joiner is welcomed. + cast.deliver(&add, &[4]); + assert!(cast.session(4).stalled_delivery().is_empty()); + assert!(cast.session(4).is_welcomed()); +} + +#[test] +fn test_an_application_message_stalled_on_a_concurrent_add_reports_exactly_the_missing_add() { + let mut cast = Cast::new(&[1, 2, 3, 4, 5]); + let create = cast.create(1, &[1, 2, 3]); + cast.deliver(&create, &[2, 3, 4, 5]); + + // Two concurrent adds: neither names the other. + let add_by_2 = concurrent_add(&mut cast, 2, 4, &[1, 2, 3, 4]); + let add_by_3 = concurrent_add(&mut cast, 3, 5, &[1, 2, 3, 5]); + + // Member 1 hears both adds (2's first, then 3's), so a message it sends names both, under + // the newest epoch it holds. + cast.deliver(&add_by_2, &[1]); + cast.deliver(&add_by_3, &[1]); + let message = cast.send(1, "names both adds"); + + // Member 3 authored the second add but never heard the first: it holds the epoch the + // message is under, so the message reaches its orderer and waits there instead of being + // read, naming exactly the add it lacks. + assert_eq!(cast.deliver(&message, &[3]), vec![Vec::::new()]); + assert_eq!( + cast.session(3).stalled_delivery(), + vec![StalledDelivery { + message: message.id(), + missing: vec![add_by_2.id()] + }], + ); + + // The missing add arrives: the message is delivered and the stall is gone. + assert_eq!( + cast.deliver(&add_by_2, &[3]), + vec![vec![text("names both adds")]] + ); + assert!(cast.session(3).stalled_delivery().is_empty()); +} + #[test] fn test_each_epoch_adds_about_forty_bytes_to_the_welcome() { // S12: serde_bytes on the two byte fields costs 40.0 bytes per epoch. The stock integer arrays diff --git a/pkgs/id/src/log_sync.rs b/pkgs/id/src/log_sync.rs index 7d4b2c2d0b..8103d4d349 100644 --- a/pkgs/id/src/log_sync.rs +++ b/pkgs/id/src/log_sync.rs @@ -1023,15 +1023,7 @@ mod tests { } }); // The first arrival, however it gets here, starts the batch. - let first = loop { - match rx.try_recv() { - Ok(arrival) => break arrival, - Err(mpsc::error::TryRecvError::Empty) => { - std::thread::sleep(Duration::from_millis(1)); - } - Err(error) => panic!("the pacer went away: {error}"), - } - }; + let first = rx.blocking_recv().expect("the pacer went away"); let batch = collect_batch(first, &mut rx); pacer.join().unwrap(); @@ -1057,15 +1049,7 @@ mod tests { std::thread::sleep(Duration::from_millis(2)); } }); - let first = loop { - match rx.try_recv() { - Ok(arrival) => break arrival, - Err(mpsc::error::TryRecvError::Empty) => { - std::thread::sleep(Duration::from_millis(1)); - } - Err(error) => panic!("the pacer went away: {error}"), - } - }; + let first = rx.blocking_recv().expect("the pacer went away"); let batch = collect_batch(first, &mut rx); pacer.join().unwrap(); @@ -1126,15 +1110,7 @@ mod tests { std::thread::sleep(Duration::from_millis(2)); } }); - let first = loop { - match rx.try_recv() { - Ok(arrival) => break arrival, - Err(mpsc::error::TryRecvError::Empty) => { - std::thread::sleep(Duration::from_millis(1)); - } - Err(error) => panic!("the pacer went away: {error}"), - } - }; + let first = rx.blocking_recv().expect("the pacer went away"); let started = std::time::Instant::now(); let batch = collect_batch(first, &mut rx); let elapsed = started.elapsed(); diff --git a/pkgs/id/src/world_doctor.rs b/pkgs/id/src/world_doctor.rs index 63212516df..4b20bde88e 100644 --- a/pkgs/id/src/world_doctor.rs +++ b/pkgs/id/src/world_doctor.rs @@ -435,7 +435,14 @@ mod tests { let _ = std::fs::create_dir_all(dir.join("targets/wasm32")); let _ = std::fs::write(dir.join("platform.roc"), "platform"); let _ = std::fs::write(dir.join("targets/wasm32/host.wasm"), "wasm"); - let roc = crate::world_compile::locate_roc().ok(); + let roc_path = dir.join("roc"); + let _ = std::fs::write(&roc_path, "roc binary"); + #[cfg(unix)] + let _ = std::fs::set_permissions( + &roc_path, + std::os::unix::fs::PermissionsExt::from_mode(0o755), + ); + let roc = Some(roc_path.to_string_lossy().into_owned()); let roc_resolved = roc .clone() .map(Ok)