Skip to content

Commit

Permalink
Allow emitting a payment event without tx_id
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Dec 19, 2024
1 parent d572465 commit 3f49619
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 60 deletions.
25 changes: 2 additions & 23 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,27 +716,6 @@ impl ChainSwapHandler {
})
}

fn notify_swap_changes(
&self,
swap: ChainSwap,
updated_swap: ChainSwap,
) -> Result<(), PaymentError> {
let payment_id = match swap.direction {
Direction::Incoming => updated_swap
.claim_tx_id
.clone()
.or(swap.claim_tx_id.clone()),
Direction::Outgoing => updated_swap
.user_lockup_tx_id
.clone()
.or(swap.user_lockup_tx_id.clone()),
};
if let Some(payment_id) = payment_id {
let _ = self.subscription_notifier.send(payment_id);
}
Ok(())
}

// Updates the swap without state transition validation
pub(crate) fn update_swap(&self, updated_swap: ChainSwap) -> Result<(), PaymentError> {
let swap = self.fetch_chain_swap_by_id(&updated_swap.id)?;
Expand All @@ -751,7 +730,7 @@ impl ChainSwapHandler {
updated_swap.refund_tx_id
);
self.persister.insert_or_update_chain_swap(&updated_swap)?;
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand All @@ -767,7 +746,7 @@ impl ChainSwapHandler {
self.persister.try_handle_chain_swap_update(swap_update)?;
let updated_swap = self.fetch_chain_swap_by_id(&swap_update.swap_id)?;
if updated_swap != swap {
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand Down
6 changes: 5 additions & 1 deletion lib/core/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ impl Persister {
Ok(self
.get_connection()?
.query_row(
&self.select_payment_query(Some("ptx.tx_id = ?1"), None, None),
&self.select_payment_query(
Some("(ptx.tx_id = ?1 OR COALESCE(rs.id, ss.id, cs.id) = ?1)"),
None,
None,
),
params![id],
|row| self.sql_row_to_payment(row),
)
Expand Down
21 changes: 2 additions & 19 deletions lib/core/src/receive_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,6 @@ impl ReceiveSwapHandler {
})
}

fn notify_swap_changes(
&self,
swap: ReceiveSwap,
updated_swap: ReceiveSwap,
) -> Result<(), PaymentError> {
let payment_id = updated_swap
.claim_tx_id
.clone()
.or(updated_swap.mrh_tx_id.clone())
.or(swap.claim_tx_id.clone())
.or(swap.mrh_tx_id.clone());
if let Some(payment_id) = payment_id {
let _ = self.subscription_notifier.send(payment_id);
}
Ok(())
}

// Updates the swap without state transition validation
pub(crate) fn update_swap(&self, updated_swap: ReceiveSwap) -> Result<(), PaymentError> {
let swap = self.fetch_receive_swap_by_id(&updated_swap.id)?;
Expand All @@ -267,7 +250,7 @@ impl ReceiveSwapHandler {
);
self.persister
.insert_or_update_receive_swap(&updated_swap)?;
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand Down Expand Up @@ -303,7 +286,7 @@ impl ReceiveSwapHandler {
}

if updated_swap != swap {
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand Down
19 changes: 2 additions & 17 deletions lib/core/src/send_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,6 @@ impl SendSwapHandler {
})
}

fn notify_swap_changes(
&self,
swap: SendSwap,
updated_swap: SendSwap,
) -> Result<(), PaymentError> {
let payment_id = updated_swap
.lockup_tx_id
.clone()
.or(swap.lockup_tx_id.clone());
if let Some(payment_id) = payment_id {
let _ = self.subscription_notifier.send(payment_id);
}
Ok(())
}

// Updates the swap without state transition validation
pub(crate) fn update_swap(&self, updated_swap: SendSwap) -> Result<(), PaymentError> {
let swap = self.fetch_send_swap_by_id(&updated_swap.id)?;
Expand All @@ -277,7 +262,7 @@ impl SendSwapHandler {
updated_swap.refund_tx_id
);
self.persister.insert_or_update_send_swap(&updated_swap)?;
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand Down Expand Up @@ -306,7 +291,7 @@ impl SendSwapHandler {
)?;
let updated_swap = self.fetch_send_swap_by_id(swap_id)?;
if updated_swap != swap {
self.notify_swap_changes(swap, updated_swap)?;
let _ = self.subscription_notifier.send(updated_swap.id);
}
Ok(())
}
Expand Down

0 comments on commit 3f49619

Please sign in to comment.