diff --git a/rust-peer/Cargo.lock b/rust-peer/Cargo.lock index e2af46ee..399c8db5 100644 --- a/rust-peer/Cargo.lock +++ b/rust-peer/Cargo.lock @@ -1596,7 +1596,6 @@ dependencies = [ "libp2p-kad", "libp2p-mdns", "libp2p-metrics", - "libp2p-ping", "libp2p-quic", "libp2p-relay", "libp2p-request-response", @@ -1808,7 +1807,6 @@ dependencies = [ "libp2p-identify", "libp2p-identity", "libp2p-kad", - "libp2p-ping", "libp2p-relay", "libp2p-swarm", "once_cell", @@ -1840,24 +1838,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "libp2p-ping" -version = "0.43.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e702d75cd0827dfa15f8fd92d15b9932abe38d10d21f47c50438c71dd1b5dae3" -dependencies = [ - "either", - "futures", - "futures-timer", - "instant", - "libp2p-core", - "libp2p-identity", - "libp2p-swarm", - "log", - "rand", - "void", -] - [[package]] name = "libp2p-quic" version = "0.9.2" diff --git a/rust-peer/Cargo.toml b/rust-peer/Cargo.toml index aef963e5..fbe8522d 100644 --- a/rust-peer/Cargo.toml +++ b/rust-peer/Cargo.toml @@ -11,7 +11,7 @@ clap = { version = "4.1.11", features = ["derive"] } env_logger = "0.10.0" futures = "0.3.27" futures-timer = "3.0.2" -libp2p = { version = "0.52.3", features = ["identify", "ping", "tokio", "gossipsub", "macros", "relay", "kad", "rsa", "ed25519", "quic", "request-response", "dns"] } +libp2p = { version = "0.52.3", features = ["identify", "tokio", "gossipsub", "macros", "relay", "kad", "rsa", "ed25519", "quic", "request-response", "dns"] } libp2p-webrtc = { version = "0.6.0-alpha", features = ["tokio", "pem"] } log = "0.4.17" rand = "0.8.5" diff --git a/rust-peer/src/main.rs b/rust-peer/src/main.rs index 35a78896..6ff7aec2 100644 --- a/rust-peer/src/main.rs +++ b/rust-peer/src/main.rs @@ -158,54 +158,36 @@ async fn main() -> Result<()> { )) => { debug!("{peer_id} subscribed to {topic}"); } - SwarmEvent::Behaviour(BehaviourEvent::Identify(e)) => { - info!("BehaviourEvent::Identify {:?}", e); - - if let identify::Event::Error { peer_id, error } = e { - match error { - libp2p::swarm::StreamUpgradeError::Timeout => { - // When a browser tab closes, we don't get a swarm event - // maybe there's a way to get this with TransportEvent - // but for now remove the peer from routing table if there's an Identify timeout - swarm.behaviour_mut().kademlia.remove_peer(&peer_id); - info!("Removed {peer_id} from the routing table (if it was in there)."); - } - _ => { - debug!("{error}"); - } - } - } else if let identify::Event::Received { - peer_id, - info: - identify::Info { - listen_addrs, - protocols, - observed_addr, - .. - }, - } = e - { - debug!("identify::Event::Received observed_addr: {}", observed_addr); - - swarm.add_external_address(observed_addr); - - // TODO: The following should no longer be necessary after https://github.com/libp2p/rust-libp2p/pull/4371. - if protocols.iter().any(|p| p == &KADEMLIA_PROTOCOL_NAME) { - for addr in listen_addrs { - debug!("identify::Event::Received listen addr: {}", addr); - // TODO (fixme): the below doesn't work because the address is still missing /webrtc/p2p even after https://github.com/libp2p/js-libp2p-webrtc/pull/121 - // swarm.behaviour_mut().kademlia.add_address(&peer_id, addr); - - let webrtc_address = addr - .with(Protocol::WebRTCDirect) - .with(Protocol::P2p(peer_id)); - - swarm - .behaviour_mut() - .kademlia - .add_address(&peer_id, webrtc_address.clone()); - info!("Added {webrtc_address} to the routing table."); - } + SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { + peer_id, + info: + identify::Info { + listen_addrs, + protocols, + observed_addr, + .. + }, + })) => { + debug!("identify::Event::Received observed_addr: {}", observed_addr); + + swarm.add_external_address(observed_addr); + + // TODO: The following should no longer be necessary after https://github.com/libp2p/rust-libp2p/pull/4371. + if protocols.iter().any(|p| p == &KADEMLIA_PROTOCOL_NAME) { + for addr in listen_addrs { + debug!("identify::Event::Received listen addr: {}", addr); + // TODO (fixme): the below doesn't work because the address is still missing /webrtc/p2p even after https://github.com/libp2p/js-libp2p-webrtc/pull/121 + // swarm.behaviour_mut().kademlia.add_address(&peer_id, addr); + + let webrtc_address = addr + .with(Protocol::WebRTCDirect) + .with(Protocol::P2p(peer_id)); + + swarm + .behaviour_mut() + .kademlia + .add_address(&peer_id, webrtc_address.clone()); + info!("Added {webrtc_address} to the routing table."); } } } @@ -328,10 +310,10 @@ fn create_swarm( dns::TokioDnsConfig::system(mapped)?.boxed() }; - let identify_config = identify::Behaviour::new( - identify::Config::new("/ipfs/0.1.0".into(), local_key.public()) - .with_interval(Duration::from_secs(60)), // do this so we can get timeouts for dropped WebRTC connections - ); + let identify_config = identify::Behaviour::new(identify::Config::new( + "/ipfs/0.1.0".into(), + local_key.public(), + )); // Create a Kademlia behaviour. let mut cfg = KademliaConfig::default();