Skip to content

Commit

Permalink
fix(uploader): clarify the use of root and wallet dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Mar 27, 2024
1 parent 062a938 commit c49ff0e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
8 changes: 4 additions & 4 deletions sn_cli/src/files/iterative_uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions sn_client/src/uploader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down
21 changes: 10 additions & 11 deletions sn_client/src/uploader/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");

Expand Down Expand Up @@ -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<XorName, UploadItem>,
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -803,8 +803,7 @@ impl InnerUploader {
task_result_sender: mpsc::Sender<TaskResult>,
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.");
Expand Down Expand Up @@ -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<WalletClient> {
fn load_wallet_client(client: Client, root_dir: &Path) -> Result<WalletClient> {
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))
}
Expand Down
9 changes: 4 additions & 5 deletions sn_transfers/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions sn_transfers/src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// Cached version of `wallet_dir/payment`
/// Cached version of `root_dir/wallet_dir/payments`
payment_dir: Arc<PathBuf>,
}

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)),
Expand Down
4 changes: 1 addition & 3 deletions sn_transfers/src/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions sn_transfers/src/wallet/watch_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit c49ff0e

Please sign in to comment.