From 79b3d8deb7e130166b6c35a431b02daad85570e8 Mon Sep 17 00:00:00 2001 From: yse Date: Wed, 4 Dec 2024 16:11:57 +0100 Subject: [PATCH] fix: set initial pulled state to `Recoverable` --- lib/core/src/chain_swap.rs | 5 +++++ lib/core/src/model.rs | 6 ++++++ lib/core/src/persist/sync.rs | 6 +++--- lib/core/src/receive_swap.rs | 9 ++++++--- lib/core/src/sdk.rs | 6 +++--- lib/core/src/send_swap.rs | 5 +++++ 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/core/src/chain_swap.rs b/lib/core/src/chain_swap.rs index 72150b34c..19baec76c 100644 --- a/lib/core/src/chain_swap.rs +++ b/lib/core/src/chain_swap.rs @@ -1096,6 +1096,11 @@ impl ChainSwapHandler { to_state: PaymentState, ) -> Result<(), PaymentError> { match (from_state, to_state) { + (Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()), + (_, Recoverable) => Err(PaymentError::Generic { + err: format!("Cannot transition from {from_state:?} to Recoverable state"), + }), + (_, Created) => Err(PaymentError::Generic { err: "Cannot transition to Created state".to_string(), }), diff --git a/lib/core/src/model.rs b/lib/core/src/model.rs index a5e971a38..ec48ec95b 100644 --- a/lib/core/src/model.rs +++ b/lib/core/src/model.rs @@ -1003,6 +1003,12 @@ pub enum PaymentState { /// /// When the refund tx is broadcast, `refund_tx_id` is set in the swap. RefundPending = 6, + + /// ## Recoverable Swaps + /// + /// The status for swaps that have been synced in, and whose information is recoverable from + /// chain + Recoverable = 7, } impl ToSql for PaymentState { fn to_sql(&self) -> rusqlite::Result> { diff --git a/lib/core/src/persist/sync.rs b/lib/core/src/persist/sync.rs index edb59f06f..f53ddad2d 100644 --- a/lib/core/src/persist/sync.rs +++ b/lib/core/src/persist/sync.rs @@ -479,7 +479,7 @@ impl Persister { :description, :state )", - [params, &[(":state", &PaymentState::Created)]] + [params, &[(":state", &PaymentState::Recoverable)]] .concat() .as_slice(), )?; @@ -572,7 +572,7 @@ impl Persister { :description, :state )", - [params, &[(":state", &PaymentState::Created)]] + [params, &[(":state", &PaymentState::Recoverable)]] .concat() .as_slice(), )?; @@ -684,7 +684,7 @@ impl Persister { :server_lockup_tx_id, :state )", - [params, &[(":state", &PaymentState::Created)]] + [params, &[(":state", &PaymentState::Recoverable)]] .concat() .as_slice(), )?; diff --git a/lib/core/src/receive_swap.rs b/lib/core/src/receive_swap.rs index fd87c5558..730befc9e 100644 --- a/lib/core/src/receive_swap.rs +++ b/lib/core/src/receive_swap.rs @@ -9,9 +9,7 @@ use lwk_wollet::hashes::hex::DisplayHex; use tokio::sync::{broadcast, Mutex}; use crate::chain::liquid::LiquidChainService; -use crate::model::PaymentState::{ - Complete, Created, Failed, Pending, RefundPending, Refundable, TimedOut, -}; +use crate::model::PaymentState::*; use crate::model::{Config, PaymentTxData, PaymentType, ReceiveSwap}; use crate::prelude::{Swap, Transaction}; use crate::{ensure_sdk, utils}; @@ -372,6 +370,11 @@ impl ReceiveSwapHandler { to_state: PaymentState, ) -> Result<(), PaymentError> { match (from_state, to_state) { + (Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()), + (_, Recoverable) => Err(PaymentError::Generic { + err: format!("Cannot transition from {from_state:?} to Recoverable state"), + }), + (_, Created) => Err(PaymentError::Generic { err: "Cannot transition to Created state".to_string(), }), diff --git a/lib/core/src/sdk.rs b/lib/core/src/sdk.rs index 59a959327..e4f3e9e48 100644 --- a/lib/core/src/sdk.rs +++ b/lib/core/src/sdk.rs @@ -546,12 +546,12 @@ impl LiquidSdk { None => pending_send_sat += p.amount_sat, }, Created => pending_send_sat += p.amount_sat, - Refundable | RefundPending | TimedOut => {} + Refundable | RefundPending | TimedOut | Recoverable => {} }, PaymentType::Receive => match p.status { Complete => confirmed_received_sat += p.amount_sat, Pending => pending_receive_sat += p.amount_sat, - Created | Refundable | RefundPending | Failed | TimedOut => {} + Created | Refundable | RefundPending | Failed | TimedOut | Recoverable => {} }, } } @@ -1035,7 +1035,7 @@ impl LiquidSdk { } Pending => return Err(PaymentError::PaymentInProgress), Complete => return Err(PaymentError::AlreadyPaid), - RefundPending | Refundable | Failed => { + RefundPending | Refundable | Failed | Recoverable => { return Err(PaymentError::invalid_invoice( "Payment has already failed. Please try with another invoice", )) diff --git a/lib/core/src/send_swap.rs b/lib/core/src/send_swap.rs index 7665b3ac2..a1c7f7888 100644 --- a/lib/core/src/send_swap.rs +++ b/lib/core/src/send_swap.rs @@ -498,6 +498,11 @@ impl SendSwapHandler { to_state: PaymentState, ) -> Result<(), PaymentError> { match (from_state, to_state) { + (Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()), + (_, Recoverable) => Err(PaymentError::Generic { + err: format!("Cannot transition from {from_state:?} to Recoverable state"), + }), + (TimedOut, Created) => Ok(()), (_, Created) => Err(PaymentError::Generic { err: "Cannot transition from {from_state:?} to Created state".to_string(),