Skip to content

Commit bd41e04

Browse files
nazar-pcmxinden
andauthored
protocols/kad: Require owned key in get_record (#2477)
Co-authored-by: Max Inden <[email protected]>
1 parent 78f5981 commit bd41e04

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

examples/distributed-key-value-store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn handle_input_line(kademlia: &mut Kademlia<MemoryStore>, line: String) {
187187
}
188188
}
189189
};
190-
kademlia.get_record(&key, Quorum::One);
190+
kademlia.get_record(key, Quorum::One);
191191
}
192192
Some("GET_PROVIDERS") => {
193193
let key = {

protocols/kad/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Update to `libp2p-swarm` `v0.34.0`.
66

7+
- Require owned key in `get_record()` method (see [PR 2477]).
8+
9+
[PR 2477]: https://github.com/libp2p/rust-libp2p/pull/2477
10+
711
# 0.34.0 [2022-01-27]
812

913
- Update dependencies.

protocols/kad/src/behaviour.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,13 @@ where
679679
///
680680
/// The result of this operation is delivered in a
681681
/// [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetRecord}`].
682-
pub fn get_record(&mut self, key: &record::Key, quorum: Quorum) -> QueryId {
682+
pub fn get_record(&mut self, key: record::Key, quorum: Quorum) -> QueryId {
683683
let quorum = quorum.eval(self.queries.config().replication_factor);
684684
let mut records = Vec::with_capacity(quorum.get());
685685

686-
if let Some(record) = self.store.get(key) {
686+
if let Some(record) = self.store.get(&key) {
687687
if record.is_expired(Instant::now()) {
688-
self.store.remove(key)
688+
self.store.remove(&key)
689689
} else {
690690
records.push(PeerRecord {
691691
peer: None,
@@ -697,7 +697,7 @@ where
697697
let done = records.len() >= quorum.get();
698698
let target = kbucket::Key::new(key.clone());
699699
let info = QueryInfo::GetRecord {
700-
key: key.clone(),
700+
key,
701701
records,
702702
quorum,
703703
cache_candidates: BTreeMap::new(),
@@ -1231,7 +1231,7 @@ where
12311231

12321232
if let Some(target) = remaining.next() {
12331233
let info = QueryInfo::Bootstrap {
1234-
peer: target.clone().into_preimage(),
1234+
peer: *target.preimage(),
12351235
remaining: Some(remaining),
12361236
};
12371237
let peers = self.kbuckets.closest_keys(&target);

protocols/kad/src/behaviour/test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ fn get_record_not_found() {
448448
let target_key = record::Key::from(random_multihash());
449449
let qid = swarms[0]
450450
.behaviour_mut()
451-
.get_record(&target_key, Quorum::One);
451+
.get_record(target_key.clone(), Quorum::One);
452452

453453
block_on(poll_fn(move |ctx| {
454454
for swarm in &mut swarms {
@@ -761,7 +761,7 @@ fn get_record() {
761761
swarms[2].behaviour_mut().store.put(record.clone()).unwrap();
762762
let qid = swarms[0]
763763
.behaviour_mut()
764-
.get_record(&record.key, Quorum::One);
764+
.get_record(record.key.clone(), Quorum::One);
765765

766766
block_on(poll_fn(move |ctx| {
767767
for swarm in &mut swarms {
@@ -817,7 +817,9 @@ fn get_record_many() {
817817
}
818818

819819
let quorum = Quorum::N(NonZeroUsize::new(num_results).unwrap());
820-
let qid = swarms[0].behaviour_mut().get_record(&record.key, quorum);
820+
let qid = swarms[0]
821+
.behaviour_mut()
822+
.get_record(record.key.clone(), quorum);
821823

822824
block_on(poll_fn(move |ctx| {
823825
for swarm in &mut swarms {
@@ -1116,7 +1118,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
11161118
let (mut alice, mut bob, mut trudy) = (alice.1, bob.1, trudy.1);
11171119

11181120
// Have `alice` query the Dht for `key` with a quorum of 1.
1119-
alice.behaviour_mut().get_record(&key, Quorum::One);
1121+
alice.behaviour_mut().get_record(key, Quorum::One);
11201122

11211123
// The default peer timeout is 10 seconds. Choosing 1 seconds here should
11221124
// give enough head room to prevent connections to `bob` to time out.

protocols/kad/src/kbucket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ where
240240
/// increasing distance.
241241
pub fn closest_keys<'a, T>(&'a mut self, target: &'a T) -> impl Iterator<Item = TKey> + 'a
242242
where
243-
T: Clone + AsRef<KeyBytes>,
243+
T: AsRef<KeyBytes>,
244244
{
245245
let distance = self.local_key.as_ref().distance(target);
246246
ClosestIter {

0 commit comments

Comments
 (0)