homestar_runtime/settings/
libp2p_config.rs

1//! libp2p configuration.
2
3use derive_builder::Builder;
4use http::Uri;
5use serde::{Deserialize, Serialize};
6use serde_with::{serde_as, DurationMilliSeconds, DurationSeconds};
7use std::time::Duration;
8
9/// libp2p settings.
10#[serde_as]
11#[derive(Builder, Clone, Debug, Serialize, Deserialize, PartialEq)]
12#[builder(default)]
13#[serde(default)]
14pub struct Libp2p {
15    /// Multiaddrs of the external addresses this node will announce to the
16    /// network.
17    #[serde_as(as = "Vec<serde_with::DisplayFromStr>")]
18    pub(crate) announce_addresses: Vec<libp2p::Multiaddr>,
19    /// Kademlia DHT Settings
20    pub(crate) dht: Dht,
21    #[serde_as(as = "DurationSeconds<u64>")]
22    pub(crate) idle_connection_timeout: Duration,
23    /// Address for [Swarm] to listen on.
24    ///
25    /// [Swarm]: libp2p::swarm::Swarm
26    #[serde(with = "http_serde::uri")]
27    pub(crate) listen_address: Uri,
28    /// Maximum number of peers we will dial.
29    pub(crate) max_connected_peers: u32,
30    /// Limit on the number of external addresses we announce to other peers.
31    pub(crate) max_announce_addresses: u32,
32    /// Multiaddrs of the trusted nodes to connect to on startup.
33    #[serde_as(as = "Vec<serde_with::DisplayFromStr>")]
34    pub(crate) node_addresses: Vec<libp2p::Multiaddr>,
35    /// Quic Settings.
36    pub(crate) quic: Quic,
37    /// mDNS Settings.
38    pub(crate) mdns: Mdns,
39    /// Pubsub Settings.
40    pub(crate) pubsub: Pubsub,
41    /// Rendezvous Settings.
42    pub(crate) rendezvous: Rendezvous,
43    /// Transport connection timeout.
44    #[serde_as(as = "DurationSeconds<u64>")]
45    pub(crate) transport_connection_timeout: Duration,
46    /// Dial interval.
47    #[serde_as(as = "DurationSeconds<u64>")]
48    pub(crate) dial_interval: Duration,
49    /// Bootstrap dial interval.
50    #[serde_as(as = "DurationSeconds<u64>")]
51    pub(crate) bootstrap_interval: Duration,
52}
53
54/// DHT settings.
55#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
56pub(crate) struct Quic {
57    /// Enable Quic transport.
58    pub(crate) enable: bool,
59}
60
61/// DHT settings.
62#[serde_as]
63#[derive(Builder, Clone, Debug, Serialize, Deserialize, PartialEq)]
64#[builder(default)]
65#[serde(default)]
66pub struct Dht {
67    /// Enable resolve receipts in background.
68    pub(crate) enable_resolve_receipts_in_background: bool,
69    /// Timeout for p2p receipt record lookups in milliseconds.
70    #[serde_as(as = "DurationMilliSeconds<u64>")]
71    pub(crate) p2p_receipt_timeout: Duration,
72    /// Timeout for p2p workflow info lookups in milliseconds.
73    #[serde_as(as = "DurationMilliSeconds<u64>")]
74    pub(crate) p2p_workflow_info_timeout: Duration,
75    /// Timeout for p2p provider workflow info lookups in milliseconds.
76    #[serde_as(as = "DurationMilliSeconds<u64>")]
77    pub(crate) p2p_provider_timeout: Duration,
78    /// Quorum for receipt records on the DHT.
79    pub(crate) receipt_quorum: usize,
80    /// Quorum for [workflow::Info] records on the DHT.
81    ///
82    /// [workflow::Info]: crate::workflow::Info
83    pub(crate) workflow_quorum: usize,
84}
85
86/// mDNS settings.
87#[serde_as]
88#[derive(Builder, Clone, Debug, Serialize, Deserialize, PartialEq)]
89#[builder(default)]
90#[serde(default)]
91pub struct Mdns {
92    /// Enable mDNS.
93    pub(crate) enable: bool,
94    /// mDNS IPv6 enable flag
95    pub(crate) enable_ipv6: bool,
96    /// mDNS query interval.
97    #[serde_as(as = "DurationSeconds<u64>")]
98    pub(crate) query_interval: Duration,
99    /// mDNS TTL.
100    #[serde_as(as = "DurationSeconds<u64>")]
101    pub(crate) ttl: Duration,
102}
103
104/// Pubsub settings.
105#[serde_as]
106#[derive(Builder, Clone, Debug, Serialize, Deserialize, PartialEq)]
107#[builder(default)]
108#[serde(default)]
109pub struct Pubsub {
110    /// Enable pub/sub.
111    pub(crate) enable: bool,
112    /// Pub/sub duplicate cache time.
113    #[serde_as(as = "DurationSeconds<u64>")]
114    pub(crate) duplication_cache_time: Duration,
115    /// Pub/sub hearbeat interval for mesh configuration.
116    #[serde_as(as = "DurationSeconds<u64>")]
117    pub(crate) heartbeat: Duration,
118    /// Maximum byte size of pub/sub messages.
119    pub(crate) max_transmit_size: usize,
120    /// Minimum number of pub/sub peers.
121    pub(crate) mesh_n_low: usize,
122    /// Maximum number of pub/sub peers.
123    pub(crate) mesh_n_high: usize,
124    /// Target number of pub/sub peers.
125    pub(crate) mesh_n: usize,
126    /// Minimum outbound pub/sub peers before adding more peers.
127    pub(crate) mesh_outbound_min: usize,
128}
129
130/// Rendezvous settings.
131#[serde_as]
132#[derive(Builder, Clone, Debug, Serialize, Deserialize, PartialEq)]
133#[builder(default)]
134#[serde(default)]
135pub struct Rendezvous {
136    /// Enable Rendezvous protocol client.
137    pub(crate) enable_client: bool,
138    /// Enable Rendezvous protocol server.
139    pub(crate) enable_server: bool,
140    /// Rendezvous registration TTL.
141    #[serde_as(as = "DurationSeconds<u64>")]
142    pub(crate) registration_ttl: Duration,
143    /// Rendezvous discovery interval.
144    #[serde_as(as = "DurationSeconds<u64>")]
145    pub(crate) discovery_interval: Duration,
146}
147
148impl Default for Libp2p {
149    fn default() -> Self {
150        Self {
151            announce_addresses: Vec::new(),
152            dht: Dht::default(),
153            // https://github.com/libp2p/rust-libp2p/pull/4967
154            // https://github.com/libp2p/rust-libp2p/pull/4887
155            idle_connection_timeout: Duration::new(60, 0),
156            listen_address: Uri::from_static("/ip4/0.0.0.0/tcp/0"),
157            max_connected_peers: 32,
158            max_announce_addresses: 10,
159            quic: Quic::default(),
160            mdns: Mdns::default(),
161            node_addresses: Vec::new(),
162            pubsub: Pubsub::default(),
163            rendezvous: Rendezvous::default(),
164            transport_connection_timeout: Duration::new(60, 0),
165            dial_interval: Duration::new(30, 0),
166            bootstrap_interval: Duration::new(30, 0),
167        }
168    }
169}
170
171impl Libp2p {
172    /// DHT settings getter.
173    pub(crate) fn dht(&self) -> &Dht {
174        &self.dht
175    }
176
177    /// Pub/sub settings getter.
178    pub(crate) fn pubsub(&self) -> &Pubsub {
179        &self.pubsub
180    }
181}
182
183impl Default for Dht {
184    fn default() -> Self {
185        Self {
186            enable_resolve_receipts_in_background: true,
187            p2p_receipt_timeout: Duration::from_millis(500),
188            p2p_workflow_info_timeout: Duration::from_millis(500),
189            p2p_provider_timeout: Duration::from_millis(10000),
190            receipt_quorum: 2,
191            workflow_quorum: 3,
192        }
193    }
194}
195
196impl Default for Quic {
197    fn default() -> Self {
198        Self { enable: true }
199    }
200}
201
202impl Default for Mdns {
203    fn default() -> Self {
204        Self {
205            enable: true,
206            enable_ipv6: false,
207            query_interval: Duration::from_secs(5 * 60),
208            ttl: Duration::from_secs(60 * 9),
209        }
210    }
211}
212
213impl Default for Pubsub {
214    fn default() -> Self {
215        Self {
216            enable: true,
217            duplication_cache_time: Duration::new(1, 0),
218            heartbeat: Duration::new(60, 0),
219            max_transmit_size: 10 * 1024 * 1024,
220            mesh_n_low: 1,
221            mesh_n_high: 10,
222            mesh_n: 2,
223            mesh_outbound_min: 1,
224        }
225    }
226}
227
228impl Default for Rendezvous {
229    fn default() -> Self {
230        Self {
231            enable_client: true,
232            enable_server: false,
233            registration_ttl: Duration::from_secs(2 * 60 * 60),
234            discovery_interval: Duration::from_secs(10 * 60),
235        }
236    }
237}