Skip to content

Commit 3165b12

Browse files
feat(kad): remove deprecated public modules
This patch removes the 3 out of 4 deprecated public modules. I've left `store` for now because we made some mistakes in declaring that. The items within `store` still need to be publicly visible but I haven't yet figured out a good way of exporting / naming them. Thus, I've left that to a follow-up PR. Related: #3647. Pull-Request: #3896.
1 parent 43c6d21 commit 3165b12

File tree

15 files changed

+144
-165
lines changed

15 files changed

+144
-165
lines changed

misc/metrics/src/identify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl super::Recorder<libp2p_identify::Event> for Metrics {
148148
libp2p_identify::PROTOCOL_NAME,
149149
libp2p_identify::PUSH_PROTOCOL_NAME,
150150
#[cfg(feature = "kad")]
151-
libp2p_kad::protocol::DEFAULT_PROTO_NAME,
151+
libp2p_kad::PROTOCOL_NAME,
152152
#[cfg(feature = "ping")]
153153
libp2p_ping::PROTOCOL_NAME,
154154
#[cfg(feature = "relay")]

protocols/kad/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
- Raise MSRV to 1.65.
44
See [PR 3715].
55

6+
- Remove deprecated public modules `handler`, `protocol` and `kbucket`.
7+
See [PR 3896].
8+
69
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
10+
[PR 3896]: https://github.com/libp2p/rust-libp2p/pull/3896
711

812
## 0.43.3
913

protocols/kad/src/behaviour.rs

Lines changed: 66 additions & 68 deletions
Large diffs are not rendered by default.

protocols/kad/src/behaviour/test.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
use super::*;
2424

25-
use crate::kbucket_priv::Distance;
25+
use crate::kbucket::Distance;
2626
use crate::record_priv::{store::MemoryStore, Key};
2727
use crate::{K_VALUE, SHA_256_MH};
2828
use futures::{executor::block_on, future::poll_fn, prelude::*};
@@ -234,10 +234,10 @@ fn bootstrap() {
234234

235235
#[test]
236236
fn query_iter() {
237-
fn distances<K>(key: &kbucket_priv::Key<K>, peers: Vec<PeerId>) -> Vec<Distance> {
237+
fn distances<K>(key: &kbucket::Key<K>, peers: Vec<PeerId>) -> Vec<Distance> {
238238
peers
239239
.into_iter()
240-
.map(kbucket_priv::Key::from)
240+
.map(kbucket::Key::from)
241241
.map(|k| k.distance(key))
242242
.collect()
243243
}
@@ -253,7 +253,7 @@ fn query_iter() {
253253
// Ask the first peer in the list to search a random peer. The search should
254254
// propagate forwards through the list of peers.
255255
let search_target = PeerId::random();
256-
let search_target_key = kbucket_priv::Key::from(search_target);
256+
let search_target_key = kbucket::Key::from(search_target);
257257
let qid = swarms[0].behaviour_mut().get_closest_peers(search_target);
258258

259259
match swarms[0].behaviour_mut().query(&qid) {
@@ -290,7 +290,7 @@ fn query_iter() {
290290
assert_eq!(swarm_ids[i], expected_swarm_id);
291291
assert_eq!(swarm.behaviour_mut().queries.size(), 0);
292292
assert!(expected_peer_ids.iter().all(|p| ok.peers.contains(p)));
293-
let key = kbucket_priv::Key::new(ok.key);
293+
let key = kbucket::Key::new(ok.key);
294294
assert_eq!(expected_distances, distances(&key, ok.peers));
295295
return Poll::Ready(());
296296
}
@@ -653,17 +653,17 @@ fn put_record() {
653653
assert_eq!(r.expires, expected.expires);
654654
assert_eq!(r.publisher, Some(*swarms[0].local_peer_id()));
655655

656-
let key = kbucket_priv::Key::new(r.key.clone());
656+
let key = kbucket::Key::new(r.key.clone());
657657
let mut expected = swarms
658658
.iter()
659659
.skip(1)
660660
.map(Swarm::local_peer_id)
661661
.cloned()
662662
.collect::<Vec<_>>();
663663
expected.sort_by(|id1, id2| {
664-
kbucket_priv::Key::from(*id1)
664+
kbucket::Key::from(*id1)
665665
.distance(&key)
666-
.cmp(&kbucket_priv::Key::from(*id2).distance(&key))
666+
.cmp(&kbucket::Key::from(*id2).distance(&key))
667667
});
668668

669669
let expected = expected
@@ -992,11 +992,11 @@ fn add_provider() {
992992
.map(Swarm::local_peer_id)
993993
.cloned()
994994
.collect::<Vec<_>>();
995-
let kbucket_key = kbucket_priv::Key::new(key);
995+
let kbucket_key = kbucket::Key::new(key);
996996
expected.sort_by(|id1, id2| {
997-
kbucket_priv::Key::from(*id1)
997+
kbucket::Key::from(*id1)
998998
.distance(&kbucket_key)
999-
.cmp(&kbucket_priv::Key::from(*id2).distance(&kbucket_key))
999+
.cmp(&kbucket::Key::from(*id2).distance(&kbucket_key))
10001000
});
10011001

10021002
let expected = expected
File renamed without changes.

protocols/kad/src/kbucket_priv.rs renamed to protocols/kad/src/kbucket.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const NUM_BUCKETS: usize = 256;
8484

8585
/// A `KBucketsTable` represents a Kademlia routing table.
8686
#[derive(Debug, Clone)]
87-
pub struct KBucketsTable<TKey, TVal> {
87+
pub(crate) struct KBucketsTable<TKey, TVal> {
8888
/// The key identifying the local peer that owns the routing table.
8989
local_key: TKey,
9090
/// The buckets comprising the routing table.
@@ -154,7 +154,7 @@ where
154154
/// The given `pending_timeout` specifies the duration after creation of
155155
/// a [`PendingEntry`] after which it becomes eligible for insertion into
156156
/// a full bucket, replacing the least-recently (dis)connected node.
157-
pub fn new(local_key: TKey, pending_timeout: Duration) -> Self {
157+
pub(crate) fn new(local_key: TKey, pending_timeout: Duration) -> Self {
158158
KBucketsTable {
159159
local_key,
160160
buckets: (0..NUM_BUCKETS)
@@ -165,13 +165,13 @@ where
165165
}
166166

167167
/// Returns the local key.
168-
pub fn local_key(&self) -> &TKey {
168+
pub(crate) fn local_key(&self) -> &TKey {
169169
&self.local_key
170170
}
171171

172172
/// Returns an `Entry` for the given key, representing the state of the entry
173173
/// in the routing table.
174-
pub fn entry<'a>(&'a mut self, key: &'a TKey) -> Entry<'a, TKey, TVal> {
174+
pub(crate) fn entry<'a>(&'a mut self, key: &'a TKey) -> Entry<'a, TKey, TVal> {
175175
let index = BucketIndex::new(&self.local_key.as_ref().distance(key));
176176
if let Some(i) = index {
177177
let bucket = &mut self.buckets[i.get()];
@@ -188,7 +188,7 @@ where
188188
///
189189
/// The buckets are ordered by proximity to the `local_key`, i.e. the first
190190
/// bucket is the closest bucket (containing at most one key).
191-
pub fn iter(&mut self) -> impl Iterator<Item = KBucketRef<'_, TKey, TVal>> + '_ {
191+
pub(crate) fn iter(&mut self) -> impl Iterator<Item = KBucketRef<'_, TKey, TVal>> + '_ {
192192
let applied_pending = &mut self.applied_pending;
193193
self.buckets.iter_mut().enumerate().map(move |(i, b)| {
194194
if let Some(applied) = b.apply_pending() {
@@ -204,7 +204,7 @@ where
204204
/// Returns the bucket for the distance to the given key.
205205
///
206206
/// Returns `None` if the given key refers to the local key.
207-
pub fn bucket<K>(&mut self, key: &K) -> Option<KBucketRef<'_, TKey, TVal>>
207+
pub(crate) fn bucket<K>(&mut self, key: &K) -> Option<KBucketRef<'_, TKey, TVal>>
208208
where
209209
K: AsRef<KeyBytes>,
210210
{
@@ -232,13 +232,16 @@ where
232232
/// buckets are updated accordingly. The fact that a pending entry was applied is
233233
/// recorded in the `KBucketsTable` in the form of `AppliedPending` results, which must be
234234
/// consumed by calling this function.
235-
pub fn take_applied_pending(&mut self) -> Option<AppliedPending<TKey, TVal>> {
235+
pub(crate) fn take_applied_pending(&mut self) -> Option<AppliedPending<TKey, TVal>> {
236236
self.applied_pending.pop_front()
237237
}
238238

239239
/// Returns an iterator over the keys closest to `target`, ordered by
240240
/// increasing distance.
241-
pub fn closest_keys<'a, T>(&'a mut self, target: &'a T) -> impl Iterator<Item = TKey> + 'a
241+
pub(crate) fn closest_keys<'a, T>(
242+
&'a mut self,
243+
target: &'a T,
244+
) -> impl Iterator<Item = TKey> + 'a
242245
where
243246
T: AsRef<KeyBytes>,
244247
{
@@ -256,7 +259,7 @@ where
256259

257260
/// Returns an iterator over the nodes closest to the `target` key, ordered by
258261
/// increasing distance.
259-
pub fn closest<'a, T>(
262+
pub(crate) fn closest<'a, T>(
260263
&'a mut self,
261264
target: &'a T,
262265
) -> impl Iterator<Item = EntryView<TKey, TVal>> + 'a
@@ -286,7 +289,7 @@ where
286289
///
287290
/// The number of nodes between the local node and the target are
288291
/// calculated by backtracking from the target towards the local key.
289-
pub fn count_nodes_between<T>(&mut self, target: &T) -> usize
292+
pub(crate) fn count_nodes_between<T>(&mut self, target: &T) -> usize
290293
where
291294
T: AsRef<KeyBytes>,
292295
{
@@ -460,7 +463,7 @@ where
460463
}
461464
}
462465

463-
/// A reference to a bucket in a [`KBucketsTable`].
466+
/// A reference to a bucket.
464467
pub struct KBucketRef<'a, TKey, TVal> {
465468
index: BucketIndex,
466469
bucket: &'a mut KBucket<TKey, TVal>,
@@ -471,7 +474,7 @@ where
471474
TKey: Clone + AsRef<KeyBytes>,
472475
TVal: Clone,
473476
{
474-
/// Returns the minimum inclusive and maximum inclusive [`Distance`] for
477+
/// Returns the minimum inclusive and maximum inclusive distance for
475478
/// this bucket.
476479
pub fn range(&self) -> (Distance, Distance) {
477480
self.index.range()

protocols/kad/src/kbucket_priv/bucket.rs renamed to protocols/kad/src/kbucket/bucket.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
//! > of the `KBucketsTable` and in particular the public `Entry` API.
2727
2828
use super::*;
29-
pub use crate::K_VALUE;
30-
29+
pub(crate) use crate::K_VALUE;
3130
/// A `PendingNode` is a `Node` that is pending insertion into a `KBucket`.
3231
#[derive(Debug, Clone)]
3332
pub(crate) struct PendingNode<TKey, TVal> {
@@ -130,7 +129,7 @@ pub(crate) struct KBucket<TKey, TVal> {
130129
/// The result of inserting an entry into a bucket.
131130
#[must_use]
132131
#[derive(Debug, Clone, PartialEq, Eq)]
133-
pub enum InsertResult<TKey> {
132+
pub(crate) enum InsertResult<TKey> {
134133
/// The entry has been successfully inserted.
135134
Inserted,
136135
/// The entry is pending insertion because the relevant bucket is currently full.
@@ -152,12 +151,12 @@ pub enum InsertResult<TKey> {
152151
/// The result of applying a pending node to a bucket, possibly
153152
/// replacing an existing node.
154153
#[derive(Debug, Clone, PartialEq, Eq)]
155-
pub struct AppliedPending<TKey, TVal> {
154+
pub(crate) struct AppliedPending<TKey, TVal> {
156155
/// The key of the inserted pending node.
157-
pub inserted: Node<TKey, TVal>,
156+
pub(crate) inserted: Node<TKey, TVal>,
158157
/// The node that has been evicted from the bucket to make room for the
159158
/// pending node, if any.
160-
pub evicted: Option<Node<TKey, TVal>>,
159+
pub(crate) evicted: Option<Node<TKey, TVal>>,
161160
}
162161

163162
impl<TKey, TVal> KBucket<TKey, TVal>

protocols/kad/src/kbucket_priv/entry.rs renamed to protocols/kad/src/kbucket/entry.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! The `Entry` API for quering and modifying the entries of a `KBucketsTable`
2222
//! representing the nodes participating in the Kademlia DHT.
2323
24-
pub use super::bucket::{AppliedPending, InsertResult, Node, NodeStatus, K_VALUE};
24+
pub(crate) use super::bucket::{AppliedPending, InsertResult, Node, NodeStatus, K_VALUE};
2525
pub use super::key::*;
2626

2727
use super::*;
@@ -74,7 +74,7 @@ impl<TKey: AsRef<KeyBytes>, TVal> AsRef<KeyBytes> for EntryView<TKey, TVal> {
7474

7575
/// A reference into a single entry of a `KBucketsTable`.
7676
#[derive(Debug)]
77-
pub enum Entry<'a, TPeerId, TVal> {
77+
pub(crate) enum Entry<'a, TPeerId, TVal> {
7878
/// The entry is present in a bucket.
7979
Present(PresentEntry<'a, TPeerId, TVal>, NodeStatus),
8080
/// The entry is pending insertion in a bucket.
@@ -115,7 +115,7 @@ where
115115
///
116116
/// Returns `None` if the entry is neither present in a bucket nor
117117
/// pending insertion into a bucket.
118-
pub fn view(&'a mut self) -> Option<EntryRefView<'a, TKey, TVal>> {
118+
pub(crate) fn view(&'a mut self) -> Option<EntryRefView<'a, TKey, TVal>> {
119119
match self {
120120
Entry::Present(entry, status) => Some(EntryRefView {
121121
node: NodeRefView {
@@ -140,7 +140,7 @@ where
140140
/// Returns `None` if the `Key` used to construct this `Entry` is not a valid
141141
/// key for an entry in a bucket, which is the case for the `local_key` of
142142
/// the `KBucketsTable` referring to the local node.
143-
pub fn key(&self) -> Option<&TKey> {
143+
pub(crate) fn key(&self) -> Option<&TKey> {
144144
match self {
145145
Entry::Present(entry, _) => Some(entry.key()),
146146
Entry::Pending(entry, _) => Some(entry.key()),
@@ -153,7 +153,7 @@ where
153153
///
154154
/// Returns `None` if the entry is absent from any bucket or refers to the
155155
/// local node.
156-
pub fn value(&mut self) -> Option<&mut TVal> {
156+
pub(crate) fn value(&mut self) -> Option<&mut TVal> {
157157
match self {
158158
Entry::Present(entry, _) => Some(entry.value()),
159159
Entry::Pending(entry, _) => Some(entry.value()),
@@ -165,8 +165,7 @@ where
165165

166166
/// An entry present in a bucket.
167167
#[derive(Debug)]
168-
pub struct PresentEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
169-
168+
pub(crate) struct PresentEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
170169
impl<'a, TKey, TVal> PresentEntry<'a, TKey, TVal>
171170
where
172171
TKey: Clone + AsRef<KeyBytes>,
@@ -177,12 +176,12 @@ where
177176
}
178177

179178
/// Returns the key of the entry.
180-
pub fn key(&self) -> &TKey {
179+
pub(crate) fn key(&self) -> &TKey {
181180
self.0.key
182181
}
183182

184183
/// Returns the value associated with the key.
185-
pub fn value(&mut self) -> &mut TVal {
184+
pub(crate) fn value(&mut self) -> &mut TVal {
186185
&mut self
187186
.0
188187
.bucket
@@ -192,12 +191,12 @@ where
192191
}
193192

194193
/// Sets the status of the entry to the provided [`NodeStatus`].
195-
pub fn update(&mut self, status: NodeStatus) {
194+
pub(crate) fn update(&mut self, status: NodeStatus) {
196195
self.0.bucket.update(self.0.key, status);
197196
}
198197

199198
/// Removes the entry from the bucket.
200-
pub fn remove(self) -> EntryView<TKey, TVal> {
199+
pub(crate) fn remove(self) -> EntryView<TKey, TVal> {
201200
let (node, status, _pos) = self
202201
.0
203202
.bucket
@@ -209,8 +208,7 @@ where
209208

210209
/// An entry waiting for a slot to be available in a bucket.
211210
#[derive(Debug)]
212-
pub struct PendingEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
213-
211+
pub(crate) struct PendingEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
214212
impl<'a, TKey, TVal> PendingEntry<'a, TKey, TVal>
215213
where
216214
TKey: Clone + AsRef<KeyBytes>,
@@ -221,12 +219,12 @@ where
221219
}
222220

223221
/// Returns the key of the entry.
224-
pub fn key(&self) -> &TKey {
222+
pub(crate) fn key(&self) -> &TKey {
225223
self.0.key
226224
}
227225

228226
/// Returns the value associated with the key.
229-
pub fn value(&mut self) -> &mut TVal {
227+
pub(crate) fn value(&mut self) -> &mut TVal {
230228
self.0
231229
.bucket
232230
.pending_mut()
@@ -235,13 +233,13 @@ where
235233
}
236234

237235
/// Updates the status of the pending entry.
238-
pub fn update(self, status: NodeStatus) -> PendingEntry<'a, TKey, TVal> {
236+
pub(crate) fn update(self, status: NodeStatus) -> PendingEntry<'a, TKey, TVal> {
239237
self.0.bucket.update_pending(status);
240238
PendingEntry::new(self.0.bucket, self.0.key)
241239
}
242240

243241
/// Removes the pending entry from the bucket.
244-
pub fn remove(self) -> EntryView<TKey, TVal> {
242+
pub(crate) fn remove(self) -> EntryView<TKey, TVal> {
245243
let pending = self.0.bucket.remove_pending().expect(
246244
"We can only build a PendingEntry if the entry is pending insertion
247245
into the bucket; QED",
@@ -254,8 +252,7 @@ where
254252

255253
/// An entry that is not present in any bucket.
256254
#[derive(Debug)]
257-
pub struct AbsentEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
258-
255+
pub(crate) struct AbsentEntry<'a, TKey, TVal>(EntryRef<'a, TKey, TVal>);
259256
impl<'a, TKey, TVal> AbsentEntry<'a, TKey, TVal>
260257
where
261258
TKey: Clone + AsRef<KeyBytes>,
@@ -266,12 +263,12 @@ where
266263
}
267264

268265
/// Returns the key of the entry.
269-
pub fn key(&self) -> &TKey {
266+
pub(crate) fn key(&self) -> &TKey {
270267
self.0.key
271268
}
272269

273270
/// Attempts to insert the entry into a bucket.
274-
pub fn insert(self, value: TVal, status: NodeStatus) -> InsertResult<TKey> {
271+
pub(crate) fn insert(self, value: TVal, status: NodeStatus) -> InsertResult<TKey> {
275272
self.0.bucket.insert(
276273
Node {
277274
key: self.0.key.clone(),
File renamed without changes.

0 commit comments

Comments
 (0)