From b544cbe69d8ed2b222e0910069b2b565041bd3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Mon, 26 Oct 2026 17:27:41 +0100 Subject: [PATCH] node/test: Wake simulated nodes on their own clock The simulator scheduled a wakeup relative to the simulation's time, even when the node's clock is ahead of it, for example after `Peer::elapse`. Tests that stagger nodes this way, such as `announcement_message_amplification`, ended up running every node's periodic tasks at the same moment. Schedule the wakeup relative to the node's clock, or to the simulation's time if the node's clock is behind. Other outputs are still scheduled relative to the simulation's time. With the nodes staggered, `announcement_message_amplification` still measures close to the worst case: a node does not record the sender of a duplicate as a relayer, as the `FIXME` in `handle_announcement` notes, and so relays the message back to it. --- crates/radicle-node/src/test/simulator.rs | 44 +++++++++++++++++------ crates/radicle-node/src/tests.rs | 6 ++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/radicle-node/src/test/simulator.rs b/crates/radicle-node/src/test/simulator.rs index 70758191c..b815dc6a8 100644 --- a/crates/radicle-node/src/test/simulator.rs +++ b/crates/radicle-node/src/test/simulator.rs @@ -336,7 +336,7 @@ where let id = *peer.nid(); while let Some(o) = peer.next() { - self.schedule(&id, o); + self.schedule(&id, *peer.clock(), o); } } // Next high-priority message. @@ -431,7 +431,7 @@ where } } while let Some(o) = p.next() { - self.schedule(&node, o); + self.schedule(&node, *p.clock(), o); } } else { panic!("Node {node} not found when attempting to schedule {input:?}",); @@ -440,8 +440,8 @@ where !self.is_done() } - /// Process a service output event from a node. - pub fn schedule(&mut self, node: &NodeId, out: Io) { + /// Process a service output event from a node with the given clock. + pub fn schedule(&mut self, node: &NodeId, clock: LocalTime, out: Io) { let node = *node; match out { @@ -595,7 +595,9 @@ where ); } Io::Wakeup(duration) => { - let time = self.time + duration; + // NOTE: A node's clock may be ahead of the simulation, + // e.g., after `Peer::elapse`. + let time = clock.max(self.time) + duration; if !self.inbox.is_waking(&node, time) { self.inbox.insert( @@ -670,7 +672,7 @@ mod tests { use radicle::crypto::{Signer as _, SigningKey}; use radicle::test::storage::MockStorage; - use crate::test::peer::{AMY, BOB}; + use crate::test::peer::{AMY, BOB, CID}; use super::*; @@ -696,12 +698,34 @@ mod tests { let (amy, bob) = (nid(AMY), nid(BOB)); let duration = LocalDuration::from_secs(6); - sim.schedule(&amy, Io::Wakeup(duration)); - sim.schedule(&amy, Io::Wakeup(duration)); - sim.schedule(&bob, Io::Wakeup(duration)); - sim.schedule(&bob, Io::Wakeup(duration)); + sim.schedule(&amy, time, Io::Wakeup(duration)); + sim.schedule(&amy, time, Io::Wakeup(duration)); + sim.schedule(&bob, time, Io::Wakeup(duration)); + sim.schedule(&bob, time, Io::Wakeup(duration)); let nodes = wakes(&sim).into_iter().map(|(_, n)| n).collect::>(); assert_eq!(nodes, [amy, bob]); } + + #[test] + fn wake_node_on_its_clock() { + let time = LocalTime::from_secs(1_700_000_000); + let mut sim = Simulation::::with_seed(time, 1, Options::default()); + let (amy, bob, cid) = (nid(AMY), nid(BOB), nid(CID)); + let (two, six) = (LocalDuration::from_secs(2), LocalDuration::from_secs(6)); + + sim.schedule(&amy, time + two, Io::Wakeup(six)); + sim.schedule(&bob, time, Io::Wakeup(six)); + // A clock behind the simulation must not schedule a wakeup in the past. + sim.schedule(&cid, time - six, Io::Wakeup(two)); + + assert_eq!( + wakes(&sim), + [ + (time + two, cid), + (time + six, bob), + (time + six + two, amy) + ] + ); + } } diff --git a/crates/radicle-node/src/tests.rs b/crates/radicle-node/src/tests.rs index 3921898ef..40e36a827 100644 --- a/crates/radicle-node/src/tests.rs +++ b/crates/radicle-node/src/tests.rs @@ -2144,9 +2144,9 @@ fn announcement_message_amplification() { // In the above case of 5 nodes, this is 4 * 4 = 16 messages. This is an amplification of 4.0. // The best case is an amplification of 1.0, ie. each node receives the message once only. // - // Delayed message propagation is meant to bring this down closer to the minimum. But the - // nodes wake at the same time, and so all relay the message before receiving it from each - // other, which keeps the amplification close to the worst case. + // Delayed message propagation is meant to bring this down closer to the minimum. But a node + // does not record the sender of a duplicate as a relayer, and so relays the message back to + // it, which keeps the amplification close to the worst case. log::debug!(target: "test", "Average message amplification: {amp}"); // FIXME: This should be lower.