Skip to content

Commit

Permalink
Use kademlia's add_address instead of on_new_address_event
Browse files Browse the repository at this point in the history
  • Loading branch information
StemCll committed Sep 17, 2023
1 parent 2a92826 commit 18ba25d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 116 deletions.
36 changes: 3 additions & 33 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(_)
Expand Down
83 changes: 0 additions & 83 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 18ba25d

Please sign in to comment.