Skip to content

Commit

Permalink
protocols/kad: Check local store on get_providers (libp2p#2221)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartnelson3 authored Sep 27, 2021
1 parent d93a5ab commit c13f033
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
- Introduce `KademliaStoreInserts` option, which allows to filter records (see
[PR 2163]).

- Check local store when calling `Kademlia::get_providers` (see [PR 2221]).

[PR 2163]: https://github.com/libp2p/rust-libp2p/pull/2163
[PR 2221]: https://github.com/libp2p/rust-libp2p/pull/2163

# 0.31.0 [2021-07-12]

Expand Down
9 changes: 8 additions & 1 deletion protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,16 @@ where
/// The result of this operation is delivered in a
/// reported via [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetProviders}`].
pub fn get_providers(&mut self, key: record::Key) -> QueryId {
let providers = self
.store
.providers(&key)
.into_iter()
.filter(|p| !p.is_expired(Instant::now()))
.map(|p| p.provider)
.collect();
let info = QueryInfo::GetProviders {
key: key.clone(),
providers: HashSet::new(),
providers,
};
let target = kbucket::Key::new(key);
let peers = self.kbuckets.closest_keys(&target);
Expand Down
48 changes: 48 additions & 0 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,3 +1317,51 @@ fn network_behaviour_inject_address_change() {
kademlia.addresses_of_peer(&remote_peer_id),
);
}

#[test]
fn get_providers() {
fn prop(key: record::Key) {
let (_, mut single_swarm) = build_node();
single_swarm
.behaviour_mut()
.start_providing(key.clone())
.expect("could not provide");

block_on(async {
match single_swarm.next().await.unwrap() {
SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
result: QueryResult::StartProviding(Ok(_)),
..
}) => {}
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {:?}", e),
_ => {}
}
});

let query_id = single_swarm.behaviour_mut().get_providers(key.clone());

block_on(async {
match single_swarm.next().await.unwrap() {
SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id,
result:
QueryResult::GetProviders(Ok(GetProvidersOk {
key: found_key,
providers,
..
})),
..
}) if id == query_id => {
assert_eq!(key, found_key);
assert_eq!(
single_swarm.local_peer_id(),
providers.iter().next().unwrap()
);
}
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {:?}", e),
_ => {}
}
});
}
QuickCheck::new().tests(10).quickcheck(prop as fn(_))
}

0 comments on commit c13f033

Please sign in to comment.