-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor(swarm)!: remove handler
from NetworkBehaviourAction::Dial
#3328
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
Merged
Merged
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
3a84c14
Use single quotes to avoid running commands
thomaseizinger 458aa53
Use intermediary variable
thomaseizinger d0d8b77
Make connection IDs globally unique
thomaseizinger 11e5f74
Deprecate old new function
thomaseizinger 47cf01f
Add changelog entry
thomaseizinger 341d096
Merge branch 'master' into no-run-title-as-command
thomaseizinger f77e443
Remove `handler` field from `NetworkBehaviourAction::Dial`
thomaseizinger f6cf751
Port dcutr protocol
thomaseizinger 08c5160
Port relay protocol
thomaseizinger 8e0d7d8
Add changelog entry to swarm
thomaseizinger b337ec1
Merge branch 'no-run-title-as-command' into 2824-unique-connection-ids
thomaseizinger 3c4ab32
Merge branch '2824-unique-connection-ids' into 2824-no-handler-in-dial
thomaseizinger 1beec8c
Remove ordering functionality from `ConnectionId`
thomaseizinger eb48acb
Merge branch '2824-unique-connection-ids' of github.com:libp2p/rust-l…
thomaseizinger de9f152
Remove unnecessary visibility qualifiers
thomaseizinger 7692c77
Convert `DialOpts` to struct variant
thomaseizinger b492236
Eagerly set defaults in `DialOpts`
thomaseizinger 92b2985
Merge branch '2824-refactor-dial-opts' into 2824-no-handler-in-dial
thomaseizinger 951a0dc
Make `ConnectionId` part of `DialOpts`
thomaseizinger 41e49a5
Inline `_dial`
thomaseizinger 3547b53
Rustfmt
thomaseizinger 75b30ac
Fix wrong default
thomaseizinger 30df5c8
Merge branch '2824-refactor-dial-opts' into 2824-no-handler-in-dial
thomaseizinger 3d91173
Merge branch 'master' into 2824-unique-connection-ids
thomaseizinger aa19d69
Merge branch '2824-unique-connection-ids' of github.com:libp2p/rust-l…
thomaseizinger 0f53692
Merge branch 'master' into 2824-unique-connection-ids
thomaseizinger 4084846
Merge branch '2824-unique-connection-ids' into 2824-no-handler-in-dial
thomaseizinger 42c9758
Expand changelog entry
thomaseizinger 14b9841
Move most important changelog entry to the top
thomaseizinger 1a3a100
Add more text to changelog entry
thomaseizinger 5535395
Fix formatting
thomaseizinger 244c171
Merge branch 'master' into 2824-unique-connection-ids
thomaseizinger a82e087
Merge branch '2824-unique-connection-ids' into 2824-no-handler-in-dial
thomaseizinger b536773
Merge branch 'master' into 2824-unique-connection-ids
mergify[bot] 64a333d
Merge branch 'master' into 2824-unique-connection-ids
thomaseizinger 867c880
Merge branch '2824-unique-connection-ids' into 2824-no-handler-in-dial
thomaseizinger e97d899
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger c0a7d2a
Remove unnecessary `..`
thomaseizinger 8e20996
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger 9a420e4
Don't use else block if we return in the previous one
thomaseizinger f5f37ab
Remove closure in favor of spawning connection from the outside
thomaseizinger e867b26
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger dd1eb8c
Add `ConnectionId` to `ListenFailure`
thomaseizinger 32a465b
Clean up pending commands on failure
thomaseizinger 91f942f
Add docs and fix bug
thomaseizinger 6f19246
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger d704e89
Consistently use `THandlerInEvent`
thomaseizinger 55baab1
Use type alias
thomaseizinger 2c84f28
Remove unnecessary indentation and "quote"-ing
thomaseizinger cf77246
Fix bad changelog entry
thomaseizinger 92a9625
Spawn connection at right point in time
thomaseizinger d90ac81
Fix rustdoc
thomaseizinger cfe9f95
One more missing rustdoc link
thomaseizinger 3f8f73c
Remove `banned_peer_connections`
thomaseizinger 063ef5c
Fix test
thomaseizinger 26f6cf0
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger c3ba2e3
Merge branch 'master' into 2824-no-handler-in-dial
thomaseizinger 3ecd623
Inline `spawn` function
thomaseizinger 02cbf42
Box future inside `spawn`
thomaseizinger 1c906db
Merge impl blocks
thomaseizinger 87cf5fd
Gracefully close connection to banned peer
thomaseizinger f204859
Ignore error when closing connection
thomaseizinger 21f0c46
Merge branch 'master' into 2824-no-handler-in-dial
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,13 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailu | |
use libp2p_swarm::dial_opts::{self, DialOpts}; | ||
use libp2p_swarm::{ | ||
ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, | ||
NotifyHandler, PollParameters, | ||
NotifyHandler, PollParameters, THandlerInEvent, | ||
}; | ||
use libp2p_swarm::{ConnectionId, THandlerOutEvent}; | ||
use std::collections::{HashMap, HashSet, VecDeque}; | ||
use std::task::{Context, Poll}; | ||
use thiserror::Error; | ||
use void::Void; | ||
|
||
const MAX_NUMBER_OF_UPGRADE_ATTEMPTS: u8 = 3; | ||
|
||
|
@@ -68,14 +69,20 @@ pub enum Error { | |
|
||
pub struct Behaviour { | ||
/// Queue of actions to return when polled. | ||
queued_events: VecDeque<NetworkBehaviourAction<Event, handler::Prototype>>, | ||
queued_events: VecDeque<NetworkBehaviourAction<Event, Either<handler::relayed::Command, Void>>>, | ||
|
||
/// All direct (non-relayed) connections. | ||
direct_connections: HashMap<PeerId, HashSet<ConnectionId>>, | ||
|
||
external_addresses: ExternalAddresses, | ||
|
||
local_peer_id: PeerId, | ||
|
||
direct_to_relayed_connections: HashMap<ConnectionId, ConnectionId>, | ||
|
||
/// Indexed by the [`ConnectionId`] of the relayed connection and | ||
/// the [`PeerId`] we are trying to establish a direct connection to. | ||
outgoing_direct_connection_attempts: HashMap<(ConnectionId, PeerId), u8>, | ||
thomaseizinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl Behaviour { | ||
|
@@ -85,6 +92,8 @@ impl Behaviour { | |
direct_connections: Default::default(), | ||
external_addresses: Default::default(), | ||
local_peer_id, | ||
direct_to_relayed_connections: Default::default(), | ||
outgoing_direct_connection_attempts: Default::default(), | ||
} | ||
} | ||
|
||
|
@@ -148,40 +157,57 @@ impl Behaviour { | |
fn on_dial_failure( | ||
&mut self, | ||
DialFailure { | ||
peer_id, handler, .. | ||
}: DialFailure<<Self as NetworkBehaviour>::ConnectionHandler>, | ||
peer_id, | ||
connection_id: failed_direct_connection, | ||
.. | ||
}: DialFailure, | ||
) { | ||
if let handler::Prototype::DirectConnection { | ||
relayed_connection_id, | ||
role: handler::Role::Initiator { attempt }, | ||
} = handler | ||
let peer_id = if let Some(peer_id) = peer_id { | ||
peer_id | ||
} else { | ||
return; | ||
}; | ||
thomaseizinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let relayed_connection_id = if let Some(relayed_connection_id) = self | ||
.direct_to_relayed_connections | ||
.get(&failed_direct_connection) | ||
{ | ||
let peer_id = peer_id.expect("Peer of `Prototype::DirectConnection` is always known."); | ||
if attempt < MAX_NUMBER_OF_UPGRADE_ATTEMPTS { | ||
self.queued_events | ||
.push_back(NetworkBehaviourAction::NotifyHandler { | ||
handler: NotifyHandler::One(relayed_connection_id), | ||
peer_id, | ||
event: Either::Left(handler::relayed::Command::Connect { | ||
attempt: attempt + 1, | ||
obs_addrs: self.observed_addreses(), | ||
}), | ||
}) | ||
} else { | ||
self.queued_events.extend([ | ||
NetworkBehaviourAction::NotifyHandler { | ||
peer_id, | ||
handler: NotifyHandler::One(relayed_connection_id), | ||
event: Either::Left( | ||
handler::relayed::Command::UpgradeFinishedDontKeepAlive, | ||
), | ||
}, | ||
NetworkBehaviourAction::GenerateEvent(Event::DirectConnectionUpgradeFailed { | ||
remote_peer_id: peer_id, | ||
error: Error::Dial, | ||
*relayed_connection_id | ||
} else { | ||
return; | ||
}; | ||
|
||
let attempt = if let Some(attempt) = self | ||
.outgoing_direct_connection_attempts | ||
.get(&(relayed_connection_id, peer_id)) | ||
{ | ||
*attempt | ||
} else { | ||
return; | ||
}; | ||
|
||
if attempt < MAX_NUMBER_OF_UPGRADE_ATTEMPTS { | ||
self.queued_events | ||
.push_back(NetworkBehaviourAction::NotifyHandler { | ||
handler: NotifyHandler::One(relayed_connection_id), | ||
peer_id, | ||
event: Either::Left(handler::relayed::Command::Connect { | ||
attempt: attempt + 1, | ||
obs_addrs: self.observed_addreses(), | ||
}), | ||
]); | ||
} | ||
}) | ||
} else { | ||
self.queued_events.extend([ | ||
NetworkBehaviourAction::NotifyHandler { | ||
peer_id, | ||
handler: NotifyHandler::One(relayed_connection_id), | ||
event: Either::Left(handler::relayed::Command::UpgradeFinishedDontKeepAlive), | ||
}, | ||
NetworkBehaviourAction::GenerateEvent(Event::DirectConnectionUpgradeFailed { | ||
remote_peer_id: peer_id, | ||
error: Error::Dial, | ||
}), | ||
]); | ||
} | ||
} | ||
|
||
|
@@ -215,23 +241,34 @@ impl NetworkBehaviour for Behaviour { | |
type OutEvent = Event; | ||
|
||
fn new_handler(&mut self) -> Self::ConnectionHandler { | ||
handler::Prototype::UnknownConnection | ||
handler::Prototype | ||
} | ||
|
||
fn on_connection_handler_event( | ||
&mut self, | ||
event_source: PeerId, | ||
connection: ConnectionId, | ||
connection_id: ConnectionId, | ||
handler_event: THandlerOutEvent<Self>, | ||
) { | ||
let relayed_connection_id = match handler_event.as_ref() { | ||
Either::Left(_) => connection_id, | ||
Either::Right(_) => match self.direct_to_relayed_connections.get(&connection_id) { | ||
None => { | ||
// If the connection ID is unknown to us, it means we didn't create it so ignore any event coming from it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. This will be handled by the type-system once we have #3254. Then we can return a |
||
return; | ||
} | ||
Some(relayed_connection_id) => *relayed_connection_id, | ||
}, | ||
}; | ||
|
||
match handler_event { | ||
Either::Left(handler::relayed::Event::InboundConnectRequest { | ||
inbound_connect, | ||
remote_addr, | ||
}) => { | ||
self.queued_events.extend([ | ||
NetworkBehaviourAction::NotifyHandler { | ||
handler: NotifyHandler::One(connection), | ||
handler: NotifyHandler::One(relayed_connection_id), | ||
peer_id: event_source, | ||
event: Either::Left(handler::relayed::Command::AcceptInboundConnect { | ||
inbound_connect, | ||
|
@@ -256,16 +293,17 @@ impl NetworkBehaviour for Behaviour { | |
)); | ||
} | ||
Either::Left(handler::relayed::Event::InboundConnectNegotiated(remote_addrs)) => { | ||
self.queued_events.push_back(NetworkBehaviourAction::Dial { | ||
opts: DialOpts::peer_id(event_source) | ||
.addresses(remote_addrs) | ||
.condition(dial_opts::PeerCondition::Always) | ||
.build(), | ||
handler: handler::Prototype::DirectConnection { | ||
relayed_connection_id: connection, | ||
role: handler::Role::Listener, | ||
}, | ||
}); | ||
let opts = DialOpts::peer_id(event_source) | ||
.addresses(remote_addrs) | ||
.condition(dial_opts::PeerCondition::Always) | ||
.build(); | ||
|
||
let maybe_direct_connection_id = opts.connection_id(); | ||
|
||
self.direct_to_relayed_connections | ||
.insert(maybe_direct_connection_id, relayed_connection_id); | ||
self.queued_events | ||
.push_back(NetworkBehaviourAction::Dial { opts }); | ||
} | ||
Either::Left(handler::relayed::Event::OutboundNegotiationFailed { error }) => { | ||
self.queued_events | ||
|
@@ -276,27 +314,25 @@ impl NetworkBehaviour for Behaviour { | |
}, | ||
)); | ||
} | ||
Either::Left(handler::relayed::Event::OutboundConnectNegotiated { | ||
remote_addrs, | ||
attempt, | ||
}) => { | ||
self.queued_events.push_back(NetworkBehaviourAction::Dial { | ||
opts: DialOpts::peer_id(event_source) | ||
.condition(dial_opts::PeerCondition::Always) | ||
.addresses(remote_addrs) | ||
.override_role() | ||
.build(), | ||
handler: handler::Prototype::DirectConnection { | ||
relayed_connection_id: connection, | ||
role: handler::Role::Initiator { attempt }, | ||
}, | ||
}); | ||
Either::Left(handler::relayed::Event::OutboundConnectNegotiated { remote_addrs }) => { | ||
let opts = DialOpts::peer_id(event_source) | ||
.condition(dial_opts::PeerCondition::Always) | ||
.addresses(remote_addrs) | ||
.override_role() | ||
.build(); | ||
|
||
let maybe_direct_connection_id = opts.connection_id(); | ||
|
||
self.direct_to_relayed_connections | ||
.insert(maybe_direct_connection_id, relayed_connection_id); | ||
*self | ||
.outgoing_direct_connection_attempts | ||
.entry((relayed_connection_id, event_source)) | ||
.or_default() += 1; | ||
self.queued_events | ||
.push_back(NetworkBehaviourAction::Dial { opts }); | ||
} | ||
Either::Right(Either::Left( | ||
handler::direct::Event::DirectConnectionUpgradeSucceeded { | ||
relayed_connection_id, | ||
}, | ||
)) => { | ||
Either::Right(handler::direct::Event::DirectConnectionEstablished) => { | ||
self.queued_events.extend([ | ||
NetworkBehaviourAction::NotifyHandler { | ||
peer_id: event_source, | ||
|
@@ -312,15 +348,14 @@ impl NetworkBehaviour for Behaviour { | |
), | ||
]); | ||
} | ||
Either::Right(Either::Right(event)) => void::unreachable(event), | ||
}; | ||
} | ||
|
||
fn poll( | ||
&mut self, | ||
_cx: &mut Context<'_>, | ||
_: &mut impl PollParameters, | ||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> { | ||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> { | ||
if let Some(event) = self.queued_events.pop_front() { | ||
return Poll::Ready(event); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.