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

fix(rust): don't use failed identify messages to detect disconnection #93

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
20 changes: 0 additions & 20 deletions rust-peer/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 rust-peer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
86 changes: 34 additions & 52 deletions rust-peer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
Expand Down Expand Up @@ -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();
Expand Down