From 121a22c8305eda580d09cb9bda92ca894a229371 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Thu, 21 Sep 2023 19:53:59 +0200 Subject: [PATCH] Add WalletCommand::SetPow command (#1280) --- cli/CHANGELOG.md | 1 + cli/src/command/wallet.rs | 28 +++++++++++++++++++ cli/src/wallet.rs | 10 ++++++- sdk/src/client/node_api/core/routes.rs | 1 + .../operations/output_consolidation.rs | 1 + 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index db9b54a62e..8c42e0a2ba 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -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 diff --git a/cli/src/command/wallet.rs b/cli/src/command/wallet.rs index 06e33286fc..605baee51e 100644 --- a/cli/src/command/wallet.rs +++ b/cli/src/command/wallet.rs @@ -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, + }, /// Synchronize all accounts. Sync, } @@ -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, +) -> Result { + 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 { let password = get_password("Stronghold password", !snapshot_path.exists())?; let wallet = unlock_wallet(storage_path, snapshot_path, password).await?; diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index e8ce5bdf4f..b098c9d675 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -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}, @@ -54,6 +55,13 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option, Option { + 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) diff --git a/sdk/src/client/node_api/core/routes.rs b/sdk/src/client/node_api/core/routes.rs index ec459f59d2..091365838c 100644 --- a/sdk/src/client/node_api/core/routes.rs +++ b/sdk/src/client/node_api/core/routes.rs @@ -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; diff --git a/sdk/src/wallet/account/operations/output_consolidation.rs b/sdk/src/wallet/account/operations/output_consolidation.rs index 071b2d1c11..b8a0f5bbeb 100644 --- a/sdk/src/wallet/account/operations/output_consolidation.rs +++ b/sdk/src/wallet/account/operations/output_consolidation.rs @@ -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 => {