From 4dfc45bfec008405988edca2432c27ed5b93535f Mon Sep 17 00:00:00 2001 From: P1R0 Date: Wed, 21 Aug 2024 02:18:46 -0600 Subject: [PATCH 01/36] refactor: ping tutorial using tokio Solves issue #5554 also referenced first in #4449 Pull-Request: #5559. --- libp2p/src/tutorials/ping.rs | 131 +++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/libp2p/src/tutorials/ping.rs b/libp2p/src/tutorials/ping.rs index 309d5b36baa..31bf5ba3a14 100644 --- a/libp2p/src/tutorials/ping.rs +++ b/libp2p/src/tutorials/ping.rs @@ -44,7 +44,7 @@ //! 3. Adding `libp2p` as well as `futures` as dependencies in the //! `Cargo.toml` file. Current crate versions may be found at //! [crates.io](https://crates.io/). -//! We will also include `async-std` with the +//! We will also include `tokio` with the //! "attributes" feature to allow for an `async main`. //! At the time of writing we have: //! @@ -55,9 +55,9 @@ //! edition = "2021" //! //! [dependencies] -//! libp2p = { version = "0.52", features = ["tcp", "tls", "dns", "async-std", "noise", "yamux", "websocket", "ping", "macros"] } -//! futures = "0.3.21" -//! async-std = { version = "1.12.0", features = ["attributes"] } +//! libp2p = { version = "0.54", features = ["noise", "ping", "tcp", "tokio", "yamux"] } +//! futures = "0.3.30" +//! tokio = { version = "1.37.0", features = ["full"] } //! tracing-subscriber = { version = "0.3", features = ["env-filter"] } //! ``` //! @@ -74,9 +74,11 @@ //! use std::error::Error; //! use tracing_subscriber::EnvFilter; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity(); //! @@ -89,7 +91,7 @@ //! ## Transport //! //! Next up we need to construct a transport. Each transport in libp2p provides encrypted streams. -//! E.g. combining TCP to establish connections, TLS to encrypt these connections and Yamux to run +//! E.g. combining TCP to establish connections, NOISE to encrypt these connections and Yamux to run //! one or more streams on a connection. Another libp2p transport is QUIC, providing encrypted //! streams out-of-the-box. We will stick to TCP for now. Each of these implement the [`Transport`] //! trait. @@ -97,17 +99,20 @@ //! ```rust //! use std::error::Error; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )?; //! //! Ok(()) @@ -137,20 +142,22 @@ //! With the above in mind, let's extend our example, creating a [`ping::Behaviour`](crate::ping::Behaviour) at the end: //! //! ```rust -//! use libp2p::ping; -//! use tracing_subscriber::EnvFilter; //! use std::error::Error; +//! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())?; //! @@ -166,20 +173,22 @@ //! to the [`Transport`] as well as events from the [`Transport`] to the [`NetworkBehaviour`]. //! //! ```rust -//! use libp2p::ping; //! use std::error::Error; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? //! .build(); @@ -199,24 +208,25 @@ //! Thus, without any other behaviour in place, we would not be able to observe the pings. //! //! ```rust -//! use libp2p::ping; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! Ok(()) @@ -250,24 +260,25 @@ //! remote peer. //! //! ```rust -//! use libp2p::{ping, Multiaddr}; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux, Multiaddr}; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely.. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned @@ -293,26 +304,26 @@ //! outgoing connection in case we specify an address on the CLI. //! //! ```no_run -//! use futures::prelude::*; -//! use libp2p::swarm::SwarmEvent; -//! use libp2p::{ping, Multiaddr}; -//! use std::error::Error; -//! use std::time::Duration; +//! use std::{error::Error, time::Duration}; //! use tracing_subscriber::EnvFilter; +//! use libp2p::{noise, ping, tcp, yamux, Multiaddr, swarm::SwarmEvent}; +//! use futures::prelude::*; //! -//! #[async_std::main] +//! #[tokio::main] //! async fn main() -> Result<(), Box> { -//! tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init(); +//! let _ = tracing_subscriber::fmt() +//! .with_env_filter(EnvFilter::from_default_env()) +//! .try_init(); //! //! let mut swarm = libp2p::SwarmBuilder::with_new_identity() -//! .with_async_std() +//! .with_tokio() //! .with_tcp( -//! libp2p::tcp::Config::default(), -//! libp2p::tls::Config::new, -//! libp2p::yamux::Config::default, +//! tcp::Config::default(), +//! noise::Config::new, +//! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) // Allows us to observe pings indefinitely. +//! .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned From f0cbd4fb0cef8d1ae2298901eab95acb5f104ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Thu, 22 Aug 2024 18:49:08 +0100 Subject: [PATCH 02/36] chore(ci): update Rust stable version Pull-Request: #5568. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0465540d47..2c1dfc8aaef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -225,7 +225,7 @@ jobs: fail-fast: false matrix: rust-version: [ - 1.78.0, # current stable + 1.80.0, # current stable beta, ] steps: From de8cba961b16d2f418153c6ca550f39256d8c12b Mon Sep 17 00:00:00 2001 From: Panagiotis Ganelis <50522617+PanGan21@users.noreply.github.com> Date: Tue, 27 Aug 2024 10:25:13 +0300 Subject: [PATCH 03/36] feat(kad): emit `ToSwarm::NewExternalAddrOfPeer` (#5549) ## Description Updates `libp2p-kad` to emit new event `ToSwarm::NewExternalAddrOfPeer` whenever it discovers a new address through the DHT. Related: #5103 ## Notes & open questions ## Change checklist - [X] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [X] I have added tests that prove my fix is effective or that my feature works - [ ] A changelog entry has been made in the appropriate crates --------- Co-authored-by: Guillaume Michel --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 5 +++++ protocols/kad/Cargo.toml | 2 +- protocols/kad/src/behaviour.rs | 8 +++++++- protocols/kad/tests/client_mode.rs | 18 ++++++++++++++++-- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8932f40a04c..783480bb8b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2928,7 +2928,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.46.1" +version = "0.46.2" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index 31c3a8e4b9e..8216c7a1787 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,7 +86,7 @@ libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.45.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } -libp2p-kad = { version = "0.46.1", path = "protocols/kad" } +libp2p-kad = { version = "0.46.2", path = "protocols/kad" } libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.15.0", path = "misc/metrics" } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index a41d6b9a131..f4e25e0de05 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.46.2 + +- Emit `ToSwarm::NewExternalAddrOfPeer`. + See [PR 5549](https://github.com/libp2p/rust-libp2p/pull/5549) + ## 0.46.1 - Use new provider record update strategy to prevent Sybil attack. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index a00959fced6..11a670933db 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.46.1" +version = "0.46.2" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index fc3d8a1adaa..a541648707a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -2562,13 +2562,19 @@ where // Drain applied pending entries from the routing table. if let Some(entry) = self.kbuckets.take_applied_pending() { let kbucket::Node { key, value } = entry.inserted; + let peer_id = key.into_preimage(); + self.queued_events + .push_back(ToSwarm::NewExternalAddrOfPeer { + peer_id, + address: value.first().clone(), + }); let event = Event::RoutingUpdated { bucket_range: self .kbuckets .bucket(&key) .map(|b| b.range()) .expect("Self to never be applied from pending."), - peer: key.into_preimage(), + peer: peer_id, is_new_peer: true, addresses: value, old_peer: entry.evicted.map(|n| n.key.into_preimage()), diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index 6aceeb27263..2c8d11beac7 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -23,14 +23,21 @@ async fn server_gets_added_to_routing_table_by_client() { let server_peer_id = *server.local_peer_id(); async_std::task::spawn(server.loop_on_next()); - let peer = client + let external_event_peer = client + .wait(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id), + _ => None, + }) + .await; + let routing_updated_peer = client .wait(|e| match e { SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer), _ => None, }) .await; - assert_eq!(peer, server_peer_id); + assert_eq!(external_event_peer, server_peer_id); + assert_eq!(routing_updated_peer, server_peer_id); } #[async_std::test] @@ -126,6 +133,12 @@ async fn set_client_to_server_mode() { let server_peer_id = *server.local_peer_id(); + let peer_id = client + .wait(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id), + _ => None, + }) + .await; let client_event = client.wait(|e| match e { SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer), _ => None, @@ -138,6 +151,7 @@ async fn set_client_to_server_mode() { let (peer, info) = futures::future::join(client_event, server_event).await; assert_eq!(peer, server_peer_id); + assert_eq!(peer_id, server_peer_id); assert!(info .protocols .iter() From aa9317fbdd88cc4c9a39ea608fa1d57e022297fc Mon Sep 17 00:00:00 2001 From: Probot <94048855+Prabhat1308@users.noreply.github.com> Date: Tue, 27 Aug 2024 18:32:30 +0530 Subject: [PATCH 04/36] fix: Change `__Nonexhaustive` to `__Invalid` and update web-sys (#5569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Bumps up web-sys version to `0.3.70` fixes #5557 ## Change checklist - [X] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [X] A changelog entry has been made in the appropriate crates --------- Co-authored-by: Darius Clark Co-authored-by: João Oliveira Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- Cargo.lock | 54 +++++++++++-------- Cargo.toml | 2 +- transports/webrtc-websys/CHANGELOG.md | 5 ++ transports/webrtc-websys/Cargo.toml | 4 +- transports/webrtc-websys/src/connection.rs | 9 ++-- transports/webrtc-websys/src/sdp.rs | 8 +-- .../src/stream/poll_data_channel.rs | 3 +- transports/webtransport-websys/CHANGELOG.md | 2 + transports/webtransport-websys/Cargo.toml | 8 +-- wasm-tests/webtransport-tests/Cargo.toml | 8 +-- 10 files changed, 62 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 783480bb8b1..d4350c0496b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2571,9 +2571,9 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" dependencies = [ "wasm-bindgen", ] @@ -3509,7 +3509,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc-websys" -version = "0.4.0-alpha" +version = "0.4.0-alpha.2" dependencies = [ "bytes", "futures", @@ -3804,6 +3804,16 @@ dependencies = [ "unicase", ] +[[package]] +name = "minicov" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71e683cd655513b99affab7d317deb690528255a0d5f717f1024093c12b169" +dependencies = [ + "cc", + "walkdir", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -6677,19 +6687,20 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" dependencies = [ "cfg-if", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" dependencies = [ "bumpalo", "log", @@ -6702,9 +6713,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" dependencies = [ "cfg-if", "js-sys", @@ -6714,9 +6725,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -6724,9 +6735,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", @@ -6737,18 +6748,19 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "wasm-bindgen-test" -version = "0.3.42" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9bf62a58e0780af3e852044583deee40983e5886da43a271dd772379987667b" +checksum = "68497a05fb21143a08a7d24fc81763384a3072ee43c44e86aad1744d6adef9d9" dependencies = [ "console_error_panic_hook", "js-sys", + "minicov", "scoped-tls", "wasm-bindgen", "wasm-bindgen-futures", @@ -6757,9 +6769,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.42" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f89739351a2e03cb94beb799d47fb2cac01759b40ec441f7de39b00cbf7ef0" +checksum = "4b8220be1fa9e4c889b30fd207d4906657e7e90b12e0e6b0c8b8d8709f5de021" dependencies = [ "proc-macro2", "quote", @@ -6779,9 +6791,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 8216c7a1787..bd6018b024f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,7 +111,7 @@ libp2p-uds = { version = "0.41.0", path = "transports/uds" } libp2p-upnp = { version = "0.3.0", path = "protocols/upnp" } libp2p-webrtc = { version = "0.8.0-alpha", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.3.0", path = "misc/webrtc-utils" } -libp2p-webrtc-websys = { version = "0.4.0-alpha", path = "transports/webrtc-websys" } +libp2p-webrtc-websys = { version = "0.4.0-alpha.2", path = "transports/webrtc-websys" } libp2p-websocket = { version = "0.44.0", path = "transports/websocket" } libp2p-websocket-websys = { version = "0.4.0", path = "transports/websocket-websys" } libp2p-webtransport-websys = { version = "0.4.0", path = "transports/webtransport-websys" } diff --git a/transports/webrtc-websys/CHANGELOG.md b/transports/webrtc-websys/CHANGELOG.md index 475b13727e6..5b8f2efb3b0 100644 --- a/transports/webrtc-websys/CHANGELOG.md +++ b/transports/webrtc-websys/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.4.0-alpha.2 + +- Bump version of web-sys and update `__Nonexhaustive` to `__Invalid`. + See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569) + ## 0.4.0-alpha - Implement refactored `Transport`. diff --git a/transports/webrtc-websys/Cargo.toml b/transports/webrtc-websys/Cargo.toml index c874b33bfc7..453abe57f74 100644 --- a/transports/webrtc-websys/Cargo.toml +++ b/transports/webrtc-websys/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT" name = "libp2p-webrtc-websys" repository = "https://github.com/libp2p/rust-libp2p" rust-version = { workspace = true } -version = "0.4.0-alpha" +version = "0.4.0-alpha.2" publish = true [dependencies] @@ -25,7 +25,7 @@ thiserror = "1" tracing = { workspace = true } wasm-bindgen = { version = "0.2.90" } wasm-bindgen-futures = { version = "0.4.42" } -web-sys = { version = "0.3.69", features = ["Document", "Location", "MessageEvent", "Navigator", "RtcCertificate", "RtcConfiguration", "RtcDataChannel", "RtcDataChannelEvent", "RtcDataChannelInit", "RtcDataChannelState", "RtcDataChannelType", "RtcPeerConnection", "RtcSdpType", "RtcSessionDescription", "RtcSessionDescriptionInit", "Window"] } +web-sys = { version = "0.3.70", features = ["Document", "Location", "MessageEvent", "Navigator", "RtcCertificate", "RtcConfiguration", "RtcDataChannel", "RtcDataChannelEvent", "RtcDataChannelInit", "RtcDataChannelState", "RtcDataChannelType", "RtcPeerConnection", "RtcSdpType", "RtcSessionDescription", "RtcSessionDescriptionInit", "Window"] } [lints] workspace = true diff --git a/transports/webrtc-websys/src/connection.rs b/transports/webrtc-websys/src/connection.rs index b858237da63..d0c6ccd2238 100644 --- a/transports/webrtc-websys/src/connection.rs +++ b/transports/webrtc-websys/src/connection.rs @@ -186,11 +186,11 @@ impl RtcPeerConnection { let certificate = JsFuture::from(certificate_promise).await?; - let mut config = RtcConfiguration::default(); + let config = RtcConfiguration::default(); // wrap certificate in a js Array first before adding it to the config object let certificate_arr = js_sys::Array::new(); certificate_arr.push(&certificate); - config.certificates(&certificate_arr); + config.set_certificates(&certificate_arr); let inner = web_sys::RtcPeerConnection::new_with_configuration(&config)?; @@ -214,8 +214,9 @@ impl RtcPeerConnection { let dc = match negotiated { true => { - let mut options = RtcDataChannelInit::new(); - options.negotiated(true).id(0); // id is only ever set to zero when negotiated is true + let options = RtcDataChannelInit::new(); + options.set_negotiated(true); + options.set_id(0); // id is only ever set to zero when negotiated is true self.inner .create_data_channel_with_data_channel_dict(LABEL, &options) diff --git a/transports/webrtc-websys/src/sdp.rs b/transports/webrtc-websys/src/sdp.rs index 439182ea4db..9e63fd92462 100644 --- a/transports/webrtc-websys/src/sdp.rs +++ b/transports/webrtc-websys/src/sdp.rs @@ -8,8 +8,8 @@ pub(crate) fn answer( server_fingerprint: Fingerprint, client_ufrag: &str, ) -> RtcSessionDescriptionInit { - let mut answer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Answer); - answer_obj.sdp(&libp2p_webrtc_utils::sdp::answer( + let answer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Answer); + answer_obj.set_sdp(&libp2p_webrtc_utils::sdp::answer( addr, server_fingerprint, client_ufrag, @@ -48,8 +48,8 @@ pub(crate) fn offer(offer: String, client_ufrag: &str) -> RtcSessionDescriptionI tracing::trace!(offer=%munged_sdp_offer, "Created SDP offer"); - let mut offer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Offer); - offer_obj.sdp(&munged_sdp_offer); + let offer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Offer); + offer_obj.set_sdp(&munged_sdp_offer); offer_obj } diff --git a/transports/webrtc-websys/src/stream/poll_data_channel.rs b/transports/webrtc-websys/src/stream/poll_data_channel.rs index dfd861de9df..3ec744342eb 100644 --- a/transports/webrtc-websys/src/stream/poll_data_channel.rs +++ b/transports/webrtc-websys/src/stream/poll_data_channel.rs @@ -143,7 +143,8 @@ impl PollDataChannel { RtcDataChannelState::Closing | RtcDataChannelState::Closed => { return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())) } - RtcDataChannelState::Open | RtcDataChannelState::__Nonexhaustive => {} + RtcDataChannelState::Open | RtcDataChannelState::__Invalid => {} + _ => {} } if self.overloaded.load(Ordering::SeqCst) { diff --git a/transports/webtransport-websys/CHANGELOG.md b/transports/webtransport-websys/CHANGELOG.md index 2aab226ab12..411117918bd 100644 --- a/transports/webtransport-websys/CHANGELOG.md +++ b/transports/webtransport-websys/CHANGELOG.md @@ -2,6 +2,8 @@ - Implement refactored `Transport`. See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568) +- Bump version of web-sys and wasm-bindgen. + See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569) ## 0.3.0 diff --git a/transports/webtransport-websys/Cargo.toml b/transports/webtransport-websys/Cargo.toml index 370158190b1..9541c49b737 100644 --- a/transports/webtransport-websys/Cargo.toml +++ b/transports/webtransport-websys/Cargo.toml @@ -15,7 +15,7 @@ categories = ["network-programming", "asynchronous"] [dependencies] futures = { workspace = true } -js-sys = "0.3.69" +js-sys = "0.3.70" libp2p-core = { workspace = true } libp2p-identity = { workspace = true } libp2p-noise = { workspace = true } @@ -24,9 +24,9 @@ multihash = { workspace = true } send_wrapper = { version = "0.6.0", features = ["futures"] } thiserror = "1.0.61" tracing = { workspace = true } -wasm-bindgen = "0.2.90" -wasm-bindgen-futures = "0.4.42" -web-sys = { version = "0.3.69", features = [ +wasm-bindgen = "0.2.93" +wasm-bindgen-futures = "0.4.43" +web-sys = { version = "0.3.70", features = [ "ReadableStreamDefaultReader", "WebTransport", "WebTransportBidirectionalStream", diff --git a/wasm-tests/webtransport-tests/Cargo.toml b/wasm-tests/webtransport-tests/Cargo.toml index cf51a510a3f..d7db378ab1a 100644 --- a/wasm-tests/webtransport-tests/Cargo.toml +++ b/wasm-tests/webtransport-tests/Cargo.toml @@ -17,10 +17,10 @@ libp2p-noise = { workspace = true } libp2p-webtransport-websys = { workspace = true } multiaddr = { workspace = true } multihash = { workspace = true } -wasm-bindgen = "0.2.90" -wasm-bindgen-futures = "0.4.42" -wasm-bindgen-test = "0.3.42" -web-sys = { version = "0.3.69", features = ["Response", "Window"] } +wasm-bindgen = "0.2.93" +wasm-bindgen-futures = "0.4.43" +wasm-bindgen-test = "0.3.43" +web-sys = { version = "0.3.70", features = ["Response", "Window"] } [lints] workspace = true From 56b6c62f6c71e8fa217e9e772d98824ba5d79c5c Mon Sep 17 00:00:00 2001 From: maqi Date: Wed, 28 Aug 2024 00:03:31 +0800 Subject: [PATCH 05/36] feat(kad): expose a kad query facility allowing dynamic num_results (#5555) ## Description This PR is to expose a kad query facility that allowing specify num_results dynamically. It is related to the [Sybil Defence issue](https://github.com/libp2p/rust-libp2p/issues/4769), that during the attempt of implementation on higher level code, it is find will be useful if libp2p-kad can expose such facility. The PR try not to cause any interference to the existing work flow, only introduce an `extra exposal`. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 5 +++ protocols/kad/Cargo.toml | 2 +- protocols/kad/src/behaviour.rs | 32 ++++++++++++++- protocols/kad/src/behaviour/test.rs | 64 ++++++++++++++++++++++++++++- protocols/kad/src/query.rs | 10 ++++- 7 files changed, 110 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4350c0496b..41ed8883c52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2928,7 +2928,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.46.2" +version = "0.47.0" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index bd6018b024f..c23bb8650f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,7 +86,7 @@ libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.45.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } -libp2p-kad = { version = "0.46.2", path = "protocols/kad" } +libp2p-kad = { version = "0.47.0", path = "protocols/kad" } libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.15.0", path = "misc/metrics" } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index f4e25e0de05..12ccca2d7f1 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.47.0 + +- Expose a kad query facility allowing specify num_results dynamicly. + See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555). + ## 0.46.2 - Emit `ToSwarm::NewExternalAddrOfPeer`. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 11a670933db..5b95b8ac17d 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.46.2" +version = "0.47.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index a541648707a..50715c53c74 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -732,6 +732,31 @@ where /// The result of the query is delivered in a /// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`]. pub fn get_closest_peers(&mut self, key: K) -> QueryId + where + K: Into> + Into> + Clone, + { + self.get_closest_peers_inner(key, None) + } + + /// Initiates an iterative query for the closest peers to the given key. + /// The expected responding peers is specified by `num_results` + /// Note that the result is capped after exceeds K_VALUE + /// + /// The result of the query is delivered in a + /// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`]. + pub fn get_n_closest_peers(&mut self, key: K, num_results: NonZeroUsize) -> QueryId + where + K: Into> + Into> + Clone, + { + // The inner code never expect higher than K_VALUE results to be returned. + // And removing such cap will be tricky, + // since it would involve forging a new key and additional requests. + // Hence bound to K_VALUE here to set clear expectation and prevent unexpected behaviour. + let capped_num_results = std::cmp::min(num_results, K_VALUE); + self.get_closest_peers_inner(key, Some(capped_num_results)) + } + + fn get_closest_peers_inner(&mut self, key: K, num_results: Option) -> QueryId where K: Into> + Into> + Clone, { @@ -740,6 +765,7 @@ where let info = QueryInfo::GetClosestPeers { key, step: ProgressStep::first(), + num_results, }; let peer_keys: Vec> = self.kbuckets.closest_keys(&target).collect(); self.queries.add_iter_closest(target, peer_keys, info) @@ -1485,7 +1511,7 @@ where }) } - QueryInfo::GetClosestPeers { key, mut step } => { + QueryInfo::GetClosestPeers { key, mut step, .. } => { step.last = true; Some(Event::OutboundQueryProgressed { @@ -1702,7 +1728,7 @@ where }, }), - QueryInfo::GetClosestPeers { key, mut step } => { + QueryInfo::GetClosestPeers { key, mut step, .. } => { step.last = true; Some(Event::OutboundQueryProgressed { id: query_id, @@ -3181,6 +3207,8 @@ pub enum QueryInfo { key: Vec, /// Current index of events. step: ProgressStep, + /// If required, `num_results` specifies expected responding peers + num_results: Option, }, /// A (repeated) query initiated by [`Behaviour::get_providers`]. diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index c4859f2f138..7409168ac2a 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -263,7 +263,7 @@ fn query_iter() { match swarms[0].behaviour_mut().query(&qid) { Some(q) => match q.info() { - QueryInfo::GetClosestPeers { key, step } => { + QueryInfo::GetClosestPeers { key, step, .. } => { assert_eq!(&key[..], search_target.to_bytes().as_slice()); assert_eq!(usize::from(step.count), 1); } @@ -425,6 +425,68 @@ fn unresponsive_not_returned_indirect() { })) } +// Test the result of get_closest_peers with different num_results +// Note that the result is capped after exceeds K_VALUE +#[test] +fn get_closest_with_different_num_results() { + let k_value = K_VALUE.get(); + for replication_factor in [5, k_value / 2, k_value] { + for num_results in k_value / 2..k_value * 2 { + get_closest_with_different_num_results_inner(num_results, replication_factor) + } + } +} + +fn get_closest_with_different_num_results_inner(num_results: usize, replication_factor: usize) { + let k_value = K_VALUE.get(); + let num_of_nodes = 3 * k_value; + let mut cfg = Config::new(PROTOCOL_NAME); + cfg.set_replication_factor(NonZeroUsize::new(replication_factor).unwrap()); + let swarms = build_connected_nodes_with_config(num_of_nodes, replication_factor - 1, cfg); + + let mut swarms = swarms + .into_iter() + .map(|(_addr, swarm)| swarm) + .collect::>(); + + // Ask first to search a random value. + let search_target = PeerId::random(); + let Some(num_results_nonzero) = std::num::NonZeroUsize::new(num_results) else { + panic!("Unexpected NonZeroUsize val of {num_results}"); + }; + swarms[0] + .behaviour_mut() + .get_n_closest_peers(search_target, num_results_nonzero); + + block_on(poll_fn(move |ctx| { + for swarm in &mut swarms { + loop { + match swarm.poll_next_unpin(ctx) { + Poll::Ready(Some(SwarmEvent::Behaviour(Event::OutboundQueryProgressed { + result: QueryResult::GetClosestPeers(Ok(ok)), + .. + }))) => { + assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); + if num_results > k_value { + assert_eq!(ok.peers.len(), k_value, "Failed with replication_factor: {replication_factor}, num_results: {num_results}"); + } else { + assert_eq!(ok.peers.len(), num_results, "Failed with replication_factor: {replication_factor}, num_results: {num_results}"); + } + + return Poll::Ready(()); + } + // Ignore any other event. + Poll::Ready(Some(_)) => (), + e @ Poll::Ready(_) => panic!("Unexpected return value: {e:?}"), + Poll::Pending => break, + } + } + } + + Poll::Pending + })) +} + #[test] fn get_record_not_found() { let mut swarms = build_nodes(3); diff --git a/protocols/kad/src/query.rs b/protocols/kad/src/query.rs index c598bac012e..1a895d9627c 100644 --- a/protocols/kad/src/query.rs +++ b/protocols/kad/src/query.rs @@ -138,8 +138,16 @@ impl QueryPool { T: Into + Clone, I: IntoIterator>, { + let num_results = match info { + QueryInfo::GetClosestPeers { + num_results: Some(val), + .. + } => val, + _ => self.config.replication_factor, + }; + let cfg = ClosestPeersIterConfig { - num_results: self.config.replication_factor, + num_results, parallelism: self.config.parallelism, ..ClosestPeersIterConfig::default() }; From 64c6eb299ce905b2f81fd1317ec616f56dd37986 Mon Sep 17 00:00:00 2001 From: Elias Rad <146735585+nnsW3@users.noreply.github.com> Date: Tue, 27 Aug 2024 20:54:23 +0300 Subject: [PATCH 06/36] chore: fix spelling issues (#5522) Hello I found several spelling issues in your docs. Br, Elias. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- docs/maintainer-handbook.md | 2 +- examples/ipfs-kad/README.md | 2 +- wasm-tests/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/maintainer-handbook.md b/docs/maintainer-handbook.md index 6d36f6fe77c..0b090901216 100644 --- a/docs/maintainer-handbook.md +++ b/docs/maintainer-handbook.md @@ -31,7 +31,7 @@ This will have mergify approve your PR, thus fulfilling all requirements to auto Our CI checks that each crate which is modified gets a changelog entry. Whilst this is a good default safety-wise, it creates a lot of false-positives for changes that are internal and don't need a changelog entry. -For PRs that in the categories `chore`, `deps`, `refactor` and `docs`, this check is disabled automatically. +For PRs in the categories `chore`, `deps`, `refactor` and `docs`, this check is disabled automatically. Any other PR needs to explicitly disable this check if desired by applying the `internal-change` label. ## Dependencies diff --git a/examples/ipfs-kad/README.md b/examples/ipfs-kad/README.md index a46246a3920..05556c89382 100644 --- a/examples/ipfs-kad/README.md +++ b/examples/ipfs-kad/README.md @@ -87,5 +87,5 @@ Failed to insert the PK record ## Conclusion In conclusion, this example provides a practical demonstration of using the Rust P2P Library to interact with the Kademlia protocol on the IPFS network. -By examining the code and running the example, users can gain insights into the inner workings of Kademlia and how it performs various basic actions like getting the closes peers or inserting records into the DHT. +By examining the code and running the example, users can gain insights into the inner workings of Kademlia and how it performs various basic actions like getting the closest peers or inserting records into the DHT. This knowledge can be valuable when developing peer-to-peer applications or understanding decentralized networks. diff --git a/wasm-tests/README.md b/wasm-tests/README.md index 1d0902b106c..2538e48a145 100644 --- a/wasm-tests/README.md +++ b/wasm-tests/README.md @@ -8,4 +8,4 @@ Before you run the tests you need to install the following: # Run tests -Just call `run-all.sh` or `run.sh` in the test directory you are interested. +Just call `run-all.sh` or `run.sh` in the test directory if you are interested. From e63975d7742710d4498b941e151c5177e06392ce Mon Sep 17 00:00:00 2001 From: stormshield-frb <144998884+stormshield-frb@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:07:26 +0200 Subject: [PATCH 07/36] feat(allow-block-list): add getters and return results (#5572) ## Description Small changes to improve usability of the `allow-block-list` Behaviour. When trying to use it, we found ourselves wanting to known: - which were the current allowed or blocked peers: hence the new methods `allowed_peers` and `blocked_peers` - if the peer was already present in the set when adding or removing it from the set: that is why `allow/disallow_peer` and `block/unblock_peer` methods now return a boolean, allowing the end user the know if there was a change or not (in our case, we needed it in order to log something). ## Notes & open questions ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --- Cargo.lock | 2 +- Cargo.toml | 2 +- misc/allow-block-list/CHANGELOG.md | 6 +++ misc/allow-block-list/Cargo.toml | 2 +- misc/allow-block-list/src/lib.rs | 66 ++++++++++++++++++++++-------- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41ed8883c52..7f37b652c30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2675,7 +2675,7 @@ dependencies = [ [[package]] name = "libp2p-allow-block-list" -version = "0.4.0" +version = "0.4.1" dependencies = [ "async-std", "libp2p-core", diff --git a/Cargo.toml b/Cargo.toml index c23bb8650f3..8d63ac3ee1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,7 +76,7 @@ asynchronous-codec = { version = "0.7.0" } futures-bounded = { version = "0.2.4" } futures-rustls = { version = "0.26.0", default-features = false } libp2p = { version = "0.54.1", path = "libp2p" } -libp2p-allow-block-list = { version = "0.4.0", path = "misc/allow-block-list" } +libp2p-allow-block-list = { version = "0.4.1", path = "misc/allow-block-list" } libp2p-autonat = { version = "0.13.0", path = "protocols/autonat" } libp2p-connection-limits = { version = "0.4.0", path = "misc/connection-limits" } libp2p-core = { version = "0.42.0", path = "core" } diff --git a/misc/allow-block-list/CHANGELOG.md b/misc/allow-block-list/CHANGELOG.md index 0017cbc8648..3cda0603ee4 100644 --- a/misc/allow-block-list/CHANGELOG.md +++ b/misc/allow-block-list/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.4.1 + +- Add getters & setters for the allowed/blocked peers. + Return a `bool` for every "insert/remove" function, informing if a change was performed. + See [PR 5572](https://github.com/libp2p/rust-libp2p/pull/5572). + ## 0.4.0 diff --git a/misc/allow-block-list/Cargo.toml b/misc/allow-block-list/Cargo.toml index 4209d72ab4f..1ff0ccff906 100644 --- a/misc/allow-block-list/Cargo.toml +++ b/misc/allow-block-list/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-allow-block-list" edition = "2021" rust-version = { workspace = true } description = "Allow/block list connection management for libp2p." -version = "0.4.0" +version = "0.4.1" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index c877ab09c9b..7646638a651 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -94,44 +94,74 @@ pub struct BlockedPeers { } impl Behaviour { + /// Peers that are currently allowed. + pub fn allowed_peers(&self) -> &HashSet { + &self.state.peers + } + /// Allow connections to the given peer. - pub fn allow_peer(&mut self, peer: PeerId) { - self.state.peers.insert(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set. + pub fn allow_peer(&mut self, peer: PeerId) -> bool { + let inserted = self.state.peers.insert(peer); + if inserted { + if let Some(waker) = self.waker.take() { + waker.wake() + } } + inserted } /// Disallow connections to the given peer. /// /// All active connections to this peer will be closed immediately. - pub fn disallow_peer(&mut self, peer: PeerId) { - self.state.peers.remove(&peer); - self.close_connections.push_back(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set. + pub fn disallow_peer(&mut self, peer: PeerId) -> bool { + let removed = self.state.peers.remove(&peer); + if removed { + self.close_connections.push_back(peer); + if let Some(waker) = self.waker.take() { + waker.wake() + } } + removed } } impl Behaviour { + /// Peers that are currently blocked. + pub fn blocked_peers(&self) -> &HashSet { + &self.state.peers + } + /// Block connections to a given peer. /// /// All active connections to this peer will be closed immediately. - pub fn block_peer(&mut self, peer: PeerId) { - self.state.peers.insert(peer); - self.close_connections.push_back(peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was newly inserted. Does nothing if the peer was already present in the set. + pub fn block_peer(&mut self, peer: PeerId) -> bool { + let inserted = self.state.peers.insert(peer); + if inserted { + self.close_connections.push_back(peer); + if let Some(waker) = self.waker.take() { + waker.wake() + } } + inserted } /// Unblock connections to a given peer. - pub fn unblock_peer(&mut self, peer: PeerId) { - self.state.peers.remove(&peer); - if let Some(waker) = self.waker.take() { - waker.wake() + /// + /// Returns whether the peer was present in the set. Does nothing if the peer was not present in the set. + pub fn unblock_peer(&mut self, peer: PeerId) -> bool { + let removed = self.state.peers.remove(&peer); + if removed { + if let Some(waker) = self.waker.take() { + waker.wake() + } } + removed } } From cefd22b8846ab005b311d5a3bc3d90c7e3a67872 Mon Sep 17 00:00:00 2001 From: stormshield-frb <144998884+stormshield-frb@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:58:41 +0200 Subject: [PATCH 08/36] feat(kad): add `mode` getter on `Behaviour` (#5573) ## Description Small PR adding a getter for the `mode` attribute of the `kad::Behaviour` in order to get the mode that the DHT is operating in, at the moment. In our project, we needed to expose an API endpoint which included the mode that the DHT was operating. Having a getter was necessary so we are upstreaming this change. ## Notes & open questions ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --- protocols/kad/CHANGELOG.md | 3 +++ protocols/kad/src/behaviour.rs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 12ccca2d7f1..d0ab7986aad 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -2,6 +2,9 @@ - Expose a kad query facility allowing specify num_results dynamicly. See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555). +- Add `mode` getter on `Behaviour`. + See [PR 5573](https://github.com/libp2p/rust-libp2p/pull/5573). + ## 0.46.2 diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 50715c53c74..0b15e507ba4 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1111,6 +1111,11 @@ where } } + /// Get the [`Mode`] in which the DHT is currently operating. + pub fn mode(&self) -> Mode { + self.mode + } + fn reconfigure_mode(&mut self) { if self.connections.is_empty() { return; From 95f40ff87be47cf919f80d7c4d63257fecb227a3 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 30 Aug 2024 22:36:28 +1000 Subject: [PATCH 09/36] chore: add funding.json (#5581) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This adds a FUNDING.json file to the main repo. This is required for various projects, however the immediate case is for registering for an optimism retropgf grant. --------- Co-authored-by: João Oliveira --- FUNDING.json | 5 +++++ funding.json | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 FUNDING.json delete mode 100644 funding.json diff --git a/FUNDING.json b/FUNDING.json new file mode 100644 index 00000000000..cce3fb3fe4c --- /dev/null +++ b/FUNDING.json @@ -0,0 +1,5 @@ +{ + "opRetro": { + "projectId": "0xdf1bb03d08808e2d789f5eac8462bdc560f1bb5b0877f0cf8c66ab53a0bc2f5c" + } +} diff --git a/funding.json b/funding.json deleted file mode 100644 index bcf7fc2783d..00000000000 --- a/funding.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "opRetro": { - "projectId": "0x966804cb492e1a4bde5d781a676a44a23d69aa5dd2562fa7a4f95bb606021c8b" - } -} From f38fb5c334079b170d9cf8365bf5824210606f36 Mon Sep 17 00:00:00 2001 From: P1R0 Date: Fri, 30 Aug 2024 11:06:02 -0600 Subject: [PATCH 10/36] chore(examples): remove unused dependency from rendezvous (#5580) ## Description Following on issue #4449 refactor: remove unnecesary dependencies rendezvous example (async-std) ## Notes & open questions in the rendezvous example there where unnecessary dependencies in the Cargo.toml ## Change checklist * Removed unnecessary dependencies on examples/rendezvous/Cargo.toml - [x] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] A changelog entry has been made in the appropriate crates Co-authored-by: David E. Perez Negron R. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- Cargo.lock | 2 -- examples/rendezvous/Cargo.toml | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7f37b652c30..d0e45e9bb1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4984,8 +4984,6 @@ dependencies = [ name = "rendezvous-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "futures", "libp2p", "tokio", diff --git a/examples/rendezvous/Cargo.toml b/examples/rendezvous/Cargo.toml index 5a0672dcec8..4eea38616f4 100644 --- a/examples/rendezvous/Cargo.toml +++ b/examples/rendezvous/Cargo.toml @@ -9,10 +9,8 @@ license = "MIT" release = false [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" futures = { workspace = true } -libp2p = { path = "../../libp2p", features = [ "async-std", "identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] } +libp2p = { path = "../../libp2p", features = ["identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] } tokio = { workspace = true, features = ["rt-multi-thread", "macros", "time"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } From f0589c8d47bee507cba6cb2d3c73942ef1611a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 3 Sep 2024 12:50:23 +0100 Subject: [PATCH 11/36] chore(ci): update Dockerfiles Rust version (#5587) --- misc/server/Dockerfile | 2 +- protocols/perf/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/server/Dockerfile b/misc/server/Dockerfile index 9d2742f97e8..1583fba6bef 100644 --- a/misc/server/Dockerfile +++ b/misc/server/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.73.0 as chef +FROM rust:1.75.0 as chef RUN wget -q -O- https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.62/cargo-chef-x86_64-unknown-linux-gnu.tar.gz | tar -zx -C /usr/local/bin RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 WORKDIR /app diff --git a/protocols/perf/Dockerfile b/protocols/perf/Dockerfile index 6523e3bede1..1bd846cc228 100644 --- a/protocols/perf/Dockerfile +++ b/protocols/perf/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.67.0 as builder +FROM rust:1.75.0 as builder # Run with access to the target cache to speed up builds WORKDIR /workspace From 93169cc6fa4a35aa0b3d5ddbd9e393118e46f412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 3 Sep 2024 18:45:33 +0100 Subject: [PATCH 12/36] fix(gossipsub): Attempt to publish to at least mesh_n peers (#5578) ## Description With flood published disabled we've noticed that it can be the case that we have connected peers on topics but these peers are not in our mesh (perhaps due to their own mesh requirements). Currently, we fail to publish the message if there are no peers in our mesh. This PR adjusts this logic to always attempt to publish to at least mesh_n peers. If we have peers that are subscribed to a topic, we will now attempt to publish messages to them (provided they have the required score). This PR also simplies the peer and respective topics by moving the topic list each peer has subscribed to `PeerConnections` and removing both `peer_topics` and `topic_peers` from the main `Behaviour`. Per commit review is suggested. --------- Co-authored-by: Darius Clark Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/gossipsub/CHANGELOG.md | 5 + protocols/gossipsub/Cargo.toml | 2 +- protocols/gossipsub/src/behaviour.rs | 399 +++++++++------------ protocols/gossipsub/src/behaviour/tests.rs | 101 +++--- protocols/gossipsub/src/metrics.rs | 15 +- protocols/gossipsub/src/types.rs | 4 +- 8 files changed, 242 insertions(+), 288 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0e45e9bb1a..9c8eae458fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2838,7 +2838,7 @@ dependencies = [ [[package]] name = "libp2p-gossipsub" -version = "0.47.0" +version = "0.47.1" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index 8d63ac3ee1e..587bef4480a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ libp2p-core = { version = "0.42.0", path = "core" } libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } libp2p-dns = { version = "0.42.0", path = "transports/dns" } libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } -libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" } +libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" } libp2p-identify = { version = "0.45.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } libp2p-kad = { version = "0.47.0", path = "protocols/kad" } diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 8e115052d31..c47a9f40f66 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.47.1 + +- Attempt to publish to at least mesh_n peers when flood publish is disabled. + See [PR 5578](https://github.com/libp2p/rust-libp2p/pull/5578). + ## 0.47.0 diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 4cb590bed0c..665f757fcb3 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-gossipsub" edition = "2021" rust-version = { workspace = true } description = "Gossipsub protocol for libp2p" -version = "0.47.0" +version = "0.47.1" authors = ["Age Manning "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 0d1af1ada0c..16adb555a44 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -259,12 +259,6 @@ pub struct Behaviour { /// the set of [`ConnectionId`]s. connected_peers: HashMap, - /// A map of all connected peers - A map of topic hash to a list of gossipsub peer Ids. - topic_peers: HashMap>, - - /// A map of all connected peers to their subscribed topics. - peer_topics: HashMap>, - /// A set of all explicit peers. These are peers that remain connected and we unconditionally /// forward messages to, outside of the scoring system. explicit_peers: HashSet, @@ -443,8 +437,6 @@ where control_pool: HashMap::new(), publish_config: privacy.into(), duplicate_cache: DuplicateCache::new(config.duplicate_cache_time()), - topic_peers: HashMap::new(), - peer_topics: HashMap::new(), explicit_peers: HashSet::new(), blacklisted_peers: HashSet::new(), mesh: HashMap::new(), @@ -501,9 +493,9 @@ where /// Lists all known peers and their associated subscribed topics. pub fn all_peers(&self) -> impl Iterator)> { - self.peer_topics + self.connected_peers .iter() - .map(|(peer_id, topic_set)| (peer_id, topic_set.iter().collect())) + .map(|(peer_id, peer)| (peer_id, peer.topics.iter().collect())) } /// Lists all known peers and their associated protocol. @@ -535,7 +527,7 @@ where } // send subscription request to all peers - for peer in self.peer_topics.keys().copied().collect::>() { + for peer in self.connected_peers.keys().copied().collect::>() { tracing::debug!(%peer, "Sending SUBSCRIBE to peer"); let event = RpcOut::Subscribe(topic_hash.clone()); self.send_message(peer, event); @@ -563,7 +555,7 @@ where } // announce to all peers - for peer in self.peer_topics.keys().copied().collect::>() { + for peer in self.connected_peers.keys().copied().collect::>() { tracing::debug!(%peer, "Sending UNSUBSCRIBE to peer"); let event = RpcOut::Unsubscribe(topic_hash.clone()); self.send_message(peer, event); @@ -621,84 +613,103 @@ where let topic_hash = raw_message.topic.clone(); + let mut peers_on_topic = self + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(&topic_hash)) + .map(|(peer_id, _)| peer_id) + .peekable(); + + if peers_on_topic.peek().is_none() { + return Err(PublishError::InsufficientPeers); + } + let mut recipient_peers = HashSet::new(); - if let Some(set) = self.topic_peers.get(&topic_hash) { - if self.config.flood_publish() { - // Forward to all peers above score and all explicit peers - recipient_peers.extend(set.iter().filter(|p| { - self.explicit_peers.contains(*p) - || !self.score_below_threshold(p, |ts| ts.publish_threshold).0 - })); - } else { - match self.mesh.get(&raw_message.topic) { - // Mesh peers - Some(mesh_peers) => { - recipient_peers.extend(mesh_peers); + if self.config.flood_publish() { + // Forward to all peers above score and all explicit peers + recipient_peers.extend(peers_on_topic.filter(|p| { + self.explicit_peers.contains(*p) + || !self.score_below_threshold(p, |ts| ts.publish_threshold).0 + })); + } else { + match self.mesh.get(&topic_hash) { + // Mesh peers + Some(mesh_peers) => { + // We have a mesh set. We want to make sure to publish to at least `mesh_n` + // peers (if possible). + let needed_extra_peers = self.config.mesh_n().saturating_sub(mesh_peers.len()); + + if needed_extra_peers > 0 { + // We don't have `mesh_n` peers in our mesh, we will randomly select extras + // and publish to them. + + // Get a random set of peers that are appropriate to send messages too. + let peer_list = get_random_peers( + &self.connected_peers, + &topic_hash, + needed_extra_peers, + |peer| { + !mesh_peers.contains(peer) + && !self.explicit_peers.contains(peer) + && !self + .score_below_threshold(peer, |pst| pst.publish_threshold) + .0 + }, + ); + recipient_peers.extend(peer_list); } - // Gossipsub peers - None => { - tracing::debug!(topic=%topic_hash, "Topic not in the mesh"); - // If we have fanout peers add them to the map. - if self.fanout.contains_key(&topic_hash) { - for peer in self.fanout.get(&topic_hash).expect("Topic must exist") { - recipient_peers.insert(*peer); - } - } else { - // We have no fanout peers, select mesh_n of them and add them to the fanout - let mesh_n = self.config.mesh_n(); - let new_peers = get_random_peers( - &self.topic_peers, - &self.connected_peers, - &topic_hash, - mesh_n, - { - |p| { - !self.explicit_peers.contains(p) - && !self - .score_below_threshold(p, |pst| { - pst.publish_threshold - }) - .0 - } - }, - ); - // Add the new peers to the fanout and recipient peers - self.fanout.insert(topic_hash.clone(), new_peers.clone()); - for peer in new_peers { - tracing::debug!(%peer, "Peer added to fanout"); - recipient_peers.insert(peer); - } + + recipient_peers.extend(mesh_peers); + } + // Gossipsub peers + None => { + tracing::debug!(topic=%topic_hash, "Topic not in the mesh"); + // If we have fanout peers add them to the map. + if self.fanout.contains_key(&topic_hash) { + for peer in self.fanout.get(&topic_hash).expect("Topic must exist") { + recipient_peers.insert(*peer); + } + } else { + // We have no fanout peers, select mesh_n of them and add them to the fanout + let mesh_n = self.config.mesh_n(); + let new_peers = + get_random_peers(&self.connected_peers, &topic_hash, mesh_n, { + |p| { + !self.explicit_peers.contains(p) + && !self + .score_below_threshold(p, |pst| pst.publish_threshold) + .0 + } + }); + // Add the new peers to the fanout and recipient peers + self.fanout.insert(topic_hash.clone(), new_peers.clone()); + for peer in new_peers { + tracing::debug!(%peer, "Peer added to fanout"); + recipient_peers.insert(peer); } - // We are publishing to fanout peers - update the time we published - self.fanout_last_pub - .insert(topic_hash.clone(), Instant::now()); } + // We are publishing to fanout peers - update the time we published + self.fanout_last_pub + .insert(topic_hash.clone(), Instant::now()); } + } - // Explicit peers - for peer in &self.explicit_peers { - if set.contains(peer) { - recipient_peers.insert(*peer); - } - } + // Explicit peers that are part of the topic + recipient_peers + .extend(peers_on_topic.filter(|peer_id| self.explicit_peers.contains(peer_id))); - // Floodsub peers - for (peer, connections) in &self.connected_peers { - if connections.kind == PeerKind::Floodsub - && !self - .score_below_threshold(peer, |ts| ts.publish_threshold) - .0 - { - recipient_peers.insert(*peer); - } + // Floodsub peers + for (peer, connections) in &self.connected_peers { + if connections.kind == PeerKind::Floodsub + && !self + .score_below_threshold(peer, |ts| ts.publish_threshold) + .0 + { + recipient_peers.insert(*peer); } } } - if recipient_peers.is_empty() { - return Err(PublishError::InsufficientPeers); - } - // If the message isn't a duplicate and we have sent it to some peers add it to the // duplicate cache and memcache. self.duplicate_cache.insert(msg_id.clone()); @@ -964,7 +975,6 @@ where if added_peers.len() < self.config.mesh_n() { // get the peers let new_peers = get_random_peers( - &self.topic_peers, &self.connected_peers, topic_hash, self.config.mesh_n() - added_peers.len(), @@ -1009,7 +1019,6 @@ where peer_id, vec![topic_hash], &self.mesh, - self.peer_topics.get(&peer_id), &mut self.events, &self.connected_peers, ); @@ -1056,7 +1065,6 @@ where // Select peers for peer exchange let peers = if do_px { get_random_peers( - &self.topic_peers, &self.connected_peers, topic_hash, self.config.prune_peers(), @@ -1107,7 +1115,6 @@ where peer, topic_hash, &self.mesh, - self.peer_topics.get(&peer), &mut self.events, &self.connected_peers, ); @@ -1118,7 +1125,7 @@ where /// Checks if the given peer is still connected and if not dials the peer again. fn check_explicit_peer_connection(&mut self, peer_id: &PeerId) { - if !self.peer_topics.contains_key(peer_id) { + if !self.connected_peers.contains_key(peer_id) { // Connect to peer tracing::debug!(peer=%peer_id, "Connecting to explicit peer"); self.events.push_back(ToSwarm::Dial { @@ -1329,17 +1336,19 @@ where let mut do_px = self.config.do_px(); + let Some(connected_peer) = self.connected_peers.get_mut(peer_id) else { + tracing::error!(peer_id = %peer_id, "Peer non-existent when handling graft"); + return; + }; + // For each topic, if a peer has grafted us, then we necessarily must be in their mesh // and they must be subscribed to the topic. Ensure we have recorded the mapping. for topic in &topics { - self.peer_topics - .entry(*peer_id) - .or_default() - .insert(topic.clone()); - self.topic_peers - .entry(topic.clone()) - .or_default() - .insert(*peer_id); + if connected_peer.topics.insert(topic.clone()) { + if let Some(m) = self.metrics.as_mut() { + m.inc_topic_peers(topic); + } + } } // we don't GRAFT to/from explicit peers; complain loudly if this happens @@ -1441,7 +1450,6 @@ where *peer_id, vec![&topic_hash], &self.mesh, - self.peer_topics.get(peer_id), &mut self.events, &self.connected_peers, ); @@ -1514,7 +1522,6 @@ where *peer_id, topic_hash, &self.mesh, - self.peer_topics.get(peer_id), &mut self.events, &self.connected_peers, ); @@ -1825,7 +1832,7 @@ where let mut unsubscribed_peers = Vec::new(); - let Some(subscribed_topics) = self.peer_topics.get_mut(propagation_source) else { + let Some(peer) = self.connected_peers.get_mut(propagation_source) else { tracing::error!( peer=%propagation_source, "Subscription by unknown peer" @@ -1841,7 +1848,7 @@ where let filtered_topics = match self .subscription_filter - .filter_incoming_subscriptions(subscriptions, subscribed_topics) + .filter_incoming_subscriptions(subscriptions, &peer.topics) { Ok(topics) => topics, Err(s) => { @@ -1857,29 +1864,24 @@ where for subscription in filtered_topics { // get the peers from the mapping, or insert empty lists if the topic doesn't exist let topic_hash = &subscription.topic_hash; - let peer_list = self.topic_peers.entry(topic_hash.clone()).or_default(); match subscription.action { SubscriptionAction::Subscribe => { - if peer_list.insert(*propagation_source) { + if peer.topics.insert(topic_hash.clone()) { tracing::debug!( peer=%propagation_source, topic=%topic_hash, "SUBSCRIPTION: Adding gossip peer to topic" ); - } - // add to the peer_topics mapping - subscribed_topics.insert(topic_hash.clone()); + if let Some(m) = self.metrics.as_mut() { + m.inc_topic_peers(topic_hash); + } + } // if the mesh needs peers add the peer to the mesh if !self.explicit_peers.contains(propagation_source) - && matches!( - self.connected_peers - .get(propagation_source) - .map(|v| &v.kind), - Some(PeerKind::Gossipsubv1_1) | Some(PeerKind::Gossipsub) - ) + && matches!(peer.kind, PeerKind::Gossipsubv1_1 | PeerKind::Gossipsub) && !Self::score_below_threshold_from_scores( &self.peer_score, propagation_source, @@ -1922,16 +1924,18 @@ where })); } SubscriptionAction::Unsubscribe => { - if peer_list.remove(propagation_source) { + if peer.topics.remove(topic_hash) { tracing::debug!( peer=%propagation_source, topic=%topic_hash, "SUBSCRIPTION: Removing gossip peer from topic" ); + + if let Some(m) = self.metrics.as_mut() { + m.dec_topic_peers(topic_hash); + } } - // remove topic from the peer_topics mapping - subscribed_topics.remove(topic_hash); unsubscribed_peers.push((*propagation_source, topic_hash.clone())); // generate an unsubscribe event to be polled application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed { @@ -1940,10 +1944,6 @@ where })); } } - - if let Some(m) = self.metrics.as_mut() { - m.set_topic_peers(topic_hash, peer_list.len()); - } } // remove unsubscribed peers from the mesh if it exists @@ -1958,7 +1958,6 @@ where *propagation_source, topics_joined, &self.mesh, - self.peer_topics.get(propagation_source), &mut self.events, &self.connected_peers, ); @@ -2039,7 +2038,6 @@ where for (topic_hash, peers) in self.mesh.iter_mut() { let explicit_peers = &self.explicit_peers; let backoffs = &self.backoffs; - let topic_peers = &self.topic_peers; let outbound_peers = &self.outbound_peers; // drop all peers with negative score, without PX @@ -2087,18 +2085,13 @@ where ); // not enough peers - get mesh_n - current_length more let desired_peers = self.config.mesh_n() - peers.len(); - let peer_list = get_random_peers( - topic_peers, - &self.connected_peers, - topic_hash, - desired_peers, - |peer| { + let peer_list = + get_random_peers(&self.connected_peers, topic_hash, desired_peers, |peer| { !peers.contains(peer) && !explicit_peers.contains(peer) && !backoffs.is_backoff_with_slack(topic_hash, peer) && *scores.get(peer).unwrap_or(&0.0) >= 0.0 - }, - ); + }); for peer in &peer_list { let current_topic = to_graft.entry(*peer).or_insert_with(Vec::new); current_topic.push(topic_hash.clone()); @@ -2154,10 +2147,9 @@ where if outbound <= self.config.mesh_outbound_min() { // do not remove anymore outbound peers continue; - } else { - // an outbound peer gets removed - outbound -= 1; } + // an outbound peer gets removed + outbound -= 1; } // remove the peer @@ -2180,19 +2172,14 @@ where // if we have not enough outbound peers, graft to some new outbound peers if outbound < self.config.mesh_outbound_min() { let needed = self.config.mesh_outbound_min() - outbound; - let peer_list = get_random_peers( - topic_peers, - &self.connected_peers, - topic_hash, - needed, - |peer| { + let peer_list = + get_random_peers(&self.connected_peers, topic_hash, needed, |peer| { !peers.contains(peer) && !explicit_peers.contains(peer) && !backoffs.is_backoff_with_slack(topic_hash, peer) && *scores.get(peer).unwrap_or(&0.0) >= 0.0 && outbound_peers.contains(peer) - }, - ); + }); for peer in &peer_list { let current_topic = to_graft.entry(*peer).or_insert_with(Vec::new); current_topic.push(topic_hash.clone()); @@ -2249,7 +2236,6 @@ where // GRAFT if median < thresholds.opportunistic_graft_threshold { let peer_list = get_random_peers( - topic_peers, &self.connected_peers, topic_hash, self.config.opportunistic_graft_peers(), @@ -2308,22 +2294,22 @@ where Some((_, thresholds, _, _)) => thresholds.publish_threshold, _ => 0.0, }; - for peer in peers.iter() { + for peer_id in peers.iter() { // is the peer still subscribed to the topic? - let peer_score = *scores.get(peer).unwrap_or(&0.0); - match self.peer_topics.get(peer) { - Some(topics) => { - if !topics.contains(topic_hash) || peer_score < publish_threshold { + let peer_score = *scores.get(peer_id).unwrap_or(&0.0); + match self.connected_peers.get(peer_id) { + Some(peer) => { + if !peer.topics.contains(topic_hash) || peer_score < publish_threshold { tracing::debug!( topic=%topic_hash, "HEARTBEAT: Peer removed from fanout for topic" ); - to_remove_peers.push(*peer); + to_remove_peers.push(*peer_id); } } None => { // remove if the peer has disconnected - to_remove_peers.push(*peer); + to_remove_peers.push(*peer_id); } } } @@ -2340,17 +2326,12 @@ where ); let needed_peers = self.config.mesh_n() - peers.len(); let explicit_peers = &self.explicit_peers; - let new_peers = get_random_peers( - &self.topic_peers, - &self.connected_peers, - topic_hash, - needed_peers, - |peer_id| { + let new_peers = + get_random_peers(&self.connected_peers, topic_hash, needed_peers, |peer_id| { !peers.contains(peer_id) && !explicit_peers.contains(peer_id) && *scores.get(peer_id).unwrap_or(&0.0) < publish_threshold - }, - ); + }); peers.extend(new_peers); } } @@ -2432,17 +2413,12 @@ where ) }; // get gossip_lazy random peers - let to_msg_peers = get_random_peers_dynamic( - &self.topic_peers, - &self.connected_peers, - topic_hash, - n_map, - |peer| { + let to_msg_peers = + get_random_peers_dynamic(&self.connected_peers, topic_hash, n_map, |peer| { !peers.contains(peer) && !self.explicit_peers.contains(peer) && !self.score_below_threshold(peer, |ts| ts.gossip_threshold).0 - }, - ); + }); tracing::debug!("Gossiping IHAVE to {} peers", to_msg_peers.len()); @@ -2492,7 +2468,6 @@ where peer, vec![topic], &self.mesh, - self.peer_topics.get(&peer), &mut self.events, &self.connected_peers, ); @@ -2543,7 +2518,6 @@ where *peer, topic_hash, &self.mesh, - self.peer_topics.get(peer), &mut self.events, &self.connected_peers, ); @@ -2577,11 +2551,11 @@ where // Add explicit peers for peer_id in &self.explicit_peers { - if let Some(topics) = self.peer_topics.get(peer_id) { + if let Some(peer) = self.connected_peers.get(peer_id) { if Some(peer_id) != propagation_source && !originating_peers.contains(peer_id) && Some(peer_id) != message.source.as_ref() - && topics.contains(&message.topic) + && peer.topics.contains(&message.topic) { recipient_peers.insert(*peer_id); } @@ -2790,6 +2764,7 @@ where .or_insert(PeerConnections { kind: PeerKind::Floodsub, connections: vec![], + topics: Default::default(), }) .connections .push(connection_id); @@ -2798,9 +2773,6 @@ where return; // Not our first connection to this peer, hence nothing to do. } - // Insert an empty set of the topics of this peer until known. - self.peer_topics.insert(peer_id, Default::default()); - if let Some((peer_score, ..)) = &mut self.peer_score { peer_score.add_peer(peer_id); } @@ -2843,28 +2815,26 @@ where if remaining_established != 0 { // Remove the connection from the list - if let Some(connections) = self.connected_peers.get_mut(&peer_id) { - let index = connections + if let Some(peer) = self.connected_peers.get_mut(&peer_id) { + let index = peer .connections .iter() .position(|v| v == &connection_id) .expect("Previously established connection to peer must be present"); - connections.connections.remove(index); + peer.connections.remove(index); // If there are more connections and this peer is in a mesh, inform the first connection // handler. - if !connections.connections.is_empty() { - if let Some(topics) = self.peer_topics.get(&peer_id) { - for topic in topics { - if let Some(mesh_peers) = self.mesh.get(topic) { - if mesh_peers.contains(&peer_id) { - self.events.push_back(ToSwarm::NotifyHandler { - peer_id, - event: HandlerIn::JoinedMesh, - handler: NotifyHandler::One(connections.connections[0]), - }); - break; - } + if !peer.connections.is_empty() { + for topic in &peer.topics { + if let Some(mesh_peers) = self.mesh.get(topic) { + if mesh_peers.contains(&peer_id) { + self.events.push_back(ToSwarm::NotifyHandler { + peer_id, + event: HandlerIn::JoinedMesh, + handler: NotifyHandler::One(peer.connections[0]), + }); + break; } } } @@ -2874,7 +2844,7 @@ where // remove from mesh, topic_peers, peer_topic and the fanout tracing::debug!(peer=%peer_id, "Peer disconnected"); { - let Some(topics) = self.peer_topics.get(&peer_id) else { + let Some(peer) = self.connected_peers.get(&peer_id) else { debug_assert!( self.blacklisted_peers.contains(&peer_id), "Disconnected node not in connected list" @@ -2883,7 +2853,7 @@ where }; // remove peer from all mappings - for topic in topics { + for topic in peer.topics.iter() { // check the mesh for the topic if let Some(mesh_peers) = self.mesh.get_mut(topic) { // check if the peer is in the mesh and remove it @@ -2895,24 +2865,8 @@ where }; } - // remove from topic_peers - if let Some(peer_list) = self.topic_peers.get_mut(topic) { - if !peer_list.remove(&peer_id) { - // debugging purposes - tracing::warn!( - peer=%peer_id, - "Disconnected node: peer not in topic_peers" - ); - } - if let Some(m) = self.metrics.as_mut() { - m.set_topic_peers(topic, peer_list.len()) - } - } else { - tracing::warn!( - peer=%peer_id, - topic=%topic, - "Disconnected node: peer with topic not in topic_peers" - ); + if let Some(m) = self.metrics.as_mut() { + m.dec_topic_peers(topic); } // remove from fanout @@ -2926,11 +2880,6 @@ where self.px_peers.remove(&peer_id); self.outbound_peers.remove(&peer_id); - // Remove peer from peer_topics and connected_peers - // NOTE: It is possible the peer has already been removed from all mappings if it does not - // support the protocol. - self.peer_topics.remove(&peer_id); - // If metrics are enabled, register the disconnection of a peer based on its protocol. if let Some(metrics) = self.metrics.as_mut() { let peer_kind = &self @@ -3190,7 +3139,6 @@ fn peer_added_to_mesh( peer_id: PeerId, new_topics: Vec<&TopicHash>, mesh: &HashMap>, - known_topics: Option<&BTreeSet>, events: &mut VecDeque>, connections: &HashMap, ) { @@ -3204,8 +3152,8 @@ fn peer_added_to_mesh( conn.connections[0] }; - if let Some(topics) = known_topics { - for topic in topics { + if let Some(peer) = connections.get(&peer_id) { + for topic in &peer.topics { if !new_topics.contains(&topic) { if let Some(mesh_peers) = mesh.get(topic) { if mesh_peers.contains(&peer_id) { @@ -3231,7 +3179,6 @@ fn peer_removed_from_mesh( peer_id: PeerId, old_topic: &TopicHash, mesh: &HashMap>, - known_topics: Option<&BTreeSet>, events: &mut VecDeque>, connections: &HashMap, ) { @@ -3243,8 +3190,8 @@ fn peer_removed_from_mesh( .first() .expect("There should be at least one connection to a peer."); - if let Some(topics) = known_topics { - for topic in topics { + if let Some(peer) = connections.get(&peer_id) { + for topic in &peer.topics { if topic != old_topic { if let Some(mesh_peers) = mesh.get(topic) { if mesh_peers.contains(&peer_id) { @@ -3267,28 +3214,19 @@ fn peer_removed_from_mesh( /// filtered by the function `f`. The number of peers to get equals the output of `n_map` /// that gets as input the number of filtered peers. fn get_random_peers_dynamic( - topic_peers: &HashMap>, connected_peers: &HashMap, topic_hash: &TopicHash, // maps the number of total peers to the number of selected peers n_map: impl Fn(usize) -> usize, mut f: impl FnMut(&PeerId) -> bool, ) -> BTreeSet { - let mut gossip_peers = match topic_peers.get(topic_hash) { - // if they exist, filter the peers by `f` - Some(peer_list) => peer_list - .iter() - .copied() - .filter(|p| { - f(p) && match connected_peers.get(p) { - Some(connections) if connections.kind == PeerKind::Gossipsub => true, - Some(connections) if connections.kind == PeerKind::Gossipsubv1_1 => true, - _ => false, - } - }) - .collect(), - None => Vec::new(), - }; + let mut gossip_peers = connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(topic_hash)) + .filter(|(peer_id, _)| f(peer_id)) + .filter(|(_, p)| p.kind == PeerKind::Gossipsub || p.kind == PeerKind::Gossipsubv1_1) + .map(|(peer_id, _)| *peer_id) + .collect::>(); // if we have less than needed, return them let n = n_map(gossip_peers.len()); @@ -3309,13 +3247,12 @@ fn get_random_peers_dynamic( /// Helper function to get a set of `n` random gossipsub peers for a `topic_hash` /// filtered by the function `f`. fn get_random_peers( - topic_peers: &HashMap>, connected_peers: &HashMap, topic_hash: &TopicHash, n: usize, f: impl FnMut(&PeerId) -> bool, ) -> BTreeSet { - get_random_peers_dynamic(topic_peers, connected_peers, topic_hash, |_| n, f) + get_random_peers_dynamic(connected_peers, topic_hash, |_| n, f) } /// Validates the combination of signing, privacy and message validation to ensure the @@ -3355,8 +3292,6 @@ impl fmt::Debug for Behaviour>(), "First peer should be subscribed to three topics" ); - let peer_topics = gs.peer_topics.get(&peers[1]).unwrap().clone(); + let peer1 = gs.connected_peers.get(&peers[1]).unwrap(); assert!( - peer_topics == topic_hashes.iter().take(3).cloned().collect(), + peer1.topics + == topic_hashes + .iter() + .take(3) + .cloned() + .collect::>(), "Second peer should be subscribed to three topics" ); assert!( - !gs.peer_topics.contains_key(&unknown_peer), + !gs.connected_peers.contains_key(&unknown_peer), "Unknown peer should not have been added" ); for topic_hash in topic_hashes[..3].iter() { - let topic_peers = gs.topic_peers.get(topic_hash).unwrap().clone(); + let topic_peers = gs + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(topic_hash)) + .map(|(peer_id, _)| *peer_id) + .collect::>(); assert!( topic_peers == peers[..2].iter().cloned().collect(), "Two peers should be added to the first three topics" @@ -894,13 +914,21 @@ fn test_handle_received_subscriptions() { &peers[0], ); - let peer_topics = gs.peer_topics.get(&peers[0]).unwrap().clone(); - assert!( - peer_topics == topic_hashes[1..3].iter().cloned().collect(), + let peer = gs.connected_peers.get(&peers[0]).unwrap().clone(); + assert_eq!( + peer.topics, + topic_hashes[1..3].iter().cloned().collect::>(), "Peer should be subscribed to two topics" ); - let topic_peers = gs.topic_peers.get(&topic_hashes[0]).unwrap().clone(); // only gossipsub at the moment + // only gossipsub at the moment + let topic_peers = gs + .connected_peers + .iter() + .filter(|(_, p)| p.topics.contains(&topic_hashes[0])) + .map(|(peer_id, _)| *peer_id) + .collect::>(); + assert!( topic_peers == peers[1..2].iter().cloned().collect(), "Only the second peers should be in the first topic" @@ -924,9 +952,8 @@ fn test_get_random_peers() { for _ in 0..20 { peers.push(PeerId::random()) } - - gs.topic_peers - .insert(topic_hash.clone(), peers.iter().cloned().collect()); + let mut topics = BTreeSet::new(); + topics.insert(topic_hash.clone()); gs.connected_peers = peers .iter() @@ -936,52 +963,32 @@ fn test_get_random_peers() { PeerConnections { kind: PeerKind::Gossipsubv1_1, connections: vec![ConnectionId::new_unchecked(0)], + topics: topics.clone(), }, ) }) .collect(); - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 5, |_| { - true - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 5, |_| true); assert_eq!(random_peers.len(), 5, "Expected 5 peers to be returned"); - let random_peers = get_random_peers( - &gs.topic_peers, - &gs.connected_peers, - &topic_hash, - 30, - |_| true, - ); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 30, |_| true); assert!(random_peers.len() == 20, "Expected 20 peers to be returned"); assert!( random_peers == peers.iter().cloned().collect(), "Expected no shuffling" ); - let random_peers = get_random_peers( - &gs.topic_peers, - &gs.connected_peers, - &topic_hash, - 20, - |_| true, - ); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 20, |_| true); assert!(random_peers.len() == 20, "Expected 20 peers to be returned"); assert!( random_peers == peers.iter().cloned().collect(), "Expected no shuffling" ); - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 0, |_| { - true - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 0, |_| true); assert!(random_peers.is_empty(), "Expected 0 peers to be returned"); // test the filter - let random_peers = - get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 5, |_| { - false - }); + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 5, |_| false); assert!(random_peers.is_empty(), "Expected 0 peers to be returned"); - let random_peers = get_random_peers(&gs.topic_peers, &gs.connected_peers, &topic_hash, 10, { + let random_peers = get_random_peers(&gs.connected_peers, &topic_hash, 10, { |peer| peers.contains(peer) }); assert!(random_peers.len() == 10, "Expected 10 peers to be returned"); diff --git a/protocols/gossipsub/src/metrics.rs b/protocols/gossipsub/src/metrics.rs index e044ca67e71..7d4acada3c7 100644 --- a/protocols/gossipsub/src/metrics.rs +++ b/protocols/gossipsub/src/metrics.rs @@ -355,12 +355,17 @@ impl Metrics { } } - /// Register how many peers do we known are subscribed to this topic. - pub(crate) fn set_topic_peers(&mut self, topic: &TopicHash, count: usize) { + /// Increase the number of peers that are subscribed to this topic. + pub(crate) fn inc_topic_peers(&mut self, topic: &TopicHash) { if self.register_topic(topic).is_ok() { - self.topic_peers_count - .get_or_create(topic) - .set(count as i64); + self.topic_peers_count.get_or_create(topic).inc(); + } + } + + /// Decrease the number of peers that are subscribed to this topic. + pub(crate) fn dec_topic_peers(&mut self, topic: &TopicHash) { + if self.register_topic(topic).is_ok() { + self.topic_peers_count.get_or_create(topic).dec(); } } diff --git a/protocols/gossipsub/src/types.rs b/protocols/gossipsub/src/types.rs index d1b92ff0ba8..a88f4822ac2 100644 --- a/protocols/gossipsub/src/types.rs +++ b/protocols/gossipsub/src/types.rs @@ -24,8 +24,8 @@ use libp2p_identity::PeerId; use libp2p_swarm::ConnectionId; use prometheus_client::encoding::EncodeLabelValue; use quick_protobuf::MessageWrite; -use std::fmt; use std::fmt::Debug; +use std::{collections::BTreeSet, fmt}; use crate::rpc_proto::proto; #[cfg(feature = "serde")] @@ -77,6 +77,8 @@ pub(crate) struct PeerConnections { pub(crate) kind: PeerKind, /// Its current connections. pub(crate) connections: Vec, + /// Subscribed topics. + pub(crate) topics: BTreeSet, } /// Describes the types of peers that can exist in the gossipsub context. From 5137e4e4e710aaf3c38c2507a4e65b76d99a4834 Mon Sep 17 00:00:00 2001 From: P1R0 Date: Tue, 3 Sep 2024 16:58:15 -0600 Subject: [PATCH 13/36] refactor(examples): use tokio instead of async-std in identify example (#5579) ## Description Following on issue #4449 refactor: use tokio instead of async-std in the identify example and remove unnecesary dependencies ## Notes & open questions ## Change checklist * Removed unnecessary dependencies on examples/identify/Cargo.toml - [x] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] A changelog entry has been made in the appropriate crates --------- Co-authored-by: David E. Perez Negron R. Co-authored-by: Darius Clark --- Cargo.lock | 3 +-- examples/identify/Cargo.toml | 5 ++--- examples/identify/src/main.rs | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c8eae458fc..5c419ed0348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2311,10 +2311,9 @@ dependencies = [ name = "identify-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/examples/identify/Cargo.toml b/examples/identify/Cargo.toml index cbe569b9d07..8d12699afa7 100644 --- a/examples/identify/Cargo.toml +++ b/examples/identify/Cargo.toml @@ -9,10 +9,9 @@ license = "MIT" release = false [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" +tokio = { version = "1.37.0", features = ["full"] } futures = { workspace = true } -libp2p = { path = "../../libp2p", features = ["async-std", "dns", "dcutr", "identify", "macros", "noise", "ping", "relay", "rendezvous", "tcp", "tokio","yamux"] } +libp2p = { path = "../../libp2p", features = ["identify", "noise", "tcp", "tokio", "yamux"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } diff --git a/examples/identify/src/main.rs b/examples/identify/src/main.rs index 916317a5a43..22474061da6 100644 --- a/examples/identify/src/main.rs +++ b/examples/identify/src/main.rs @@ -25,14 +25,14 @@ use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; -#[async_std::main] +#[tokio::main] async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); let mut swarm = libp2p::SwarmBuilder::with_new_identity() - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, From 3837e33cd4c40ae703138e6aed6f6c9d52928a80 Mon Sep 17 00:00:00 2001 From: Benno Date: Thu, 5 Sep 2024 18:05:20 +0200 Subject: [PATCH 14/36] feat(identify): add hide_listen_addrs config option (#5507) ## Description Implements #4010, which was closed. It was closed because it appeared that the Identify specification doesn't dictate this feature. But, in the discussion on the specs repo (https://github.com/libp2p/specs/pull/597) it is mentioned that this might very well be an implementation detail. This PR introduces a `hide_listen_addrs` flag that will prevent our listen addresses to be included, effectively only sharing our external addresses. ## Notes & open questions An alternative implementation would be to allow us to filter the addresses we are sending out, by providing a closure I imagine. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: Darius Clark --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/identify/CHANGELOG.md | 5 ++ protocols/identify/Cargo.toml | 2 +- protocols/identify/src/behaviour.rs | 23 ++++++++-- protocols/identify/tests/smoke.rs | 71 +++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c419ed0348..4c12e6fb984 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2874,7 +2874,7 @@ dependencies = [ [[package]] name = "libp2p-identify" -version = "0.45.0" +version = "0.45.1" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index 587bef4480a..da8d32e1a4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } libp2p-dns = { version = "0.42.0", path = "transports/dns" } libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" } -libp2p-identify = { version = "0.45.0", path = "protocols/identify" } +libp2p-identify = { version = "0.45.1", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } libp2p-kad = { version = "0.47.0", path = "protocols/kad" } libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 275f7114b28..c5778ff92ee 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.45.1 + +- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. + See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507). + ## 0.45.0 - Address translation is moved here from `libp2p-core`. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index cdc5ce587de..c3fb585c99c 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = { workspace = true } description = "Nodes identification protocol for libp2p" -version = "0.45.0" +version = "0.45.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 6590ccd6588..c8672674c1a 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -147,6 +147,12 @@ pub struct Config { /// /// Disabled by default. pub cache_size: usize, + + /// Whether to include our listen addresses in our responses. If enabled, + /// we will effectively only share our external addresses. + /// + /// Disabled by default. + pub hide_listen_addrs: bool, } impl Config { @@ -160,6 +166,7 @@ impl Config { interval: Duration::from_secs(5 * 60), push_listen_addr_updates: false, cache_size: 100, + hide_listen_addrs: false, } } @@ -189,6 +196,12 @@ impl Config { self.cache_size = cache_size; self } + + /// Configures whether we prevent sending out our listen addresses. + pub fn with_hide_listen_addrs(mut self, b: bool) -> Self { + self.hide_listen_addrs = b; + self + } } impl Behaviour { @@ -258,11 +271,11 @@ impl Behaviour { } fn all_addresses(&self) -> HashSet { - self.listen_addresses - .iter() - .chain(self.external_addresses.iter()) - .cloned() - .collect() + let mut addrs = HashSet::from_iter(self.external_addresses.iter().cloned()); + if !self.config.hide_listen_addrs { + addrs.extend(self.listen_addresses.iter().cloned()); + }; + addrs } fn emit_new_external_addr_candidate_event( diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index 49ae9f0726f..d624005408e 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -222,6 +222,77 @@ async fn emits_unique_listen_addresses() { assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); } +#[async_std::test] +async fn hides_listen_addresses() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + let mut swarm1 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("a".to_string(), identity.public()) + .with_agent_version("b".to_string()) + .with_interval(Duration::from_secs(1)) + .with_cache_size(10), + ) + }); + let mut swarm2 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("c".to_string(), identity.public()) + .with_agent_version("d".to_string()) + .with_hide_listen_addrs(true), + ) + }); + + let (_swarm2_mem_listen_addr, swarm2_tcp_listen_addr) = + swarm2.listen().with_tcp_addr_external().await; + let swarm2_peer_id = *swarm2.local_peer_id(); + swarm1.connect(&mut swarm2).await; + + async_std::task::spawn(swarm2.loop_on_next()); + + let swarm_events = futures::stream::poll_fn(|cx| swarm1.poll_next_unpin(cx)) + .take(8) + .collect::>() + .await; + + let infos = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::Behaviour(identify::Event::Received { info, .. }) => Some(info.clone()), + _ => None, + }) + .collect::>(); + + assert!( + infos.len() > 1, + "should exchange identify payload more than once" + ); + + let listen_addrs = infos + .iter() + .map(|i| i.listen_addrs.clone()) + .collect::>(); + + for addrs in listen_addrs { + assert_eq!(addrs.len(), 1); + assert!(addrs.contains(&swarm2_tcp_listen_addr)); + } + + let reported_addrs = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => { + Some((*peer_id, address.clone())) + } + _ => None, + }) + .collect::>(); + + assert_eq!(reported_addrs.len(), 1, "To have one TCP address of remote"); + assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); +} + #[async_std::test] async fn identify_push() { let _ = tracing_subscriber::fmt() From cdc9638ac1256f8a5305adb2f50a188de8874a0f Mon Sep 17 00:00:00 2001 From: Piotr Galar Date: Fri, 13 Sep 2024 19:18:30 +0200 Subject: [PATCH 15/36] chore: parameterise s3 build cache setup (#5586) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As we're setting up a new cache bucket, we'd like to be able to control its' configuration via GitHub vars/secrets fully. FYI, the secrets are not set up yet. --------- Co-authored-by: João Oliveira Co-authored-by: Guillaume Michel --- .github/workflows/docker-image.yml | 8 +------- .github/workflows/interop-test.yml | 17 +++++++++-------- scripts/build-interop-image.sh | 4 ++-- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 21863d0ed39..5cbfc20d69d 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -6,7 +6,6 @@ on: - 'master' tags: - 'libp2p-server-**' - pull_request: jobs: server: @@ -34,11 +33,6 @@ jobs: with: context: . file: ./misc/server/Dockerfile - push: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' }} # Only push image if we have the required permissions, i.e. not running from a fork - cache-from: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} - cache-to: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} + push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - env: - AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} diff --git a/.github/workflows/interop-test.yml b/.github/workflows/interop-test.yml index f3950897089..1d70ca2eaee 100644 --- a/.github/workflows/interop-test.yml +++ b/.github/workflows/interop-test.yml @@ -24,8 +24,9 @@ jobs: - name: Build ${{ matrix.flavour }} image run: ./scripts/build-interop-image.sh env: - AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + AWS_BUCKET_NAME: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + AWS_ACCESS_KEY_ID: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} FLAVOUR: ${{ matrix.flavour }} - name: Run ${{ matrix.flavour }} tests @@ -33,9 +34,9 @@ jobs: with: test-filter: ${{ matrix.flavour }}-rust-libp2p-head extra-versions: ${{ github.workspace }}/interop-tests/${{ matrix.flavour }}-ping-version.json - s3-cache-bucket: libp2p-by-tf-aws-bootstrap - s3-access-key-id: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - s3-secret-access-key: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + s3-cache-bucket: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + s3-access-key-id: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + s3-secret-access-key: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} worker-count: 16 run-holepunching-interop: name: Run hole-punch interoperability tests @@ -50,7 +51,7 @@ jobs: with: test-filter: rust-libp2p-head extra-versions: ${{ github.workspace }}/hole-punching-tests/version.json - s3-cache-bucket: libp2p-by-tf-aws-bootstrap - s3-access-key-id: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} - s3-secret-access-key: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} + s3-cache-bucket: ${{ vars.S3_LIBP2P_BUILD_CACHE_BUCKET_NAME }} + s3-access-key-id: ${{ vars.S3_LIBP2P_BUILD_CACHE_AWS_ACCESS_KEY_ID }} + s3-secret-access-key: ${{ secrets.S3_LIBP2P_BUILD_CACHE_AWS_SECRET_ACCESS_KEY }} worker-count: 16 diff --git a/scripts/build-interop-image.sh b/scripts/build-interop-image.sh index 28a8db9188d..ad6ef78b153 100755 --- a/scripts/build-interop-image.sh +++ b/scripts/build-interop-image.sh @@ -6,13 +6,13 @@ CACHE_TO="" # If we have credentials, write to cache if [[ -n "${AWS_SECRET_ACCESS_KEY}" ]]; then - CACHE_TO="--cache-to type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=${FLAVOUR}-rust-libp2p-head" + CACHE_TO="--cache-to type=s3,mode=max,bucket=${AWS_BUCKET_NAME},region=us-east-1,prefix=buildCache,name=${FLAVOUR}-rust-libp2p-head" fi docker buildx build \ --load \ $CACHE_TO \ - --cache-from type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=${FLAVOUR}-rust-libp2p-head \ + --cache-from type=s3,mode=max,bucket=${AWS_BUCKET_NAME},region=us-east-1,prefix=buildCache,name=${FLAVOUR}-rust-libp2p-head \ -t ${FLAVOUR}-rust-libp2p-head \ . \ -f interop-tests/Dockerfile.${FLAVOUR} From a2a281609a0a64b211f7917aa856924983b63200 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 14 Sep 2024 00:33:14 +0200 Subject: [PATCH 16/36] fix(autonat): reject inbound dial request from peer if its not connected (#5597) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description As discovered and described in the issue below, there are situations where an incoming AutoNAT dial can come from a non-connected peer. However `resolve_inbound_request` expects that this situation cannot occur. This PR adds a check upfront and refuses the incoming dial when no connected peer is found. Fixes https://github.com/libp2p/rust-libp2p/issues/5570. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates Co-authored-by: João Oliveira --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/autonat/CHANGELOG.md | 3 +++ protocols/autonat/Cargo.toml | 2 +- protocols/autonat/src/v1/behaviour/as_server.rs | 15 +++++++++++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c12e6fb984..b3d1cd0d76d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2687,7 +2687,7 @@ dependencies = [ [[package]] name = "libp2p-autonat" -version = "0.13.0" +version = "0.13.1" dependencies = [ "async-std", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index da8d32e1a4a..c9fe928096d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,7 @@ futures-bounded = { version = "0.2.4" } futures-rustls = { version = "0.26.0", default-features = false } libp2p = { version = "0.54.1", path = "libp2p" } libp2p-allow-block-list = { version = "0.4.1", path = "misc/allow-block-list" } -libp2p-autonat = { version = "0.13.0", path = "protocols/autonat" } +libp2p-autonat = { version = "0.13.1", path = "protocols/autonat" } libp2p-connection-limits = { version = "0.4.0", path = "misc/connection-limits" } libp2p-core = { version = "0.42.0", path = "core" } libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } diff --git a/protocols/autonat/CHANGELOG.md b/protocols/autonat/CHANGELOG.md index e171412aa58..f1aeda6ac18 100644 --- a/protocols/autonat/CHANGELOG.md +++ b/protocols/autonat/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.13.1 +- Verify that an incoming AutoNAT dial comes from a connected peer. See [PR 5597](https://github.com/libp2p/rust-libp2p/pull/5597). + ## 0.13.0 - Due to the refactor of `Transport` it's no longer required to create a seperate transport for diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index 2c01d18dceb..0c0e757641d 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-autonat" edition = "2021" rust-version = { workspace = true } description = "NAT and firewall detection for libp2p" -version = "0.13.0" +version = "0.13.1" authors = ["David Craven ", "Elena Frank ", "Hannes Furmans "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/autonat/src/v1/behaviour/as_server.rs b/protocols/autonat/src/v1/behaviour/as_server.rs index 3ecdd3ac26e..1289bd53d24 100644 --- a/protocols/autonat/src/v1/behaviour/as_server.rs +++ b/protocols/autonat/src/v1/behaviour/as_server.rs @@ -107,6 +107,21 @@ impl<'a> HandleInnerEvent for AsServer<'a> { }, } => { let probe_id = self.probe_id.next(); + if !self.connected.contains_key(&peer) { + tracing::debug!( + %peer, + "Reject inbound dial request from peer since it is not connected" + ); + + return VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe( + InboundProbeEvent::Error { + probe_id, + peer, + error: InboundProbeError::Response(ResponseError::DialRefused), + }, + ))]); + } + match self.resolve_inbound_request(peer, request) { Ok(addrs) => { tracing::debug!( From fd4e1e1e89189af938460a182ad3c2374654c7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Fri, 20 Sep 2024 22:37:32 +0100 Subject: [PATCH 17/36] chore(ci): only run interop tests on commits to master (#5604) ## Description This is done as temporary measure to unblock PR merging as the CI is currently broken Co-authored-by: Guillaume Michel --- .github/workflows/interop-test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/interop-test.yml b/.github/workflows/interop-test.yml index 1d70ca2eaee..558adcda66c 100644 --- a/.github/workflows/interop-test.yml +++ b/.github/workflows/interop-test.yml @@ -1,6 +1,5 @@ name: Interoperability Testing on: - pull_request: push: branches: - "master" From c6cf7fec6913aa590622aeea16709fce6e9c99a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 24 Sep 2024 16:00:02 +0100 Subject: [PATCH 18/36] fix(ci): address cargo-deny advisories (#5596) ## Description by updating: - `bytes` to 1.7.1, `1.6.0` was [yanked](https://crates.io/crates/bytes/1.6.0) - `quinn-proto` to 0.11.8 to address [RUSTSEC-2024-0373](https://rustsec.org/advisories/RUSTSEC-2024-0373.html) - thirtyfour-macros to 0.1.11 to remove `proc-macro-error` dependency and address [RUSTSEC-2024-0370](https://rustsec.org/advisories/RUSTSEC-2024-0370.html) --- Cargo.lock | 73 ++++++++++++------------------- examples/autonatv2/Dockerfile | 2 +- hole-punching-tests/Dockerfile | 2 +- interop-tests/Dockerfile.chromium | 2 +- interop-tests/Dockerfile.native | 2 +- misc/server/Dockerfile | 2 +- protocols/perf/Dockerfile | 2 +- 7 files changed, 33 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3d1cd0d76d..4c3498a0635 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,9 +833,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" dependencies = [ "serde", ] @@ -4584,30 +4584,6 @@ dependencies = [ "elliptic-curve", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.85" @@ -4724,7 +4700,7 @@ dependencies = [ "pin-project-lite", "quinn-proto", "quinn-udp", - "rustc-hash", + "rustc-hash 1.1.0", "rustls 0.23.11", "thiserror", "tokio", @@ -4733,14 +4709,14 @@ dependencies = [ [[package]] name = "quinn-proto" -version = "0.11.2" +version = "0.11.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e974563a4b1c2206bbc61191ca4da9c22e4308b4c455e8906751cc7828393f08" +checksum = "fadfaed2cd7f389d0161bb73eeb07b7b78f8691047a6f3e73caaeae55310a4a6" dependencies = [ "bytes", "rand 0.8.5", "ring 0.17.8", - "rustc-hash", + "rustc-hash 2.0.0", "rustls 0.23.11", "slab", "thiserror", @@ -5206,6 +5182,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hash" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" + [[package]] name = "rustc_version" version = "0.4.0" @@ -5471,18 +5453,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.203" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -5513,9 +5495,9 @@ dependencies = [ [[package]] name = "serde_repr" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", @@ -6002,30 +5984,29 @@ dependencies = [ [[package]] name = "thirtyfour-macros" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cae91d1c7c61ec65817f1064954640ee350a50ae6548ff9a1bdd2489d6ffbb0" +checksum = "b72d056365e368fc57a56d0cec9e41b02fb4a3474a61c8735262b1cfebe67425" dependencies = [ - "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.66", ] [[package]] name = "thiserror" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", @@ -6576,9 +6557,9 @@ dependencies = [ [[package]] name = "url" -version = "2.5.0" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" dependencies = [ "form_urlencoded", "idna 0.5.0", diff --git a/examples/autonatv2/Dockerfile b/examples/autonatv2/Dockerfile index 5a523649d80..6bc92e4d11b 100644 --- a/examples/autonatv2/Dockerfile +++ b/examples/autonatv2/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.75-alpine as builder +FROM rust:1.81-alpine as builder RUN apk add musl-dev diff --git a/hole-punching-tests/Dockerfile b/hole-punching-tests/Dockerfile index af00ef2272f..403cc301fc6 100644 --- a/hole-punching-tests/Dockerfile +++ b/hole-punching-tests/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.75.0 as builder +FROM rust:1.81.0 as builder # Run with access to the target cache to speed up builds WORKDIR /workspace diff --git a/interop-tests/Dockerfile.chromium b/interop-tests/Dockerfile.chromium index a6b0fc89e82..86edbc5b9d2 100644 --- a/interop-tests/Dockerfile.chromium +++ b/interop-tests/Dockerfile.chromium @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.75.0 as chef +FROM rust:1.81 as chef RUN rustup target add wasm32-unknown-unknown RUN wget -q -O- https://github.com/rustwasm/wasm-pack/releases/download/v0.12.1/wasm-pack-v0.12.1-x86_64-unknown-linux-musl.tar.gz | tar -zx -C /usr/local/bin --strip-components 1 --wildcards "wasm-pack-*/wasm-pack" RUN wget -q -O- https://github.com/WebAssembly/binaryen/releases/download/version_115/binaryen-version_115-x86_64-linux.tar.gz | tar -zx -C /usr/local/bin --strip-components 2 --wildcards "binaryen-version_*/bin/wasm-opt" diff --git a/interop-tests/Dockerfile.native b/interop-tests/Dockerfile.native index b122ac72991..499c73437fc 100644 --- a/interop-tests/Dockerfile.native +++ b/interop-tests/Dockerfile.native @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM lukemathwalker/cargo-chef:0.1.62-rust-1.75.0 as chef +FROM lukemathwalker/cargo-chef:0.1.67-rust-bullseye as chef WORKDIR /app FROM chef AS planner diff --git a/misc/server/Dockerfile b/misc/server/Dockerfile index 1583fba6bef..24ae2b9fd99 100644 --- a/misc/server/Dockerfile +++ b/misc/server/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.75.0 as chef +FROM rust:1.81.0 as chef RUN wget -q -O- https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.62/cargo-chef-x86_64-unknown-linux-gnu.tar.gz | tar -zx -C /usr/local/bin RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 WORKDIR /app diff --git a/protocols/perf/Dockerfile b/protocols/perf/Dockerfile index 1bd846cc228..f68ea6ef211 100644 --- a/protocols/perf/Dockerfile +++ b/protocols/perf/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.5-labs -FROM rust:1.75.0 as builder +FROM rust:1.81.0 as builder # Run with access to the target cache to speed up builds WORKDIR /workspace From f3e0e554821ca9233f202e80d1bae9e27cfb3ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Wed, 25 Sep 2024 10:36:01 +0100 Subject: [PATCH 19/36] chore(ci): address beta clippy lints (#5606) Co-authored-by: Darius Clark --- misc/allow-block-list/src/lib.rs | 2 ++ misc/connection-limits/src/lib.rs | 4 ++++ misc/memory-connection-limits/src/lib.rs | 2 ++ misc/memory-connection-limits/tests/util/mod.rs | 2 ++ misc/quick-protobuf-codec/src/lib.rs | 1 + .../autonat/src/v2/client/handler/dial_back.rs | 2 ++ .../autonat/src/v2/client/handler/dial_request.rs | 2 ++ protocols/autonat/src/v2/server/behaviour.rs | 2 ++ .../autonat/src/v2/server/handler/dial_request.rs | 2 ++ protocols/dcutr/src/behaviour.rs | 2 ++ protocols/dcutr/src/handler/relayed.rs | 8 ++++++++ protocols/gossipsub/src/handler.rs | 4 ++++ protocols/gossipsub/src/transform.rs | 1 + protocols/kad/src/handler.rs | 2 ++ protocols/perf/src/client/handler.rs | 4 ++++ protocols/perf/src/server/handler.rs | 8 ++++++++ protocols/ping/src/handler.rs | 2 ++ protocols/relay/src/behaviour.rs | 2 ++ protocols/relay/src/behaviour/handler.rs | 2 ++ protocols/relay/src/priv_client.rs | 2 ++ protocols/relay/src/priv_client/handler.rs | 6 ++++++ protocols/request-response/src/cbor.rs | 2 ++ protocols/request-response/src/handler.rs | 6 ++++++ protocols/stream/src/handler.rs | 4 ++++ swarm/src/behaviour/toggle.rs | 4 ++++ swarm/src/connection.rs | 10 ++++++++++ swarm/src/connection/pool/task.rs | 4 ++++ swarm/src/dummy.rs | 14 ++++++++++++++ swarm/src/handler/pending.rs | 8 ++++++++ swarm/src/upgrade.rs | 1 + swarm/tests/swarm_derive.rs | 2 ++ transports/quic/src/hole_punching.rs | 2 ++ 32 files changed, 119 insertions(+) diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index 7646638a651..56de29d1985 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -271,6 +271,8 @@ where _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/connection-limits/src/lib.rs b/misc/connection-limits/src/lib.rs index b02e52f25a1..05a9b639f26 100644 --- a/misc/connection-limits/src/lib.rs +++ b/misc/connection-limits/src/lib.rs @@ -355,6 +355,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -586,6 +588,8 @@ mod tests { _connection_id: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/memory-connection-limits/src/lib.rs b/misc/memory-connection-limits/src/lib.rs index 7b5803a61aa..757ff770487 100644 --- a/misc/memory-connection-limits/src/lib.rs +++ b/misc/memory-connection-limits/src/lib.rs @@ -190,6 +190,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/memory-connection-limits/tests/util/mod.rs b/misc/memory-connection-limits/tests/util/mod.rs index d18aa78fd22..01e8cd9f655 100644 --- a/misc/memory-connection-limits/tests/util/mod.rs +++ b/misc/memory-connection-limits/tests/util/mod.rs @@ -116,6 +116,8 @@ impl NetworkBehaviour _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index 166ee82ff08..32cee8eccac 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -12,6 +12,7 @@ mod generated; pub use generated::test as proto; /// [`Codec`] implements [`Encoder`] and [`Decoder`], uses [`unsigned_varint`] +/// /// to prefix messages with their length and uses [`quick_protobuf`] and a provided /// `struct` implementing [`MessageRead`] and [`MessageWrite`] to do the encoding. pub struct Codec { diff --git a/protocols/autonat/src/v2/client/handler/dial_back.rs b/protocols/autonat/src/v2/client/handler/dial_back.rs index b94580e69ba..98a41a82504 100644 --- a/protocols/autonat/src/v2/client/handler/dial_back.rs +++ b/protocols/autonat/src/v2/client/handler/dial_back.rs @@ -83,6 +83,8 @@ impl ConnectionHandler for Handler { tracing::warn!("Dial back request dropped, too many requests in flight"); } } + // TODO: remove when Rust 1.82 is MSRVprotocols/autonat/src/v2/client/handler/dial_back.rs + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { error, .. }) => { void::unreachable(error); } diff --git a/protocols/autonat/src/v2/client/handler/dial_request.rs b/protocols/autonat/src/v2/client/handler/dial_request.rs index 9d2df8ee6b4..85ad176ec30 100644 --- a/protocols/autonat/src/v2/client/handler/dial_request.rs +++ b/protocols/autonat/src/v2/client/handler/dial_request.rs @@ -216,6 +216,8 @@ async fn start_stream_handle( .map_err(|e| match e { StreamUpgradeError::NegotiationFailed => Error::UnsupportedProtocol, StreamUpgradeError::Timeout => Error::Io(io::ErrorKind::TimedOut.into()), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(v) => void::unreachable(v), StreamUpgradeError::Io(e) => Error::Io(e), })?; diff --git a/protocols/autonat/src/v2/server/behaviour.rs b/protocols/autonat/src/v2/server/behaviour.rs index 5f7b21d165b..9264c728fe4 100644 --- a/protocols/autonat/src/v2/server/behaviour.rs +++ b/protocols/autonat/src/v2/server/behaviour.rs @@ -112,6 +112,8 @@ where Either::Left(Either::Left(Err(e))) => { tracing::debug!("dial back error: {e:?}"); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Left(Either::Right(v)) => void::unreachable(v), Either::Right(Either::Left(cmd)) => { let addr = cmd.addr.clone(); diff --git a/protocols/autonat/src/v2/server/handler/dial_request.rs b/protocols/autonat/src/v2/server/handler/dial_request.rs index 9a3729d4ccf..14ddb153416 100644 --- a/protocols/autonat/src/v2/server/handler/dial_request.rs +++ b/protocols/autonat/src/v2/server/handler/dial_request.rs @@ -143,6 +143,8 @@ where ); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { error, .. }) => { tracing::debug!("inbound request failed: {:?}", error); } diff --git a/protocols/dcutr/src/behaviour.rs b/protocols/dcutr/src/behaviour.rs index 574c96205fa..babd56bd28e 100644 --- a/protocols/dcutr/src/behaviour.rs +++ b/protocols/dcutr/src/behaviour.rs @@ -314,6 +314,8 @@ impl NetworkBehaviour for Behaviour { .or_default() += 1; self.queued_events.push_back(ToSwarm::Dial { opts }); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(never) => void::unreachable(never), }; } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index eba58f89313..72af9fec264 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -115,6 +115,8 @@ impl Handler { self.attempts += 1; } // A connection listener denies all incoming substreams, thus none can ever be fully negotiated. + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(output) => void::unreachable(output), } } @@ -153,6 +155,8 @@ impl Handler { ::InboundProtocol, >, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(error.into_inner()); } @@ -164,6 +168,8 @@ impl Handler { >, ) { let error = match error { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(v) => void::unreachable(v), StreamUpgradeError::NegotiationFailed => outbound::Error::Unsupported, StreamUpgradeError::Io(e) => outbound::Error::Io(e), @@ -298,6 +304,8 @@ impl ConnectionHandler for Handler { ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => { self.on_fully_negotiated_outbound(fully_negotiated_outbound) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { self.on_listen_upgrade_error(listen_upgrade_error) } diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 88def13a521..8e3b3a8b022 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -493,6 +493,8 @@ impl ConnectionHandler for Handler { .. }) => match protocol { Either::Left(protocol) => handler.on_fully_negotiated_inbound(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }, ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => { @@ -504,6 +506,8 @@ impl ConnectionHandler for Handler { }) => { tracing::debug!("Dial upgrade error: Protocol negotiation timeout"); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { error: StreamUpgradeError::Apply(e), .. diff --git a/protocols/gossipsub/src/transform.rs b/protocols/gossipsub/src/transform.rs index 6f57d9fc46b..4831f9781b0 100644 --- a/protocols/gossipsub/src/transform.rs +++ b/protocols/gossipsub/src/transform.rs @@ -28,6 +28,7 @@ use crate::{Message, RawMessage, TopicHash}; /// A general trait of transforming a [`RawMessage`] into a [`Message`]. The +/// /// [`RawMessage`] is obtained from the wire and the [`Message`] is used to /// calculate the [`crate::MessageId`] of the message and is what is sent to the application. /// diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 5e7c2e21b8b..17c483da709 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -501,6 +501,8 @@ impl Handler { // is a `Void`. let protocol = match protocol { future::Either::Left(p) => p, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(p) => void::unreachable(p), }; diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 2a2c5499fc2..55fafad7fcc 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -112,6 +112,8 @@ impl ConnectionHandler for Handler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), @@ -144,6 +146,8 @@ impl ConnectionHandler for Handler { result: Err(error.into()), })); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index ddfe8f881e5..4cb535a452c 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -73,6 +73,8 @@ impl ConnectionHandler for Handler { } fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(v) } @@ -98,16 +100,22 @@ impl ConnectionHandler for Handler { tracing::warn!("Dropping inbound stream because we are at capacity"); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { info, .. }) => { void::unreachable(info) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { info, .. }) => { void::unreachable(info) } ConnectionEvent::AddressChange(_) | ConnectionEvent::LocalProtocolsChange(_) | ConnectionEvent::RemoteProtocolsChange(_) => {} + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 2816cdc4048..7b36b2d4b3d 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -210,6 +210,8 @@ impl Handler { "ping protocol negotiation timed out", )), }, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::Io(e) => Failure::Other { error: Box::new(e) }, }; diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 463febf9f2f..46419ae64e3 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -366,6 +366,8 @@ impl NetworkBehaviour for Behaviour { ) { let event = match event { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 92557287099..23e90f4b3f8 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -449,6 +449,8 @@ impl Handler { StreamUpgradeError::Timeout => outbound_stop::Error::Io(io::ErrorKind::TimedOut.into()), StreamUpgradeError::NegotiationFailed => outbound_stop::Error::Unsupported, StreamUpgradeError::Io(e) => outbound_stop::Error::Io(e), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index f8d1d9c9eb2..8bbc813ec4c 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -236,6 +236,8 @@ impl NetworkBehaviour for Behaviour { ) { let handler_event = match handler_event { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 662d63cc742..05fdd5673ae 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -445,6 +445,8 @@ impl ConnectionHandler for Handler { let _ = next.send(Ok(ev.protocol)); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(ev) => void::unreachable(ev.error), ConnectionEvent::DialUpgradeError(ev) => { if let Some(next) = self.pending_streams.pop_front() { @@ -583,6 +585,8 @@ fn into_reserve_error(e: StreamUpgradeError) -> outbound_hop::ReserveError StreamUpgradeError::Timeout => { outbound_hop::ReserveError::Io(io::ErrorKind::TimedOut.into()) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(never) => void::unreachable(never), StreamUpgradeError::NegotiationFailed => outbound_hop::ReserveError::Unsupported, StreamUpgradeError::Io(e) => outbound_hop::ReserveError::Io(e), @@ -594,6 +598,8 @@ fn into_connect_error(e: StreamUpgradeError) -> outbound_hop::ConnectError StreamUpgradeError::Timeout => { outbound_hop::ConnectError::Io(io::ErrorKind::TimedOut.into()) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(never) => void::unreachable(never), StreamUpgradeError::NegotiationFailed => outbound_hop::ConnectError::Unsupported, StreamUpgradeError::Io(e) => outbound_hop::ConnectError::Io(e), diff --git a/protocols/request-response/src/cbor.rs b/protocols/request-response/src/cbor.rs index 44d82be2630..a27d069e758 100644 --- a/protocols/request-response/src/cbor.rs +++ b/protocols/request-response/src/cbor.rs @@ -143,6 +143,8 @@ mod codec { fn decode_into_io_error(err: cbor4ii::serde::DecodeError) -> io::Error { match err { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] cbor4ii::serde::DecodeError::Core(DecodeError::Read(e)) => { io::Error::new(io::ErrorKind::Other, e) } diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index f0467593f85..0591b37dc30 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -240,6 +240,8 @@ where self.pending_events .push_back(Event::OutboundUnsupportedProtocols(message.request_id)); } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::Io(e) => { self.pending_events.push_back(Event::OutboundStreamFailed { @@ -256,6 +258,8 @@ where ::InboundProtocol, >, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(error) } } @@ -484,6 +488,8 @@ where ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { self.on_listen_upgrade_error(listen_upgrade_error) } diff --git a/protocols/stream/src/handler.rs b/protocols/stream/src/handler.rs index f63b93c1761..bf80e30c3c6 100644 --- a/protocols/stream/src/handler.rs +++ b/protocols/stream/src/handler.rs @@ -96,6 +96,8 @@ impl ConnectionHandler for Handler { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -143,6 +145,8 @@ impl ConnectionHandler for Handler { swarm::StreamUpgradeError::Timeout => { OpenStreamError::Io(io::Error::from(io::ErrorKind::TimedOut)) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] swarm::StreamUpgradeError::Apply(v) => void::unreachable(v), swarm::StreamUpgradeError::NegotiationFailed => { OpenStreamError::UnsupportedProtocol(p) diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 398c919ae86..5d72534c91e 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -210,6 +210,8 @@ where ) { let out = match out { future::Either::Left(out) => out, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] future::Either::Right(v) => void::unreachable(v), }; @@ -251,6 +253,8 @@ where let err = match err { Either::Left(e) => e, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right(v) => void::unreachable(v), }; diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 603a5b0d7c4..2f9afc38418 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -1189,10 +1189,14 @@ mod tests { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, .. @@ -1200,6 +1204,8 @@ mod tests { ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => { self.error = Some(error) } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) | ConnectionEvent::LocalProtocolsChange(_) @@ -1208,6 +1214,8 @@ mod tests { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -1283,6 +1291,8 @@ mod tests { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index 08674fd2ee5..13977a17b85 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -105,6 +105,8 @@ pub(crate) async fn new_for_pending_outgoing_connection( }) .await; } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Left((Ok(v), _)) => void::unreachable(v), Either::Right((Ok((address, output, errors)), _)) => { let _ = events @@ -143,6 +145,8 @@ pub(crate) async fn new_for_pending_incoming_connection( }) .await; } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Left((Ok(v), _)) => void::unreachable(v), Either::Right((Ok(output), _)) => { let _ = events diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 6e1b4d56eb9..0bd8c06862d 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -49,6 +49,8 @@ impl NetworkBehaviour for Behaviour { _: ConnectionId, event: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -76,6 +78,8 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { } fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(event) } @@ -98,19 +102,29 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] StreamUpgradeError::Timeout => unreachable!(), StreamUpgradeError::Apply(e) => void::unreachable(e), StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => { unreachable!("Denied upgrade does not support any protocols") } }, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) | ConnectionEvent::LocalProtocolsChange(_) diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 23b9adcfd90..9601f5cf78b 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -52,6 +52,8 @@ impl ConnectionHandler for PendingConnectionHandler { } fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(v) } @@ -74,9 +76,13 @@ impl ConnectionHandler for PendingConnectionHandler { >, ) { match event { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, .. }) => void::unreachable(protocol), + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, info: _info, @@ -87,6 +93,8 @@ impl ConnectionHandler for PendingConnectionHandler { void::unreachable(_info); } } + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] ConnectionEvent::AddressChange(_) | ConnectionEvent::DialUpgradeError(_) | ConnectionEvent::ListenUpgradeError(_) diff --git a/swarm/src/upgrade.rs b/swarm/src/upgrade.rs index 53b627458c9..f6c6648a373 100644 --- a/swarm/src/upgrade.rs +++ b/swarm/src/upgrade.rs @@ -121,6 +121,7 @@ where } /// Wraps around a type that implements [`OutboundUpgradeSend`], [`InboundUpgradeSend`], or +/// /// both, and implements [`OutboundUpgrade`](upgrade::OutboundUpgrade) and/or /// [`InboundUpgrade`](upgrade::InboundUpgrade). /// diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 919ed0cab7f..667f68408cf 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -577,6 +577,8 @@ fn custom_out_event_no_type_parameters() { _connection: ConnectionId, message: THandlerOutEvent, ) { + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] void::unreachable(message); } diff --git a/transports/quic/src/hole_punching.rs b/transports/quic/src/hole_punching.rs index 605799af5e1..a38d123a6a4 100644 --- a/transports/quic/src/hole_punching.rs +++ b/transports/quic/src/hole_punching.rs @@ -20,6 +20,8 @@ pub(crate) async fn hole_puncher( match futures::future::select(P::sleep(timeout_duration), punch_holes_future).await { Either::Left(_) => Error::HandshakeTimedOut, Either::Right((Err(hole_punch_err), _)) => hole_punch_err, + // TODO: remove when Rust 1.82 is MSRV + #[allow(unreachable_patterns)] Either::Right((Ok(never), _)) => match never {}, } } From 8ceadaac5aec4b462463ef4082d6af577a3158b1 Mon Sep 17 00:00:00 2001 From: stormshield-frb <144998884+stormshield-frb@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:17:47 +0200 Subject: [PATCH 20/36] fix(swarm): don't report `NewExternalAddrCandidate` if already confirmed (#5582) ## Description Currently, `NewExternalAddrCandidate` events are emitted for every connections. However, we continue to get this event even when `autonat` has already confirmed that this address is external. So we should not continue to advertise the "candidate" event. ## Notes & open questions We have made the changes in the `swarm` instead of `identify` because it does not make it necessary to duplicate the `ConfirmedExternalAddr` vector in the `identify` Behaviour. Moreover, if any future Behaviour emit `NewExternalAddrCandidate`, the same rule will also be applied. I had to edit the `autonat_v2` tests which were always expecting a `NewExternalAddrCandidate` but the address was already confirmed. ## Change checklist - [x] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: Darius Clark Co-authored-by: Guillaume Michel --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/autonat/tests/autonatv2.rs | 38 +++++++++++++--------------- swarm/CHANGELOG.md | 5 ++++ swarm/Cargo.toml | 2 +- swarm/src/lib.rs | 14 +++++----- 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c3498a0635..79eb2eb4ad5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3329,7 +3329,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.45.1" +version = "0.45.2" dependencies = [ "async-std", "criterion", diff --git a/Cargo.toml b/Cargo.toml index c9fe928096d..32f72c5e252 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,7 @@ libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" } libp2p-server = { version = "0.12.7", path = "misc/server" } libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" } -libp2p-swarm = { version = "0.45.1", path = "swarm" } +libp2p-swarm = { version = "0.45.2", path = "swarm" } libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. libp2p-swarm-test = { version = "0.4.0", path = "swarm-test" } libp2p-tcp = { version = "0.42.0", path = "transports/tcp" } diff --git a/protocols/autonat/tests/autonatv2.rs b/protocols/autonat/tests/autonatv2.rs index abd0c4bd8eb..3d792172445 100644 --- a/protocols/autonat/tests/autonatv2.rs +++ b/protocols/autonat/tests/autonatv2.rs @@ -1,5 +1,6 @@ use libp2p_autonat::v2::client::{self, Config}; use libp2p_autonat::v2::server; +use libp2p_core::multiaddr::Protocol; use libp2p_core::transport::TransportError; use libp2p_core::Multiaddr; use libp2p_swarm::{ @@ -21,17 +22,10 @@ async fn confirm_successful() { let cor_server_peer = *alice.local_peer_id(); let cor_client_peer = *bob.local_peer_id(); - let bob_external_addrs = Arc::new(bob.external_addresses().cloned().collect::>()); - let alice_bob_external_addrs = bob_external_addrs.clone(); + let bob_tcp_listeners = Arc::new(tcp_listeners(&bob)); + let alice_bob_tcp_listeners = bob_tcp_listeners.clone(); let alice_task = async { - let _ = alice - .wait(|event| match event { - SwarmEvent::NewExternalAddrCandidate { .. } => Some(()), - _ => None, - }) - .await; - let (dialed_peer_id, dialed_connection_id) = alice .wait(|event| match event { SwarmEvent::Dialing { @@ -76,10 +70,10 @@ async fn confirm_successful() { }) .await; - assert_eq!(tested_addr, bob_external_addrs.first().cloned().unwrap()); + assert_eq!(tested_addr, bob_tcp_listeners.first().cloned().unwrap()); assert_eq!(data_amount, 0); assert_eq!(client, cor_client_peer); - assert_eq!(&all_addrs[..], &bob_external_addrs[..]); + assert_eq!(&all_addrs[..], &bob_tcp_listeners[..]); assert!(result.is_ok(), "Result: {result:?}"); }; @@ -122,7 +116,7 @@ async fn confirm_successful() { .await; assert_eq!( tested_addr, - alice_bob_external_addrs.first().cloned().unwrap() + alice_bob_tcp_listeners.first().cloned().unwrap() ); assert_eq!(bytes_sent, 0); assert_eq!(server, cor_server_peer); @@ -446,7 +440,7 @@ async fn new_client() -> Swarm { identity.public().clone(), )), }); - node.listen().with_tcp_addr_external().await; + node.listen().await; node } @@ -490,13 +484,6 @@ async fn bootstrap() -> (Swarm, Swarm) { let cor_client_peer = *bob.local_peer_id(); let alice_task = async { - let _ = alice - .wait(|event| match event { - SwarmEvent::NewExternalAddrCandidate { .. } => Some(()), - _ => None, - }) - .await; - let (dialed_peer_id, dialed_connection_id) = alice .wait(|event| match event { SwarmEvent::Dialing { @@ -566,3 +553,14 @@ async fn bootstrap() -> (Swarm, Swarm) { tokio::join!(alice_task, bob_task); (alice, bob) } + +fn tcp_listeners(swarm: &Swarm) -> Vec { + swarm + .listeners() + .filter(|addr| { + addr.iter() + .any(|protocol| matches!(protocol, Protocol::Tcp(_))) + }) + .cloned() + .collect::>() +} diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index e7931a60de2..c5d10872d40 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.45.2 + +- Don't report `NewExternalAddrCandidate` for confirmed external addresses. + See [PR 5582](https://github.com/libp2p/rust-libp2p/pull/5582). + ## 0.45.1 - Update `libp2p-swarm-derive` to version `0.35.0`, see [PR 5545] diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 3d0b1a84eee..cdee67f3fb3 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = { workspace = true } description = "The libp2p swarm" -version = "0.45.1" +version = "0.45.2" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 81b1ca1a68d..12280e99f07 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1140,12 +1140,14 @@ where self.pending_handler_event = Some((peer_id, handler, event)); } ToSwarm::NewExternalAddrCandidate(addr) => { - self.behaviour - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr: &addr }, - )); - self.pending_swarm_events - .push_back(SwarmEvent::NewExternalAddrCandidate { address: addr }); + if !self.confirmed_external_addr.contains(&addr) { + self.behaviour + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { addr: &addr }, + )); + self.pending_swarm_events + .push_back(SwarmEvent::NewExternalAddrCandidate { address: addr }); + } } ToSwarm::ExternalAddrConfirmed(addr) => { self.add_external_address(addr.clone()); From 89d78dda296fc3747d3760f73d4b6c558e34c8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Fri, 4 Oct 2024 14:35:08 +0100 Subject: [PATCH 21/36] chore(autonat-v2): fix dial_back_to_non_libp2p test (#5621) ## Description By avoiding dialing an external address Co-authored-by: Guillaume Michel --- protocols/autonat/tests/autonatv2.rs | 147 +++++++++++++-------------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/protocols/autonat/tests/autonatv2.rs b/protocols/autonat/tests/autonatv2.rs index 3d792172445..f22a2e51470 100644 --- a/protocols/autonat/tests/autonatv2.rs +++ b/protocols/autonat/tests/autonatv2.rs @@ -232,87 +232,84 @@ async fn dial_back_to_non_libp2p() { let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); - for addr_str in ["/ip4/169.150.247.38/tcp/32", "/ip6/::1/tcp/1000"] { - let addr: Multiaddr = addr_str.parse().unwrap(); - let bob_addr = addr.clone(); - bob.behaviour_mut() - .autonat - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr: &addr }, - )); - - let alice_task = async { - let (alice_dialing_peer, alice_conn_id) = alice - .wait(|event| match event { - SwarmEvent::Dialing { - peer_id, - connection_id, - } => peer_id.map(|p| (p, connection_id)), - _ => None, - }) - .await; - let mut outgoing_conn_error = alice - .wait(|event| match event { - SwarmEvent::OutgoingConnectionError { - connection_id, - peer_id: Some(peer_id), - error: DialError::Transport(peers), - } if connection_id == alice_conn_id && peer_id == alice_dialing_peer => { - Some(peers) - } - _ => None, - }) - .await; - - if let Some((multiaddr, TransportError::Other(o))) = outgoing_conn_error.pop() { - assert_eq!( - multiaddr, - addr.clone().with_p2p(alice_dialing_peer).unwrap() - ); - let error_string = o.to_string(); - assert!( - error_string.contains("Connection refused"), - "Correct error string: {error_string} for {addr_str}" - ); - } else { - panic!("No outgoing connection errors"); - } + let addr_str = "/ip6/::1/tcp/1000"; + let addr: Multiaddr = addr_str.parse().unwrap(); + let bob_addr = addr.clone(); + bob.behaviour_mut() + .autonat + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { addr: &addr }, + )); - alice - .wait(|event| match event { - SwarmEvent::Behaviour(CombinedServerEvent::Autonat(server::Event { - all_addrs, - tested_addr, - client, - data_amount, - result: Ok(()), - })) if all_addrs == vec![addr.clone()] - && tested_addr == addr - && alice_dialing_peer == client => - { - Some(data_amount) - } - _ => None, - }) - .await - }; - let bob_task = async { - bob.wait(|event| match event { - SwarmEvent::Behaviour(CombinedClientEvent::Autonat(client::Event { + let alice_task = async { + let (alice_dialing_peer, alice_conn_id) = alice + .wait(|event| match event { + SwarmEvent::Dialing { + peer_id, + connection_id, + } => peer_id.map(|p| (p, connection_id)), + _ => None, + }) + .await; + let mut outgoing_conn_error = alice + .wait(|event| match event { + SwarmEvent::OutgoingConnectionError { + connection_id, + peer_id: Some(peer_id), + error: DialError::Transport(peers), + } if connection_id == alice_conn_id && peer_id == alice_dialing_peer => Some(peers), + _ => None, + }) + .await; + + if let Some((multiaddr, TransportError::Other(o))) = outgoing_conn_error.pop() { + assert_eq!( + multiaddr, + addr.clone().with_p2p(alice_dialing_peer).unwrap() + ); + let error_string = o.to_string(); + assert!( + error_string.contains("Connection refused"), + "Correct error string: {error_string} for {addr_str}" + ); + } else { + panic!("No outgoing connection errors"); + } + + alice + .wait(|event| match event { + SwarmEvent::Behaviour(CombinedServerEvent::Autonat(server::Event { + all_addrs, tested_addr, - bytes_sent, - server, - result: Err(_), - })) if tested_addr == bob_addr && server == alice_peer_id => Some(bytes_sent), + client, + data_amount, + result: Ok(()), + })) if all_addrs == vec![addr.clone()] + && tested_addr == addr + && alice_dialing_peer == client => + { + Some(data_amount) + } _ => None, }) .await - }; + }; + let bob_task = async { + bob.wait(|event| match event { + SwarmEvent::Behaviour(CombinedClientEvent::Autonat(client::Event { + tested_addr, + bytes_sent, + server, + result: Err(_), + })) if tested_addr == bob_addr && server == alice_peer_id => Some(bytes_sent), + _ => None, + }) + .await + }; - let (alice_bytes_sent, bob_bytes_sent) = tokio::join!(alice_task, bob_task); - assert_eq!(alice_bytes_sent, bob_bytes_sent); - bob.behaviour_mut().autonat.validate_addr(&addr); - } + let (alice_bytes_sent, bob_bytes_sent) = tokio::join!(alice_task, bob_task); + assert_eq!(alice_bytes_sent, bob_bytes_sent); + bob.behaviour_mut().autonat.validate_addr(&addr); } #[tokio::test] From b83dd95c384fefc0aca489f5ffefa74c72db0d9d Mon Sep 17 00:00:00 2001 From: Guillaume Michel Date: Fri, 4 Oct 2024 15:50:57 +0200 Subject: [PATCH 22/36] chore: update interop test run condition (#5611) ## Description Follow up to https://github.com/libp2p/rust-libp2p/pull/5604. Interop tests only work on the main `rust-libp2p` repo, and not on forks, because of the S3 cache (introduced in https://github.com/libp2p/rust-libp2p/pull/5586). The interop tests currently don't run in the PRs, but they run after the PRs are merged to `master`. This PR is trying to run interop tests in PR that are branches of the main repo (not forks). --- .github/workflows/interop-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/interop-test.yml b/.github/workflows/interop-test.yml index 558adcda66c..57d0f1a692d 100644 --- a/.github/workflows/interop-test.yml +++ b/.github/workflows/interop-test.yml @@ -11,6 +11,7 @@ concurrency: jobs: run-transport-interop: name: Run transport interoperability tests + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository runs-on: ${{ fromJSON(github.repository == 'libp2p/rust-libp2p' && '["self-hosted", "linux", "x64", "4xlarge"]' || '"ubuntu-latest"') }} strategy: matrix: @@ -39,6 +40,7 @@ jobs: worker-count: 16 run-holepunching-interop: name: Run hole-punch interoperability tests + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository runs-on: ${{ fromJSON(github.repository == 'libp2p/rust-libp2p' && '["self-hosted", "linux", "x64", "4xlarge"]' || '"ubuntu-latest"') }} steps: - uses: actions/checkout@v4 From 7669b1696bf62fa82e2a1da959cf537dc98959ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Fri, 4 Oct 2024 15:45:16 +0100 Subject: [PATCH 23/36] deps: update metrics example dependencies (#5617) ## Description and address cargo-deny [RUSTSEC-2024-0376](https://rustsec.org/advisories/RUSTSEC-2024-0376.html) --- Cargo.lock | 196 +++++++++++++---------------------- examples/metrics/Cargo.toml | 8 +- examples/metrics/src/main.rs | 8 +- 3 files changed, 80 insertions(+), 132 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79eb2eb4ad5..38c3436237a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -545,34 +545,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "axum" -version = "0.6.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" -dependencies = [ - "async-trait", - "axum-core 0.3.4", - "bitflags 1.3.2", - "bytes", - "futures-util", - "http 0.2.9", - "http-body 0.4.5", - "hyper 0.14.27", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "sync_wrapper 0.1.2", - "tower", - "tower-layer", - "tower-service", -] - [[package]] name = "axum" version = "0.7.5" @@ -580,13 +552,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" dependencies = [ "async-trait", - "axum-core 0.4.3", + "axum-core", "bytes", "futures-util", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "http-body-util", - "hyper 1.1.0", + "hyper 1.4.1", "hyper-util", "itoa", "matchit", @@ -607,23 +579,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "axum-core" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http 0.2.9", - "http-body 0.4.5", - "mime", - "rustversion", - "tower-layer", - "tower-service", -] - [[package]] name = "axum-core" version = "0.4.3" @@ -633,7 +588,7 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "http-body-util", "mime", @@ -779,7 +734,7 @@ name = "browser-webrtc-example" version = "0.1.0" dependencies = [ "anyhow", - "axum 0.7.5", + "axum", "futures", "js-sys", "libp2p", @@ -1940,7 +1895,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http 1.0.0", + "http 1.1.0", "indexmap 2.2.1", "slab", "tokio", @@ -2131,9 +2086,9 @@ dependencies = [ [[package]] name = "http" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", "fnv", @@ -2158,7 +2113,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http 1.0.0", + "http 1.1.0", ] [[package]] @@ -2169,7 +2124,7 @@ checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", "futures-util", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "pin-project-lite", ] @@ -2224,20 +2179,21 @@ dependencies = [ [[package]] name = "hyper" -version = "1.1.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5aa53871fc917b1a9ed87b683a5d86db645e23acb32c2e0785a353e522fb75" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" dependencies = [ "bytes", "futures-channel", "futures-util", "h2 0.4.4", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "httparse", "httpdate", "itoa", "pin-project-lite", + "smallvec", "tokio", "want", ] @@ -2249,8 +2205,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", - "http 1.0.0", - "hyper 1.1.0", + "http 1.1.0", + "hyper 1.4.1", "hyper-util", "rustls 0.22.4", "rustls-pki-types", @@ -2261,14 +2217,15 @@ dependencies = [ [[package]] name = "hyper-timeout" -version = "0.4.1" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" +checksum = "3203a961e5c83b6f5498933e78b6b263e208c197b63e9c6c53cc82ffd3f63793" dependencies = [ - "hyper 0.14.27", + "hyper 1.4.1", + "hyper-util", "pin-project-lite", "tokio", - "tokio-io-timeout", + "tower-service", ] [[package]] @@ -2279,7 +2236,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", "http-body-util", - "hyper 1.1.0", + "hyper 1.4.1", "hyper-util", "native-tls", "tokio", @@ -2289,20 +2246,19 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.3" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", - "hyper 1.1.0", + "hyper 1.4.1", "pin-project-lite", "socket2 0.5.7", "tokio", - "tower", "tower-service", "tracing", ] @@ -2456,7 +2412,7 @@ name = "interop-tests" version = "0.1.0" dependencies = [ "anyhow", - "axum 0.7.5", + "axum", "console_error_panic_hook", "either", "futures", @@ -3295,7 +3251,7 @@ dependencies = [ name = "libp2p-server" version = "0.12.7" dependencies = [ - "axum 0.7.5", + "axum", "base64 0.22.1", "clap", "futures", @@ -3774,16 +3730,16 @@ dependencies = [ name = "metrics-example" version = "0.1.0" dependencies = [ - "axum 0.7.5", + "axum", "futures", "libp2p", - "opentelemetry 0.23.0", + "opentelemetry 0.25.0", "opentelemetry-otlp", - "opentelemetry_sdk 0.23.0", + "opentelemetry_sdk 0.25.0", "prometheus-client", "tokio", "tracing", - "tracing-opentelemetry 0.24.0", + "tracing-opentelemetry 0.26.0", "tracing-subscriber", ] @@ -4198,9 +4154,9 @@ dependencies = [ [[package]] name = "opentelemetry" -version = "0.23.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b69a91d4893e713e06f724597ad630f1fa76057a5e1026c0ca67054a9032a76" +checksum = "803801d3d3b71cd026851a53f974ea03df3d179cb758b260136a6c9e22e196af" dependencies = [ "futures-core", "futures-sink", @@ -4228,16 +4184,16 @@ dependencies = [ [[package]] name = "opentelemetry-otlp" -version = "0.16.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a94c69209c05319cdf7460c6d4c055ed102be242a0a6245835d7bc42c6ec7f54" +checksum = "596b1719b3cab83addb20bcbffdf21575279d9436d9ccccfe651a3bf0ab5ab06" dependencies = [ "async-trait", "futures-core", - "http 0.2.9", - "opentelemetry 0.23.0", + "http 1.1.0", + "opentelemetry 0.25.0", "opentelemetry-proto", - "opentelemetry_sdk 0.23.0", + "opentelemetry_sdk 0.25.0", "prost", "thiserror", "tokio", @@ -4246,12 +4202,12 @@ dependencies = [ [[package]] name = "opentelemetry-proto" -version = "0.6.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "984806e6cf27f2b49282e2a05e288f30594f3dbc74eb7a6e99422bc48ed78162" +checksum = "2c43620e8f93359eb7e627a3b16ee92d8585774986f24f2ab010817426c5ce61" dependencies = [ - "opentelemetry 0.23.0", - "opentelemetry_sdk 0.23.0", + "opentelemetry 0.25.0", + "opentelemetry_sdk 0.25.0", "prost", "tonic", ] @@ -4289,21 +4245,20 @@ dependencies = [ [[package]] name = "opentelemetry_sdk" -version = "0.23.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae312d58eaa90a82d2e627fd86e075cf5230b3f11794e2ed74199ebbe572d4fd" +checksum = "e0da0d6b47a3dbc6e9c9e36a0520e25cf943e046843818faaa3f87365a548c82" dependencies = [ "async-trait", "futures-channel", "futures-executor", "futures-util", "glob", - "lazy_static", "once_cell", - "opentelemetry 0.23.0", - "ordered-float 4.2.0", + "opentelemetry 0.25.0", "percent-encoding", "rand 0.8.5", + "serde_json", "thiserror", "tokio", "tokio-stream", @@ -4618,9 +4573,9 @@ dependencies = [ [[package]] name = "prost" -version = "0.12.3" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +checksum = "7b0487d90e047de87f984913713b85c601c05609aad5b0df4b4573fbf69aa13f" dependencies = [ "bytes", "prost-derive", @@ -4628,9 +4583,9 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.12.3" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", "itertools", @@ -4978,10 +4933,10 @@ dependencies = [ "futures-core", "futures-util", "h2 0.4.4", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "http-body-util", - "hyper 1.1.0", + "hyper 1.4.1", "hyper-rustls", "hyper-tls", "hyper-util", @@ -5965,7 +5920,7 @@ dependencies = [ "async-trait", "base64 0.22.1", "futures", - "http 1.0.0", + "http 1.1.0", "indexmap 2.2.1", "parking_lot", "paste", @@ -6120,16 +6075,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "tokio-io-timeout" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" -dependencies = [ - "pin-project-lite", - "tokio", -] - [[package]] name = "tokio-macros" version = "2.3.0" @@ -6164,9 +6109,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.14" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" dependencies = [ "futures-core", "pin-project-lite", @@ -6223,23 +6168,26 @@ dependencies = [ [[package]] name = "tonic" -version = "0.11.0" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76c4eb7a4e9ef9d4763600161f12f5070b92a578e1b634db88a6887844c91a13" +checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ "async-stream", "async-trait", - "axum 0.6.20", - "base64 0.21.7", + "axum", + "base64 0.22.1", "bytes", - "h2 0.3.26", - "http 0.2.9", - "http-body 0.4.5", - "hyper 0.14.27", + "h2 0.4.4", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.4.1", "hyper-timeout", + "hyper-util", "percent-encoding", "pin-project", "prost", + "socket2 0.5.7", "tokio", "tokio-stream", "tower", @@ -6277,7 +6225,7 @@ dependencies = [ "bitflags 2.4.1", "bytes", "futures-util", - "http 1.0.0", + "http 1.1.0", "http-body 1.0.0", "http-body-util", "http-range-header", @@ -6369,14 +6317,14 @@ dependencies = [ [[package]] name = "tracing-opentelemetry" -version = "0.24.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f68803492bf28ab40aeccaecc7021096bd256baf7ca77c3d425d89b35a7be4e4" +checksum = "5eabc56d23707ad55ba2a0750fc24767125d5a0f51993ba41ad2c441cc7b8dea" dependencies = [ "js-sys", "once_cell", - "opentelemetry 0.23.0", - "opentelemetry_sdk 0.23.0", + "opentelemetry 0.25.0", + "opentelemetry_sdk 0.25.0", "smallvec", "tracing", "tracing-core", diff --git a/examples/metrics/Cargo.toml b/examples/metrics/Cargo.toml index 2b82668f52a..129b1abb1f3 100644 --- a/examples/metrics/Cargo.toml +++ b/examples/metrics/Cargo.toml @@ -12,13 +12,13 @@ release = false futures = { workspace = true } axum = "0.7" libp2p = { path = "../../libp2p", features = ["tokio", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] } -opentelemetry = { version = "0.23.0", features = ["metrics"] } -opentelemetry-otlp = { version = "0.16.0", features = ["metrics"] } -opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio", "metrics"] } +opentelemetry = { version = "0.25.0", features = ["metrics"] } +opentelemetry-otlp = { version = "0.25.0", features = ["metrics"] } +opentelemetry_sdk = { version = "0.25.0", features = ["rt-tokio", "metrics"] } prometheus-client = { workspace = true } tokio = { workspace = true, features = ["full"] } tracing = { workspace = true } -tracing-opentelemetry = "0.24.0" +tracing-opentelemetry = "0.26.0" tracing-subscriber = { workspace = true, features = ["env-filter"] } [lints] diff --git a/examples/metrics/src/main.rs b/examples/metrics/src/main.rs index 99a9ca66aaf..1755c769053 100644 --- a/examples/metrics/src/main.rs +++ b/examples/metrics/src/main.rs @@ -25,7 +25,7 @@ use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p::{identify, identity, noise, ping, tcp, yamux}; -use opentelemetry::KeyValue; +use opentelemetry::{trace::TracerProvider, KeyValue}; use prometheus_client::registry::Registry; use std::error::Error; use std::time::Duration; @@ -90,7 +90,7 @@ async fn main() -> Result<(), Box> { } fn setup_tracing() -> Result<(), Box> { - let tracer = opentelemetry_otlp::new_pipeline() + let provider = opentelemetry_otlp::new_pipeline() .tracing() .with_exporter(opentelemetry_otlp::new_exporter().tonic()) .with_trace_config(opentelemetry_sdk::trace::Config::default().with_resource( @@ -102,10 +102,10 @@ fn setup_tracing() -> Result<(), Box> { .with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env())) .with( tracing_opentelemetry::layer() - .with_tracer(tracer) + .with_tracer(provider.tracer("libp2p-subscriber")) .with_filter(EnvFilter::from_default_env()), ) - .try_init()?; + .init(); Ok(()) } From fcff3f80401895362060f8e448ec992b6db7fb9b Mon Sep 17 00:00:00 2001 From: P1R0 Date: Fri, 4 Oct 2024 16:40:47 -0600 Subject: [PATCH 24/36] refactor(examples): use tokio instead of async-std in relay-server (#5600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Following on issue #4449 refactor: use tokio instead of async-std in the relay-servert example and remove unnecessary dependencies ## Notes & open questions Fails on testing with the [whole punch tutorial](https://docs.rs/libp2p/0.54.1/libp2p/tutorials/hole_punching/index.html) possibly because of my networking topology. connection established event registered. I will publish the logs and testing information as a comment ## Change checklist * Removed unnecessary dependencies on examples/relay-server/Cargo.toml * Updated tokio version to "1.37.0" - [ ] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] A changelog entry has been made in the appropriate crates --------- Co-authored-by: David E. Perez Negron R. Co-authored-by: Guillaume Michel Co-authored-by: João Oliveira --- Cargo.lock | 3 +-- examples/relay-server/Cargo.toml | 5 ++-- examples/relay-server/src/main.rs | 42 +++++++++++++++---------------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38c3436237a..91767968898 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4901,11 +4901,10 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" name = "relay-server-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "clap", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/examples/relay-server/Cargo.toml b/examples/relay-server/Cargo.toml index 12d3e2467ce..7385cf6c033 100644 --- a/examples/relay-server/Cargo.toml +++ b/examples/relay-server/Cargo.toml @@ -10,10 +10,9 @@ release = false [dependencies] clap = { version = "4.5.6", features = ["derive"] } -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" +tokio = { version = "1.37.0", features = ["full"] } futures = { workspace = true } -libp2p = { path = "../../libp2p", features = [ "async-std", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } +libp2p = { path = "../../libp2p", features = ["tokio", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index bf5817454f8..46a122d0717 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -22,8 +22,7 @@ #![doc = include_str!("../README.md")] use clap::Parser; -use futures::executor::block_on; -use futures::stream::StreamExt; +use futures::StreamExt; use libp2p::{ core::multiaddr::Protocol, core::Multiaddr, @@ -35,7 +34,8 @@ use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; use tracing_subscriber::EnvFilter; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); @@ -46,7 +46,7 @@ fn main() -> Result<(), Box> { let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed); let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key) - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, @@ -81,27 +81,25 @@ fn main() -> Result<(), Box> { .with(Protocol::QuicV1); swarm.listen_on(listen_addr_quic)?; - block_on(async { - loop { - match swarm.next().await.expect("Infinite Stream.") { - SwarmEvent::Behaviour(event) => { - if let BehaviourEvent::Identify(identify::Event::Received { - info: identify::Info { observed_addr, .. }, - .. - }) = &event - { - swarm.add_external_address(observed_addr.clone()); - } - - println!("{event:?}") + loop { + match swarm.next().await.expect("Infinite Stream.") { + SwarmEvent::Behaviour(event) => { + if let BehaviourEvent::Identify(identify::Event::Received { + info: identify::Info { observed_addr, .. }, + .. + }) = &event + { + swarm.add_external_address(observed_addr.clone()); } - SwarmEvent::NewListenAddr { address, .. } => { - println!("Listening on {address:?}"); - } - _ => {} + + println!("{event:?}") + } + SwarmEvent::NewListenAddr { address, .. } => { + println!("Listening on {address:?}"); } + _ => {} } - }) + } } #[derive(NetworkBehaviour)] From 93ad28c4cce236b1657241ba623ea30077565afd Mon Sep 17 00:00:00 2001 From: Guillaume Michel Date: Sat, 5 Oct 2024 01:07:49 +0200 Subject: [PATCH 25/36] fix(server): removing dependency on libp2p-lookup (#5610) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Remove dependency on [`libp2p-lookup`](https://github.com/mxinden/libp2p-lookup) which is no longer maintained, and [makes checks fail](https://github.com/libp2p/rust-libp2p/actions/runs/11016492372/job/30628121728). ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: João Oliveira --- Cargo.lock | 2 +- Cargo.toml | 8 ++++++-- misc/server/CHANGELOG.md | 30 ++++++++++++++++++++++++++++++ misc/server/Cargo.toml | 20 ++++++++++++++++++-- misc/server/Dockerfile | 2 -- misc/server/README.md | 7 ------- 6 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91767968898..28107c00ee9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3249,7 +3249,7 @@ dependencies = [ [[package]] name = "libp2p-server" -version = "0.12.7" +version = "0.12.8" dependencies = [ "axum", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index 32f72c5e252..cc677013fa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,7 +100,7 @@ libp2p-quic = { version = "0.11.1", path = "transports/quic" } libp2p-relay = { version = "0.18.0", path = "protocols/relay" } libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" } -libp2p-server = { version = "0.12.7", path = "misc/server" } +libp2p-server = { version = "0.12.8", path = "misc/server" } libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" } libp2p-swarm = { version = "0.45.2", path = "swarm" } libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. @@ -151,4 +151,8 @@ clippy.manual_let_else = "warn" clippy.dbg_macro = "warn" [workspace.metadata.release] -pre-release-hook = ["/bin/sh", '-c', '/bin/sh $WORKSPACE_ROOT/scripts/add-changelog-header.sh'] # Nested use of shell to expand variables. +pre-release-hook = [ + "/bin/sh", + '-c', + '/bin/sh $WORKSPACE_ROOT/scripts/add-changelog-header.sh', +] # Nested use of shell to expand variables. diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index 5369163460c..fe48de0f553 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.12.8 + +### Changed + +- Remove deprecated [`libp2p-lookup`](https://github.com/mxinden/libp2p-lookup) from Dockerfile. + See [PR 5610](https://github.com/libp2p/rust-libp2p/pull/5610). + ## 0.12.7 ### Changed @@ -31,6 +38,7 @@ ## 0.12.3 ### Changed + - Add libp2p-lookup to Dockerfile to enable healthchecks. ### Fixed @@ -42,14 +50,18 @@ [PR 4467]: https://github.com/libp2p/rust-libp2p/pull/4467 ## 0.12.2 + ### Fixed + - Adhere to `--metrics-path` flag and listen on `0.0.0.0:8888` (default IPFS metrics port). [PR 4392] [PR 4392]: https://github.com/libp2p/rust-libp2p/pull/4392 ## 0.12.1 + ### Changed + - Move to tokio and hyper. See [PR 4311]. - Move to distroless Docker base image. @@ -58,39 +70,57 @@ [PR 4311]: https://github.com/libp2p/rust-libp2p/pull/4311 ## 0.8.0 + ### Changed + - Remove mplex support. ## 0.7.0 + ### Changed + - Update to libp2p v0.47.0. ## 0.6.0 - 2022-05-05 + ### Changed + - Update to libp2p v0.44.0. ## 0.5.4 - 2022-01-11 + ### Changed + - Pull latest autonat changes. ## 0.5.3 - 2021-12-25 + ### Changed + - Update dependencies. - Pull in autonat fixes. ## 0.5.2 - 2021-12-20 + ### Added + - Add support for libp2p autonat protocol via `--enable-autonat`. ## 0.5.1 - 2021-12-20 + ### Fixed + - Update dependencies. - Fix typo in command line flag `--enable-kademlia`. ## 0.5.0 - 2021-11-18 + ### Changed + - Disable Kademlia protocol by default. ## 0.4.0 - 2021-11-18 + ### Fixed + - Update dependencies. diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 798ecfa07a9..0954e2f38d8 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-server" -version = "0.12.7" +version = "0.12.8" authors = ["Max Inden "] edition = "2021" repository = "https://github.com/libp2p/rust-libp2p" @@ -16,7 +16,23 @@ clap = { version = "4.5.6", features = ["derive"] } futures = { workspace = true } futures-timer = "3" axum = "0.7" -libp2p = { workspace = true, features = ["autonat", "dns", "tokio", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros", "quic", "websocket"] } +libp2p = { workspace = true, features = [ + "autonat", + "dns", + "tokio", + "noise", + "tcp", + "yamux", + "identify", + "kad", + "ping", + "relay", + "metrics", + "rsa", + "macros", + "quic", + "websocket", +] } prometheus-client = { workspace = true } serde = "1.0.203" serde_derive = "1.0.125" diff --git a/misc/server/Dockerfile b/misc/server/Dockerfile index 24ae2b9fd99..12a8982eb3f 100644 --- a/misc/server/Dockerfile +++ b/misc/server/Dockerfile @@ -1,7 +1,6 @@ # syntax=docker/dockerfile:1.5-labs FROM rust:1.81.0 as chef RUN wget -q -O- https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.62/cargo-chef-x86_64-unknown-linux-gnu.tar.gz | tar -zx -C /usr/local/bin -RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 WORKDIR /app FROM chef AS planner @@ -17,5 +16,4 @@ COPY . . RUN cargo build --release --package libp2p-server FROM gcr.io/distroless/cc -COPY --from=builder /usr/local/bin/libp2p-server /usr/local/bin/libp2p-lookup /usr/local/bin/ CMD ["libp2p-server"] diff --git a/misc/server/README.md b/misc/server/README.md index 0da1bd8abd9..f9a5d65124a 100644 --- a/misc/server/README.md +++ b/misc/server/README.md @@ -25,7 +25,6 @@ Options: -h, --help Print help ``` - ``` cargo run -- --config ~/.ipfs/config @@ -33,9 +32,3 @@ Local peer id: PeerId("12D3KooWSa1YEeQVSwvoqAMhwjKQ6kqZQckhWPb3RWEGV3sZGU6Z") Listening on "/ip4/127.0.0.1/udp/4001/quic" [...] ``` - -The Docker container includes [libp2-lookup](https://github.com/mxinden/libp2p-lookup/) to enable adding a proper healthcheck for container startup, e.g. - -``` shell -docker run --health-cmd 'libp2p-lookup direct --address /ip4/127.0.0.1/tcp/4001/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa' /home/ipfs/.ipfs:/ipfs ghcr.io/libp2p/rust-libp2p-server --config /ipfs/config -``` From 9a45db3f82b760c93099e66ec77a7a772d1f6cd3 Mon Sep 17 00:00:00 2001 From: Darius Clark Date: Tue, 8 Oct 2024 10:16:59 -0400 Subject: [PATCH 26/36] chore: update igd-next to 0.15.1 (#5625) ## Description Resolves #5506. ## Notes & open questions ## Change checklist - [ ] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --- Cargo.lock | 102 +++++++++--------------------------- Cargo.toml | 2 +- protocols/upnp/CHANGELOG.md | 4 ++ protocols/upnp/Cargo.toml | 4 +- 4 files changed, 32 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28107c00ee9..3d77ed104dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -556,9 +556,9 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body 1.0.0", + "http-body", "http-body-util", - "hyper 1.4.1", + "hyper", "hyper-util", "itoa", "matchit", @@ -589,7 +589,7 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body 1.0.0", + "http-body", "http-body-util", "mime", "pin-project-lite", @@ -1865,25 +1865,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "h2" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 0.2.9", - "indexmap 2.2.1", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "h2" version = "0.4.4" @@ -2095,17 +2076,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http-body" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" -dependencies = [ - "bytes", - "http 0.2.9", - "pin-project-lite", -] - [[package]] name = "http-body" version = "1.0.0" @@ -2125,7 +2095,7 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body 1.0.0", + "http-body", "pin-project-lite", ] @@ -2153,30 +2123,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" -[[package]] -name = "hyper" -version = "0.14.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2 0.3.26", - "http 0.2.9", - "http-body 0.4.5", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "socket2 0.4.9", - "tokio", - "tower-service", - "tracing", - "want", -] - [[package]] name = "hyper" version = "1.4.1" @@ -2186,9 +2132,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.4", + "h2", "http 1.1.0", - "http-body 1.0.0", + "http-body", "httparse", "httpdate", "itoa", @@ -2206,7 +2152,7 @@ checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", "http 1.1.0", - "hyper 1.4.1", + "hyper", "hyper-util", "rustls 0.22.4", "rustls-pki-types", @@ -2221,7 +2167,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3203a961e5c83b6f5498933e78b6b263e208c197b63e9c6c53cc82ffd3f63793" dependencies = [ - "hyper 1.4.1", + "hyper", "hyper-util", "pin-project-lite", "tokio", @@ -2236,7 +2182,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", "http-body-util", - "hyper 1.4.1", + "hyper", "hyper-util", "native-tls", "tokio", @@ -2254,8 +2200,8 @@ dependencies = [ "futures-channel", "futures-util", "http 1.1.0", - "http-body 1.0.0", - "hyper 1.4.1", + "http-body", + "hyper", "pin-project-lite", "socket2 0.5.7", "tokio", @@ -2326,16 +2272,18 @@ dependencies = [ [[package]] name = "igd-next" -version = "0.14.3" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "064d90fec10d541084e7b39ead8875a5a80d9114a2b18791565253bae25f49e4" +checksum = "76b0d7d4541def58a37bf8efc559683f21edce7c82f0d866c93ac21f7e098f93" dependencies = [ "async-trait", "attohttpc", "bytes", "futures", - "http 0.2.9", - "hyper 0.14.27", + "http 1.1.0", + "http-body-util", + "hyper", + "hyper-util", "log", "rand 0.8.5", "tokio", @@ -3399,7 +3347,7 @@ dependencies = [ [[package]] name = "libp2p-upnp" -version = "0.3.0" +version = "0.3.1" dependencies = [ "futures", "futures-timer", @@ -4931,11 +4879,11 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.4.4", + "h2", "http 1.1.0", - "http-body 1.0.0", + "http-body", "http-body-util", - "hyper 1.4.1", + "hyper", "hyper-rustls", "hyper-tls", "hyper-util", @@ -6176,11 +6124,11 @@ dependencies = [ "axum", "base64 0.22.1", "bytes", - "h2 0.4.4", + "h2", "http 1.1.0", - "http-body 1.0.0", + "http-body", "http-body-util", - "hyper 1.4.1", + "hyper", "hyper-timeout", "hyper-util", "percent-encoding", @@ -6225,7 +6173,7 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body 1.0.0", + "http-body", "http-body-util", "http-range-header", "httpdate", diff --git a/Cargo.toml b/Cargo.toml index cc677013fa9..9e18af5f706 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,7 +108,7 @@ libp2p-swarm-test = { version = "0.4.0", path = "swarm-test" } libp2p-tcp = { version = "0.42.0", path = "transports/tcp" } libp2p-tls = { version = "0.5.0", path = "transports/tls" } libp2p-uds = { version = "0.41.0", path = "transports/uds" } -libp2p-upnp = { version = "0.3.0", path = "protocols/upnp" } +libp2p-upnp = { version = "0.3.1", path = "protocols/upnp" } libp2p-webrtc = { version = "0.8.0-alpha", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.3.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.4.0-alpha.2", path = "transports/webrtc-websys" } diff --git a/protocols/upnp/CHANGELOG.md b/protocols/upnp/CHANGELOG.md index 21e90f9534b..d9c24f8efcc 100644 --- a/protocols/upnp/CHANGELOG.md +++ b/protocols/upnp/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.1 +- update igd-next to 0.15.1. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). + ## 0.3.0 diff --git a/protocols/upnp/Cargo.toml b/protocols/upnp/Cargo.toml index e9c7414236d..209733f53e6 100644 --- a/protocols/upnp/Cargo.toml +++ b/protocols/upnp/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-upnp" edition = "2021" rust-version = "1.60.0" description = "UPnP support for libp2p transports" -version = "0.3.0" +version = "0.3.1" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] @@ -13,7 +13,7 @@ publish = true [dependencies] futures = { workspace = true } futures-timer = "3.0.3" -igd-next = "0.14.3" +igd-next = "0.15.1" libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } tokio = { workspace = true, default-features = false, features = ["rt"], optional = true } From d3228adf64cf68f419eeb0b18a5c0b762d29abbe Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 09:00:56 +0000 Subject: [PATCH 27/36] deps: bump Swatinem/rust-cache from 2.7.3 to 2.7.5 (#5633) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bumps [Swatinem/rust-cache](https://github.com/swatinem/rust-cache) from 2.7.3 to 2.7.5.
Release notes

