use commonware_cryptography::PublicKey;
use commonware_p2p::simulated::Link;
use commonware_runtime::deterministic;
use std::time::Duration;
#[derive(Clone)]
pub enum Crash<P: PublicKey> {
Random {
frequency: Duration,
downtime: Duration,
count: usize,
},
Delay {
count: usize,
after: u64,
},
Schedule(Schedule<P>),
}
#[derive(Clone)]
pub struct Schedule<P: PublicKey> {
pub events: Vec<(Duration, Action<P>)>,
}
impl<P: PublicKey> Schedule<P> {
pub const fn new() -> Self {
Self { events: vec![] }
}
pub fn at(mut self, time: Duration, action: Action<P>) -> Self {
if let Some((last_time, _)) = self.events.last() {
assert!(
time >= *last_time,
"schedule events must be added in nondecreasing time order"
);
}
self.events.push((time, action));
self
}
}
impl<P: PublicKey> Default for Schedule<P> {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone)]
pub enum Action<P: PublicKey> {
SetStorageFault(deterministic::FaultConfig),
Heal(Link),
UpdateLink {
from: P,
to: P,
link: Link,
},
Crash(P),
Restart(P),
}
#[cfg(test)]
mod tests {
use super::{Action, Schedule};
use commonware_cryptography::{ed25519, Signer as _};
use std::time::Duration;
#[test]
#[should_panic(expected = "schedule events must be added in nondecreasing time order")]
fn schedule_rejects_out_of_order_events() {
let pk = ed25519::PrivateKey::from_seed(0).public_key();
let _ = Schedule::new()
.at(Duration::from_secs(5), Action::Crash(pk.clone()))
.at(Duration::from_secs(2), Action::Restart(pk));
}
}