Skip to content

Commit

Permalink
Replace the macro in libp2p-tcp with a generic, inner implementation
Browse files Browse the repository at this point in the history
To avoid code duplication, a generic inner implementation is used
that does all of the heavy lifting. To abstract over the individual
types, we create traits that are private to the module.

Consumers of the crate shouldn't be concerned with this because
a) none of the generic types are exported
b) there are no type parameters in public interfaces
  • Loading branch information
thomaseizinger committed Mar 30, 2020
1 parent 0d3e4f2 commit 47afe64
Show file tree
Hide file tree
Showing 21 changed files with 880 additions and 666 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default = [
"pnet",
"secio",
"secp256k1",
"tcp",
"tcp-async-std",
"uds",
"wasm-ext",
"websocket",
Expand All @@ -44,7 +44,8 @@ ping = ["libp2p-ping"]
plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
secio = ["libp2p-secio"]
tcp = ["libp2p-tcp"]
tcp-async-std = ["libp2p-tcp", "libp2p-tcp/async-std"]
tcp-tokio = ["libp2p-tcp", "libp2p-tcp/tokio"]
uds = ["libp2p-uds"]
wasm-ext = ["libp2p-wasm-ext"]
websocket = ["libp2p-websocket"]
Expand Down Expand Up @@ -115,3 +116,6 @@ members = [
"transports/wasm-ext"
]

[[example]]
name = "ipfs-private"
required-features = ["tcp-async-std"]
2 changes: 1 addition & 1 deletion core/src/connection/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::{collections::VecDeque, fmt, pin::Pin};
/// use futures::prelude::*;
/// use libp2p_core::connection::{ListenersEvent, ListenersStream};
///
/// let mut listeners = ListenersStream::new(libp2p_tcp::TcpConfig::new());
/// let mut listeners = ListenersStream::new(libp2p_tcp::async_std::TcpConfig::new());
///
/// // Ask the `listeners` to start listening on the given multiaddress.
/// listeners.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions core/tests/network_dial_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn deny_incoming_connec() {
let mut swarm1 = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand All @@ -56,7 +56,7 @@ fn deny_incoming_connec() {
let mut swarm2 = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down Expand Up @@ -122,7 +122,7 @@ fn dial_self() {
let mut swarm = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down Expand Up @@ -197,7 +197,7 @@ fn dial_self_by_id() {
let mut swarm = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand All @@ -216,7 +216,7 @@ fn multiple_addresses_err() {
let mut swarm = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use libp2p::{
pnet::{PnetConfig, PreSharedKey},
secio::SecioConfig,
swarm::NetworkBehaviourEventProcess,
tcp::TcpConfig,
tcp::async_std::TcpConfig,
yamux::Config as YamuxConfig,
Multiaddr, NetworkBehaviour, PeerId, Swarm, Transport,
};
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/tests/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{muxing, upgrade, Transport};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::oneshot};
use std::sync::Arc;

Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/tests/two_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{muxing, upgrade, Transport};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{channel::oneshot, prelude::*};
use std::sync::Arc;

Expand Down
2 changes: 1 addition & 1 deletion protocols/deflate/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use futures::{future, prelude::*};
use libp2p_core::{transport::Transport, upgrade};
use libp2p_deflate::DeflateConfig;
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use quickcheck::{QuickCheck, RngCore, TestResult};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ mod tests {
Transport,
upgrade
};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use libp2p_secio::SecioConfig;
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_mplex::MplexConfig;
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn parse_proto_msg(msg: impl AsRef<[u8]>) -> Result<(IdentifyInfo, Multiaddr), i
#[cfg(test)]
mod tests {
use crate::protocol::{IdentifyInfo, RemoteInfo, IdentifyProtocolConfig};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::oneshot};
use libp2p_core::{
identity,
Expand Down
2 changes: 1 addition & 1 deletion protocols/noise/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//!
//! ```
//! use libp2p_core::{identity, Transport, upgrade};
//! use libp2p_tcp::TcpConfig;
//! use libp2p_tcp::async_std::TcpConfig;
//! use libp2p_noise::{Keypair, X25519, NoiseConfig};
//!
//! # fn main() {
Expand Down
2 changes: 1 addition & 1 deletion protocols/noise/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use libp2p_core::identity;
use libp2p_core::upgrade::{self, Negotiated, apply_inbound, apply_outbound};
use libp2p_core::transport::{Transport, ListenerEvent};
use libp2p_noise::{Keypair, X25519, NoiseConfig, RemoteIdentity, NoiseError, NoiseOutput};
use libp2p_tcp::{TcpConfig, TcpTransStream};
use libp2p_tcp::{async_std::TcpConfig, async_std::TcpTransStream};
use log::info;
use quickcheck::QuickCheck;
use std::{convert::TryInto, io};
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use libp2p_core::{
use libp2p_ping::*;
use libp2p_secio::SecioConfig;
use libp2p_swarm::Swarm;
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::mpsc};
use std::{io, time::Duration};

Expand Down
2 changes: 1 addition & 1 deletion protocols/secio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! use libp2p_core::{PeerId, Multiaddr, identity, upgrade};
//! use libp2p_core::transport::Transport;
//! use libp2p_mplex::MplexConfig;
//! use libp2p_tcp::TcpConfig;
//! use libp2p_tcp::async_std::TcpConfig;
//!
//! // Create a local peer identity.
//! let local_keys = identity::Keypair::generate_ed25519();
Expand Down
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! Example (Dialing a TCP/IP multi-address):
//!
//! ```rust
//! use libp2p::{Multiaddr, Transport, tcp::TcpConfig};
//! use libp2p::{Multiaddr, Transport, tcp::async_std::TcpConfig};
//! let tcp = TcpConfig::new();
//! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr");
//! let _conn = tcp.dial(addr);
Expand Down Expand Up @@ -85,8 +85,8 @@
//! Example ([`secio`] + [`yamux`] Protocol Upgrade):
//!
//! ```rust
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "secio", feature = "yamux"))] {
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "secio", feature = "yamux"))] {
//! use libp2p::{Transport, core::upgrade, tcp::async_std::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
//! let tcp = TcpConfig::new();
//! let secio = SecioConfig::new(Keypair::generate_ed25519());
//! let yamux = yamux::Config::default();
Expand Down Expand Up @@ -141,7 +141,7 @@
//! [`Keypair`]: identity::Keypair
//! [`PublicKey`]: identity::PublicKey
//! [`Future`]: futures::Future
//! [`TcpConfig`]: tcp::TcpConfig
//! [`TcpConfig`]: tcp::async_std::TcpConfig
//! [`NetworkBehaviour`]: swarm::NetworkBehaviour
//! [`StreamMuxer`]: core::muxing::StreamMuxer
//! [`Yamux`]: yamux::Yamux
Expand Down Expand Up @@ -217,8 +217,8 @@ pub use libp2p_plaintext as plaintext;
pub use libp2p_secio as secio;
#[doc(inline)]
pub use libp2p_swarm as swarm;
#[cfg(feature = "tcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
#[cfg(any(feature = "tcp-async-std", feature = "tcp-tokio-std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-std", feature = "tcp-tokio-std"))))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_tcp as tcp;
Expand Down Expand Up @@ -268,8 +268,8 @@ use std::{error, io, time::Duration};
///
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
/// > reserves the right to support additional protocols or remove deprecated protocols.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
pub fn build_development_transport(keypair: identity::Keypair)
-> io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<io::Error>> + Send + Sync), Error = impl error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
Expand All @@ -282,13 +282,13 @@ pub fn build_development_transport(keypair: identity::Keypair)
/// and mplex or yamux as the multiplexing layer.
///
/// > **Note**: If you ever need to express the type of this `Transport`.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
-> io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<io::Error>> + Send + Sync), Error = impl error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true);
let tcp = tcp::async_std::TcpConfig::new().nodelay(true);
let transport = dns::DnsConfig::new(tcp)?;
let trans_clone = transport.clone();
transport.or_transport(websocket::WsConfig::new(trans_clone))
Expand All @@ -308,13 +308,13 @@ pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
/// and mplex or yamux as the multiplexing layer.
///
/// > **Note**: If you ever need to express the type of this `Transport`.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))))]
pub fn build_tcp_ws_pnet_secio_mplex_yamux(keypair: identity::Keypair, psk: PreSharedKey)
-> io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<io::Error>> + Send + Sync), Error = impl error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true);
let tcp = tcp::async_std::TcpConfig::new().nodelay(true);
let transport = dns::DnsConfig::new(tcp)?;
let trans_clone = transport.clone();
transport.or_transport(websocket::WsConfig::new(trans_clone))
Expand Down
3 changes: 2 additions & 1 deletion transports/tcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ipnet = "2.0.0"
libp2p-core = { version = "0.16.0", path = "../../core" }
log = "0.4.1"
tokio = { version = "0.2", default-features = false, features = ["tcp"], optional = true }
async-trait = "0.1"

[features]
default = ["async-std"]
default = []
Loading

0 comments on commit 47afe64

Please sign in to comment.