Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
client/network: Change DiscoveryBehaviour::add_self_reported signature
  • Loading branch information
mxinden committed Jul 27, 2020
commit c585fb806aebe71039dd293b0cbbfc28e2480925
2 changes: 1 addition & 1 deletion client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<peer_info::PeerInfoEven
}

for addr in listen_addrs {
self.discovery.add_self_reported_address(&peer_id, &protocols, addr);
self.discovery.add_self_reported_address(&peer_id, protocols.iter(), addr);
}
self.substrate.add_discovered_nodes(iter::once(peer_id));
}
Expand Down
47 changes: 28 additions & 19 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,26 @@ impl DiscoveryBehaviour {
}
}

/// Call this method when a node reports an address for itself.
/// Add a self-reported address of a remote peer to the k-buckets of the supported
/// DHTs (`supported_protocols`).
///
/// **Note**: It is important that you call this method. The discovery mechanism will not
/// automatically add connecting peers to the Kademlia k-buckets.
pub fn add_self_reported_address(&mut self, peer_id: &PeerId, protocols: &[String], addr: Multiaddr) {
if self.allow_non_globals_in_dht || self.can_add_to_dht(&addr) {
let mut added = false;
pub fn add_self_reported_address(
&mut self,
peer_id: &PeerId,
supported_protocols: impl Iterator<Item = impl AsRef<[u8]>>,
addr: Multiaddr
) {
if !self.allow_non_globals_in_dht && !self.can_add_to_dht(&addr) {
log::trace!(target: "sub-libp2p", "Ignoring self-reported non-global address {} from {}.", addr, peer_id);
return
}

let mut added = false;
for protocol in supported_protocols {
for kademlia in self.kademlias.values_mut() {
if protocols.iter().any(|p| p.as_bytes() == kademlia.protocol_name()) {
if protocol.as_ref() == kademlia.protocol_name() {
log::trace!(
target: "sub-libp2p",
"Adding self-reported address {} from {} to Kademlia DHT {}.",
Expand All @@ -270,16 +281,14 @@ impl DiscoveryBehaviour {
added = true;
}
}
}

if !added {
log::trace!(
target: "sub-libp2p",
"Ignoring self-reported address {} from {} as remote node is not part of any \
Kademlia DHTs supported by the local node.", addr, peer_id,
);
}
} else {
log::trace!(target: "sub-libp2p", "Ignoring self-reported non-global address {} from {}.", addr, peer_id);
if !added {
log::trace!(
target: "sub-libp2p",
"Ignoring self-reported address {} from {} as remote node is not part of any \
Kademlia DHTs supported by the local node.", addr, peer_id,
);
}
}

Expand Down Expand Up @@ -814,7 +823,7 @@ mod tests {
.unwrap();
swarms[swarm_n].0.add_self_reported_address(
&other,
&[String::from_utf8(protocol_name_from_protocol_id(&protocol_id)).unwrap()],
[protocol_name_from_protocol_id(&protocol_id)].iter(),
addr,
);

Expand Down Expand Up @@ -862,7 +871,7 @@ mod tests {
// Add remote peer with unsupported protocol.
discovery.add_self_reported_address(
&remote_peer_id,
&[String::from_utf8(protocol_name_from_protocol_id(&unsupported_protocol_id)).unwrap()],
[protocol_name_from_protocol_id(&unsupported_protocol_id)].iter(),
remote_addr.clone(),
);

Expand All @@ -878,7 +887,7 @@ mod tests {
// Add remote peer with supported protocol.
discovery.add_self_reported_address(
&remote_peer_id,
&[String::from_utf8(protocol_name_from_protocol_id(&supported_protocol_id)).unwrap()],
[protocol_name_from_protocol_id(&supported_protocol_id)].iter(),
remote_addr.clone(),
);

Expand Down Expand Up @@ -915,7 +924,7 @@ mod tests {
// Add remote peer with `protocol_a` only.
discovery.add_self_reported_address(
&remote_peer_id,
&[String::from_utf8(protocol_name_from_protocol_id(&protocol_a)).unwrap()],
[protocol_name_from_protocol_id(&protocol_a)].iter(),
remote_addr.clone(),
);

Expand All @@ -936,7 +945,7 @@ mod tests {
.kbucket(remote_peer_id.clone())
.expect("Remote peer id not to be equal to local peer id.")
.is_empty(),
"Expected remote peer not to be added to `protocol_a` Kademlia instance.",
"Expected remote peer not to be added to `protocol_b` Kademlia instance.",
);
}
}