diff --git a/ant-evm/src/data_payments.rs b/ant-evm/src/data_payments.rs index c4647540cb..89751e4d23 100644 --- a/ant-evm/src/data_payments.rs +++ b/ant-evm/src/data_payments.rs @@ -54,8 +54,12 @@ pub struct QuotingMetrics { /// number of times that got paid pub received_payment_count: usize, /// the duration that node keeps connected to the network, measured in hours - /// TODO: take `restart` into accout pub live_time: u64, + /// network density from this node's perspective, which is the responsible_range as well + /// This could be calculated via sampling, or equation calculation. + pub network_density: Option<[u8; 32]>, + /// estimated network size + pub network_size: Option, } impl QuotingMetrics { @@ -66,6 +70,8 @@ impl QuotingMetrics { max_records: 0, received_payment_count: 0, live_time: 0, + network_density: None, + network_size: None, } } } @@ -84,6 +90,7 @@ pub struct PaymentQuote { /// the content paid for pub content: XorName, /// how much the node demands for storing the content + /// TODO: to be removed once swtich to `client querying smart_contract` pub cost: AttoTokens, /// the local node time when the quote was created pub timestamp: SystemTime, diff --git a/ant-networking/src/cmd.rs b/ant-networking/src/cmd.rs index de66fcdf56..cba58c1f3b 100644 --- a/ant-networking/src/cmd.rs +++ b/ant-networking/src/cmd.rs @@ -575,12 +575,21 @@ impl SwarmDriver { } LocalSwarmCmd::GetLocalStoreCost { key, sender } => { cmd_string = "GetLocalStoreCost"; + let ( + _index, + _total_peers, + peers_in_non_full_buckets, + num_of_full_buckets, + _kbucket_table_stats, + ) = self.kbuckets_status(); + let estimated_network_size = + Self::estimate_network_size(peers_in_non_full_buckets, num_of_full_buckets); let (cost, quoting_metrics) = self .swarm .behaviour_mut() .kademlia .store_mut() - .store_cost(&key); + .store_cost(&key, Some(estimated_network_size as u64)); self.record_metrics(Marker::StoreCost { cost: cost.as_atto(), diff --git a/ant-networking/src/record_store.rs b/ant-networking/src/record_store.rs index 34a593c441..744a7fd807 100644 --- a/ant-networking/src/record_store.rs +++ b/ant-networking/src/record_store.rs @@ -725,7 +725,11 @@ impl NodeRecordStore { } /// Calculate the cost to store data for our current store state - pub(crate) fn store_cost(&self, key: &Key) -> (AttoTokens, QuotingMetrics) { + pub(crate) fn store_cost( + &self, + key: &Key, + network_size: Option, + ) -> (AttoTokens, QuotingMetrics) { let records_stored = self.records.len(); let live_time = if let Ok(elapsed) = self.timestamp.elapsed() { @@ -739,11 +743,16 @@ impl NodeRecordStore { max_records: self.config.max_records, received_payment_count: self.received_payment_count, live_time, + network_density: None, + network_size, }; if let Some(distance_range) = self.responsible_distance_range { let relevant_records = self.get_records_within_distance_range(distance_range); + // The `responsible_range` is the network density + quoting_metrics.network_density = Some(distance_range.0.into()); + quoting_metrics.close_records_stored = relevant_records; } else { info!("Basing cost of _total_ records stored."); @@ -1167,13 +1176,13 @@ mod tests { swarm_cmd_sender, ); - let store_cost_before = store.store_cost(&r.key); + let store_cost_before = store.store_cost(&r.key, None); // An initial unverified put should not write to disk assert!(store.put(r.clone()).is_ok()); assert!(store.get(&r.key).is_none()); // Store cost should not change if no PUT has been added assert_eq!( - store.store_cost(&r.key).0, + store.store_cost(&r.key, None).0, store_cost_before.0, "store cost should not change over unverified put" ); @@ -1950,6 +1959,8 @@ mod tests { max_records: MAX_RECORDS_COUNT, received_payment_count: 1, // unimportant for cost calc live_time: 0, // unimportant for cost calc + network_density: None, + network_size: None, }, bad_nodes: vec![], pub_key: bls::SecretKey::random().public_key().to_bytes().to_vec(), diff --git a/ant-networking/src/record_store_api.rs b/ant-networking/src/record_store_api.rs index f9af14165b..7923c0d1b3 100644 --- a/ant-networking/src/record_store_api.rs +++ b/ant-networking/src/record_store_api.rs @@ -111,13 +111,17 @@ impl UnifiedRecordStore { } } - pub(crate) fn store_cost(&self, key: &RecordKey) -> (AttoTokens, QuotingMetrics) { + pub(crate) fn store_cost( + &self, + key: &RecordKey, + network_size: Option, + ) -> (AttoTokens, QuotingMetrics) { match self { Self::Client(_) => { warn!("Calling store cost calculation at Client. This should not happen"); (AttoTokens::zero(), Default::default()) } - Self::Node(store) => store.store_cost(key), + Self::Node(store) => store.store_cost(key, network_size), } }