Skip to content

Commit

Permalink
Add WalletCommand::SetPow command
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Sep 21, 2023
1 parent 610dc50 commit 89c5654
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `outputs`, `unspent-outputs` print a list that includes number and type of the output;
- `Account::switch` command to allow changing accounts quickly;
- UX improvements (Ctrl+l, TAB completion/suggestions and more) during interactive account management;
- `WalletCommand::SetPow` command;

### Changed

Expand Down
28 changes: 28 additions & 0 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ pub enum WalletCommand {
/// Node URL to use for all future operations.
url: String,
},
/// Set the PoW options.
SetPow {
/// Whether the PoW should be done locally or remotely.
#[arg(short, long, action = clap::ArgAction::Set)]
local_pow: bool,
/// The amount of workers that should be used for PoW, default is num_cpus::get().
#[arg(short, long)]
worker_count: Option<usize>,
},
/// Synchronize all accounts.
Sync,
}
Expand Down Expand Up @@ -272,6 +281,25 @@ pub async fn set_node_url_command(storage_path: &Path, snapshot_path: &Path, url
Ok(wallet)
}

pub async fn set_pow_command(
storage_path: &Path,
snapshot_path: &Path,
local_pow: bool,
worker_count: Option<usize>,
) -> Result<Wallet, Error> {
let password = get_password("Stronghold password", !snapshot_path.exists())?;
let wallet = unlock_wallet(storage_path, snapshot_path, password).await?;
// Need to get the current node, so it's not removed
let node = wallet.client().get_node().await?;
let client_options = ClientOptions::new()
.with_node(node.url.as_ref())?
.with_local_pow(local_pow)
.with_pow_worker_count(worker_count);
wallet.set_client_options(client_options).await?;

Ok(wallet)
}

pub async fn sync_command(storage_path: &Path, snapshot_path: &Path) -> Result<Wallet, Error> {
let password = get_password("Stronghold password", !snapshot_path.exists())?;
let wallet = unlock_wallet(storage_path, snapshot_path, password).await?;
Expand Down
10 changes: 9 additions & 1 deletion cli/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::{
command::wallet::{
accounts_command, add_account, backup_command, change_password_command, init_command,
migrate_stronghold_snapshot_v2_to_v3_command, mnemonic_command, new_account_command, node_info_command,
restore_command, set_node_url_command, sync_command, unlock_wallet, InitParameters, WalletCli, WalletCommand,
restore_command, set_node_url_command, set_pow_command, sync_command, unlock_wallet, InitParameters, WalletCli,
WalletCommand,
},
error::Error,
helper::{get_account_alias, get_decision, get_password, pick_account},
Expand Down Expand Up @@ -54,6 +55,13 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<Accoun
let wallet = set_node_url_command(storage_path, snapshot_path, url).await?;
(Some(wallet), None)
}
WalletCommand::SetPow {
local_pow,
worker_count,
} => {
let wallet = set_pow_command(storage_path, snapshot_path, local_pow, worker_count).await?;
(Some(wallet), None)
}
WalletCommand::Sync => {
let wallet = sync_command(storage_path, snapshot_path).await?;
(Some(wallet), None)
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl ClientInner {
if !self.get_fallback_to_local_pow().await {
return Err(Error::Node(crate::client::node_api::error::Error::UnavailablePow));
}
log::debug!("[post_block] falling back to local PoW");

self.network_info.write().await.local_pow = true;

Expand Down
1 change: 1 addition & 0 deletions sdk/src/wallet/account/operations/output_consolidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ where

drop(account_details);

#[allow(clippy::option_if_let_else)]
let output_threshold = match params.output_threshold {
Some(t) => t,
None => {
Expand Down

0 comments on commit 89c5654

Please sign in to comment.