cliquenet/
lib.rs

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