From a772e5ae17c0963873f76bb8cd65315f7768400b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sat, 24 Sep 2022 09:42:13 +1000 Subject: [PATCH 1/5] Properly deprecate types with `Ping` prefix --- examples/ipfs-private.rs | 9 ++++----- misc/metrics/src/lib.rs | 4 ++-- misc/metrics/src/ping.rs | 18 +++++++++--------- protocols/ping/CHANGELOG.md | 3 +++ protocols/ping/src/lib.rs | 29 +++++++++++++++++++---------- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/examples/ipfs-private.rs b/examples/ipfs-private.rs index b67bfa9009e..58dfa8ad0a9 100644 --- a/examples/ipfs-private.rs +++ b/examples/ipfs-private.rs @@ -42,8 +42,7 @@ use libp2p::{ identify::{Identify, IdentifyConfig, IdentifyEvent}, identity, multiaddr::Protocol, - noise, - ping::{self, PingEvent}, + noise, ping, pnet::{PnetConfig, PreSharedKey}, swarm::SwarmEvent, tcp::TcpTransport, @@ -165,7 +164,7 @@ async fn main() -> Result<(), Box> { enum MyBehaviourEvent { Gossipsub(GossipsubEvent), Identify(IdentifyEvent), - Ping(PingEvent), + Ping(ping::Event), } impl From for MyBehaviourEvent { @@ -180,8 +179,8 @@ async fn main() -> Result<(), Box> { } } - impl From for MyBehaviourEvent { - fn from(event: PingEvent) -> Self { + impl From for MyBehaviourEvent { + fn from(event: ping::Event) -> Self { MyBehaviourEvent::Ping(event) } } diff --git a/misc/metrics/src/lib.rs b/misc/metrics/src/lib.rs index d9fa3c40ffe..a13a1ff037e 100644 --- a/misc/metrics/src/lib.rs +++ b/misc/metrics/src/lib.rs @@ -126,8 +126,8 @@ impl Recorder for Metrics { } #[cfg(feature = "ping")] -impl Recorder for Metrics { - fn record(&self, event: &libp2p_ping::PingEvent) { +impl Recorder for Metrics { + fn record(&self, event: &libp2p_ping::Event) { self.ping.record(event) } } diff --git a/misc/metrics/src/ping.rs b/misc/metrics/src/ping.rs index b7c3ef60f9b..fbd89338cd5 100644 --- a/misc/metrics/src/ping.rs +++ b/misc/metrics/src/ping.rs @@ -29,16 +29,16 @@ struct FailureLabels { reason: Failure, } -impl From<&libp2p_ping::PingFailure> for FailureLabels { - fn from(failure: &libp2p_ping::PingFailure) -> Self { +impl From<&libp2p_ping::Failure> for FailureLabels { + fn from(failure: &libp2p_ping::Failure) -> Self { match failure { - libp2p_ping::PingFailure::Timeout => FailureLabels { + libp2p_ping::Failure::Timeout => FailureLabels { reason: Failure::Timeout, }, - libp2p_ping::PingFailure::Unsupported => FailureLabels { + libp2p_ping::Failure::Unsupported => FailureLabels { reason: Failure::Unsupported, }, - libp2p_ping::PingFailure::Other { .. } => FailureLabels { + libp2p_ping::Failure::Other { .. } => FailureLabels { reason: Failure::Other, }, } @@ -92,13 +92,13 @@ impl Metrics { } } -impl super::Recorder for Metrics { - fn record(&self, event: &libp2p_ping::PingEvent) { +impl super::Recorder for Metrics { + fn record(&self, event: &libp2p_ping::Event) { match &event.result { - Ok(libp2p_ping::PingSuccess::Pong) => { + Ok(libp2p_ping::Success::Pong) => { self.pong_received.inc(); } - Ok(libp2p_ping::PingSuccess::Ping { rtt }) => { + Ok(libp2p_ping::Success::Ping { rtt }) => { self.rtt.observe(rtt.as_secs_f64()); } Err(failure) => { diff --git a/protocols/ping/CHANGELOG.md b/protocols/ping/CHANGELOG.md index f7932c38b4f..942b30c969b 100644 --- a/protocols/ping/CHANGELOG.md +++ b/protocols/ping/CHANGELOG.md @@ -1,8 +1,11 @@ # 0.39.1 [unreleased] - Bump rand to 0.8 and quickcheck to 1. See [PR 2857]. +- Deprecate types with `Ping` prefix. Prefer importing them via the `ping` namespace, i.e. `libp2p::ping::Event` instead + of `libp2p::ping::PingEvent`. See [PR XXXX]. [PR 2857]: https://github.com/libp2p/rust-libp2p/pull/2857 +[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX # 0.39.0 diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 2a01025ee6d..01c9d30551a 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -52,16 +52,25 @@ use std::{ task::{Context, Poll}, }; -#[deprecated( - since = "0.30.0", - note = "Use re-exports that omit `Ping` prefix, i.e. `libp2p::ping::Config` etc" -)] -pub use self::{ - protocol::PROTOCOL_NAME, Config as PingConfig, Event as PingEvent, Failure as PingFailure, - Result as PingResult, Success as PingSuccess, -}; -#[deprecated(since = "0.30.0", note = "Use libp2p::ping::Behaviour instead.")] -pub use Behaviour as Ping; +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Config instead.")] +pub type PingConfig = Config; + +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Event instead.")] +pub type PingEvent = Event; + +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Success instead.")] +pub type PingSuccess = Success; + +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Failure instead.")] +pub type PingFailure = Failure; + +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Result instead.")] +pub type PingResult = Result; + +#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Behaviour instead.")] +pub type Ping = Behaviour; + +pub use self::protocol::PROTOCOL_NAME; /// The result of an inbound or outbound ping. pub type Result = std::result::Result; From 76feefb443857b09d310537ad57ad51457ae59ed Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 27 Sep 2022 22:03:52 +1000 Subject: [PATCH 2/5] Update protocols/ping/CHANGELOG.md Co-authored-by: Elena Frank --- protocols/ping/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/ping/CHANGELOG.md b/protocols/ping/CHANGELOG.md index 942b30c969b..ac25d64074d 100644 --- a/protocols/ping/CHANGELOG.md +++ b/protocols/ping/CHANGELOG.md @@ -2,10 +2,10 @@ - Bump rand to 0.8 and quickcheck to 1. See [PR 2857]. - Deprecate types with `Ping` prefix. Prefer importing them via the `ping` namespace, i.e. `libp2p::ping::Event` instead - of `libp2p::ping::PingEvent`. See [PR XXXX]. + of `libp2p::ping::PingEvent`. See [PR 2937]. [PR 2857]: https://github.com/libp2p/rust-libp2p/pull/2857 -[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX +[PR 2937]: https://github.com/libp2p/rust-libp2p/pull/2937 # 0.39.0 From ec6a8114b025ed0bc7c13c091a3a27896db4aef3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 28 Sep 2022 11:46:51 +1000 Subject: [PATCH 3/5] Remove deprecated ping usages from examples and docs --- misc/metrics/examples/metrics/main.rs | 4 +- protocols/dcutr/examples/client.rs | 13 ++-- protocols/ping/src/handler.rs | 4 +- protocols/ping/src/lib.rs | 6 +- protocols/relay/examples/relay_v2.rs | 13 ++-- protocols/relay/tests/v2.rs | 27 ++++----- protocols/rendezvous/examples/discover.rs | 18 +++--- protocols/rendezvous/examples/register.rs | 16 ++--- .../examples/register_with_identify.rs | 18 +++--- .../rendezvous/examples/rendezvous_point.rs | 11 ++-- src/tutorials/ping.rs | 34 +++++------ swarm-derive/tests/test.rs | 59 ++++++++++--------- swarm/src/behaviour.rs | 10 ++-- 13 files changed, 112 insertions(+), 121 deletions(-) diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index d4f491b4af3..51b0df8c2a4 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -52,7 +52,7 @@ use futures::executor::block_on; use futures::stream::StreamExt; use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; -use libp2p::ping::{Ping, PingConfig}; +use libp2p::ping; use libp2p::swarm::SwarmEvent; use libp2p::{identity, PeerId, Swarm}; use prometheus_client::registry::Registry; @@ -72,7 +72,7 @@ fn main() -> Result<(), Box> { let mut swarm = Swarm::new( block_on(libp2p::development_transport(local_key))?, - Ping::new(PingConfig::new().with_keep_alive(true)), + ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), local_peer_id, ); diff --git a/protocols/dcutr/examples/client.rs b/protocols/dcutr/examples/client.rs index e70e54cca3b..ac86b80b61e 100644 --- a/protocols/dcutr/examples/client.rs +++ b/protocols/dcutr/examples/client.rs @@ -25,15 +25,14 @@ use futures::stream::StreamExt; use libp2p::core::multiaddr::{Multiaddr, Protocol}; use libp2p::core::transport::OrTransport; use libp2p::core::upgrade; -use libp2p::dcutr; use libp2p::dns::DnsConfig; use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent, IdentifyInfo}; use libp2p::noise; -use libp2p::ping::{Ping, PingConfig, PingEvent}; use libp2p::relay::v2::client::{self, Client}; use libp2p::swarm::{SwarmBuilder, SwarmEvent}; use libp2p::tcp::{GenTcpConfig, TcpTransport}; use libp2p::Transport; +use libp2p::{dcutr, ping}; use libp2p::{identity, NetworkBehaviour, PeerId}; use log::info; use std::convert::TryInto; @@ -108,21 +107,21 @@ fn main() -> Result<(), Box> { #[behaviour(out_event = "Event", event_process = false)] struct Behaviour { relay_client: Client, - ping: Ping, + ping: ping::Behaviour, identify: Identify, dcutr: dcutr::behaviour::Behaviour, } #[derive(Debug)] enum Event { - Ping(PingEvent), + Ping(ping::Event), Identify(IdentifyEvent), Relay(client::Event), Dcutr(dcutr::behaviour::Event), } - impl From for Event { - fn from(e: PingEvent) -> Self { + impl From for Event { + fn from(e: ping::Event) -> Self { Event::Ping(e) } } @@ -147,7 +146,7 @@ fn main() -> Result<(), Box> { let behaviour = Behaviour { relay_client: client, - ping: Ping::new(PingConfig::new()), + ping: ping::Behaviour::new(ping::Config::new()), identify: Identify::new(IdentifyConfig::new( "/TODO/0.0.1".to_string(), local_key.public(), diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index f0e71fb070e..76f6a0cc8ba 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -56,7 +56,7 @@ pub struct Config { } impl Config { - /// Creates a new `PingConfig` with the following default settings: + /// Creates a new [`Config`] with the following default settings: /// /// * [`Config::with_interval`] 15s /// * [`Config::with_timeout`] 20s @@ -208,7 +208,7 @@ enum State { } impl Handler { - /// Builds a new `PingHandler` with the given configuration. + /// Builds a new [`Handler`] with the given configuration. pub fn new(config: Config) -> Self { Handler { config, diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 01c9d30551a..42234488964 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -26,16 +26,16 @@ //! //! # Usage //! -//! The [`Ping`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`], +//! The [`Behaviour`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`], //! it will respond to inbound ping requests and as necessary periodically send outbound //! ping requests on every established connection. If a configurable number of consecutive //! pings fail, the connection will be closed. //! -//! The `Ping` network behaviour produces [`PingEvent`]s, which may be consumed from the `Swarm` +//! The [`Behaviour`] network behaviour produces [`Event`]s, which may be consumed from the [`Swarm`] //! by an application, e.g. to collect statistics. //! //! > **Note**: The ping protocol does not keep otherwise idle connections alive -//! > by default, see [`PingConfig::with_keep_alive`] for changing this behaviour. +//! > by default, see [`Config::with_keep_alive`] for changing this behaviour. //! //! [`Swarm`]: libp2p_swarm::Swarm //! [`Transport`]: libp2p_core::Transport diff --git a/protocols/relay/examples/relay_v2.rs b/protocols/relay/examples/relay_v2.rs index 0e75b486ae1..c9346569d1e 100644 --- a/protocols/relay/examples/relay_v2.rs +++ b/protocols/relay/examples/relay_v2.rs @@ -25,13 +25,12 @@ use futures::stream::StreamExt; use libp2p::core::upgrade; use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent}; use libp2p::multiaddr::Protocol; -use libp2p::ping::{Ping, PingConfig, PingEvent}; use libp2p::relay::v2::relay::{self, Relay}; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::tcp::TcpTransport; -use libp2p::Transport; use libp2p::{identity, NetworkBehaviour, PeerId}; use libp2p::{noise, Multiaddr}; +use libp2p::{ping, Transport}; use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; @@ -59,7 +58,7 @@ fn main() -> Result<(), Box> { let behaviour = Behaviour { relay: Relay::new(local_peer_id, Default::default()), - ping: Ping::new(PingConfig::new()), + ping: ping::Behaviour::new(ping::Config::new()), identify: Identify::new(IdentifyConfig::new( "/TODO/0.0.1".to_string(), local_key.public(), @@ -96,19 +95,19 @@ fn main() -> Result<(), Box> { #[behaviour(out_event = "Event", event_process = false)] struct Behaviour { relay: Relay, - ping: Ping, + ping: ping::Behaviour, identify: Identify, } #[derive(Debug)] enum Event { - Ping(PingEvent), + Ping(ping::Event), Identify(IdentifyEvent), Relay(relay::Event), } -impl From for Event { - fn from(e: PingEvent) -> Self { +impl From for Event { + fn from(e: ping::Event) -> Self { Event::Ping(e) } } diff --git a/protocols/relay/tests/v2.rs b/protocols/relay/tests/v2.rs index 384e5e556ed..772d3bdd1df 100644 --- a/protocols/relay/tests/v2.rs +++ b/protocols/relay/tests/v2.rs @@ -29,12 +29,11 @@ use libp2p::core::transport::choice::OrTransport; use libp2p::core::transport::{Boxed, MemoryTransport, Transport}; use libp2p::core::PublicKey; use libp2p::core::{identity, upgrade, PeerId}; -use libp2p::ping::{Ping, PingConfig, PingEvent}; use libp2p::plaintext::PlainText2Config; use libp2p::relay::v2::client; use libp2p::relay::v2::relay; use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p::NetworkBehaviour; +use libp2p::{ping, NetworkBehaviour}; use std::time::Duration; #[test] @@ -217,7 +216,7 @@ fn connect() { match src.select_next_some().await { SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {} SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {} - SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. })) + SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. })) if peer == dst_peer_id => { break @@ -225,7 +224,7 @@ fn connect() { SwarmEvent::Behaviour(ClientEvent::Relay( client::Event::OutboundCircuitEstablished { .. }, )) => {} - SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. })) + SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. })) if peer == relay_peer_id => {} SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == dst_peer_id => { break @@ -299,7 +298,7 @@ fn build_relay() -> Swarm { Swarm::new( transport, Relay { - ping: Ping::new(PingConfig::new()), + ping: ping::Behaviour::new(ping::Config::new()), relay: relay::Relay::new( local_peer_id, relay::Config { @@ -326,7 +325,7 @@ fn build_client() -> Swarm { Swarm::new( transport, Client { - ping: Ping::new(PingConfig::new()), + ping: ping::Behaviour::new(ping::Config::new()), relay: behaviour, }, local_peer_id, @@ -351,13 +350,13 @@ where #[behaviour(out_event = "RelayEvent", event_process = false)] struct Relay { relay: relay::Relay, - ping: Ping, + ping: ping::Behaviour, } #[derive(Debug)] enum RelayEvent { Relay(relay::Event), - Ping(PingEvent), + Ping(ping::Event), } impl From for RelayEvent { @@ -366,8 +365,8 @@ impl From for RelayEvent { } } -impl From for RelayEvent { - fn from(event: PingEvent) -> Self { +impl From for RelayEvent { + fn from(event: ping::Event) -> Self { RelayEvent::Ping(event) } } @@ -376,13 +375,13 @@ impl From for RelayEvent { #[behaviour(out_event = "ClientEvent", event_process = false)] struct Client { relay: client::Client, - ping: Ping, + ping: ping::Behaviour, } #[derive(Debug)] enum ClientEvent { Relay(client::Event), - Ping(PingEvent), + Ping(ping::Event), } impl From for ClientEvent { @@ -391,8 +390,8 @@ impl From for ClientEvent { } } -impl From for ClientEvent { - fn from(event: PingEvent) -> Self { +impl From for ClientEvent { + fn from(event: ping::Event) -> Self { ClientEvent::Ping(event) } } diff --git a/protocols/rendezvous/examples/discover.rs b/protocols/rendezvous/examples/discover.rs index ceca71c6c4c..abe5dcc484a 100644 --- a/protocols/rendezvous/examples/discover.rs +++ b/protocols/rendezvous/examples/discover.rs @@ -22,7 +22,7 @@ use futures::StreamExt; use libp2p::core::identity; use libp2p::core::PeerId; use libp2p::multiaddr::Protocol; -use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess}; +use libp2p::ping; use libp2p::swarm::SwarmEvent; use libp2p::Swarm; use libp2p::{development_transport, rendezvous, Multiaddr}; @@ -44,8 +44,8 @@ async fn main() { development_transport(identity.clone()).await.unwrap(), MyBehaviour { rendezvous: rendezvous::client::Behaviour::new(identity.clone()), - ping: Ping::new( - PingConfig::new() + ping: ping::Behaviour::new( + ping::Config::new() .with_interval(Duration::from_secs(1)) .with_keep_alive(true), ), @@ -100,9 +100,9 @@ async fn main() { } } } - SwarmEvent::Behaviour(MyEvent::Ping(PingEvent { + SwarmEvent::Behaviour(MyEvent::Ping(ping::Event { peer, - result: Ok(PingSuccess::Ping { rtt }), + result: Ok(ping::Success::Ping { rtt }), })) if peer != rendezvous_point => { log::info!("Ping to {} is {}ms", peer, rtt.as_millis()) } @@ -124,7 +124,7 @@ async fn main() { #[derive(Debug)] enum MyEvent { Rendezvous(rendezvous::client::Event), - Ping(PingEvent), + Ping(ping::Event), } impl From for MyEvent { @@ -133,8 +133,8 @@ impl From for MyEvent { } } -impl From for MyEvent { - fn from(event: PingEvent) -> Self { +impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -144,5 +144,5 @@ impl From for MyEvent { #[behaviour(out_event = "MyEvent")] struct MyBehaviour { rendezvous: rendezvous::client::Behaviour, - ping: Ping, + ping: ping::Behaviour, } diff --git a/protocols/rendezvous/examples/register.rs b/protocols/rendezvous/examples/register.rs index 9c21874d50c..3fbfa02785d 100644 --- a/protocols/rendezvous/examples/register.rs +++ b/protocols/rendezvous/examples/register.rs @@ -21,7 +21,7 @@ use futures::StreamExt; use libp2p::core::identity; use libp2p::core::PeerId; -use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess}; +use libp2p::ping; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::{development_transport, rendezvous}; use libp2p::{Multiaddr, NetworkBehaviour}; @@ -43,7 +43,7 @@ async fn main() { development_transport(identity.clone()).await.unwrap(), MyBehaviour { rendezvous: rendezvous::client::Behaviour::new(identity.clone()), - ping: Ping::new(PingConfig::new().with_interval(Duration::from_secs(1))), + ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), }, PeerId::from(identity.public()), ); @@ -98,9 +98,9 @@ async fn main() { log::error!("Failed to register {}", error); return; } - SwarmEvent::Behaviour(MyEvent::Ping(PingEvent { + SwarmEvent::Behaviour(MyEvent::Ping(ping::Event { peer, - result: Ok(PingSuccess::Ping { rtt }), + result: Ok(ping::Success::Ping { rtt }), })) if peer != rendezvous_point => { log::info!("Ping to {} is {}ms", peer, rtt.as_millis()) } @@ -114,7 +114,7 @@ async fn main() { #[derive(Debug)] enum MyEvent { Rendezvous(rendezvous::client::Event), - Ping(PingEvent), + Ping(ping::Event), } impl From for MyEvent { @@ -123,8 +123,8 @@ impl From for MyEvent { } } -impl From for MyEvent { - fn from(event: PingEvent) -> Self { +impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -134,5 +134,5 @@ impl From for MyEvent { #[behaviour(out_event = "MyEvent")] struct MyBehaviour { rendezvous: rendezvous::client::Behaviour, - ping: Ping, + ping: ping::Behaviour, } diff --git a/protocols/rendezvous/examples/register_with_identify.rs b/protocols/rendezvous/examples/register_with_identify.rs index 3896db3e3d1..197620a8005 100644 --- a/protocols/rendezvous/examples/register_with_identify.rs +++ b/protocols/rendezvous/examples/register_with_identify.rs @@ -22,7 +22,7 @@ use futures::StreamExt; use libp2p::core::identity; use libp2p::core::PeerId; use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent}; -use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess}; +use libp2p::ping; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::{development_transport, rendezvous}; use libp2p::{Multiaddr, NetworkBehaviour}; @@ -47,8 +47,8 @@ async fn main() { identity.public(), )), rendezvous: rendezvous::client::Behaviour::new(identity.clone()), - ping: Ping::new( - PingConfig::new() + ping: ping::Behaviour::new( + ping::Config::new() .with_interval(Duration::from_secs(1)) .with_keep_alive(true), ), @@ -100,9 +100,9 @@ async fn main() { log::error!("Failed to register {}", error); return; } - SwarmEvent::Behaviour(MyEvent::Ping(PingEvent { + SwarmEvent::Behaviour(MyEvent::Ping(ping::Event { peer, - result: Ok(PingSuccess::Ping { rtt }), + result: Ok(ping::Success::Ping { rtt }), })) if peer != rendezvous_point => { log::info!("Ping to {} is {}ms", peer, rtt.as_millis()) } @@ -117,7 +117,7 @@ async fn main() { enum MyEvent { Rendezvous(rendezvous::client::Event), Identify(IdentifyEvent), - Ping(PingEvent), + Ping(ping::Event), } impl From for MyEvent { @@ -132,8 +132,8 @@ impl From for MyEvent { } } -impl From for MyEvent { - fn from(event: PingEvent) -> Self { +impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -144,5 +144,5 @@ impl From for MyEvent { struct MyBehaviour { identify: Identify, rendezvous: rendezvous::client::Behaviour, - ping: Ping, + ping: ping::Behaviour, } diff --git a/protocols/rendezvous/examples/rendezvous_point.rs b/protocols/rendezvous/examples/rendezvous_point.rs index 9d5ee6ca7db..450fa1e2061 100644 --- a/protocols/rendezvous/examples/rendezvous_point.rs +++ b/protocols/rendezvous/examples/rendezvous_point.rs @@ -25,7 +25,6 @@ use libp2p::identify::Identify; use libp2p::identify::IdentifyConfig; use libp2p::identify::IdentifyEvent; use libp2p::ping; -use libp2p::ping::{Ping, PingEvent}; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::NetworkBehaviour; use libp2p::{development_transport, rendezvous}; @@ -54,7 +53,7 @@ async fn main() { identity.public(), )), rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()), - ping: Ping::new(ping::Config::new().with_keep_alive(true)), + ping: ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), }, PeerId::from(identity.public()), ); @@ -104,7 +103,7 @@ async fn main() { #[derive(Debug)] enum MyEvent { Rendezvous(rendezvous::server::Event), - Ping(PingEvent), + Ping(ping::Event), Identify(IdentifyEvent), } @@ -114,8 +113,8 @@ impl From for MyEvent { } } -impl From for MyEvent { - fn from(event: PingEvent) -> Self { +impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -132,5 +131,5 @@ impl From for MyEvent { struct MyBehaviour { identify: Identify, rendezvous: rendezvous::server::Behaviour, - ping: Ping, + ping: ping::Behaviour, } diff --git a/src/tutorials/ping.rs b/src/tutorials/ping.rs index f33d90015bf..ce982861d06 100644 --- a/src/tutorials/ping.rs +++ b/src/tutorials/ping.rs @@ -127,12 +127,11 @@ //! _what_ bytes to send on the network. //! //! To make this more concrete, let's take a look at a simple implementation of -//! the [`NetworkBehaviour`] trait: the [`Ping`](crate::ping::Ping) -//! [`NetworkBehaviour`]. As you might have guessed, similar to the good old -//! `ping` network tool, libp2p [`Ping`](crate::ping::Ping) sends a ping to a -//! peer and expects to receive a pong in turn. The -//! [`Ping`](crate::ping::Ping) [`NetworkBehaviour`] does not care _how_ the -//! ping and pong messages are sent on the network, whether they are sent via +//! the [`NetworkBehaviour`] trait: the [`ping::Behaviour`](crate::ping::Behaviour). +//! As you might have guessed, similar to the good old `ping` network tool, +//! libp2p [`ping::Behaviour`](crate::ping::Behaviour) sends a ping to a peer and expects +//! to receive a pong in turn. The [`ping::Behaviour`](crate::ping::Behaviour) does not care _how_ +//! the ping and pong messages are sent on the network, whether they are sent via //! TCP, whether they are encrypted via [noise](crate::noise) or just in //! [plaintext](crate::plaintext). It only cares about _what_ messages are sent //! on the network. @@ -140,12 +139,10 @@ //! The two traits [`Transport`] and [`NetworkBehaviour`] allow us to cleanly //! separate _how_ to send bytes from _what_ bytes to send. //! -//! With the above in mind, let's extend our example, creating a -//! [`Ping`](crate::ping::Ping) [`NetworkBehaviour`] at the end: +//! With the above in mind, let's extend our example, creating a [`ping::Behaviour`](crate::ping::Behaviour) at the end: //! //! ```rust -//! use libp2p::{identity, PeerId}; -//! use libp2p::ping::{Ping, PingConfig}; +//! use libp2p::{identity, PeerId, ping}; //! use std::error::Error; //! //! #[async_std::main] @@ -161,7 +158,7 @@ //! // For illustrative purposes, the ping protocol is configured to //! // keep the connection alive, so a continuous sequence of pings //! // can be observed. -//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true)); +//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true)); //! //! Ok(()) //! } @@ -177,8 +174,7 @@ //! [`Transport`] to the [`NetworkBehaviour`]. //! //! ```rust -//! use libp2p::{identity, PeerId}; -//! use libp2p::ping::{Ping, PingConfig}; +//! use libp2p::{identity, PeerId, ping}; //! use libp2p::swarm::Swarm; //! use std::error::Error; //! @@ -195,7 +191,7 @@ //! // For illustrative purposes, the ping protocol is configured to //! // keep the connection alive, so a continuous sequence of pings //! // can be observed. -//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true)); +//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true)); //! //! let mut swarm = Swarm::new(transport, behaviour, local_peer_id); //! @@ -230,8 +226,7 @@ //! remote peer. //! //! ```rust -//! use libp2p::{identity, Multiaddr, PeerId}; -//! use libp2p::ping::{Ping, PingConfig}; +//! use libp2p::{identity, Multiaddr, PeerId, ping}; //! use libp2p::swarm::{Swarm, dial_opts::DialOpts}; //! use std::error::Error; //! @@ -248,7 +243,7 @@ //! // For illustrative purposes, the ping protocol is configured to //! // keep the connection alive, so a continuous sequence of pings //! // can be observed. -//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true)); +//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true)); //! //! let mut swarm = Swarm::new(transport, behaviour, local_peer_id); //! @@ -276,9 +271,8 @@ //! //! ```no_run //! use futures::prelude::*; -//! use libp2p::ping::{Ping, PingConfig}; //! use libp2p::swarm::{Swarm, SwarmEvent, dial_opts::DialOpts}; -//! use libp2p::{identity, Multiaddr, PeerId}; +//! use libp2p::{identity, Multiaddr, PeerId, ping}; //! use std::error::Error; //! //! #[async_std::main] @@ -294,7 +288,7 @@ //! // For illustrative purposes, the ping protocol is configured to //! // keep the connection alive, so a continuous sequence of pings //! // can be observed. -//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true)); +//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true)); //! //! let mut swarm = Swarm::new(transport, behaviour, local_peer_id); //! diff --git a/swarm-derive/tests/test.rs b/swarm-derive/tests/test.rs index e0f77eefd30..1f1fe39e0a8 100644 --- a/swarm-derive/tests/test.rs +++ b/swarm-derive/tests/test.rs @@ -19,6 +19,7 @@ // DEALINGS IN THE SOFTWARE. use futures::prelude::*; +use libp2p::ping; use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p_swarm_derive::*; use std::fmt::Debug; @@ -40,7 +41,7 @@ fn one_field() { #[allow(dead_code)] #[derive(NetworkBehaviour)] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, } #[allow(dead_code)] @@ -48,7 +49,7 @@ fn one_field() { fn foo() { let _out_event: ::OutEvent = unimplemented!(); match _out_event { - FooEvent::Ping(libp2p::ping::Event { .. }) => {} + FooEvent::Ping(ping::Event { .. }) => {} } } } @@ -58,7 +59,7 @@ fn two_fields() { #[allow(dead_code)] #[derive(NetworkBehaviour)] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, identify: libp2p::identify::Identify, } @@ -67,7 +68,7 @@ fn two_fields() { fn foo() { let _out_event: ::OutEvent = unimplemented!(); match _out_event { - FooEvent::Ping(libp2p::ping::Event { .. }) => {} + FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { let _: libp2p::identify::IdentifyEvent = event; } @@ -80,7 +81,7 @@ fn three_fields() { #[allow(dead_code)] #[derive(NetworkBehaviour)] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, identify: libp2p::identify::Identify, kad: libp2p::kad::Kademlia, } @@ -90,7 +91,7 @@ fn three_fields() { fn foo() { let _out_event: ::OutEvent = unimplemented!(); match _out_event { - FooEvent::Ping(libp2p::ping::Event { .. }) => {} + FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { let _: libp2p::identify::IdentifyEvent = event; } @@ -107,17 +108,17 @@ fn custom_event() { #[derive(NetworkBehaviour)] #[behaviour(out_event = "MyEvent")] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, identify: libp2p::identify::Identify, } enum MyEvent { - Ping(libp2p::ping::PingEvent), + Ping(ping::Event), Identify(libp2p::identify::IdentifyEvent), } - impl From for MyEvent { - fn from(event: libp2p::ping::PingEvent) -> Self { + impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -140,17 +141,17 @@ fn custom_event_mismatching_field_names() { #[derive(NetworkBehaviour)] #[behaviour(out_event = "MyEvent")] struct Foo { - a: libp2p::ping::Ping, + a: ping::Behaviour, b: libp2p::identify::Identify, } enum MyEvent { - Ping(libp2p::ping::PingEvent), + Ping(ping::Event), Identify(libp2p::identify::IdentifyEvent), } - impl From for MyEvent { - fn from(event: libp2p::ping::PingEvent) -> Self { + impl From for MyEvent { + fn from(event: ping::Event) -> Self { MyEvent::Ping(event) } } @@ -175,7 +176,7 @@ fn bound() { where ::OutEvent: Debug, { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, bar: T, } } @@ -189,7 +190,7 @@ fn where_clause() { T: Copy + NetworkBehaviour, ::OutEvent: Debug, { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, bar: T, } } @@ -199,7 +200,7 @@ fn nested_derives_with_import() { #[allow(dead_code)] #[derive(NetworkBehaviour)] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, } #[allow(dead_code)] @@ -213,7 +214,7 @@ fn nested_derives_with_import() { fn foo() { let _out_event: ::OutEvent = unimplemented!(); match _out_event { - BarEvent::Foo(FooEvent::Ping(libp2p::ping::Event { .. })) => {} + BarEvent::Foo(FooEvent::Ping(ping::Event { .. })) => {} } } } @@ -221,12 +222,12 @@ fn nested_derives_with_import() { #[test] fn custom_event_emit_event_through_poll() { enum BehaviourOutEvent { - Ping(libp2p::ping::PingEvent), + Ping(ping::Event), Identify(libp2p::identify::IdentifyEvent), } - impl From for BehaviourOutEvent { - fn from(event: libp2p::ping::PingEvent) -> Self { + impl From for BehaviourOutEvent { + fn from(event: ping::Event) -> Self { BehaviourOutEvent::Ping(event) } } @@ -241,7 +242,7 @@ fn custom_event_emit_event_through_poll() { #[derive(NetworkBehaviour)] #[behaviour(out_event = "BehaviourOutEvent")] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, identify: libp2p::identify::Identify, } @@ -272,7 +273,7 @@ fn with_toggle() { #[derive(NetworkBehaviour)] struct Foo { identify: libp2p::identify::Identify, - ping: Toggle, + ping: Toggle, } #[allow(dead_code)] @@ -289,7 +290,7 @@ fn with_either() { #[derive(NetworkBehaviour)] struct Foo { kad: libp2p::kad::Kademlia, - ping_or_identify: Either, + ping_or_identify: Either, } #[allow(dead_code)] @@ -304,7 +305,7 @@ fn custom_event_with_either() { enum BehaviourOutEvent { Kad(libp2p::kad::KademliaEvent), - PingOrIdentify(Either), + PingOrIdentify(Either), } impl From for BehaviourOutEvent { @@ -313,8 +314,8 @@ fn custom_event_with_either() { } } - impl From> for BehaviourOutEvent { - fn from(event: Either) -> Self { + impl From> for BehaviourOutEvent { + fn from(event: Either) -> Self { BehaviourOutEvent::PingOrIdentify(event) } } @@ -324,7 +325,7 @@ fn custom_event_with_either() { #[behaviour(out_event = "BehaviourOutEvent")] struct Foo { kad: libp2p::kad::Kademlia, - ping_or_identify: Either, + ping_or_identify: Either, } #[allow(dead_code)] @@ -338,7 +339,7 @@ fn generated_out_event_derive_debug() { #[allow(dead_code)] #[derive(NetworkBehaviour)] struct Foo { - ping: libp2p::ping::Ping, + ping: ping::Behaviour, } fn require_debug() diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index a5bf0e06f06..d5e8ec5b89a 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -91,18 +91,18 @@ pub(crate) type THandlerOutEvent = /// /// ``` rust /// # use libp2p::identify::{Identify, IdentifyEvent}; -/// # use libp2p::ping::{Ping, PingEvent}; +/// # use libp2p::ping; /// # use libp2p::NetworkBehaviour; /// #[derive(NetworkBehaviour)] /// #[behaviour(out_event = "Event")] /// struct MyBehaviour { /// identify: Identify, -/// ping: Ping, +/// ping: ping::Behaviour, /// } /// /// enum Event { /// Identify(IdentifyEvent), -/// Ping(PingEvent), +/// Ping(ping::Event), /// } /// /// impl From for Event { @@ -111,8 +111,8 @@ pub(crate) type THandlerOutEvent = /// } /// } /// -/// impl From for Event { -/// fn from(event: PingEvent) -> Self { +/// impl From for Event { +/// fn from(event: ping::Event) -> Self { /// Self::Ping(event) /// } /// } From 8a24e0632e334510b61fd4ce6a160ed867f2f2fd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 30 Sep 2022 16:02:13 +1000 Subject: [PATCH 4/5] Rename `PingState` --- protocols/ping/src/handler.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 76f6a0cc8ba..63209b08328 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -185,7 +185,7 @@ pub struct Handler { /// Each successful ping resets this counter to 0. failures: u32, /// The outbound ping state. - outbound: Option, + outbound: Option, /// The inbound pong handler, i.e. if there is an inbound /// substream, this is always a future that waits for the /// next inbound ping to be answered. @@ -241,7 +241,7 @@ impl ConnectionHandler for Handler { fn inject_fully_negotiated_outbound(&mut self, stream: NegotiatedSubstream, (): ()) { self.timer.reset(self.config.timeout); - self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed())); + self.outbound = Some(State::Ping(protocol::send_ping(stream).boxed())); } fn inject_event(&mut self, _: Void) {} @@ -330,19 +330,19 @@ impl ConnectionHandler for Handler { // Continue outbound pings. match self.outbound.take() { - Some(PingState::Ping(mut ping)) => match ping.poll_unpin(cx) { + Some(State::Ping(mut ping)) => match ping.poll_unpin(cx) { Poll::Pending => { if self.timer.poll_unpin(cx).is_ready() { self.pending_errors.push_front(Failure::Timeout); } else { - self.outbound = Some(PingState::Ping(ping)); + self.outbound = Some(State::Ping(ping)); break; } } Poll::Ready(Ok((stream, rtt))) => { self.failures = 0; self.timer.reset(self.config.interval); - self.outbound = Some(PingState::Idle(stream)); + self.outbound = Some(State::Idle(stream)); return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping { rtt, }))); @@ -352,22 +352,22 @@ impl ConnectionHandler for Handler { .push_front(Failure::Other { error: Box::new(e) }); } }, - Some(PingState::Idle(stream)) => match self.timer.poll_unpin(cx) { + Some(State::Idle(stream)) => match self.timer.poll_unpin(cx) { Poll::Pending => { - self.outbound = Some(PingState::Idle(stream)); + self.outbound = Some(State::Idle(stream)); break; } Poll::Ready(()) => { self.timer.reset(self.config.timeout); - self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed())); + self.outbound = Some(State::Ping(protocol::send_ping(stream).boxed())); } }, - Some(PingState::OpenStream) => { - self.outbound = Some(PingState::OpenStream); + Some(State::OpenStream) => { + self.outbound = Some(State::OpenStream); break; } None => { - self.outbound = Some(PingState::OpenStream); + self.outbound = Some(State::OpenStream); let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ()) .with_timeout(self.config.timeout); return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { @@ -385,7 +385,7 @@ type PingFuture = BoxFuture<'static, Result<(NegotiatedSubstream, Duration), io: type PongFuture = BoxFuture<'static, Result>; /// The current state w.r.t. outbound pings. -enum PingState { +enum State { /// A new substream is being negotiated for the ping protocol. OpenStream, /// The substream is idle, waiting to send the next ping. From 68c18ad003a09decf5daa33f451a416d41ff00bb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 30 Sep 2022 16:39:12 +1000 Subject: [PATCH 5/5] Fix clash in naming --- protocols/ping/src/handler.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 63209b08328..d2176434c58 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -185,7 +185,7 @@ pub struct Handler { /// Each successful ping resets this counter to 0. failures: u32, /// The outbound ping state. - outbound: Option, + outbound: Option, /// The inbound pong handler, i.e. if there is an inbound /// substream, this is always a future that waits for the /// next inbound ping to be answered. @@ -241,7 +241,7 @@ impl ConnectionHandler for Handler { fn inject_fully_negotiated_outbound(&mut self, stream: NegotiatedSubstream, (): ()) { self.timer.reset(self.config.timeout); - self.outbound = Some(State::Ping(protocol::send_ping(stream).boxed())); + self.outbound = Some(OutboundState::Ping(protocol::send_ping(stream).boxed())); } fn inject_event(&mut self, _: Void) {} @@ -330,19 +330,19 @@ impl ConnectionHandler for Handler { // Continue outbound pings. match self.outbound.take() { - Some(State::Ping(mut ping)) => match ping.poll_unpin(cx) { + Some(OutboundState::Ping(mut ping)) => match ping.poll_unpin(cx) { Poll::Pending => { if self.timer.poll_unpin(cx).is_ready() { self.pending_errors.push_front(Failure::Timeout); } else { - self.outbound = Some(State::Ping(ping)); + self.outbound = Some(OutboundState::Ping(ping)); break; } } Poll::Ready(Ok((stream, rtt))) => { self.failures = 0; self.timer.reset(self.config.interval); - self.outbound = Some(State::Idle(stream)); + self.outbound = Some(OutboundState::Idle(stream)); return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping { rtt, }))); @@ -352,22 +352,23 @@ impl ConnectionHandler for Handler { .push_front(Failure::Other { error: Box::new(e) }); } }, - Some(State::Idle(stream)) => match self.timer.poll_unpin(cx) { + Some(OutboundState::Idle(stream)) => match self.timer.poll_unpin(cx) { Poll::Pending => { - self.outbound = Some(State::Idle(stream)); + self.outbound = Some(OutboundState::Idle(stream)); break; } Poll::Ready(()) => { self.timer.reset(self.config.timeout); - self.outbound = Some(State::Ping(protocol::send_ping(stream).boxed())); + self.outbound = + Some(OutboundState::Ping(protocol::send_ping(stream).boxed())); } }, - Some(State::OpenStream) => { - self.outbound = Some(State::OpenStream); + Some(OutboundState::OpenStream) => { + self.outbound = Some(OutboundState::OpenStream); break; } None => { - self.outbound = Some(State::OpenStream); + self.outbound = Some(OutboundState::OpenStream); let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ()) .with_timeout(self.config.timeout); return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { @@ -385,7 +386,7 @@ type PingFuture = BoxFuture<'static, Result<(NegotiatedSubstream, Duration), io: type PongFuture = BoxFuture<'static, Result>; /// The current state w.r.t. outbound pings. -enum State { +enum OutboundState { /// A new substream is being negotiated for the ping protocol. OpenStream, /// The substream is idle, waiting to send the next ping.