use commonware_cryptography::PublicKey; use commonware_runtime::Spawner; use futures::{channel::mpsc, StreamExt}; use std::sync::{Arc, Mutex}; /// Message type for coordinator updates pub enum CoordinatorMsg

{ /// Update the peers list UpdatePeers(Vec

), } /// A coordinator that can be used for testing #[derive(Clone)] pub struct Coordinator { /// The state of the coordinator state: Arc>>, } /// The state of the coordinator struct State { peers: Vec

, peer_set_id: u64, } impl Coordinator

{ /// Creates a new coordinator with the given initial peers pub fn new(initial_peers: Vec

) -> Self { let state = State { peers: initial_peers, peer_set_id: 0, }; Self { state: Arc::new(Mutex::new(state)), } } /// Creates a channel for sending updates to this coordinator pub fn create_update_channel(&self, context: E) -> mpsc::Sender> { let (sender, mut receiver) = mpsc::channel(8); let coordinator = self.clone(); context.spawn(|_| async move { while let Some(msg) = receiver.next().await { match msg { CoordinatorMsg::UpdatePeers(new_peers) => { let mut state = coordinator.state.lock().unwrap(); state.peers = new_peers; state.peer_set_id += 1; } } } }); sender } /// Updates the peers of the coordinator directly /// Note: Prefer using the update channel in multithreaded contexts pub fn set_peers(&self, new_peers: Vec

) { let mut state = self.state.lock().unwrap(); state.peers = new_peers; state.peer_set_id += 1; } // Helper to get a cloned vector to support the trait implementation fn get_peers(&self) -> Vec

{ let state = self.state.lock().unwrap(); state.peers.clone() } } impl crate::p2p::Coordinator for Coordinator

{ type PublicKey = P; fn peers(&self) -> &Vec { // Still using the hack for testing purposes let peers = self.get_peers(); Box::leak(Box::new(peers)) } fn peer_set_id(&self) -> u64 { let state = self.state.lock().unwrap(); state.peer_set_id } }