Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/kad: Extend emitted events with basic information #2087

Merged
merged 11 commits into from
Jun 28, 2021
2 changes: 1 addition & 1 deletion examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Called when `kademlia` produces an event.
fn inject_event(&mut self, message: KademliaEvent) {
match message {
KademliaEvent::QueryResult { result, .. } => match result {
KademliaEvent::OutboundQueryCompleted { result, .. } => match result {
QueryResult::GetProviders(Ok(ok)) => {
for peer in ok.providers {
println!(
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
task::block_on(async move {
loop {
let event = swarm.select_next_some().await;
if let SwarmEvent::Behaviour(KademliaEvent::QueryResult {
if let SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
result: QueryResult::GetClosestPeers(result),
..
}) = event {
Expand Down
11 changes: 11 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

- Update dependencies.

- Expose inbound request information (see [PR 2087]). Note:
`KademliaEvent::QueryResult` is renamed to
`KademliaEvent::OutboundQueryCompleted`.

- Expose whether `KademliaEvent::RoutingUpdated` is triggered with new peer (see
[PR 2087]).

- Expose kbucket range on `KademliaEvent::RoutingUpdated` (see [PR 2087]).

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

# 0.30.0 [2021-04-13]

- Update `libp2p-swarm`.
Expand Down
160 changes: 130 additions & 30 deletions protocols/kad/src/behaviour.rs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn bootstrap() {
for (i, swarm) in swarms.iter_mut().enumerate() {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::Bootstrap(Ok(ok)), ..
}))) => {
assert_eq!(id, qid);
Expand Down Expand Up @@ -265,7 +265,7 @@ fn query_iter() {
for (i, swarm) in swarms.iter_mut().enumerate() {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::GetClosestPeers(Ok(ok)), ..
}))) => {
assert_eq!(id, qid);
Expand Down Expand Up @@ -318,7 +318,7 @@ fn unresponsive_not_returned_direct() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
result: QueryResult::GetClosestPeers(Ok(ok)), ..
}))) => {
assert_eq!(&ok.key[..], search_target.to_bytes().as_slice());
Expand Down Expand Up @@ -368,7 +368,7 @@ fn unresponsive_not_returned_indirect() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
result: QueryResult::GetClosestPeers(Ok(ok)), ..
}))) => {
assert_eq!(&ok.key[..], search_target.to_bytes().as_slice());
Expand Down Expand Up @@ -412,7 +412,7 @@ fn get_record_not_found() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::GetRecord(Err(e)), ..
}))) => {
assert_eq!(id, qid);
Expand Down Expand Up @@ -519,10 +519,10 @@ fn put_record() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::PutRecord(res), stats
}))) |
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::RepublishRecord(res), stats
}))) => {
assert!(qids.is_empty() || qids.remove(&id));
Expand Down Expand Up @@ -652,7 +652,7 @@ fn get_record() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::GetRecord(Ok(GetRecordOk {
records, cache_candidates
Expand Down Expand Up @@ -702,7 +702,7 @@ fn get_record_many() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::GetRecord(Ok(GetRecordOk { records, .. })),
..
Expand Down Expand Up @@ -784,10 +784,10 @@ fn add_provider() {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::StartProviding(res), ..
}))) |
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
id, result: QueryResult::RepublishProvider(res), ..
}))) => {
assert!(qids.is_empty() || qids.remove(&id));
Expand Down Expand Up @@ -903,7 +903,7 @@ fn exceed_jobs_max_queries() {
loop {
if let Poll::Ready(Some(e)) = swarm.poll_next_unpin(ctx) {
match e {
SwarmEvent::Behaviour(KademliaEvent::QueryResult {
SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted {
result: QueryResult::GetClosestPeers(Ok(r)), ..
}) => break assert!(r.peers.is_empty()),
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {:?}", e),
Expand Down Expand Up @@ -972,7 +972,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
for (i, swarm) in [&mut alice, &mut trudy].iter_mut().enumerate() {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult{
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted{
result: QueryResult::GetRecord(result),
..
}))) => {
Expand Down Expand Up @@ -1025,7 +1025,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
for (i, swarm) in [&mut alice, &mut bob].iter_mut().enumerate() {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::QueryResult{
Poll::Ready(Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted{
result: QueryResult::GetRecord(result),
..
}))) => {
Expand Down
6 changes: 2 additions & 4 deletions protocols/kad/src/kbucket/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,9 @@ where
.value
}

/// Sets the status of the entry to `NodeStatus::Disconnected`.
pub fn update(self, status: NodeStatus) -> Self {
/// Sets the status of the entry to the provided [`NodeStatus`].
pub fn update(&mut self, status: NodeStatus) {
self.0.bucket.update(self.0.key, status);
Self::new(self.0.bucket, self.0.key)
}

/// Removes the entry from the bucket.
Expand Down Expand Up @@ -274,4 +273,3 @@ where
}, status)
}
}

2 changes: 2 additions & 0 deletions protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub use behaviour::{
Quorum
};
pub use behaviour::{
InboundRequest,

QueryRef,
QueryMut,

Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ fn firewalled_src_discover_firewalled_dst_via_kad_and_connect_to_dst_via_routabl
let query_id = dst_swarm.behaviour_mut().kad.bootstrap().unwrap();
loop {
match dst_swarm.select_next_some().await {
SwarmEvent::Behaviour(CombinedEvent::Kad(KademliaEvent::QueryResult {
SwarmEvent::Behaviour(CombinedEvent::Kad(KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::Bootstrap(Ok(_)),
..
Expand Down Expand Up @@ -647,7 +647,7 @@ fn firewalled_src_discover_firewalled_dst_via_kad_and_connect_to_dst_via_routabl
SwarmEvent::Dialing(peer_id)
if peer_id == relay_peer_id || peer_id == dst_peer_id => {}
SwarmEvent::Behaviour(CombinedEvent::Ping(_)) => {}
SwarmEvent::Behaviour(CombinedEvent::Kad(KademliaEvent::QueryResult {
SwarmEvent::Behaviour(CombinedEvent::Kad(KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::GetClosestPeers(Ok(GetClosestPeersOk { .. })),
..
Expand Down