From dc1ad6df7d5ef06b2a9cacf1a7ed967fe21f232e Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:23:47 +0100 Subject: [PATCH 01/11] Upgrade monero-wallet-rpc to `v0.18.3.1` --- swap/src/monero/wallet_rpc.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index e44d800e..cecdd620 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -44,20 +44,20 @@ const MONERO_DAEMONS: [MoneroDaemon; 17] = [ compile_error!("unsupported operating system"); #[cfg(all(target_os = "macos", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.1.2.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.0.0.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.1.2.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "arm"))] const DOWNLOAD_URL: &str = - "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.1.2.tar.bz2"; + "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2"; #[cfg(target_os = "windows")] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.1.2.zip"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.3.1.zip"; #[cfg(any(target_os = "macos", target_os = "linux"))] const PACKED_FILE: &str = "monero-wallet-rpc"; @@ -65,7 +65,7 @@ const PACKED_FILE: &str = "monero-wallet-rpc"; #[cfg(target_os = "windows")] const PACKED_FILE: &str = "monero-wallet-rpc.exe"; -const WALLET_RPC_VERSION: &str = "v0.18.1.2"; +const WALLET_RPC_VERSION: &str = "v0.18.3.1"; #[derive(Debug, Clone, Copy, thiserror::Error)] #[error("monero wallet rpc executable not found in downloaded archive")] From 9e33e8b1d193e59b07c549c90f622af8b6a2a502 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:22:55 +0100 Subject: [PATCH 02/11] Give feedback to user about state of monero refresh and retry if fails This commit changes the following behaviour in the refresh functionality of the monero wallet - Allows for multiple retries because in some cases users have experienced an issue where the wallet rpc returns `no connection to daemon` even though the daemon is available. I'm not 100% sure why this happens but retrying often fixes the issue - Attempt to print the current sync height while the wallet is syncing. This only works to some degree because the `monero-wallet-rpc` stops responding (or takes a long time to respond) while it's refreshing - The `monero-wallet-rpc` is started with the `--no-initial-sync` flag which ensures that as soon as it's started, it's ready to respond to requests --- swap/src/monero/wallet.rs | 68 +++++++++++++++++++++++++++++++++-- swap/src/monero/wallet_rpc.rs | 1 + swap/src/protocol/bob/swap.rs | 2 +- swap/tests/harness/mod.rs | 2 +- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 56fd8e60..3ba99aac 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -45,6 +45,7 @@ impl Wallet { pub async fn connect(client: wallet::Client, name: String, env_config: Config) -> Result { let main_address = monero::Address::from_str(client.get_address(0).await?.address.as_str())?; + Ok(Self { inner: Mutex::new(client), network: env_config.monero_network, @@ -144,7 +145,7 @@ impl Wallet { .await?; // Try to send all the funds from the generated wallet to the default wallet - match wallet.refresh().await { + match self.refresh(3).await { Ok(_) => match wallet.sweep_all(self.main_address.to_string()).await { Ok(sweep_all) => { for tx in sweep_all.tx_hash_list { @@ -261,8 +262,69 @@ impl Wallet { self.main_address } - pub async fn refresh(&self) -> Result { - Ok(self.inner.lock().await.refresh().await?) + pub async fn refresh(&self, max_attempts: usize) -> Result { + const GET_HEIGHT_INTERVAL: Duration = Duration::from_secs(5); + const RETRY_INTERVAL: Duration = Duration::from_secs(2); + + let inner = self.inner.lock().await; + + // Cloning this is relatively cheap because reqwest::Client is a wrapper around an Arc + let inner_clone = inner.clone(); + let wallet_name_clone = self.name.clone(); + + let refresh_task = tokio::task::spawn(async move { + loop { + let height = inner_clone.get_height().await; + + match height { + Err(error) => { + tracing::warn!(name = %wallet_name_clone, %error, "Failed to get current Monero wallet sync height"); + } + Ok(height) => { + tracing::debug!(name = %wallet_name_clone, current_sync_height = height.height, "Syncing Monero wallet"); + } + } + + tokio::time::sleep(GET_HEIGHT_INTERVAL).await; + } + }); + + let refresh_result = tokio::select! { + biased; + _ = refresh_task => { + unreachable!("Current sync height refresh task should never finish") + } + refresh_result = async { + for i in 1..=max_attempts { + tracing::info!(name = %self.name, attempt=i, "Syncing Monero wallet"); + + let result = inner.refresh().await; + + match result { + Ok(refreshed) => { + tracing::info!(name = %self.name, "Monero wallet synced"); + return Ok(refreshed); + } + Err(error) => { + let attempts_left = max_attempts - i; + tracing::warn!(attempt=i, %attempts_left, name = %self.name, %error, "Failed to sync Monero wallet"); + + if attempts_left == 0 { + return Err(error); + } + } + } + + tokio::time::sleep(RETRY_INTERVAL).await; + } + + unreachable!("Loop should always return before it breaks") + } => { + refresh_result + } + }; + + Ok(refresh_result?) } } diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index cecdd620..e2f019a3 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -309,6 +309,7 @@ impl WalletRpc { .arg("--disable-rpc-login") .arg("--wallet-dir") .arg(self.working_dir.join("monero-data")) + .arg("--no-initial-sync") .spawn()?; let stdout = child diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 66933a87..fa4cd8d3 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -247,7 +247,7 @@ async fn next_state( } // Ensure that the generated wallet is synced so we have a proper balance - monero_wallet.refresh().await?; + monero_wallet.refresh(3).await?; // Sweep (transfer all funds) to the given address let tx_hashes = monero_wallet.sweep_all(monero_receive_address).await?; diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 4f3f5fee..e81c78d3 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -865,7 +865,7 @@ impl Wallet for monero::Wallet { type Amount = monero::Amount; async fn refresh(&self) -> Result<()> { - self.refresh().await?; + self.refresh(1).await?; Ok(()) } From 07101deab1d4b802ce0d82e9d9020ef6985f3920 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 14 Dec 2023 18:34:30 +0100 Subject: [PATCH 03/11] Unify monero-wallet-rpc downloader logging --- swap/src/monero/wallet.rs | 2 +- swap/src/monero/wallet_rpc.rs | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 3ba99aac..8792994c 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -278,7 +278,7 @@ impl Wallet { match height { Err(error) => { - tracing::warn!(name = %wallet_name_clone, %error, "Failed to get current Monero wallet sync height"); + tracing::debug!(name = %wallet_name_clone, %error, "Failed to get current Monero wallet sync height"); } Ok(height) => { tracing::debug!(name = %wallet_name_clone, current_sync_height = height.height, "Syncing Monero wallet"); diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index e2f019a3..8973c021 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -221,9 +221,10 @@ impl WalletRpc { .parse::()?; tracing::info!( - "Downloading monero-wallet-rpc ({}) from {}", - content_length.big_byte(2), - DOWNLOAD_URL + progress="0%", + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", ); let byte_stream = response @@ -250,12 +251,24 @@ impl WalletRpc { let total = 3 * content_length; let percent = 100 * received as u64 / total; if percent != notified && percent % 10 == 0 { - tracing::debug!("{}%", percent); + tracing::info!( + progress=format!("{}%", percent), + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", + ); notified = percent; } file.write_all(&bytes).await?; } + tracing::info!( + progress="100%", + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", + ); + file.flush().await?; tracing::debug!("Extracting archive"); From 48abcd5b437597cae3e1c6684d821ab551701e28 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 14 Dec 2023 20:58:28 +0100 Subject: [PATCH 04/11] Add extra log message before opening redeem XMR wallet on Bob --- swap/src/protocol/bob/swap.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index fa4cd8d3..92d4f628 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -227,6 +227,9 @@ async fn next_state( let (spend_key, view_key) = state.xmr_keys(); let wallet_file_name = swap_id.to_string(); + + tracing::info!(%wallet_file_name, "Generating and opening Monero wallet from the extracted keys to redeem the Monero"); + if let Err(e) = monero_wallet .create_from_and_load( wallet_file_name.clone(), From bd3e6136cecbb2bfd1ed5c940e1543d1f1970790 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Fri, 29 Dec 2023 14:13:20 +0100 Subject: [PATCH 05/11] Update wallet.rs --- swap/src/monero/wallet.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 8792994c..61ed4ca4 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -310,6 +310,7 @@ impl Wallet { tracing::warn!(attempt=i, %attempts_left, name = %self.name, %error, "Failed to sync Monero wallet"); if attempts_left == 0 { + tracing::error!(name = %self.name, %error, "Failed to sync Monero wallet"); return Err(error); } } From eb15f477fac1872af4028ded403c511993ea2bed Mon Sep 17 00:00:00 2001 From: Ian McKenzie <13459320+ikmckenz@users.noreply.github.com> Date: Fri, 22 Mar 2024 02:18:40 -0700 Subject: [PATCH 06/11] Verify hashes of monero cli on download (#1572) * Bump Monero CLI for macos aarch64 to match other platforms * Check hash on download of monero cli * change panic to bail --------- Co-authored-by: Byron Hambly --- Cargo.lock | 1 + swap/Cargo.toml | 1 + swap/src/monero/wallet_rpc.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index c7736aea..8c58895b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4290,6 +4290,7 @@ dependencies = [ "curve25519-dalek-ng", "data-encoding", "dialoguer", + "digest 0.10.7", "directories-next", "ecdsa_fun", "ed25519-dalek", diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 3cb84995..8b60517a 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -25,6 +25,7 @@ conquer-once = "0.4" curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" } data-encoding = "2.5" dialoguer = "0.11" +digest = "0.10.7" directories-next = "2" ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] } ed25519-dalek = "1" diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index e44d800e..e227c3f3 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -1,11 +1,13 @@ use ::monero::Network; use anyhow::{bail, Context, Error, Result}; use big_bytes::BigByte; +use data_encoding::HEXLOWER; use futures::{StreamExt, TryStreamExt}; use monero_rpc::wallet::{Client, MoneroWalletRpc as _}; use reqwest::header::CONTENT_LENGTH; use reqwest::Url; use serde::Deserialize; +use sha2::{Digest, Sha256}; use std::fmt; use std::fmt::{Debug, Display, Formatter}; use std::io::ErrorKind; @@ -45,19 +47,29 @@ compile_error!("unsupported operating system"); #[cfg(all(target_os = "macos", target_arch = "x86_64"))] const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.1.2.tar.bz2"; +#[cfg(all(target_os = "macos", target_arch = "x86_64"))] +const DOWNLOAD_HASH: &str = "ba1108c7a5e5efe15b6a628fb007c50f01c231f61137bba7427605286dbc6f01"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.0.0.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.1.2.tar.bz2"; +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +const DOWNLOAD_HASH: &str = "620b825c04f84845ed09de03b207a3230a34f74b30a8a07dde504a7d376ee4b9"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.1.2.tar.bz2"; +#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +const DOWNLOAD_HASH: &str = "7d51e7072351f65d0c7909e745827cfd3b00abe5e7c4cc4c104a3c9b526da07e"; #[cfg(all(target_os = "linux", target_arch = "arm"))] const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.1.2.tar.bz2"; +#[cfg(all(target_os = "linux", target_arch = "arm"))] +const DOWNLOAD_HASH: &str = "94ece435ed60f85904114643482c2b6716f74bf97040a7af237450574a9cf06d"; #[cfg(target_os = "windows")] const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.1.2.zip"; +#[cfg(target_os = "windows")] +const DOWNLOAD_HASH: &str = "0a3d4d1af7e094c05352c31b2dafcc6ccbc80edc195ca9eaedc919c36accd05a"; #[cfg(any(target_os = "macos", target_os = "linux"))] const PACKED_FILE: &str = "monero-wallet-rpc"; @@ -226,8 +238,14 @@ impl WalletRpc { DOWNLOAD_URL ); + let mut hasher = Sha256::new(); + let byte_stream = response .bytes_stream() + .map_ok(|bytes| { + hasher.update(&bytes); + bytes + }) .map_err(|err| std::io::Error::new(ErrorKind::Other, err)); #[cfg(not(target_os = "windows"))] @@ -256,6 +274,18 @@ impl WalletRpc { file.write_all(&bytes).await?; } + let result = hasher.finalize(); + let result_hash = HEXLOWER.encode(result.as_ref()); + if result_hash != DOWNLOAD_HASH { + bail!( + "SHA256 of download ({}) does not match expected ({})!", + result_hash, + DOWNLOAD_HASH + ); + } else { + tracing::debug!("Hashes match"); + } + file.flush().await?; tracing::debug!("Extracting archive"); From 85cee51eeee41055570eb4cf73d43a6982562287 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:05:21 +0100 Subject: [PATCH 07/11] Run dprint fmt --- swap/src/monero/wallet_rpc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index d0d3d6ed..bb696ba0 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -61,7 +61,8 @@ const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64 const DOWNLOAD_HASH: &str = "23af572fdfe3459b9ab97e2e9aa7e3c11021c955d6064b801a27d7e8c21ae09d"; #[cfg(all(target_os = "linux", target_arch = "arm"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2"; +const DOWNLOAD_URL: &str = + "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "arm"))] const DOWNLOAD_HASH: &str = "2ea2c8898cbab88f49423f4f6c15f2a94046cb4bbe827493dd061edc0fd5f1ca"; From bfc1e829dc6f4eed81d362d34456183d04974edd Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:07:11 +0100 Subject: [PATCH 08/11] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3ad105..594cae30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Minimum Supported Rust Version (MSRV) bumped to 1.70 +- Update monero-wallet-rpc version to v0.18.3.1 +- Add retry logic to monero-wallet-rpc wallet refresh ## [0.12.3] - 2023-09-20 From d8dacbdee99a8d951977e62db1c888cc9ed56ea8 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:08:54 +0100 Subject: [PATCH 09/11] Monero wallet refresh fix (#1487) * Upgrade monero-wallet-rpc to `v0.18.3.1` * Give feedback to user about state of monero refresh and retry if fails This commit changes the following behaviour in the refresh functionality of the monero wallet - Allows for multiple retries because in some cases users have experienced an issue where the wallet rpc returns `no connection to daemon` even though the daemon is available. I'm not 100% sure why this happens but retrying often fixes the issue - Attempt to print the current sync height while the wallet is syncing. This only works to some degree because the `monero-wallet-rpc` stops responding (or takes a long time to respond) while it's refreshing - The `monero-wallet-rpc` is started with the `--no-initial-sync` flag which ensures that as soon as it's started, it's ready to respond to requests --------- Co-authored-by: Byron Hambly Co-authored-by: Byron Hambly --- CHANGELOG.md | 2 + swap/src/monero/wallet.rs | 69 +++++++++++++++++++++++++++++++++-- swap/src/monero/wallet_rpc.rs | 44 ++++++++++++++-------- swap/src/protocol/bob/swap.rs | 5 ++- swap/tests/harness/mod.rs | 2 +- 5 files changed, 102 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3ad105..594cae30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Minimum Supported Rust Version (MSRV) bumped to 1.70 +- Update monero-wallet-rpc version to v0.18.3.1 +- Add retry logic to monero-wallet-rpc wallet refresh ## [0.12.3] - 2023-09-20 diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 56fd8e60..61ed4ca4 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -45,6 +45,7 @@ impl Wallet { pub async fn connect(client: wallet::Client, name: String, env_config: Config) -> Result { let main_address = monero::Address::from_str(client.get_address(0).await?.address.as_str())?; + Ok(Self { inner: Mutex::new(client), network: env_config.monero_network, @@ -144,7 +145,7 @@ impl Wallet { .await?; // Try to send all the funds from the generated wallet to the default wallet - match wallet.refresh().await { + match self.refresh(3).await { Ok(_) => match wallet.sweep_all(self.main_address.to_string()).await { Ok(sweep_all) => { for tx in sweep_all.tx_hash_list { @@ -261,8 +262,70 @@ impl Wallet { self.main_address } - pub async fn refresh(&self) -> Result { - Ok(self.inner.lock().await.refresh().await?) + pub async fn refresh(&self, max_attempts: usize) -> Result { + const GET_HEIGHT_INTERVAL: Duration = Duration::from_secs(5); + const RETRY_INTERVAL: Duration = Duration::from_secs(2); + + let inner = self.inner.lock().await; + + // Cloning this is relatively cheap because reqwest::Client is a wrapper around an Arc + let inner_clone = inner.clone(); + let wallet_name_clone = self.name.clone(); + + let refresh_task = tokio::task::spawn(async move { + loop { + let height = inner_clone.get_height().await; + + match height { + Err(error) => { + tracing::debug!(name = %wallet_name_clone, %error, "Failed to get current Monero wallet sync height"); + } + Ok(height) => { + tracing::debug!(name = %wallet_name_clone, current_sync_height = height.height, "Syncing Monero wallet"); + } + } + + tokio::time::sleep(GET_HEIGHT_INTERVAL).await; + } + }); + + let refresh_result = tokio::select! { + biased; + _ = refresh_task => { + unreachable!("Current sync height refresh task should never finish") + } + refresh_result = async { + for i in 1..=max_attempts { + tracing::info!(name = %self.name, attempt=i, "Syncing Monero wallet"); + + let result = inner.refresh().await; + + match result { + Ok(refreshed) => { + tracing::info!(name = %self.name, "Monero wallet synced"); + return Ok(refreshed); + } + Err(error) => { + let attempts_left = max_attempts - i; + tracing::warn!(attempt=i, %attempts_left, name = %self.name, %error, "Failed to sync Monero wallet"); + + if attempts_left == 0 { + tracing::error!(name = %self.name, %error, "Failed to sync Monero wallet"); + return Err(error); + } + } + } + + tokio::time::sleep(RETRY_INTERVAL).await; + } + + unreachable!("Loop should always return before it breaks") + } => { + refresh_result + } + }; + + Ok(refresh_result?) } } diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index e227c3f3..bb696ba0 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -46,30 +46,30 @@ const MONERO_DAEMONS: [MoneroDaemon; 17] = [ compile_error!("unsupported operating system"); #[cfg(all(target_os = "macos", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.1.2.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "macos", target_arch = "x86_64"))] -const DOWNLOAD_HASH: &str = "ba1108c7a5e5efe15b6a628fb007c50f01c231f61137bba7427605286dbc6f01"; +const DOWNLOAD_HASH: &str = "7f8bd9364ef16482b418aa802a65be0e4cc660c794bb5d77b2d17bc84427883a"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.1.2.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_HASH: &str = "620b825c04f84845ed09de03b207a3230a34f74b30a8a07dde504a7d376ee4b9"; +const DOWNLOAD_HASH: &str = "915288b023cb5811e626e10052adc6ac5323dd283c5a25b91059b0fb86a21fb6"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.1.2.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -const DOWNLOAD_HASH: &str = "7d51e7072351f65d0c7909e745827cfd3b00abe5e7c4cc4c104a3c9b526da07e"; +const DOWNLOAD_HASH: &str = "23af572fdfe3459b9ab97e2e9aa7e3c11021c955d6064b801a27d7e8c21ae09d"; #[cfg(all(target_os = "linux", target_arch = "arm"))] const DOWNLOAD_URL: &str = - "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.1.2.tar.bz2"; + "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "arm"))] -const DOWNLOAD_HASH: &str = "94ece435ed60f85904114643482c2b6716f74bf97040a7af237450574a9cf06d"; +const DOWNLOAD_HASH: &str = "2ea2c8898cbab88f49423f4f6c15f2a94046cb4bbe827493dd061edc0fd5f1ca"; #[cfg(target_os = "windows")] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.1.2.zip"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.3.1.zip"; #[cfg(target_os = "windows")] -const DOWNLOAD_HASH: &str = "0a3d4d1af7e094c05352c31b2dafcc6ccbc80edc195ca9eaedc919c36accd05a"; +const DOWNLOAD_HASH: &str = "35dcc4bee4caad3442659d37837e0119e4649a77f2e3b5e80dd6d9b8fc4fb6ad"; #[cfg(any(target_os = "macos", target_os = "linux"))] const PACKED_FILE: &str = "monero-wallet-rpc"; @@ -77,7 +77,7 @@ const PACKED_FILE: &str = "monero-wallet-rpc"; #[cfg(target_os = "windows")] const PACKED_FILE: &str = "monero-wallet-rpc.exe"; -const WALLET_RPC_VERSION: &str = "v0.18.1.2"; +const WALLET_RPC_VERSION: &str = "v0.18.3.1"; #[derive(Debug, Clone, Copy, thiserror::Error)] #[error("monero wallet rpc executable not found in downloaded archive")] @@ -233,9 +233,10 @@ impl WalletRpc { .parse::()?; tracing::info!( - "Downloading monero-wallet-rpc ({}) from {}", - content_length.big_byte(2), - DOWNLOAD_URL + progress="0%", + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", ); let mut hasher = Sha256::new(); @@ -268,12 +269,24 @@ impl WalletRpc { let total = 3 * content_length; let percent = 100 * received as u64 / total; if percent != notified && percent % 10 == 0 { - tracing::debug!("{}%", percent); + tracing::info!( + progress=format!("{}%", percent), + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", + ); notified = percent; } file.write_all(&bytes).await?; } + tracing::info!( + progress="100%", + size=%content_length.big_byte(2), + download_url=DOWNLOAD_URL, + "Downloading monero-wallet-rpc", + ); + let result = hasher.finalize(); let result_hash = HEXLOWER.encode(result.as_ref()); if result_hash != DOWNLOAD_HASH { @@ -339,6 +352,7 @@ impl WalletRpc { .arg("--disable-rpc-login") .arg("--wallet-dir") .arg(self.working_dir.join("monero-data")) + .arg("--no-initial-sync") .spawn()?; let stdout = child diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 66933a87..92d4f628 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -227,6 +227,9 @@ async fn next_state( let (spend_key, view_key) = state.xmr_keys(); let wallet_file_name = swap_id.to_string(); + + tracing::info!(%wallet_file_name, "Generating and opening Monero wallet from the extracted keys to redeem the Monero"); + if let Err(e) = monero_wallet .create_from_and_load( wallet_file_name.clone(), @@ -247,7 +250,7 @@ async fn next_state( } // Ensure that the generated wallet is synced so we have a proper balance - monero_wallet.refresh().await?; + monero_wallet.refresh(3).await?; // Sweep (transfer all funds) to the given address let tx_hashes = monero_wallet.sweep_all(monero_receive_address).await?; diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index bd039477..7331f96f 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -863,7 +863,7 @@ impl Wallet for monero::Wallet { type Amount = monero::Amount; async fn refresh(&self) -> Result<()> { - self.refresh().await?; + self.refresh(1).await?; Ok(()) } From a19501a0023fa8bbd7342cac736cd079a58361ce Mon Sep 17 00:00:00 2001 From: Byron Hambly Date: Tue, 26 Mar 2024 09:06:31 +0200 Subject: [PATCH 10/11] Revert "Monero wallet refresh fix (#1487)" This reverts commit d8dacbdee99a8d951977e62db1c888cc9ed56ea8. --- CHANGELOG.md | 2 - swap/src/monero/wallet.rs | 69 ++--------------------------------- swap/src/monero/wallet_rpc.rs | 44 ++++++++-------------- swap/src/protocol/bob/swap.rs | 5 +-- swap/tests/harness/mod.rs | 2 +- 5 files changed, 20 insertions(+), 102 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 594cae30..4d3ad105 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Minimum Supported Rust Version (MSRV) bumped to 1.70 -- Update monero-wallet-rpc version to v0.18.3.1 -- Add retry logic to monero-wallet-rpc wallet refresh ## [0.12.3] - 2023-09-20 diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 61ed4ca4..56fd8e60 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -45,7 +45,6 @@ impl Wallet { pub async fn connect(client: wallet::Client, name: String, env_config: Config) -> Result { let main_address = monero::Address::from_str(client.get_address(0).await?.address.as_str())?; - Ok(Self { inner: Mutex::new(client), network: env_config.monero_network, @@ -145,7 +144,7 @@ impl Wallet { .await?; // Try to send all the funds from the generated wallet to the default wallet - match self.refresh(3).await { + match wallet.refresh().await { Ok(_) => match wallet.sweep_all(self.main_address.to_string()).await { Ok(sweep_all) => { for tx in sweep_all.tx_hash_list { @@ -262,70 +261,8 @@ impl Wallet { self.main_address } - pub async fn refresh(&self, max_attempts: usize) -> Result { - const GET_HEIGHT_INTERVAL: Duration = Duration::from_secs(5); - const RETRY_INTERVAL: Duration = Duration::from_secs(2); - - let inner = self.inner.lock().await; - - // Cloning this is relatively cheap because reqwest::Client is a wrapper around an Arc - let inner_clone = inner.clone(); - let wallet_name_clone = self.name.clone(); - - let refresh_task = tokio::task::spawn(async move { - loop { - let height = inner_clone.get_height().await; - - match height { - Err(error) => { - tracing::debug!(name = %wallet_name_clone, %error, "Failed to get current Monero wallet sync height"); - } - Ok(height) => { - tracing::debug!(name = %wallet_name_clone, current_sync_height = height.height, "Syncing Monero wallet"); - } - } - - tokio::time::sleep(GET_HEIGHT_INTERVAL).await; - } - }); - - let refresh_result = tokio::select! { - biased; - _ = refresh_task => { - unreachable!("Current sync height refresh task should never finish") - } - refresh_result = async { - for i in 1..=max_attempts { - tracing::info!(name = %self.name, attempt=i, "Syncing Monero wallet"); - - let result = inner.refresh().await; - - match result { - Ok(refreshed) => { - tracing::info!(name = %self.name, "Monero wallet synced"); - return Ok(refreshed); - } - Err(error) => { - let attempts_left = max_attempts - i; - tracing::warn!(attempt=i, %attempts_left, name = %self.name, %error, "Failed to sync Monero wallet"); - - if attempts_left == 0 { - tracing::error!(name = %self.name, %error, "Failed to sync Monero wallet"); - return Err(error); - } - } - } - - tokio::time::sleep(RETRY_INTERVAL).await; - } - - unreachable!("Loop should always return before it breaks") - } => { - refresh_result - } - }; - - Ok(refresh_result?) + pub async fn refresh(&self) -> Result { + Ok(self.inner.lock().await.refresh().await?) } } diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index bb696ba0..e227c3f3 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -46,30 +46,30 @@ const MONERO_DAEMONS: [MoneroDaemon; 17] = [ compile_error!("unsupported operating system"); #[cfg(all(target_os = "macos", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.3.1.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-x64-v0.18.1.2.tar.bz2"; #[cfg(all(target_os = "macos", target_arch = "x86_64"))] -const DOWNLOAD_HASH: &str = "7f8bd9364ef16482b418aa802a65be0e4cc660c794bb5d77b2d17bc84427883a"; +const DOWNLOAD_HASH: &str = "ba1108c7a5e5efe15b6a628fb007c50f01c231f61137bba7427605286dbc6f01"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.3.1.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-mac-armv8-v0.18.1.2.tar.bz2"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] -const DOWNLOAD_HASH: &str = "915288b023cb5811e626e10052adc6ac5323dd283c5a25b91059b0fb86a21fb6"; +const DOWNLOAD_HASH: &str = "620b825c04f84845ed09de03b207a3230a34f74b30a8a07dde504a7d376ee4b9"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.3.1.tar.bz2"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-linux-x64-v0.18.1.2.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -const DOWNLOAD_HASH: &str = "23af572fdfe3459b9ab97e2e9aa7e3c11021c955d6064b801a27d7e8c21ae09d"; +const DOWNLOAD_HASH: &str = "7d51e7072351f65d0c7909e745827cfd3b00abe5e7c4cc4c104a3c9b526da07e"; #[cfg(all(target_os = "linux", target_arch = "arm"))] const DOWNLOAD_URL: &str = - "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.3.1.tar.bz2"; + "https://downloads.getmonero.org/cli/monero-linux-armv7-v0.18.1.2.tar.bz2"; #[cfg(all(target_os = "linux", target_arch = "arm"))] -const DOWNLOAD_HASH: &str = "2ea2c8898cbab88f49423f4f6c15f2a94046cb4bbe827493dd061edc0fd5f1ca"; +const DOWNLOAD_HASH: &str = "94ece435ed60f85904114643482c2b6716f74bf97040a7af237450574a9cf06d"; #[cfg(target_os = "windows")] -const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.3.1.zip"; +const DOWNLOAD_URL: &str = "https://downloads.getmonero.org/cli/monero-win-x64-v0.18.1.2.zip"; #[cfg(target_os = "windows")] -const DOWNLOAD_HASH: &str = "35dcc4bee4caad3442659d37837e0119e4649a77f2e3b5e80dd6d9b8fc4fb6ad"; +const DOWNLOAD_HASH: &str = "0a3d4d1af7e094c05352c31b2dafcc6ccbc80edc195ca9eaedc919c36accd05a"; #[cfg(any(target_os = "macos", target_os = "linux"))] const PACKED_FILE: &str = "monero-wallet-rpc"; @@ -77,7 +77,7 @@ const PACKED_FILE: &str = "monero-wallet-rpc"; #[cfg(target_os = "windows")] const PACKED_FILE: &str = "monero-wallet-rpc.exe"; -const WALLET_RPC_VERSION: &str = "v0.18.3.1"; +const WALLET_RPC_VERSION: &str = "v0.18.1.2"; #[derive(Debug, Clone, Copy, thiserror::Error)] #[error("monero wallet rpc executable not found in downloaded archive")] @@ -233,10 +233,9 @@ impl WalletRpc { .parse::()?; tracing::info!( - progress="0%", - size=%content_length.big_byte(2), - download_url=DOWNLOAD_URL, - "Downloading monero-wallet-rpc", + "Downloading monero-wallet-rpc ({}) from {}", + content_length.big_byte(2), + DOWNLOAD_URL ); let mut hasher = Sha256::new(); @@ -269,24 +268,12 @@ impl WalletRpc { let total = 3 * content_length; let percent = 100 * received as u64 / total; if percent != notified && percent % 10 == 0 { - tracing::info!( - progress=format!("{}%", percent), - size=%content_length.big_byte(2), - download_url=DOWNLOAD_URL, - "Downloading monero-wallet-rpc", - ); + tracing::debug!("{}%", percent); notified = percent; } file.write_all(&bytes).await?; } - tracing::info!( - progress="100%", - size=%content_length.big_byte(2), - download_url=DOWNLOAD_URL, - "Downloading monero-wallet-rpc", - ); - let result = hasher.finalize(); let result_hash = HEXLOWER.encode(result.as_ref()); if result_hash != DOWNLOAD_HASH { @@ -352,7 +339,6 @@ impl WalletRpc { .arg("--disable-rpc-login") .arg("--wallet-dir") .arg(self.working_dir.join("monero-data")) - .arg("--no-initial-sync") .spawn()?; let stdout = child diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 92d4f628..66933a87 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -227,9 +227,6 @@ async fn next_state( let (spend_key, view_key) = state.xmr_keys(); let wallet_file_name = swap_id.to_string(); - - tracing::info!(%wallet_file_name, "Generating and opening Monero wallet from the extracted keys to redeem the Monero"); - if let Err(e) = monero_wallet .create_from_and_load( wallet_file_name.clone(), @@ -250,7 +247,7 @@ async fn next_state( } // Ensure that the generated wallet is synced so we have a proper balance - monero_wallet.refresh(3).await?; + monero_wallet.refresh().await?; // Sweep (transfer all funds) to the given address let tx_hashes = monero_wallet.sweep_all(monero_receive_address).await?; diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 7331f96f..bd039477 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -863,7 +863,7 @@ impl Wallet for monero::Wallet { type Amount = monero::Amount; async fn refresh(&self) -> Result<()> { - self.refresh(1).await?; + self.refresh().await?; Ok(()) } From 9d426066a995272d4dfd36fbdb841b17f6206963 Mon Sep 17 00:00:00 2001 From: Byron Hambly Date: Tue, 26 Mar 2024 09:08:38 +0200 Subject: [PATCH 11/11] ci: lock install version for sqlx-cli --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69c11c72..14c5a835 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,7 +54,7 @@ jobs: - uses: Swatinem/rust-cache@v2.7.3 - name: Install sqlx-cli - run: cargo install --version 0.6.3 sqlx-cli + run: cargo install --locked --version 0.6.3 sqlx-cli - name: Run sqlite_dev_setup.sh script run: |