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

feat: more metrics of QuotingMetrics #2491

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion ant-evm/src/data_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@
/// 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<u64>,
}

impl QuotingMetrics {
Expand All @@ -66,6 +70,8 @@
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
}
}
}
Expand All @@ -84,6 +90,7 @@
/// 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`

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
pub cost: AttoTokens,
/// the local node time when the quote was created
pub timestamp: SystemTime,
Expand Down
11 changes: 10 additions & 1 deletion ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 14 additions & 3 deletions ant-networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
) -> (AttoTokens, QuotingMetrics) {
let records_stored = self.records.len();

let live_time = if let Ok(elapsed) = self.timestamp.elapsed() {
Expand All @@ -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.");
Expand Down Expand Up @@ -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"
);
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 6 additions & 2 deletions ant-networking/src/record_store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
) -> (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),
}
}

Expand Down
Loading