From 05dd63dd1ba848adbdca36fbfdb04b4fcf68c67c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 13 Dec 2022 07:58:01 +1100 Subject: [PATCH] fix: Remove circular dependencies across workspace (#3023) Circular dependencies are problematic in several ways: - They result in cognitive overhead for developers, in trying to figure out what depends on what. - They present `cargo` with limits in what order the crates can be compiled in. - They invalidate build caches unnecessarily thus forcing `cargo` to rebuild certain crates. - They cause problems with tooling such as `release-please`. To actually break the circular dependencies, this patch inlines the uses of `development_transport` in the examples and tests for all sub-crates. This is only meant to be a short-term fix until https://github.com/libp2p/rust-libp2p/issues/3111 and https://github.com/libp2p/rust-libp2p/pull/2888 are fixed. To ensure we don't accidentally reintroduce this dependency, we add a basic CI that queries `cargo metadata` using `jq`. Resolves https://github.com/libp2p/rust-libp2p/issues/3053. Fixes https://github.com/libp2p/rust-libp2p/issues/3223. Related: https://github.com/libp2p/rust-libp2p/pull/2918#discussion_r976514245 Related: https://github.com/googleapis/release-please/issues/1662 --- Cargo.toml | 8 ++++++-- examples/autonat_client.rs | 21 ++++++++++++++------- examples/autonat_server.rs | 20 +++++++++++++------- tests/test_client.rs | 17 ++++++++++------- tests/test_server.rs | 24 ++++++++++++++---------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb4d12b0a15..6fbad657900 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,9 +27,13 @@ prost = "0.11" [dev-dependencies] async-std = { version = "1.10", features = ["attributes"] } -env_logger = "0.10" clap = { version = "4.0.13", features = ["derive"] } -libp2p = { path = "../..", features = ["full"] } +env_logger = "0.10" +libp2p-identify = { path = "../identify" } +libp2p-noise = { path = "../../transports/noise" } +libp2p-swarm = { path = "../../swarm", features = ["async-std", "macros"] } +libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] } +libp2p-yamux = { path = "../../muxers/yamux" } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/examples/autonat_client.rs b/examples/autonat_client.rs index 1c897620db6..c124dcf98b6 100644 --- a/examples/autonat_client.rs +++ b/examples/autonat_client.rs @@ -31,11 +31,14 @@ use clap::Parser; use futures::prelude::*; -use libp2p::autonat; -use libp2p::identify; -use libp2p::multiaddr::Protocol; -use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p::{identity, Multiaddr, PeerId}; +use libp2p_autonat as autonat; +use libp2p_core::multiaddr::Protocol; +use libp2p_core::{identity, upgrade::Version, Multiaddr, PeerId, Transport}; +use libp2p_identify as identify; +use libp2p_noise as noise; +use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent}; +use libp2p_tcp as tcp; +use libp2p_yamux as yamux; use std::error::Error; use std::net::Ipv4Addr; use std::time::Duration; @@ -63,7 +66,11 @@ async fn main() -> Result<(), Box> { let local_peer_id = PeerId::from(local_key.public()); println!("Local peer id: {:?}", local_peer_id); - let transport = libp2p::development_transport(local_key.clone()).await?; + let transport = tcp::async_io::Transport::default() + .upgrade(Version::V1) + .authenticate(noise::NoiseAuthenticated::xx(&local_key)?) + .multiplex(yamux::YamuxConfig::default()) + .boxed(); let behaviour = Behaviour::new(local_key.public()); @@ -89,7 +96,7 @@ async fn main() -> Result<(), Box> { } #[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event")] +#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { identify: identify::Behaviour, auto_nat: autonat::Behaviour, diff --git a/examples/autonat_server.rs b/examples/autonat_server.rs index a3bcda1ee34..a5e98012f79 100644 --- a/examples/autonat_server.rs +++ b/examples/autonat_server.rs @@ -28,11 +28,13 @@ use clap::Parser; use futures::prelude::*; -use libp2p::autonat; -use libp2p::identify; -use libp2p::multiaddr::Protocol; -use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p::{identity, Multiaddr, PeerId}; +use libp2p_autonat as autonat; +use libp2p_core::{identity, multiaddr::Protocol, upgrade::Version, Multiaddr, PeerId, Transport}; +use libp2p_identify as identify; +use libp2p_noise as noise; +use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent}; +use libp2p_tcp as tcp; +use libp2p_yamux as yamux; use std::error::Error; use std::net::Ipv4Addr; @@ -53,7 +55,11 @@ async fn main() -> Result<(), Box> { let local_peer_id = PeerId::from(local_key.public()); println!("Local peer id: {:?}", local_peer_id); - let transport = libp2p::development_transport(local_key.clone()).await?; + let transport = tcp::async_io::Transport::default() + .upgrade(Version::V1) + .authenticate(noise::NoiseAuthenticated::xx(&local_key)?) + .multiplex(yamux::YamuxConfig::default()) + .boxed(); let behaviour = Behaviour::new(local_key.public()); @@ -74,7 +80,7 @@ async fn main() -> Result<(), Box> { } #[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event")] +#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { identify: identify::Behaviour, auto_nat: autonat::Behaviour, diff --git a/tests/test_client.rs b/tests/test_client.rs index 5e23304e3f2..1fd03925e38 100644 --- a/tests/test_client.rs +++ b/tests/test_client.rs @@ -20,15 +20,14 @@ use futures::{channel::oneshot, Future, FutureExt, StreamExt}; use futures_timer::Delay; -use libp2p::{ - development_transport, - identity::Keypair, - swarm::{AddressScore, Swarm, SwarmEvent}, - Multiaddr, PeerId, -}; use libp2p_autonat::{ Behaviour, Config, Event, NatStatus, OutboundProbeError, OutboundProbeEvent, ResponseError, }; +use libp2p_core::{identity::Keypair, upgrade::Version, Multiaddr, PeerId, Transport}; +use libp2p_noise as noise; +use libp2p_swarm::{AddressScore, Swarm, SwarmEvent}; +use libp2p_tcp as tcp; +use libp2p_yamux as yamux; use std::time::Duration; const MAX_CONFIDENCE: usize = 3; @@ -38,7 +37,11 @@ const TEST_REFRESH_INTERVAL: Duration = Duration::from_secs(2); async fn init_swarm(config: Config) -> Swarm { let keypair = Keypair::generate_ed25519(); let local_id = PeerId::from_public_key(&keypair.public()); - let transport = development_transport(keypair).await.unwrap(); + let transport = tcp::async_io::Transport::default() + .upgrade(Version::V1) + .authenticate(noise::NoiseAuthenticated::xx(&keypair).unwrap()) + .multiplex(yamux::YamuxConfig::default()) + .boxed(); let behaviour = Behaviour::new(local_id, config); Swarm::with_async_std_executor(transport, behaviour, local_id) } diff --git a/tests/test_server.rs b/tests/test_server.rs index 57d20ef2a5f..140bb928bc5 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -20,24 +20,28 @@ use futures::{channel::oneshot, Future, FutureExt, StreamExt}; use futures_timer::Delay; -use libp2p::core::{ConnectedPoint, Endpoint}; -use libp2p::swarm::DialError; -use libp2p::{ - development_transport, - identity::Keypair, - multiaddr::Protocol, - swarm::{AddressScore, Swarm, SwarmEvent}, - Multiaddr, PeerId, -}; use libp2p_autonat::{ Behaviour, Config, Event, InboundProbeError, InboundProbeEvent, ResponseError, }; +use libp2p_core::{ + identity::Keypair, multiaddr::Protocol, upgrade::Version, ConnectedPoint, Endpoint, Multiaddr, + PeerId, Transport, +}; +use libp2p_noise as noise; +use libp2p_swarm::DialError; +use libp2p_swarm::{AddressScore, Swarm, SwarmEvent}; +use libp2p_tcp as tcp; +use libp2p_yamux as yamux; use std::{num::NonZeroU32, time::Duration}; async fn init_swarm(config: Config) -> Swarm { let keypair = Keypair::generate_ed25519(); let local_id = PeerId::from_public_key(&keypair.public()); - let transport = development_transport(keypair).await.unwrap(); + let transport = tcp::async_io::Transport::default() + .upgrade(Version::V1) + .authenticate(noise::NoiseAuthenticated::xx(&keypair).unwrap()) + .multiplex(yamux::YamuxConfig::default()) + .boxed(); let behaviour = Behaviour::new(local_id, config); Swarm::with_async_std_executor(transport, behaviour, local_id) }