litep2p/protocol/libp2p/kademlia/
config.rs1use crate::{
22 codec::ProtocolCodec,
23 protocol::libp2p::kademlia::handle::{
24 IncomingRecordValidationMode, KademliaCommand, KademliaEvent, KademliaHandle,
25 RoutingTableUpdateMode,
26 },
27 types::protocol::ProtocolName,
28 PeerId, DEFAULT_CHANNEL_SIZE,
29};
30
31use multiaddr::Multiaddr;
32use tokio::sync::mpsc::{channel, Receiver, Sender};
33
34use std::{collections::HashMap, time::Duration};
35
36const DEFAULT_TTL: Duration = Duration::from_secs(36 * 60 * 60);
38
39const DEFAULT_PROVIDER_TTL: Duration = Duration::from_secs(48 * 60 * 60);
41
42const PROTOCOL_NAME: &str = "/ipfs/kad/1.0.0";
44
45const REPLICATION_FACTOR: usize = 20usize;
47
48#[derive(Debug)]
50pub struct Config {
51 pub(crate) protocol_names: Vec<ProtocolName>,
55
56 pub(crate) codec: ProtocolCodec,
58
59 pub(super) replication_factor: usize,
61
62 pub(super) known_peers: HashMap<PeerId, Vec<Multiaddr>>,
64
65 pub(super) update_mode: RoutingTableUpdateMode,
67
68 pub(super) validation_mode: IncomingRecordValidationMode,
70
71 pub(super) record_ttl: Duration,
73
74 pub(super) provider_ttl: Duration,
76
77 pub(super) event_tx: Sender<KademliaEvent>,
79
80 pub(super) cmd_rx: Receiver<KademliaCommand>,
82}
83
84impl Config {
85 fn new(
86 replication_factor: usize,
87 known_peers: HashMap<PeerId, Vec<Multiaddr>>,
88 mut protocol_names: Vec<ProtocolName>,
89 update_mode: RoutingTableUpdateMode,
90 validation_mode: IncomingRecordValidationMode,
91 record_ttl: Duration,
92 provider_ttl: Duration,
93 ) -> (Self, KademliaHandle) {
94 let (cmd_tx, cmd_rx) = channel(DEFAULT_CHANNEL_SIZE);
95 let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
96
97 if protocol_names.is_empty() {
99 protocol_names.push(ProtocolName::from(PROTOCOL_NAME));
100 }
101
102 (
103 Config {
104 protocol_names,
105 update_mode,
106 validation_mode,
107 record_ttl,
108 provider_ttl,
109 codec: ProtocolCodec::UnsignedVarint(None),
110 replication_factor,
111 known_peers,
112 cmd_rx,
113 event_tx,
114 },
115 KademliaHandle::new(cmd_tx, event_rx),
116 )
117 }
118
119 pub fn default() -> (Self, KademliaHandle) {
121 Self::new(
122 REPLICATION_FACTOR,
123 HashMap::new(),
124 Vec::new(),
125 RoutingTableUpdateMode::Automatic,
126 IncomingRecordValidationMode::Automatic,
127 DEFAULT_TTL,
128 DEFAULT_PROVIDER_TTL,
129 )
130 }
131}
132
133#[derive(Debug)]
135pub struct ConfigBuilder {
136 pub(super) replication_factor: usize,
138
139 pub(super) update_mode: RoutingTableUpdateMode,
141
142 pub(super) validation_mode: IncomingRecordValidationMode,
144
145 pub(super) known_peers: HashMap<PeerId, Vec<Multiaddr>>,
147
148 pub(super) protocol_names: Vec<ProtocolName>,
150
151 pub(super) record_ttl: Duration,
153
154 pub(super) provider_ttl: Duration,
156}
157
158impl Default for ConfigBuilder {
159 fn default() -> Self {
160 Self::new()
161 }
162}
163
164impl ConfigBuilder {
165 pub fn new() -> Self {
167 Self {
168 replication_factor: REPLICATION_FACTOR,
169 known_peers: HashMap::new(),
170 protocol_names: Vec::new(),
171 update_mode: RoutingTableUpdateMode::Automatic,
172 validation_mode: IncomingRecordValidationMode::Automatic,
173 record_ttl: DEFAULT_TTL,
174 provider_ttl: DEFAULT_PROVIDER_TTL,
175 }
176 }
177
178 pub fn with_replication_factor(mut self, replication_factor: usize) -> Self {
180 self.replication_factor = replication_factor;
181 self
182 }
183
184 pub fn with_known_peers(mut self, peers: HashMap<PeerId, Vec<Multiaddr>>) -> Self {
186 self.known_peers = peers;
187 self
188 }
189
190 pub fn with_routing_table_update_mode(mut self, mode: RoutingTableUpdateMode) -> Self {
192 self.update_mode = mode;
193 self
194 }
195
196 pub fn with_incoming_records_validation_mode(
198 mut self,
199 mode: IncomingRecordValidationMode,
200 ) -> Self {
201 self.validation_mode = mode;
202 self
203 }
204
205 pub fn with_protocol_names(mut self, protocol_names: Vec<ProtocolName>) -> Self {
215 self.protocol_names = protocol_names;
216 self
217 }
218
219 pub fn with_record_ttl(mut self, record_ttl: Duration) -> Self {
223 self.record_ttl = record_ttl;
224 self
225 }
226
227 pub fn with_provider_record_ttl(mut self, provider_record_ttl: Duration) -> Self {
231 self.provider_ttl = provider_record_ttl;
232 self
233 }
234
235 pub fn build(self) -> (Config, KademliaHandle) {
237 Config::new(
238 self.replication_factor,
239 self.known_peers,
240 self.protocol_names,
241 self.update_mode,
242 self.validation_mode,
243 self.record_ttl,
244 self.provider_ttl,
245 )
246 }
247}