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

feat(kad): report automatic changes to kad mode #4503

Merged
merged 28 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6171da9
feat!: added kademlia ModeChanged event
dhuseby Aug 18, 2023
19ad007
bumped crate version
dhuseby Aug 18, 2023
4b3bf29
Update protocols/kad/CHANGELOG.md
dhuseby Aug 18, 2023
172f07a
Update protocols/kad/CHANGELOG.md
dhuseby Aug 18, 2023
f20e377
Update protocols/kad/src/behaviour.rs
dhuseby Aug 18, 2023
9a951f9
event only emits when mode changes automatically
dhuseby Aug 18, 2023
f47a83e
fix the broken tests
dhuseby Aug 19, 2023
606d774
Update protocols/kad/CHANGELOG.md
dhuseby Sep 14, 2023
acc062b
Update protocols/kad/src/behaviour.rs
dhuseby Sep 14, 2023
5efd39a
fixed the tests to incorporate the kademlia mode change
dhuseby Sep 14, 2023
ce382c2
Update protocols/kad/src/behaviour.rs
dhuseby Sep 20, 2023
c784c52
Update protocols/kad/src/behaviour.rs
dhuseby Sep 20, 2023
c081773
merging master
dhuseby Sep 20, 2023
0a71bce
Merge branch 'add-kad-modechange' of github:dhuseby/rust-libp2p into …
dhuseby Sep 20, 2023
5edb29a
Merge branch 'master' into add-kad-modechange
dhuseby Sep 22, 2023
367c6a0
bumping kad version due to breaking change
dhuseby Sep 22, 2023
ff46c33
Merge branch 'master' into add-kad-modechange
thomaseizinger Sep 24, 2023
a8a6e95
Merge branch 'master' into add-kad-modechange
dhuseby Sep 28, 2023
95016b7
Merge branch 'add-kad-modechange' of github:dhuseby/rust-libp2p into …
dhuseby Sep 28, 2023
c9d82af
Merge branch 'master' into add-kad-modechange
dhuseby Sep 29, 2023
8a7cfdc
fixed broken symbol from rename
dhuseby Oct 1, 2023
bce012d
fixed the broken ci tests
dhuseby Oct 1, 2023
e41729f
Merge branch 'master' into add-kad-modechange
thomaseizinger Oct 1, 2023
c239097
Merge branch 'master' into add-kad-modechange
dhuseby Oct 3, 2023
739054b
Merge branch 'master' into add-kad-modechange
dhuseby Oct 3, 2023
1a66684
Merge branch 'master' into add-kad-modechange
dhuseby Oct 3, 2023
954c0f1
Merge branch 'master' into add-kad-modechange
thomaseizinger Oct 20, 2023
a00347d
Merge branch 'master' into add-kad-modechange
mergify[bot] Oct 20, 2023
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: 2 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.45.0 - unreleased

- Emit `ModeChanged` event whenever we automatically reconfigure the mode.
See [PR 4503](https://github.com/libp2p/rust-libp2p/pull/4503).

## 0.44.6

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(Event::ModeChanged {
new_mode: self.mode,
}));
}
}

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

/// This peer's mode has been updated automatically.
///
/// This happens in response to an external
/// address being added or removed.
ModeChanged { new_mode: Mode },
dhuseby marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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 @@ -1059,6 +1059,7 @@ fn exceed_jobs_max_queries() {
result: QueryResult::GetClosestPeers(Ok(r)),
..
}) => break assert!(r.peers.is_empty()),
SwarmEvent::Behaviour(Event::ModeChanged { .. }) => {}
SwarmEvent::Behaviour(e) => panic!("Unexpected event: {e:?}"),
_ => {}
}
Expand Down Expand Up @@ -1382,6 +1383,7 @@ fn get_providers_single() {
result: QueryResult::StartProviding(Ok(_)),
..
}) => {}
SwarmEvent::Behaviour(Event::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 @@ -82,21 +82,24 @@ async fn adding_an_external_addresses_activates_server_mode_on_existing_connecti
// Remove memory address to simulate a server that doesn't know its external address.
server.remove_external_address(&memory_addr);
client.dial(memory_addr.clone()).unwrap();
// 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:?}"),
}

// 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