Skip to content

Commit

Permalink
Handle Recoverable state
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Dec 5, 2024
1 parent 9279ab9 commit 54609d3
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ uint16_t uniffi_breez_sdk_liquid_bindings_checksum_method_signer_slip77_master_b
);
uint16_t uniffi_breez_sdk_liquid_bindings_checksum_method_signer_hmac_sha256(void

);
uint16_t uniffi_breez_sdk_liquid_bindings_checksum_method_signer_ecies_encrypt(void

);
uint16_t uniffi_breez_sdk_liquid_bindings_checksum_method_signer_ecies_decrypt(void

);
uint32_t ffi_breez_sdk_liquid_bindings_uniffi_contract_version(void

Expand Down
10 changes: 6 additions & 4 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ChainSwapHandler {
.persister
.list_chain_swaps()?
.into_iter()
.filter(|s| s.direction == Direction::Incoming)
.filter(|s| s.direction == Direction::Incoming && s.state != PaymentState::Recoverable)
.collect();
info!(
"Rescanning {} incoming Chain Swap(s) user lockup txs at height {}",
Expand Down Expand Up @@ -745,7 +745,7 @@ impl ChainSwapHandler {
pub(crate) async fn update_swap_info(
&self,
swap_update: &ChainSwapUpdate,
) -> Result<(), PaymentError> {
) -> Result<(PaymentState, PaymentState), PaymentError> {
info!("Updating Chain swap {swap_update:?}");
let swap = self.fetch_chain_swap_by_id(&swap_update.swap_id)?;
Self::validate_state_transition(swap.state, swap_update.to_state)?;
Expand All @@ -768,7 +768,7 @@ impl ChainSwapHandler {
if updated_swap != swap {
payment_id.and_then(|payment_id| self.subscription_notifier.send(payment_id).ok());
}
Ok(())
Ok((swap.state, updated_swap.state))
}

async fn claim(&self, swap_id: &str) -> Result<(), PaymentError> {
Expand Down Expand Up @@ -1101,7 +1101,9 @@ impl ChainSwapHandler {
to_state: PaymentState,
) -> Result<(), PaymentError> {
match (from_state, to_state) {
(Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()),
(Recoverable, Created | Pending | Refundable | RefundPending | Failed | Complete) => {
Ok(())
}
(_, Recoverable) => Err(PaymentError::Generic {
err: format!("Cannot transition from {from_state:?} to Recoverable state"),
}),
Expand Down
11 changes: 11 additions & 0 deletions lib/core/src/persist/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ impl Persister {
self.list_receive_swaps_where(&con, where_clause)
}

pub(crate) fn list_recoverable_receive_swaps(&self) -> Result<Vec<ReceiveSwap>> {
let con = self.get_connection()?;
let where_clause = vec![get_where_clause_state_in(&[
PaymentState::Created,
PaymentState::Pending,
PaymentState::Recoverable,
])];

self.list_receive_swaps_where(&con, where_clause)
}

// Only set the Receive Swap claim_tx_id if not set, otherwise return an error
pub(crate) fn set_receive_swap_claim_tx_id(
&self,
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/persist/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ impl Persister {
self.list_send_swaps_where(&con, where_clause)
}

pub(crate) fn list_pending_and_ongoing_send_swaps(&self) -> Result<Vec<SendSwap>> {
pub(crate) fn list_recoverable_send_swaps(&self) -> Result<Vec<SendSwap>> {
let con = self.get_connection()?;
let where_clause = vec![get_where_clause_state_in(&[
PaymentState::Created,
PaymentState::Pending,
PaymentState::RefundPending,
PaymentState::Recoverable,
])];
self.list_send_swaps_where(&con, where_clause)
}
Expand Down
8 changes: 5 additions & 3 deletions lib/core/src/receive_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl ReceiveSwapHandler {
lockup_tx_id: Option<&str>,
mrh_tx_id: Option<&str>,
mrh_amount_sat: Option<u64>,
) -> Result<(), PaymentError> {
) -> Result<(PaymentState, PaymentState), PaymentError> {
info!(
"Transitioning Receive swap {} to {:?} (claim_tx_id = {:?}, lockup_tx_id = {:?}, mrh_tx_id = {:?})",
swap_id, to_state, claim_tx_id, lockup_tx_id, mrh_tx_id
Expand Down Expand Up @@ -280,7 +280,7 @@ impl ReceiveSwapHandler {
if updated_swap != swap {
payment_id.and_then(|payment_id| self.subscription_notifier.send(payment_id).ok());
}
Ok(())
Ok((swap.state, updated_swap.state))
}

async fn claim(&self, swap_id: &str) -> Result<(), PaymentError> {
Expand Down Expand Up @@ -365,7 +365,9 @@ impl ReceiveSwapHandler {
to_state: PaymentState,
) -> Result<(), PaymentError> {
match (from_state, to_state) {
(Recoverable, Pending | Refundable | RefundPending | Failed | Complete) => Ok(()),
(Recoverable, Created | Pending | Refundable | RefundPending | Failed | Complete) => {
Ok(())
}
(_, Recoverable) => Err(PaymentError::Generic {
err: format!("Cannot transition from {from_state:?} to Recoverable state"),
}),
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ pub(crate) struct RecoveredOnchainData {

impl LiquidSdk {
pub(crate) async fn get_monitored_swaps_list(&self, partial_sync: bool) -> Result<SwapsList> {
let receive_swaps = self.persister.list_ongoing_receive_swaps()?;
let receive_swaps = self.persister.list_recoverable_receive_swaps()?;
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 final_swap_states = [PaymentState::Complete, PaymentState::Failed];

let send_swaps = self.persister.list_pending_and_ongoing_send_swaps()?;
let send_swaps = self.persister.list_recoverable_send_swaps()?;
let chain_swaps: Vec<ChainSwap> = self
.persister
.list_chain_swaps()?
Expand Down
Loading

0 comments on commit 54609d3

Please sign in to comment.