diff --git a/node/src/p2p.rs b/node/src/p2p.rs index d7106a95..85a69269 100644 --- a/node/src/p2p.rs +++ b/node/src/p2p.rs @@ -676,9 +676,8 @@ where BehaviourEvent::Gossipsub(ev) => self.on_gossip_sub_event(ev).await, BehaviourEvent::Kademlia(ev) => self.on_kademlia_event(ev).await?, BehaviourEvent::Bitswap(ev) => self.on_bitswap_event(ev).await, - BehaviourEvent::Autonat(_) - | BehaviourEvent::Ping(_) - | BehaviourEvent::HeaderEx(_) => {} + BehaviourEvent::Ping(ev) => self.on_ping_event(ev).await, + BehaviourEvent::Autonat(_) | BehaviourEvent::HeaderEx(_) => {} }, SwarmEvent::ConnectionEstablished { peer_id, @@ -861,6 +860,23 @@ where } } + #[instrument(level = "debug", skip_all)] + async fn on_ping_event(&mut self, ev: ping::Event) { + match ev.result { + Ok(dur) => debug!( + "Ping success: peer: {}, connection_id: {}, time: {:?}", + ev.peer, ev.connection, dur + ), + Err(e) => { + debug!( + "Ping failure: peer: {}, connection_id: {}, error: {}", + &ev.peer, &ev.connection, e + ); + self.swarm.close_connection(ev.connection); + } + } + } + #[instrument(skip_all, fields(peer_id = %peer_id))] fn peer_maybe_discovered(&mut self, peer_id: PeerId) { if !self.peer_tracker.set_maybe_discovered(peer_id) { diff --git a/node/tests/node.rs b/node/tests/node.rs index 1f40fc86..0c277ee4 100644 --- a/node/tests/node.rs +++ b/node/tests/node.rs @@ -8,7 +8,8 @@ use celestia_types::fraud_proof::BadEncodingFraudProof; use celestia_types::hash::Hash; use celestia_types::test_utils::{corrupt_eds, generate_eds, ExtendedHeaderGenerator}; use futures::StreamExt; -use libp2p::{gossipsub, identity, noise, tcp, yamux, Multiaddr, SwarmBuilder}; +use libp2p::swarm::NetworkBehaviour; +use libp2p::{gossipsub, identity, noise, ping, tcp, yamux, Multiaddr, SwarmBuilder}; use lumina_node::node::{Node, NodeConfig}; use lumina_node::store::{InMemoryStore, Store}; use lumina_node::test_utils::{ @@ -216,6 +217,12 @@ async fn stops_services_when_network_is_compromised() { } fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender { + #[derive(NetworkBehaviour)] + struct Behaviour { + ping: ping::Behaviour, + gossipsub: gossipsub::Behaviour, + } + // create a new libp2p node with gossipsub let mut announcer = SwarmBuilder::with_new_identity() .with_tokio() @@ -226,11 +233,14 @@ fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender mpsc::Sender(8); @@ -250,7 +264,7 @@ fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender (), Some(proof) = rx.recv() => { let proof = proof.encode_vec().unwrap(); - announcer.behaviour_mut().publish(topic.hash(), proof).unwrap(); + announcer.behaviour_mut().gossipsub.publish(topic.hash(), proof).unwrap(); } } }