litep2p/protocol/libp2p/kademlia/
config.rs1use crate::{
22 codec::ProtocolCodec,
23 protocol::libp2p::kademlia::{
24 handle::{
25 IncomingRecordValidationMode, KademliaCommand, KademliaEvent, KademliaHandle,
26 RoutingTableUpdateMode,
27 },
28 store::MemoryStoreConfig,
29 },
30 types::protocol::ProtocolName,
31 PeerId, DEFAULT_CHANNEL_SIZE,
32};
33
34use multiaddr::Multiaddr;
35use tokio::sync::mpsc::{channel, Receiver, Sender};
36
37use std::{
38 collections::HashMap,
39 sync::{atomic::AtomicUsize, Arc},
40 time::Duration,
41};
42
43const DEFAULT_TTL: Duration = Duration::from_secs(36 * 60 * 60);
45
46pub(super) const DEFAULT_MAX_RECORDS: usize = 1024;
48
49pub(super) const DEFAULT_MAX_RECORD_SIZE_BYTES: usize = 65 * 1024;
51
52pub(super) const DEFAULT_MAX_PROVIDER_KEYS: usize = 1024;
54
55pub(super) const DEFAULT_MAX_PROVIDER_ADDRESSES: usize = 30;
57
58pub(super) const DEFAULT_MAX_PROVIDERS_PER_KEY: usize = 20;
60
61pub(super) const DEFAULT_PROVIDER_REFRESH_INTERVAL: Duration = Duration::from_secs(22 * 60 * 60);
63
64pub(super) const DEFAULT_PROVIDER_TTL: Duration = Duration::from_secs(48 * 60 * 60);
66
67const PROTOCOL_NAME: &str = "/ipfs/kad/1.0.0";
69
70const REPLICATION_FACTOR: usize = 20usize;
72
73const DEFAULT_MAX_MESSAGE_SIZE: usize = 70 * 1024;
75
76#[derive(Debug)]
78pub struct Config {
79 pub(crate) protocol_names: Vec<ProtocolName>,
83
84 pub(crate) codec: ProtocolCodec,
86
87 pub(super) replication_factor: usize,
89
90 pub(super) known_peers: HashMap<PeerId, Vec<Multiaddr>>,
92
93 pub(super) update_mode: RoutingTableUpdateMode,
95
96 pub(super) validation_mode: IncomingRecordValidationMode,
98
99 pub(super) record_ttl: Duration,
101
102 pub(super) memory_store_config: MemoryStoreConfig,
104
105 pub(super) event_tx: Sender<KademliaEvent>,
107
108 pub(super) cmd_rx: Receiver<KademliaCommand>,
110
111 pub(super) next_query_id: Arc<AtomicUsize>,
113}
114
115impl Config {
116 fn new(
117 replication_factor: usize,
118 known_peers: HashMap<PeerId, Vec<Multiaddr>>,
119 mut protocol_names: Vec<ProtocolName>,
120 update_mode: RoutingTableUpdateMode,
121 validation_mode: IncomingRecordValidationMode,
122 record_ttl: Duration,
123 memory_store_config: MemoryStoreConfig,
124 max_message_size: usize,
125 ) -> (Self, KademliaHandle) {
126 let (cmd_tx, cmd_rx) = channel(DEFAULT_CHANNEL_SIZE);
127 let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
128 let next_query_id = Arc::new(AtomicUsize::new(0usize));
129
130 if protocol_names.is_empty() {
132 protocol_names.push(ProtocolName::from(PROTOCOL_NAME));
133 }
134
135 (
136 Config {
137 protocol_names,
138 update_mode,
139 validation_mode,
140 record_ttl,
141 memory_store_config,
142 codec: ProtocolCodec::UnsignedVarint(Some(max_message_size)),
143 replication_factor,
144 known_peers,
145 cmd_rx,
146 event_tx,
147 next_query_id: next_query_id.clone(),
148 },
149 KademliaHandle::new(cmd_tx, event_rx, next_query_id),
150 )
151 }
152
153 pub fn default() -> (Self, KademliaHandle) {
155 Self::new(
156 REPLICATION_FACTOR,
157 HashMap::new(),
158 Vec::new(),
159 RoutingTableUpdateMode::Automatic,
160 IncomingRecordValidationMode::Automatic,
161 DEFAULT_TTL,
162 Default::default(),
163 DEFAULT_MAX_MESSAGE_SIZE,
164 )
165 }
166}
167
168#[derive(Debug)]
170pub struct ConfigBuilder {
171 pub(super) replication_factor: usize,
173
174 pub(super) update_mode: RoutingTableUpdateMode,
176
177 pub(super) validation_mode: IncomingRecordValidationMode,
179
180 pub(super) known_peers: HashMap<PeerId, Vec<Multiaddr>>,
182
183 pub(super) protocol_names: Vec<ProtocolName>,
185
186 pub(super) record_ttl: Duration,
188
189 pub(super) memory_store_config: MemoryStoreConfig,
191
192 pub(crate) max_message_size: usize,
194}
195
196impl Default for ConfigBuilder {
197 fn default() -> Self {
198 Self::new()
199 }
200}
201
202impl ConfigBuilder {
203 pub fn new() -> Self {
205 Self {
206 replication_factor: REPLICATION_FACTOR,
207 known_peers: HashMap::new(),
208 protocol_names: Vec::new(),
209 update_mode: RoutingTableUpdateMode::Automatic,
210 validation_mode: IncomingRecordValidationMode::Automatic,
211 record_ttl: DEFAULT_TTL,
212 memory_store_config: Default::default(),
213 max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
214 }
215 }
216
217 pub fn with_replication_factor(mut self, replication_factor: usize) -> Self {
219 self.replication_factor = replication_factor;
220 self
221 }
222
223 pub fn with_known_peers(mut self, peers: HashMap<PeerId, Vec<Multiaddr>>) -> Self {
225 self.known_peers = peers;
226 self
227 }
228
229 pub fn with_routing_table_update_mode(mut self, mode: RoutingTableUpdateMode) -> Self {
231 self.update_mode = mode;
232 self
233 }
234
235 pub fn with_incoming_records_validation_mode(
237 mut self,
238 mode: IncomingRecordValidationMode,
239 ) -> Self {
240 self.validation_mode = mode;
241 self
242 }
243
244 pub fn with_protocol_names(mut self, protocol_names: Vec<ProtocolName>) -> Self {
254 self.protocol_names = protocol_names;
255 self
256 }
257
258 pub fn with_record_ttl(mut self, record_ttl: Duration) -> Self {
262 self.record_ttl = record_ttl;
263 self
264 }
265
266 pub fn with_max_records(mut self, max_records: usize) -> Self {
270 self.memory_store_config.max_records = max_records;
271 self
272 }
273
274 pub fn with_max_record_size(mut self, max_record_size_bytes: usize) -> Self {
278 self.memory_store_config.max_record_size_bytes = max_record_size_bytes;
279 self
280 }
281
282 pub fn with_max_provider_keys(mut self, max_provider_keys: usize) -> Self {
286 self.memory_store_config.max_provider_keys = max_provider_keys;
287 self
288 }
289
290 pub fn with_max_provider_addresses(mut self, max_provider_addresses: usize) -> Self {
294 self.memory_store_config.max_provider_addresses = max_provider_addresses;
295 self
296 }
297
298 pub fn with_max_providers_per_key(mut self, max_providers_per_key: usize) -> Self {
302 self.memory_store_config.max_providers_per_key = max_providers_per_key;
303 self
304 }
305
306 pub fn with_provider_record_ttl(mut self, provider_record_ttl: Duration) -> Self {
310 self.memory_store_config.provider_ttl = provider_record_ttl;
311 self
312 }
313
314 pub fn with_provider_refresh_interval(mut self, provider_refresh_interval: Duration) -> Self {
318 self.memory_store_config.provider_refresh_interval = provider_refresh_interval;
319 self
320 }
321
322 pub fn with_max_message_size(mut self, max_message_size: usize) -> Self {
327 self.max_message_size = max_message_size;
328 self
329 }
330
331 pub fn build(self) -> (Config, KademliaHandle) {
333 Config::new(
334 self.replication_factor,
335 self.known_peers,
336 self.protocol_names,
337 self.update_mode,
338 self.validation_mode,
339 self.record_ttl,
340 self.memory_store_config,
341 self.max_message_size,
342 )
343 }
344}