diff --git a/ant-evm/src/data_payments.rs b/ant-evm/src/data_payments.rs index 48f904f8d4..36b24869d6 100644 --- a/ant-evm/src/data_payments.rs +++ b/ant-evm/src/data_payments.rs @@ -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}; @@ -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()); @@ -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(), @@ -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)); } @@ -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 @@ -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)); diff --git a/ant-networking/src/cmd.rs b/ant-networking/src/cmd.rs index 9a694f0650..ea7c26906e 100644 --- a/ant-networking/src/cmd.rs +++ b/ant-networking/src/cmd.rs @@ -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>, }, /// Notify the node received a payment. PaymentReceived, @@ -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: "ing_metrics, @@ -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"; diff --git a/ant-networking/src/lib.rs b/ant-networking/src/lib.rs index fca47f18d0..7306bad4ce 100644 --- a/ant-networking/src/lib.rs +++ b/ant-networking/src/lib.rs @@ -775,7 +775,7 @@ impl Network { pub async fn get_local_quoting_metrics( &self, key: RecordKey, - ) -> Result<(QuotingMetrics, bool)> { + ) -> Result> { let (sender, receiver) = oneshot::channel(); self.send_local_swarm_cmd(LocalSwarmCmd::GetLocalQuotingMetrics { key, sender }); diff --git a/ant-networking/src/record_store_api.rs b/ant-networking/src/record_store_api.rs index 0955d5499f..72bb9e7f67 100644 --- a/ant-networking/src/record_store_api.rs +++ b/ant-networking/src/record_store_api.rs @@ -117,13 +117,13 @@ impl UnifiedRecordStore { &self, key: &RecordKey, network_size: Option, - ) -> (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)), } } diff --git a/ant-node/src/node.rs b/ant-node/src/node.rs index 2515af6344..559fbd6119 100644 --- a/ant-node/src/node.rs +++ b/ant-node/src/node.rs @@ -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( @@ -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 { diff --git a/evmlib/src/quoting_metrics.rs b/evmlib/src/quoting_metrics.rs index c4971a1b03..a001b1a76a 100644 --- a/evmlib/src/quoting_metrics.rs +++ b/evmlib/src/quoting_metrics.rs @@ -28,26 +28,6 @@ pub struct QuotingMetrics { pub network_size: Option, } -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); diff --git a/evmlib/tests/payment_vault.rs b/evmlib/tests/payment_vault.rs index 41c5881cbb..0c7db8eff2 100644 --- a/evmlib/tests/payment_vault.rs +++ b/evmlib/tests/payment_vault.rs @@ -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(); @@ -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, }) diff --git a/evmlib/tests/wallet.rs b/evmlib/tests/wallet.rs index e9e5f0a077..6ea4c11a1f 100644 --- a/evmlib/tests/wallet.rs +++ b/evmlib/tests/wallet.rs @@ -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;