From 9eb32bbf632b32233ad9d7e17126f5e2bbea1d5c Mon Sep 17 00:00:00 2001 From: grumbach Date: Wed, 18 Oct 2023 14:53:10 +0900 Subject: [PATCH] feat: keep transfers in mem instead of mem and i/o heavy cashnotes --- sn_client/src/wallet.rs | 10 +++---- .../src/transfers/offline_transfer.rs | 4 +-- sn_transfers/src/wallet/local_store.rs | 27 +++++++------------ 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/sn_client/src/wallet.rs b/sn_client/src/wallet.rs index 128c1c43fc..6786179207 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 cash_notes = self.wallet.get_payment_cash_notes(xorname); + let transfers = self.wallet.get_payment_transfers(xorname); info!( - "Payment cash notes retrieved from wallet: {:?}", - cash_notes.len() + "Payment transfers retrieved for {xorname:?} from wallet: {:?}", + transfers.len() ); - Ok(Transfer::transfers_from_cash_notes(cash_notes)?) + Ok(transfers) } None => Err(WalletError::InvalidAddressType), } @@ -205,8 +205,6 @@ 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 a401349123..ee984a8416 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, UniquePubkey, + SignedSpend, Transaction, TransactionBuilder, Transfer, UniquePubkey, }; use crate::{Error, Result}; @@ -36,7 +36,7 @@ pub struct OfflineTransfer { pub all_spend_requests: Vec, } -pub type PaymentDetails = (UniquePubkey, MainPubkey, NanoTokens); +pub type PaymentDetails = (Transfer, 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 4ea2578ef9..de538d7220 100644 --- a/sn_transfers/src/wallet/local_store.rs +++ b/sn_transfers/src/wallet/local_store.rs @@ -230,24 +230,17 @@ impl LocalWallet { } /// Return the payment cash_note ids for the given content address name if cached. - 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); - } + 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()); } } - cash_notes + transfers } /// Make a transfer and return all created cash_notes @@ -333,7 +326,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| { @@ -347,7 +340,7 @@ impl LocalWallet { let cash_notes_for_content: &mut Vec = all_transfers_per_address.entry(content_addr).or_default(); cash_notes_for_content.push(( - cash_note.unique_pubkey(), + Transfer::transfers_from_cash_note(cash_note.to_owned())?, *cash_note.main_pubkey(), cash_note.value()?, ));