diff --git a/node/ferret-libp2p/src/behaviour.rs b/node/ferret-libp2p/src/behaviour.rs index 0ad2c81e69c0..73ad16db9a8e 100644 --- a/node/ferret-libp2p/src/behaviour.rs +++ b/node/ferret-libp2p/src/behaviour.rs @@ -3,17 +3,25 @@ use libp2p::core::identity::Keypair; use libp2p::core::PeerId; use libp2p::gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent, Topic, TopicHash}; use libp2p::mdns::{Mdns, MdnsEvent}; +use libp2p::ping::{ + handler::{PingFailure, PingSuccess}, + Ping, PingEvent, +}; use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess}; use libp2p::tokio_io::{AsyncRead, AsyncWrite}; use libp2p::NetworkBehaviour; +use slog::{debug, Logger}; #[derive(NetworkBehaviour)] #[behaviour(out_event = "MyBehaviourEvent", poll_method = "poll")] pub struct MyBehaviour { pub gossipsub: Gossipsub, pub mdns: Mdns, + pub ping: Ping, #[behaviour(ignore)] events: Vec, + #[behaviour(ignore)] + log: Logger, } pub enum MyBehaviourEvent { @@ -61,6 +69,41 @@ impl NetworkBehaviourEventProcess NetworkBehaviourEventProcess + for MyBehaviour +{ + fn inject_event(&mut self, event: PingEvent) { + match event.result { + Result::Ok(PingSuccess::Ping { rtt }) => { + debug!( + self.log, + "PingSuccess::Ping rtt to {} is {} ms", + event.peer.to_base58(), + rtt.as_millis() + ); + } + Result::Ok(PingSuccess::Pong) => { + debug!( + self.log, + "PingSuccess::Pong from {}", + event.peer.to_base58() + ); + } + Result::Err(PingFailure::Timeout) => { + debug!(self.log, "PingFailure::Timeout {}", event.peer.to_base58()); + } + Result::Err(PingFailure::Other { error }) => { + debug!( + self.log, + "PingFailure::Other {}: {}", + event.peer.to_base58(), + error + ); + } + } + } +} + impl MyBehaviour { /// Consumes the events list when polled. fn poll( @@ -74,12 +117,14 @@ impl MyBehaviour { } impl MyBehaviour { - pub fn new(local_key: &Keypair) -> Self { + pub fn new(log: Logger, local_key: &Keypair) -> Self { let local_peer_id = local_key.public().into_peer_id(); let gossipsub_config = GossipsubConfig::default(); MyBehaviour { gossipsub: Gossipsub::new(local_peer_id, gossipsub_config), mdns: Mdns::new().expect("Failed to create mDNS service"), + ping: Ping::default(), + log, events: vec![], } } diff --git a/node/ferret-libp2p/src/service.rs b/node/ferret-libp2p/src/service.rs index 4e8aeb2919a7..b45b467dffd1 100644 --- a/node/ferret-libp2p/src/service.rs +++ b/node/ferret-libp2p/src/service.rs @@ -27,7 +27,7 @@ impl Libp2pService { let transport = build_transport(local_key.clone()); let mut swarm = { - let be = MyBehaviour::new(&local_key); + let be = MyBehaviour::new(log.clone(), &local_key); Swarm::new(transport, be, local_peer_id) }; @@ -102,7 +102,7 @@ pub enum NetworkEvent { }, } -fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> { +pub fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> { let transport = libp2p::tcp::TcpConfig::new().nodelay(true); let transport = libp2p::dns::DnsConfig::new(transport);