Skip to content

Commit

Permalink
feat(client): parallelise store cost retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Aug 18, 2023
1 parent 648bb47 commit d6fd46f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
27 changes: 19 additions & 8 deletions sn_client/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
iter::Iterator,
time::{Duration, Instant},
};
use tokio::time::sleep;
use tokio::{task::JoinSet, time::sleep};

/// A wallet client can be used to send and
/// receive tokens to/from other wallets.
Expand Down Expand Up @@ -87,7 +87,7 @@ impl WalletClient {
/// Get storecost from the network
/// Returns a Vec of proofs
pub async fn get_store_cost_at_address(
&mut self,
&self,
address: &NetworkAddress,
) -> Result<Vec<(PublicAddress, Token)>> {
self.client
Expand All @@ -110,17 +110,28 @@ impl WalletClient {

let mut payment_map = BTreeMap::default();

let mut tasks = JoinSet::new();
// we can collate all the payments together into one transfer
for content_addr in content_addrs {
// TODO: parallelise this
let amounts_to_pay = self.get_store_cost_at_address(&content_addr).await?;
let client = self.client.clone();
tasks.spawn(async move {
let costs = client
.get_store_costs_at_address(&content_addr)
.await
.map_err(|error| Error::CouldNotSendTokens(error.to_string()));
(content_addr, costs)
});
}

// TODO: We put an empty vec for now as we're not validating.
// We'll need to map dbcs to content addrs though.
payment_map.insert(content_addr, amounts_to_pay);
while let Some(res) = tasks.join_next().await {
let (content_addr, amounts_to_pay) = match res {
Ok(c) => c,
Err(e) => return Err(Error::CouldNotGetStoreCost(e.to_string())),
};
// let amounts_to_pay = amounts_to_pay;
payment_map.insert(content_addr, amounts_to_pay?);
}

// TODO: This needs to map returned Dbcs to target addrs
let (payments, cost) = self.pay_for_records(payment_map, verify_store).await?;

if let Some(cost) = total_cost.checked_add(cost) {
Expand Down
3 changes: 1 addition & 2 deletions sn_node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@ impl Node {
}
}
} else {
//TODO: we're not verifying at the moment
// return Err(ProtocolError::PaymentProofWithoutInputs(addr_name));
return Err(ProtocolError::NoPaymentForNodeToStore(addr_name));
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion sn_protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub enum Error {
#[error("At least one input of payment proof provided for {0:?} has a mismatching spend Tx")]
PaymentProofTxMismatch(XorName),
/// Payment proof received has no inputs
#[error("Payment proof received for {0:?} has no inputs in its transaction")]
#[error("Payment proof received for {0:?} has no dbc for this node in its transaction")]
NoPaymentForNodeToStore(XorName),
/// The id of the fee output found in a storage payment proof is invalid
#[error("The id of the fee output found in a storage payment proof is invalid: {0:?}")]
Expand Down

0 comments on commit d6fd46f

Please sign in to comment.