Skip to content

Commit

Permalink
feat(networking): remove distance range set on put
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Mar 31, 2024
1 parent 746129d commit 218f76b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
9 changes: 3 additions & 6 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ impl SwarmDriver {
.take(CLOSE_GROUP_SIZE)
.collect_vec();

if let Some(distance) = self.get_farthest_relevant_record(&closest_peers).await {
if let Some(distance) = self.get_farthest_relevant_record(&closest_peers) {
// set any new distance to fathest record in the store
self.swarm.behaviour_mut().kademlia.store_mut().set_distance_range(distance);
}
Expand All @@ -639,10 +639,7 @@ impl SwarmDriver {
// --------------------------------------------

/// Return the record we hold that is farthest and relevant to us
async fn get_farthest_relevant_record(
&mut self,
closest_k_peers: &Vec<PeerId>,
) -> Option<Distance> {
fn get_farthest_relevant_record(&mut self, closest_k_peers: &Vec<PeerId>) -> Option<Distance> {
let mut farthest_distance = None;
#[allow(clippy::mutable_key_type)]
let locally_stored_keys = self
Expand All @@ -655,7 +652,7 @@ impl SwarmDriver {

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 distance = our_address.distance(address);

if let Some(current_closest) = farthest_distance {
if distance > current_closest {
Expand Down
35 changes: 12 additions & 23 deletions sn_networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,6 @@ impl NodeRecordStore {
.cmp(&self.local_address.distance(&b))
});

let distance_range = self
.local_address
.distance(&NetworkAddress::from_record_key(
&sorted_records[sorted_records.len() - 10],
));

self.distance_range = Some(distance_range);
// sorting will be costly, hence pruning in a batch of 10
(sorted_records.len() - 10..sorted_records.len()).for_each(|i| {
info!(
Expand Down Expand Up @@ -395,14 +388,6 @@ impl NodeRecordStore {
let record_key = PrettyPrintRecordKey::from(&r.key).into_owned();
trace!("PUT a verified Record: {record_key:?}");

let new_distance = self
.local_address
.distance(&NetworkAddress::from_record_key(&r.key));

// set's the local distance range to be further if the new record is further
// This assumes the verified record is to be held by _us_...
self.distance_range = self.distance_range.map(|range| range.max(new_distance));

self.prune_storage_if_needed_for_record();

let filename = Self::generate_filename(&r.key);
Expand Down Expand Up @@ -442,7 +427,7 @@ impl NodeRecordStore {

/// Calculate the cost to store data for our current store state
#[allow(clippy::mutable_key_type)]
pub(crate) fn store_cost(&self, _key: &Key) -> (NanoTokens, QuotingMetrics) {
pub(crate) fn store_cost(&self, key: &Key) -> (NanoTokens, QuotingMetrics) {
let records_stored = self.records.len();
let record_keys_as_hashset: HashSet<&Key> = self.records.keys().collect();

Expand All @@ -462,13 +447,17 @@ impl NodeRecordStore {
info!("Basing cost of _total_ records stored.");
};

// vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues):
let cost = calculate_cost_for_records(
quoting_metrics.records_stored,
quoting_metrics.received_payment_count,
quoting_metrics.max_records,
quoting_metrics.live_time,
);
let cost = if self.contains(key) {
0
} else {
// vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues):
calculate_cost_for_records(
quoting_metrics.records_stored,
quoting_metrics.received_payment_count,
quoting_metrics.max_records,
quoting_metrics.live_time,
)
};
// vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues):
info!("Cost is now {cost:?} for quoting_metrics {quoting_metrics:?}");
(NanoTokens::from(cost), quoting_metrics)
Expand Down

0 comments on commit 218f76b

Please sign in to comment.