Skip to content

feat(kad): report changes to our mode as an event #4499

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.43.0", path = "protocols/identify" }
libp2p-identity = { version = "0.2.3" }
libp2p-kad = { version = "0.44.4", path = "protocols/kad" }
libp2p-kad = { version = "0.45.0", path = "protocols/kad" }
libp2p-mdns = { version = "0.44.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.1.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.13.1", path = "misc/metrics" }
Expand Down
7 changes: 7 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.45.0 - unreleased

- Emit `ModeChanged` event whenever we automatically reconfigure the mode.
See [PR 4341].

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

## 0.44.4

- Implement common traits on `RoutingUpdate`.
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-kad"
edition = "2021"
rust-version = { workspace = true }
description = "Kademlia protocol for libp2p"
version = "0.44.4"
version = "0.45.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
15 changes: 15 additions & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ where
}

fn determine_mode_from_external_addresses(&mut self) {
let old_mode = self.mode;

self.mode = match (self.external_addresses.as_slice(), self.mode) {
([], Mode::Server) => {
log::debug!("Switching to client-mode because we no longer have any confirmed external addresses");
Expand Down Expand Up @@ -1082,6 +1084,13 @@ where
};

self.reconfigure_mode();

if old_mode != self.mode {
self.queued_events
.push_back(ToSwarm::GenerateEvent(KademliaEvent::ModeChanged {
new_mode: self.mode,
}));
}
}

/// Processes discovered peers from a successful request in an iterative `Query`.
Expand Down Expand Up @@ -2674,6 +2683,12 @@ pub enum KademliaEvent {
/// See [`Kademlia::kbucket`] for insight into the contents of
/// the k-bucket of `peer`.
PendingRoutablePeer { peer: PeerId, address: Multiaddr },

/// This peer's mode has been updated.
///
/// This usually happens in response to an address change or a new external
/// address being added or removed.
ModeChanged { new_mode: Mode },
}

/// Information about progress events.
Expand Down
2 changes: 2 additions & 0 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ fn exceed_jobs_max_queries() {
result: QueryResult::GetClosestPeers(Ok(r)),
..
}) => break assert!(r.peers.is_empty()),
SwarmEvent::Behaviour(KademliaEvent::ModeChanged { .. }) => {}
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {e:?}"),
_ => {}
}
Expand Down Expand Up @@ -1393,6 +1394,7 @@ fn get_providers_single() {
result: QueryResult::StartProviding(Ok(_)),
..
}) => {}
SwarmEvent::Behaviour(KademliaEvent::ModeChanged { .. }) => {}
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {e:?}"),
_ => {}
}
Expand Down
11 changes: 7 additions & 4 deletions protocols/kad/tests/client_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ async fn adding_an_external_addresses_activates_server_mode_on_existing_connecti

use MyBehaviourEvent::*;

// Do the usual identify send/receive dance.
// Do the usual identify send/receive dance. This triggers a mode change to Mode::Client.
match libp2p_swarm_test::drive(&mut client, &mut server).await {
([Identify(_), Identify(_)], [Identify(_), Identify(_)]) => {}
([Identify(_), Identify(_)], [Kad(ModeChanged { new_mode }), Identify(_), Identify(_)]) => {
assert_eq!(new_mode, Mode::Client);
}
other => panic!("Unexpected events: {other:?}"),
}

Expand All @@ -99,12 +101,13 @@ async fn adding_an_external_addresses_activates_server_mode_on_existing_connecti
// Server learns its external address (this could be through AutoNAT or some other mechanism).
server.add_external_address(memory_addr);

// The server reconfigured its connection to the client to be in server mode, pushes that information to client which as a result updates its routing table.
// The server reconfigured its connection to the client to be in server mode, pushes that information to client which as a result updates its routing table and triggers a mode change to Mode::Server.
match libp2p_swarm_test::drive(&mut client, &mut server).await {
(
[Identify(identify::Event::Received { .. }), Kad(RoutingUpdated { peer: peer1, .. })],
[Identify(identify::Event::Pushed { .. })],
[Kad(ModeChanged { new_mode }), Identify(identify::Event::Pushed { .. })],
) => {
assert_eq!(new_mode, Mode::Server);
assert_eq!(peer1, server_peer_id);
}
other => panic!("Unexpected events: {other:?}"),
Expand Down