Skip to content

Commit

Permalink
chore(client): always validate storage payments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Oct 18, 2023
1 parent 7b77c7b commit bc0cb8d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 35 deletions.
3 changes: 1 addition & 2 deletions sn_cli/src/subcommands/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async fn upload_files(
for chunks_batch in chunks_to_upload.chunks(batch_size) {
// pay for and verify payment... if we don't verify here, chunks uploads will surely fail
let (cost, new_balance) = match file_api
.pay_for_chunks(chunks_batch.iter().map(|(name, _)| *name).collect(), true)
.pay_for_chunks(chunks_batch.iter().map(|(name, _)| *name).collect())
.await
{
Ok((cost, new_balance)) => (cost, new_balance),
Expand Down Expand Up @@ -481,7 +481,6 @@ async fn verify_and_repay_if_needed(
failed_chunks_batch
.iter()
.map(|(addr, _path)| sn_protocol::NetworkAddress::ChunkAddress(*addr)),
true,
)
.await
.wrap_err("Failed to repay for record storage for {failed_chunks_batch:?}.")?;
Expand Down
3 changes: 1 addition & 2 deletions sn_cli/src/subcommands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ pub(crate) async fn wallet_cmds(
.map(|(n, _)| *n)
.collect();

// pay for and verify payment... if we don't verify here, chunks uploads will surely fail
file_api.pay_for_chunks(all_chunks, verify_store).await?;
file_api.pay_for_chunks(all_chunks).await?;
}
cmd => {
return Err(eyre!(
Expand Down
28 changes: 9 additions & 19 deletions sn_client/src/file_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,16 @@ impl Files {
/// Pay for a given set of chunks.
///
/// Returns the cost and the resulting new balance of the local wallet.
pub async fn pay_for_chunks(
&self,
chunks: Vec<XorName>,
verify_store: bool,
) -> Result<(NanoTokens, NanoTokens)> {
pub async fn pay_for_chunks(&self, chunks: Vec<XorName>) -> Result<(NanoTokens, NanoTokens)> {
let mut wallet_client = self.wallet()?;
info!("Paying for and uploading {:?} chunks", chunks.len());

let cost = wallet_client
.pay_for_storage(
chunks.iter().map(|name| {
let cost =
wallet_client
.pay_for_storage(chunks.iter().map(|name| {
sn_protocol::NetworkAddress::ChunkAddress(ChunkAddress::new(*name))
}),
verify_store,
)
.await?;
}))
.await?;

wallet_client.store_local_wallet()?;
let new_balance = wallet_client.balance();
Expand Down Expand Up @@ -296,7 +290,6 @@ impl Files {
chunks
.iter()
.map(|(name, _)| NetworkAddress::ChunkAddress(ChunkAddress::new(*name))),
true,
)
.await
.expect("Failed to pay for storage for new file at {file_addr:?}");
Expand All @@ -317,12 +310,9 @@ impl Files {
// Now we pay again or top up, depending on the new current store cost is
let new_cost = self
.wallet()?
.pay_for_storage(
failed_chunks.iter().map(|(addr, _path)| {
sn_protocol::NetworkAddress::ChunkAddress(ChunkAddress::new(*addr))
}),
true,
)
.pay_for_storage(failed_chunks.iter().map(|(addr, _path)| {
sn_protocol::NetworkAddress::ChunkAddress(ChunkAddress::new(*addr))
}))
.await?;

cost = cost.checked_add(new_cost).ok_or(Error::Transfers(
Expand Down
2 changes: 1 addition & 1 deletion sn_client/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl ClientRegister {
let net_addr = sn_protocol::NetworkAddress::RegisterAddress(addr);
// Let's make the storage payment
cost = wallet_client
.pay_for_storage(std::iter::once(net_addr.clone()), true)
.pay_for_storage(std::iter::once(net_addr.clone()))
.await?;

println!("Successfully made payment of {cost} for a Register (At a cost per record of {cost:?}.)");
Expand Down
2 changes: 1 addition & 1 deletion sn_client/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ impl WalletClient {
pub async fn pay_for_storage(
&mut self,
content_addrs: impl Iterator<Item = NetworkAddress>,
verify_store: bool,
) -> WalletResult<NanoTokens> {
let verify_store = true;
let mut total_cost = NanoTokens::zero();

let mut payment_map = BTreeMap::default();
Expand Down
13 changes: 4 additions & 9 deletions sn_node/tests/storage_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn storage_payment_succeeds() -> Result<()> {
);

let _cost = wallet_client
.pay_for_storage(random_content_addrs.clone().into_iter(), true)
.pay_for_storage(random_content_addrs.clone().into_iter())
.await?;

println!("Verifying balance has been paid from the wallet...");
Expand Down Expand Up @@ -90,7 +90,6 @@ async fn storage_payment_fails_with_insufficient_money() -> Result<()> {
.into_iter()
.take(subset_len)
.map(|(name, _)| NetworkAddress::ChunkAddress(ChunkAddress::new(name))),
true,
)
.await?;

Expand Down Expand Up @@ -131,10 +130,7 @@ async fn storage_payment_proofs_cached_in_wallet() -> Result<()> {
let subset_len = random_content_addrs.len() / 3;
println!("Paying for {subset_len} random addresses...",);
let storage_cost = wallet_client
.pay_for_storage(
random_content_addrs.clone().into_iter().take(subset_len),
true,
)
.pay_for_storage(random_content_addrs.clone().into_iter().take(subset_len))
.await?;

// check we've paid only for the subset of addresses, 1 nano per addr
Expand All @@ -154,7 +150,7 @@ async fn storage_payment_proofs_cached_in_wallet() -> Result<()> {
// now let's request to pay for all addresses, even that we've already paid for a subset of them
let mut wallet_client = WalletClient::new(client.clone(), paying_wallet);
let storage_cost = wallet_client
.pay_for_storage(random_content_addrs.clone().into_iter(), false)
.pay_for_storage(random_content_addrs.clone().into_iter())
.await?;

// check we've paid only for addresses we haven't previously paid for, 1 nano per addr
Expand Down Expand Up @@ -198,7 +194,6 @@ async fn storage_payment_chunk_upload_succeeds() -> Result<()> {
chunks
.iter()
.map(|(name, _)| NetworkAddress::ChunkAddress(ChunkAddress::new(*name))),
true,
)
.await?;

Expand Down Expand Up @@ -288,7 +283,7 @@ async fn storage_payment_register_creation_succeeds() -> Result<()> {
let net_addr = NetworkAddress::from_register_address(address);

let _cost = wallet_client
.pay_for_storage(std::iter::once(net_addr), true)
.pay_for_storage(std::iter::once(net_addr))
.await?;

let (mut register, _cost) = client
Expand Down
1 change: 0 additions & 1 deletion sn_node/tests/verify_data_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ async fn store_chunks(
chunks
.iter()
.map(|(name, _)| NetworkAddress::ChunkAddress(ChunkAddress::new(*name))),
true,
)
.await
.expect("Failed to pay for storage for new file at {file_addr:?}");
Expand Down

0 comments on commit bc0cb8d

Please sign in to comment.