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 17 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: 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 @@ -83,7 +83,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.5", 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
6 changes: 6 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.45.0
- Emit `ModeChanged` event whenever we automatically reconfigure the mode.
See [PR 4503].

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

## 0.44.5
- Migrate to `quick-protobuf-codec` crate for codec logic.
See [PR 4501].
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.5"
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 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 @@ -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