Skip to content

Commit

Permalink
Merge branch 'master' of github.com:libp2p/rust-libp2p into identify-…
Browse files Browse the repository at this point in the history
…revise-symbol-naming
  • Loading branch information
jxs committed Oct 3, 2022
2 parents 2850c1e + 1da75b2 commit 5112915
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 168 deletions.
9 changes: 4 additions & 5 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ use libp2p::{
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
identify, identity,
multiaddr::Protocol,
noise,
ping::{self, PingEvent},
noise, ping,
pnet::{PnetConfig, PreSharedKey},
swarm::SwarmEvent,
tcp::TcpTransport,
Expand Down Expand Up @@ -164,7 +163,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
enum MyBehaviourEvent {
Gossipsub(GossipsubEvent),
Identify(identify::Event),
Ping(PingEvent),
Ping(ping::Event),
}

impl From<GossipsubEvent> for MyBehaviourEvent {
Expand All @@ -179,8 +178,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

impl From<PingEvent> for MyBehaviourEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyBehaviourEvent {
fn from(event: ping::Event) -> Self {
MyBehaviourEvent::Ping(event)
}
}
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/examples/metrics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn Error>> {

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,
);

Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ impl Recorder<libp2p_kad::KademliaEvent> for Metrics {
}

#[cfg(feature = "ping")]
impl Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
self.ping.record(event)
}
}
Expand Down
18 changes: 9 additions & 9 deletions misc/metrics/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -92,13 +92,13 @@ impl Metrics {
}
}

impl super::Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl super::Recorder<libp2p_ping::Event> 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) => {
Expand Down
13 changes: 6 additions & 7 deletions protocols/dcutr/examples/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;
Expand Down Expand Up @@ -108,21 +107,21 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay_client: Client,
ping: Ping,
ping: ping::Behaviour,
identify: identify::Behaviour,
dcutr: dcutr::behaviour::Behaviour,
}

#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(identify::Event),
Relay(client::Event),
Dcutr(dcutr::behaviour::Event),
}

impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}
Expand All @@ -147,7 +146,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let behaviour = Behaviour {
relay_client: client,
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(identify::Config::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
Expand Down
3 changes: 3 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# 0.40.0 [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 2937].

- Update to `libp2p-core` `v0.37.0`.

- Update to `libp2p-swarm` `v0.40.0`.

[PR 2857]: https://github.com/libp2p/rust-libp2p/pull/2857
[PR 2937]: https://github.com/libp2p/rust-libp2p/pull/2937

# 0.39.0

Expand Down
29 changes: 15 additions & 14 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -185,7 +185,7 @@ pub struct Handler {
/// Each successful ping resets this counter to 0.
failures: u32,
/// The outbound ping state.
outbound: Option<PingState>,
outbound: Option<OutboundState>,
/// 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.
Expand All @@ -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,
Expand Down Expand Up @@ -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(OutboundState::Ping(protocol::send_ping(stream).boxed()));
}

fn inject_event(&mut self, _: Void) {}
Expand Down Expand Up @@ -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(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(PingState::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(PingState::Idle(stream));
self.outbound = Some(OutboundState::Idle(stream));
return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping {
rtt,
})));
Expand All @@ -352,22 +352,23 @@ impl ConnectionHandler for Handler {
.push_front(Failure::Other { error: Box::new(e) });
}
},
Some(PingState::Idle(stream)) => match self.timer.poll_unpin(cx) {
Some(OutboundState::Idle(stream)) => match self.timer.poll_unpin(cx) {
Poll::Pending => {
self.outbound = Some(PingState::Idle(stream));
self.outbound = Some(OutboundState::Idle(stream));
break;
}
Poll::Ready(()) => {
self.timer.reset(self.config.timeout);
self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed()));
self.outbound =
Some(OutboundState::Ping(protocol::send_ping(stream).boxed()));
}
},
Some(PingState::OpenStream) => {
self.outbound = Some(PingState::OpenStream);
Some(OutboundState::OpenStream) => {
self.outbound = Some(OutboundState::OpenStream);
break;
}
None => {
self.outbound = Some(PingState::OpenStream);
self.outbound = Some(OutboundState::OpenStream);
let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ())
.with_timeout(self.config.timeout);
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
Expand All @@ -385,7 +386,7 @@ type PingFuture = BoxFuture<'static, Result<(NegotiatedSubstream, Duration), io:
type PongFuture = BoxFuture<'static, Result<NegotiatedSubstream, io::Error>>;

/// The current state w.r.t. outbound pings.
enum PingState {
enum OutboundState {
/// A new substream is being negotiated for the ping protocol.
OpenStream,
/// The substream is idle, waiting to send the next ping.
Expand Down
35 changes: 22 additions & 13 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Success, Failure>;
Expand Down
13 changes: 6 additions & 7 deletions protocols/relay/examples/relay_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ use futures::stream::StreamExt;
use libp2p::core::upgrade;
use libp2p::identify;
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};

Expand Down Expand Up @@ -59,7 +58,7 @@ fn main() -> Result<(), Box<dyn Error>> {

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::Behaviour::new(identify::Config::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
Expand Down Expand Up @@ -96,19 +95,19 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay: Relay,
ping: Ping,
ping: ping::Behaviour,
identify: identify::Behaviour,
}

#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(identify::Event),
Relay(relay::Event),
}

impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}
Expand Down
Loading

0 comments on commit 5112915

Please sign in to comment.