Skip to content

Commit

Permalink
chore: update verify data payment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke committed Dec 5, 2024
1 parent 7cbeb17 commit 849de7e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 271 deletions.
2 changes: 1 addition & 1 deletion autonomi/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::client::payment::Receipt;
use crate::utils::receipt_from_cost_map_and_payments;
use ant_evm::{EvmWallet, ProofOfPayment, QuotePayment};
use ant_networking::{
GetRecordCfg, Network, NetworkError, PayeeQuote, PutRecordCfg, VerificationKind,
Expand All @@ -30,6 +29,7 @@ use super::{
Client,
};
use crate::self_encryption::DataMapLevel;
use crate::utils::receipt_from_cost_map_and_payments;

impl Client {
/// Fetch and decrypt all chunks in the data map.
Expand Down
1 change: 1 addition & 0 deletions autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ pub use client::Client;

#[cfg(feature = "extension-module")]
mod python;
mod utils;
39 changes: 39 additions & 0 deletions autonomi/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::client::payment::Receipt;
use ant_evm::{PaymentQuote, ProofOfPayment, QuoteHash, TxHash};
use ant_networking::PayeeQuote;
use std::collections::{BTreeMap, HashMap};
use xor_name::XorName;

pub fn cost_map_to_quotes(
cost_map: HashMap<XorName, PayeeQuote>,
) -> HashMap<XorName, PaymentQuote> {
cost_map.into_iter().map(|(k, (_, _, v))| (k, v)).collect()
}

pub fn receipt_from_cost_map_and_payments(
cost_map: HashMap<XorName, PayeeQuote>,
payments: &BTreeMap<QuoteHash, TxHash>,
) -> Receipt {
let quotes = cost_map_to_quotes(cost_map);
receipt_from_quotes_and_payments(&quotes, payments)
}

pub fn receipt_from_quotes_and_payments(
quotes: &HashMap<XorName, PaymentQuote>,
payments: &BTreeMap<QuoteHash, TxHash>,
) -> Receipt {
quotes
.iter()
.filter_map(|(xor_name, quote)| {
payments.get(&quote.hash()).map(|tx_hash| {
(
*xor_name,
ProofOfPayment {
quote: quote.clone(),
tx_hash: *tx_hash,
},
)
})
})
.collect()
}
26 changes: 21 additions & 5 deletions evmlib/src/contract/payment_vault/interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::common::{Address, Amount, QuoteHash};
use crate::common::{Address, Amount, QuoteHash, U256};
use crate::quoting_metrics::QuotingMetrics;
use alloy::primitives::FixedBytes;
use alloy::sol;

sol!(
Expand All @@ -9,11 +11,25 @@ sol!(
);

impl From<(QuoteHash, Address, Amount)> for IPaymentVault::DataPayment {
fn from(data: (QuoteHash, Address, Amount)) -> Self {
fn from(value: (QuoteHash, Address, Amount)) -> Self {
Self {
rewardsAddress: data.1,
amount: data.2,
quoteHash: data.0,
rewardsAddress: value.1,
amount: value.2,
quoteHash: value.0,
}
}
}

impl From<QuotingMetrics> for IPaymentVault::QuotingMetrics {
fn from(value: QuotingMetrics) -> Self {
Self {
closeRecordsStored: U256::from(value.close_records_stored),
maxRecords: U256::from(value.max_records),
receivedPaymentCount: U256::from(value.received_payment_count),
liveTime: U256::from(value.live_time),
networkDensity: FixedBytes::<32>::from(value.network_density.unwrap_or_default())
.into(),
networkSize: value.network_size.map(U256::from).unwrap_or_default(),
}
}
}
71 changes: 0 additions & 71 deletions evmlib/src/event.rs

This file was deleted.

21 changes: 4 additions & 17 deletions evmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::common::{Address, QuoteHash, TxHash};
use crate::common::{Address, QuoteHash};
use crate::transaction::verify_data_payment;
use alloy::primitives::address;
use alloy::transports::http::reqwest;
use common::Amount;
use quoting_metrics::QuotingMetrics;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
Expand All @@ -23,7 +22,6 @@ extern crate tracing;
pub mod common;
pub mod contract;
pub mod cryptography;
pub(crate) mod event;
#[cfg(feature = "external-signer")]
pub mod external_signer;
pub mod quoting_metrics;
Expand Down Expand Up @@ -139,21 +137,10 @@ impl Network {

pub async fn verify_data_payment(
&self,
tx_hash: TxHash,
quote_hash: QuoteHash,
_quoting_metrics: QuotingMetrics,
quoting_metrics: QuotingMetrics,
reward_addr: Address,
quote_expiration_timestamp_in_secs: u64,
) -> Result<Amount, transaction::Error> {
verify_data_payment(
self,
tx_hash,
quote_hash,
// quoting_metrics, // NB TODO use them here @Mick
reward_addr,
Default::default(), // NB TODO remove amounts @Mick
quote_expiration_timestamp_in_secs,
)
.await
) -> Result<(), transaction::Error> {
verify_data_payment(self, quote_hash, reward_addr, quoting_metrics).await
}
}
Loading

0 comments on commit 849de7e

Please sign in to comment.