feat(swarm): make stream uprade errors more ergonomic

The currently provided `ConnectionHandlerUpgrErr` is very hard to use. Not only does it have a long name, it also features 3 levels of nesting which results in a lot of boilerplate. Last but not least, it exposes `multistream-select` as a dependency to all protocols.

We fix all of the above by renaming the type to `StreamUpgradeError` and flattening out its interface. Unrecoverable errors during protocol selection are hidden within the `Io` variant.

Related: #3759.

Pull-Request: #3882.
This commit is contained in:
Thomas Eizinger
2023-05-08 10:55:17 +02:00
committed by GitHub
parent 0e36c7c072
commit 81c424ea9e
23 changed files with 234 additions and 300 deletions

View File

@@ -30,7 +30,7 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailu
use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::{dummy, ConnectionDenied, ConnectionId, THandler, THandlerOutEvent};
use libp2p_swarm::{
ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NotifyHandler, PollParameters,
ExternalAddresses, NetworkBehaviour, NotifyHandler, PollParameters, StreamUpgradeError,
THandlerInEvent, ToSwarm,
};
use std::collections::{HashMap, HashSet, VecDeque};
@@ -65,7 +65,7 @@ pub enum Error {
#[error("Failed to dial peer.")]
Dial,
#[error("Failed to establish substream: {0}.")]
Handler(ConnectionHandlerUpgrErr<Void>),
Handler(StreamUpgradeError<Void>),
}
pub struct Behaviour {

View File

@@ -23,8 +23,7 @@
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_swarm::handler::ConnectionEvent;
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
};
use std::task::{Context, Poll};
use void::Void;
@@ -42,7 +41,7 @@ pub struct Handler {
impl ConnectionHandler for Handler {
type InEvent = void::Void;
type OutEvent = Event;
type Error = ConnectionHandlerUpgrErr<std::io::Error>;
type Error = StreamUpgradeError<std::io::Error>;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
type OutboundOpenInfo = Void;

View File

@@ -26,15 +26,14 @@ use futures::future;
use futures::future::{BoxFuture, FutureExt};
use instant::Instant;
use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::upgrade::{DeniedUpgrade, NegotiationError, UpgradeError};
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::ConnectedPoint;
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::fmt;
@@ -82,11 +81,11 @@ pub enum Event {
remote_addr: Multiaddr,
},
InboundNegotiationFailed {
error: ConnectionHandlerUpgrErr<void::Void>,
error: StreamUpgradeError<void::Void>,
},
InboundConnectNegotiated(Vec<Multiaddr>),
OutboundNegotiationFailed {
error: ConnectionHandlerUpgrErr<void::Void>,
error: StreamUpgradeError<void::Void>,
},
OutboundConnectNegotiated {
remote_addrs: Vec<Multiaddr>,
@@ -127,7 +126,7 @@ pub struct Handler {
endpoint: ConnectedPoint,
/// A pending fatal error that results in the connection being closed.
pending_error: Option<
ConnectionHandlerUpgrErr<
StreamUpgradeError<
Either<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
>,
>,
@@ -212,12 +211,10 @@ impl Handler {
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(
match error {
Either::Left(e) => Either::Left(e),
Either::Right(v) => void::unreachable(v),
},
)));
self.pending_error = Some(StreamUpgradeError::Apply(match error {
Either::Left(e) => Either::Left(e),
Either::Right(v) => void::unreachable(v),
}));
}
fn on_dial_upgrade_error(
@@ -230,29 +227,27 @@ impl Handler {
self.keep_alive = KeepAlive::No;
match error {
ConnectionHandlerUpgrErr::Timeout => {
StreamUpgradeError::Timeout => {
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::OutboundNegotiationFailed {
error: ConnectionHandlerUpgrErr::Timeout,
error: StreamUpgradeError::Timeout,
},
));
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
StreamUpgradeError::NegotiationFailed => {
// The remote merely doesn't support the DCUtR protocol.
// This is no reason to close the connection, which may
// successfully communicate with other protocols already.
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::OutboundNegotiationFailed {
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::Failed,
)),
error: StreamUpgradeError::NegotiationFailed,
},
));
}
_ => {
// Anything else is considered a fatal error or misbehaviour of
// the remote peer and results in closing the connection.
self.pending_error = Some(error.map_upgrade_err(|e| e.map_err(Either::Right)));
self.pending_error = Some(error.map_upgrade_err(Either::Right));
}
}
}
@@ -261,7 +256,7 @@ impl Handler {
impl ConnectionHandler for Handler {
type InEvent = Command;
type OutEvent = Event;
type Error = ConnectionHandlerUpgrErr<
type Error = StreamUpgradeError<
Either<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
>;
type InboundProtocol = Either<protocol::inbound::Upgrade, DeniedUpgrade>;
@@ -352,9 +347,9 @@ impl ConnectionHandler for Handler {
));
}
Err(e) => {
return Poll::Ready(ConnectionHandlerEvent::Close(
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(e))),
))
return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply(
Either::Left(e),
)))
}
}
}

