Skip to content

Commit

Permalink
Merge pull request #496 from breez/ok300-fix-rev-swap-state-transitions
Browse files Browse the repository at this point in the history
Fix rev swap state transitions
  • Loading branch information
ok300 authored Oct 2, 2023
2 parents 6e166c8 + 247392e commit f79ba7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 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
7 changes: 4 additions & 3 deletions libs/sdk-core/src/persist/reverseswap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::db::SqliteStorage;
use crate::{FullReverseSwapInfo, ReverseSwapInfoCached, ReverseSwapStatus};
use anyhow::Result;
use rusqlite::types::FromSqlError;
use rusqlite::{named_params, Row};

impl SqliteStorage {
Expand Down Expand Up @@ -47,7 +46,7 @@ impl SqliteStorage {
debug!("Persisting new status for reverse swap {id} to be {status:?}");

self.get_connection()?.execute(
"UPDATE reverse_swaps_info SET status=:status where id=:id",
"INSERT OR REPLACE INTO reverse_swaps_info VALUES(:id, :status)",
named_params! {
":status": serde_json::to_value(status)?,
":id": id,
Expand Down Expand Up @@ -82,8 +81,10 @@ impl SqliteStorage {
sat_per_vbyte: row.get("sat_per_vbyte")?,
redeem_script: row.get("redeem_script")?,
cache: ReverseSwapInfoCached {
// The status is stored in the main DB, which is empty when the node is restored.
// We therefore default to the Initial state. This will be updated at the end of sync().
status: serde_json::from_value(row.get("status")?)
.map_err(|_| FromSqlError::InvalidType)?,
.unwrap_or(ReverseSwapStatus::Initial),
},
})
}
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 f79ba7c

Please sign in to comment.