Skip to content

Commit

Permalink
feat(networking): use an estimate of responsible distance range
Browse files Browse the repository at this point in the history
uses CLOSE_GROUP_SIZE + 1 peers' distance as a proxy. This gives some margin for error
and should err on the side of caution
  • Loading branch information
joshuef authored and maqi committed Apr 2, 2024
1 parent 776089a commit 90790a6
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ impl SwarmDriver {
let closest_k_peers = self
.get_closest_k_value_local_peers();

if let Some(distance) = self.get_farthest_relevant_record(&closest_k_peers) {
if let Some(distance) = self.get_farthest_relevant_address_estimate(&closest_k_peers) {
// set any new distance to fathest record in the store
self.swarm.behaviour_mut().kademlia.store_mut().set_distance_range(distance);
}
Expand All @@ -634,30 +634,24 @@ impl SwarmDriver {
// ---------- Crate helpers -------------------
// --------------------------------------------

/// Return the record we hold that is farthest and relevant to us
fn get_farthest_relevant_record(&mut self, closest_k_peers: &Vec<PeerId>) -> Option<Distance> {
/// Return a far address, close to but probably farther than our responsibilty range.
/// This simply uses the closest k peers to estimate the farthest address as CLOSE_GROUP_SIZE + 1 peer's
/// address distance.
fn get_farthest_relevant_address_estimate(
&mut self,
// Sorted list of closest k peers to our peer id.
closest_k_peers: &[PeerId],
) -> Option<Distance> {
// if we don't have enough peers we don't set the distance range yet.
let mut farthest_distance = None;
#[allow(clippy::mutable_key_type)]
let locally_stored_keys = self
.swarm
.behaviour_mut()
.kademlia
.store_mut()
.record_addresses_ref();
let our_address = NetworkAddress::from_peer(self.self_peer_id);

for (_key, (address, _r_type)) in locally_stored_keys.iter() {
if Self::is_in_close_range(&self.self_peer_id, address, closest_k_peers) {
let distance = our_address.distance(address);
let our_address = NetworkAddress::from_peer(self.self_peer_id);

if let Some(current_closest) = farthest_distance {
if distance > current_closest {
farthest_distance = Some(distance);
}
} else {
farthest_distance = Some(distance);
}
}
// get CLOSES_PEERS_COUNT + 1 peer's address distance
if let Some(peer) = closest_k_peers.get(CLOSE_GROUP_SIZE + 1) {
let address = NetworkAddress::from_peer(*peer);
let distance = our_address.distance(&address);
farthest_distance = Some(distance);
}

farthest_distance
Expand Down

0 comments on commit 90790a6

Please sign in to comment.