diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index bbb812dd..83588698 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,10 @@ +# 0.39.0 [unreleased] + +- Move `ConnectionId` to `libp2p-swarm`. See [PR 3221]. +- Move `PendingPoint` to `libp2p-swarm` and make it crate-private. See [PR 3221]. + +[PR 3221]: https://github.com/libp2p/rust-libp2p/pull/3221 + # 0.38.0 - Remove deprecated functions `StreamMuxerExt::next_{inbound,outbound}`. See [PR 3031]. diff --git a/core/src/connection.rs b/core/src/connection.rs index 91008408..7a339b06 100644 --- a/core/src/connection.rs +++ b/core/src/connection.rs @@ -20,29 +20,6 @@ use crate::multiaddr::{Multiaddr, Protocol}; -/// Connection identifier. -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct ConnectionId(usize); - -impl ConnectionId { - /// Creates a `ConnectionId` from a non-negative integer. - /// - /// This is primarily useful for creating connection IDs - /// in test environments. There is in general no guarantee - /// that all connection IDs are based on non-negative integers. - pub fn new(id: usize) -> Self { - Self(id) - } -} - -impl std::ops::Add for ConnectionId { - type Output = Self; - - fn add(self, other: usize) -> Self { - Self(self.0 + other) - } -} - /// The endpoint roles associated with a peer-to-peer communication channel. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Endpoint { @@ -75,42 +52,6 @@ impl Endpoint { } } -/// The endpoint roles associated with a pending peer-to-peer connection. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum PendingPoint { - /// The socket comes from a dialer. - /// - /// There is no single address associated with the Dialer of a pending - /// connection. Addresses are dialed in parallel. Only once the first dial - /// is successful is the address of the connection known. - Dialer { - /// Same as [`ConnectedPoint::Dialer`] `role_override`. - role_override: Endpoint, - }, - /// The socket comes from a listener. - Listener { - /// Local connection address. - local_addr: Multiaddr, - /// Address used to send back data to the remote. - send_back_addr: Multiaddr, - }, -} - -impl From for PendingPoint { - fn from(endpoint: ConnectedPoint) -> Self { - match endpoint { - ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override }, - ConnectedPoint::Listener { - local_addr, - send_back_addr, - } => PendingPoint::Listener { - local_addr, - send_back_addr, - }, - } - } -} - /// The endpoint roles associated with an established peer-to-peer connection. #[derive(PartialEq, Eq, Debug, Clone, Hash)] pub enum ConnectedPoint { diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index 995ee440..dd88663d 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -28,9 +28,7 @@ use as_server::AsServer; pub use as_server::{InboundProbeError, InboundProbeEvent}; use futures_timer::Delay; use instant::Instant; -use libp2p_core::{ - connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId, -}; +use libp2p_core::{multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId}; use libp2p_request_response::{ self as request_response, ProtocolSupport, RequestId, ResponseChannel, }; @@ -39,8 +37,8 @@ use libp2p_swarm::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, ExpiredListenAddr, FromSwarm, }, - ConnectionHandler, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, - NetworkBehaviourAction, PollParameters, + ConnectionHandler, ConnectionId, ExternalAddresses, IntoConnectionHandler, ListenAddresses, + NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; use std::{ collections::{HashMap, VecDeque}, diff --git a/protocols/autonat/src/behaviour/as_client.rs b/protocols/autonat/src/behaviour/as_client.rs index 51527162..1018bebd 100644 --- a/protocols/autonat/src/behaviour/as_client.rs +++ b/protocols/autonat/src/behaviour/as_client.rs @@ -27,10 +27,11 @@ use super::{ use futures::FutureExt; use futures_timer::Delay; use instant::Instant; -use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId}; +use libp2p_core::{Multiaddr, PeerId}; use libp2p_request_response::{self as request_response, OutboundFailure, RequestId}; use libp2p_swarm::{ - AddressScore, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, PollParameters, + AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, + PollParameters, }; use rand::{seq::SliceRandom, thread_rng}; use std::{ diff --git a/protocols/autonat/src/behaviour/as_server.rs b/protocols/autonat/src/behaviour/as_server.rs index 8d08f7ed..7a8c9f9c 100644 --- a/protocols/autonat/src/behaviour/as_server.rs +++ b/protocols/autonat/src/behaviour/as_server.rs @@ -23,13 +23,13 @@ use super::{ ResponseError, }; use instant::Instant; -use libp2p_core::{connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId}; +use libp2p_core::{multiaddr::Protocol, Multiaddr, PeerId}; use libp2p_request_response::{ self as request_response, InboundFailure, RequestId, ResponseChannel, }; use libp2p_swarm::{ dial_opts::{DialOpts, PeerCondition}, - DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters, + ConnectionId, DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; use std::{ collections::{HashMap, HashSet, VecDeque}, diff --git a/protocols/dcutr/src/behaviour_impl.rs b/protocols/dcutr/src/behaviour_impl.rs index d4cae973..7d77a032 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -22,11 +22,12 @@ use crate::handler; use either::Either; -use libp2p_core::connection::{ConnectedPoint, ConnectionId}; +use libp2p_core::connection::ConnectedPoint; use libp2p_core::multiaddr::Protocol; use libp2p_core::{Multiaddr, PeerId}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::dial_opts::{self, DialOpts}; +use libp2p_swarm::ConnectionId; use libp2p_swarm::{ ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, diff --git a/protocols/dcutr/src/handler.rs b/protocols/dcutr/src/handler.rs index aca0809e..84d1c8d8 100644 --- a/protocols/dcutr/src/handler.rs +++ b/protocols/dcutr/src/handler.rs @@ -20,12 +20,11 @@ use crate::protocol; use either::Either; -use libp2p_core::connection::ConnectionId; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::{ConnectedPoint, PeerId}; use libp2p_swarm::dummy; use libp2p_swarm::handler::SendWrapper; -use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler}; +use libp2p_swarm::{ConnectionHandler, ConnectionId, IntoConnectionHandler}; pub mod direct; pub mod relayed; diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index 9e675997..b979c821 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -20,11 +20,10 @@ //! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection. -use libp2p_core::connection::ConnectionId; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_swarm::handler::ConnectionEvent; use libp2p_swarm::{ - ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, + ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId, KeepAlive, SubstreamProtocol, }; use std::task::{Context, Poll}; diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 776c0e85..87dd5a92 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -26,11 +26,11 @@ use crate::topic::Topic; use crate::FloodsubConfig; use cuckoofilter::{CuckooError, CuckooFilter}; use fnv::FnvHashSet; -use libp2p_core::{connection::ConnectionId, PeerId}; +use libp2p_core::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::{ - dial_opts::DialOpts, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, - PollParameters, + dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, + OneShotHandler, PollParameters, }; use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler}; use log::warn; diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index fce25c12..af199fcd 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -36,14 +36,13 @@ use prost::Message; use rand::{seq::SliceRandom, thread_rng}; use libp2p_core::{ - connection::ConnectionId, identity::Keypair, multiaddr::Protocol::Ip4, - multiaddr::Protocol::Ip6, Multiaddr, PeerId, + identity::Keypair, multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Multiaddr, PeerId, }; use libp2p_swarm::{ behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm}, dial_opts::DialOpts, - ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, - NotifyHandler, PollParameters, + ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, + NetworkBehaviourAction, NotifyHandler, PollParameters, }; use wasm_timer::Instant; diff --git a/protocols/gossipsub/src/types.rs b/protocols/gossipsub/src/types.rs index 10482611..1fc7f450 100644 --- a/protocols/gossipsub/src/types.rs +++ b/protocols/gossipsub/src/types.rs @@ -21,7 +21,8 @@ //! A collection of types using the Gossipsub system. use crate::rpc_proto; use crate::TopicHash; -use libp2p_core::{connection::ConnectionId, PeerId}; +use libp2p_core::PeerId; +use libp2p_swarm::ConnectionId; use prometheus_client::encoding::EncodeLabelValue; use prost::Message; use std::fmt; diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 123c8140..22363ebb 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -20,10 +20,9 @@ use crate::handler::{self, InEvent, Proto}; use crate::protocol::{Info, Protocol, UpgradeError}; -use libp2p_core::{ - connection::ConnectionId, multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey, -}; +use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; +use libp2p_swarm::ConnectionId; use libp2p_swarm::{ dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 6da88c2e..9ef1067a 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -22,11 +22,11 @@ use crate::structs_proto; use asynchronous_codec::{FramedRead, FramedWrite}; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{ - connection::ConnectionId, identity, multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, Multiaddr, PublicKey, }; +use libp2p_swarm::ConnectionId; use log::{debug, trace}; use std::convert::TryFrom; use std::{io, iter, pin::Pin}; diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 6ceb8c73..66e1b426 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -39,14 +39,14 @@ use crate::record::{ use crate::K_VALUE; use fnv::{FnvHashMap, FnvHashSet}; use instant::Instant; -use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId}; +use libp2p_core::{ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::behaviour::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, }; use libp2p_swarm::{ dial_opts::{self, DialOpts}, - DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, - NotifyHandler, PollParameters, + ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, + NetworkBehaviourAction, NotifyHandler, PollParameters, }; use log::{debug, info, warn}; use smallvec::SmallVec; diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index bf3bd7cf..84a3e76a 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -28,7 +28,7 @@ use crate::K_VALUE; use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures_timer::Delay; use libp2p_core::{ - connection::{ConnectedPoint, ConnectionId}, + connection::ConnectedPoint, identity, multiaddr::{multiaddr, Multiaddr, Protocol}, multihash::{Code, Multihash, MultihashDigest}, @@ -36,7 +36,7 @@ use libp2p_core::{ upgrade, Endpoint, PeerId, Transport, }; use libp2p_noise as noise; -use libp2p_swarm::{Swarm, SwarmEvent}; +use libp2p_swarm::{ConnectionId, Swarm, SwarmEvent}; use libp2p_yamux as yamux; use quickcheck::*; use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng}; diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 815a23c9..9c280980 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -189,7 +189,7 @@ where fn on_connection_handler_event( &mut self, _: PeerId, - _: libp2p_core::connection::ConnectionId, + _: libp2p_swarm::ConnectionId, ev: ::OutEvent, ) { void::unreachable(ev) diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 6e481500..492e6edd 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -47,9 +47,9 @@ mod protocol; use handler::Handler; pub use handler::{Config, Failure, Success}; -use libp2p_core::{connection::ConnectionId, PeerId}; +use libp2p_core::PeerId; use libp2p_swarm::{ - behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters, + behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; use std::{ collections::VecDeque, diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index b478de2e..5cb269f6 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -27,13 +27,12 @@ use crate::message_proto; use crate::protocol::{inbound_hop, outbound_stop}; use either::Either; use instant::Instant; -use libp2p_core::connection::ConnectionId; use libp2p_core::multiaddr::Protocol; use libp2p_core::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::{ - ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, - NotifyHandler, PollParameters, + ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour, + NetworkBehaviourAction, NotifyHandler, PollParameters, }; use std::collections::{hash_map, HashMap, HashSet, VecDeque}; use std::num::NonZeroU32; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index a35d3fbb..a1aaf0fa 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -30,14 +30,13 @@ use futures::io::AsyncWriteExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; use instant::Instant; -use libp2p_core::connection::ConnectionId; use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, ListenUpgradeError, SendWrapper, }; use libp2p_swarm::{ - dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, + dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, SubstreamProtocol, }; use std::collections::VecDeque; diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index 7bb79f5d..4399ccea 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -32,13 +32,12 @@ use futures::future::{BoxFuture, FutureExt}; use futures::io::{AsyncRead, AsyncWrite}; use futures::ready; use futures::stream::StreamExt; -use libp2p_core::connection::ConnectionId; use libp2p_core::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::dial_opts::DialOpts; use libp2p_swarm::{ - ConnectionHandlerUpgrErr, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, - NotifyHandler, PollParameters, + ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour, + NetworkBehaviourAction, NotifyHandler, PollParameters, }; use std::collections::{hash_map, HashMap, VecDeque}; use std::io::{Error, ErrorKind, IoSlice}; diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 41c4daf0..60cb0e65 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -28,14 +28,13 @@ use futures::future::FutureExt; use futures::stream::FuturesUnordered; use futures::stream::StreamExt; use instant::Duration; -use libp2p_core::connection::ConnectionId; use libp2p_core::identity::error::SigningError; use libp2p_core::identity::Keypair; use libp2p_core::{Multiaddr, PeerId, PeerRecord}; use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::{ - CloseConnection, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, - PollParameters, + CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, + NotifyHandler, PollParameters, }; use std::collections::{HashMap, VecDeque}; use std::iter::FromIterator; diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 4126b6e3..5f5fe0a8 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -27,11 +27,11 @@ use futures::future::BoxFuture; use futures::ready; use futures::stream::FuturesUnordered; use futures::{FutureExt, StreamExt}; -use libp2p_core::connection::ConnectionId; use libp2p_core::PeerId; use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::{ - CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, + PollParameters, }; use std::collections::{HashMap, HashSet, VecDeque}; use std::iter::FromIterator; diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index f5fa3067..89732eec 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -70,11 +70,12 @@ pub use handler::ProtocolSupport; use futures::channel::oneshot; use handler::{Handler, RequestProtocol}; -use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId}; +use libp2p_core::{ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::{ behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, dial_opts::DialOpts, - IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + ConnectionId, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, + PollParameters, }; use smallvec::SmallVec; use std::{ diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 6fc494b1..896b7aa3 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -26,12 +26,11 @@ pub mod toggle; pub use external_addresses::ExternalAddresses; pub use listen_addresses::ListenAddresses; +use crate::connection::ConnectionId; use crate::dial_opts::DialOpts; use crate::handler::{ConnectionHandler, IntoConnectionHandler}; use crate::{AddressRecord, AddressScore, DialError}; -use libp2p_core::{ - connection::ConnectionId, transport::ListenerId, ConnectedPoint, Multiaddr, PeerId, -}; +use libp2p_core::{transport::ListenerId, ConnectedPoint, Multiaddr, PeerId}; use std::{task::Context, task::Poll}; /// Custom event that can be received by the [`ConnectionHandler`]. @@ -259,14 +258,14 @@ pub enum NetworkBehaviourAction< /// ```rust /// # use futures::executor::block_on; /// # use futures::stream::StreamExt; - /// # use libp2p_core::connection::ConnectionId; /// # use libp2p_core::identity; /// # use libp2p_core::transport::{MemoryTransport, Transport}; /// # use libp2p_core::upgrade::{self, DeniedUpgrade, InboundUpgrade, OutboundUpgrade}; /// # use libp2p_core::PeerId; /// # use libp2p_plaintext::PlainText2Config; /// # use libp2p_swarm::{ - /// # FromSwarm, DialFailure, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, + /// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, + /// # FromSwarm, DialFailure, /// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler, /// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent, /// # }; diff --git a/swarm/src/behaviour/either.rs b/swarm/src/behaviour/either.rs index 073a0275..ce94070c 100644 --- a/swarm/src/behaviour/either.rs +++ b/swarm/src/behaviour/either.rs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; +use crate::connection::ConnectionId; use crate::handler::either::IntoEitherHandler; use either::Either; use libp2p_core::{Multiaddr, PeerId}; @@ -69,7 +70,7 @@ where fn on_connection_handler_event( &mut self, peer_id: PeerId, - connection_id: libp2p_core::connection::ConnectionId, + connection_id: ConnectionId, event: crate::THandlerOutEvent, ) { match (self, event) { diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 75dff7a6..897d515c 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::behaviour::FromSwarm; +use crate::connection::ConnectionId; use crate::handler::{ AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, @@ -93,7 +94,7 @@ where fn on_connection_handler_event( &mut self, peer_id: PeerId, - connection_id: libp2p_core::connection::ConnectionId, + connection_id: ConnectionId, event: crate::THandlerOutEvent, ) { if let Some(behaviour) = &mut self.inner { diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 15a414c0..563f2111 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -42,13 +42,36 @@ use libp2p_core::connection::ConnectedPoint; use libp2p_core::multiaddr::Multiaddr; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerEvent, StreamMuxerExt, SubstreamBox}; use libp2p_core::upgrade::{InboundUpgradeApply, OutboundUpgradeApply}; -use libp2p_core::PeerId; use libp2p_core::{upgrade, UpgradeError}; +use libp2p_core::{Endpoint, PeerId}; use std::future::Future; use std::task::Waker; use std::time::Duration; use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll}; +/// Connection identifier. +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct ConnectionId(usize); + +impl ConnectionId { + /// Creates a `ConnectionId` from a non-negative integer. + /// + /// This is primarily useful for creating connection IDs + /// in test environments. There is in general no guarantee + /// that all connection IDs are based on non-negative integers. + pub fn new(id: usize) -> Self { + Self(id) + } +} + +impl std::ops::Add for ConnectionId { + type Output = Self; + + fn add(self, other: usize) -> Self { + Self(self.0 + other) + } +} + /// Information about a successfully established connection. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Connected { @@ -824,3 +847,39 @@ mod tests { } } } + +/// The endpoint roles associated with a pending peer-to-peer connection. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +enum PendingPoint { + /// The socket comes from a dialer. + /// + /// There is no single address associated with the Dialer of a pending + /// connection. Addresses are dialed in parallel. Only once the first dial + /// is successful is the address of the connection known. + Dialer { + /// Same as [`ConnectedPoint::Dialer`] `role_override`. + role_override: Endpoint, + }, + /// The socket comes from a listener. + Listener { + /// Local connection address. + local_addr: Multiaddr, + /// Address used to send back data to the remote. + send_back_addr: Multiaddr, + }, +} + +impl From for PendingPoint { + fn from(endpoint: ConnectedPoint) -> Self { + match endpoint { + ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override }, + ConnectedPoint::Listener { + local_addr, + send_back_addr, + } => PendingPoint::Listener { + local_addr, + send_back_addr, + }, + } + } +} diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 30dff859..80b464a0 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::connection::Connection; +use crate::connection::{Connection, ConnectionId, PendingPoint}; use crate::{ behaviour::{THandlerInEvent, THandlerOutEvent}, connection::{ @@ -39,7 +39,7 @@ use futures::{ stream::FuturesUnordered, }; use instant::Instant; -use libp2p_core::connection::{ConnectionId, Endpoint, PendingPoint}; +use libp2p_core::connection::Endpoint; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt}; use std::{ collections::{hash_map, HashMap}, diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index 326e381d..27a2a478 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -24,7 +24,8 @@ use super::concurrent_dial::ConcurrentDial; use crate::{ connection::{ - self, ConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, + self, ConnectionError, ConnectionId, PendingInboundConnectionError, + PendingOutboundConnectionError, }, transport::TransportError, ConnectionHandler, Multiaddr, PeerId, @@ -34,7 +35,6 @@ use futures::{ future::{poll_fn, Either, Future}, SinkExt, StreamExt, }; -use libp2p_core::connection::ConnectionId; use libp2p_core::muxing::StreamMuxerBox; use std::pin::Pin; use void::Void; diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 4ec58581..b7ecde6d 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -1,9 +1,9 @@ use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; +use crate::connection::ConnectionId; use crate::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, }; use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol}; -use libp2p_core::connection::ConnectionId; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::PeerId; use libp2p_core::UpgradeError; diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index bd1ed812..b6085b7d 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -1,9 +1,9 @@ use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; +use crate::connection::ConnectionId; use crate::handler::{ ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, SubstreamProtocol, }; -use libp2p_core::connection::ConnectionId; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::PeerId; use std::task::{Context, Poll}; diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 47fc410a..044e3dbf 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -84,6 +84,7 @@ pub mod derive_prelude { pub use crate::behaviour::NewExternalAddr; pub use crate::behaviour::NewListenAddr; pub use crate::behaviour::NewListener; + pub use crate::connection::ConnectionId; pub use crate::ConnectionHandler; pub use crate::DialError; pub use crate::IntoConnectionHandler; @@ -92,7 +93,6 @@ pub mod derive_prelude { pub use crate::NetworkBehaviourAction; pub use crate::PollParameters; pub use futures::prelude as futures; - pub use libp2p_core::connection::ConnectionId; pub use libp2p_core::either::EitherOutput; pub use libp2p_core::transport::ListenerId; pub use libp2p_core::ConnectedPoint; @@ -108,8 +108,8 @@ pub use behaviour::{ }; pub use connection::pool::{ConnectionCounters, ConnectionLimits}; pub use connection::{ - ConnectionError, ConnectionLimit, PendingConnectionError, PendingInboundConnectionError, - PendingOutboundConnectionError, + ConnectionError, ConnectionId, ConnectionLimit, PendingConnectionError, + PendingInboundConnectionError, PendingOutboundConnectionError, }; pub use executor::Executor; pub use handler::{ @@ -125,7 +125,6 @@ use connection::pool::{EstablishedConnection, Pool, PoolConfig, PoolEvent}; use connection::IncomingInfo; use dial_opts::{DialOpts, PeerCondition}; use futures::{executor::ThreadPoolBuilder, prelude::*, stream::FusedStream}; -use libp2p_core::connection::ConnectionId; use libp2p_core::muxing::SubstreamBox; use libp2p_core::{ connection::ConnectedPoint, diff --git a/swarm/src/test.rs b/swarm/src/test.rs index d850a174..5536d50a 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -23,12 +23,10 @@ use crate::behaviour::{ FromSwarm, ListenerClosed, ListenerError, NewExternalAddr, NewListenAddr, NewListener, }; use crate::{ - ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, - PollParameters, -}; -use libp2p_core::{ - connection::ConnectionId, multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId, + ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, + NetworkBehaviourAction, PollParameters, }; +use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId}; use std::collections::HashMap; use std::task::{Context, Poll}; diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 08f04081..38d0f388 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -368,10 +368,10 @@ fn generated_out_event_derive_debug() { #[test] fn custom_out_event_no_type_parameters() { - use libp2p_core::connection::ConnectionId; use libp2p_core::PeerId; use libp2p_swarm::{ - ConnectionHandler, IntoConnectionHandler, NetworkBehaviourAction, PollParameters, + ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviourAction, + PollParameters, }; use std::task::Context; use std::task::Poll;