From c49ff0e0561b8ee607c3b6ad457f488bd81338ae Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Wed, 27 Mar 2024 19:10:27 +0530 Subject: [PATCH] fix(uploader): clarify the use of root and wallet dirs --- sn_cli/src/files/iterative_uploader.rs | 8 ++++---- sn_client/src/uploader/mod.rs | 4 ++-- sn_client/src/uploader/upload.rs | 21 ++++++++++----------- sn_transfers/src/wallet.rs | 9 ++++----- sn_transfers/src/wallet/api.rs | 16 +++++++++++++--- sn_transfers/src/wallet/hot_wallet.rs | 4 +--- sn_transfers/src/wallet/watch_only.rs | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/sn_cli/src/files/iterative_uploader.rs b/sn_cli/src/files/iterative_uploader.rs index 9b97604d89..0ab9e0c2c2 100644 --- a/sn_cli/src/files/iterative_uploader.rs +++ b/sn_cli/src/files/iterative_uploader.rs @@ -29,15 +29,15 @@ use xor_name::XorName; pub struct IterativeUploader { chunk_manager: ChunkManager, client: Client, - wallet_dir: PathBuf, + root_dir: PathBuf, } impl IterativeUploader { - pub fn new(chunk_manager: ChunkManager, client: Client, wallet_dir: PathBuf) -> Self { + pub fn new(chunk_manager: ChunkManager, client: Client, root_dir: PathBuf) -> Self { Self { chunk_manager, client, - wallet_dir, + root_dir, } } } @@ -58,7 +58,7 @@ impl IterativeUploader { batch_size, retry_strategy, } = options; - let mut uploader = Uploader::new(self.client, self.wallet_dir) + let mut uploader = Uploader::new(self.client, self.root_dir) .set_batch_size(batch_size) .set_verify_store(verify_store) .set_retry_strategy(retry_strategy); diff --git a/sn_client/src/uploader/mod.rs b/sn_client/src/uploader/mod.rs index 4a9aadbc7b..f45e580140 100644 --- a/sn_client/src/uploader/mod.rs +++ b/sn_client/src/uploader/mod.rs @@ -92,9 +92,9 @@ impl Uploader { /// Creates a new instance of `Uploader` with the default configuration. /// To modify the configuration, use the provided setter methods (`set_...` functions). // NOTE: Self has to be constructed only using this method. We expect `Self::inner` is present everywhere. - pub fn new(client: Client, wallet_dir: PathBuf) -> Self { + pub fn new(client: Client, root_dir: PathBuf) -> Self { Self { - inner: Some(InnerUploader::new(client, wallet_dir)), + inner: Some(InnerUploader::new(client, root_dir)), } } diff --git a/sn_client/src/uploader/upload.rs b/sn_client/src/uploader/upload.rs index 633fb4f5d3..9267bb5483 100644 --- a/sn_client/src/uploader/upload.rs +++ b/sn_client/src/uploader/upload.rs @@ -109,11 +109,9 @@ pub(super) async fn start_upload( if uploader.all_upload_items.is_empty() { debug!("Upload items are empty, exiting main upload loop."); // To avoid empty final_balance when all items are skipped. - uploader.upload_final_balance = InnerUploader::load_wallet_client( - uploader.client.clone(), - uploader.wallet_api.wallet_dir(), - )? - .balance(); + uploader.upload_final_balance = + InnerUploader::load_wallet_client(uploader.client.clone(), &uploader.root_dir)? + .balance(); #[cfg(test)] trace!("UPLOADER STATE: finished uploading all items {uploader:?}"); @@ -649,6 +647,7 @@ pub(super) struct InnerUploader { pub(super) client: Client, #[debug(skip)] pub(super) wallet_api: WalletApi, + pub(super) root_dir: PathBuf, // states pub(super) all_upload_items: HashMap, @@ -691,7 +690,7 @@ pub(super) struct InnerUploader { } impl InnerUploader { - pub(super) fn new(client: Client, wallet_dir: PathBuf) -> Self { + pub(super) fn new(client: Client, root_dir: PathBuf) -> Self { Self { batch_size: BATCH_SIZE, verify_store: true, @@ -701,7 +700,8 @@ impl InnerUploader { collect_registers: false, client, - wallet_api: WalletApi::new(&wallet_dir), + wallet_api: WalletApi::new_from_root_dir(&root_dir), + root_dir, all_upload_items: Default::default(), pending_to_get_register: Default::default(), @@ -803,8 +803,7 @@ impl InnerUploader { task_result_sender: mpsc::Sender, batch_size: usize, ) -> Result<()> { - let mut wallet_client = - Self::load_wallet_client(self.client.clone(), self.wallet_api.wallet_dir())?; + let mut wallet_client = Self::load_wallet_client(self.client.clone(), &self.root_dir)?; let verify_store = self.verify_store; let _handle = tokio::spawn(async move { debug!("Spawning the long running make payment processing loop."); @@ -1035,9 +1034,9 @@ impl InnerUploader { } /// Create a new WalletClient for a given root directory. - fn load_wallet_client(client: Client, wallet_dir: &Path) -> Result { + fn load_wallet_client(client: Client, root_dir: &Path) -> Result { let wallet = - HotWallet::load_from(wallet_dir).map_err(|_| ClientError::FailedToAccessWallet)?; + HotWallet::load_from(root_dir).map_err(|_| ClientError::FailedToAccessWallet)?; Ok(WalletClient::new(client, wallet)) } diff --git a/sn_transfers/src/wallet.rs b/sn_transfers/src/wallet.rs index b2f3c4391e..a55e936359 100644 --- a/sn_transfers/src/wallet.rs +++ b/sn_transfers/src/wallet.rs @@ -61,21 +61,20 @@ mod wallet_file; mod watch_only; pub use self::{ - api::WalletApi, + api::{WalletApi, WALLET_DIR_NAME}, data_payments::{Payment, PaymentQuote, QuotingMetrics, QUOTE_EXPIRATION_SECS}, error::{Error, Result}, hot_wallet::HotWallet, keys::bls_secret_from_hex, + wallet_file::wallet_lockfile_name, watch_only::WatchOnlyWallet, }; -use crate::{NanoTokens, UniquePubkey}; -pub use hot_wallet::WALLET_DIR_NAME; pub(crate) use keys::store_new_keypair; -use wallet_file::wallet_file_name; -pub use wallet_file::wallet_lockfile_name; +use crate::{NanoTokens, UniquePubkey}; use serde::{Deserialize, Serialize}; use std::{collections::BTreeMap, fs, path::Path}; +use wallet_file::wallet_file_name; #[derive(Default, Serialize, Deserialize)] pub(super) struct KeyLessWallet { diff --git a/sn_transfers/src/wallet/api.rs b/sn_transfers/src/wallet/api.rs index d2875ad161..bfea5a8ebc 100644 --- a/sn_transfers/src/wallet/api.rs +++ b/sn_transfers/src/wallet/api.rs @@ -17,19 +17,29 @@ use std::{ use xor_name::XorName; const PAYMENTS_DIR_NAME: &str = "payments"; +pub const WALLET_DIR_NAME: &str = "wallet"; /// Contains some common API's used by wallet implementations. #[derive(serde::Serialize, serde::Deserialize, Clone)] pub struct WalletApi { /// The dir of the wallet file, main key, public address, and new cash_notes. wallet_dir: Arc, - /// Cached version of `wallet_dir/payment` + /// Cached version of `root_dir/wallet_dir/payments` payment_dir: Arc, } impl WalletApi { - /// Create a new instance of the StateLessWallet - pub fn new(wallet_dir: &Path) -> Self { + /// Create a new instance give the root dir. + pub fn new_from_root_dir(root_dir: &Path) -> Self { + let wallet_dir = root_dir.join(WALLET_DIR_NAME); + Self { + payment_dir: Arc::new(wallet_dir.join(PAYMENTS_DIR_NAME)), + wallet_dir: Arc::new(wallet_dir), + } + } + + /// Create a new instance give the root dir. + pub fn new_from_wallet_dir(wallet_dir: &Path) -> Self { Self { wallet_dir: Arc::new(wallet_dir.to_path_buf()), payment_dir: Arc::new(wallet_dir.join(PAYMENTS_DIR_NAME)), diff --git a/sn_transfers/src/wallet/hot_wallet.rs b/sn_transfers/src/wallet/hot_wallet.rs index 49541b76d2..bcf23d97ae 100644 --- a/sn_transfers/src/wallet/hot_wallet.rs +++ b/sn_transfers/src/wallet/hot_wallet.rs @@ -7,7 +7,7 @@ // permissions and limitations relating to use of the SAFE Network Software. use super::{ - api::WalletApi, + api::{WalletApi, WALLET_DIR_NAME}, data_payments::{PaymentDetails, PaymentQuote}, keys::{get_main_key, store_new_keypair}, wallet_file::{ @@ -34,8 +34,6 @@ use std::{ }; use xor_name::XorName; -pub const WALLET_DIR_NAME: &str = "wallet"; - /// A locked file handle, that when dropped releases the lock. pub type WalletExclusiveAccess = File; diff --git a/sn_transfers/src/wallet/watch_only.rs b/sn_transfers/src/wallet/watch_only.rs index 7c9a885e3d..38d4338611 100644 --- a/sn_transfers/src/wallet/watch_only.rs +++ b/sn_transfers/src/wallet/watch_only.rs @@ -53,7 +53,7 @@ impl WatchOnlyWallet { ) -> Self { Self { main_pubkey, - api: WalletApi::new(wallet_dir), + api: WalletApi::new_from_wallet_dir(wallet_dir), wallet_dir: wallet_dir.to_path_buf(), keyless_wallet, } @@ -277,7 +277,7 @@ impl WatchOnlyWallet { Ok(Self { main_pubkey, - api: WalletApi::new(wallet_dir), + api: WalletApi::new_from_wallet_dir(wallet_dir), wallet_dir: wallet_dir.to_path_buf(), keyless_wallet, })