Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce responsible range #2059

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
chore(node): reduce the replication range
maqi committed Aug 21, 2024
commit 3dbc58104c03daf0d6fab509d5b4f6319173d941
23 changes: 13 additions & 10 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
@@ -791,7 +791,7 @@ impl SwarmDriver {
if !self.is_client {
let closest_k_peers = self.get_closest_k_value_local_peers();

if let Some(distance) = self.get_farthest_data_address_estimate(&closest_k_peers) {
if let Some(distance) = self.get_responsbile_range_estimate(&closest_k_peers) {
info!("Set responsible range to {distance}");
// set any new distance to farthest record in the store
self.swarm.behaviour_mut().kademlia.store_mut().set_distance_range(distance);
@@ -809,27 +809,30 @@ impl SwarmDriver {
// ---------- Crate helpers -------------------
// --------------------------------------------

/// Returns the farthest bucket, close to but probably farther than our responsibilty range.
/// This simply uses the closest k peers to estimate the farthest address as
/// `K_VALUE`th peer's bucket.
fn get_farthest_data_address_estimate(
/// Uses the closest k peers to estimate the farthest address as
/// `K_VALUE / 2`th peer's bucket.
fn get_responsbile_range_estimate(
&mut self,
// Sorted list of closest k peers to our peer id.
closest_k_peers: &[PeerId],
) -> Option<u32> {
// if we don't have enough peers we don't set the distance range yet.
let mut farthest_distance = None;

if closest_k_peers.is_empty() {
return farthest_distance;
}

let our_address = NetworkAddress::from_peer(self.self_peer_id);

// get K_VALUEth peer's address distance
// get `K_VALUE / 2`th peer's address distance
// This is a rough estimate of the farthest address we might be responsible for.
// We want this to be higher than actually necessary, so we retain more data
// and can be sure to pass bad node checks
if let Some(peer) = closest_k_peers.last() {
let address = NetworkAddress::from_peer(*peer);
farthest_distance = our_address.distance(&address).ilog2();
}
let target_index = std::cmp::min(K_VALUE.get() / 2, closest_k_peers.len()) - 1;

let address = NetworkAddress::from_peer(closest_k_peers[target_index]);
farthest_distance = our_address.distance(&address).ilog2();

farthest_distance
}