Skip to content

Commit

Permalink
Merge pull request #2491 from maqi/quoting_update_for_new_econ
Browse files Browse the repository at this point in the history
feat: more metrics of QuotingMetrics
  • Loading branch information
maqi authored Dec 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents ef3a2b1 + d1b2c81 commit f0775e0
Showing 4 changed files with 38 additions and 7 deletions.
9 changes: 8 additions & 1 deletion ant-evm/src/data_payments.rs
Original file line number Diff line number Diff line change
@@ -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<u64>,
}

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,
11 changes: 10 additions & 1 deletion ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -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(),
17 changes: 14 additions & 3 deletions ant-networking/src/record_store.rs
Original file line number Diff line number Diff line change
@@ -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() {
@@ -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(),
8 changes: 6 additions & 2 deletions ant-networking/src/record_store_api.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}

0 comments on commit f0775e0

Please sign in to comment.