Skip to main content

bytesandbrains_core/
peer_sampling.rs

1use crate::index::OpRef;
2
3/// Capability trait for protocols that can sample peers from their view.
4///
5/// The selection strategy is passed as a parameter to each selection call,
6/// making the trait stateless with respect to selection mode.
7pub trait PeerSampling {
8    /// The peer type exposed by this protocol's view.
9    type Peer: Clone;
10
11    /// Immutable reference to the current peer view.
12    type PeerView<'a> where Self: 'a;
13
14    /// The type used to choose between peer selection strategies.
15    type SamplingMode;
16
17    /// Handle to an in-flight peer selection operation.
18    type SelectPeerRef<'a>: OpRef where Self: 'a;
19
20    /// Return an immutable reference to the current peer view.
21    fn view(&self) -> Self::PeerView<'_>;
22
23    /// Number of peers currently in the view.
24    fn view_len(&self) -> usize;
25
26    /// Select a single peer using the given selection strategy.
27    fn select_peer(&mut self, mode: &Self::SamplingMode) -> Self::SelectPeerRef<'_>;
28
29    /// Return all peers known to this sampler for broadcast operations.
30    ///
31    /// Unlike [`select_peer`](PeerSampling::select_peer) which returns a single peer,
32    /// this returns all peers in the view. Useful for protocols that need to
33    /// send messages to multiple peers (e.g., gossip push-pull).
34    ///
35    /// The caller is responsible for limiting fan-out if needed.
36    fn broadcast(&self) -> Vec<Self::Peer>;
37}