From 72a41ea645b6edb624eb69a3a3f3488a1f283281 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Wed, 18 Oct 2023 11:52:10 +0200 Subject: [PATCH] Revert "feat: keep transfers in mem instead of mem and i/o heavy cashnotes" This reverts commit 9eb32bbf632b32233ad9d7e17126f5e2bbea1d5c. --- sn_client/src/wallet.rs | 10 ++++--- .../src/transfers/offline_transfer.rs | 4 +-- sn_transfers/src/wallet/local_store.rs | 27 ++++++++++++------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/sn_client/src/wallet.rs b/sn_client/src/wallet.rs index 0ce6936905..622a4e6233 100644 --- a/sn_client/src/wallet.rs +++ b/sn_client/src/wallet.rs @@ -59,13 +59,13 @@ impl WalletClient { pub fn get_payment_transfers(&self, address: &NetworkAddress) -> WalletResult> { match &address.as_xorname() { Some(xorname) => { - let transfers = self.wallet.get_payment_transfers(xorname); + let cash_notes = self.wallet.get_payment_cash_notes(xorname); info!( - "Payment transfers retrieved for {xorname:?} from wallet: {:?}", - transfers.len() + "Payment cash notes retrieved from wallet: {:?}", + cash_notes.len() ); - Ok(transfers) + Ok(Transfer::transfers_from_cash_notes(cash_notes)?) } None => Err(WalletError::InvalidAddressType), } @@ -207,6 +207,8 @@ impl WalletClient { all_data_payments: BTreeMap>, verify_store: bool, ) -> WalletResult { + // TODO: + // Check for any existing payment CashNotes, and use them if they exist, only topping up if needs be let mut total_cost = NanoTokens::zero(); for (_data, costs) in all_data_payments.iter() { for (_target, cost) in costs { diff --git a/sn_transfers/src/transfers/offline_transfer.rs b/sn_transfers/src/transfers/offline_transfer.rs index ee984a8416..a401349123 100644 --- a/sn_transfers/src/transfers/offline_transfer.rs +++ b/sn_transfers/src/transfers/offline_transfer.rs @@ -8,7 +8,7 @@ use crate::{ rng, CashNote, DerivationIndex, DerivedSecretKey, Hash, Input, MainPubkey, NanoTokens, - SignedSpend, Transaction, TransactionBuilder, Transfer, UniquePubkey, + SignedSpend, Transaction, TransactionBuilder, UniquePubkey, }; use crate::{Error, Result}; @@ -36,7 +36,7 @@ pub struct OfflineTransfer { pub all_spend_requests: Vec, } -pub type PaymentDetails = (Transfer, MainPubkey, NanoTokens); +pub type PaymentDetails = (UniquePubkey, MainPubkey, NanoTokens); /// Xorname of data from which the content was fetched, mapping to the CashNote UniquePubkey (its id on disk) /// the main key for that CashNote and the value diff --git a/sn_transfers/src/wallet/local_store.rs b/sn_transfers/src/wallet/local_store.rs index de538d7220..4ea2578ef9 100644 --- a/sn_transfers/src/wallet/local_store.rs +++ b/sn_transfers/src/wallet/local_store.rs @@ -230,17 +230,24 @@ impl LocalWallet { } /// Return the payment cash_note ids for the given content address name if cached. - pub fn get_payment_transfers(&self, name: &XorName) -> Vec { - let mut transfers: Vec = vec![]; - - if let Some(payment) = self.get_payment_unique_pubkeys_and_values(name) { - for (trans, _main_pub_key, value) in payment { - trace!("Current transfer for chunk {name:?} is of {value:?} tokens."); - transfers.push(trans.to_owned()); + pub fn get_payment_cash_notes(&self, name: &XorName) -> Vec { + let ids = self.get_payment_unique_pubkeys_and_values(name); + // now grab all those cash_notes + let mut cash_notes: Vec = vec![]; + + if let Some(ids) = ids { + for (id, _main_pub_key, _value) in ids { + if let Some(cash_note) = load_created_cash_note(id, &self.wallet_dir) { + trace!( + "Current cash_note of chunk {name:?} is paying {:?} tokens.", + cash_note.value() + ); + cash_notes.push(cash_note); + } } } - transfers + cash_notes } /// Make a transfer and return all created cash_notes @@ -326,7 +333,7 @@ impl LocalWallet { for (content_addr, payees) in all_data_payments { for (payee, token) in payees { if let Some(cash_note) = - offline_transfer + &offline_transfer .created_cash_notes .iter() .find(|cash_note| { @@ -340,7 +347,7 @@ impl LocalWallet { let cash_notes_for_content: &mut Vec = all_transfers_per_address.entry(content_addr).or_default(); cash_notes_for_content.push(( - Transfer::transfers_from_cash_note(cash_note.to_owned())?, + cash_note.unique_pubkey(), *cash_note.main_pubkey(), cash_note.value()?, ));