From 617a07096b13283325b48777055526f7a0e26157 Mon Sep 17 00:00:00 2001 From: yse Date: Tue, 31 Dec 2024 13:06:18 +0100 Subject: [PATCH] fix: address review comments taken from https://github.com/breez/breez-sdk-liquid/pull/622#pullrequestreview-2525447533 --- .../BreezSDKLiquid/Task/SwapUpdated.swift | 2 +- lib/core/src/chain/liquid.rs | 7 +++---- lib/core/src/model.rs | 4 ++-- lib/core/src/recover/recoverer.rs | 13 +++++-------- lib/core/src/sdk.rs | 4 ++-- lib/core/src/sync/model/data.rs | 2 ++ lib/core/src/sync/model/mod.rs | 2 +- lib/core/src/test_utils/chain.rs | 16 +++------------- 8 files changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift index bec2dce0a..9e785aaeb 100644 --- a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift +++ b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift @@ -52,7 +52,7 @@ class SwapUpdatedTask : TaskProtocol { func getSwapId(details: PaymentDetails?) -> String? { if let details = details { switch details { - case let .bitcoin(swapId, _, _, _, _): + case let .bitcoin(swapId, _, _, _, _, _): return swapId case let .lightning(swapId, _, _, _, _, _, _, _, _, _): return swapId diff --git a/lib/core/src/chain/liquid.rs b/lib/core/src/chain/liquid.rs index bd8222466..03e1f617a 100644 --- a/lib/core/src/chain/liquid.rs +++ b/lib/core/src/chain/liquid.rs @@ -5,7 +5,6 @@ use async_trait::async_trait; use boltz_client::ToHex; use log::{info, warn}; use lwk_wollet::elements::hex::FromHex; -use lwk_wollet::elements::BlockHeader; use lwk_wollet::{ elements::{ pset::serialize::Serialize, Address, BlockHash, OutPoint, Script, Transaction, Txid, @@ -27,7 +26,7 @@ const LIQUID_ESPLORA_URL: &str = "https://lq1.breez.technology/liquid/api"; #[async_trait] pub trait LiquidChainService: Send + Sync { /// Get the blockchain latest block - async fn tip(&mut self) -> Result; + async fn tip(&mut self) -> Result; /// Broadcast a transaction async fn broadcast(&self, tx: &Transaction, swap_id: Option<&str>) -> Result; @@ -100,8 +99,8 @@ impl HybridLiquidChainService { #[async_trait] impl LiquidChainService for HybridLiquidChainService { - async fn tip(&mut self) -> Result { - Ok(self.electrum_client.tip()?) + async fn tip(&mut self) -> Result { + Ok(self.electrum_client.tip()?.height) } async fn broadcast(&self, tx: &Transaction, swap_id: Option<&str>) -> Result { diff --git a/lib/core/src/model.rs b/lib/core/src/model.rs index a1e83b85e..8984308c4 100644 --- a/lib/core/src/model.rs +++ b/lib/core/src/model.rs @@ -1282,8 +1282,8 @@ pub struct PaymentSwapData { pub refund_tx_amount_sat: Option, /// Present only for chain swaps. - /// In case of an outgoing chain swap, it's the Bitcoin address which will receive the funds - /// In case of an incoming chain swap, it's the Liquid address which will receive the funds + /// In case of an incoming chain swap, it's the Bitcoin address which will receive the funds + /// In case of an outgoing chain swap, it's the Liquid address which will receive the funds pub claim_address: Option, /// Payment status derived from the swap status diff --git a/lib/core/src/recover/recoverer.rs b/lib/core/src/recover/recoverer.rs index 3f93c3599..0fe175bc5 100644 --- a/lib/core/src/recover/recoverer.rs +++ b/lib/core/src/recover/recoverer.rs @@ -161,9 +161,8 @@ impl Recoverer { log::warn!("Could not apply recovered data for Send swap {swap_id}: recovery data not found"); continue; }; - let timeout_block_height = - send_swap.get_boltz_create_response()?.timeout_block_height as u32; - let is_expired = liquid_tip.height >= timeout_block_height; + let timeout_block_height = send_swap.timeout_block_height as u32; + let is_expired = liquid_tip >= timeout_block_height; if let Some(new_state) = recovered_data.derive_partial_state(is_expired) { send_swap.state = new_state; } @@ -184,10 +183,8 @@ impl Recoverer { log::warn!("Could not apply recovered data for Receive swap {swap_id}: recovery data not found"); continue; }; - let timeout_block_height = receive_swap - .get_boltz_create_response()? - .timeout_block_height; - let is_expired = liquid_tip.height >= timeout_block_height; + let timeout_block_height = receive_swap.timeout_block_height; + let is_expired = liquid_tip >= timeout_block_height; if let Some(new_state) = recovered_data.derive_partial_state(is_expired) { receive_swap.state = new_state; } @@ -247,7 +244,7 @@ impl Recoverer { log::warn!("Could not apply recovered data for outgoing Chain swap {swap_id}: recovery data not found"); continue; }; - let is_expired = liquid_tip.height >= chain_swap.timeout_block_height; + let is_expired = liquid_tip >= chain_swap.timeout_block_height; if let Some(new_state) = recovered_data.derive_partial_state(is_expired) { chain_swap.state = new_state; } diff --git a/lib/core/src/sdk.rs b/lib/core/src/sdk.rs index 001813695..d2f11047a 100644 --- a/lib/core/src/sdk.rs +++ b/lib/core/src/sdk.rs @@ -355,7 +355,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.map(|tip| tip.height); + let liquid_tip_res = cloned.liquid_chain_service.lock().await.tip().await; let is_new_liquid_block = match &liquid_tip_res { Ok(height) => { debug!("Got Liquid tip: {height}"); @@ -2325,7 +2325,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?.height; + let liquid_height = self.liquid_chain_service.lock().await.tip().await?; let final_swap_states = [PaymentState::Complete, PaymentState::Failed]; let send_swaps = self diff --git a/lib/core/src/sync/model/data.rs b/lib/core/src/sync/model/data.rs index 9d16eb13d..51adf5570 100644 --- a/lib/core/src/sync/model/data.rs +++ b/lib/core/src/sync/model/data.rs @@ -99,6 +99,7 @@ pub(crate) struct SendSyncData { pub(crate) refund_private_key: String, pub(crate) payer_amount_sat: u64, pub(crate) receiver_amount_sat: u64, + #[serde(default)] pub(crate) timeout_block_height: u64, pub(crate) created_at: u32, pub(crate) preimage: Option, @@ -173,6 +174,7 @@ pub(crate) struct ReceiveSyncData { pub(crate) payer_amount_sat: u64, pub(crate) receiver_amount_sat: u64, pub(crate) mrh_address: String, + #[serde(default)] pub(crate) timeout_block_height: u32, pub(crate) created_at: u32, pub(crate) payment_hash: Option, diff --git a/lib/core/src/sync/model/mod.rs b/lib/core/src/sync/model/mod.rs index 7038cc4e5..4d2c009b2 100644 --- a/lib/core/src/sync/model/mod.rs +++ b/lib/core/src/sync/model/mod.rs @@ -18,7 +18,7 @@ pub(crate) mod sync; const MESSAGE_PREFIX: &[u8; 13] = b"realtimesync:"; lazy_static! { - static ref CURRENT_SCHEMA_VERSION: Version = Version::parse("0.0.1").unwrap(); + static ref CURRENT_SCHEMA_VERSION: Version = Version::parse("0.1.0").unwrap(); } #[derive(Copy, Clone)] diff --git a/lib/core/src/test_utils/chain.rs b/lib/core/src/test_utils/chain.rs index 17085893c..0a0073d58 100644 --- a/lib/core/src/test_utils/chain.rs +++ b/lib/core/src/test_utils/chain.rs @@ -16,8 +16,7 @@ use electrum_client::{ }; use lwk_wollet::{ bitcoin::constants::genesis_block, - elements::{BlockHash, BlockHeader, TxMerkleNode, Txid as ElementsTxid}, - hashes::Hash, + elements::{BlockHash, Txid as ElementsTxid}, History, }; @@ -64,17 +63,8 @@ impl MockLiquidChainService { #[async_trait] impl LiquidChainService for MockLiquidChainService { - async fn tip(&mut self) -> Result { - let block_enc = BlockHash::engine(); - let merkle_enc = TxMerkleNode::engine(); - Ok(BlockHeader { - version: 0, - prev_blockhash: BlockHash::from_engine(block_enc), - merkle_root: TxMerkleNode::from_engine(merkle_enc), - time: 0, - height: 0, - ext: Default::default(), - }) + async fn tip(&mut self) -> Result { + Ok(0) } async fn broadcast(