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: report changes in supported protocols to ConnectionHandler #3651

Merged
merged 61 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
f2801fb
Report changes in supported protocols back to `ConnectionHandler`
thomaseizinger Mar 20, 2023
9a28cd2
Allow reporting of supported protocols by remote
thomaseizinger Mar 22, 2023
e536dea
Consume supported protocols in identify
thomaseizinger Mar 24, 2023
7699a1e
Report a remote's protocols to other handlers
thomaseizinger Mar 24, 2023
450fc1e
Add test for kademlia client mode
thomaseizinger Mar 24, 2023
a972b9c
Implement kademlia client-mode
thomaseizinger Mar 24, 2023
f9cf33f
Add tests for two servers connecting
thomaseizinger Mar 24, 2023
deb30f3
Report additions and removals of protocols instead
thomaseizinger Apr 13, 2023
3c1bf5f
Report listen protocols on startup to connection
thomaseizinger Apr 13, 2023
9e70729
Introduce `SupportedProtocols` type
thomaseizinger Apr 13, 2023
f1328c5
Deduplicate code and propagate supported protocols only once
thomaseizinger Apr 19, 2023
a4fbcda
Extend docs
thomaseizinger Apr 19, 2023
3e33108
Merge branch 'master' into 2680-explore
thomaseizinger Apr 19, 2023
572ed90
Fix compile errors
thomaseizinger Apr 19, 2023
586394b
Report changes to listen/external address set
thomaseizinger Apr 26, 2023
b329a09
Simplify identify protocol
thomaseizinger Apr 26, 2023
a63af89
Check for changes in inbound protocols on every poll
thomaseizinger Apr 26, 2023
72510dd
Fix clippy lints
thomaseizinger Apr 26, 2023
8ffafdd
Report changes in `SupportedProtocols`
thomaseizinger Apr 26, 2023
ea1a087
Only report changes to handler if there actually was a change
thomaseizinger Apr 26, 2023
27bc507
Do not implicitly dial peers upon push
thomaseizinger Apr 27, 2023
58039d9
Merge branch 'master' into 2680-explore
thomaseizinger Apr 27, 2023
74dd94a
Remove unused import
thomaseizinger Apr 27, 2023
628b519
Fix compile error
thomaseizinger Apr 27, 2023
c7b5011
Remove dbg!
thomaseizinger Apr 27, 2023
a02ca55
Update swarm/src/handler.rs
thomaseizinger Apr 27, 2023
c347c8a
Merge branch '2680-explore' of github.com:libp2p/rust-libp2p into 268…
thomaseizinger May 2, 2023
f3e5e71
Combine match arms where possible
thomaseizinger May 2, 2023
b7fa7ef
Add comment explaining static hashset
thomaseizinger May 2, 2023
2bd9d73
Add docs
thomaseizinger May 2, 2023
e90c40d
Update supported protocols for push messages
thomaseizinger May 2, 2023
84979e4
Use `pop` to avoid panicking branch in `remove`
thomaseizinger May 2, 2023
dbfc7e7
Use `poll_unpin`
thomaseizinger May 2, 2023
f2d2c88
Use `if let` for consistency
thomaseizinger May 2, 2023
b41aeb8
Rewrite to `if let` for consistency
thomaseizinger May 2, 2023
bcd872b
Fill in todo in toggle
thomaseizinger May 2, 2023
021f1d4
Merge branch 'master' into 2680-explore
thomaseizinger May 2, 2023
fb096ad
Merge branch 'master' into 2680-explore
thomaseizinger May 2, 2023
e95c738
Merge branch 'master' into 2680-explore
thomaseizinger May 4, 2023
82642b8
Restore `libp2p-kad`
thomaseizinger May 4, 2023
eb66489
Fix formatting
thomaseizinger May 4, 2023
8c47bd6
Fix unit tests
thomaseizinger May 4, 2023
bf9421e
Change test to assert actual events received
thomaseizinger May 4, 2023
a82343a
Don't report empty set of protocols to handler
thomaseizinger May 4, 2023
19cd9b9
Use constructor
thomaseizinger May 4, 2023
6d3e9ee
Introduce test helper
thomaseizinger May 4, 2023
df93a4e
Make tests less noisy
thomaseizinger May 4, 2023
c50bcfd
Further simplify test and add comments
thomaseizinger May 4, 2023
a799798
Add failing test
thomaseizinger May 4, 2023
0260ad1
Make test pass, i.e. only report actual changes back to the handler
thomaseizinger May 4, 2023
bf99654
Extract helpers to make fields crate-private and add docs
thomaseizinger May 4, 2023
46f4e96
Return `SmallVec` from `from_full_sets` which allows iteration
thomaseizinger May 4, 2023
ae7fc93
Fix clippy warnings
thomaseizinger May 4, 2023
fe9a6e3
Fix rustdoc
thomaseizinger May 4, 2023
d7651b4
Merge branch 'master' into 2680-explore
thomaseizinger May 8, 2023
75681c1
Add changelog entry
thomaseizinger May 8, 2023
bdfb04f
Merge branch 'master' into 2680-explore
thomaseizinger May 8, 2023
88362e5
Sort imports
thomaseizinger May 8, 2023
3c8d326
Merge branch 'master' into 2680-explore
thomaseizinger May 8, 2023
77e4e5b
Merge branch 'master' into 2680-explore
thomaseizinger May 8, 2023
5a772e1
Merge branch 'master' into 2680-explore
thomaseizinger May 8, 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
8 changes: 8 additions & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,14 @@ where
self.connection_updated(source, address, NodeStatus::Connected);
}

