Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Close connections that failed on ping #289

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions node/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions node/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -216,6 +217,12 @@ async fn stops_services_when_network_is_compromised() {
}

fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender<BadEncodingFraudProof> {
#[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()
Expand All @@ -226,11 +233,14 @@ fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender<BadEncodingFraudP
)
.unwrap()
.with_behaviour(|key| {
let ping = ping::Behaviour::new(ping::Config::default());

let config = gossipsub::ConfigBuilder::default().build().unwrap();
let message_authenticity = gossipsub::MessageAuthenticity::Signed(key.clone());
let behaviour: gossipsub::Behaviour =
let gossipsub: gossipsub::Behaviour =
gossipsub::Behaviour::new(message_authenticity, config).unwrap();
Ok(behaviour)

Ok(Behaviour { ping, gossipsub })
})
.unwrap()
.build();
Expand All @@ -239,7 +249,11 @@ fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender<BadEncodingFraudP

// subscribe to the fraud-sub topic
let topic = gossipsub::IdentTopic::new("/badencoding/fraud-sub/private/v0.0.1");
announcer.behaviour_mut().subscribe(&topic).unwrap();
announcer
.behaviour_mut()
.gossipsub
.subscribe(&topic)
.unwrap();

// a channel for proof announcment
let (tx, mut rx) = mpsc::channel::<BadEncodingFraudProof>(8);
Expand All @@ -250,7 +264,7 @@ fn spawn_befp_announcer(connect_to: Multiaddr) -> mpsc::Sender<BadEncodingFraudP
_ = announcer.select_next_some() => (),
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();
}
}
}
Expand Down
Loading