From 37bd2d47512cba206a3981a61bd6d6adec9202e2 Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:43:50 +0100 Subject: [PATCH 1/8] Move Connection ID --- core/src/connection.rs | 23 -------------------- protocols/autonat/src/behaviour.rs | 8 +++---- protocols/autonat/src/behaviour/as_client.rs | 4 ++-- protocols/autonat/src/behaviour/as_server.rs | 4 ++-- protocols/dcutr/src/behaviour.rs | 3 ++- protocols/dcutr/src/handler.rs | 3 +-- protocols/dcutr/src/handler/direct.rs | 3 +-- protocols/floodsub/src/layer.rs | 6 ++--- protocols/gossipsub/src/behaviour.rs | 7 +++--- protocols/gossipsub/src/types.rs | 3 ++- protocols/identify/src/behaviour.rs | 5 ++--- protocols/kad/src/behaviour.rs | 5 +++-- protocols/kad/src/behaviour/test.rs | 4 ++-- protocols/mdns/src/behaviour.rs | 2 +- protocols/ping/src/lib.rs | 4 ++-- protocols/relay/src/v2/client.rs | 5 ++--- protocols/relay/src/v2/relay.rs | 5 ++--- protocols/relay/src/v2/relay/handler.rs | 3 +-- protocols/rendezvous/src/client.rs | 4 ++-- protocols/rendezvous/src/server.rs | 4 ++-- protocols/request-response/src/lib.rs | 5 +++-- swarm/src/behaviour.rs | 8 +++---- swarm/src/behaviour/either.rs | 3 ++- swarm/src/behaviour/toggle.rs | 3 ++- swarm/src/connection.rs | 23 ++++++++++++++++++++ swarm/src/connection/pool.rs | 4 ++-- swarm/src/connection/pool/task.rs | 4 ++-- swarm/src/dummy.rs | 2 +- swarm/src/keep_alive.rs | 2 +- swarm/src/lib.rs | 7 +++--- swarm/src/test.rs | 8 +++---- swarm/tests/swarm_derive.rs | 4 ++-- 32 files changed, 85 insertions(+), 93 deletions(-) diff --git a/core/src/connection.rs b/core/src/connection.rs index 91008408fe2..2eb60f87bb1 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 { diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index cc0adc51c02..19ce6e4aecd 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::{ ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent, RequestResponseMessage, ResponseChannel, @@ -40,8 +38,8 @@ use libp2p_swarm::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, ExpiredListenAddr, FromSwarm, }, - ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, - PollParameters, + ConnectionHandler, ConnectionId, IntoConnectionHandler, 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 cbb63f6aa95..888109e68be 100644 --- a/protocols/autonat/src/behaviour/as_client.rs +++ b/protocols/autonat/src/behaviour/as_client.rs @@ -27,11 +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::{ OutboundFailure, RequestId, RequestResponse, RequestResponseEvent, RequestResponseMessage, }; -use libp2p_swarm::{AddressScore, NetworkBehaviourAction, PollParameters}; +use libp2p_swarm::{AddressScore, ConnectionId, NetworkBehaviourAction, PollParameters}; use rand::{seq::SliceRandom, thread_rng}; use std::{ collections::{HashMap, VecDeque}, diff --git a/protocols/autonat/src/behaviour/as_server.rs b/protocols/autonat/src/behaviour/as_server.rs index f858c48ceb7..12be84760c4 100644 --- a/protocols/autonat/src/behaviour/as_server.rs +++ b/protocols/autonat/src/behaviour/as_server.rs @@ -23,14 +23,14 @@ 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::{ InboundFailure, RequestId, RequestResponse, RequestResponseEvent, RequestResponseMessage, 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.rs b/protocols/dcutr/src/behaviour.rs index 78b22c89ac1..6354e8178e6 100644 --- a/protocols/dcutr/src/behaviour.rs +++ b/protocols/dcutr/src/behaviour.rs @@ -23,11 +23,12 @@ use crate::handler; use crate::protocol; 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, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, diff --git a/protocols/dcutr/src/handler.rs b/protocols/dcutr/src/handler.rs index e854b395308..6c37d3a163b 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::{self, 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 f0fdf5930ac..a534449f8f2 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, InboundUpgrade, OutboundUpgrade}; use libp2p_swarm::handler::ConnectionEvent; use libp2p_swarm::{ - ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, + ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId, KeepAlive, NegotiatedSubstream, SubstreamProtocol, }; use std::task::{Context, Poll}; diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 776c0e8551b..87dd5a9237a 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 c361fc4fdbc..3b033fa6813 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -37,14 +37,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 29c72d1f044..572845e5f2f 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::text::Encode; use prost::Message; use std::fmt; diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 10bfab3ed7d..58178026a71 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -21,10 +21,9 @@ use crate::handler::{self, Proto, Push}; use crate::protocol::{Info, ReplySubstream, UpgradeError}; use futures::prelude::*; -use libp2p_core::{ - connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey, -}; +use libp2p_core::{multiaddr::Protocol, 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, IntoConnectionHandler, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 643f618567c..dc0c6c3425c 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -39,14 +39,15 @@ 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, ExpiredListenAddr, FromSwarm, NewExternalAddr, NewListenAddr, }; use libp2p_swarm::{ dial_opts::{self, DialOpts}, - DialError, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + ConnectionId, DialError, 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 00f8816e52a..afb62a2cb2a 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -28,10 +28,10 @@ use crate::K_VALUE; use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures_timer::Delay; use libp2p::noise; -use libp2p::swarm::{Swarm, SwarmEvent}; +use libp2p::swarm::{Swarm, SwarmEvent, ConnectionId}; use libp2p::yamux; use libp2p_core::{ - connection::{ConnectedPoint, ConnectionId}, + connection::ConnectedPoint, identity, multiaddr::{multiaddr, Multiaddr, Protocol}, multihash::{Code, Multihash, MultihashDigest}, diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index ef9ac50addf..39926f87579 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -182,7 +182,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 6e481500df9..492e6edd86b 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/v2/client.rs b/protocols/relay/src/v2/client.rs index d9a2d977588..eeb45063100 100644 --- a/protocols/relay/src/v2/client.rs +++ b/protocols/relay/src/v2/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/relay/src/v2/relay.rs b/protocols/relay/src/v2/relay.rs index 42ccdd69069..4bc79413c03 100644 --- a/protocols/relay/src/v2/relay.rs +++ b/protocols/relay/src/v2/relay.rs @@ -27,13 +27,12 @@ use crate::v2::message_proto; use crate::v2::protocol::inbound_hop; 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, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, - PollParameters, + ConnectionHandlerUpgrErr, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, + NotifyHandler, PollParameters, }; use std::collections::{hash_map, HashMap, HashSet, VecDeque}; use std::num::NonZeroU32; diff --git a/protocols/relay/src/v2/relay/handler.rs b/protocols/relay/src/v2/relay/handler.rs index ef8b40755b2..e4fad46b900 100644 --- a/protocols/relay/src/v2/relay/handler.rs +++ b/protocols/relay/src/v2/relay/handler.rs @@ -30,7 +30,6 @@ use futures::io::AsyncWriteExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; use instant::Instant; -use libp2p_core::connection::ConnectionId; use libp2p_core::either::EitherError; use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::handler::{ @@ -38,7 +37,7 @@ use libp2p_swarm::handler::{ 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/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 173831d95d6..bcf960278bd 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -28,13 +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, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + CloseConnection, ConnectionId, 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 4126b6e3e28..5f5fe0a8964 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 68c6212579c..89145c5bd95 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -66,11 +66,12 @@ pub use handler::ProtocolSupport; use futures::channel::oneshot; use handler::{RequestProtocol, RequestResponseHandler, RequestResponseHandlerEvent}; -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 6240e570888..a14633a2aaf 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -21,12 +21,11 @@ mod either; pub mod toggle; +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`]. @@ -444,14 +443,13 @@ 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::{ - /// # DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, + /// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, /// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler, /// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent, /// # }; diff --git a/swarm/src/behaviour/either.rs b/swarm/src/behaviour/either.rs index 4154db1a0de..66b66a01fff 100644 --- a/swarm/src/behaviour/either.rs +++ b/swarm/src/behaviour/either.rs @@ -21,6 +21,7 @@ use crate::behaviour::{ self, inject_from_swarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; +use crate::connection::ConnectionId; use crate::handler::either::IntoEitherHandler; use either::Either; use libp2p_core::{Multiaddr, PeerId}; @@ -77,7 +78,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 81255a40274..30dc88e2c4b 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::behaviour::{inject_from_swarm, FromSwarm}; +use crate::connection::ConnectionId; use crate::handler::{ ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, IntoConnectionHandler, @@ -95,7 +96,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 272f78f3a6b..daf2f6cc667 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -46,6 +46,29 @@ 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 { diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 8729b2e36e1..97184a9ce89 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}; use crate::{ behaviour::{THandlerInEvent, THandlerOutEvent}, connection::{ @@ -38,7 +38,7 @@ use futures::{ ready, stream::FuturesUnordered, }; -use libp2p_core::connection::{ConnectionId, Endpoint, PendingPoint}; +use libp2p_core::connection::{Endpoint, PendingPoint}; 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 8e1129d8cae..37e8cabb5b4 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::{Transport, TransportError}, ConnectionHandler, Multiaddr, PeerId, @@ -34,7 +35,6 @@ use futures::{ future::{poll_fn, Either, Future}, SinkExt, StreamExt, }; -use libp2p_core::connection::ConnectionId; use std::pin::Pin; use void::Void; diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 4ec58581c2e..b7ecde6de13 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 bd1ed812b8b..b6085b7db9e 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 0c5cadcf021..8c8d2c5945a 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; @@ -105,8 +105,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::{ @@ -123,7 +123,6 @@ use connection::IncomingInfo; use dial_opts::{DialOpts, PeerCondition}; use either::Either; 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 94a5fbfef54..17c7822945a 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 84fb3bf4683..f3394b2e634 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -370,10 +370,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; From d348d031bd6c3b2d1517f7cfc9f32ee999edc8ae Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:47:13 +0100 Subject: [PATCH 2/8] Run cargo fmt --- protocols/kad/src/behaviour/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index afb62a2cb2a..a5e95d3f1b6 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::noise; -use libp2p::swarm::{Swarm, SwarmEvent, ConnectionId}; +use libp2p::swarm::{ConnectionId, Swarm, SwarmEvent}; use libp2p::yamux; use libp2p_core::{ connection::ConnectedPoint, From b0dd4c8d010eaefc400a5e2d9bde841ab952782e Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:49:00 +0100 Subject: [PATCH 3/8] Added CHANGELOG entry --- core/CHANGELOG.md | 6 ++++++ swarm/CHANGELOG.md | 3 +++ 2 files changed, 9 insertions(+) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index a7e24f5d616..8e9c11e7a0b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.39.0 [unreleased] + +- Move `ConnectionId` to `libp2p-swarm`. 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/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index e111ea099c0..c26dccf4111 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -4,8 +4,11 @@ - Add `estblished_in` to `SwarmEvent::ConnectionEstablished`. See [PR 3134]. +- Moved `ConnectionId` from `libp2p-core` to `libp2p-swarm`. See [PR 3221]. + [PR 3170]: https://github.com/libp2p/rust-libp2p/pull/3170 [PR 3134]: https://github.com/libp2p/rust-libp2p/pull/3134 +[PR 3221]: https://github.com/libp2p/rust-libp2p/pull/3221 # 0.41.1 From a3e4d42a98c2370d722ec40b8a0c3c120560bcb3 Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:50:11 +0100 Subject: [PATCH 4/8] Add version bump in libp2p-core --- core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index cb95d814d77..0695f0ee7a6 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-core" edition = "2021" rust-version = "1.60.0" description = "Core traits and structs of libp2p" -version = "0.38.0" +version = "0.39.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From ba1b845e1853473484f574fd6d57f9a3bf796360 Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:52:03 +0100 Subject: [PATCH 5/8] Add version bump to libp2p Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b5bc389494d..b3efdbb49bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,7 +93,7 @@ getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature libp2p-autonat = { version = "0.10.0", path = "protocols/autonat", optional = true } -libp2p-core = { version = "0.38.0", path = "core" } +libp2p-core = { version = "0.39.0", path = "core" } libp2p-dcutr = { version = "0.9.0", path = "protocols/dcutr", optional = true } libp2p-floodsub = { version = "0.42.0", path = "protocols/floodsub", optional = true } libp2p-identify = { version = "0.42.0", path = "protocols/identify", optional = true } From 218ce649a0a8ec32e65f87a62e9aad188a368460 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 14:55:41 +1100 Subject: [PATCH 6/8] Move `PendingPoint` and make it crate private --- core/CHANGELOG.md | 1 + core/src/connection.rs | 36 ---------------------------------- swarm/src/connection.rs | 38 +++++++++++++++++++++++++++++++++++- swarm/src/connection/pool.rs | 10 +++++----- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 8e9c11e7a0b..68590354285 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,6 +1,7 @@ # 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 diff --git a/core/src/connection.rs b/core/src/connection.rs index 2eb60f87bb1..7a339b0661b 100644 --- a/core/src/connection.rs +++ b/core/src/connection.rs @@ -52,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/swarm/src/connection.rs b/swarm/src/connection.rs index 68b4d75aa14..2d8bf6c5a31 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -42,7 +42,7 @@ 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::{Endpoint, PeerId}; use libp2p_core::{upgrade, UpgradeError}; use std::future::Future; use std::task::Waker; @@ -847,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 4d6725bfd7e..e90de1b2f96 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -19,27 +19,27 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::connection::{Connection, ConnectionId}; +use crate::connection::{Connection, ConnectionId, PendingPoint}; use crate::{ behaviour::{THandlerInEvent, THandlerOutEvent}, + ConnectedPoint, connection::{ Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, }, - transport::TransportError, - ConnectedPoint, ConnectionHandler, Executor, IntoConnectionHandler, Multiaddr, PeerId, + ConnectionHandler, Executor, IntoConnectionHandler, Multiaddr, PeerId, transport::TransportError, }; use concurrent_dial::ConcurrentDial; use fnv::FnvHashMap; use futures::prelude::*; use futures::{ channel::{mpsc, oneshot}, - future::{poll_fn, BoxFuture, Either}, + future::{BoxFuture, Either, poll_fn}, ready, stream::FuturesUnordered, }; use instant::Instant; -use libp2p_core::connection::{Endpoint, PendingPoint}; +use libp2p_core::connection::Endpoint; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt}; use std::{ collections::{hash_map, HashMap}, From 903760ebc265a3cae6c91ece67cc8b919260b435 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 14:58:32 +1100 Subject: [PATCH 7/8] Rustfmt --- swarm/src/connection.rs | 2 +- swarm/src/connection/pool.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 2d8bf6c5a31..563f2111ee0 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -42,8 +42,8 @@ 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::{Endpoint, PeerId}; use libp2p_core::{upgrade, UpgradeError}; +use libp2p_core::{Endpoint, PeerId}; use std::future::Future; use std::task::Waker; use std::time::Duration; diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index e90de1b2f96..80b464a033e 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -22,19 +22,19 @@ use crate::connection::{Connection, ConnectionId, PendingPoint}; use crate::{ behaviour::{THandlerInEvent, THandlerOutEvent}, - ConnectedPoint, connection::{ Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, }, - ConnectionHandler, Executor, IntoConnectionHandler, Multiaddr, PeerId, transport::TransportError, + transport::TransportError, + ConnectedPoint, ConnectionHandler, Executor, IntoConnectionHandler, Multiaddr, PeerId, }; use concurrent_dial::ConcurrentDial; use fnv::FnvHashMap; use futures::prelude::*; use futures::{ channel::{mpsc, oneshot}, - future::{BoxFuture, Either, poll_fn}, + future::{poll_fn, BoxFuture, Either}, ready, stream::FuturesUnordered, }; From 84eb59cc1a357013743d485d82bd75703331115f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 15:02:00 +1100 Subject: [PATCH 8/8] Remove changelog entry from swarm This belongs into `libp2p-core`. --- swarm/CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 5df610940c9..25969d67052 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -15,14 +15,11 @@ - Remove type parameter from `PendingOutboundConnectionError` and `PendingInboundConnectionError`. These two types are always used with `std::io::Error`. See [PR 3272]. -- Moved `ConnectionId` from `libp2p-core` to `libp2p-swarm`. See [PR 3221]. - [PR 3170]: https://github.com/libp2p/rust-libp2p/pull/3170 [PR 3134]: https://github.com/libp2p/rust-libp2p/pull/3134 [PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153 [PR 3264]: https://github.com/libp2p/rust-libp2p/pull/3264 [PR 3272]: https://github.com/libp2p/rust-libp2p/pull/3272 -[PR 3221]: https://github.com/libp2p/rust-libp2p/pull/3221 # 0.41.1