Skip to content

Commit 9228dc2

Browse files
authored
fix(identify): Remove peer from cache on DialError::WrongPeerId event
This PR handles the `DialError::WrongPeerID` event in `identify` protocol. Scenario: Consider a network with two nodes, N1 and N2, where N2 periodically initiates a connection/substream to N1. Now, if N1 is shut down and a new node, N3, starts up on the same transport address but with a different keypair, the identify protocol in N2 will continue to associate the old (PeerId, Multiaddr) pair in its cache. However, since N1 no longer exists and N3 has a different identity, this cached entry is now invalid. This PR ensures that the protocol properly detects and handles such situations, preventing stale peer information from causing connectivity issues. Pull-Request: #5890.
1 parent 09fa853 commit 9228dc2

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

protocols/identify/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Implement optional `signedPeerRecord` support for identify messages.
44
See [PR 5785](https://github.com/libp2p/rust-libp2p/pull/5785)
5+
- Fix `Identify::discovered_peers` to remove peers on `DialError::{WrongPeerId, LocalPeerId}` events.
6+
See [PR 5890](https://github.com/libp2p/rust-libp2p/pull/5890).
57

68
## 0.46.0
79

protocols/identify/src/behaviour.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub struct Config {
149149
/// How many entries of discovered peers to keep before we discard
150150
/// the least-recently used one.
151151
///
152-
/// Disabled by default.
152+
/// Defaults to 100
153153
cache_size: usize,
154154

155155
/// Whether to include our listen addresses in our responses. If enabled,
@@ -591,13 +591,24 @@ impl NetworkBehaviour for Behaviour {
591591
self.outbound_connections_with_ephemeral_port
592592
.remove(&connection_id);
593593
}
594-
FromSwarm::DialFailure(DialFailure { peer_id, error, .. }) => {
595-
if let (Some(peer_id), Some(cache), DialError::Transport(errors)) =
596-
(peer_id, self.discovered_peers.0.as_mut(), error)
597-
{
598-
for (addr, _error) in errors {
599-
cache.remove(&peer_id, addr);
600-
}
594+
FromSwarm::DialFailure(DialFailure {
595+
peer_id: Some(peer_id),
596+
error,
597+
..
598+
}) => {
599+
if let Some(cache) = self.discovered_peers.0.as_mut() {
600+
match error {
601+
DialError::Transport(errors) => {
602+
for (addr, _error) in errors {
603+
cache.remove(&peer_id, addr);
604+
}
605+
}
606+
DialError::WrongPeerId { address, .. }
607+
| DialError::LocalPeerId { address } => {
608+
cache.remove(&peer_id, address);
609+
}
610+
_ => (),
611+
};
601612
}
602613
}
603614
_ => {}

0 commit comments

Comments
 (0)