cs_mwc_libp2p_kad/lib.rs
1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Implementation of the libp2p-specific Kademlia protocol.
22
23// TODO: we allow dead_code for now because this library contains a lot of unused code that will
24// be useful later for record store
25#![allow(dead_code)]
26
27pub mod handler;
28pub mod kbucket;
29pub mod protocol;
30pub mod record;
31
32mod addresses;
33mod behaviour;
34mod jobs;
35mod query;
36
37mod dht_proto {
38 include!(concat!(env!("OUT_DIR"), "/dht.pb.rs"));
39}
40
41pub use addresses::Addresses;
42pub use behaviour::{Kademlia, KademliaBucketInserts, KademliaConfig, KademliaEvent, Quorum};
43pub use behaviour::{
44 QueryRef,
45 QueryMut,
46
47 QueryResult,
48 QueryInfo,
49 QueryStats,
50
51 PeerRecord,
52
53 BootstrapResult,
54 BootstrapOk,
55 BootstrapError,
56
57 GetRecordResult,
58 GetRecordOk,
59 GetRecordError,
60
61 PutRecordPhase,
62 PutRecordContext,
63 PutRecordResult,
64 PutRecordOk,
65 PutRecordError,
66
67 GetClosestPeersResult,
68 GetClosestPeersOk,
69 GetClosestPeersError,
70
71 AddProviderPhase,
72 AddProviderContext,
73 AddProviderResult,
74 AddProviderOk,
75 AddProviderError,
76
77 GetProvidersResult,
78 GetProvidersOk,
79 GetProvidersError,
80};
81pub use query::QueryId;
82pub use protocol::KadConnectionType;
83pub use record::{store, Record, ProviderRecord};
84
85use std::num::NonZeroUsize;
86
87/// The `k` parameter of the Kademlia specification.
88///
89/// This parameter determines:
90///
91/// 1) The (fixed) maximum number of nodes in a bucket.
92/// 2) The (default) replication factor, which in turn determines:
93/// a) The number of closer peers returned in response to a request.
94/// b) The number of closest peers to a key to search for in an iterative query.
95///
96/// The choice of (1) is fixed to this constant. The replication factor is configurable
97/// but should generally be no greater than `K_VALUE`. All nodes in a Kademlia
98/// DHT should agree on the choices made for (1) and (2).
99///
100/// The current value is `20`.
101pub const K_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };
102
103/// The `α` parameter of the Kademlia specification.
104///
105/// This parameter determines the default parallelism for iterative queries,
106/// i.e. the allowed number of in-flight requests that an iterative query is
107/// waiting for at a particular time while it continues to make progress towards
108/// locating the closest peers to a key.
109///
110/// The current value is `3`.
111pub const ALPHA_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) };