Skip to content

Commit

Permalink
feat(ant-cli): report already paid for records
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Jan 7, 2025
1 parent 7e7c066 commit c995d16
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> Result<(
println!("At address: {local_addr}");
info!("Successfully uploaded: {file} at address: {local_addr}");
println!("Number of chunks uploaded: {}", summary.record_count);
println!(
"Number of chunks already paid/uploaded: {}",
summary.records_already_paid
);
println!("Total cost: {} AttoTokens", summary.tokens_spent);
}
info!("Summary for upload of file {file} at {local_addr:?}: {summary:?}");
Expand Down
4 changes: 4 additions & 0 deletions ant-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn collect_upload_summary(
let stats_thread = tokio::spawn(async move {
let mut tokens_spent: Amount = Amount::from(0);
let mut record_count = 0;
let mut records_already_paid = 0;

loop {
tokio::select! {
Expand All @@ -28,6 +29,7 @@ pub fn collect_upload_summary(
Some(ClientEvent::UploadComplete(upload_summary)) => {
tokens_spent += upload_summary.tokens_spent;
record_count += upload_summary.record_count;
records_already_paid += upload_summary.records_already_paid;
}
None => break,
}
Expand All @@ -42,13 +44,15 @@ pub fn collect_upload_summary(
ClientEvent::UploadComplete(upload_summary) => {
tokens_spent += upload_summary.tokens_spent;
record_count += upload_summary.record_count;
records_already_paid += upload_summary.records_already_paid;
}
}
}

UploadSummary {
tokens_spent,
record_count,
records_already_paid,
}
});

Expand Down
5 changes: 3 additions & 2 deletions autonomi/src/client/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl Client {
// Pay for all chunks
let xor_names: Vec<_> = chunks.iter().map(|chunk| *chunk.name()).collect();
info!("Paying for {} addresses", xor_names.len());
let receipt = self
let (receipt, skipped_payments) = self
.pay_for_content_addrs(xor_names.into_iter(), payment_option)
.await
.inspect_err(|err| error!("Error paying for data: {err:?}"))?;
Expand All @@ -244,7 +244,7 @@ impl Client {
return Err(last_chunk_fail.1);
}

let record_count = chunks.len();
let record_count = chunks.len() - skipped_payments;

// Reporting
if let Some(channel) = self.client_event_sender.as_ref() {
Expand All @@ -255,6 +255,7 @@ impl Client {

let summary = UploadSummary {
record_count,
records_already_paid: skipped_payments,
tokens_spent,
};
if let Err(err) = channel.send(ClientEvent::UploadComplete(summary)).await {
Expand Down
5 changes: 3 additions & 2 deletions autonomi/src/client/data/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Client {

// Pay for all chunks + data map chunk
info!("Paying for {} addresses", xor_names.len());
let receipt = self
let (receipt, skipped_payments) = self
.pay_for_content_addrs(xor_names.into_iter(), payment_option)
.await
.inspect_err(|err| error!("Error paying for data: {err:?}"))?;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Client {
return Err(last_chunk_fail.1);
}

let record_count = chunks.len() + 1;
let record_count = (chunks.len() + 1) - skipped_payments;

// Reporting
if let Some(channel) = self.client_event_sender.as_ref() {
Expand All @@ -98,6 +98,7 @@ impl Client {

let summary = UploadSummary {
record_count,
records_already_paid: skipped_payments,
tokens_spent,
};
if let Err(err) = channel.send(ClientEvent::UploadComplete(summary)).await {
Expand Down
7 changes: 7 additions & 0 deletions autonomi/src/client/files/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ impl Client {
let bytes = archive
.to_bytes()
.map_err(|e| PutError::Serialization(format!("Failed to serialize archive: {e:?}")))?;

#[cfg(feature = "loud")]
println!(
"Uploading private archive referencing {} files",
archive.map().len()
);

let result = self.data_put(bytes, payment_option).await;
debug!("Uploaded private archive {archive:?} to the network and address is {result:?}");
result
Expand Down
7 changes: 7 additions & 0 deletions autonomi/src/client/files/archive_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ impl Client {
let bytes = archive
.to_bytes()
.map_err(|e| PutError::Serialization(format!("Failed to serialize archive: {e:?}")))?;

#[cfg(feature = "loud")]
println!(
"Uploading public archive referencing {} files",
archive.map().len()
);

let result = self.data_put_public(bytes, wallet.into()).await;
debug!("Uploaded archive {archive:?} to the network and the address is {result:?}");
result
Expand Down
5 changes: 3 additions & 2 deletions autonomi/src/client/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Client {
// pay for the transaction
let xor_name = address.xorname();
debug!("Paying for transaction at address: {address:?}");
let payment_proofs = self
let (payment_proofs, skipped_payments) = self
.pay(std::iter::once(*xor_name), wallet)
.await
.inspect_err(|err| {
Expand Down Expand Up @@ -121,7 +121,8 @@ impl Client {
// send client event
if let Some(channel) = self.client_event_sender.as_ref() {
let summary = UploadSummary {
record_count: 1,
record_count: 1usize.saturating_sub(skipped_payments),
records_already_paid: skipped_payments,
tokens_spent: price.as_atto(),
};
if let Err(err) = channel.send(ClientEvent::UploadComplete(summary)).await {
Expand Down
4 changes: 4 additions & 0 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ pub enum ClientEvent {
/// Summary of an upload operation.
#[derive(Debug, Clone)]
pub struct UploadSummary {
/// Records that were uploaded to the network
pub record_count: usize,
/// Records that were already paid for so were not re-uploaded
pub records_already_paid: usize,
/// Total cost of the upload
pub tokens_spent: Amount,
}
10 changes: 6 additions & 4 deletions autonomi/src/client/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ant_evm::{AttoTokens, EncodedPeerId, EvmWallet, ProofOfPayment};
use std::collections::HashMap;
use xor_name::XorName;

use super::utils::AlreadyPaidAddressesCount;

/// Contains the proof of payments for each XOR address and the amount paid
pub type Receipt = HashMap<XorName, (ProofOfPayment, AttoTokens)>;

Expand Down Expand Up @@ -65,13 +67,13 @@ impl Client {
&self,
content_addrs: impl Iterator<Item = XorName> + Clone,
payment_option: PaymentOption,
) -> Result<Receipt, PayError> {
) -> Result<(Receipt, AlreadyPaidAddressesCount), PayError> {
match payment_option {
PaymentOption::Wallet(wallet) => {
let receipt = self.pay(content_addrs, &wallet).await?;
Ok(receipt)
let (receipt, skipped) = self.pay(content_addrs, &wallet).await?;
Ok((receipt, skipped))
}
PaymentOption::Receipt(receipt) => Ok(receipt),
PaymentOption::Receipt(receipt) => Ok((receipt, 0)),
}
}
}
2 changes: 1 addition & 1 deletion autonomi/src/client/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Client {
// pay for the pointer storage
let xor_name = *address.xorname();
debug!("Paying for pointer at address: {address:?}");
let payment_proofs = self
let (payment_proofs, _skipped_payments) = self
.pay(std::iter::once(xor_name), wallet)
.await
.inspect_err(|err| {
Expand Down
9 changes: 5 additions & 4 deletions autonomi/src/client/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ impl Client {

let reg_xor = address.xorname();
debug!("Paying for register at address: {address}");
let payment_proofs = self
let (payment_proofs, skipped_payments) = self
.pay(std::iter::once(reg_xor), wallet)
.await
.inspect_err(|err| {
error!("Failed to pay for register at address: {address} : {err}")
})?;
error!("Failed to pay for register at address: {address} : {err}")
})?;
let (proof, price) = if let Some((proof, price)) = payment_proofs.get(&reg_xor) {
(proof, price)
} else {
Expand Down Expand Up @@ -370,7 +370,8 @@ impl Client {

if let Some(channel) = self.client_event_sender.as_ref() {
let summary = UploadSummary {
record_count: 1,
record_count: 1usize.saturating_sub(skipped_payments),
records_already_paid: skipped_payments,
tokens_spent: price.as_atto(),
};
if let Err(err) = channel.send(ClientEvent::UploadComplete(summary)).await {
Expand Down
6 changes: 4 additions & 2 deletions autonomi/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use super::{
};
use crate::self_encryption::DataMapLevel;

pub type AlreadyPaidAddressesCount = usize;

impl Client {
/// Fetch and decrypt all chunks in the data map.
pub(crate) async fn fetch_from_data_map(&self, data_map: &DataMap) -> Result<Bytes, GetError> {
Expand Down Expand Up @@ -164,7 +166,7 @@ impl Client {
&self,
content_addrs: impl Iterator<Item = XorName> + Clone,
wallet: &EvmWallet,
) -> Result<Receipt, PayError> {
) -> Result<(Receipt, AlreadyPaidAddressesCount), PayError> {
let number_of_content_addrs = content_addrs.clone().count();
let quotes = self.get_store_quotes(content_addrs).await?;

Expand Down Expand Up @@ -194,7 +196,7 @@ impl Client {

let receipt = receipt_from_store_quotes(quotes);

Ok(receipt)
Ok((receipt, skipped_chunks))
}
}

Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Client {
info!("Writing to vault at {scratch_address:?}",);

let record = if is_new {
let receipt = self
let (receipt, _skipped_payments) = self
.pay_for_content_addrs(std::iter::once(scratch.xorname()), payment_option)
.await
.inspect_err(|err| {
Expand Down

0 comments on commit c995d16

Please sign in to comment.