protocols/kad: Require owned key in get_record (#2477)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Nazar Mokrynskyi
2022-02-07 23:56:30 +02:00
committed by GitHub
parent 78f5981856
commit bd41e0461e
5 changed files with 17 additions and 11 deletions

View File

@ -187,7 +187,7 @@ fn handle_input_line(kademlia: &mut Kademlia<MemoryStore>, line: String) {
} }
} }
}; };
kademlia.get_record(&key, Quorum::One); kademlia.get_record(key, Quorum::One);
} }
Some("GET_PROVIDERS") => { Some("GET_PROVIDERS") => {
let key = { let key = {

View File

@ -4,6 +4,10 @@
- Update to `libp2p-swarm` `v0.34.0`. - Update to `libp2p-swarm` `v0.34.0`.
- Require owned key in `get_record()` method (see [PR 2477]).
[PR 2477]: https://github.com/libp2p/rust-libp2p/pull/2477
# 0.34.0 [2022-01-27] # 0.34.0 [2022-01-27]
- Update dependencies. - Update dependencies.

View File

@ -679,13 +679,13 @@ where
/// ///
/// The result of this operation is delivered in a /// The result of this operation is delivered in a
/// [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetRecord}`]. /// [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetRecord}`].
pub fn get_record(&mut self, key: &record::Key, quorum: Quorum) -> QueryId { pub fn get_record(&mut self, key: record::Key, quorum: Quorum) -> QueryId {
let quorum = quorum.eval(self.queries.config().replication_factor); let quorum = quorum.eval(self.queries.config().replication_factor);
let mut records = Vec::with_capacity(quorum.get()); let mut records = Vec::with_capacity(quorum.get());
if let Some(record) = self.store.get(key) { if let Some(record) = self.store.get(&key) {
if record.is_expired(Instant::now()) { if record.is_expired(Instant::now()) {
self.store.remove(key) self.store.remove(&key)
} else { } else {
records.push(PeerRecord { records.push(PeerRecord {
peer: None, peer: None,
@ -697,7 +697,7 @@ where
let done = records.len() >= quorum.get(); let done = records.len() >= quorum.get();
let target = kbucket::Key::new(key.clone()); let target = kbucket::Key::new(key.clone());
let info = QueryInfo::GetRecord { let info = QueryInfo::GetRecord {
key: key.clone(), key,
records, records,
quorum, quorum,
cache_candidates: BTreeMap::new(), cache_candidates: BTreeMap::new(),
@ -1231,7 +1231,7 @@ where
if let Some(target) = remaining.next() { if let Some(target) = remaining.next() {
let info = QueryInfo::Bootstrap { let info = QueryInfo::Bootstrap {
peer: target.clone().into_preimage(), peer: *target.preimage(),
remaining: Some(remaining), remaining: Some(remaining),
}; };
let peers = self.kbuckets.closest_keys(&target); let peers = self.kbuckets.closest_keys(&target);

View File

@ -448,7 +448,7 @@ fn get_record_not_found() {
let target_key = record::Key::from(random_multihash()); let target_key = record::Key::from(random_multihash());
let qid = swarms[0] let qid = swarms[0]
.behaviour_mut() .behaviour_mut()
.get_record(&target_key, Quorum::One); .get_record(target_key.clone(), Quorum::One);
block_on(poll_fn(move |ctx| { block_on(poll_fn(move |ctx| {
for swarm in &mut swarms { for swarm in &mut swarms {
@ -761,7 +761,7 @@ fn get_record() {
swarms[2].behaviour_mut().store.put(record.clone()).unwrap(); swarms[2].behaviour_mut().store.put(record.clone()).unwrap();
let qid = swarms[0] let qid = swarms[0]
.behaviour_mut() .behaviour_mut()
.get_record(&record.key, Quorum::One); .get_record(record.key.clone(), Quorum::One);
block_on(poll_fn(move |ctx| { block_on(poll_fn(move |ctx| {
for swarm in &mut swarms { for swarm in &mut swarms {
@ -817,7 +817,9 @@ fn get_record_many() {
} }
let quorum = Quorum::N(NonZeroUsize::new(num_results).unwrap()); let quorum = Quorum::N(NonZeroUsize::new(num_results).unwrap());
let qid = swarms[0].behaviour_mut().get_record(&record.key, quorum); let qid = swarms[0]
.behaviour_mut()
.get_record(record.key.clone(), quorum);
block_on(poll_fn(move |ctx| { block_on(poll_fn(move |ctx| {
for swarm in &mut swarms { for swarm in &mut swarms {
@ -1116,7 +1118,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
let (mut alice, mut bob, mut trudy) = (alice.1, bob.1, trudy.1); let (mut alice, mut bob, mut trudy) = (alice.1, bob.1, trudy.1);
// Have `alice` query the Dht for `key` with a quorum of 1. // Have `alice` query the Dht for `key` with a quorum of 1.
alice.behaviour_mut().get_record(&key, Quorum::One); alice.behaviour_mut().get_record(key, Quorum::One);
// The default peer timeout is 10 seconds. Choosing 1 seconds here should // The default peer timeout is 10 seconds. Choosing 1 seconds here should
// give enough head room to prevent connections to `bob` to time out. // give enough head room to prevent connections to `bob` to time out.

View File

@ -240,7 +240,7 @@ where
/// increasing distance. /// increasing distance.
pub fn closest_keys<'a, T>(&'a mut self, target: &'a T) -> impl Iterator<Item = TKey> + 'a pub fn closest_keys<'a, T>(&'a mut self, target: &'a T) -> impl Iterator<Item = TKey> + 'a
where where
T: Clone + AsRef<KeyBytes>, T: AsRef<KeyBytes>,
{ {
let distance = self.local_key.as_ref().distance(target); let distance = self.local_key.as_ref().distance(target);
ClosestIter { ClosestIter {