mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-23 16:11:19 +00:00
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
This commit is contained in:
parent
f8f19baad0
commit
d7363a53d3
5
.github/workflows/ci.yml
vendored
5
.github/workflows/ci.yml
vendored
@ -75,6 +75,11 @@ jobs:
|
||||
cargo install cargo-semver-checks --locked
|
||||
cargo semver-checks check-release -p ${{ matrix.crate }}
|
||||
|
||||
- name: Enforce no dependency on meta crate
|
||||
run: |
|
||||
cargo metadata --format-version=1 --no-deps | \
|
||||
jq -e -r '.packages[] | select(.name == "${{ matrix.crate }}") | .dependencies | all(.name != "libp2p")'
|
||||
|
||||
cross:
|
||||
name: Compile on ${{ matrix.target }}
|
||||
strategy:
|
||||
|
@ -61,7 +61,6 @@ identify = ["dep:libp2p-identify", "libp2p-metrics?/identify"]
|
||||
kad = ["dep:libp2p-kad", "libp2p-metrics?/kad"]
|
||||
macros = ["libp2p-swarm/macros"]
|
||||
mdns = ["dep:libp2p-mdns"]
|
||||
tls = ["dep:libp2p-tls"]
|
||||
metrics = ["dep:libp2p-metrics"]
|
||||
mplex = ["dep:libp2p-mplex"]
|
||||
noise = ["dep:libp2p-noise"]
|
||||
@ -76,6 +75,7 @@ rsa = ["libp2p-core/rsa"]
|
||||
secp256k1 = ["libp2p-core/secp256k1"]
|
||||
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
|
||||
tcp = ["dep:libp2p-tcp"]
|
||||
tls = ["dep:libp2p-tls"]
|
||||
tokio = ["libp2p-swarm/tokio", "libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-quic?/tokio", "libp2p-webrtc?/tokio"]
|
||||
uds = ["dep:libp2p-uds"]
|
||||
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen"]
|
||||
|
@ -47,7 +47,8 @@ ring = { version = "0.16.9", features = ["alloc", "std"], default-features = fal
|
||||
async-std = { version = "1.6.2", features = ["attributes"] }
|
||||
base64 = "0.13.0"
|
||||
criterion = "0.4"
|
||||
libp2p = { path = "..", features = ["full"] }
|
||||
libp2p-mplex = { path = "../muxers/mplex" }
|
||||
libp2p-noise = { path = "../transports/noise" }
|
||||
multihash = { version = "0.16", default-features = false, features = ["arb"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" }
|
||||
rmp-serde = "1.0"
|
||||
|
@ -19,11 +19,11 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::transport::{MemoryTransport, Transport};
|
||||
use libp2p::core::upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use libp2p::mplex::MplexConfig;
|
||||
use libp2p::noise;
|
||||
use libp2p_core::identity;
|
||||
use libp2p_core::transport::{MemoryTransport, Transport};
|
||||
use libp2p_core::upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use libp2p_mplex::MplexConfig;
|
||||
use libp2p_noise as noise;
|
||||
use multiaddr::{Multiaddr, Protocol};
|
||||
use rand::random;
|
||||
use std::{io, pin::Pin};
|
||||
|
@ -32,11 +32,15 @@ prometheus-client = "0.18.0"
|
||||
libp2p-gossipsub = { version = "0.44.0", path = "../../protocols/gossipsub", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
log = "0.4.0"
|
||||
env_logger = "0.10.0"
|
||||
futures = "0.3.1"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
hyper = { version="0.14", features = ["server", "tcp", "http1"] }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-ping = { path = "../../protocols/ping" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["macros"] }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
log = "0.4.0"
|
||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
@ -45,3 +49,7 @@ tokio = { version = "1", features = ["rt-multi-thread"] }
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
rustc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[[example]]
|
||||
name = "metrics"
|
||||
required-features = ["ping"]
|
||||
|
@ -51,11 +51,17 @@
|
||||
use env_logger::Env;
|
||||
use futures::executor::block_on;
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::core::Multiaddr;
|
||||
use libp2p::metrics::{Metrics, Recorder};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
|
||||
use libp2p::{identify, identity, ping, PeerId, Swarm};
|
||||
use libp2p_core::{identity, upgrade::Version, Multiaddr, PeerId, Transport};
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_metrics::{Metrics, Recorder};
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_swarm::keep_alive;
|
||||
use libp2p_swarm::NetworkBehaviour;
|
||||
use libp2p_swarm::Swarm;
|
||||
use libp2p_swarm::SwarmEvent;
|
||||
use libp2p_tcp as tcp;
|
||||
use libp2p_yamux as yamux;
|
||||
use log::info;
|
||||
use prometheus_client::registry::Registry;
|
||||
use std::error::Error;
|
||||
@ -69,10 +75,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
let local_pub_key = local_key.public();
|
||||
info!("Local peer id: {:?}", local_peer_id);
|
||||
info!("Local peer id: {local_peer_id:?}");
|
||||
|
||||
let mut swarm = Swarm::without_executor(
|
||||
block_on(libp2p::development_transport(local_key))?,
|
||||
tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(noise::NoiseAuthenticated::xx(&local_key)?)
|
||||
.multiplex(yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
Behaviour::new(local_pub_key),
|
||||
local_peer_id,
|
||||
);
|
||||
@ -115,6 +125,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
/// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen
|
||||
/// and can be observed via the metrics.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
|
||||
struct Behaviour {
|
||||
identify: identify::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
@ -122,7 +133,7 @@ struct Behaviour {
|
||||
}
|
||||
|
||||
impl Behaviour {
|
||||
fn new(local_pub_key: libp2p::identity::PublicKey) -> Self {
|
||||
fn new(local_pub_key: identity::PublicKey) -> Self {
|
||||
Self {
|
||||
ping: ping::Behaviour::default(),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
|
@ -21,7 +21,10 @@ unsigned-varint = "0.7"
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.2"
|
||||
env_logger = "0.10"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-core = { path = "../../core" }
|
||||
libp2p-mplex = { path = "../../muxers/mplex" }
|
||||
libp2p-plaintext = { path = "../../transports/plaintext" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["async-std"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
rand = "0.8"
|
||||
rw-stream-sink = { version = "0.3.0", path = "../../misc/rw-stream-sink" }
|
||||
|
@ -19,16 +19,16 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::{channel::oneshot, prelude::*, ready};
|
||||
use libp2p::core::{
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
multiaddr::Protocol,
|
||||
muxing::StreamMuxerBox,
|
||||
transport::{self, MemoryTransport},
|
||||
upgrade, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use libp2p::mplex::MplexConfig;
|
||||
use libp2p::plaintext::PlainText2Config;
|
||||
use libp2p::swarm::{dummy, Swarm, SwarmEvent};
|
||||
use libp2p_mplex::MplexConfig;
|
||||
use libp2p_plaintext::PlainText2Config;
|
||||
use libp2p_swarm::{dummy, Swarm, SwarmEvent};
|
||||
use rand::random;
|
||||
use std::task::Poll;
|
||||
|
||||
|
@ -27,8 +27,9 @@ async-std = { version = "1.7.0", features = ["attributes"] }
|
||||
criterion = "0.4"
|
||||
env_logger = "0.10"
|
||||
futures = "0.3"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-muxer-test-harness = { path = "../test-harness" }
|
||||
libp2p-plaintext = { path = "../../transports/plaintext" }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
[[bench]]
|
||||
|
@ -26,12 +26,12 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughpu
|
||||
use futures::future::poll_fn;
|
||||
use futures::prelude::*;
|
||||
use futures::{channel::oneshot, future::join};
|
||||
use libp2p::core::muxing::StreamMuxerExt;
|
||||
use libp2p::core::{
|
||||
use libp2p_core::muxing::StreamMuxerExt;
|
||||
use libp2p_core::{
|
||||
identity, multiaddr::multiaddr, muxing, transport, upgrade, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use libp2p::plaintext::PlainText2Config;
|
||||
use libp2p::{mplex, tcp};
|
||||
use libp2p_mplex as mplex;
|
||||
use libp2p_plaintext::PlainText2Config;
|
||||
use std::pin::Pin;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -169,7 +169,7 @@ fn tcp_transport(split_send_size: usize) -> BenchTransport {
|
||||
let mut mplex = mplex::MplexConfig::default();
|
||||
mplex.set_split_send_size(split_send_size);
|
||||
|
||||
tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
|
||||
libp2p_tcp::async_io::Transport::new(libp2p_tcp::Config::default().nodelay(true))
|
||||
.upgrade(upgrade::Version::V1)
|
||||
.authenticate(PlainText2Config { local_public_key })
|
||||
.multiplex(mplex)
|
||||
|
@ -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
|
||||
|
@ -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<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());
|
||||
|
||||
@ -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,
|
||||
|
@ -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<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());
|
||||
|
||||
@ -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,
|
||||
|
@ -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<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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -29,10 +29,18 @@ void = "1"
|
||||
prost-build = "0.11"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
rand = "0.8"
|
||||
clap = { version = "4.0.13", features = ["derive"] }
|
||||
env_logger = "0.10.0"
|
||||
libp2p-dns = { path = "../../transports/dns", features = ["async-std"] }
|
||||
libp2p-identify = { path = "../../protocols/identify" }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-ping = { path = "../../protocols/ping" }
|
||||
libp2p-plaintext = { path = "../../transports/plaintext" }
|
||||
libp2p-relay = { path = "../relay" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["macros"] }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
rand = "0.8"
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
# More information: https://docs.rs/about/builds#cross-compiling
|
||||
|
@ -22,18 +22,19 @@ use clap::Parser;
|
||||
use futures::executor::{block_on, ThreadPool};
|
||||
use futures::future::FutureExt;
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p::core::transport::OrTransport;
|
||||
use libp2p::core::upgrade;
|
||||
use libp2p::dns::DnsConfig;
|
||||
use libp2p::identify;
|
||||
use libp2p::noise;
|
||||
use libp2p::relay::v2::client::{self, Client};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::tcp;
|
||||
use libp2p::Transport;
|
||||
use libp2p::{dcutr, ping};
|
||||
use libp2p::{identity, PeerId};
|
||||
use libp2p_core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p_core::transport::OrTransport;
|
||||
use libp2p_core::upgrade;
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_core::{identity, PeerId};
|
||||
use libp2p_dcutr as dcutr;
|
||||
use libp2p_dns::DnsConfig;
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_relay::v2::client::{self, Client};
|
||||
use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p_tcp as tcp;
|
||||
use log::info;
|
||||
use std::convert::TryInto;
|
||||
use std::error::Error;
|
||||
@ -100,11 +101,15 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
noise::NoiseAuthenticated::xx(&local_key)
|
||||
.expect("Signing libp2p-noise static DH keypair failed."),
|
||||
)
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "Event", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "Event",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Behaviour {
|
||||
relay_client: Client,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -23,17 +23,17 @@ use futures::future::FutureExt;
|
||||
use futures::io::{AsyncRead, AsyncWrite};
|
||||
use futures::stream::StreamExt;
|
||||
use futures::task::Spawn;
|
||||
use libp2p::core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p::core::muxing::StreamMuxerBox;
|
||||
use libp2p::core::transport::upgrade::Version;
|
||||
use libp2p::core::transport::{Boxed, MemoryTransport, OrTransport, Transport};
|
||||
use libp2p::core::PublicKey;
|
||||
use libp2p::core::{identity, PeerId};
|
||||
use libp2p::dcutr;
|
||||
use libp2p::plaintext::PlainText2Config;
|
||||
use libp2p::relay::v2::client;
|
||||
use libp2p::relay::v2::relay;
|
||||
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p_core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p_core::muxing::StreamMuxerBox;
|
||||
use libp2p_core::transport::upgrade::Version;
|
||||
use libp2p_core::transport::{Boxed, MemoryTransport, OrTransport, Transport};
|
||||
use libp2p_core::PublicKey;
|
||||
use libp2p_core::{identity, PeerId};
|
||||
use libp2p_dcutr as dcutr;
|
||||
use libp2p_plaintext::PlainText2Config;
|
||||
use libp2p_relay::v2::client;
|
||||
use libp2p_relay::v2::relay;
|
||||
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
@ -142,12 +142,16 @@ where
|
||||
transport
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(PlainText2Config { local_public_key })
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed()
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "ClientEvent", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "ClientEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Client {
|
||||
relay: client::Client,
|
||||
dcutr: dcutr::behaviour::Behaviour,
|
||||
|
@ -37,11 +37,15 @@ prometheus-client = "0.18.0"
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.3"
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
hex = "0.4.2"
|
||||
derive_builder = "0.11.1"
|
||||
env_logger = "0.10.0"
|
||||
hex = "0.4.2"
|
||||
libp2p-mplex = { path = "../../muxers/mplex" }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-plaintext = { path = "../../transports/plaintext" }
|
||||
libp2p-swarm = { path = "../../swarm" }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.11"
|
||||
|
@ -101,8 +101,8 @@
|
||||
//! // This is test transport (memory).
|
||||
//! let transport = MemoryTransport::default()
|
||||
//! .upgrade(libp2p_core::upgrade::Version::V1)
|
||||
//! .authenticate(libp2p::noise::NoiseAuthenticated::xx(&local_key).unwrap())
|
||||
//! .multiplex(libp2p::mplex::MplexConfig::new())
|
||||
//! .authenticate(libp2p_noise::NoiseAuthenticated::xx(&local_key).unwrap())
|
||||
//! .multiplex(libp2p_mplex::MplexConfig::new())
|
||||
//! .boxed();
|
||||
//!
|
||||
//! // Create a Gossipsub topic
|
||||
|
@ -29,16 +29,16 @@ use std::{
|
||||
};
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::{
|
||||
use libp2p_core::{
|
||||
identity, multiaddr::Protocol, transport::MemoryTransport, upgrade, Multiaddr, Transport,
|
||||
};
|
||||
use libp2p::gossipsub::{
|
||||
use libp2p_gossipsub::{
|
||||
Gossipsub, GossipsubConfigBuilder, GossipsubEvent, IdentTopic as Topic, MessageAuthenticity,
|
||||
ValidationMode,
|
||||
};
|
||||
use libp2p::plaintext::PlainText2Config;
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::yamux;
|
||||
use libp2p_plaintext::PlainText2Config;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_yamux as yamux;
|
||||
|
||||
struct Graph {
|
||||
pub nodes: Vec<(Multiaddr, Swarm<Gossipsub>)>,
|
||||
|
@ -27,7 +27,11 @@ void = "1.0"
|
||||
[dev-dependencies]
|
||||
async-std = { version = "1.6.2", features = ["attributes"] }
|
||||
env_logger = "0.10"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-mplex = { path = "../../muxers/mplex" }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["async-std"] }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.11"
|
||||
|
@ -37,7 +37,9 @@
|
||||
//! and will send each other identify info which is then printed to the console.
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::{identify, identity, Multiaddr, PeerId};
|
||||
use libp2p_core::upgrade::Version;
|
||||
use libp2p_core::{identity, Multiaddr, PeerId, Transport};
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use std::error::Error;
|
||||
|
||||
@ -47,7 +49,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 = libp2p_tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&local_key).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
|
||||
// Create a identify network behaviour.
|
||||
let behaviour = identify::Behaviour::new(identify::Config::new(
|
||||
|
@ -557,11 +557,11 @@ impl PeerCache {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::pin_mut;
|
||||
use libp2p::mplex::MplexConfig;
|
||||
use libp2p::noise;
|
||||
use libp2p::tcp;
|
||||
use libp2p_core::{identity, muxing::StreamMuxerBox, transport, upgrade, PeerId, Transport};
|
||||
use libp2p_mplex::MplexConfig;
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_tcp as tcp;
|
||||
use std::time::Duration;
|
||||
|
||||
fn transport() -> (
|
||||
|
@ -46,22 +46,22 @@ pub use self::protocol::{Info, UpgradeError, PROTOCOL_NAME, PUSH_PROTOCOL_NAME};
|
||||
|
||||
#[deprecated(
|
||||
since = "0.40.0",
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p::identify::Config`"
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p_identify::Config`"
|
||||
)]
|
||||
pub type IdentifyConfig = Config;
|
||||
|
||||
#[deprecated(
|
||||
since = "0.40.0",
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p::identify::Event`"
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p_identify::Event`"
|
||||
)]
|
||||
pub type IdentifyEvent = Event;
|
||||
|
||||
#[deprecated(since = "0.40.0", note = "Use libp2p::identify::Behaviour instead.")]
|
||||
#[deprecated(since = "0.40.0", note = "Use libp2p_identify::Behaviour instead.")]
|
||||
pub type Identify = Behaviour;
|
||||
|
||||
#[deprecated(
|
||||
since = "0.40.0",
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p::identify::Info`"
|
||||
note = "Use re-exports that omit `Identify` prefix, i.e. `libp2p_identify::Info`"
|
||||
)]
|
||||
pub type IdentifyInfo = Info;
|
||||
|
||||
|
@ -275,12 +275,12 @@ pub enum UpgradeError {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::channel::oneshot;
|
||||
use libp2p::tcp;
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
upgrade::{self, apply_inbound, apply_outbound},
|
||||
Transport,
|
||||
};
|
||||
use libp2p_tcp as tcp;
|
||||
|
||||
#[test]
|
||||
fn correct_transfer() {
|
||||
|
@ -35,7 +35,8 @@ thiserror = "1"
|
||||
[dev-dependencies]
|
||||
env_logger = "0.10.0"
|
||||
futures-timer = "3.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
[build-dependencies]
|
||||
|
@ -27,9 +27,6 @@ use crate::record::{store::MemoryStore, Key};
|
||||
use crate::K_VALUE;
|
||||
use futures::{executor::block_on, future::poll_fn, prelude::*};
|
||||
use futures_timer::Delay;
|
||||
use libp2p::noise;
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::yamux;
|
||||
use libp2p_core::{
|
||||
connection::{ConnectedPoint, ConnectionId},
|
||||
identity,
|
||||
@ -38,6 +35,9 @@ use libp2p_core::{
|
||||
transport::MemoryTransport,
|
||||
upgrade, Endpoint, PeerId, Transport,
|
||||
};
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_yamux as yamux;
|
||||
use quickcheck::*;
|
||||
use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng};
|
||||
use std::{
|
||||
|
@ -32,10 +32,12 @@ async-io = ["dep:async-io", "if-watch/smol"]
|
||||
[dev-dependencies]
|
||||
async-std = { version = "1.9.0", features = ["attributes"] }
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["tokio", "async-std"] }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["tokio", "async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
tokio = { version = "1.19", default-features = false, features = ["macros", "rt", "rt-multi-thread", "time"] }
|
||||
|
||||
|
||||
[[test]]
|
||||
name = "use-async-std"
|
||||
required-features = ["async-io"]
|
||||
|
@ -20,18 +20,17 @@
|
||||
|
||||
use std::{
|
||||
marker::Unpin,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
/// Simple wrapper for the differents type of timers
|
||||
/// Simple wrapper for the different type of timers
|
||||
#[derive(Debug)]
|
||||
#[cfg(any(feature = "async-io", feature = "tokio"))]
|
||||
pub struct Timer<T> {
|
||||
inner: T,
|
||||
}
|
||||
|
||||
/// Builder interface to homogenize the differents implementations
|
||||
/// Builder interface to homogenize the different implementations
|
||||
pub trait Builder: Send + Unpin + 'static {
|
||||
/// Creates a timer that emits an event once at the given time instant.
|
||||
fn at(instant: Instant) -> Self;
|
||||
@ -48,6 +47,10 @@ pub mod asio {
|
||||
use super::*;
|
||||
use async_io::Timer as AsioTimer;
|
||||
use futures::Stream;
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// Async Timer
|
||||
pub type AsyncTimer = Timer<AsioTimer>;
|
||||
@ -86,6 +89,10 @@ pub mod tokio {
|
||||
use super::*;
|
||||
use ::tokio::time::{self, Instant as TokioInstant, Interval, MissedTickBehavior};
|
||||
use futures::Stream;
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// Tokio wrapper
|
||||
pub type TokioTimer = Timer<Interval>;
|
||||
|
@ -19,12 +19,10 @@
|
||||
// DEALINGS IN THE SOFTWARE.use futures::StreamExt;
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
identity,
|
||||
mdns::{async_io::Behaviour, Config, Event},
|
||||
swarm::{Swarm, SwarmEvent},
|
||||
PeerId,
|
||||
};
|
||||
use libp2p_core::{identity, upgrade::Version, PeerId, Transport};
|
||||
use libp2p_mdns::Event;
|
||||
use libp2p_mdns::{async_io::Behaviour, Config};
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -60,7 +58,11 @@ async fn test_expired_async_std() -> Result<(), Box<dyn Error>> {
|
||||
async fn create_swarm(config: Config) -> Result<Swarm<Behaviour>, Box<dyn Error>> {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::development_transport(id_keys).await?;
|
||||
let transport = libp2p_tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&id_keys).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
let behaviour = Behaviour::new(config)?;
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
|
@ -18,12 +18,10 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.use futures::StreamExt;
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
identity,
|
||||
mdns::{tokio::Behaviour, Config, Event},
|
||||
swarm::{Swarm, SwarmEvent},
|
||||
PeerId,
|
||||
};
|
||||
use libp2p_core::{identity, upgrade::Version, PeerId, Transport};
|
||||
use libp2p_mdns::{tokio::Behaviour, Config, Event};
|
||||
use libp2p_swarm::Swarm;
|
||||
use libp2p_swarm::SwarmEvent;
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -56,7 +54,11 @@ async fn test_expired_tokio() -> Result<(), Box<dyn Error>> {
|
||||
async fn create_swarm(config: Config) -> Result<Swarm<Behaviour>, Box<dyn Error>> {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::tokio_development_transport(id_keys)?;
|
||||
let transport = libp2p_tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&id_keys).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
let behaviour = Behaviour::new(config)?;
|
||||
let mut swarm = Swarm::with_tokio_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
|
@ -22,7 +22,11 @@ void = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.2"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-mplex = { path = "../../muxers/mplex" }
|
||||
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" }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
|
@ -21,19 +21,19 @@
|
||||
//! Integration tests for the `Ping` network behaviour.
|
||||
|
||||
use futures::{channel::mpsc, prelude::*};
|
||||
use libp2p::core::{
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
muxing::StreamMuxerBox,
|
||||
transport::{self, Transport},
|
||||
upgrade, Multiaddr, PeerId,
|
||||
};
|
||||
use libp2p::mplex;
|
||||
use libp2p::noise;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::tcp;
|
||||
use libp2p::yamux;
|
||||
use libp2p_mplex as mplex;
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_swarm::keep_alive;
|
||||
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p_tcp as tcp;
|
||||
use libp2p_yamux as yamux;
|
||||
use quickcheck::*;
|
||||
use std::{num::NonZeroU8, time::Duration};
|
||||
|
||||
@ -271,6 +271,7 @@ impl Arbitrary for MuxerChoice {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour, Default)]
|
||||
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
|
||||
struct Behaviour {
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -33,10 +33,16 @@ void = "1"
|
||||
prost-build = "0.11"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
clap = { version = "4.0.13", features = ["derive"] }
|
||||
env_logger = "0.10.0"
|
||||
libp2p-identify = { path = "../../protocols/identify" }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-ping = { path = "../../protocols/ping" }
|
||||
libp2p-plaintext = { path = "../../transports/plaintext" }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["macros"] }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
# More information: https://docs.rs/about/builds#cross-compiling
|
||||
|
@ -22,16 +22,15 @@
|
||||
use clap::Parser;
|
||||
use futures::executor::block_on;
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::core::upgrade;
|
||||
use libp2p::identify;
|
||||
use libp2p::multiaddr::Protocol;
|
||||
use libp2p::ping;
|
||||
use libp2p::relay::v2::relay::{self, Relay};
|
||||
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::tcp;
|
||||
use libp2p::Transport;
|
||||
use libp2p::{identity, PeerId};
|
||||
use libp2p::{noise, Multiaddr};
|
||||
use libp2p_core::multiaddr::Protocol;
|
||||
use libp2p_core::upgrade;
|
||||
use libp2p_core::{identity, Multiaddr, PeerId, Transport};
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_relay::v2::relay::{self, Relay};
|
||||
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p_tcp as tcp;
|
||||
use std::error::Error;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
@ -54,7 +53,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
noise::NoiseAuthenticated::xx(&local_key)
|
||||
.expect("Signing libp2p-noise static DH keypair failed."),
|
||||
)
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
|
||||
let behaviour = Behaviour {
|
||||
@ -93,7 +92,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "Event", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "Event",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Behaviour {
|
||||
relay: Relay,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -23,17 +23,17 @@ use futures::future::FutureExt;
|
||||
use futures::io::{AsyncRead, AsyncWrite};
|
||||
use futures::stream::StreamExt;
|
||||
use futures::task::Spawn;
|
||||
use libp2p::core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p::core::muxing::StreamMuxerBox;
|
||||
use libp2p::core::transport::choice::OrTransport;
|
||||
use libp2p::core::transport::{Boxed, MemoryTransport, Transport};
|
||||
use libp2p::core::PublicKey;
|
||||
use libp2p::core::{identity, upgrade, PeerId};
|
||||
use libp2p::ping;
|
||||
use libp2p::plaintext::PlainText2Config;
|
||||
use libp2p::relay::v2::client;
|
||||
use libp2p::relay::v2::relay;
|
||||
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p_core::multiaddr::{Multiaddr, Protocol};
|
||||
use libp2p_core::muxing::StreamMuxerBox;
|
||||
use libp2p_core::transport::choice::OrTransport;
|
||||
use libp2p_core::transport::{Boxed, MemoryTransport, Transport};
|
||||
use libp2p_core::PublicKey;
|
||||
use libp2p_core::{identity, upgrade, PeerId};
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_plaintext::PlainText2Config;
|
||||
use libp2p_relay::v2::client;
|
||||
use libp2p_relay::v2::relay;
|
||||
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
@ -338,12 +338,16 @@ where
|
||||
transport
|
||||
.upgrade(upgrade::Version::V1)
|
||||
.authenticate(PlainText2Config { local_public_key })
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed()
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "RelayEvent", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "RelayEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Relay {
|
||||
relay: relay::Relay,
|
||||
ping: ping::Behaviour,
|
||||
@ -368,7 +372,11 @@ impl From<ping::Event> for RelayEvent {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "ClientEvent", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "ClientEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Client {
|
||||
relay: client::Client,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -30,7 +30,13 @@ void = "1"
|
||||
[dev-dependencies]
|
||||
async-trait = "0.1"
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["macros", "tokio"] }
|
||||
libp2p-mplex = { path = "../../muxers/mplex" }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-ping = { path = "../ping" }
|
||||
libp2p-identify = { path = "../identify" }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["tokio"] }
|
||||
rand = "0.8"
|
||||
tokio = { version = "1.15", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] }
|
||||
|
||||
|
@ -19,13 +19,10 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::PeerId;
|
||||
use libp2p::multiaddr::Protocol;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, SwarmEvent};
|
||||
use libp2p::Swarm;
|
||||
use libp2p::{rendezvous, tokio_development_transport, Multiaddr};
|
||||
use libp2p_core::{identity, multiaddr::Protocol, upgrade::Version, Multiaddr, PeerId, Transport};
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_rendezvous as rendezvous;
|
||||
use libp2p_swarm::{keep_alive, Swarm, SwarmEvent};
|
||||
use std::time::Duration;
|
||||
use void::Void;
|
||||
|
||||
@ -42,7 +39,11 @@ async fn main() {
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
libp2p_tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
@ -143,9 +144,12 @@ impl From<Void> for MyEvent {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(libp2p::swarm::NetworkBehaviour)]
|
||||
#[behaviour(event_process = false)]
|
||||
#[behaviour(out_event = "MyEvent")]
|
||||
#[derive(libp2p_swarm::NetworkBehaviour)]
|
||||
#[behaviour(
|
||||
out_event = "MyEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -19,13 +19,11 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::PeerId;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::Multiaddr;
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use libp2p_core::{identity, upgrade::Version, Multiaddr, PeerId, Transport};
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_rendezvous as rendezvous;
|
||||
use libp2p_swarm::AddressScore;
|
||||
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use std::time::Duration;
|
||||
|
||||
#[tokio::main]
|
||||
@ -40,7 +38,11 @@ async fn main() {
|
||||
let identity = identity::Keypair::generate_ed25519();
|
||||
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
libp2p_tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
@ -130,8 +132,11 @@ impl From<ping::Event> for MyEvent {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(event_process = false)]
|
||||
#[behaviour(out_event = "MyEvent")]
|
||||
#[behaviour(
|
||||
out_event = "MyEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
|
@ -19,13 +19,11 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::PeerId;
|
||||
use libp2p::identify;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::Multiaddr;
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use libp2p_core::{identity, upgrade::Version, Multiaddr, PeerId, Transport};
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_rendezvous as rendezvous;
|
||||
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use std::time::Duration;
|
||||
use void::Void;
|
||||
|
||||
@ -41,7 +39,11 @@ async fn main() {
|
||||
let identity = identity::Keypair::generate_ed25519();
|
||||
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
libp2p_tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
@ -144,8 +146,11 @@ impl From<Void> for MyEvent {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(event_process = false)]
|
||||
#[behaviour(out_event = "MyEvent")]
|
||||
#[behaviour(
|
||||
out_event = "MyEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct MyBehaviour {
|
||||
identify: identify::Behaviour,
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
|
@ -19,12 +19,11 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::PeerId;
|
||||
use libp2p::identify;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use libp2p_core::{identity, upgrade::Version, PeerId, Transport};
|
||||
use libp2p_identify as identify;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_rendezvous as rendezvous;
|
||||
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use void::Void;
|
||||
|
||||
/// Examples for the rendezvous protocol:
|
||||
@ -44,7 +43,11 @@ async fn main() {
|
||||
let identity = identity::Keypair::Ed25519(key.into());
|
||||
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
libp2p_tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
@ -131,8 +134,11 @@ impl From<Void> for MyEvent {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(event_process = false)]
|
||||
#[behaviour(out_event = "MyEvent")]
|
||||
#[behaviour(
|
||||
out_event = "MyEvent",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct MyBehaviour {
|
||||
identify: identify::Behaviour,
|
||||
rendezvous: rendezvous::server::Behaviour,
|
||||
|
@ -22,14 +22,14 @@ use async_trait::async_trait;
|
||||
use futures::stream::FusedStream;
|
||||
use futures::StreamExt;
|
||||
use futures::{future, Stream};
|
||||
use libp2p::core::transport::upgrade::Version;
|
||||
use libp2p::core::transport::MemoryTransport;
|
||||
use libp2p::core::upgrade::SelectUpgrade;
|
||||
use libp2p::core::{identity, Multiaddr, PeerId, Transport};
|
||||
use libp2p::mplex::MplexConfig;
|
||||
use libp2p::noise::NoiseAuthenticated;
|
||||
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::yamux::YamuxConfig;
|
||||
use libp2p_core::transport::upgrade::Version;
|
||||
use libp2p_core::transport::MemoryTransport;
|
||||
use libp2p_core::upgrade::SelectUpgrade;
|
||||
use libp2p_core::{identity, Multiaddr, PeerId, Transport};
|
||||
use libp2p_mplex::MplexConfig;
|
||||
use libp2p_noise::NoiseAuthenticated;
|
||||
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p_yamux::YamuxConfig;
|
||||
use std::fmt::Debug;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -107,15 +107,15 @@ where
|
||||
macro_rules! assert_behaviour_events {
|
||||
($swarm: ident: $pat: pat, || $body: block) => {
|
||||
match await_event_or_timeout(&mut $swarm).await {
|
||||
libp2p::swarm::SwarmEvent::Behaviour($pat) => $body,
|
||||
libp2p_swarm::SwarmEvent::Behaviour($pat) => $body,
|
||||
_ => panic!("Unexpected combination of events emitted, check logs for details"),
|
||||
}
|
||||
};
|
||||
($swarm1: ident: $pat1: pat, $swarm2: ident: $pat2: pat, || $body: block) => {
|
||||
match await_events_or_timeout(&mut $swarm1, &mut $swarm2).await {
|
||||
(
|
||||
libp2p::swarm::SwarmEvent::Behaviour($pat1),
|
||||
libp2p::swarm::SwarmEvent::Behaviour($pat2),
|
||||
libp2p_swarm::SwarmEvent::Behaviour($pat1),
|
||||
libp2p_swarm::SwarmEvent::Behaviour($pat2),
|
||||
) => $body,
|
||||
_ => panic!("Unexpected combination of events emitted, check logs for details"),
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ pub mod harness;
|
||||
use crate::harness::{await_event_or_timeout, await_events_or_timeout, new_swarm, SwarmExt};
|
||||
use futures::stream::FuturesUnordered;
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::identity;
|
||||
use libp2p::rendezvous;
|
||||
use libp2p::swarm::{DialError, Swarm, SwarmEvent};
|
||||
use libp2p_core::identity;
|
||||
use libp2p_rendezvous as rendezvous;
|
||||
use libp2p_swarm::{DialError, Swarm, SwarmEvent};
|
||||
use std::convert::TryInto;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -366,8 +366,12 @@ async fn new_impersonating_client() -> Swarm<rendezvous::client::Behaviour> {
|
||||
eve
|
||||
}
|
||||
|
||||
#[derive(libp2p::swarm::NetworkBehaviour)]
|
||||
#[behaviour(event_process = false, out_event = "CombinedEvent")]
|
||||
#[derive(libp2p_swarm::NetworkBehaviour)]
|
||||
#[behaviour(
|
||||
event_process = false,
|
||||
out_event = "CombinedEvent",
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct CombinedBehaviour {
|
||||
client: rendezvous::client::Behaviour,
|
||||
server: rendezvous::server::Behaviour,
|
||||
|
@ -25,7 +25,9 @@ unsigned-varint = { version = "0.7", features = ["std", "futures"] }
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.2"
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-noise = { path = "../../transports/noise" }
|
||||
libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
rand = "0.8"
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
|
@ -22,18 +22,17 @@
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::{channel::mpsc, prelude::*, AsyncWriteExt};
|
||||
use libp2p::core::{
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
muxing::StreamMuxerBox,
|
||||
transport,
|
||||
upgrade::{self, read_length_prefixed, write_length_prefixed},
|
||||
Multiaddr, PeerId,
|
||||
Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use libp2p::noise::NoiseAuthenticated;
|
||||
use libp2p::request_response::*;
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::tcp;
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_noise::NoiseAuthenticated;
|
||||
use libp2p_request_response::*;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_tcp as tcp;
|
||||
use rand::{self, Rng};
|
||||
use std::{io, iter};
|
||||
|
||||
@ -302,7 +301,7 @@ fn mk_transport() -> (PeerId, transport::Boxed<(PeerId, StreamMuxerBox)>) {
|
||||
tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
|
||||
.upgrade(upgrade::Version::V1)
|
||||
.authenticate(NoiseAuthenticated::xx(&id_keys).unwrap())
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed(),
|
||||
)
|
||||
}
|
||||
|
@ -174,8 +174,7 @@
|
||||
//! [`Transport`] to the [`NetworkBehaviour`].
|
||||
//!
|
||||
//! ```rust
|
||||
//! use libp2p::{identity, PeerId, ping};
|
||||
//! use libp2p::swarm::Swarm;
|
||||
//! use libp2p::{identity, PeerId, ping, Swarm};
|
||||
//! use std::error::Error;
|
||||
//!
|
||||
//! #[async_std::main]
|
||||
@ -226,8 +225,7 @@
|
||||
//! remote peer.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use libp2p::{identity, Multiaddr, PeerId, ping};
|
||||
//! use libp2p::swarm::{Swarm, dial_opts::DialOpts};
|
||||
//! use libp2p::{identity, Multiaddr, PeerId, ping, Swarm, swarm::dial_opts::DialOpts};
|
||||
//! use std::error::Error;
|
||||
//!
|
||||
//! #[async_std::main]
|
||||
@ -271,8 +269,7 @@
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use futures::prelude::*;
|
||||
//! use libp2p::swarm::{Swarm, SwarmEvent, dial_opts::DialOpts};
|
||||
//! use libp2p::{identity, Multiaddr, PeerId, ping};
|
||||
//! use libp2p::{identity, Multiaddr, PeerId, ping, swarm::{Swarm, SwarmEvent, dial_opts::DialOpts}};
|
||||
//! use std::error::Error;
|
||||
//!
|
||||
//! #[async_std::main]
|
||||
|
@ -17,7 +17,7 @@ flate2 = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.2"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-tcp = { path = "../tcp", features = ["async-io"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
rand = "0.8"
|
||||
|
||||
|
@ -19,9 +19,9 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::{future, prelude::*};
|
||||
use libp2p::core::{transport::Transport, upgrade};
|
||||
use libp2p::deflate::DeflateConfig;
|
||||
use libp2p::tcp;
|
||||
use libp2p_core::{transport::Transport, upgrade};
|
||||
use libp2p_deflate::DeflateConfig;
|
||||
use libp2p_tcp as tcp;
|
||||
use quickcheck::{QuickCheck, TestResult};
|
||||
use rand::RngCore;
|
||||
|
||||
|
@ -573,7 +573,7 @@ fn invalid_data(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> io::E
|
||||
io::Error::new(io::ErrorKind::InvalidData, e)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(all(test, any(feature = "tokio", feature = "async-std")))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::future::BoxFuture;
|
||||
|
@ -31,11 +31,11 @@ snow = { version = "0.9.0", features = ["default-resolver"], default-features =
|
||||
|
||||
[dev-dependencies]
|
||||
async-io = "1.2.0"
|
||||
ed25519-compact = "1.0.11"
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
libp2p-tcp = { path = "../tcp", features = ["async-io"] }
|
||||
libsodium-sys-stable = { version = "1.19.22", features = ["fetch-latest"] }
|
||||
ed25519-compact = "2.0.2"
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.11"
|
||||
|
@ -40,8 +40,8 @@
|
||||
//!
|
||||
//! ```
|
||||
//! use libp2p_core::{identity, Transport, upgrade};
|
||||
//! use libp2p::tcp::TcpTransport;
|
||||
//! use libp2p::noise::{Keypair, X25519Spec, NoiseAuthenticated};
|
||||
//! use libp2p_tcp::TcpTransport;
|
||||
//! use libp2p_noise::{Keypair, X25519Spec, NoiseAuthenticated};
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let id_keys = identity::Keypair::generate_ed25519();
|
||||
|
@ -23,14 +23,14 @@ use futures::{
|
||||
future::{self, Either},
|
||||
prelude::*,
|
||||
};
|
||||
use libp2p::core::identity;
|
||||
use libp2p::core::transport::{self, Transport};
|
||||
use libp2p::core::upgrade::{self, apply_inbound, apply_outbound, Negotiated};
|
||||
use libp2p::noise::{
|
||||
Keypair, NoiseAuthenticated, NoiseConfig, NoiseOutput, RemoteIdentity, X25519Spec, X25519,
|
||||
use libp2p_core::transport::Transport;
|
||||
use libp2p_core::upgrade::{apply_inbound, apply_outbound, Negotiated};
|
||||
use libp2p_core::{identity, transport, upgrade};
|
||||
use libp2p_noise::{
|
||||
Keypair, NoiseAuthenticated, NoiseConfig, NoiseError, NoiseOutput, RemoteIdentity, X25519Spec,
|
||||
X25519,
|
||||
};
|
||||
use libp2p::tcp;
|
||||
use libp2p_noise::NoiseError;
|
||||
use libp2p_tcp as tcp;
|
||||
use log::info;
|
||||
use quickcheck::*;
|
||||
use std::{convert::TryInto, io, net::TcpStream};
|
||||
|
@ -38,8 +38,10 @@ rustc-args = ["--cfg", "docsrs"]
|
||||
[dev-dependencies]
|
||||
async-std = { version = "1.12.0", features = ["attributes"] }
|
||||
env_logger = "0.10.0"
|
||||
libp2p = { path = "../..", features = ["tcp", "yamux", "noise", "async-std"] }
|
||||
libp2p-muxer-test-harness = { path = "../../muxers/test-harness" }
|
||||
libp2p-noise = { path = "../noise" }
|
||||
libp2p-tcp = { path = "../tcp", features = ["async-io"] }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
quickcheck = "1"
|
||||
tokio = { version = "1.21.1", features = ["macros", "rt-multi-thread", "time"] }
|
||||
|
||||
|
@ -4,14 +4,14 @@ use futures::channel::{mpsc, oneshot};
|
||||
use futures::future::{poll_fn, Either};
|
||||
use futures::stream::StreamExt;
|
||||
use futures::{future, AsyncReadExt, AsyncWriteExt, FutureExt, SinkExt};
|
||||
use libp2p::core::multiaddr::Protocol;
|
||||
use libp2p::core::Transport;
|
||||
use libp2p::{noise, tcp, yamux, Multiaddr};
|
||||
use libp2p_core::either::EitherOutput;
|
||||
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt, SubstreamBox};
|
||||
use libp2p_core::transport::{Boxed, OrTransport, TransportEvent};
|
||||
use libp2p_core::{upgrade, PeerId};
|
||||
use libp2p_core::{multiaddr::Protocol, upgrade, Multiaddr, PeerId, Transport};
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_quic as quic;
|
||||
use libp2p_tcp as tcp;
|
||||
use libp2p_yamux as yamux;
|
||||
use quic::Provider;
|
||||
use rand::RngCore;
|
||||
use std::future::Future;
|
||||
@ -94,7 +94,7 @@ async fn ipv4_dial_ipv6() {
|
||||
#[async_std::test]
|
||||
#[ignore] // Transport currently does not validate PeerId. Enable once we make use of PeerId validation in rustls.
|
||||
async fn wrong_peerid() {
|
||||
use libp2p::PeerId;
|
||||
use libp2p_core::PeerId;
|
||||
|
||||
let (a_peer_id, mut a_transport) = create_default_transport::<quic::async_std::Provider>();
|
||||
let (b_peer_id, mut b_transport) = create_default_transport::<quic::async_std::Provider>();
|
||||
@ -189,7 +189,7 @@ async fn draft_29_support() {
|
||||
use std::task::Poll;
|
||||
|
||||
use futures::{future::poll_fn, select};
|
||||
use libp2p::TransportError;
|
||||
use libp2p_core::transport::TransportError;
|
||||
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
@ -379,8 +379,8 @@ macro_rules! swap_protocol {
|
||||
};
|
||||
}
|
||||
|
||||
fn generate_tls_keypair() -> libp2p::identity::Keypair {
|
||||
libp2p::identity::Keypair::generate_ed25519()
|
||||
fn generate_tls_keypair() -> libp2p_core::identity::Keypair {
|
||||
libp2p_core::identity::Keypair::generate_ed25519()
|
||||
}
|
||||
|
||||
fn create_default_transport<P: Provider>() -> (PeerId, Boxed<(PeerId, StreamMuxerBox)>) {
|
||||
|
@ -28,7 +28,9 @@ features = ["dangerous_configuration"] # Must enable this to allow for custom ve
|
||||
[dev-dependencies]
|
||||
hex = "0.4.3"
|
||||
hex-literal = "0.3.4"
|
||||
libp2p = { path = "../..", features = ["yamux", "rsa", "ecdsa", "secp256k1"], default-features = false }
|
||||
libp2p-core = { path = "../../core", features = ["rsa", "ecdsa", "secp256k1"] }
|
||||
libp2p-swarm = { path = "../../swarm" }
|
||||
libp2p-yamux = { path = "../../muxers/yamux" }
|
||||
tokio = { version = "1.21.1", features = ["full"] }
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
|
@ -1,10 +1,9 @@
|
||||
use futures::{future, StreamExt};
|
||||
use libp2p::multiaddr::Protocol;
|
||||
use libp2p::swarm::{keep_alive, SwarmEvent};
|
||||
use libp2p::Swarm;
|
||||
use libp2p_core::multiaddr::Protocol;
|
||||
use libp2p_core::transport::MemoryTransport;
|
||||
use libp2p_core::upgrade::Version;
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_swarm::{keep_alive, Swarm, SwarmEvent};
|
||||
|
||||
#[tokio::test]
|
||||
async fn can_establish_connection() {
|
||||
@ -57,12 +56,12 @@ async fn can_establish_connection() {
|
||||
}
|
||||
|
||||
fn make_swarm() -> Swarm<keep_alive::Behaviour> {
|
||||
let identity = libp2p::identity::Keypair::generate_ed25519();
|
||||
let identity = libp2p_core::identity::Keypair::generate_ed25519();
|
||||
|
||||
let transport = MemoryTransport::default()
|
||||
.upgrade(Version::V1)
|
||||
.authenticate(libp2p_tls::Config::new(&identity).unwrap())
|
||||
.multiplex(libp2p::yamux::YamuxConfig::default())
|
||||
.multiplex(libp2p_yamux::YamuxConfig::default())
|
||||
.boxed();
|
||||
|
||||
Swarm::without_executor(
|
||||
|
@ -45,10 +45,12 @@ prost-build = "0.11"
|
||||
anyhow = "1.0"
|
||||
env_logger = "0.10"
|
||||
hex-literal = "0.3"
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-swarm = { path = "../../swarm", features = ["macros", "tokio"] }
|
||||
libp2p-ping = { path = "../../protocols/ping" }
|
||||
tokio = { version = "1.19", features = ["full"] }
|
||||
unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }
|
||||
void = "1"
|
||||
quickcheck = "1.0.3"
|
||||
|
||||
[[test]]
|
||||
name = "smoke"
|
||||
|
@ -1,10 +1,10 @@
|
||||
use anyhow::Result;
|
||||
use futures::StreamExt;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour};
|
||||
use libp2p::Transport;
|
||||
use libp2p::{ping, Swarm};
|
||||
use libp2p_core::identity;
|
||||
use libp2p_core::muxing::StreamMuxerBox;
|
||||
use libp2p_core::Transport;
|
||||
use libp2p_ping as ping;
|
||||
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm};
|
||||
use rand::thread_rng;
|
||||
use void::Void;
|
||||
|
||||
@ -41,7 +41,11 @@ fn create_swarm() -> Result<Swarm<Behaviour>> {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour, Default)]
|
||||
#[behaviour(out_event = "Event", event_process = false)]
|
||||
#[behaviour(
|
||||
out_event = "Event",
|
||||
event_process = false,
|
||||
prelude = "libp2p_swarm::derive_prelude"
|
||||
)]
|
||||
struct Behaviour {
|
||||
ping: ping::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
|
@ -18,469 +18,355 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use futures::{
|
||||
future::{select, Either, FutureExt},
|
||||
io::{AsyncRead, AsyncWrite, AsyncWriteExt},
|
||||
stream::StreamExt,
|
||||
};
|
||||
use libp2p::core::{identity, muxing::StreamMuxerBox, upgrade, Transport as _};
|
||||
use libp2p::request_response::{
|
||||
ProtocolName, ProtocolSupport, RequestResponse, RequestResponseCodec, RequestResponseConfig,
|
||||
RequestResponseEvent, RequestResponseMessage,
|
||||
};
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::webrtc::tokio as webrtc;
|
||||
use futures::channel::mpsc;
|
||||
use futures::future::{BoxFuture, Either};
|
||||
use futures::stream::StreamExt;
|
||||
use futures::{future, ready, AsyncReadExt, AsyncWriteExt, FutureExt, SinkExt};
|
||||
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt};
|
||||
use libp2p_core::transport::{Boxed, TransportEvent};
|
||||
use libp2p_core::{Multiaddr, PeerId, Transport};
|
||||
use libp2p_webrtc as webrtc;
|
||||
use rand::{thread_rng, RngCore};
|
||||
|
||||
use std::{io, iter};
|
||||
use std::future::Future;
|
||||
use std::num::NonZeroU8;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::time::Duration;
|
||||
|
||||
#[tokio::test]
|
||||
async fn smoke() -> Result<()> {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
async fn smoke() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let (a_peer_id, mut a_transport) = create_transport();
|
||||
let (b_peer_id, mut b_transport) = create_transport();
|
||||
|
||||
let mut a = create_swarm()?;
|
||||
let mut b = create_swarm()?;
|
||||
let addr = start_listening(&mut a_transport, "/ip4/127.0.0.1/udp/0/webrtc").await;
|
||||
start_listening(&mut b_transport, "/ip4/127.0.0.1/udp/0/webrtc").await;
|
||||
let ((a_connected, _, _), (b_connected, _)) =
|
||||
connect(&mut a_transport, &mut b_transport, addr).await;
|
||||
|
||||
Swarm::listen_on(&mut a, "/ip4/127.0.0.1/udp/0/webrtc".parse()?)?;
|
||||
Swarm::listen_on(&mut b, "/ip4/127.0.0.1/udp/0/webrtc".parse()?)?;
|
||||
|
||||
let addr = match a.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
// skip other interface addresses
|
||||
while a.next().now_or_never().is_some() {}
|
||||
|
||||
let _ = match b.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
// skip other interface addresses
|
||||
while b.next().now_or_never().is_some() {}
|
||||
|
||||
let mut data = vec![0; 4096];
|
||||
rng.fill_bytes(&mut data);
|
||||
|
||||
b.behaviour_mut()
|
||||
.add_address(Swarm::local_peer_id(&a), addr);
|
||||
b.behaviour_mut()
|
||||
.send_request(Swarm::local_peer_id(&a), Ping(data.clone()));
|
||||
|
||||
match b.next().await {
|
||||
Some(SwarmEvent::Dialing(_)) => {}
|
||||
e => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Left((Some(SwarmEvent::IncomingConnection { .. }), _)) => {}
|
||||
Either::Left((e, _)) => panic!("{:?}", e),
|
||||
Either::Right(_) => panic!("b completed first"),
|
||||
}
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Left((Some(SwarmEvent::ConnectionEstablished { .. }), _)) => {}
|
||||
Either::Left((e, _)) => panic!("{:?}", e),
|
||||
Either::Right((Some(SwarmEvent::ConnectionEstablished { .. }), _)) => {}
|
||||
Either::Right((e, _)) => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Left((Some(SwarmEvent::ConnectionEstablished { .. }), _)) => {}
|
||||
Either::Left((e, _)) => panic!("{:?}", e),
|
||||
Either::Right((Some(SwarmEvent::ConnectionEstablished { .. }), _)) => {}
|
||||
Either::Right((e, _)) => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
assert!(b.next().now_or_never().is_none());
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Left((
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Request {
|
||||
request: Ping(ping),
|
||||
channel,
|
||||
..
|
||||
},
|
||||
..
|
||||
})),
|
||||
_,
|
||||
)) => {
|
||||
a.behaviour_mut()
|
||||
.send_response(channel, Pong(ping))
|
||||
.unwrap();
|
||||
}
|
||||
Either::Left((e, _)) => panic!("{:?}", e),
|
||||
Either::Right(_) => panic!("b completed first"),
|
||||
}
|
||||
|
||||
match a.next().await {
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::ResponseSent { .. })) => {}
|
||||
e => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Right((
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Response {
|
||||
response: Pong(pong),
|
||||
..
|
||||
},
|
||||
..
|
||||
})),
|
||||
_,
|
||||
)) => assert_eq!(data, pong),
|
||||
Either::Right((e, _)) => panic!("{:?}", e),
|
||||
Either::Left(_) => panic!("a completed first"),
|
||||
}
|
||||
|
||||
a.behaviour_mut().send_request(
|
||||
Swarm::local_peer_id(&b),
|
||||
Ping(b"another substream".to_vec()),
|
||||
);
|
||||
|
||||
assert!(a.next().now_or_never().is_none());
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Right((
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Request {
|
||||
request: Ping(data),
|
||||
channel,
|
||||
..
|
||||
},
|
||||
..
|
||||
})),
|
||||
_,
|
||||
)) => {
|
||||
b.behaviour_mut()
|
||||
.send_response(channel, Pong(data))
|
||||
.unwrap();
|
||||
}
|
||||
Either::Right((e, _)) => panic!("{:?}", e),
|
||||
Either::Left(_) => panic!("a completed first"),
|
||||
}
|
||||
|
||||
match b.next().await {
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::ResponseSent { .. })) => {}
|
||||
e => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
let pair = select(a.next(), b.next());
|
||||
match pair.await {
|
||||
Either::Left((
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Response {
|
||||
response: Pong(data),
|
||||
..
|
||||
},
|
||||
..
|
||||
})),
|
||||
_,
|
||||
)) => assert_eq!(data, b"another substream".to_vec()),
|
||||
Either::Left((e, _)) => panic!("{:?}", e),
|
||||
Either::Right(_) => panic!("b completed first"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
assert_eq!(a_connected, b_peer_id);
|
||||
assert_eq!(b_connected, a_peer_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dial_failure() -> Result<()> {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
// Note: This test should likely be ported to the muxer compliance test suite.
|
||||
#[test]
|
||||
fn concurrent_connections_and_streams_tokio() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut a = create_swarm()?;
|
||||
let mut b = create_swarm()?;
|
||||
|
||||
Swarm::listen_on(&mut a, "/ip4/127.0.0.1/udp/0/webrtc".parse()?)?;
|
||||
Swarm::listen_on(&mut b, "/ip4/127.0.0.1/udp/0/webrtc".parse()?)?;
|
||||
|
||||
let addr = match a.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
// skip other interface addresses
|
||||
while a.next().now_or_never().is_some() {}
|
||||
|
||||
let _ = match b.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
// skip other interface addresses
|
||||
while b.next().now_or_never().is_some() {}
|
||||
|
||||
let a_peer_id = &Swarm::local_peer_id(&a).clone();
|
||||
drop(a); // stop a swarm so b can never reach it
|
||||
|
||||
b.behaviour_mut().add_address(a_peer_id, addr);
|
||||
b.behaviour_mut()
|
||||
.send_request(a_peer_id, Ping(b"hello world".to_vec()));
|
||||
|
||||
match b.next().await {
|
||||
Some(SwarmEvent::Dialing(_)) => {}
|
||||
e => panic!("{:?}", e),
|
||||
}
|
||||
|
||||
match b.next().await {
|
||||
Some(SwarmEvent::OutgoingConnectionError { .. }) => {}
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
match b.next().await {
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::OutboundFailure { .. })) => {}
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let _guard = rt.enter();
|
||||
quickcheck::QuickCheck::new()
|
||||
.min_tests_passed(1)
|
||||
.quickcheck(prop as fn(_, _) -> _);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_connections_and_streams() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
fn generate_tls_keypair() -> libp2p_core::identity::Keypair {
|
||||
libp2p_core::identity::Keypair::generate_ed25519()
|
||||
}
|
||||
|
||||
let num_listeners = 3usize;
|
||||
let num_streams = 8usize;
|
||||
fn create_transport() -> (PeerId, Boxed<(PeerId, StreamMuxerBox)>) {
|
||||
let keypair = generate_tls_keypair();
|
||||
let peer_id = keypair.public().to_peer_id();
|
||||
|
||||
let mut data = vec![0; 4096];
|
||||
rand::thread_rng().fill_bytes(&mut data);
|
||||
let mut listeners = vec![];
|
||||
let transport = webrtc::tokio::Transport::new(
|
||||
keypair,
|
||||
webrtc::tokio::Certificate::generate(&mut thread_rng()).unwrap(),
|
||||
)
|
||||
.map(|(p, c), _| (p, StreamMuxerBox::new(c)))
|
||||
.boxed();
|
||||
|
||||
(peer_id, transport)
|
||||
}
|
||||
|
||||
async fn start_listening(transport: &mut Boxed<(PeerId, StreamMuxerBox)>, addr: &str) -> Multiaddr {
|
||||
transport.listen_on(addr.parse().unwrap()).unwrap();
|
||||
match transport.next().await {
|
||||
Some(TransportEvent::NewAddress { listen_addr, .. }) => listen_addr,
|
||||
e => panic!("{:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
fn prop(number_listeners: NonZeroU8, number_streams: NonZeroU8) -> quickcheck::TestResult {
|
||||
const BUFFER_SIZE: usize = 4096 * 10;
|
||||
|
||||
let number_listeners = u8::from(number_listeners) as usize;
|
||||
let number_streams = u8::from(number_streams) as usize;
|
||||
|
||||
if number_listeners > 10 || number_streams > 10 {
|
||||
return quickcheck::TestResult::discard();
|
||||
}
|
||||
|
||||
let (listeners_tx, mut listeners_rx) = mpsc::channel(number_listeners);
|
||||
|
||||
log::info!("Creating {number_streams} streams on {number_listeners} connections");
|
||||
|
||||
// Spawn the listener nodes.
|
||||
for _ in 0..num_listeners {
|
||||
let mut listener = create_swarm().unwrap();
|
||||
Swarm::listen_on(
|
||||
&mut listener,
|
||||
"/ip4/127.0.0.1/udp/0/webrtc".parse().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
for _ in 0..number_listeners {
|
||||
tokio::spawn({
|
||||
let mut listeners_tx = listeners_tx.clone();
|
||||
|
||||
// Wait to listen on address.
|
||||
let addr = match listener.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
async move {
|
||||
let (peer_id, mut listener) = create_transport();
|
||||
let addr = start_listening(&mut listener, "/ip4/127.0.0.1/udp/0/webrtc").await;
|
||||
|
||||
listeners.push((*listener.local_peer_id(), addr));
|
||||
listeners_tx.send((peer_id, addr)).await.unwrap();
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match listener.next().await {
|
||||
Some(SwarmEvent::IncomingConnection { .. }) => {
|
||||
log::debug!("listener IncomingConnection");
|
||||
}
|
||||
Some(SwarmEvent::ConnectionEstablished { .. }) => {
|
||||
log::debug!("listener ConnectionEstablished");
|
||||
}
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Request {
|
||||
request: Ping(ping),
|
||||
channel,
|
||||
..
|
||||
},
|
||||
..
|
||||
})) => {
|
||||
log::debug!("listener got Message");
|
||||
listener
|
||||
.behaviour_mut()
|
||||
.send_response(channel, Pong(ping))
|
||||
.unwrap();
|
||||
}
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::ResponseSent { .. })) => {
|
||||
log::debug!("listener ResponseSent");
|
||||
}
|
||||
Some(SwarmEvent::ConnectionClosed { .. }) => {}
|
||||
Some(SwarmEvent::NewListenAddr { .. }) => {
|
||||
log::debug!("listener NewListenAddr");
|
||||
}
|
||||
Some(e) => {
|
||||
panic!("unexpected event {:?}", e);
|
||||
}
|
||||
None => {
|
||||
panic!("listener stopped");
|
||||
loop {
|
||||
if let TransportEvent::Incoming { upgrade, .. } =
|
||||
listener.select_next_some().await
|
||||
{
|
||||
let (_, connection) = upgrade.await.unwrap();
|
||||
|
||||
tokio::spawn(answer_inbound_streams::<BUFFER_SIZE>(connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let mut dialer = create_swarm().unwrap();
|
||||
Swarm::listen_on(&mut dialer, "/ip4/127.0.0.1/udp/0/webrtc".parse().unwrap()).unwrap();
|
||||
|
||||
// Wait to listen on address.
|
||||
match dialer.next().await {
|
||||
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
|
||||
e => panic!("{:?}", e),
|
||||
};
|
||||
let (completed_streams_tx, completed_streams_rx) =
|
||||
mpsc::channel(number_streams * number_listeners);
|
||||
|
||||
// For each listener node start `number_streams` requests.
|
||||
for (listener_peer_id, listener_addr) in &listeners {
|
||||
dialer
|
||||
.behaviour_mut()
|
||||
.add_address(listener_peer_id, listener_addr.clone());
|
||||
tokio::spawn(async move {
|
||||
let (_, mut dialer) = create_transport();
|
||||
|
||||
dialer.dial(*listener_peer_id).unwrap();
|
||||
while let Some((_, listener_addr)) = listeners_rx.next().await {
|
||||
let (_, connection) = Dial::new(&mut dialer, listener_addr.clone()).await;
|
||||
|
||||
tokio::spawn(open_outbound_streams::<BUFFER_SIZE>(
|
||||
connection,
|
||||
number_streams,
|
||||
completed_streams_tx.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
// Drive the dialer.
|
||||
loop {
|
||||
dialer.next().await;
|
||||
}
|
||||
});
|
||||
|
||||
let completed_streams = number_streams * number_listeners;
|
||||
|
||||
// Wait for all streams to complete.
|
||||
tokio::runtime::Handle::current()
|
||||
.block_on(tokio::time::timeout(
|
||||
Duration::from_secs(30),
|
||||
completed_streams_rx
|
||||
.take(completed_streams)
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
quickcheck::TestResult::passed()
|
||||
}
|
||||
|
||||
async fn answer_inbound_streams<const BUFFER_SIZE: usize>(mut connection: StreamMuxerBox) {
|
||||
loop {
|
||||
let mut inbound_stream = match future::poll_fn(|cx| {
|
||||
let _ = connection.poll_unpin(cx)?;
|
||||
|
||||
connection.poll_inbound_unpin(cx)
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(s) => s,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
// FIXME: Need to write _some_ data before we can read on both sides.
|
||||
// Do a ping-pong exchange.
|
||||
{
|
||||
let mut pong = [0u8; 4];
|
||||
inbound_stream.write_all(b"PING").await.unwrap();
|
||||
inbound_stream.flush().await.unwrap();
|
||||
inbound_stream.read_exact(&mut pong).await.unwrap();
|
||||
assert_eq!(&pong, b"PONG");
|
||||
}
|
||||
|
||||
let mut data = vec![0; BUFFER_SIZE];
|
||||
|
||||
inbound_stream.read_exact(&mut data).await.unwrap();
|
||||
inbound_stream.write_all(&data).await.unwrap();
|
||||
inbound_stream.close().await.unwrap();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn open_outbound_streams<const BUFFER_SIZE: usize>(
|
||||
mut connection: StreamMuxerBox,
|
||||
number_streams: usize,
|
||||
completed_streams_tx: mpsc::Sender<()>,
|
||||
) {
|
||||
for _ in 0..number_streams {
|
||||
let mut outbound_stream = future::poll_fn(|cx| {
|
||||
let _ = connection.poll_unpin(cx)?;
|
||||
|
||||
connection.poll_outbound_unpin(cx)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::spawn({
|
||||
let mut completed_streams_tx = completed_streams_tx.clone();
|
||||
|
||||
async move {
|
||||
// FIXME: Need to write _some_ data before we can read on both sides.
|
||||
// Do a ping-pong exchange.
|
||||
{
|
||||
let mut ping = [0u8; 4];
|
||||
outbound_stream.write_all(b"PONG").await.unwrap();
|
||||
outbound_stream.flush().await.unwrap();
|
||||
outbound_stream.read_exact(&mut ping).await.unwrap();
|
||||
assert_eq!(&ping, b"PING");
|
||||
}
|
||||
|
||||
let mut data = vec![0; BUFFER_SIZE];
|
||||
rand::thread_rng().fill_bytes(&mut data);
|
||||
|
||||
let mut received = Vec::new();
|
||||
|
||||
outbound_stream.write_all(&data).await.unwrap();
|
||||
outbound_stream.flush().await.unwrap();
|
||||
outbound_stream.read_to_end(&mut received).await.unwrap();
|
||||
|
||||
assert_eq!(received, data);
|
||||
|
||||
completed_streams_tx.send(()).await.unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for responses to each request.
|
||||
let mut num_responses = 0;
|
||||
loop {
|
||||
match dialer.next().await {
|
||||
Some(SwarmEvent::Dialing(_)) => {
|
||||
log::debug!("dialer Dialing");
|
||||
}
|
||||
Some(SwarmEvent::ConnectionEstablished { peer_id, .. }) => {
|
||||
log::debug!("dialer Connection established");
|
||||
for _ in 0..num_streams {
|
||||
dialer
|
||||
.behaviour_mut()
|
||||
.send_request(&peer_id, Ping(data.clone()));
|
||||
log::info!("Created {number_streams} streams");
|
||||
|
||||
while future::poll_fn(|cx| connection.poll_unpin(cx))
|
||||
.await
|
||||
.is_ok()
|
||||
{}
|
||||
}
|
||||
|
||||
async fn connect(
|
||||
a_transport: &mut Boxed<(PeerId, StreamMuxerBox)>,
|
||||
b_transport: &mut Boxed<(PeerId, StreamMuxerBox)>,
|
||||
addr: Multiaddr,
|
||||
) -> (
|
||||
(PeerId, Multiaddr, StreamMuxerBox),
|
||||
(PeerId, StreamMuxerBox),
|
||||
) {
|
||||
match futures::future::select(
|
||||
ListenUpgrade::new(a_transport),
|
||||
Dial::new(b_transport, addr),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::Left((listen_done, dial)) => {
|
||||
let mut pending_dial = dial;
|
||||
|
||||
loop {
|
||||
match future::select(pending_dial, a_transport.next()).await {
|
||||
Either::Left((dial_done, _)) => return (listen_done, dial_done),
|
||||
Either::Right((_, dial)) => {
|
||||
pending_dial = dial;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(SwarmEvent::Behaviour(RequestResponseEvent::Message {
|
||||
message:
|
||||
RequestResponseMessage::Response {
|
||||
response: Pong(pong),
|
||||
..
|
||||
},
|
||||
..
|
||||
})) => {
|
||||
log::debug!("dialer got Message");
|
||||
num_responses += 1;
|
||||
assert_eq!(data, pong);
|
||||
let should_be = num_listeners * num_streams;
|
||||
log::debug!(
|
||||
"num of responses: {}, num of listeners * num of streams: {}",
|
||||
num_responses,
|
||||
should_be
|
||||
);
|
||||
if num_responses == should_be {
|
||||
break;
|
||||
}
|
||||
Either::Right((dial_done, listen)) => {
|
||||
let mut pending_listen = listen;
|
||||
|
||||
loop {
|
||||
match future::select(pending_listen, b_transport.next()).await {
|
||||
Either::Left((listen_done, _)) => return (listen_done, dial_done),
|
||||
Either::Right((_, listen)) => {
|
||||
pending_listen = listen;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(SwarmEvent::ConnectionClosed { .. }) => {
|
||||
log::debug!("dialer ConnectionClosed");
|
||||
}
|
||||
Some(SwarmEvent::NewListenAddr { .. }) => {
|
||||
log::debug!("dialer NewListenAddr");
|
||||
}
|
||||
e => {
|
||||
panic!("unexpected event {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct PingProtocol();
|
||||
struct ListenUpgrade<'a> {
|
||||
listener: &'a mut Boxed<(PeerId, StreamMuxerBox)>,
|
||||
listener_upgrade_task: Option<BoxFuture<'static, (PeerId, Multiaddr, StreamMuxerBox)>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PingCodec();
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct Ping(Vec<u8>);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct Pong(Vec<u8>);
|
||||
|
||||
impl ProtocolName for PingProtocol {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
"/ping/1".as_bytes()
|
||||
impl<'a> ListenUpgrade<'a> {
|
||||
pub fn new(listener: &'a mut Boxed<(PeerId, StreamMuxerBox)>) -> Self {
|
||||
Self {
|
||||
listener,
|
||||
listener_upgrade_task: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl RequestResponseCodec for PingCodec {
|
||||
type Protocol = PingProtocol;
|
||||
type Request = Ping;
|
||||
type Response = Pong;
|
||||
struct Dial<'a> {
|
||||
dialer: &'a mut Boxed<(PeerId, StreamMuxerBox)>,
|
||||
dial_task: BoxFuture<'static, (PeerId, StreamMuxerBox)>,
|
||||
}
|
||||
|
||||
async fn read_request<T>(&mut self, _: &PingProtocol, io: &mut T) -> io::Result<Self::Request>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
upgrade::read_length_prefixed(io, 4096)
|
||||
.map(|res| match res {
|
||||
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)),
|
||||
Ok(vec) if vec.is_empty() => Err(io::ErrorKind::UnexpectedEof.into()),
|
||||
Ok(vec) => Ok(Ping(vec)),
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn read_response<T>(&mut self, _: &PingProtocol, io: &mut T) -> io::Result<Self::Response>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
upgrade::read_length_prefixed(io, 4096)
|
||||
.map(|res| match res {
|
||||
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)),
|
||||
Ok(vec) if vec.is_empty() => Err(io::ErrorKind::UnexpectedEof.into()),
|
||||
Ok(vec) => Ok(Pong(vec)),
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn write_request<T>(
|
||||
&mut self,
|
||||
_: &PingProtocol,
|
||||
io: &mut T,
|
||||
Ping(data): Ping,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
upgrade::write_length_prefixed(io, data).await?;
|
||||
io.close().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write_response<T>(
|
||||
&mut self,
|
||||
_: &PingProtocol,
|
||||
io: &mut T,
|
||||
Pong(data): Pong,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
upgrade::write_length_prefixed(io, data).await?;
|
||||
io.close().await?;
|
||||
Ok(())
|
||||
impl<'a> Dial<'a> {
|
||||
fn new(dialer: &'a mut Boxed<(PeerId, StreamMuxerBox)>, addr: Multiaddr) -> Self {
|
||||
Self {
|
||||
dial_task: dialer.dial(addr).unwrap().map(|r| r.unwrap()).boxed(),
|
||||
dialer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_swarm() -> Result<Swarm<RequestResponse<PingCodec>>> {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let peer_id = id_keys.public().to_peer_id();
|
||||
let transport = webrtc::Transport::new(
|
||||
id_keys,
|
||||
webrtc::Certificate::generate(&mut thread_rng()).unwrap(),
|
||||
);
|
||||
impl Future for Dial<'_> {
|
||||
type Output = (PeerId, StreamMuxerBox);
|
||||
|
||||
let protocols = iter::once((PingProtocol(), ProtocolSupport::Full));
|
||||
let cfg = RequestResponseConfig::default();
|
||||
let behaviour = RequestResponse::new(PingCodec(), protocols, cfg);
|
||||
let transport = transport
|
||||
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
|
||||
.boxed();
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
loop {
|
||||
match self.dialer.poll_next_unpin(cx) {
|
||||
Poll::Ready(_) => {
|
||||
continue;
|
||||
}
|
||||
Poll::Pending => {}
|
||||
}
|
||||
|
||||
Ok(Swarm::with_tokio_executor(transport, behaviour, peer_id))
|
||||
let conn = ready!(self.dial_task.poll_unpin(cx));
|
||||
return Poll::Ready(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Future for ListenUpgrade<'_> {
|
||||
type Output = (PeerId, Multiaddr, StreamMuxerBox);
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
loop {
|
||||
match dbg!(self.listener.poll_next_unpin(cx)) {
|
||||
Poll::Ready(Some(TransportEvent::Incoming {
|
||||
upgrade,
|
||||
send_back_addr,
|
||||
..
|
||||
})) => {
|
||||
self.listener_upgrade_task = Some(
|
||||
async move {
|
||||
let (peer, conn) = upgrade.await.unwrap();
|
||||
|
||||
(peer, send_back_addr, conn)
|
||||
}
|
||||
.boxed(),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
Poll::Ready(None) => unreachable!("stream never ends"),
|
||||
Poll::Ready(Some(_)) => continue,
|
||||
Poll::Pending => {}
|
||||
}
|
||||
|
||||
let conn = match self.listener_upgrade_task.as_mut() {
|
||||
None => return Poll::Pending,
|
||||
Some(inner) => ready!(inner.poll_unpin(cx)),
|
||||
};
|
||||
|
||||
return Poll::Ready(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ url = "2.1"
|
||||
webpki-roots = "0.22"
|
||||
|
||||
[dev-dependencies]
|
||||
libp2p = { path = "../..", features = ["full"] }
|
||||
libp2p-tcp = { path = "../tcp", features = ["async-io"] }
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
# More information: https://docs.rs/about/builds#cross-compiling
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
@ -221,8 +221,8 @@ where
|
||||
mod tests {
|
||||
use super::WsConfig;
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::{multiaddr::Protocol, Multiaddr, PeerId, Transport};
|
||||
use libp2p::tcp;
|
||||
use libp2p_core::{multiaddr::Protocol, Multiaddr, PeerId, Transport};
|
||||
use libp2p_tcp as tcp;
|
||||
|
||||
#[test]
|
||||
fn dialer_connects_to_listener_ipv4() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user