hotshot_new_protocol/
network.rs1pub mod cliquenet;
2
3use hotshot_types::{
4 data::{EpochNumber, ViewNumber},
5 epoch_membership::EpochMembershipCoordinator,
6 traits::node_implementation::NodeType,
7};
8
9use crate::message::{Message, Unchecked, Validated};
10
11type Result<T> = std::result::Result<T, NetworkError>;
12
13pub trait Network<T: NodeType> {
14 type PeerData;
15
16 fn broadcast(&mut self, v: ViewNumber, m: &Message<T, Validated>) -> Result<()>;
17
18 fn unicast(
19 &mut self,
20 v: ViewNumber,
21 to: &T::SignatureKey,
22 m: &Message<T, Validated>,
23 ) -> Result<()>;
24
25 fn multicast(
26 &mut self,
27 v: ViewNumber,
28 to: Vec<&T::SignatureKey>,
29 m: &Message<T, Validated>,
30 ) -> Result<()>;
31
32 fn receive(&mut self) -> impl Future<Output = Result<Message<T, Unchecked>>> + Send;
33
34 fn shutdown(&mut self) -> impl Future<Output = ()> + Send;
35
36 fn gc(&mut self, v: ViewNumber) -> Result<()>;
37
38 fn add_peers(&mut self, r: PeerRole, ps: Vec<(T::SignatureKey, Self::PeerData)>) -> Result<()>;
39 fn remove_peers(&mut self, ps: Vec<&T::SignatureKey>) -> Result<()>;
40 fn assign_role(&mut self, r: PeerRole, ps: Vec<&T::SignatureKey>) -> Result<()>;
41
42 fn apply_epoch(
44 &mut self,
45 epoch: EpochNumber,
46 coord: &EpochMembershipCoordinator<T>,
47 ) -> Result<()>;
48}
49
50#[derive(Clone, Copy, Debug)]
51pub enum PeerRole {
52 Active,
53 Passive,
54}
55
56#[derive(Debug, thiserror::Error)]
57pub enum NetworkError {
58 #[error("{0}")]
59 Io(#[source] Box<dyn std::error::Error + Send + Sync>),
60
61 #[error("{0}")]
62 Critical(#[source] Box<dyn std::error::Error + Send + Sync>),
63}
64
65impl NetworkError {
66 pub fn is_critical(&self) -> bool {
67 matches!(self, Self::Critical(_))
68 }
69}