cliquenet/
lib.rs

1mod chan;
2mod error;
3mod frame;
4mod id;
5mod net;
6mod time;
7
8#[cfg(feature = "metrics")]
9mod metrics;
10
11pub mod retry;
12
13use std::{fmt, sync::Arc};
14
15use bon::Builder;
16pub use error::{NetworkDown, NetworkError};
17#[cfg(feature = "metrics")]
18use hotshot_types::traits::metrics::Metrics;
19use hotshot_types::{
20    addr::NetAddr,
21    x25519::{Keypair, PublicKey},
22};
23pub use id::Id;
24pub use net::Network;
25pub use retry::Retry;
26use tokio::sync::Semaphore;
27
28/// Max. number of bytes for a message (potentially consisting of several frames).
29pub const MAX_MESSAGE_SIZE: usize = 8 * 1024 * 1024;
30
31const NUM_DELAYS: usize = 5;
32const LAST_DELAY: usize = NUM_DELAYS - 1;
33
34/// Network peer role.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub enum Role {
37    /// Active peers receive broadcast messages.
38    Active,
39    /// Passive peers are excluded from broadcasts.
40    ///
41    /// Note however that passive peers can be addressed directly in
42    /// unicast or multicast operations.
43    Passive,
44}
45
46impl Role {
47    pub fn is_active(self) -> bool {
48        matches!(self, Self::Active)
49    }
50}
51
52impl fmt::Display for Role {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::Active => f.write_str("active"),
56            Self::Passive => f.write_str("passive"),
57        }
58    }
59}
60
61#[derive(Debug, Builder)]
62pub struct NetConf<K> {
63    /// Network name.
64    name: &'static str,
65
66    /// Network public key.
67    label: K,
68
69    /// DH keypair
70    keypair: Keypair,
71
72    /// Address to bind to.
73    bind: NetAddr,
74
75    /// Committee members with key material and bind address.
76    #[builder(with = <_>::from_iter)]
77    parties: Vec<(K, PublicKey, NetAddr)>,
78
79    /// Total egress channel capacity.
80    #[builder(default = 64 * parties.len())]
81    total_capacity_egress: usize,
82
83    /// Total ingress channel capacity.
84    #[builder(default = 32 * parties.len())]
85    total_capacity_ingress: usize,
86
87    /// Egress channel capacity per peer.
88    #[builder(default = 64)]
89    peer_capacity_egress: usize,
90
91    /// Ingress channel capacity per peer.
92    #[builder(default = 32)]
93    peer_capacity_ingress: usize,
94
95    /// Max. number of bytes per message to send or receive.
96    #[builder(default = MAX_MESSAGE_SIZE)]
97    max_message_size: usize,
98
99    /// Default retry delays in seconds.
100    #[builder(default = [1, 3, 5, 15, 30])]
101    retry_delays: [u8; NUM_DELAYS],
102
103    #[cfg(feature = "metrics")]
104    metrics: Box<dyn Metrics>,
105}
106
107impl<K> NetConf<K> {
108    fn new_budget(&self) -> Arc<Semaphore> {
109        Arc::new(Semaphore::new(self.peer_capacity_ingress))
110    }
111}