Skip to content

Commit

Permalink
Handle Cancelled edge cases for reverse swap
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Oct 2, 2023
1 parent 41362aa commit 247392e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,10 +1182,12 @@ impl BreezServices {
r#"
info,
breez_sdk_core::input_parser=warn,
breez_sdk_core::backup=info,
breez_sdk_core::persist::reverseswap=info,
breez_sdk_core::reverseswap=info,
gl_client=warn,
h2=warn,
hyper=warn,
breez_sdk_core::reverseswap=info,
lightning_signer=warn,
reqwest=warn,
rustls=warn,
Expand Down
2 changes: 2 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ impl From<FullReverseSwapInfo> for ReverseSwapInfo {
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum ReverseSwapStatus {
/// HODL invoice payment is not completed yet
///
/// This is also the temporary status of a reverse swap when restoring a node, until `sync` finishes.
Initial = 0,

/// HODL invoice payment was successfully triggered and confirmed by Boltz, but the reverse swap
Expand Down
35 changes: 27 additions & 8 deletions libs/sdk-core/src/reverseswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::chain::{get_utxos, ChainService, MempoolSpace};
use crate::models::{ReverseSwapServiceAPI, ReverseSwapperRoutingAPI};
use crate::ReverseSwapStatus::*;
use crate::{
BreezEvent, Config, FullReverseSwapInfo, NodeAPI, ReverseSwapInfoCached, ReverseSwapPairInfo,
ReverseSwapStatus,
BreezEvent, Config, FullReverseSwapInfo, NodeAPI, PaymentStatus, ReverseSwapInfoCached,
ReverseSwapPairInfo, ReverseSwapStatus,
};
use anyhow::{anyhow, ensure, Result};
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
Expand Down Expand Up @@ -449,11 +449,17 @@ impl BTCSendSwap {
"Tried to get status for non-monitored reverse swap"
);

let payment_hash_hex = &rsi.get_preimage_hash().to_hex();
let payment_status = self.persister.get_payment_by_hash(payment_hash_hex)?;
if let Some(ref payment) = payment_status {
if payment.status == PaymentStatus::Failed {
warn!("Payment failed for reverse swap {}", rsi.id);
return Ok(Some(Cancelled));
}
}

let new_status = match &current_status {
Initial => match self
.persister
.get_payment_by_hash(&rsi.get_preimage_hash().to_hex())?
{
Initial => match payment_status {
Some(_) => Some(InProgress),
None => match self
.reverse_swap_service_api
Expand All @@ -469,11 +475,24 @@ impl BTCSendSwap {
_ => None,
},
},
InProgress | CompletedSeen => match self.get_claim_tx_status(rsi).await? {
TxStatus::Unknown => None,
InProgress => match self.get_claim_tx_status(rsi).await? {
TxStatus::Unknown => {
let block_height = self.chain_service.current_tip().await?;
match block_height >= rsi.timeout_block_height {
true => {
warn!("Reverse swap {} crossed the timeout block height", rsi.id);
Some(Cancelled)
}
false => None,
}
}
TxStatus::Mempool => Some(CompletedSeen),
TxStatus::Confirmed => Some(CompletedConfirmed),
},
CompletedSeen => match self.get_claim_tx_status(rsi).await? {
TxStatus::Confirmed => Some(CompletedConfirmed),
_ => None,
},
_ => None,
};

Expand Down

0 comments on commit 247392e

Please sign in to comment.