//! Mock sender implementations for testing. use commonware_actor::{Feedback, Unreliable}; use commonware_codec::ReadExt as _; use commonware_cryptography::PublicKey; use commonware_p2p::{CheckedSender, LimitedSender, Recipients}; use commonware_runtime::IoBufs; use commonware_utils::sync::Mutex; use std::{sync::Arc, time::SystemTime}; /// A sender that always returns [Feedback::Closed]. #[derive(Clone, Debug)] pub struct Failing { _phantom: std::marker::PhantomData

, } impl Failing

{ /// Creates a new failing sender. pub fn new() -> Self { Self { _phantom: std::marker::PhantomData, } } } impl LimitedSender for Failing

{ type PublicKey = P; type Checked<'a> = CheckedFailing

; fn check(&mut self, _recipients: Recipients

) -> Result, SystemTime> { Ok(CheckedFailing { _phantom: std::marker::PhantomData, }) } } /// The [CheckedSender] of a [Failing] sender. pub struct CheckedFailing { _phantom: std::marker::PhantomData

, } impl CheckedSender for CheckedFailing

{ type PublicKey = P; fn recipients(&self) -> Vec { Vec::new() } fn send(self, _message: impl Into + Send, _priority: bool) -> Unreliable { Unreliable::new(Feedback::Closed) } } /// Recipients, first payload byte, and priority of each send made through a [Recording] sender. pub type Sent

= Arc, u8, bool)>>>; /// A sender that records the recipients, first payload byte, and priority of every send and /// reports each explicitly named recipient as sent to. #[derive(Clone, Debug)] pub struct Recording { sent: Sent

, } impl Recording

{ /// Creates a recording sender and the log it appends to. pub fn new() -> (Self, Sent

) { let sent = Sent::

::default(); (Self { sent: sent.clone() }, sent) } } impl LimitedSender for Recording

{ type PublicKey = P; type Checked<'a> = CheckedRecording

; fn check(&mut self, recipients: Recipients

) -> Result, SystemTime> { Ok(CheckedRecording { sent: self.sent.clone(), recipients, }) } } /// The [CheckedSender] of a [Recording] sender. pub struct CheckedRecording { sent: Sent

, recipients: Recipients

, } impl CheckedSender for CheckedRecording

{ type PublicKey = P; fn recipients(&self) -> Vec { match &self.recipients { Recipients::All => Vec::new(), Recipients::Some(peers) => peers.clone(), Recipients::One(peer) => vec![peer.clone()], } } fn send(self, message: impl Into + Send, priority: bool) -> Unreliable { let mut payload: IoBufs = message.into(); let first = u8::read(&mut payload).expect("a payload must not be empty"); self.sent.lock().push((self.recipients, first, priority)); Unreliable::new(Feedback::Ok) } }