Skip to content

Commit

Permalink
fix: always specify the quoting metric explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 23, 2024
1 parent 4e671d7 commit 4d9ac69
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 51 deletions.
35 changes: 16 additions & 19 deletions ant-evm/src/data_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::EvmError;
use evmlib::{
common::{Address as RewardsAddress, QuoteHash},
quoting_metrics::QuotingMetrics,
utils::dummy_address,
};
use libp2p::{identity::PublicKey, PeerId};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -134,18 +133,6 @@ pub struct PaymentQuote {
}

impl PaymentQuote {
/// create an empty PaymentQuote
pub fn zero() -> Self {
Self {
content: Default::default(),
timestamp: SystemTime::now(),
quoting_metrics: Default::default(),
rewards_address: dummy_address(),
pub_key: vec![],
signature: vec![],
}
}

pub fn hash(&self) -> QuoteHash {
let mut bytes = self.bytes_for_sig();
bytes.extend_from_slice(self.pub_key.as_slice());
Expand Down Expand Up @@ -232,11 +219,21 @@ impl PaymentQuote {
}

/// test utility to create a dummy quote
#[cfg(test)]
pub fn test_dummy(xorname: XorName) -> Self {
use evmlib::utils::dummy_address;

Self {
content: xorname,
timestamp: SystemTime::now(),
quoting_metrics: Default::default(),
quoting_metrics: QuotingMetrics {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
},
pub_key: vec![],
signature: vec![],
rewards_address: dummy_address(),
Expand Down Expand Up @@ -328,9 +325,9 @@ mod tests {

#[test]
fn test_is_newer_than() {
let old_quote = PaymentQuote::zero();
let old_quote = PaymentQuote::test_dummy(Default::default());
sleep(Duration::from_millis(100));
let new_quote = PaymentQuote::zero();
let new_quote = PaymentQuote::test_dummy(Default::default());
assert!(new_quote.is_newer_than(&old_quote));
assert!(!old_quote.is_newer_than(&new_quote));
}
Expand All @@ -342,7 +339,7 @@ mod tests {

let false_peer = PeerId::random();

let mut quote = PaymentQuote::zero();
let mut quote = PaymentQuote::test_dummy(Default::default());
let bytes = quote.bytes_for_sig();
let signature = if let Ok(sig) = keypair.sign(&bytes) {
sig
Expand Down Expand Up @@ -373,9 +370,9 @@ mod tests {

#[test]
fn test_historical_verify() {
let mut old_quote = PaymentQuote::zero();
let mut old_quote = PaymentQuote::test_dummy(Default::default());
sleep(Duration::from_millis(100));
let mut new_quote = PaymentQuote::zero();
let mut new_quote = PaymentQuote::test_dummy(Default::default());

// historical_verify will swap quotes to compare based on timeline automatically
assert!(new_quote.historical_verify(&old_quote));
Expand Down
12 changes: 8 additions & 4 deletions ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub enum LocalSwarmCmd {
/// Returns the quoting metrics and whether the record at `key` is already stored locally
GetLocalQuotingMetrics {
key: RecordKey,
sender: oneshot::Sender<(QuotingMetrics, bool)>,
sender: oneshot::Sender<Option<(QuotingMetrics, bool)>>,
},
/// Notify the node received a payment.
PaymentReceived,
Expand Down Expand Up @@ -586,12 +586,16 @@ impl SwarmDriver {
) = self.kbuckets_status();
let estimated_network_size =
Self::estimate_network_size(peers_in_non_full_buckets, num_of_full_buckets);
let (quoting_metrics, is_already_stored) = self
let Some((quoting_metrics, is_already_stored)) = self
.swarm
.behaviour_mut()
.kademlia
.store_mut()
.quoting_metrics(&key, Some(estimated_network_size as u64));
.quoting_metrics(&key, Some(estimated_network_size as u64))
else {
let _res = sender.send(None);
return Ok(());
};

self.record_metrics(Marker::QuotingMetrics {
quoting_metrics: &quoting_metrics,
Expand Down Expand Up @@ -631,7 +635,7 @@ impl SwarmDriver {
.retain(|peer_addr| key_address.distance(peer_addr) < boundary_distance);
}

let _res = sender.send((quoting_metrics, is_already_stored));
let _res = sender.send(Some((quoting_metrics, is_already_stored)));
}
LocalSwarmCmd::PaymentReceived => {
cmd_string = "PaymentReceived";
Expand Down
2 changes: 1 addition & 1 deletion ant-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ impl Network {
pub async fn get_local_quoting_metrics(
&self,
key: RecordKey,
) -> Result<(QuotingMetrics, bool)> {
) -> Result<Option<(QuotingMetrics, bool)>> {
let (sender, receiver) = oneshot::channel();
self.send_local_swarm_cmd(LocalSwarmCmd::GetLocalQuotingMetrics { key, sender });

Expand Down
6 changes: 3 additions & 3 deletions ant-networking/src/record_store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ impl UnifiedRecordStore {
&self,
key: &RecordKey,
network_size: Option<u64>,
) -> (QuotingMetrics, bool) {
) -> Option<(QuotingMetrics, bool)> {
match self {
Self::Client(_) => {
warn!("Calling quoting metrics calculation at Client. This should not happen");
Default::default()
None
}
Self::Node(store) => store.quoting_metrics(key, network_size),
Self::Node(store) => Some(store.quoting_metrics(key, network_size)),
}
}

Expand Down
10 changes: 9 additions & 1 deletion ant-node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ impl Node {
};

match maybe_quoting_metrics {
Ok((quoting_metrics, is_already_stored)) => {
Ok(Some((quoting_metrics, is_already_stored))) => {
if is_already_stored {
QueryResponse::GetStoreQuote {
quote: Err(ProtocolError::RecordExists(
Expand All @@ -618,6 +618,14 @@ impl Node {
}
}
}
Ok(None) => {
error!("Quoting metrics not found for {key:?}. This might be because we are using a ClientRecordStore??. This should not happen");
QueryResponse::GetStoreQuote {
quote: Err(ProtocolError::GetStoreQuoteFailed),
peer_address: NetworkAddress::from_peer(self_id),
storage_proofs,
}
}
Err(err) => {
warn!("GetStoreQuote failed for {key:?}: {err}");
QueryResponse::GetStoreQuote {
Expand Down
20 changes: 0 additions & 20 deletions evmlib/src/quoting_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ pub struct QuotingMetrics {
pub network_size: Option<u64>,
}

impl QuotingMetrics {
/// construct an empty QuotingMetrics
pub fn new() -> Self {
Self {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
}
}
}

impl Default for QuotingMetrics {
fn default() -> Self {
Self::new()
}
}

impl Debug for QuotingMetrics {
fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
let density_u256 = self.network_density.map(U256::from_be_bytes);
Expand Down
19 changes: 17 additions & 2 deletions evmlib/tests/payment_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ async fn test_proxy_reachable() {
let payment_vault = PaymentVaultHandler::new(*network.data_payments_address(), provider);

let amount = payment_vault
.get_quote(vec![QuotingMetrics::default()])
.get_quote(vec![QuotingMetrics {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
}])
.await
.unwrap();

Expand Down Expand Up @@ -156,7 +163,15 @@ async fn test_verify_payment() {
let payment_verifications: Vec<_> = quote_payments
.into_iter()
.map(|v| interface::IPaymentVault::PaymentVerification {
metrics: QuotingMetrics::default().into(),
metrics: QuotingMetrics {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
}
.into(),
rewardsAddress: v.1,
quoteHash: v.0,
})
Expand Down
13 changes: 12 additions & 1 deletion evmlib/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ async fn test_pay_for_quotes_and_data_payment_verification() {
let result = verify_data_payment(
&network,
vec![*quote_hash],
vec![(*quote_hash, QuotingMetrics::default(), *reward_addr)],
vec![(
*quote_hash,
QuotingMetrics {
close_records_stored: 0,
max_records: 0,
received_payment_count: 0,
live_time: 0,
network_density: None,
network_size: None,
},
*reward_addr,
)],
)
.await;

Expand Down

0 comments on commit 4d9ac69

Please sign in to comment.