KademliaHandlerEvent::ProtocolNotSupported { endpoint } => {
let address = match endpoint {
ConnectedPoint::Dialer { address, .. } => Some(address),
ConnectedPoint::Listener { .. } => None,
};
self.connection_updated(source, address, NodeStatus::Disconnected);
}

KademliaHandlerEvent::FindNodeReq { key, request_id } => {
let closer_peers = self.find_closest(&kbucket::Key::new(key), &source);

Expand Down
41 changes: 30 additions & 11 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use libp2p_core::{upgrade, ConnectedPoint};
use libp2p_identity::PeerId;
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ProtocolsChange,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
Expand Down Expand Up @@ -92,10 +93,12 @@ pub struct KademliaHandler<TUserData> {
enum ProtocolStatus {
/// It is as yet unknown whether the remote supports the
/// configured protocol name.
Unconfirmed,
Unknown,
/// The configured protocol name has been confirmed by the remote
/// but has not yet been reported to the `Kademlia` behaviour.
Confirmed,
/// The configured protocol name(s) are not or no longer supported by the remote.
NotSupported,
/// The configured protocol has been confirmed by the remote
/// and the confirmation reported to the `Kademlia` behaviour.
Reported,
Expand Down Expand Up @@ -226,13 +229,11 @@ impl<TUserData> InboundSubstreamState<TUserData> {
#[derive(Debug)]
pub enum KademliaHandlerEvent<TUserData> {
/// The configured protocol name has been confirmed by the peer through
/// a successfully negotiated substream.
///
/// This event is only emitted once by a handler upon the first
/// successfully negotiated inbound or outbound substream and
/// indicates that the connected peer participates in the Kademlia
/// overlay network identified by the configured protocol name.
/// a successfully negotiated substream or by learning the supported protocols of the remote.
ProtocolConfirmed { endpoint: ConnectedPoint },
/// The configured protocol name(s) are not or no longer supported by the peer on the provided
/// connection and it should be removed from the routing table.
ProtocolNotSupported { endpoint: ConnectedPoint },

/// Request for the list of nodes whose IDs are the closest to `key`. The number of nodes
/// returned is not specified, but should be around 20.
Expand Down Expand Up @@ -501,7 +502,7 @@ where
num_requested_outbound_streams: 0,
requested_streams: Default::default(),
keep_alive,
protocol_status: ProtocolStatus::Unconfirmed,
protocol_status: ProtocolStatus::Unknown,
}
}

Expand All @@ -520,7 +521,7 @@ where
protocol, msg, user_data,
));
self.num_requested_outbound_streams -= 1;
if let ProtocolStatus::Unconfirmed = self.protocol_status {
if let ProtocolStatus::Unknown = self.protocol_status {
// Upon the first successfully negotiated substream, we know that the
// remote is configured with the same protocol name and we want
// the behaviour to add this peer to the routing table, if possible.
Expand All @@ -542,7 +543,7 @@ where
future::Either::Right(p) => void::unreachable(p),
};

if let ProtocolStatus::Unconfirmed = self.protocol_status {
if let ProtocolStatus::Unknown = self.protocol_status {
// Upon the first successfully negotiated substream, we know that the
// remote is configured with the same protocol name and we want
// the behaviour to add this peer to the routing table, if possible.
Expand Down Expand Up @@ -789,7 +790,25 @@ where
ConnectionEvent::AddressChange(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::LocalProtocolsChange(_) => {}
ConnectionEvent::RemoteProtocolsChange(_) => {}
ConnectionEvent::RemoteProtocolsChange(ProtocolsChange { protocols }) => {
// TODO: We should cache this / it will get simpler with #2831.
let kademlia_protocols = self
.config
.protocol_config
.protocol_names()
.iter()
.filter_map(|b| String::from_utf8(b.to_vec()).ok())
.collect::<Vec<_>>();

let remote_supports_our_kademlia_protocols =
kademlia_protocols.iter().all(|p| protocols.contains(p));
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

if remote_supports_our_kademlia_protocols {
self.protocol_status = ProtocolStatus::Confirmed;
} else {
self.protocol_status = ProtocolStatus::NotSupported;
}
}
}
}
}
Expand Down