Skip to content

Commit ca0e9f9

Browse files
authored
{core,swarm}: Remove Network abstraction (#2492)
This commit removes the `Network` abstraction, thus managing `Listeners` and the connection `Pool` in `Swarm` directly. This is done under the assumption that noone uses the `Network` abstraction directly, but instead everyone always uses it through `Swarm`. Both `Listeners` and `Pool` are moved from `libp2p-core` into `libp2p-swarm`. Given that they are no longer exposed via `Network`, they can be treated as an implementation detail of `libp2p-swarm` and `Swarm`. This change does not include any behavioural changes. This change has the followin benefits: - Removal of `NetworkEvent`, which was mostly an isomorphism of `SwarmEvent`. - Removal of the never-directly-used `Network` abstraction. - Removal of now obsolete verbose `Peer` (`core/src/network/peer.rs`) construct. - Removal of `libp2p-core` `DialOpts`, which is a direct mapping of `libp2p-swarm` `DialOpts`. - Allowing breaking changes to the connection handling and `Swarm` API interface without a breaking change in `libp2p-core` and thus a without a breaking change in `/transport` protocols. This change enables the following potential future changes: - Removal of `NodeHandler` and `ConnectionHandler`. Thus allowing to rename `ProtocolsHandler` into `ConnectionHandler`. - Moving `NetworkBehaviour` and `ProtocolsHandler` into `libp2p-core`, having `libp2p-xxx` protocol crates only depend on `libp2p-core` and thus allowing general breaking changes to `Swarm` without breaking all `libp2p-xxx` crates.
1 parent 461a7a9 commit ca0e9f9

29 files changed

+1324
-3203
lines changed

core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 0.32.0 [unreleased]
22

3+
- Remove `Network`. `libp2p-core` is from now on an auxiliary crate only. Users
4+
that have previously used `Network` only, will need to use `Swarm` instead. See
5+
[PR 2492].
6+
37
- Update to `multiaddr` `v0.14.0`.
48

59
- Update to `multihash` `v0.16.0`.
@@ -10,6 +14,7 @@
1014

1115
[PR 2456]: https://github.com/libp2p/rust-libp2p/pull/2456
1216
[RUSTSEC-2022-0009]: https://rustsec.org/advisories/RUSTSEC-2022-0009.html
17+
[PR 2492]: https://github.com/libp2p/rust-libp2p/pull/2492
1318

1419
# 0.31.0 [2022-01-27]
1520

core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ criterion = "0.3"
4949
libp2p-mplex = { path = "../muxers/mplex" }
5050
libp2p-noise = { path = "../transports/noise" }
5151
libp2p-tcp = { path = "../transports/tcp" }
52-
serde_json = "1.0"
53-
rmp-serde = "1.0"
5452
multihash = { version = "0.16", default-features = false, features = ["arb"] }
5553
quickcheck = "0.9.0"
5654
rand07 = { package = "rand", version = "0.7" }
55+
rmp-serde = "1.0"
56+
serde_json = "1.0"
5757

5858
[build-dependencies]
5959
prost-build = "0.9"

core/src/connection.rs

Lines changed: 28 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
mod error;
22-
pub(crate) mod handler;
23-
mod listeners;
24-
mod substream;
25-
26-
pub(crate) mod pool;
27-
28-
pub use error::{
29-
ConnectionError, PendingConnectionError, PendingInboundConnectionError,
30-
PendingOutboundConnectionError,
31-
};
32-
pub use handler::{ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler};
33-
pub use listeners::{ListenerId, ListenersEvent, ListenersStream};
34-
pub use pool::{ConnectionCounters, ConnectionLimits};
35-
pub use pool::{EstablishedConnection, EstablishedConnectionIter, PendingConnection};
36-
pub use substream::{Close, Substream, SubstreamEndpoint};
37-
3821
use crate::multiaddr::{Multiaddr, Protocol};
39-
use crate::muxing::StreamMuxer;
40-
use crate::PeerId;
41-
use std::hash::Hash;
42-
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
43-
use substream::{Muxing, SubstreamEvent};
4422

4523
/// Connection identifier.
4624
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
@@ -53,7 +31,34 @@ impl ConnectionId {
5331
/// in test environments. There is in general no guarantee
5432
/// that all connection IDs are based on non-negative integers.
5533
pub fn new(id: usize) -> Self {
56-
ConnectionId(id)
34+
Self(id)
35+
}
36+
}
37+
38+
impl std::ops::Add<usize> for ConnectionId {
39+
type Output = Self;
40+
41+
fn add(self, other: usize) -> Self {
42+
Self(self.0 + other)
43+
}
44+
}
45+
46+
/// The ID of a single listener.
47+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
48+
pub struct ListenerId(u64);
49+
50+
impl ListenerId {
51+
/// Creates a `ListenerId` from a non-negative integer.
52+
pub fn new(id: u64) -> Self {
53+
Self(id)
54+
}
55+
}
56+
57+
impl std::ops::Add<u64> for ListenerId {
58+
type Output = Self;
59+
60+
fn add(self, other: u64) -> Self {
61+
Self(self.0 + other)
5762
}
5863
}
5964

@@ -236,181 +241,3 @@ impl ConnectedPoint {
236241
}
237242
}
238243
}
239-
240-
/// Information about a successfully established connection.
241-
#[derive(Debug, Clone, PartialEq, Eq)]
242-
pub struct Connected {
243-
/// The connected endpoint, including network address information.
244-
pub endpoint: ConnectedPoint,
245-
/// Information obtained from the transport.
246-
pub peer_id: PeerId,
247-
}
248-
249-
/// Event generated by a [`Connection`].
250-
#[derive(Debug, Clone)]
251-
pub enum Event<T> {
252-
/// Event generated by the [`ConnectionHandler`].
253-
Handler(T),
254-
/// Address of the remote has changed.
255-
AddressChange(Multiaddr),
256-
}
257-
258-
/// A multiplexed connection to a peer with an associated `ConnectionHandler`.
259-
pub struct Connection<TMuxer, THandler>
260-
where
261-
TMuxer: StreamMuxer,
262-
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
263-
{
264-
/// Node that handles the muxing.
265-
muxing: substream::Muxing<TMuxer, THandler::OutboundOpenInfo>,
266-
/// Handler that processes substreams.
267-
handler: THandler,
268-
}
269-
270-
impl<TMuxer, THandler> fmt::Debug for Connection<TMuxer, THandler>
271-
where
272-
TMuxer: StreamMuxer,
273-
THandler: ConnectionHandler<Substream = Substream<TMuxer>> + fmt::Debug,
274-
{
275-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
276-
f.debug_struct("Connection")
277-
.field("muxing", &self.muxing)
278-
.field("handler", &self.handler)
279-
.finish()
280-
}
281-
}
282-
283-
impl<TMuxer, THandler> Unpin for Connection<TMuxer, THandler>
284-
where
285-
TMuxer: StreamMuxer,
286-
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
287-
{
288-
}
289-
290-
impl<TMuxer, THandler> Connection<TMuxer, THandler>
291-
where
292-
TMuxer: StreamMuxer,
293-
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
294-
{
295-
/// Builds a new `Connection` from the given substream multiplexer
296-
/// and connection handler.
297-
pub fn new(muxer: TMuxer, handler: THandler) -> Self {
298-
Connection {
299-
muxing: Muxing::new(muxer),
300-
handler,
301-
}
302-
}
303-
304-
/// Returns a reference to the `ConnectionHandler`
305-
pub fn handler(&self) -> &THandler {
306-
&self.handler
307-
}
308-
309-
/// Returns a mutable reference to the `ConnectionHandler`
310-
pub fn handler_mut(&mut self) -> &mut THandler {
311-
&mut self.handler
312-
}
313-
314-
/// Notifies the connection handler of an event.
315-
pub fn inject_event(&mut self, event: THandler::InEvent) {
316-
self.handler.inject_event(event);
317-
}
318-
319-
/// Begins an orderly shutdown of the connection, returning the connection
320-
/// handler and a `Future` that resolves when connection shutdown is complete.
321-
pub fn close(self) -> (THandler, Close<TMuxer>) {
322-
(self.handler, self.muxing.close().0)
323-
}
324-
325-
/// Polls the connection for events produced by the associated handler
326-
/// as a result of I/O activity on the substream multiplexer.
327-
pub fn poll(
328-
mut self: Pin<&mut Self>,
329-
cx: &mut Context<'_>,
330-
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
331-
loop {
332-
let mut io_pending = false;
333-
334-
// Perform I/O on the connection through the muxer, informing the handler
335-
// of new substreams.
336-
match self.muxing.poll(cx) {
337-
Poll::Pending => io_pending = true,
338-
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => self
339-
.handler
340-
.inject_substream(substream, SubstreamEndpoint::Listener),
341-
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream {
342-
user_data,
343-
substream,
344-
})) => {
345-
let endpoint = SubstreamEndpoint::Dialer(user_data);
346-
self.handler.inject_substream(substream, endpoint)
347-
}
348-
Poll::Ready(Ok(SubstreamEvent::AddressChange(address))) => {
349-
self.handler.inject_address_change(&address);
350-
return Poll::Ready(Ok(Event::AddressChange(address)));
351-
}
352-
Poll::Ready(Err(err)) => return Poll::Ready(Err(ConnectionError::IO(err))),
353-
}
354-
355-
// Poll the handler for new events.
356-
match self.handler.poll(cx) {
357-
Poll::Pending => {
358-
if io_pending {
359-
return Poll::Pending; // Nothing to do
360-
}
361-
}
362-
Poll::Ready(Ok(ConnectionHandlerEvent::OutboundSubstreamRequest(user_data))) => {
363-
self.muxing.open_substream(user_data);
364-
}
365-
Poll::Ready(Ok(ConnectionHandlerEvent::Custom(event))) => {
366-
return Poll::Ready(Ok(Event::Handler(event)));
367-
}
368-
Poll::Ready(Err(err)) => return Poll::Ready(Err(ConnectionError::Handler(err))),
369-
}
370-
}
371-
}
372-
}
373-
374-
/// Borrowed information about an incoming connection currently being negotiated.
375-
#[derive(Debug, Copy, Clone)]
376-
pub struct IncomingInfo<'a> {
377-
/// Local connection address.
378-
pub local_addr: &'a Multiaddr,
379-
/// Address used to send back data to the remote.
380-
pub send_back_addr: &'a Multiaddr,
381-
}
382-
383-
impl<'a> IncomingInfo<'a> {
384-
/// Builds the [`PendingPoint`] corresponding to the incoming connection.
385-
pub fn to_pending_point(&self) -> PendingPoint {
386-
PendingPoint::Listener {
387-
local_addr: self.local_addr.clone(),
388-
send_back_addr: self.send_back_addr.clone(),
389-
}
390-
}
391-
/// Builds the [`ConnectedPoint`] corresponding to the incoming connection.
392-
pub fn to_connected_point(&self) -> ConnectedPoint {
393-
ConnectedPoint::Listener {
394-
local_addr: self.local_addr.clone(),
395-
send_back_addr: self.send_back_addr.clone(),
396-
}
397-
}
398-
}
399-
400-
/// Information about a connection limit.
401-
#[derive(Debug, Clone)]
402-
pub struct ConnectionLimit {
403-
/// The maximum number of connections.
404-
pub limit: u32,
405-
/// The current number of connections.
406-
pub current: u32,
407-
}
408-
409-
impl fmt::Display for ConnectionLimit {
410-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411-
write!(f, "{}/{}", self.current, self.limit)
412-
}
413-
}
414-
415-
/// A `ConnectionLimit` can represent an error if it has been exceeded.
416-
impl Error for ConnectionLimit {}

core/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,16 @@ pub mod connection;
6161
pub mod either;
6262
pub mod identity;
6363
pub mod muxing;
64-
pub mod network;
6564
pub mod peer_record;
6665
pub mod signed_envelope;
6766
pub mod transport;
6867
pub mod upgrade;
6968

70-
pub use connection::{Connected, ConnectedPoint, Endpoint};
69+
pub use connection::{ConnectedPoint, Endpoint};
7170
pub use identity::PublicKey;
7271
pub use multiaddr::Multiaddr;
7372
pub use multihash;
7473
pub use muxing::StreamMuxer;
75-
pub use network::{DialOpts, Network};
7674
pub use peer_id::PeerId;
7775
pub use peer_record::PeerRecord;
7876
pub use signed_envelope::SignedEnvelope;

0 commit comments

Comments
 (0)