Skip to content

Commit

Permalink
refactor(*): Transport redesign
Browse files Browse the repository at this point in the history
Resolves: libp2p#4226.
Resolves: libp2p#3953.
Resolves: libp2p#3889.

Pull-Request: libp2p#4568.
  • Loading branch information
umgefahren committed Aug 3, 2024
1 parent 53eb339 commit 079b2d6
Show file tree
Hide file tree
Showing 129 changed files with 1,294 additions and 1,175 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 19 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,45 +76,44 @@ futures-bounded = { version = "0.2.4" }
futures-rustls = { version = "0.26.0", default-features = false }
libp2p = { version = "0.54.0", path = "libp2p" }
libp2p-allow-block-list = { version = "0.3.0", path = "misc/allow-block-list" }
libp2p-autonat = { version = "0.12.1", path = "protocols/autonat" }
libp2p-autonat = { version = "0.13.0", path = "protocols/autonat" }
libp2p-connection-limits = { version = "0.3.1", path = "misc/connection-limits" }
libp2p-core = { version = "0.41.3", path = "core" }
libp2p-dcutr = { version = "0.11.1", path = "protocols/dcutr" }
libp2p-dns = { version = "0.41.1", path = "transports/dns" }
libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" }
libp2p-core = { version = "0.42.0", path = "core" }
libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" }
libp2p-dns = { version = "0.42.0", path = "transports/dns" }
libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.45.0", path = "protocols/identify" }
libp2p-identity = { version = "0.2.9" }
libp2p-kad = { version = "0.46.0", path = "protocols/kad" }
libp2p-mdns = { version = "0.45.2", path = "protocols/mdns" }
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.14.2", path = "misc/metrics" }
libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
libp2p-noise = { version = "0.44.0", path = "transports/noise" }
libp2p-perf = { version = "0.4.0", path = "protocols/perf" }
libp2p-ping = { version = "0.44.2", path = "protocols/ping" }
libp2p-ping = { version = "0.45.0", path = "protocols/ping" }
libp2p-plaintext = { version = "0.41.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.24.0", path = "transports/pnet" }
libp2p-quic = { version = "0.10.3", path = "transports/quic" }
libp2p-relay = { version = "0.17.3", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.14.1", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.4", path = "protocols/request-response" }
libp2p-quic = { version = "0.11.0", path = "transports/quic" }
libp2p-relay = { version = "0.18.0", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" }
libp2p-server = { version = "0.12.7", path = "misc/server" }
libp2p-stream = { version = "0.1.0-alpha.1", path = "protocols/stream" }
libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" }
libp2p-swarm = { version = "0.45.0", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.2", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.42.0", path = "transports/tcp" }
libp2p-tls = { version = "0.4.1", path = "transports/tls" }
libp2p-uds = { version = "0.40.0", path = "transports/uds" }
libp2p-upnp = { version = "0.2.2", path = "protocols/upnp" }
libp2p-webrtc = { version = "0.7.1-alpha", path = "transports/webrtc" }
libp2p-uds = { version = "0.41.0", path = "transports/uds" }
libp2p-upnp = { version = "0.3.0", path = "protocols/upnp" }
libp2p-webrtc = { version = "0.8.0-alpha", path = "transports/webrtc" }
libp2p-webrtc-utils = { version = "0.2.1", path = "misc/webrtc-utils" }
libp2p-webrtc-websys = { version = "0.3.0-alpha", path = "transports/webrtc-websys" }
libp2p-websocket = { version = "0.43.2", path = "transports/websocket" }
libp2p-websocket-websys = { version = "0.3.3", path = "transports/websocket-websys" }
libp2p-webtransport-websys = { version = "0.3.0", path = "transports/webtransport-websys" }
libp2p-webrtc-websys = { version = "0.4.0-alpha", path = "transports/webrtc-websys" }
libp2p-websocket = { version = "0.44.0", path = "transports/websocket" }
libp2p-websocket-websys = { version = "0.4.0", path = "transports/websocket-websys" }
libp2p-webtransport-websys = { version = "0.4.0", path = "transports/webtransport-websys" }
libp2p-yamux = { version = "0.45.2", path = "muxers/yamux" }
multiaddr = "0.18.1"
multihash = "0.19.1"
Expand Down
11 changes: 11 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 0.42.0

- Update `Transport::dial` function signature with a `DialOpts` param and remove `Transport::dial_as_listener`:
- `DialOpts` struct contains `PortUse` and `Endpoint`,
- `PortUse` allows controling port allocation of new connections (defaults to `PortUse::Reuse`) -
- Add `port_use` field to `ConnectedPoint`
- Set `endpoint` field in `DialOpts` to `Endpoint::Listener` to dial as a listener
- Remove `Transport::address_translation` and relocate functionality to `libp2p_swarm`

See [PR 4568].

## 0.41.3
- Use `web-time` instead of `instant`.
See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347).
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-core"
edition = "2021"
rust-version = { workspace = true }
description = "Core traits and structs of libp2p"
version = "0.41.3"
version = "0.42.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
13 changes: 12 additions & 1 deletion core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::multiaddr::{Multiaddr, Protocol};
use crate::{
multiaddr::{Multiaddr, Protocol},
transport::PortUse,
};

