Skip to content

Commit

Permalink
Remove Identify, add Ping
Browse files Browse the repository at this point in the history
Unfortunately we have to compose Ping into the mox until libp2p/rust-libp2p#2109 is fixed.
The ping interval is set to a very large value (roughly 100 years), so we don't spam nodes from the rendezvous node, but react to incoming pings.
I opted to not print the ping events because they spam the logs.
  • Loading branch information
da-kami authored and rishflab committed Jul 2, 2021
1 parent a624805 commit 0305a18
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
53 changes: 26 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2018"
[dependencies]
anyhow = "1"
futures = { version = "0.3", default-features = false }
libp2p = { git = "https://github.com/comit-network/rust-libp2p.git", rev = "96002105b0a7019330413826a332b3a12a7e8a57", default-features = false, features = [ "rendezvous", "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "identify" ] }
libp2p = { git = "https://github.com/comit-network/rust-libp2p.git", rev = "2e143a01aebbbf318231e7841884f9f34638c4e2", default-features = false, features = [ "rendezvous", "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "ping" ] }
structopt = { version = "0.3", default-features = false }
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] }
2 changes: 1 addition & 1 deletion examples/register_once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn main() -> Result<()> {
println!("Lost connection to rendezvous point {}", error);
}
// once `/identify` did its job, we know our external address and can register
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received { .. })) => {
SwarmEvent::Behaviour(Event::Ping(IdentifyEvent::Received { .. })) => {
swarm
.behaviour_mut()
.rendezvous
Expand Down
19 changes: 10 additions & 9 deletions src/bin/rendezvous_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use anyhow::Result;
use libp2p::core::identity::ed25519::SecretKey;
use libp2p::dns::TokioDnsConfig;
use libp2p::futures::StreamExt;
use libp2p::identify::{Identify, IdentifyConfig};
use libp2p::rendezvous::{Config, Rendezvous};
use libp2p::swarm::SwarmBuilder;
use libp2p::swarm::{SwarmBuilder, SwarmEvent};
use libp2p::tcp::TokioTcpConfig;
use libp2p::{identity, PeerId, Transport};
use rendezvous_server::transport::authenticate_and_multiplex;
use rendezvous_server::{parse_secret_key, Behaviour};
use rendezvous_server::{parse_secret_key, Behaviour, Event};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -29,15 +28,11 @@ async fn main() -> Result<()> {

let transport = authenticate_and_multiplex(tcp_with_dns.boxed(), &identity).unwrap();

let identify = Identify::new(IdentifyConfig::new(
"rendezvous/1.0.0".to_string(),
identity.public(),
));
let rendezvous = Rendezvous::new(identity.clone(), Config::default());

let peer_id = PeerId::from(identity.public());

let mut swarm = SwarmBuilder::new(transport, Behaviour::new(identify, rendezvous), peer_id)
let mut swarm = SwarmBuilder::new(transport, Behaviour::new(rendezvous), peer_id)
.executor(Box::new(|f| {
tokio::spawn(f);
}))
Expand All @@ -51,6 +46,12 @@ async fn main() -> Result<()> {

loop {
let event = swarm.next().await;
println!("swarm event: {:?}", event);

if let Some(event) = event {
match event {
SwarmEvent::Behaviour(Event::Ping(_)) => {}
event => println!("swarm event: {:?}", event),
}
}
}
}
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Result;
use libp2p::core::identity::ed25519::SecretKey;
use libp2p::identify::{Identify, IdentifyEvent};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::rendezvous::Rendezvous;
use libp2p::{rendezvous, NetworkBehaviour};
use std::time::Duration;

pub mod transport;

Expand All @@ -15,7 +16,7 @@ pub fn parse_secret_key(s: &str) -> Result<SecretKey> {
#[derive(Debug)]
pub enum Event {
Rendezvous(rendezvous::Event),
Identify(IdentifyEvent),
Ping(PingEvent),
}

impl From<rendezvous::Event> for Event {
Expand All @@ -24,24 +25,30 @@ impl From<rendezvous::Event> for Event {
}
}

impl From<IdentifyEvent> for Event {
fn from(event: IdentifyEvent) -> Self {
Event::Identify(event)
impl From<PingEvent> for Event {
fn from(event: PingEvent) -> Self {
Event::Ping(event)
}
}

#[derive(NetworkBehaviour)]
#[behaviour(event_process = false)]
#[behaviour(out_event = "Event")]
pub struct Behaviour {
identify: Identify,
ping: Ping,
pub rendezvous: Rendezvous,
}

impl Behaviour {
pub fn new(identify: Identify, rendezvous: Rendezvous) -> Self {
pub fn new(rendezvous: Rendezvous) -> Self {
Self {
identify,
// TODO: Remove Ping behaviour once https://github.com/libp2p/rust-libp2p/issues/2109 is fixed
// interval for sending Ping set to ~100 years
ping: Ping::new(
PingConfig::new()
.with_keep_alive(false)
.with_interval(Duration::from_secs(3_154_000_000)),
),
rendezvous,
}
}
Expand Down

0 comments on commit 0305a18

Please sign in to comment.