diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 90142f52cf60..2b17ec23ab85 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1993,38 +1993,6 @@ where } } - fn on_new_address_event( - &mut self, - NewExternalAddrOfPeer { addr, peer_id }: NewExternalAddrOfPeer, - ) { - let key = kbucket::Key::from(peer_id.clone()); - - match self.kbuckets.entry(&key) { - kbucket::Entry::Present(mut entry, _node_status) => { - if entry.value().insert(addr.clone()) { - self.queued_events.push_back(ToSwarm::GenerateEvent( - KademliaEvent::RoutingUpdated { - peer: *peer_id, - is_new_peer: false, - addresses: entry.value().clone(), - old_peer: None, - bucket_range: self - .kbuckets - .bucket(&key) - .map(|b| b.range()) - .expect("Not kbucket::Entry::SelfEntry."), - }, - )) - } - } - kbucket::Entry::Pending(mut entry, _node_status) => { - entry.value().insert(addr.clone()); - () - } - kbucket::Entry::Absent(_) | kbucket::Entry::SelfEntry => {} - } - } - fn on_dial_failure(&mut self, DialFailure { peer_id, error, .. }: DialFailure) { let peer_id = match peer_id { Some(id) => id, @@ -2583,7 +2551,9 @@ where } FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), - FromSwarm::NewExternalAddrOfPeer(addr) => self.on_new_address_event(addr), + FromSwarm::NewExternalAddrOfPeer(NewExternalAddrOfPeer { peer_id, addr }) => { + self.add_address(peer_id, addr.clone()); + } FromSwarm::ExpiredListenAddr(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::NewListenAddr(_) diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index ade1bc8ddf77..cd4337e9094c 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -1378,89 +1378,6 @@ fn network_behaviour_on_address_change() { ); } -#[test] -fn network_behaviour_on_new_external_address_of_peer_for_present_entry() { - let local_peer_id = PeerId::random(); - - let remote_peer_id = PeerId::random(); - let connection_id = ConnectionId::new_unchecked(0); - let address: Multiaddr = Protocol::Memory(1).into(); - let discovered_address: Multiaddr = Protocol::Memory(2).into(); - - let mut kademlia = Kademlia::new(local_peer_id, MemoryStore::new(local_peer_id)); - - let endpoint = ConnectedPoint::Dialer { - address: address.clone(), - role_override: Endpoint::Dialer, - }; - - // Mimick a connection being established. - kademlia.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished { - peer_id: remote_peer_id, - connection_id, - endpoint: &endpoint, - failed_addresses: &[], - other_established: 0, - })); - - // At this point the remote is not yet known to support the - // configured protocol name, so the peer is not yet in the - // local routing table and hence no addresses are known. - assert!(kademlia - .handle_pending_outbound_connection( - connection_id, - Some(remote_peer_id), - &[], - Endpoint::Dialer - ) - .unwrap() - .is_empty()); - - // Mimick the connection handler confirming the protocol for - // the test connection, so that the peer is added to the routing table. - kademlia.on_connection_handler_event( - remote_peer_id, - connection_id, - KademliaHandlerEvent::ProtocolConfirmed { endpoint }, - ); - - let key = kbucket::Key::from(remote_peer_id); - - assert!(matches!( - kademlia.kbuckets.entry(&key), - kbucket::Entry::Present(_, _) - )); - - assert_eq!( - vec![address.clone()], - kademlia - .handle_pending_outbound_connection( - connection_id, - Some(remote_peer_id), - &[], - Endpoint::Dialer - ) - .unwrap(), - ); - - kademlia.on_new_address_event(NewExternalAddrOfPeer { - addr: &discovered_address, - peer_id: &remote_peer_id, - }); - - assert_eq!( - vec![address, discovered_address], - kademlia - .handle_pending_outbound_connection( - connection_id, - Some(remote_peer_id), - &[], - Endpoint::Dialer - ) - .unwrap(), - ); -} - #[test] fn get_providers_single() { fn prop(key: record_priv::Key) {