mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-01-16 12:34:22 +01:00
remove lock for liquid chain servivce
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
@@ -17,7 +18,7 @@ use crate::{model::Config, utils};
|
||||
#[async_trait]
|
||||
pub trait LiquidChainService: Send + Sync {
|
||||
/// Get the blockchain latest block
|
||||
async fn tip(&mut self) -> Result<u32>;
|
||||
async fn tip(&self) -> Result<u32>;
|
||||
|
||||
/// Broadcast a transaction
|
||||
async fn broadcast(&self, tx: &Transaction) -> Result<Txid>;
|
||||
@@ -58,20 +59,25 @@ pub trait LiquidChainService: Send + Sync {
|
||||
|
||||
pub(crate) struct HybridLiquidChainService {
|
||||
electrum_client: ElectrumClient,
|
||||
tip_client: Mutex<ElectrumClient>,
|
||||
}
|
||||
|
||||
impl HybridLiquidChainService {
|
||||
pub(crate) fn new(config: Config) -> Result<Self> {
|
||||
let electrum_url = ElectrumUrl::new(&config.liquid_electrum_url, true, true)?;
|
||||
let electrum_client = ElectrumClient::new(&electrum_url)?;
|
||||
Ok(Self { electrum_client })
|
||||
let tip_client = ElectrumClient::new(&electrum_url)?;
|
||||
Ok(Self {
|
||||
electrum_client,
|
||||
tip_client: Mutex::new(tip_client),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LiquidChainService for HybridLiquidChainService {
|
||||
async fn tip(&mut self) -> Result<u32> {
|
||||
Ok(self.electrum_client.tip()?.height)
|
||||
async fn tip(&self) -> Result<u32> {
|
||||
Ok(self.tip_client.lock().unwrap().tip()?.height)
|
||||
}
|
||||
|
||||
async fn broadcast(&self, tx: &Transaction) -> Result<Txid> {
|
||||
|
||||
@@ -41,7 +41,7 @@ pub(crate) struct ChainSwapHandler {
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
bitcoin_chain_service: Arc<Mutex<dyn BitcoinChainService>>,
|
||||
subscription_notifier: broadcast::Sender<String>,
|
||||
}
|
||||
@@ -70,7 +70,7 @@ impl ChainSwapHandler {
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
bitcoin_chain_service: Arc<Mutex<dyn BitcoinChainService>>,
|
||||
) -> Result<Self> {
|
||||
let (subscription_notifier, _) = broadcast::channel::<String>(30);
|
||||
@@ -759,9 +759,7 @@ impl ChainSwapHandler {
|
||||
.await?;
|
||||
|
||||
let lockup_tx_id = self
|
||||
.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.liquid_chain_service
|
||||
.broadcast(&lockup_tx)
|
||||
.await?
|
||||
.to_string();
|
||||
@@ -843,9 +841,8 @@ impl ChainSwapHandler {
|
||||
Ok(_) => {
|
||||
let broadcast_res = match claim_tx {
|
||||
// We attempt broadcasting via chain service, then fallback to Boltz
|
||||
SdkTransaction::Liquid(tx) => {
|
||||
let liquid_chain_service = self.liquid_chain_service.lock().await;
|
||||
liquid_chain_service
|
||||
SdkTransaction::Liquid(tx) => {
|
||||
self.liquid_chain_service
|
||||
.broadcast(&tx)
|
||||
.await
|
||||
.map(|tx_id| tx_id.to_hex())
|
||||
@@ -1043,13 +1040,13 @@ impl ChainSwapHandler {
|
||||
});
|
||||
};
|
||||
|
||||
let liquid_chain_service = self.liquid_chain_service.lock().await;
|
||||
|
||||
let script_pk = swap_script
|
||||
.to_address(self.config.network.into())
|
||||
.map_err(|e| anyhow!("Could not retrieve address from swap script: {e:?}"))?
|
||||
.to_unconfidential()
|
||||
.script_pubkey();
|
||||
let utxos = liquid_chain_service.get_script_utxos(&script_pk).await?;
|
||||
let utxos = self.liquid_chain_service.get_script_utxos(&script_pk).await?;
|
||||
|
||||
let refund_address = self.onchain_wallet.next_unused_address().await?.to_string();
|
||||
let SdkTransaction::Liquid(refund_tx) = self.swapper.create_refund_tx(
|
||||
@@ -1067,7 +1064,7 @@ impl ChainSwapHandler {
|
||||
),
|
||||
});
|
||||
};
|
||||
let refund_tx_id = liquid_chain_service
|
||||
let refund_tx_id = self.liquid_chain_service
|
||||
.broadcast(&refund_tx)
|
||||
.await?
|
||||
.to_string();
|
||||
@@ -1264,9 +1261,7 @@ impl ChainSwapHandler {
|
||||
.to_address(self.config.network.into())
|
||||
.map_err(|e| anyhow!("Failed to get swap script address {e:?}"))?;
|
||||
let tx = self
|
||||
.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.liquid_chain_service
|
||||
.verify_tx(
|
||||
&address,
|
||||
&swap_update_tx.id,
|
||||
@@ -1447,9 +1442,7 @@ impl ChainSwapHandler {
|
||||
.to_unconfidential();
|
||||
let script = Script::from_hex(hex::encode(address.script_pubkey().as_bytes()).as_str())
|
||||
.map_err(|e| anyhow!("Failed to get script from address {e:?}"))?;
|
||||
self.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
self.liquid_chain_service
|
||||
.get_script_history_with_retry(&script, 5)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use lwk_wollet::elements::secp256k1_zkp::Secp256k1;
|
||||
use lwk_wollet::elements::{Transaction, Txid};
|
||||
use lwk_wollet::hashes::hex::DisplayHex;
|
||||
use lwk_wollet::secp256k1::SecretKey;
|
||||
use tokio::sync::{broadcast, Mutex};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
use crate::chain::liquid::LiquidChainService;
|
||||
use crate::model::{BlockListener, PaymentState::*};
|
||||
@@ -32,7 +32,7 @@ pub(crate) struct ReceiveSwapHandler {
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
subscription_notifier: broadcast::Sender<String>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -52,7 +52,7 @@ impl ReceiveSwapHandler {
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
) -> Self {
|
||||
let (subscription_notifier, _) = broadcast::channel::<String>(30);
|
||||
Self {
|
||||
@@ -354,8 +354,7 @@ impl ReceiveSwapHandler {
|
||||
match self.persister.set_receive_swap_claim_tx_id(swap_id, &tx_id) {
|
||||
Ok(_) => {
|
||||
// We attempt broadcasting via chain service, then fallback to Boltz
|
||||
let liquid_chain_service = self.liquid_chain_service.lock().await;
|
||||
let broadcast_res = liquid_chain_service
|
||||
let broadcast_res = self.liquid_chain_service
|
||||
.broadcast(&claim_tx)
|
||||
.await
|
||||
.map(|tx_id| tx_id.to_hex())
|
||||
@@ -440,8 +439,6 @@ impl ReceiveSwapHandler {
|
||||
let swap_id = &receive_swap.id;
|
||||
let tx_hex = self
|
||||
.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.get_transaction_hex(&Txid::from_str(&tx_id)?)
|
||||
.await?
|
||||
.ok_or(anyhow!("Lockup tx not found for Receive swap {swap_id}"))?
|
||||
@@ -516,8 +513,6 @@ impl ReceiveSwapHandler {
|
||||
.to_address(self.config.network.into())
|
||||
.map_err(|e| anyhow!("Failed to get swap script address {e:?}"))?;
|
||||
self.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.verify_tx(&address, tx_id, tx_hex, verify_confirmation)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ pub(crate) struct Recoverer {
|
||||
master_blinding_key: MasterBlindingKey,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
bitcoin_chain_service: Arc<Mutex<dyn BitcoinChainService>>,
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ impl Recoverer {
|
||||
master_blinding_key: Vec<u8>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
bitcoin_chain_service: Arc<Mutex<dyn BitcoinChainService>>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
@@ -79,8 +79,6 @@ impl Recoverer {
|
||||
let claim_tx_ids: Vec<Txid> = failed_cooperative.values().cloned().collect();
|
||||
let claim_txs = self
|
||||
.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.get_transactions(claim_tx_ids.as_slice())
|
||||
.await
|
||||
.map_err(|e| anyhow!("Failed to fetch claim txs from recovery: {e}"))?;
|
||||
@@ -166,6 +164,20 @@ impl Recoverer {
|
||||
/// - `tx_map`: all known onchain txs of this wallet at this time, essentially our own LWK cache.
|
||||
/// - `swaps`: immutable data of the swaps for which we want to recover onchain data.
|
||||
pub(crate) async fn recover_from_onchain(&self, swaps: &mut [Swap]) -> Result<()> {
|
||||
//println!("swaps: {:?}", swaps.iter().map(|s|s.).collect::<Vec<_>>());
|
||||
for swap in swaps.iter() {
|
||||
match swap {
|
||||
Swap::Send(send_swap) => {
|
||||
println!("send_swap: {:?}", send_swap.id);
|
||||
}
|
||||
Swap::Receive(receive_swap) => {
|
||||
println!("receive_swap: {:?}", receive_swap.id);
|
||||
}
|
||||
Swap::Chain(chain_swap) => {
|
||||
println!("chain_swap: {:?}", chain_swap.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let tx_map = TxMap::from_raw_tx_map(self.onchain_wallet.transactions_by_tx_id().await?);
|
||||
|
||||
let swaps_list = swaps.to_vec().try_into()?;
|
||||
@@ -203,7 +215,7 @@ impl Recoverer {
|
||||
)?;
|
||||
|
||||
let bitcoin_tip = self.bitcoin_chain_service.lock().await.tip()?;
|
||||
let liquid_tip = self.liquid_chain_service.lock().await.tip().await?;
|
||||
let liquid_tip = self.liquid_chain_service.tip().await?;
|
||||
|
||||
for swap in swaps.iter_mut() {
|
||||
let swap_id = &swap.id();
|
||||
@@ -355,8 +367,6 @@ impl Recoverer {
|
||||
let swap_lbtc_scripts = swaps_list.get_swap_lbtc_scripts();
|
||||
let lbtc_script_histories = self
|
||||
.liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.get_scripts_history(&swap_lbtc_scripts.iter().collect::<Vec<&LBtcScript>>())
|
||||
.await?;
|
||||
let lbtc_swap_scripts_len = swap_lbtc_scripts.len();
|
||||
|
||||
@@ -75,7 +75,7 @@ pub struct LiquidSdk {
|
||||
pub(crate) status_stream: Arc<dyn SwapperStatusStream>,
|
||||
pub(crate) swapper: Arc<dyn Swapper>,
|
||||
pub(crate) recoverer: Arc<Recoverer>,
|
||||
pub(crate) liquid_chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
pub(crate) liquid_chain_service: Arc<dyn LiquidChainService>,
|
||||
pub(crate) bitcoin_chain_service: Arc<Mutex<dyn BitcoinChainService>>,
|
||||
pub(crate) fiat_api: Arc<dyn FiatAPI>,
|
||||
pub(crate) is_started: RwLock<bool>,
|
||||
@@ -176,8 +176,7 @@ impl LiquidSdk {
|
||||
persister.init()?;
|
||||
persister.replace_asset_metadata(config.asset_metadata.clone())?;
|
||||
|
||||
let liquid_chain_service =
|
||||
Arc::new(Mutex::new(HybridLiquidChainService::new(config.clone())?));
|
||||
let liquid_chain_service = Arc::new(HybridLiquidChainService::new(config.clone())?);
|
||||
let bitcoin_chain_service =
|
||||
Arc::new(Mutex::new(HybridBitcoinChainService::new(config.clone())?));
|
||||
|
||||
@@ -354,7 +353,7 @@ impl LiquidSdk {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
// Get the Liquid tip and process a new block
|
||||
let liquid_tip_res = cloned.liquid_chain_service.lock().await.tip().await;
|
||||
let liquid_tip_res = cloned.liquid_chain_service.tip().await;
|
||||
let is_new_liquid_block = match &liquid_tip_res {
|
||||
Ok(height) => {
|
||||
debug!("Got Liquid tip: {height}");
|
||||
@@ -1348,8 +1347,7 @@ impl LiquidSdk {
|
||||
"Built onchain L-BTC tx with receiver_amount_sat = {receiver_amount_sat}, fees_sat = {fees_sat} and txid = {tx_id}"
|
||||
);
|
||||
|
||||
let liquid_chain_service = self.liquid_chain_service.lock().await;
|
||||
let tx_id = liquid_chain_service.broadcast(&tx).await?.to_string();
|
||||
let tx_id = self.liquid_chain_service.broadcast(&tx).await?.to_string();
|
||||
|
||||
// We insert a pseudo-tx in case LWK fails to pick up the new mempool tx for a while
|
||||
// This makes the tx known to the SDK (get_info, list_payments) instantly
|
||||
@@ -2571,7 +2569,7 @@ impl LiquidSdk {
|
||||
match partial_sync {
|
||||
false => {
|
||||
let bitcoin_height = self.bitcoin_chain_service.lock().await.tip()?.height as u32;
|
||||
let liquid_height = self.liquid_chain_service.lock().await.tip().await?;
|
||||
let liquid_height = self.liquid_chain_service.tip().await?;
|
||||
let final_swap_states = [PaymentState::Complete, PaymentState::Failed];
|
||||
|
||||
let send_swaps = self
|
||||
@@ -3521,7 +3519,7 @@ mod tests {
|
||||
create_persister!(persister);
|
||||
let swapper = Arc::new(MockSwapper::default());
|
||||
let status_stream = Arc::new(MockStatusStream::new());
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
let sdk = Arc::new(new_liquid_sdk_with_chain_services(
|
||||
@@ -3569,15 +3567,12 @@ mod tests {
|
||||
let height = (serde_json::to_string(&status).unwrap()
|
||||
== serde_json::to_string(&RevSwapStates::TransactionConfirmed).unwrap())
|
||||
as i32;
|
||||
liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.set_history(vec![MockHistory {
|
||||
txid: mock_tx_id,
|
||||
height,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
liquid_chain_service.set_history(vec![MockHistory {
|
||||
txid: mock_tx_id,
|
||||
height,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
|
||||
let persisted_swap = trigger_swap_update!(
|
||||
"receive",
|
||||
@@ -3606,15 +3601,12 @@ mod tests {
|
||||
let height = (serde_json::to_string(&status).unwrap()
|
||||
== serde_json::to_string(&RevSwapStates::TransactionConfirmed).unwrap())
|
||||
as i32;
|
||||
liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.set_history(vec![MockHistory {
|
||||
txid: mock_tx_id,
|
||||
height,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
liquid_chain_service.set_history(vec![MockHistory {
|
||||
txid: mock_tx_id,
|
||||
height,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
|
||||
let persisted_swap = trigger_swap_update!(
|
||||
"receive",
|
||||
@@ -3699,7 +3691,7 @@ mod tests {
|
||||
create_persister!(persister);
|
||||
let swapper = Arc::new(MockSwapper::default());
|
||||
let status_stream = Arc::new(MockStatusStream::new());
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
let sdk = Arc::new(new_liquid_sdk_with_chain_services(
|
||||
@@ -3789,15 +3781,12 @@ mod tests {
|
||||
}]);
|
||||
}
|
||||
Direction::Outgoing => {
|
||||
liquid_chain_service
|
||||
.lock()
|
||||
.await
|
||||
.set_history(vec![MockHistory {
|
||||
txid: Txid::from_str(user_lockup_tx_id).unwrap(),
|
||||
height: 0,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
liquid_chain_service.set_history(vec![MockHistory {
|
||||
txid: Txid::from_str(user_lockup_tx_id).unwrap(),
|
||||
height: 0,
|
||||
block_hash: None,
|
||||
block_timestamp: None,
|
||||
}]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3944,7 +3933,7 @@ mod tests {
|
||||
create_persister!(persister);
|
||||
let swapper = Arc::new(MockSwapper::new());
|
||||
let status_stream = Arc::new(MockStatusStream::new());
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
let sdk = Arc::new(new_liquid_sdk_with_chain_services(
|
||||
@@ -4008,7 +3997,7 @@ mod tests {
|
||||
create_persister!(persister);
|
||||
let swapper = Arc::new(MockSwapper::new());
|
||||
let status_stream = Arc::new(MockStatusStream::new());
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
let sdk = Arc::new(new_liquid_sdk_with_chain_services(
|
||||
|
||||
@@ -10,7 +10,7 @@ use log::{debug, error, info, warn};
|
||||
use lwk_wollet::elements::{LockTime, Transaction};
|
||||
use lwk_wollet::hashes::{sha256, Hash};
|
||||
use sdk_common::prelude::{AesSuccessActionDataResult, SuccessAction, SuccessActionProcessed};
|
||||
use tokio::sync::{broadcast, Mutex};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
use crate::chain::liquid::LiquidChainService;
|
||||
use crate::model::{
|
||||
@@ -34,7 +34,7 @@ pub(crate) struct SendSwapHandler {
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
chain_service: Arc<dyn LiquidChainService>,
|
||||
subscription_notifier: broadcast::Sender<String>,
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ impl SendSwapHandler {
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<dyn Swapper>,
|
||||
chain_service: Arc<Mutex<dyn LiquidChainService>>,
|
||||
chain_service: Arc<dyn LiquidChainService>,
|
||||
) -> Self {
|
||||
let (subscription_notifier, _) = broadcast::channel::<String>(30);
|
||||
Self {
|
||||
@@ -208,7 +208,7 @@ impl SendSwapHandler {
|
||||
|
||||
info!("Broadcasting lockup tx {lockup_tx_id} for Send swap {swap_id}",);
|
||||
|
||||
let broadcast_result = self.chain_service.lock().await.broadcast(&lockup_tx).await;
|
||||
let broadcast_result = self.chain_service.broadcast(&lockup_tx).await;
|
||||
|
||||
if let Err(err) = broadcast_result {
|
||||
debug!("Could not broadcast lockup tx for Send Swap {swap_id}: {err:?}");
|
||||
@@ -384,8 +384,6 @@ impl SendSwapHandler {
|
||||
// Get tx history of the swap script (lockup address)
|
||||
let history: Vec<_> = self
|
||||
.chain_service
|
||||
.lock()
|
||||
.await
|
||||
.get_script_history(&swap_script_pk)
|
||||
.await?;
|
||||
|
||||
@@ -405,8 +403,6 @@ impl SendSwapHandler {
|
||||
let claim_tx_id = claim_tx_entry.txid;
|
||||
let claim_tx = self
|
||||
.chain_service
|
||||
.lock()
|
||||
.await
|
||||
.get_transactions(&[claim_tx_id])
|
||||
.await
|
||||
.map_err(|e| anyhow!("Failed to fetch claim txs {claim_tx_id:?}: {e}"))?
|
||||
@@ -445,13 +441,12 @@ impl SendSwapHandler {
|
||||
let swap_script = swap.get_swap_script()?;
|
||||
let refund_address = self.onchain_wallet.next_unused_address().await?.to_string();
|
||||
|
||||
let liquid_chain_service = self.chain_service.lock().await;
|
||||
let script_pk = swap_script
|
||||
.to_address(self.config.network.into())
|
||||
.map_err(|e| anyhow!("Could not retrieve address from swap script: {e:?}"))?
|
||||
.to_unconfidential()
|
||||
.script_pubkey();
|
||||
let utxos = liquid_chain_service.get_script_utxos(&script_pk).await?;
|
||||
let utxos = self.chain_service.get_script_utxos(&script_pk).await?;
|
||||
let SdkTransaction::Liquid(refund_tx) = self.swapper.create_refund_tx(
|
||||
Swap::Send(swap.clone()),
|
||||
&refund_address,
|
||||
@@ -467,10 +462,7 @@ impl SendSwapHandler {
|
||||
),
|
||||
});
|
||||
};
|
||||
let refund_tx_id = liquid_chain_service
|
||||
.broadcast(&refund_tx)
|
||||
.await?
|
||||
.to_string();
|
||||
let refund_tx_id = self.chain_service.broadcast(&refund_tx).await?.to_string();
|
||||
|
||||
info!(
|
||||
"Successfully broadcast refund for Send Swap {}, is_cooperative: {is_cooperative}",
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use boltz_client::{
|
||||
@@ -47,7 +49,7 @@ impl From<MockHistory> for lwk_wollet::History {
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct MockLiquidChainService {
|
||||
history: Vec<MockHistory>,
|
||||
history: Mutex<Vec<MockHistory>>,
|
||||
}
|
||||
|
||||
impl MockLiquidChainService {
|
||||
@@ -55,15 +57,19 @@ impl MockLiquidChainService {
|
||||
MockLiquidChainService::default()
|
||||
}
|
||||
|
||||
pub(crate) fn set_history(&mut self, history: Vec<MockHistory>) -> &mut Self {
|
||||
self.history = history;
|
||||
pub(crate) fn set_history(&self, history: Vec<MockHistory>) -> &Self {
|
||||
*self.history.lock().unwrap() = history;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn get_history(&self) -> Vec<MockHistory> {
|
||||
self.history.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LiquidChainService for MockLiquidChainService {
|
||||
async fn tip(&mut self) -> Result<u32> {
|
||||
async fn tip(&self) -> Result<u32> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
@@ -92,7 +98,7 @@ impl LiquidChainService for MockLiquidChainService {
|
||||
&self,
|
||||
_scripts: &ElementsScript,
|
||||
) -> Result<Vec<lwk_wollet::History>> {
|
||||
Ok(self.history.clone().into_iter().map(Into::into).collect())
|
||||
Ok(self.get_history().into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
||||
async fn get_script_history_with_retry(
|
||||
@@ -100,7 +106,7 @@ impl LiquidChainService for MockLiquidChainService {
|
||||
_script: &ElementsScript,
|
||||
_retries: u64,
|
||||
) -> Result<Vec<lwk_wollet::History>> {
|
||||
Ok(self.history.clone().into_iter().map(Into::into).collect())
|
||||
Ok(self.get_history().into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
||||
async fn get_scripts_history(&self, _scripts: &[&ElementsScript]) -> Result<Vec<Vec<History>>> {
|
||||
|
||||
@@ -34,7 +34,7 @@ pub(crate) fn new_chain_swap_handler(persister: Arc<Persister>) -> Result<ChainS
|
||||
let signer: Arc<Box<dyn Signer>> = Arc::new(Box::new(MockSigner::new()?));
|
||||
let onchain_wallet = Arc::new(MockWallet::new(signer)?);
|
||||
let swapper = Arc::new(BoltzSwapper::new(config.clone(), None));
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
ChainSwapHandler::new(
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
use anyhow::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{
|
||||
model::{Config, Signer},
|
||||
persist::Persister,
|
||||
@@ -22,7 +20,7 @@ pub(crate) fn new_receive_swap_handler(persister: Arc<Persister>) -> Result<Rece
|
||||
let signer: Arc<Box<dyn Signer>> = Arc::new(Box::new(MockSigner::new()?));
|
||||
let onchain_wallet = Arc::new(MockWallet::new(signer)?);
|
||||
let swapper = Arc::new(MockSwapper::default());
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
|
||||
Ok(ReceiveSwapHandler::new(
|
||||
config,
|
||||
|
||||
@@ -14,7 +14,7 @@ pub(crate) fn new_recoverer(
|
||||
swapper: Arc<dyn Swapper>,
|
||||
onchain_wallet: Arc<dyn OnchainWallet>,
|
||||
) -> Result<Recoverer> {
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
Recoverer::new(
|
||||
|
||||
@@ -31,7 +31,7 @@ pub(crate) fn new_liquid_sdk(
|
||||
swapper: Arc<MockSwapper>,
|
||||
status_stream: Arc<MockStatusStream>,
|
||||
) -> Result<LiquidSdk> {
|
||||
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let liquid_chain_service = Arc::new(MockLiquidChainService::new());
|
||||
let bitcoin_chain_service = Arc::new(Mutex::new(MockBitcoinChainService::new()));
|
||||
|
||||
new_liquid_sdk_with_chain_services(
|
||||
@@ -48,7 +48,7 @@ pub(crate) fn new_liquid_sdk_with_chain_services(
|
||||
persister: Arc<Persister>,
|
||||
swapper: Arc<MockSwapper>,
|
||||
status_stream: Arc<MockStatusStream>,
|
||||
liquid_chain_service: Arc<Mutex<MockLiquidChainService>>,
|
||||
liquid_chain_service: Arc<MockLiquidChainService>,
|
||||
bitcoin_chain_service: Arc<Mutex<MockBitcoinChainService>>,
|
||||
onchain_fee_rate_leeway_sat_per_vbyte: Option<u32>,
|
||||
) -> Result<LiquidSdk> {
|
||||
|
||||
@@ -8,7 +8,6 @@ use crate::{
|
||||
send_swap::SendSwapHandler,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::{
|
||||
chain::MockLiquidChainService,
|
||||
@@ -21,7 +20,7 @@ pub(crate) fn new_send_swap_handler(persister: Arc<Persister>) -> Result<SendSwa
|
||||
let signer: Arc<Box<dyn Signer>> = Arc::new(Box::new(MockSigner::new()?));
|
||||
let onchain_wallet = Arc::new(MockWallet::new(signer)?);
|
||||
let swapper = Arc::new(MockSwapper::default());
|
||||
let chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
|
||||
let chain_service = Arc::new(MockLiquidChainService::new());
|
||||
|
||||
Ok(SendSwapHandler::new(
|
||||
config,
|
||||
|
||||
Reference in New Issue
Block a user