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(core)!: remove EitherUpgrade #3339

Merged
merged 20 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3a84c14
Use single quotes to avoid running commands
thomaseizinger Jan 11, 2023
458aa53
Use intermediary variable
thomaseizinger Jan 13, 2023
341d096
Merge branch 'master' into no-run-title-as-command
thomaseizinger Jan 16, 2023
aed5363
refactor(relay): introduce `Handler::new` functions (#3326)
thomaseizinger Jan 16, 2023
5e97d0e
fix: revert "refactor(relay): introduce `Handler::new` functions" (#3…
thomaseizinger Jan 17, 2023
da97e8f
Replace `EitherError` with `Either`
thomaseizinger Jan 17, 2023
2ce805d
Add changelog entry
thomaseizinger Jan 17, 2023
0a2729d
Remove `EitherTransport` in favor of `Either`
thomaseizinger Jan 17, 2023
3397266
Update core/CHANGELOG.md
thomaseizinger Jan 17, 2023
5802177
Merge branch '2650-remove-either-error' into 2650-remove-either-trans…
thomaseizinger Jan 17, 2023
7060f4b
Add changelog entry
thomaseizinger Jan 17, 2023
82eb6c2
Merge branch 'no-run-title-as-command' into 2650-remove-either-error
thomaseizinger Jan 17, 2023
955ff80
Merge branch 'master' into no-run-title-as-command
thomaseizinger Jan 17, 2023
bb84906
Merge branch 'no-run-title-as-command' into 2650-remove-either-error
thomaseizinger Jan 17, 2023
fdfaf72
Merge branch '2650-remove-either-error' into 2650-remove-either-trans…
thomaseizinger Jan 17, 2023
59f6465
Update changelog entry
thomaseizinger Jan 17, 2023
8872039
Remove `EitherUpgrade`
thomaseizinger Jan 17, 2023
d3e79cf
Update core/CHANGELOG.md
thomaseizinger Jan 17, 2023
cefc1c7
Merge branch 'master' into 2650-remove-either-upgrade
thomaseizinger Jan 18, 2023
73666f1
Merge branch 'master' into 2650-remove-either-upgrade
mergify[bot] Jan 18, 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
3 changes: 3 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338].

- Remove `EitherUpgrade` in favor of implementing `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`. See [PR 3339].

[PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031
[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058
[PR 3097]: https://github.com/libp2p/rust-libp2p/pull/3097
[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090
[PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972
[PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337
[PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338
[PR 3339]: https://github.com/libp2p/rust-libp2p/pull/3339

# 0.37.0

Expand Down
1 change: 0 additions & 1 deletion core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ use futures::future::Future;
pub use self::{
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
denied::DeniedUpgrade,
either::EitherUpgrade,
error::UpgradeError,
from_fn::{from_fn, FromFnUpgrade},
map::{MapInboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgrade, MapOutboundUpgradeErr},
Expand Down
25 changes: 9 additions & 16 deletions core/src/upgrade/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ use crate::{
};
use either::Either;

/// A type to represent two possible upgrade types (inbound or outbound).
#[derive(Debug, Clone)]
pub enum EitherUpgrade<A, B> {
A(A),
B(B),
}

impl<A, B> UpgradeInfo for EitherUpgrade<A, B>
impl<A, B> UpgradeInfo for Either<A, B>
where
A: UpgradeInfo,
B: UpgradeInfo,
Expand All @@ -44,13 +37,13 @@ where

fn protocol_info(&self) -> Self::InfoIter {
match self {
EitherUpgrade::A(a) => EitherIter::A(a.protocol_info().into_iter()),
EitherUpgrade::B(b) => EitherIter::B(b.protocol_info().into_iter()),
Either::Left(a) => EitherIter::A(a.protocol_info().into_iter()),
Either::Right(b) => EitherIter::B(b.protocol_info().into_iter()),
}
}
}

impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for EitherUpgrade<A, B>
impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for Either<A, B>
where
A: InboundUpgrade<C, Output = TA, Error = EA>,
B: InboundUpgrade<C, Output = TB, Error = EB>,
Expand All @@ -61,18 +54,18 @@ where

fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
(Either::Left(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_inbound(sock, info))
}
(EitherUpgrade::B(b), EitherName::B(info)) => {
(Either::Right(b), EitherName::B(info)) => {
EitherFuture2::B(b.upgrade_inbound(sock, info))
}
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"),
}
}
}

impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for EitherUpgrade<A, B>
impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for Either<A, B>
where
A: OutboundUpgrade<C, Output = TA, Error = EA>,
B: OutboundUpgrade<C, Output = TB, Error = EB>,
Expand All @@ -83,10 +76,10 @@ where

fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => {
(Either::Left(a), EitherName::A(info)) => {
EitherFuture2::A(a.upgrade_outbound(sock, info))
}
(EitherUpgrade::B(b), EitherName::B(info)) => {
(Either::Right(b), EitherName::B(info)) => {
EitherFuture2::B(b.upgrade_outbound(sock, info))
}
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"),
Expand Down
10 changes: 5 additions & 5 deletions protocols/dcutr/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::protocol;
use either::Either;
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::{self, DeniedUpgrade};
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::dummy;
use libp2p_swarm::handler::SendWrapper;
Expand Down Expand Up @@ -70,11 +70,11 @@ impl IntoConnectionHandler for Prototype {

fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
match self {
Prototype::UnknownConnection => upgrade::EitherUpgrade::A(SendWrapper(
upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}),
)),
Prototype::UnknownConnection => {
Either::Left(SendWrapper(Either::Left(protocol::inbound::Upgrade {})))
}
Prototype::DirectConnection { .. } => {
upgrade::EitherUpgrade::A(SendWrapper(upgrade::EitherUpgrade::B(DeniedUpgrade)))
Either::Left(SendWrapper(Either::Right(DeniedUpgrade)))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions protocols/dcutr/src/handler/relayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use futures::future::{BoxFuture, FutureExt};
use instant::Instant;
use libp2p_core::either::EitherOutput;
use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::upgrade::{self, DeniedUpgrade, NegotiationError, UpgradeError};
use libp2p_core::upgrade::{DeniedUpgrade, NegotiationError, UpgradeError};
use libp2p_core::ConnectedPoint;
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
Expand Down Expand Up @@ -305,23 +305,23 @@ impl ConnectionHandler for Handler {
type Error = ConnectionHandlerUpgrErr<
Either<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
>;
type InboundProtocol = upgrade::EitherUpgrade<protocol::inbound::Upgrade, DeniedUpgrade>;
type InboundProtocol = Either<protocol::inbound::Upgrade, DeniedUpgrade>;
type OutboundProtocol = protocol::outbound::Upgrade;
type OutboundOpenInfo = u8; // Number of upgrade attempts.
type InboundOpenInfo = ();

fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
match self.endpoint {
ConnectedPoint::Dialer { .. } => {
SubstreamProtocol::new(upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}), ())
SubstreamProtocol::new(Either::Left(protocol::inbound::Upgrade {}), ())
}
ConnectedPoint::Listener { .. } => {
// By the protocol specification the listening side of a relayed connection
// initiates the _direct connection upgrade_. In other words the listening side of
// the relayed connection opens a substream to the dialing side. (Connection roles
// and substream roles are reversed.) The listening side on a relayed connection
// never expects incoming substreams, hence the denied upgrade below.
SubstreamProtocol::new(upgrade::EitherUpgrade::B(DeniedUpgrade), ())
SubstreamProtocol::new(Either::Right(DeniedUpgrade), ())
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use futures::prelude::*;
use futures::stream::FuturesUnordered;
use futures_timer::Delay;
use libp2p_core::either::EitherOutput;
use libp2p_core::upgrade::{EitherUpgrade, SelectUpgrade};
use libp2p_core::upgrade::SelectUpgrade;
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
Expand Down Expand Up @@ -102,8 +102,7 @@ pub struct Handler {
inbound_identify_push: Option<BoxFuture<'static, Result<Info, UpgradeError>>>,
/// Pending events to yield.
events: SmallVec<
[ConnectionHandlerEvent<EitherUpgrade<Identify, Push<OutboundPush>>, (), Event, io::Error>;
4],
[ConnectionHandlerEvent<Either<Identify, Push<OutboundPush>>, (), Event, io::Error>; 4],
>,

/// Streams awaiting `BehaviourInfo` to then send identify requests.
Expand Down Expand Up @@ -277,7 +276,7 @@ impl ConnectionHandler for Handler {
type OutEvent = Event;
type Error = io::Error;
type InboundProtocol = SelectUpgrade<Identify, Push<InboundPush>>;
type OutboundProtocol = EitherUpgrade<Identify, Push<OutboundPush>>;
type OutboundProtocol = Either<Identify, Push<OutboundPush>>;
type OutboundOpenInfo = ();
type InboundOpenInfo = ();

Expand Down Expand Up @@ -306,10 +305,7 @@ impl ConnectionHandler for Handler {
Protocol::Push => {
self.events
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
EitherUpgrade::B(Push::outbound(info)),
(),
),
protocol: SubstreamProtocol::new(Either::Right(Push::outbound(info)), ()),
});
}
Protocol::Identify(_) => {
Expand Down Expand Up @@ -347,7 +343,7 @@ impl ConnectionHandler for Handler {
Poll::Ready(()) => {
self.trigger_next_identify.reset(self.interval);
let ev = ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(EitherUpgrade::A(Identify), ()),
protocol: SubstreamProtocol::new(Either::Left(Identify), ()),
};
return Poll::Ready(ev);
}
Expand Down
11 changes: 6 additions & 5 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::protocol::{
KademliaProtocolConfig,
};
use crate::record::{self, Record};
use either::Either;
use futures::prelude::*;
use futures::stream::SelectAll;
use instant::Instant;
Expand Down Expand Up @@ -68,9 +69,9 @@ impl<T: Clone + fmt::Debug + Send + 'static + Unpin> IntoConnectionHandler

fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
if self.config.allow_listening {
upgrade::EitherUpgrade::A(self.config.protocol_config.clone())
Either::Left(self.config.protocol_config.clone())
} else {
upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade)
Either::Right(upgrade::DeniedUpgrade)
}
}
}
Expand Down Expand Up @@ -633,7 +634,7 @@ where
type InEvent = KademliaHandlerIn<TUserData>;
type OutEvent = KademliaHandlerEvent<TUserData>;
type Error = io::Error; // TODO: better error type?
type InboundProtocol = upgrade::EitherUpgrade<KademliaProtocolConfig, upgrade::DeniedUpgrade>;
type InboundProtocol = Either<KademliaProtocolConfig, upgrade::DeniedUpgrade>;
type OutboundProtocol = KademliaProtocolConfig;
// Message of the request to send to the remote, and user data if we expect an answer.
type OutboundOpenInfo = (KadRequestMsg, Option<TUserData>);
Expand All @@ -642,9 +643,9 @@ where
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
if self.config.allow_listening {
SubstreamProtocol::new(self.config.protocol_config.clone(), ())
.map_upgrade(upgrade::EitherUpgrade::A)
.map_upgrade(Either::Left)
} else {
SubstreamProtocol::new(upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade), ())
SubstreamProtocol::new(Either::Right(upgrade::DeniedUpgrade), ())
}
}

Expand Down
1 change: 1 addition & 0 deletions protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
either = "1.8.0"
futures = "0.3.1"
futures-timer = "3.0.2"
instant = "0.1.11"
Expand Down
5 changes: 3 additions & 2 deletions protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

//! Integration tests for the `Ping` network behaviour.

use either::Either;
use futures::{channel::mpsc, prelude::*};
use libp2p_core::{
identity,
Expand Down Expand Up @@ -251,8 +252,8 @@ fn mk_transport(muxer: MuxerChoice) -> (PeerId, transport::Boxed<(PeerId, Stream
.upgrade(upgrade::Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&id_keys).unwrap())
.multiplex(match muxer {
MuxerChoice::Yamux => upgrade::EitherUpgrade::A(yamux::YamuxConfig::default()),
MuxerChoice::Mplex => upgrade::EitherUpgrade::B(mplex::MplexConfig::default()),
MuxerChoice::Yamux => Either::Left(yamux::YamuxConfig::default()),
MuxerChoice::Mplex => Either::Right(mplex::MplexConfig::default()),
})
.boxed(),
)
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/src/behaviour/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl IntoConnectionHandler for Prototype {
}

fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
upgrade::EitherUpgrade::A(SendWrapper(inbound_hop::Upgrade {
Either::Left(SendWrapper(inbound_hop::Upgrade {
reservation_duration: self.config.reservation_duration,
max_circuit_duration: self.config.max_circuit_duration,
max_circuit_bytes: self.config.max_circuit_bytes,
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/src/priv_client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl IntoConnectionHandler for Prototype {
}

fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
upgrade::EitherUpgrade::A(SendWrapper(inbound_stop::Upgrade {}))
Either::Left(SendWrapper(inbound_stop::Upgrade {}))
}
}

Expand Down
18 changes: 6 additions & 12 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ use crate::upgrade::SendWrapper;
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use either::Either;
use libp2p_core::{
either::EitherOutput,
upgrade::{DeniedUpgrade, EitherUpgrade},
ConnectedPoint, Multiaddr, PeerId,
either::EitherOutput, upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId,
};
use std::{task::Context, task::Poll};

Expand Down Expand Up @@ -143,9 +141,9 @@ where

fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
if let Some(inner) = self.inner.as_ref() {
EitherUpgrade::A(SendWrapper(inner.inbound_protocol()))
Either::Left(SendWrapper(inner.inbound_protocol()))
} else {
EitherUpgrade::B(SendWrapper(DeniedUpgrade))
Either::Right(SendWrapper(DeniedUpgrade))
}
}
}
Expand Down Expand Up @@ -235,8 +233,7 @@ where
type InEvent = TInner::InEvent;
type OutEvent = TInner::OutEvent;
type Error = TInner::Error;
type InboundProtocol =
EitherUpgrade<SendWrapper<TInner::InboundProtocol>, SendWrapper<DeniedUpgrade>>;
type InboundProtocol = Either<SendWrapper<TInner::InboundProtocol>, SendWrapper<DeniedUpgrade>>;
type OutboundProtocol = TInner::OutboundProtocol;
type OutboundOpenInfo = TInner::OutboundOpenInfo;
type InboundOpenInfo = Either<TInner::InboundOpenInfo, ()>;
Expand All @@ -245,13 +242,10 @@ where
if let Some(inner) = self.inner.as_ref() {
inner
.listen_protocol()
.map_upgrade(|u| EitherUpgrade::A(SendWrapper(u)))
.map_upgrade(|u| Either::Left(SendWrapper(u)))
.map_info(Either::Left)
} else {
SubstreamProtocol::new(
EitherUpgrade::B(SendWrapper(DeniedUpgrade)),
Either::Right(()),
)
SubstreamProtocol::new(Either::Right(SendWrapper(DeniedUpgrade)), Either::Right(()))
}
}

Expand Down
Loading