Sourced from Swatinem/rust-cache's releases.

v2.7.5

What's Changed

New Contributors

Full Changelog: https://github.com/Swatinem/rust-cache/compare/v2.7.3...v2.7.5

Commits

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Swatinem/rust-cache&package-manager=github_actions&previous-version=2.7.3&new-version=2.7.5)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) You can trigger a rebase of this PR by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) ---
Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/cache-factory.yml | 2 +- .github/workflows/ci.yml | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/cache-factory.yml b/.github/workflows/cache-factory.yml index 8c49b335f1b..7623b56f450 100644 --- a/.github/workflows/cache-factory.yml +++ b/.github/workflows/cache-factory.yml @@ -22,7 +22,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2c1dfc8aaef..daee569d047 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache save-if: false @@ -149,7 +149,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: key: ${{ matrix.target }} save-if: ${{ github.ref == 'refs/heads/master' }} @@ -174,7 +174,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -195,7 +195,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: key: ${{ matrix.features }} save-if: ${{ github.ref == 'refs/heads/master' }} @@ -212,7 +212,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -238,7 +238,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -254,7 +254,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: save-if: ${{ github.ref == 'refs/heads/master' }} @@ -273,7 +273,7 @@ jobs: - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: shared-key: stable-cache save-if: false @@ -365,7 +365,7 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 - run: cargo install --version 0.10.0 pb-rs --locked @@ -391,7 +391,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 + - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 - run: cargo metadata --locked --format-version=1 > /dev/null cargo-deny: From 812a7cdf5d229610bb8eda1f106eee5ac3f699d2 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 16 Oct 2024 01:32:02 +0800 Subject: [PATCH 28/36] feat: make runtime features optional in swarm-test (#5551) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Sometimes a test uses custom swarm building logic and doesn't need `fn new_ephemeral`, and sometimes a test uses `tokio` runtime other than `async-std`. This PR adds the `tokio` runtime support and makes both `async-std` and `tokio` runtimes optional behind features to make it more flexible. ## Notes & open questions ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: João Oliveira --- Cargo.lock | 2 +- Cargo.toml | 2 +- swarm-test/CHANGELOG.md | 7 +++++ swarm-test/Cargo.toml | 12 +++++-- swarm-test/src/lib.rs | 69 ++++++++++++++++++++++++++++++++--------- 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d77ed104dc..40ff97009de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3278,7 +3278,7 @@ dependencies = [ [[package]] name = "libp2p-swarm-test" -version = "0.4.0" +version = "0.4.1" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index 9e18af5f706..7b52e4a7b42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,7 +104,7 @@ libp2p-server = { version = "0.12.8", path = "misc/server" } libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" } libp2p-swarm = { version = "0.45.2", path = "swarm" } libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. -libp2p-swarm-test = { version = "0.4.0", path = "swarm-test" } +libp2p-swarm-test = { version = "0.4.1", path = "swarm-test" } libp2p-tcp = { version = "0.42.0", path = "transports/tcp" } libp2p-tls = { version = "0.5.0", path = "transports/tls" } libp2p-uds = { version = "0.41.0", path = "transports/uds" } diff --git a/swarm-test/CHANGELOG.md b/swarm-test/CHANGELOG.md index 98027fcbea2..33eebb2412c 100644 --- a/swarm-test/CHANGELOG.md +++ b/swarm-test/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.4.1 + +- Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features. + See [PR 5551]. + +[PR 5551]: https://github.com/libp2p/rust-libp2p/pull/5551 + ## 0.4.0 diff --git a/swarm-test/Cargo.toml b/swarm-test/Cargo.toml index b285da34f87..fa51454dd58 100644 --- a/swarm-test/Cargo.toml +++ b/swarm-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-swarm-test" -version = "0.4.0" +version = "0.4.1" edition = "2021" rust-version = { workspace = true } license = "MIT" @@ -16,13 +16,19 @@ async-trait = "0.1.80" libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-plaintext = { workspace = true } -libp2p-swarm = { workspace = true, features = ["async-std"] } -libp2p-tcp = { workspace = true, features = ["async-io"] } +libp2p-swarm = { workspace = true } +libp2p-tcp = { workspace = true } libp2p-yamux = { workspace = true } futures = { workspace = true } rand = "0.8.5" tracing = { workspace = true } futures-timer = "3.0.3" +[features] +default = ["async-std"] + +async-std = ["libp2p-swarm/async-std", "libp2p-tcp/async-io"] +tokio = ["libp2p-swarm/tokio", "libp2p-tcp/tokio"] + [lints] workspace = true diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 48f5bcbf4ef..bcab6e5b700 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -21,14 +21,10 @@ use async_trait::async_trait; use futures::future::{BoxFuture, Either}; use futures::{FutureExt, StreamExt}; -use libp2p_core::{ - multiaddr::Protocol, transport::MemoryTransport, upgrade::Version, Multiaddr, Transport, -}; -use libp2p_identity::{Keypair, PeerId}; -use libp2p_plaintext as plaintext; +use libp2p_core::{multiaddr::Protocol, Multiaddr}; +use libp2p_identity::PeerId; use libp2p_swarm::dial_opts::PeerCondition; -use libp2p_swarm::{self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p_yamux as yamux; +use libp2p_swarm::{dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent}; use std::fmt::Debug; use std::future::IntoFuture; use std::time::Duration; @@ -38,12 +34,23 @@ use std::time::Duration; pub trait SwarmExt { type NB: NetworkBehaviour; - /// Create a new [`Swarm`] with an ephemeral identity. + /// Create a new [`Swarm`] with an ephemeral identity and the `async-std` runtime. /// - /// The swarm will use a [`MemoryTransport`] together with a [`plaintext::Config`] authentication layer and - /// [`yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test + /// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and + /// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test /// and may change at any time. - fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self + #[cfg(feature = "async-std")] + fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self + where + Self: Sized; + + /// Create a new [`Swarm`] with an ephemeral identity and the `tokio` runtime. + /// + /// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and + /// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test + /// and may change at any time. + #[cfg(feature = "tokio")] + fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self where Self: Sized; @@ -200,18 +207,50 @@ where { type NB = B; - fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self + #[cfg(feature = "async-std")] + fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self where Self: Sized, { + use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _}; + use libp2p_identity::Keypair; + let identity = Keypair::generate_ed25519(); let peer_id = PeerId::from(identity.public()); let transport = MemoryTransport::default() .or_transport(libp2p_tcp::async_io::Transport::default()) .upgrade(Version::V1) - .authenticate(plaintext::Config::new(&identity)) - .multiplex(yamux::Config::default()) + .authenticate(libp2p_plaintext::Config::new(&identity)) + .multiplex(libp2p_yamux::Config::default()) + .timeout(Duration::from_secs(20)) + .boxed(); + + Swarm::new( + transport, + behaviour_fn(identity), + peer_id, + libp2p_swarm::Config::with_async_std_executor() + .with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures., + ) + } + + #[cfg(feature = "tokio")] + fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self + where + Self: Sized, + { + use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _}; + use libp2p_identity::Keypair; + + let identity = Keypair::generate_ed25519(); + let peer_id = PeerId::from(identity.public()); + + let transport = MemoryTransport::default() + .or_transport(libp2p_tcp::tokio::Transport::default()) + .upgrade(Version::V1) + .authenticate(libp2p_plaintext::Config::new(&identity)) + .multiplex(libp2p_yamux::Config::default()) .timeout(Duration::from_secs(20)) .boxed(); @@ -219,7 +258,7 @@ where transport, behaviour_fn(identity), peer_id, - swarm::Config::with_async_std_executor() + libp2p_swarm::Config::with_tokio_executor() .with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures., ) } From 524d7f1560d084b14b0ec53563f13ec8c4b7bcf3 Mon Sep 17 00:00:00 2001 From: yanziseeker <153156292+AdventureSeeker987@users.noreply.github.com> Date: Wed, 23 Oct 2024 07:52:20 +0800 Subject: [PATCH 29/36] chore: fix typo in comment (#5643) ## Description ## Notes & open questions ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] A changelog entry has been made in the appropriate crates --- protocols/kad/src/query/peers/fixed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/kad/src/query/peers/fixed.rs b/protocols/kad/src/query/peers/fixed.rs index b34f7516801..2d0b312454d 100644 --- a/protocols/kad/src/query/peers/fixed.rs +++ b/protocols/kad/src/query/peers/fixed.rs @@ -25,7 +25,7 @@ use std::{collections::hash_map::Entry, num::NonZeroUsize, vec}; /// A peer iterator for a fixed set of peers. pub(crate) struct FixedPeersIter { - /// Ther permitted parallelism, i.e. number of pending results. + /// The permitted parallelism, i.e. number of pending results. parallelism: NonZeroUsize, /// The state of peers emitted by the iterator. From 9ed181b3acc77cac1f081a98e4c7762411812e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Thu, 24 Oct 2024 16:13:15 +0100 Subject: [PATCH 30/36] deps(ci): update cargo-semver-checks (#5647) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index daee569d047..aad5b39aec7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -308,7 +308,7 @@ jobs: RUSTFLAGS: '' steps: - uses: actions/checkout@v4 - - run: wget -q -O- https://github.com/obi1kenobi/cargo-semver-checks/releases/download/v0.33.0/cargo-semver-checks-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C ~/.cargo/bin + - run: wget -q -O- https://github.com/obi1kenobi/cargo-semver-checks/releases/download/v0.36.0/cargo-semver-checks-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C ~/.cargo/bin shell: bash - uses: obi1kenobi/cargo-semver-checks-action@7272cc2caa468d3e009a2b0a9cc366839348237b # v2.6 From 6cb116e8fc49c0b54765a0178aaa4a057d290e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Thu, 24 Oct 2024 17:05:30 +0100 Subject: [PATCH 31/36] fix(swarm-test): set proper version (#5648) ## Description To unblock CI Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- swarm-test/CHANGELOG.md | 2 +- swarm-test/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40ff97009de..54a0f8657a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3278,7 +3278,7 @@ dependencies = [ [[package]] name = "libp2p-swarm-test" -version = "0.4.1" +version = "0.5.0" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index 7b52e4a7b42..af7e47f8359 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,7 +104,7 @@ libp2p-server = { version = "0.12.8", path = "misc/server" } libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" } libp2p-swarm = { version = "0.45.2", path = "swarm" } libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. -libp2p-swarm-test = { version = "0.4.1", path = "swarm-test" } +libp2p-swarm-test = { version = "0.5.0", path = "swarm-test" } libp2p-tcp = { version = "0.42.0", path = "transports/tcp" } libp2p-tls = { version = "0.5.0", path = "transports/tls" } libp2p-uds = { version = "0.41.0", path = "transports/uds" } diff --git a/swarm-test/CHANGELOG.md b/swarm-test/CHANGELOG.md index 33eebb2412c..5700460b3a6 100644 --- a/swarm-test/CHANGELOG.md +++ b/swarm-test/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.4.1 +## 0.5.0 - Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features. See [PR 5551]. diff --git a/swarm-test/Cargo.toml b/swarm-test/Cargo.toml index fa51454dd58..7ac7c900deb 100644 --- a/swarm-test/Cargo.toml +++ b/swarm-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-swarm-test" -version = "0.4.1" +version = "0.5.0" edition = "2021" rust-version = { workspace = true } license = "MIT" From 84c617fd16048828dfe4cc3279e893282221bc19 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 25 Oct 2024 02:15:00 +0300 Subject: [PATCH 32/36] feat(kad): add `Behavior::find_closest_local_peers()` (#5645) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Fixes https://github.com/libp2p/rust-libp2p/issues/5626 ## Notes & open questions This is the nicest way I came up with, I decided to leave `get_closest_local_peers` as is since it does return all peers, not just `replication_factor` peers. Looking at https://github.com/libp2p/rust-libp2p/pull/2436 it is not clear if @folex really needed all peers returned or it just happened that way. I'm also happy to change proposed API to return all peers if that is preferred by others. It is very unfortunate that `&mut self` is needed for this, I created https://github.com/libp2p/rust-libp2p/issues/5644 that if resolved will allow to have `&self` instead. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates Co-authored-by: João Oliveira --- protocols/kad/CHANGELOG.md | 5 ++-- protocols/kad/src/behaviour.rs | 48 ++++++++++++++++++++-------------- protocols/kad/src/lib.rs | 2 +- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index d0ab7986aad..55d269bf98f 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,10 +1,11 @@ ## 0.47.0 -- Expose a kad query facility allowing specify num_results dynamicly. +- Expose a kad query facility allowing specify num_results dynamicaly. See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555). - Add `mode` getter on `Behaviour`. See [PR 5573](https://github.com/libp2p/rust-libp2p/pull/5573). - +- Add `Behavior::find_closest_local_peers()`. + See [PR 5645](https://github.com/libp2p/rust-libp2p/pull/5645). ## 0.46.2 diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 0b15e507ba4..84133d31acb 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -771,7 +771,8 @@ where self.queries.add_iter_closest(target, peer_keys, info) } - /// Returns closest peers to the given key; takes peers from local routing table only. + /// Returns all peers ordered by distance to the given key; takes peers from local routing table + /// only. pub fn get_closest_local_peers<'a, K: Clone>( &'a mut self, key: &'a kbucket::Key, @@ -779,6 +780,23 @@ where self.kbuckets.closest_keys(key) } + /// Finds the closest peers to a `key` in the context of a request by the `source` peer, such + /// that the `source` peer is never included in the result. + /// + /// Takes peers from local routing table only. Only returns number of peers equal to configured + /// replication factor. + pub fn find_closest_local_peers<'a, K: Clone>( + &'a mut self, + key: &'a kbucket::Key, + source: &'a PeerId, + ) -> impl Iterator + 'a { + self.kbuckets + .closest(key) + .filter(move |e| e.node.key.preimage() != source) + .take(self.queries.config().replication_factor.get()) + .map(KadPeer::from) + } + /// Performs a lookup for a record in the DHT. /// /// The result of this operation is delivered in a @@ -1212,22 +1230,6 @@ where } } - /// Finds the closest peers to a `target` in the context of a request by - /// the `source` peer, such that the `source` peer is never included in the - /// result. - fn find_closest( - &mut self, - target: &kbucket::Key, - source: &PeerId, - ) -> Vec { - self.kbuckets - .closest(target) - .filter(|e| e.node.key.preimage() != source) - .take(self.queries.config().replication_factor.get()) - .map(KadPeer::from) - .collect() - } - /// Collects all peers who are known to be providers of the value for a given `Multihash`. fn provider_peers(&mut self, key: &record::Key, source: &PeerId) -> Vec { let kbuckets = &mut self.kbuckets; @@ -2300,7 +2302,9 @@ where } HandlerEvent::FindNodeReq { key, request_id } => { - let closer_peers = self.find_closest(&kbucket::Key::new(key), &source); + let closer_peers = self + .find_closest_local_peers(&kbucket::Key::new(key), &source) + .collect::>(); self.queued_events .push_back(ToSwarm::GenerateEvent(Event::InboundRequest { @@ -2328,7 +2332,9 @@ where HandlerEvent::GetProvidersReq { key, request_id } => { let provider_peers = self.provider_peers(&key, &source); - let closer_peers = self.find_closest(&kbucket::Key::new(key), &source); + let closer_peers = self + .find_closest_local_peers(&kbucket::Key::new(key), &source) + .collect::>(); self.queued_events .push_back(ToSwarm::GenerateEvent(Event::InboundRequest { @@ -2422,7 +2428,9 @@ where None => None, }; - let closer_peers = self.find_closest(&kbucket::Key::new(key), &source); + let closer_peers = self + .find_closest_local_peers(&kbucket::Key::new(key), &source) + .collect::>(); self.queued_events .push_back(ToSwarm::GenerateEvent(Event::InboundRequest { diff --git a/protocols/kad/src/lib.rs b/protocols/kad/src/lib.rs index 681d135f79b..060bfc518e4 100644 --- a/protocols/kad/src/lib.rs +++ b/protocols/kad/src/lib.rs @@ -69,7 +69,7 @@ pub use behaviour::{ pub use kbucket::{ Distance as KBucketDistance, EntryView, KBucketRef, Key as KBucketKey, NodeStatus, }; -pub use protocol::ConnectionType; +pub use protocol::{ConnectionType, KadPeer}; pub use query::QueryId; pub use record::{store, Key as RecordKey, ProviderRecord, Record}; From c7e8129726c584e1fe41ffe3aadf686863906f9f Mon Sep 17 00:00:00 2001 From: Dzmitry Kalabuk Date: Fri, 25 Oct 2024 15:30:32 +0300 Subject: [PATCH 33/36] feat(gossipsub): apply `max_transmit_size` to the published message (#5642) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description When trying to publish a message using gossipsub's `publish` method, it should be possible to predict whether it will fit in the limit defined by the `max_transmit_size` config option. If this limit applies to the final protobuf payload, it's not possible to know that in advance because the size of the added fields is not fixed. This change makes the limit apply to the passed message size instead of the final wire size. ## Notes & open questions This is a minor version change because it changes the meaning of the existing config option. However, for the existing clients the limit will only become more permissive, so it shouldn't break anything. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: Darius Clark Co-authored-by: João Oliveira --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/gossipsub/CHANGELOG.md | 5 +++++ protocols/gossipsub/Cargo.toml | 2 +- protocols/gossipsub/src/behaviour.rs | 10 +++++----- protocols/gossipsub/src/config.rs | 3 ++- 6 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54a0f8657a1..3b983a80d00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2741,7 +2741,7 @@ dependencies = [ [[package]] name = "libp2p-gossipsub" -version = "0.47.1" +version = "0.48.0" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index af7e47f8359..8869505921d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ libp2p-core = { version = "0.42.0", path = "core" } libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } libp2p-dns = { version = "0.42.0", path = "transports/dns" } libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } -libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" } +libp2p-gossipsub = { version = "0.48.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.45.1", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } libp2p-kad = { version = "0.47.0", path = "protocols/kad" } diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index c47a9f40f66..cdd170c0d4b 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.48.0 + +- Apply `max_transmit_size` to the inner message instead of the final payload. + See [PR 5642](https://github.com/libp2p/rust-libp2p/pull/5642). + ## 0.47.1 - Attempt to publish to at least mesh_n peers when flood publish is disabled. diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 665f757fcb3..734ac36a231 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-gossipsub" edition = "2021" rust-version = { workspace = true } description = "Gossipsub protocol for libp2p" -version = "0.47.1" +version = "0.48.0" authors = ["Age Manning "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 16adb555a44..6ddb25316e5 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -583,6 +583,11 @@ where .data_transform .outbound_transform(&topic, data.clone())?; + // check that the size doesn't exceed the max transmission size. + if transformed_data.len() > self.config.max_transmit_size() { + return Err(PublishError::MessageTooLarge); + } + let raw_message = self.build_raw_message(topic, transformed_data)?; // calculate the message id from the un-transformed data @@ -593,11 +598,6 @@ where topic: raw_message.topic.clone(), }); - // check that the size doesn't exceed the max transmission size - if raw_message.raw_protobuf_len() > self.config.max_transmit_size() { - return Err(PublishError::MessageTooLarge); - } - // Check the if the message has been published before if self.duplicate_cache.contains(&msg_id) { // This message has already been seen. We don't re-publish messages that have already diff --git a/protocols/gossipsub/src/config.rs b/protocols/gossipsub/src/config.rs index febe2514a30..1ee2e940661 100644 --- a/protocols/gossipsub/src/config.rs +++ b/protocols/gossipsub/src/config.rs @@ -174,7 +174,8 @@ impl Config { /// The maximum byte size for each gossipsub RPC (default is 65536 bytes). /// - /// This represents the maximum size of the entire protobuf payload. It must be at least + /// This represents the maximum size of the published message. It is additionally wrapped + /// in a protobuf struct, so the actual wire size may be a bit larger. It must be at least /// large enough to support basic control messages. If Peer eXchange is enabled, this /// must be large enough to transmit the desired peer information on pruning. It must be at /// least 100 bytes. Default is 65536 bytes. From 4e7ff3ebf27336f2f014067f33437c428c81f5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Sat, 26 Oct 2024 20:10:25 +0100 Subject: [PATCH 34/36] chore(ci): address clippy beta lints (#5649) Co-authored-by: Darius Clark --- Cargo.lock | 1 + identity/src/peer_id.rs | 2 +- identity/src/rsa.rs | 4 ++-- misc/quick-protobuf-codec/src/lib.rs | 2 +- muxers/test-harness/src/lib.rs | 2 +- .../autonat/src/v1/behaviour/as_client.rs | 4 ++-- .../autonat/src/v1/behaviour/as_server.rs | 4 ++-- protocols/gossipsub/src/backoff.rs | 3 +-- protocols/kad/src/behaviour.rs | 4 ++-- protocols/ping/src/protocol.rs | 1 - swarm/src/connection.rs | 2 +- swarm/src/handler.rs | 6 ++--- transports/noise/src/io/handshake.rs | 1 - transports/websocket-websys/src/lib.rs | 4 ++-- transports/websocket/src/framed.rs | 2 +- transports/webtransport-websys/Cargo.toml | 1 + transports/webtransport-websys/src/utils.rs | 22 +++++++------------ 17 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b983a80d00..f0c36291839 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3484,6 +3484,7 @@ dependencies = [ "multiaddr", "multibase", "multihash", + "once_cell", "send_wrapper 0.6.0", "thiserror", "tracing", diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 7b3f799f612..8ae6d99ae32 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -191,7 +191,7 @@ impl<'de> Deserialize<'de> for PeerId { struct PeerIdVisitor; - impl<'de> Visitor<'de> for PeerIdVisitor { + impl Visitor<'_> for PeerIdVisitor { type Value = PeerId; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/identity/src/rsa.rs b/identity/src/rsa.rs index cbfe3c1b919..5eb78a4af75 100644 --- a/identity/src/rsa.rs +++ b/identity/src/rsa.rs @@ -149,7 +149,7 @@ struct Asn1RawOid<'a> { object: DerObject<'a>, } -impl<'a> Asn1RawOid<'a> { +impl Asn1RawOid<'_> { /// The underlying OID as byte literal. pub(crate) fn oid(&self) -> &[u8] { self.object.value() @@ -169,7 +169,7 @@ impl<'a> DerTypeView<'a> for Asn1RawOid<'a> { } } -impl<'a> DerEncodable for Asn1RawOid<'a> { +impl DerEncodable for Asn1RawOid<'_> { fn encode(&self, sink: &mut S) -> Result<(), Asn1DerError> { self.object.encode(sink) } diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index 32cee8eccac..c57b7da7db8 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -120,7 +120,7 @@ impl<'a> BytesMutWriterBackend<'a> { } } -impl<'a> WriterBackend for BytesMutWriterBackend<'a> { +impl WriterBackend for BytesMutWriterBackend<'_> { fn pb_write_u8(&mut self, x: u8) -> quick_protobuf::Result<()> { self.dst.put_u8(x); diff --git a/muxers/test-harness/src/lib.rs b/muxers/test-harness/src/lib.rs index 16c71f414f0..d03bdbdfed7 100644 --- a/muxers/test-harness/src/lib.rs +++ b/muxers/test-harness/src/lib.rs @@ -206,7 +206,7 @@ enum Event { ProtocolComplete, } -impl<'m, M> Stream for Harness<'m, M> +impl Stream for Harness<'_, M> where M: StreamMuxer + Unpin, { diff --git a/protocols/autonat/src/v1/behaviour/as_client.rs b/protocols/autonat/src/v1/behaviour/as_client.rs index 8960163ccb3..385dee50ee1 100644 --- a/protocols/autonat/src/v1/behaviour/as_client.rs +++ b/protocols/autonat/src/v1/behaviour/as_client.rs @@ -98,7 +98,7 @@ pub(crate) struct AsClient<'a> { pub(crate) other_candidates: &'a HashSet, } -impl<'a> HandleInnerEvent for AsClient<'a> { +impl HandleInnerEvent for AsClient<'_> { fn handle_event( &mut self, event: request_response::Event, @@ -179,7 +179,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> { } } -impl<'a> AsClient<'a> { +impl AsClient<'_> { pub(crate) fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll { match self.schedule_probe.poll_unpin(cx) { Poll::Ready(()) => { diff --git a/protocols/autonat/src/v1/behaviour/as_server.rs b/protocols/autonat/src/v1/behaviour/as_server.rs index 1289bd53d24..01148add6e8 100644 --- a/protocols/autonat/src/v1/behaviour/as_server.rs +++ b/protocols/autonat/src/v1/behaviour/as_server.rs @@ -91,7 +91,7 @@ pub(crate) struct AsServer<'a> { >, } -impl<'a> HandleInnerEvent for AsServer<'a> { +impl HandleInnerEvent for AsServer<'_> { fn handle_event( &mut self, event: request_response::Event, @@ -208,7 +208,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> { } } -impl<'a> AsServer<'a> { +impl AsServer<'_> { pub(crate) fn on_outbound_connection( &mut self, peer: &PeerId, diff --git a/protocols/gossipsub/src/backoff.rs b/protocols/gossipsub/src/backoff.rs index b24da318582..4414ffb00e6 100644 --- a/protocols/gossipsub/src/backoff.rs +++ b/protocols/gossipsub/src/backoff.rs @@ -48,8 +48,7 @@ pub(crate) struct BackoffStorage { impl BackoffStorage { fn heartbeats(d: &Duration, heartbeat_interval: &Duration) -> usize { - ((d.as_nanos() + heartbeat_interval.as_nanos() - 1) / heartbeat_interval.as_nanos()) - as usize + d.as_nanos().div_ceil(heartbeat_interval.as_nanos()) as usize } pub(crate) fn new( diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 84133d31acb..f577971167f 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -3361,7 +3361,7 @@ pub struct QueryMut<'a> { query: &'a mut Query, } -impl<'a> QueryMut<'a> { +impl QueryMut<'_> { pub fn id(&self) -> QueryId { self.query.id() } @@ -3391,7 +3391,7 @@ pub struct QueryRef<'a> { query: &'a Query, } -impl<'a> QueryRef<'a> { +impl QueryRef<'_> { pub fn id(&self) -> QueryId { self.query.id() } diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 566e5e258e2..6e3f06d0498 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -44,7 +44,6 @@ pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/ping/1.0.0" /// > Nagle's algorithm, delayed acks and similar configuration options /// > which can affect latencies especially on otherwise low-volume /// > connections. - const PING_SIZE: usize = 32; /// Sends a ping and waits for the pong. diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 2f9afc38418..859d138b83a 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -516,7 +516,7 @@ pub(crate) struct IncomingInfo<'a> { pub(crate) send_back_addr: &'a Multiaddr, } -impl<'a> IncomingInfo<'a> { +impl IncomingInfo<'_> { /// Builds the [`ConnectedPoint`] corresponding to the incoming connection. pub(crate) fn create_connected_point(&self) -> ConnectedPoint { ConnectedPoint::Listener { diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 610b95b8cf1..9e31592d68d 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -226,7 +226,7 @@ pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IO RemoteProtocolsChange(ProtocolsChange<'a>), } -impl<'a, IP, OP, IOI, OOI> fmt::Debug for ConnectionEvent<'a, IP, OP, IOI, OOI> +impl fmt::Debug for ConnectionEvent<'_, IP, OP, IOI, OOI> where IP: InboundUpgradeSend + fmt::Debug, IP::Output: fmt::Debug, @@ -262,8 +262,8 @@ where } } -impl<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> - ConnectionEvent<'a, IP, OP, IOI, OOI> +impl + ConnectionEvent<'_, IP, OP, IOI, OOI> { /// Whether the event concerns an outbound stream. pub fn is_outbound(&self) -> bool { diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index 5c1fa806b6d..8993a5795b6 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -73,7 +73,6 @@ where /// will be sent and received on the given I/O resource and using the /// provided session for cryptographic operations according to the chosen /// Noise handshake pattern. - pub(crate) fn new( io: T, session: snow::HandshakeState, diff --git a/transports/websocket-websys/src/lib.rs b/transports/websocket-websys/src/lib.rs index f353d92b204..3467e802bc5 100644 --- a/transports/websocket-websys/src/lib.rs +++ b/transports/websocket-websys/src/lib.rs @@ -96,8 +96,8 @@ impl libp2p_core::Transport for Transport { return Err(TransportError::MultiaddrNotSupported(addr)); } - let url = extract_websocket_url(&addr) - .ok_or_else(|| TransportError::MultiaddrNotSupported(addr))?; + let url = + extract_websocket_url(&addr).ok_or(TransportError::MultiaddrNotSupported(addr))?; Ok(async move { let socket = match WebSocket::new(&url) { diff --git a/transports/websocket/src/framed.rs b/transports/websocket/src/framed.rs index a547aea21ef..198443508d9 100644 --- a/transports/websocket/src/framed.rs +++ b/transports/websocket/src/framed.rs @@ -442,7 +442,7 @@ pub(crate) enum WsListenProto<'a> { TlsWs(Cow<'a, str>), } -impl<'a> WsListenProto<'a> { +impl WsListenProto<'_> { pub(crate) fn append_on_addr(&self, addr: &mut Multiaddr) { match self { WsListenProto::Ws(path) => { diff --git a/transports/webtransport-websys/Cargo.toml b/transports/webtransport-websys/Cargo.toml index 9541c49b737..eeb474d4a63 100644 --- a/transports/webtransport-websys/Cargo.toml +++ b/transports/webtransport-websys/Cargo.toml @@ -21,6 +21,7 @@ libp2p-identity = { workspace = true } libp2p-noise = { workspace = true } multiaddr = { workspace = true } multihash = { workspace = true } +once_cell = "1.19.0" send_wrapper = { version = "0.6.0", features = ["futures"] } thiserror = "1.0.61" tracing = { workspace = true } diff --git a/transports/webtransport-websys/src/utils.rs b/transports/webtransport-websys/src/utils.rs index 55bad08e00c..0b3550e5b5b 100644 --- a/transports/webtransport-websys/src/utils.rs +++ b/transports/webtransport-websys/src/utils.rs @@ -1,10 +1,17 @@ use js_sys::{Promise, Reflect}; +use once_cell::sync::Lazy; use send_wrapper::SendWrapper; use std::io; use wasm_bindgen::{JsCast, JsValue}; use crate::Error; +type Closure = wasm_bindgen::closure::Closure; +static DO_NOTHING: Lazy> = Lazy::new(|| { + let cb = Closure::new(|_| {}); + SendWrapper::new(cb) +}); + /// Properly detach a promise. /// /// A promise always runs in the background, however if you don't await it, @@ -13,22 +20,9 @@ use crate::Error; // // Ref: https://github.com/typescript-eslint/typescript-eslint/blob/391a6702c0a9b5b3874a7a27047f2a721f090fb6/packages/eslint-plugin/docs/rules/no-floating-promises.md pub(crate) fn detach_promise(promise: Promise) { - type Closure = wasm_bindgen::closure::Closure; - static mut DO_NOTHING: Option> = None; - - // Allocate Closure only once and reuse it - let do_nothing = unsafe { - if DO_NOTHING.is_none() { - let cb = Closure::new(|_| {}); - DO_NOTHING = Some(SendWrapper::new(cb)); - } - - DO_NOTHING.as_deref().unwrap() - }; - // Avoid having "floating" promise and ignore any errors. // After `catch` promise is allowed to be dropped. - let _ = promise.catch(do_nothing); + let _ = promise.catch(&DO_NOTHING); } /// Typecasts a JavaScript type. From 9586071e54a090a2526c8179c508b93d3cfdf4ac Mon Sep 17 00:00:00 2001 From: Krishang Shah <93703995+kamuik16@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:48:41 +0530 Subject: [PATCH 35/36] feat: refactor distributed-key-value-store example (#5652) ## Description ref #4449 Refactored distributed-key-value-store example to use `tokio` instead of `async-std` ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --- Cargo.lock | 2 +- .../distributed-key-value-store/Cargo.toml | 4 ++-- .../distributed-key-value-store/src/main.rs | 21 ++++++++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0c36291839..d3e2fc9fa47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1361,10 +1361,10 @@ dependencies = [ name = "distributed-key-value-store-example" version = "0.1.0" dependencies = [ - "async-std", "async-trait", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/examples/distributed-key-value-store/Cargo.toml b/examples/distributed-key-value-store/Cargo.toml index 9c2e2bce5c9..3846e54c8d3 100644 --- a/examples/distributed-key-value-store/Cargo.toml +++ b/examples/distributed-key-value-store/Cargo.toml @@ -9,10 +9,10 @@ license = "MIT" release = false [dependencies] -async-std = { version = "1.12", features = ["attributes"] } +tokio = { workspace = true, features = ["full"] } async-trait = "0.1" futures = { workspace = true } -libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] } +libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } diff --git a/examples/distributed-key-value-store/src/main.rs b/examples/distributed-key-value-store/src/main.rs index 404333f3d20..6b7947b7eb3 100644 --- a/examples/distributed-key-value-store/src/main.rs +++ b/examples/distributed-key-value-store/src/main.rs @@ -20,8 +20,7 @@ #![doc = include_str!("../README.md")] -use async_std::io; -use futures::{prelude::*, select}; +use futures::stream::StreamExt; use libp2p::kad; use libp2p::kad::store::MemoryStore; use libp2p::kad::Mode; @@ -32,9 +31,13 @@ use libp2p::{ }; use std::error::Error; use std::time::Duration; +use tokio::{ + io::{self, AsyncBufReadExt}, + select, +}; use tracing_subscriber::EnvFilter; -#[async_std::main] +#[tokio::main] async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -44,11 +47,11 @@ async fn main() -> Result<(), Box> { #[derive(NetworkBehaviour)] struct Behaviour { kademlia: kad::Behaviour, - mdns: mdns::async_io::Behaviour, + mdns: mdns::tokio::Behaviour, } let mut swarm = libp2p::SwarmBuilder::with_new_identity() - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, @@ -60,7 +63,7 @@ async fn main() -> Result<(), Box> { key.public().to_peer_id(), MemoryStore::new(key.public().to_peer_id()), ), - mdns: mdns::async_io::Behaviour::new( + mdns: mdns::tokio::Behaviour::new( mdns::Config::default(), key.public().to_peer_id(), )?, @@ -72,7 +75,7 @@ async fn main() -> Result<(), Box> { swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server)); // Read full lines from stdin - let mut stdin = io::BufReader::new(io::stdin()).lines().fuse(); + let mut stdin = io::BufReader::new(io::stdin()).lines(); // Listen on all interfaces and whatever port the OS assigns. swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; @@ -80,7 +83,9 @@ async fn main() -> Result<(), Box> { // Kick it off. loop { select! { - line = stdin.select_next_some() => handle_input_line(&mut swarm.behaviour_mut().kademlia, line.expect("Stdin not to close")), + Ok(Some(line)) = stdin.next_line() => { + handle_input_line(&mut swarm.behaviour_mut().kademlia, line); + } event = swarm.select_next_some() => match event { SwarmEvent::NewListenAddr { address, .. } => { println!("Listening in {address:?}"); From 8387749048f9c922dc8a30e18a709eb82ec76503 Mon Sep 17 00:00:00 2001 From: Krishang Shah <93703995+kamuik16@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:17:50 +0530 Subject: [PATCH 36/36] chore: refactor ping tests (#5655) ## Description ref #4449 Refactored ping tests to use `tokio` instead of `async-std`. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --- Cargo.lock | 2 +- protocols/ping/Cargo.toml | 2 +- protocols/ping/src/protocol.rs | 34 ++++++++++++++++------------------ protocols/ping/tests/ping.rs | 16 ++++++++-------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3e2fc9fa47..c0ada652504 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3023,7 +3023,6 @@ dependencies = [ name = "libp2p-ping" version = "0.45.0" dependencies = [ - "async-std", "either", "futures", "futures-timer", @@ -3033,6 +3032,7 @@ dependencies = [ "libp2p-swarm-test", "quickcheck-ext", "rand 0.8.5", + "tokio", "tracing", "tracing-subscriber", "void", diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index 66775d3ba8d..794ab54ba42 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -23,11 +23,11 @@ tracing = { workspace = true } void = "1.0" [dev-dependencies] -async-std = "1.6.2" libp2p-swarm = { workspace = true, features = ["macros"] } libp2p-swarm-test = { path = "../../swarm-test" } quickcheck = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } +tokio = {workspace = true, features = ["rt", "macros"]} # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 6e3f06d0498..101c219aac4 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -89,8 +89,8 @@ mod tests { Endpoint, }; - #[test] - fn ping_pong() { + #[tokio::test] + async fn ping_pong() { let mem_addr = multiaddr![Memory(thread_rng().gen::())]; let mut transport = MemoryTransport::new().boxed(); transport.listen_on(ListenerId::next(), mem_addr).unwrap(); @@ -101,27 +101,25 @@ mod tests { .and_then(|ev| ev.into_new_address()) .expect("MemoryTransport not listening on an address!"); - async_std::task::spawn(async move { + tokio::spawn(async move { let transport_event = transport.next().await.unwrap(); let (listener_upgrade, _) = transport_event.into_incoming().unwrap(); let conn = listener_upgrade.await.unwrap(); recv_ping(conn).await.unwrap(); }); - async_std::task::block_on(async move { - let c = MemoryTransport::new() - .dial( - listener_addr, - DialOpts { - role: Endpoint::Dialer, - port_use: PortUse::Reuse, - }, - ) - .unwrap() - .await - .unwrap(); - let (_, rtt) = send_ping(c).await.unwrap(); - assert!(rtt > Duration::from_secs(0)); - }); + let c = MemoryTransport::new() + .dial( + listener_addr, + DialOpts { + role: Endpoint::Dialer, + port_use: PortUse::Reuse, + }, + ) + .unwrap() + .await + .unwrap(); + let (_, rtt) = send_ping(c).await.unwrap(); + assert!(rtt > Duration::from_secs(0)); } } diff --git a/protocols/ping/tests/ping.rs b/protocols/ping/tests/ping.rs index 3ca469f16a8..0752b1fced9 100644 --- a/protocols/ping/tests/ping.rs +++ b/protocols/ping/tests/ping.rs @@ -27,15 +27,15 @@ use libp2p_swarm_test::SwarmExt; use quickcheck::*; use std::{num::NonZeroU8, time::Duration}; -#[test] -fn ping_pong() { +#[tokio::test] +async fn ping_pong() { fn prop(count: NonZeroU8) { let cfg = ping::Config::new().with_interval(Duration::from_millis(10)); let mut swarm1 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone())); let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone())); - async_std::task::block_on(async { + tokio::spawn(async move { swarm1.listen().with_memory_addr_external().await; swarm2.connect(&mut swarm1).await; @@ -61,16 +61,16 @@ fn assert_ping_rtt_less_than_50ms(e: ping::Event) { assert!(rtt < Duration::from_millis(50)) } -#[test] -fn unsupported_doesnt_fail() { +#[tokio::test] +async fn unsupported_doesnt_fail() { let mut swarm1 = Swarm::new_ephemeral(|_| dummy::Behaviour); let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(ping::Config::new())); - let result = async_std::task::block_on(async { + let result = { swarm1.listen().with_memory_addr_external().await; swarm2.connect(&mut swarm1).await; let swarm1_peer_id = *swarm1.local_peer_id(); - async_std::task::spawn(swarm1.loop_on_next()); + tokio::spawn(swarm1.loop_on_next()); loop { match swarm2.next_swarm_event().await { @@ -89,7 +89,7 @@ fn unsupported_doesnt_fail() { _ => {} } } - }); + }; result.expect("node with ping should not fail connection due to unsupported protocol"); }