View File

@@ -27,10 +27,10 @@ use futures::future::Either;
use futures::prelude::*;
use futures::StreamExt;
use instant::Instant;
use libp2p_core::upgrade::{DeniedUpgrade, NegotiationError, UpgradeError};
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_swarm::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive,
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, StreamUpgradeError,
SubstreamProtocol,
};
use libp2p_swarm::NegotiatedSubstream;
@@ -526,20 +526,17 @@ impl ConnectionHandler for Handler {
handler.on_fully_negotiated_outbound(fully_negotiated_outbound)
}
ConnectionEvent::DialUpgradeError(DialUpgradeError {
error: ConnectionHandlerUpgrErr::Timeout,
error: StreamUpgradeError::Timeout,
..
}) => {
log::debug!("Dial upgrade error: Protocol negotiation timeout");
}
ConnectionEvent::DialUpgradeError(DialUpgradeError {
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
error: StreamUpgradeError::Apply(e),
..
}) => void::unreachable(e),
ConnectionEvent::DialUpgradeError(DialUpgradeError {
error:
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::Failed,
)),
error: StreamUpgradeError::NegotiationFailed,
..
}) => {
// The protocol is not supported
@@ -551,10 +548,7 @@ impl ConnectionHandler for Handler {
});
}
ConnectionEvent::DialUpgradeError(DialUpgradeError {
error:
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::ProtocolError(e),
)),
error: StreamUpgradeError::Io(e),
..
}) => {
log::debug!("Protocol negotiation failed: {e}")

View File

@@ -25,8 +25,8 @@ use libp2p_identity::PeerId;
use libp2p_identity::PublicKey;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
AddressScore, ConnectionDenied, ConnectionHandlerUpgrErr, DialError, ExternalAddresses,
ListenAddresses, NetworkBehaviour, NotifyHandler, PollParameters, StreamProtocol,
AddressScore, ConnectionDenied, DialError, ExternalAddresses, ListenAddresses,
NetworkBehaviour, NotifyHandler, PollParameters, StreamProtocol, StreamUpgradeError,
THandlerInEvent, ToSwarm,
};
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
@@ -492,7 +492,7 @@ pub enum Event {
/// The peer with whom the error originated.
peer_id: PeerId,
/// The error that occurred.
error: ConnectionHandlerUpgrErr<UpgradeError>,
error: StreamUpgradeError<UpgradeError>,
},
}

View File