/// The endpoint roles associated with a peer-to-peer communication channel.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -80,6 +83,13 @@ pub enum ConnectedPoint {
/// connection as a dialer and one peer dial the other and upgrade the
/// connection _as a listener_ overriding its role.
role_override: Endpoint,
/// Whether the port for the outgoing connection was reused from a listener
/// or a new port was allocated. This is useful for address translation.
///
/// The port use is implemented on a best-effort basis. It is not guaranteed
/// that [`PortUse::Reuse`] actually reused a port. A good example is the case
/// where there is no listener available to reuse a port from.
port_use: PortUse,
},
/// We received the node.
Listener {
Expand Down Expand Up @@ -133,6 +143,7 @@ impl ConnectedPoint {
ConnectedPoint::Dialer {
address,
role_override: _,
port_use: _,
} => address,
ConnectedPoint::Listener { local_addr, .. } => local_addr,
}
Expand Down
36 changes: 6 additions & 30 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use crate::muxing::StreamMuxerEvent;
use crate::transport::DialOpts;
use crate::{
muxing::StreamMuxer,
transport::{ListenerId, Transport, TransportError, TransportEvent},
Expand Down Expand Up @@ -172,48 +173,23 @@ where
}
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
use TransportError::*;
match self {
Either::Left(a) => match a.dial(addr) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
Either::Right(b) => match b.dial(addr) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Right(err))),
},
}
}

fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
) -> Result<Self::Dial, TransportError<Self::Error>>
where
Self: Sized,
{
opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>> {
use TransportError::*;
match self {
Either::Left(a) => match a.dial_as_listener(addr) {
Either::Left(a) => match a.dial(addr, opts) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
Either::Right(b) => match b.dial_as_listener(addr) {
Either::Right(b) => match b.dial(addr, opts) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Right(err))),
},
}
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
match self {
Either::Left(a) => a.address_translation(server, observed),
Either::Right(b) => b.address_translation(server, observed),
}
}
}
3 changes: 0 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ mod proto {
pub use multiaddr;
pub type Negotiated<T> = multistream_select::Negotiated<T>;

mod translation;

pub mod connection;
pub mod either;
pub mod muxing;
Expand All @@ -62,7 +60,6 @@ pub use multihash;
pub use muxing::StreamMuxer;
pub use peer_record::PeerRecord;
pub use signed_envelope::SignedEnvelope;
pub use translation::address_translation;
pub use transport::Transport;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};

Expand Down
Loading

0 comments on commit 079b2d6

Please sign in to comment.