Broadcaster

Struct Broadcaster 

Source
pub struct Broadcaster {
    replica_id: String,
    config: BroadcastConfig,
    peers: BTreeSet<String>,
    seen: HashSet<Hash>,
    seen_order: VecDeque<Hash>,
    timestamp: u64,
    pending_events: VecDeque<BroadcastEvent>,
    peer_heads: HashMap<String, HashSet<Hash>>,
}
Expand description

Gossip-based broadcaster for head dissemination.

The broadcaster maintains:

  • A set of known peers
  • A buffer of seen message IDs (for deduplication)
  • Pending outgoing messages

Fields§

§replica_id: String

Our replica ID.

§config: BroadcastConfig

Configuration.

§peers: BTreeSet<String>

Known peers (BTreeSet for deterministic iteration order).

§seen: HashSet<Hash>

Message IDs we’ve seen (for deduplication).

§seen_order: VecDeque<Hash>

Order of seen messages (for LRU eviction).

§timestamp: u64

Current logical timestamp.

§pending_events: VecDeque<BroadcastEvent>

Pending events to be processed.

§peer_heads: HashMap<String, HashSet<Hash>>

Track which peers have which heads (optimization).

Implementations§

Source§

impl Broadcaster

Source

pub fn new(replica_id: impl Into<String>) -> Self

Create a new broadcaster.

Source

pub fn with_config( replica_id: impl Into<String>, config: BroadcastConfig, ) -> Self

Create a broadcaster with custom configuration.

Source

pub fn replica_id(&self) -> &str

Get our replica ID.

Source

pub fn add_peer(&mut self, peer: impl Into<String>)

Add a peer.

Source

pub fn remove_peer(&mut self, peer: &str)

Remove a peer.

Source

pub fn peers(&self) -> impl Iterator<Item = &String>

Get all known peers.

Source

pub fn broadcast(&mut self, heads: Vec<Hash>)

Broadcast new heads to peers.

Source

pub fn receive(&mut self, from: impl Into<String>, message: BroadcastMessage)

Receive a message from a peer.

Source

pub fn poll_event(&mut self) -> Option<BroadcastEvent>

Get the next pending event.

Source

pub fn has_pending_events(&self) -> bool

Check if there are pending events.

Source

pub fn drain_events(&mut self) -> Vec<BroadcastEvent>

Get all pending events.

Source

fn mark_seen(&mut self, id: Hash)

Mark a message as seen.

Source

fn select_peers(&self, n: usize) -> Vec<String>

Select n random peers.

Source

fn select_peers_excluding(&self, n: usize, exclude: &[&str]) -> Vec<String>

Select n random peers, excluding some.

Source

pub fn stats(&self) -> BroadcastStats

Get statistics about the broadcaster.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.