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 Jan 17, 2025
1 parent 1770122 commit 3c7bbc2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 54 deletions.
37 changes: 18 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 @@ -135,18 +134,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 @@ -233,11 +220,23 @@ 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 {
data_size: 0,
data_type: 0,
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 @@ -329,9 +328,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 @@ -343,7 +342,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 @@ -374,9 +373,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 @@ -105,7 +105,7 @@ pub enum LocalSwarmCmd {
key: RecordKey,
data_type: u32,
data_size: usize,
sender: oneshot::Sender<(QuotingMetrics, bool)>,
sender: oneshot::Sender<Option<(QuotingMetrics, bool)>>,
},
/// Notify the node received a payment.
PaymentReceived,
Expand Down Expand Up @@ -593,7 +593,7 @@ 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
Expand All @@ -603,7 +603,11 @@ impl SwarmDriver {
data_type,
data_size,
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 @@ -643,7 +647,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 @@ -816,7 +816,7 @@ impl Network {
key: RecordKey,
data_type: u32,
data_size: usize,
) -> Result<(QuotingMetrics, bool)> {
) -> Result<Option<(QuotingMetrics, bool)>> {
let (sender, receiver) = oneshot::channel();
self.send_local_swarm_cmd(LocalSwarmCmd::GetLocalQuotingMetrics {
key,
Expand Down
8 changes: 5 additions & 3 deletions ant-networking/src/record_store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ impl UnifiedRecordStore {
data_type: u32,
data_size: usize,
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) => {
Some(store.quoting_metrics(key, data_type, data_size, network_size))
}
Self::Node(store) => store.quoting_metrics(key, data_type, data_size, 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 @@ -601,7 +601,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 @@ -623,6 +623,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
23 changes: 0 additions & 23 deletions evmlib/src/quoting_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ pub struct QuotingMetrics {
pub network_size: Option<u64>,
}

impl QuotingMetrics {
/// construct an empty QuotingMetrics
pub fn new() -> Self {
Self {
// Default to be charged as a `Chunk`
data_type: 0,
data_size: 0,
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
23 changes: 21 additions & 2 deletions evmlib/tests/payment_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,16 @@ async fn test_proxy_reachable_on_arb_sepolia() {
let payment_vault = PaymentVaultHandler::new(*network.data_payments_address(), provider);

let amount = payment_vault
.get_quote(vec![QuotingMetrics::default()])
.get_quote(vec![QuotingMetrics {
data_size: 0,
data_type: 0,
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 @@ -209,7 +218,17 @@ async fn test_verify_payment_on_local() {
let payment_verifications: Vec<_> = quote_payments
.into_iter()
.map(|v| interface::IPaymentVault::PaymentVerification {
metrics: QuotingMetrics::default().into(),
metrics: QuotingMetrics {
data_size: 0,
data_type: 0,
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
15 changes: 14 additions & 1 deletion evmlib/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,20 @@ 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 {
data_size: 0,
data_type: 0,
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 3c7bbc2

Please sign in to comment.