@@ -34,8 +34,8 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, StreamProtocol, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, StreamProtocol,
StreamUpgradeError, SubstreamProtocol,
};
use log::warn;
use smallvec::SmallVec;
@@ -111,7 +111,7 @@ 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(StreamUpgradeError<UpgradeError>),
}
impl Handler {
@@ -205,13 +205,7 @@ impl Handler {
<Self as ConnectionHandler>::OutboundProtocol,
>,
) {
use libp2p_core::upgrade::UpgradeError;
let err = err.map_upgrade_err(|e| match e {
UpgradeError::Select(e) => UpgradeError::Select(e),
UpgradeError::Apply(Either::Left(ioe)) => UpgradeError::Apply(ioe),
UpgradeError::Apply(Either::Right(ioe)) => UpgradeError::Apply(ioe),
});
let err = err.map_upgrade_err(|e| e.into_inner());
self.events
.push(ConnectionHandlerEvent::Custom(Event::IdentificationError(
err,
@@ -317,9 +311,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(StreamUpgradeError::Apply(err)),
)),
Poll::Ready(None) | Poll::Pending => Poll::Pending,
}

View File

@@ -33,8 +33,8 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, StreamUpgradeError,
SubstreamProtocol,
};
use log::trace;
use std::collections::VecDeque;
@@ -325,7 +325,7 @@ pub enum KademliaHandlerEvent<TUserData> {
#[derive(Debug)]
pub enum KademliaHandlerQueryErr {
/// Error while trying to perform the query.
Upgrade(ConnectionHandlerUpgrErr<io::Error>),
Upgrade(StreamUpgradeError<io::Error>),
/// Received an answer that doesn't correspond to the request.
UnexpectedMessage,
/// I/O error in the substream.
@@ -361,8 +361,8 @@ impl error::Error for KademliaHandlerQueryErr {
}
}
impl From<ConnectionHandlerUpgrErr<io::Error>> for KademliaHandlerQueryErr {
fn from(err: ConnectionHandlerUpgrErr<io::Error>) -> Self {
impl From<StreamUpgradeError<io::Error>> for KademliaHandlerQueryErr {
fn from(err: StreamUpgradeError<io::Error>) -> Self {
KademliaHandlerQueryErr::Upgrade(err)
}
}

View File

@@ -28,8 +28,8 @@ use std::{
use libp2p_core::Multiaddr;
use libp2p_identity::PeerId;
use libp2p_swarm::{
derive_prelude::ConnectionEstablished, ConnectionClosed, ConnectionHandlerUpgrErr,
ConnectionId, FromSwarm, NetworkBehaviour, NotifyHandler, PollParameters, THandlerInEvent,
derive_prelude::ConnectionEstablished, ConnectionClosed, ConnectionId, FromSwarm,
NetworkBehaviour, NotifyHandler, PollParameters, StreamUpgradeError, THandlerInEvent,
THandlerOutEvent, ToSwarm,
};
use void::Void;
@@ -41,7 +41,7 @@ use super::{RunId, RunParams, RunStats};
#[derive(Debug)]
pub struct Event {
pub id: RunId,
pub result: Result<RunStats, ConnectionHandlerUpgrErr<Void>>,
pub result: Result<RunStats, StreamUpgradeError<Void>>,
}
#[derive(Default)]

View File

@@ -31,7 +31,7 @@ use libp2p_swarm::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError,
},
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, StreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamProtocol, StreamUpgradeError,
SubstreamProtocol,
};
use void::Void;
@@ -47,7 +47,7 @@ pub struct Command {
#[derive(Debug)]
pub struct Event {
pub(crate) id: RunId,
pub(crate) result: Result<RunStats, ConnectionHandlerUpgrErr<Void>>,
pub(crate) result: Result<RunStats, StreamUpgradeError<Void>>,
}
pub struct Handler {

View File

@@ -23,13 +23,12 @@ use futures::future::BoxFuture;
use futures::prelude::*;
use futures_timer::Delay;
use libp2p_core::upgrade::ReadyUpgrade;
use libp2p_core::{upgrade::NegotiationError, UpgradeError};
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, StreamProtocol, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, StreamProtocol,
StreamUpgradeError, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::{
@@ -238,14 +237,14 @@ impl Handler {
self.outbound = None; // Request a new substream on the next `poll`.
let error = match error {
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
StreamUpgradeError::NegotiationFailed => {
debug_assert_eq!(self.state, State::Active);
self.state = State::Inactive { reported: false };
return;
}
// Note: This timeout only covers protocol negotiation.
ConnectionHandlerUpgrErr::Timeout => Failure::Timeout,
StreamUpgradeError::Timeout => Failure::Timeout,
e => Failure::Other { error: Box::new(e) },
};

View File

@@ -33,9 +33,8 @@ use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses,
NetworkBehaviour, NotifyHandler, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
ToSwarm,
dummy, ConnectionDenied, ConnectionId, ExternalAddresses, NetworkBehaviour, NotifyHandler,
PollParameters, StreamUpgradeError, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32;
@@ -170,7 +169,7 @@ pub enum Event {
CircuitReqOutboundConnectFailed {
src_peer_id: PeerId,
dst_peer_id: PeerId,
error: ConnectionHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
error: StreamUpgradeError<outbound_stop::CircuitFailedReason>,
},
/// Accepting an inbound circuit request failed.
CircuitReqAcceptFailed {

View File

@@ -30,15 +30,15 @@ use futures::io::AsyncWriteExt;
use futures::stream::{FuturesUnordered, StreamExt};
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr};
use libp2p_core::{ConnectedPoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionId, KeepAlive, NegotiatedSubstream,
StreamUpgradeError, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::fmt;
@@ -203,7 +203,7 @@ pub enum Event {
src_connection_id: ConnectionId,
inbound_circuit_req: inbound_hop::CircuitReq,
status: proto::Status,
error: ConnectionHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
error: StreamUpgradeError<outbound_stop::CircuitFailedReason>,
},
/// An inbound circuit has closed.
CircuitClosed {
@@ -347,7 +347,7 @@ pub struct Handler {
/// A pending fatal error that results in the connection being closed.
pending_error: Option<
ConnectionHandlerUpgrErr<
StreamUpgradeError<
Either<inbound_hop::FatalUpgradeError, outbound_stop::FatalUpgradeError>,
>,
>,
@@ -471,9 +471,7 @@ impl Handler {
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Apply(Either::Left(error)),
));
self.pending_error = Some(StreamUpgradeError::Apply(Either::Left(error)));
}
fn on_dial_upgrade_error(
@@ -487,33 +485,23 @@ impl Handler {
>,
) {
let (non_fatal_error, status) = match error {
ConnectionHandlerUpgrErr::Timeout => (
ConnectionHandlerUpgrErr::Timeout,
StreamUpgradeError::Timeout => (
StreamUpgradeError::Timeout,
proto::Status::CONNECTION_FAILED,
),
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)) => {
StreamUpgradeError::NegotiationFailed => {
// The remote has previously done a reservation. Doing a reservation but not
// supporting the stop protocol is pointless, thus disconnecting.
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Select(upgrade::NegotiationError::Failed),
));
self.pending_error = Some(StreamUpgradeError::NegotiationFailed);
return;
}
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::ProtocolError(e),
)) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Select(upgrade::NegotiationError::ProtocolError(e)),
));
StreamUpgradeError::Io(e) => {
self.pending_error = Some(StreamUpgradeError::Io(e));
return;
}
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => match error {
StreamUpgradeError::Apply(error) => match error {
outbound_stop::UpgradeError::Fatal(error) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Apply(Either::Right(error)),
));
self.pending_error = Some(StreamUpgradeError::Apply(Either::Right(error)));
return;
}
outbound_stop::UpgradeError::CircuitFailed(error) => {
@@ -525,10 +513,7 @@ impl Handler {
proto::Status::PERMISSION_DENIED
}
};
(
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)),
status,
)
(StreamUpgradeError::Apply(error), status)
}
},
};
@@ -563,7 +548,7 @@ type Futures<T> = FuturesUnordered<BoxFuture<'static, T>>;
impl ConnectionHandler for Handler {
type InEvent = In;
type OutEvent = Event;
type Error = ConnectionHandlerUpgrErr<
type Error = StreamUpgradeError<
Either<inbound_hop::FatalUpgradeError, outbound_stop::FatalUpgradeError>,
>;
type InboundProtocol = inbound_hop::Upgrade;

View File

@@ -39,9 +39,9 @@ use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionHandler, ConnectionHandlerUpgrErr, ConnectionId,
DialFailure, NegotiatedSubstream, NetworkBehaviour, NotifyHandler, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
dummy, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, NegotiatedSubstream,
NetworkBehaviour, NotifyHandler, PollParameters, StreamUpgradeError, THandler, THandlerInEvent,
THandlerOutEvent, ToSwarm,
};
use std::collections::{hash_map, HashMap, VecDeque};
use std::io::{Error, ErrorKind, IoSlice};
@@ -64,7 +64,7 @@ pub enum Event {
relay_peer_id: PeerId,
/// Indicates whether the request replaces an existing reservation.
renewal: bool,
error: ConnectionHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
error: StreamUpgradeError<outbound_hop::ReservationFailedReason>,
},
OutboundCircuitEstablished {
relay_peer_id: PeerId,
@@ -72,7 +72,7 @@ pub enum Event {
},
OutboundCircuitReqFailed {
relay_peer_id: PeerId,
error: ConnectionHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
error: StreamUpgradeError<outbound_hop::CircuitFailedReason>,
},
/// An inbound circuit has been established.
InboundCircuitEstablished {

View File

@@ -29,15 +29,14 @@ use futures::stream::{FuturesUnordered, StreamExt};
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{upgrade, Multiaddr};
use libp2p_core::Multiaddr;
use libp2p_identity::PeerId;
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
};
use log::debug;
use std::collections::{HashMap, VecDeque};
@@ -85,12 +84,12 @@ pub enum Event {
ReservationReqFailed {
/// Indicates whether the request replaces an existing reservation.
renewal: bool,
error: ConnectionHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
error: StreamUpgradeError<outbound_hop::ReservationFailedReason>,
},
/// An outbound circuit has been established.
OutboundCircuitEstablished { limit: Option<protocol::Limit> },
OutboundCircuitReqFailed {
error: ConnectionHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
error: StreamUpgradeError<outbound_hop::CircuitFailedReason>,
},
/// An inbound circuit has been established.
InboundCircuitEstablished {
@@ -112,7 +111,7 @@ pub struct Handler {
remote_addr: Multiaddr,
/// A pending fatal error that results in the connection being closed.
pending_error: Option<
ConnectionHandlerUpgrErr<
StreamUpgradeError<
Either<inbound_stop::FatalUpgradeError, outbound_hop::FatalUpgradeError>,
>,
>,
@@ -299,9 +298,7 @@ impl Handler {
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Apply(Either::Left(error)),
));
self.pending_error = Some(StreamUpgradeError::Apply(Either::Left(error)));
}
fn on_dial_upgrade_error(
@@ -317,42 +314,25 @@ impl Handler {
match open_info {
OutboundOpenInfo::Reserve { mut to_listener } => {
let non_fatal_error = match error {
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)),
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::ProtocolError(e),
)) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Select(
upgrade::NegotiationError::ProtocolError(e),
),
));
StreamUpgradeError::Timeout => StreamUpgradeError::Timeout,
StreamUpgradeError::NegotiationFailed => StreamUpgradeError::NegotiationFailed,
StreamUpgradeError::Io(e) => {
self.pending_error = Some(StreamUpgradeError::Io(e));
return;
}
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
match error {
outbound_hop::UpgradeError::Fatal(error) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Apply(Either::Right(error)),
));
return;
}
outbound_hop::UpgradeError::ReservationFailed(error) => {
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
error,
))
}
outbound_hop::UpgradeError::CircuitFailed(_) => {
unreachable!(
"Do not emitt `CircuitFailed` for outgoing reservation."
)
}
StreamUpgradeError::Apply(error) => match error {
outbound_hop::UpgradeError::Fatal(error) => {
self.pending_error =
Some(StreamUpgradeError::Apply(Either::Right(error)));
return;
}
}
outbound_hop::UpgradeError::ReservationFailed(error) => {
StreamUpgradeError::Apply(error)
}
outbound_hop::UpgradeError::CircuitFailed(_) => {
unreachable!("Do not emitt `CircuitFailed` for outgoing reservation.")
}
},
};
if self.pending_error.is_none() {
@@ -379,42 +359,25 @@ impl Handler {
}
OutboundOpenInfo::Connect { send_back } => {
let non_fatal_error = match error {
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)),
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::ProtocolError(e),
)) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Select(
upgrade::NegotiationError::ProtocolError(e),
),
));
StreamUpgradeError::Timeout => StreamUpgradeError::Timeout,
StreamUpgradeError::NegotiationFailed => StreamUpgradeError::NegotiationFailed,
StreamUpgradeError::Io(e) => {
self.pending_error = Some(StreamUpgradeError::Io(e));
return;
}
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
match error {
outbound_hop::UpgradeError::Fatal(error) => {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
upgrade::UpgradeError::Apply(Either::Right(error)),
));
return;
}
outbound_hop::UpgradeError::CircuitFailed(error) => {
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
error,
))
}
outbound_hop::UpgradeError::ReservationFailed(_) => {
unreachable!(
"Do not emitt `ReservationFailed` for outgoing circuit."
)
}
StreamUpgradeError::Apply(error) => match error {
outbound_hop::UpgradeError::Fatal(error) => {
self.pending_error =
Some(StreamUpgradeError::Apply(Either::Right(error)));
return;
}
}
outbound_hop::UpgradeError::CircuitFailed(error) => {
StreamUpgradeError::Apply(error)
}
outbound_hop::UpgradeError::ReservationFailed(_) => {
unreachable!("Do not emitt `ReservationFailed` for outgoing circuit.")
}
},
};
let _ = send_back.send(Err(()));
@@ -432,7 +395,7 @@ impl Handler {
impl ConnectionHandler for Handler {
type InEvent = In;
type OutEvent = Event;
type Error = ConnectionHandlerUpgrErr<
type Error = StreamUpgradeError<
Either<inbound_stop::FatalUpgradeError, outbound_hop::FatalUpgradeError>,
>;
type InboundProtocol = inbound_stop::Upgrade;

View File

@@ -28,13 +28,12 @@ use crate::{RequestId, EMPTY_QUEUE_SHRINK_THRESHOLD};
use futures::{channel::oneshot, future::BoxFuture, prelude::*, stream::FuturesUnordered};
use instant::Instant;
use libp2p_core::upgrade::{NegotiationError, UpgradeError};
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError,
};
use libp2p_swarm::{
handler::{ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive},
handler::{ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError},
SubstreamProtocol,
};
use smallvec::SmallVec;
@@ -67,7 +66,7 @@ where
/// The current connection keep-alive.
keep_alive: KeepAlive,
/// A pending fatal error that results in the connection being closed.
pending_error: Option<ConnectionHandlerUpgrErr<io::Error>>,
pending_error: Option<StreamUpgradeError<io::Error>>,
/// Queue of events to emit in `poll()`.
pending_events: VecDeque<Event<TCodec>>,
/// Outbound upgrades waiting to be emitted as an `OutboundSubstreamRequest`.
@@ -140,10 +139,10 @@ where
>,
) {
match error {
ConnectionHandlerUpgrErr::Timeout => {
StreamUpgradeError::Timeout => {
self.pending_events.push_back(Event::OutboundTimeout(info));
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
StreamUpgradeError::NegotiationFailed => {
// The remote merely doesn't support the protocol(s) we requested.
// This is no reason to close the connection, which may
// successfully communicate with other protocols already.
@@ -166,9 +165,7 @@ where
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(
error,
)));
self.pending_error = Some(StreamUpgradeError::Apply(error));
}
}
@@ -244,7 +241,7 @@ where
{
type InEvent = RequestProtocol<TCodec>;
type OutEvent = Event<TCodec>;
type Error = ConnectionHandlerUpgrErr<io::Error>;
type Error = StreamUpgradeError<io::Error>;
type InboundProtocol = ResponseProtocol<TCodec>;
type OutboundProtocol = RequestProtocol<TCodec>;
type OutboundOpenInfo = RequestId;