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

refactor(swarm)!: refactor ListenUpgradeError and DialUpgradeError #3307

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion protocols/identify/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
- Move I/O from `Behaviour` to `Handler`. Handle `Behaviour`'s Identify and Push requests independently by incoming order,
previously Push requests were prioritized. see [PR 3208].

- Update to `libp2p-swarm` `v0.42.0`.
- Update to `libp2p-swarm` `v0.42.0`. Update to the `libp2p_swarm::handler::ConnectionEvent` `DialTimeout`
introduction and consequential changes. With that introduce `Event::Timeout`. See [PR 3307].


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

# 0.41.1

Expand Down
14 changes: 10 additions & 4 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use libp2p_core::{
};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
dial_opts::DialOpts, AddressScore, ConnectionHandler, DialError, ExternalAddresses,
IntoConnectionHandler, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
};
use lru::LruCache;
use std::num::NonZeroUsize;
Expand Down Expand Up @@ -303,6 +303,9 @@ impl NetworkBehaviour for Behaviour {
error,
}));
}
handler::Event::IdentificationTimedout => self
.events
.push_back(NetworkBehaviourAction::GenerateEvent(Event::Timeout)),
}
}

Expand Down Expand Up @@ -459,8 +462,11 @@ pub enum Event {
/// The peer with whom the error originated.
peer_id: PeerId,
/// The error that occurred.
error: ConnectionHandlerUpgrErr<UpgradeError>,
error: libp2p_core::UpgradeError<UpgradeError>,
},

/// Timeout expired while attempting to identify the remote.
Timeout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A timeout is an error so it seems odd that we have an Error variant and then this. I'd suggest we either log this internally and don't report it or create a dedicated error enum ourselves.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are already reporting it up I'd go as we do in dcutr and expose it as an Error, updated to do that! Does it make sense to you to also do the same for relay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to you to also do the same for relay?

Yes I think we should apply that thinking to all protocols.

Copy link
Member Author

@jxs jxs Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks done, ptal! Also removed what seems to be unrequired inbound_hop::UpgradeError and shortcircuit to inbound_hop::FatalUpgradeError see c2eda37 and see if you agree.

}

fn supported_protocols(params: &impl PollParameters) -> Vec<String> {
Expand Down
23 changes: 15 additions & 8 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
};
use log::warn;
use smallvec::SmallVec;
Expand Down Expand Up @@ -161,7 +161,9 @@ pub enum Event {
/// We received a request for identification.
Identify,
/// Failed to identify the remote, or to reply to an identification request.
IdentificationError(ConnectionHandlerUpgrErr<UpgradeError>),
IdentificationError(libp2p_core::UpgradeError<UpgradeError>),
/// Identification Dial timedout.
IdentificationTimedout,
}

impl Handler {
Expand Down Expand Up @@ -257,11 +259,11 @@ impl Handler {
) {
use libp2p_core::upgrade::UpgradeError;

let err = err.map_upgrade_err(|e| match e {
let err = match err {
UpgradeError::Select(e) => UpgradeError::Select(e),
UpgradeError::Apply(EitherError::A(ioe)) => UpgradeError::Apply(ioe),
UpgradeError::Apply(EitherError::B(ioe)) => UpgradeError::Apply(ioe),
});
};
self.events
.push(ConnectionHandlerEvent::Custom(Event::IdentificationError(
err,
Expand Down Expand Up @@ -370,9 +372,7 @@ impl ConnectionHandler for Handler {
Event::Identification(peer_id),
)),
Poll::Ready(Some(Err(err))) => Poll::Ready(ConnectionHandlerEvent::Custom(
Event::IdentificationError(ConnectionHandlerUpgrErr::Upgrade(
libp2p_core::upgrade::UpgradeError::Apply(err),
)),
Event::IdentificationError(libp2p_core::upgrade::UpgradeError::Apply(err)),
)),
Poll::Ready(None) | Poll::Pending => Poll::Pending,
}
Expand All @@ -397,6 +397,13 @@ impl ConnectionHandler for Handler {
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
self.on_dial_upgrade_error(dial_upgrade_error)
}
ConnectionEvent::DialTimeout(_) => {
self.events.push(ConnectionHandlerEvent::Custom(
Event::IdentificationTimedout,
));
self.keep_alive = KeepAlive::No;
self.trigger_next_identify.reset(self.interval);
}
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
}
}
Expand Down