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
28pub const MAX_MESSAGE_SIZE: usize = 8 * 1024 * 1024;
30
31const NUM_DELAYS: usize = 5;
32const LAST_DELAY: usize = NUM_DELAYS - 1;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub enum Role {
37 Active,
39 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 name: &'static str,
65
66 label: K,
68
69 keypair: Keypair,
71
72 bind: NetAddr,
74
75 #[builder(with = <_>::from_iter)]
77 parties: Vec<(K, PublicKey, NetAddr)>,
78
79 #[builder(default = 64 * parties.len())]
81 total_capacity_egress: usize,
82
83 #[builder(default = 32 * parties.len())]
85 total_capacity_ingress: usize,
86
87 #[builder(default = 64)]
89 peer_capacity_egress: usize,
90
91 #[builder(default = 32)]
93 peer_capacity_ingress: usize,
94
95 #[builder(default = MAX_MESSAGE_SIZE)]
97 max_message_size: usize,
98
99 #[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}