diff --git a/cli/Cargo.lock b/cli/Cargo.lock index aa3018e..e120516 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -3046,7 +3046,7 @@ dependencies = [ [[package]] name = "sdk-common" version = "0.6.2" -source = "git+https://github.com/breez/breez-sdk?rev=238245bd34be15727493d7d0c625c6ae55f2a845#238245bd34be15727493d7d0c625c6ae55f2a845" +source = "git+https://github.com/breez/breez-sdk?rev=e537feb8ed134bc3c7af5196e10a0a189dd36ac7#e537feb8ed134bc3c7af5196e10a0a189dd36ac7" dependencies = [ "aes 0.8.4", "anyhow", diff --git a/lib/core/src/chain/bitcoin.rs b/lib/core/src/chain/bitcoin.rs index cf08420..91452b0 100644 --- a/lib/core/src/chain/bitcoin.rs +++ b/lib/core/src/chain/bitcoin.rs @@ -1,4 +1,3 @@ -use std::thread; use std::time::Duration; use anyhow::{anyhow, Result}; @@ -56,6 +55,13 @@ pub trait BitcoinChainService: Send + Sync { /// Return the confirmed and unconfirmed balances of a list of script hashes fn scripts_get_balance(&self, scripts: &[&Script]) -> Result>; + /// Return the confirmed and unconfirmed balances of a script hash + async fn script_get_balance_with_retry( + &self, + script: &Script, + retries: u64, + ) -> Result; + /// Verify that a transaction appears in the address script history async fn verify_tx( &self, @@ -173,7 +179,7 @@ impl BitcoinChainService for HybridBitcoinChainService { "Script history for {} got zero transactions, retrying in {} seconds...", script_hash, retry ); - thread::sleep(Duration::from_secs(retry)); + tokio::time::sleep(Duration::from_secs(retry)).await; } false => break, } @@ -214,6 +220,39 @@ impl BitcoinChainService for HybridBitcoinChainService { Ok(self.client.batch_script_get_balance(scripts)?) } + async fn script_get_balance_with_retry( + &self, + script: &Script, + retries: u64, + ) -> Result { + let script_hash = sha256::Hash::hash(script.as_bytes()).to_hex(); + info!("Fetching script balance for {}", script_hash); + let mut script_balance = GetBalanceRes { + confirmed: 0, + unconfirmed: 0, + }; + + let mut retry = 0; + while retry <= retries { + script_balance = self.script_get_balance(script)?; + match script_balance { + GetBalanceRes { + confirmed: 0, + unconfirmed: 0, + } => { + retry += 1; + info!( + "Got zero balance for script {}, retrying in {} seconds...", + script_hash, retry + ); + tokio::time::sleep(Duration::from_secs(retry)).await; + } + _ => break, + } + } + Ok(script_balance) + } + async fn verify_tx( &self, address: &Address, diff --git a/lib/core/src/chain/liquid.rs b/lib/core/src/chain/liquid.rs index 42dbb21..03e1f61 100644 --- a/lib/core/src/chain/liquid.rs +++ b/lib/core/src/chain/liquid.rs @@ -1,4 +1,4 @@ -use std::{str::FromStr, thread, time::Duration}; +use std::{str::FromStr, time::Duration}; use anyhow::{anyhow, Result}; use async_trait::async_trait; @@ -192,7 +192,7 @@ impl LiquidChainService for HybridLiquidChainService { retry += 1; info!("Script history for {script_hash} is empty, retrying in 1 second... ({retry} of {retries})"); // Waiting 1s between retries, so we detect the new tx as soon as possible - thread::sleep(Duration::from_secs(1)); + tokio::time::sleep(Duration::from_secs(1)).await; } false => break, } @@ -290,7 +290,7 @@ async fn get_with_retry(url: &str, retries: usize) -> Result { } let secs = 1 << attempt; - thread::sleep(Duration::from_secs(secs)); + tokio::time::sleep(Duration::from_secs(secs)).await; } } } diff --git a/lib/core/src/chain_swap.rs b/lib/core/src/chain_swap.rs index 0ca54b7..f144d43 100644 --- a/lib/core/src/chain_swap.rs +++ b/lib/core/src/chain_swap.rs @@ -513,7 +513,8 @@ impl ChainSwapHandler { .bitcoin_chain_service .lock() .await - .script_get_balance(script_pubkey.as_script())?; + .script_get_balance_with_retry(script_pubkey.as_script(), 10) + .await?; debug!("Found lockup balance {script_balance:?}"); let user_lockup_amount_sat = match script_balance.confirmed > 0 { true => script_balance.confirmed, diff --git a/lib/core/src/test_utils/chain.rs b/lib/core/src/test_utils/chain.rs index 275710a..47b9275 100644 --- a/lib/core/src/test_utils/chain.rs +++ b/lib/core/src/test_utils/chain.rs @@ -193,6 +193,14 @@ impl BitcoinChainService for MockBitcoinChainService { unimplemented!() } + async fn script_get_balance_with_retry( + &self, + _script: &boltz_client::bitcoin::Script, + _retries: u64, + ) -> Result { + unimplemented!() + } + async fn verify_tx( &self, _address: &boltz_client::Address,