Skip to content

Commit

Permalink
fix: Remove circular dependencies across workspace (libp2p#3023)
Browse files Browse the repository at this point in the history
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 libp2p#3111 and libp2p#2888 are fixed.

To ensure we don't accidentally reintroduce this dependency, we add a basic CI that queries `cargo metadata` using `jq`.

Resolves libp2p#3053.
Fixes libp2p#3223.
Related: libp2p#2918 (comment)
Related: googleapis/release-please#1662
  • Loading branch information
thomaseizinger authored Dec 12, 2022
1 parent d034477 commit 05dd63d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions examples/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +66,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
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());

Expand All @@ -89,7 +96,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "Event")]
#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")]
struct Behaviour {
identify: identify::Behaviour,
auto_nat: autonat::Behaviour,
Expand Down
20 changes: 13 additions & 7 deletions examples/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -53,7 +55,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
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());

Expand All @@ -74,7 +80,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "Event")]
#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")]
struct Behaviour {
identify: identify::Behaviour,
auto_nat: autonat::Behaviour,
Expand Down
17 changes: 10 additions & 7 deletions tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +37,11 @@ const TEST_REFRESH_INTERVAL: Duration = Duration::from_secs(2);
async fn init_swarm(config: Config) -> Swarm<Behaviour> {
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)
}
Expand Down
24 changes: 14 additions & 10 deletions tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Behaviour> {
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)
}
Expand Down

0 comments on commit 05dd63d

Please sign in to comment.