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 metrics;
33mod addresses;
34mod behaviour;
35mod contact;
36mod jobs;
37mod query;
38
39mod dht_proto {
40    include!(concat!(env!("OUT_DIR"), "/dht.pb.rs"));
41}
42
43pub use addresses::Addresses;
44pub use behaviour::{Kademlia, KademliaBucketInserts, KademliaConfig, KademliaEvent, Quorum};
45pub use behaviour::{
46    QueryRef,
47    QueryMut,
48
49    QueryResult,
50    QueryInfo,
51    QueryStats,
52
53    PeerRecord,
54
55    BootstrapResult,
56    BootstrapOk,
57    BootstrapError,
58
59    GetRecordResult,
60    GetRecordOk,
61    GetRecordError,
62
63    PutRecordPhase,
64    PutRecordContext,
65    PutRecordResult,
66    PutRecordOk,
67    PutRecordError,
68
69    GetClosestPeersResult,
70    GetClosestPeersOk,
71    GetClosestPeersError,
72
73    AddProviderPhase,
74    AddProviderContext,
75    AddProviderResult,
76    AddProviderOk,
77    AddProviderError,
78
79    GetProvidersResult,
80    GetProvidersOk,
81    GetProvidersError,
82};
83pub use protocol::KadConnectionType;
84pub use query::QueryId;
85pub use record::{store, ProviderRecord, Record};
86
87use std::num::NonZeroUsize;
88
89/// The `k` parameter of the Kademlia specification.
90///
91/// This parameter determines:
92///
93///   1) The (fixed) maximum number of nodes in a bucket.
94///   2) The (default) replication factor, which in turn determines:
95///       a) The number of closer peers returned in response to a request.
96///       b) The number of closest peers to a key to search for in an iterative query.
97///
98/// The choice of (1) is fixed to this constant. The replication factor is configurable
99/// but should generally be no greater than `K_VALUE`. All nodes in a Kademlia
100/// DHT should agree on the choices made for (1) and (2).
101///
102/// The current value is `20`.
103pub const K_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };
104
105/// Total number of weighted nodes in weighted bucket
106pub const W_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };
107
108/// The `α` parameter of the Kademlia specification.
109///
110/// This parameter determines the default parallelism for iterative queries,
111/// i.e. the allowed number of in-flight requests that an iterative query is
112/// waiting for at a particular time while it continues to make progress towards
113/// locating the closest peers to a key.
114///
115/// The current value is `3`.
116pub const ALPHA_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) };