Skip to content

Commit

Permalink
Optimistically persist accepted receiver amount
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Jan 2, 2025
1 parent c4f707f commit a020ed5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ impl ChainSwapHandler {
async fn handle_amountless_update(&self, swap: &ChainSwap) -> Result<(), PaymentError> {
let id = swap.id.clone();

// Since we optimistically persist the accepted receiver amount, if accepting a quote with
// the swapper fails, we might still think it's accepted, so now we should get rid of the
// old invalid accepted amount.
if swap.accepted_receiver_amount_sat.is_some() {
info!("Handling amountless update for swap {id} with existing accepted receiver amount. Erasing the accepted amount now...");
self.persister.update_accepted_receiver_amount(&id, None)?;
}

let quote = self
.swapper
.get_zero_amount_chain_swap_quote(&id)
Expand All @@ -394,10 +402,14 @@ impl ChainSwapHandler {
debug!("Zero-amount swap validated. Auto-accepting...");
self.persister
.update_actual_payer_amount(&id, user_lockup_amount_sat)?;
self.swapper
.accept_zero_amount_chain_swap_quote(&id, quote)?;
self.persister
.update_accepted_receiver_amount(&id, receiver_amount_sat)
.update_accepted_receiver_amount(&id, Some(receiver_amount_sat))?;
self.swapper
.accept_zero_amount_chain_swap_quote(&id, quote)
.inspect_err(|e| {
error!("Failed to accept zero-amount swap {id} quote: {e} - trying to erase the accepted receiver amount...");
let _ = self.persister.update_accepted_receiver_amount(&id, None);
})
}
ValidateAmountlessSwapResult::RequiresUserAction {
user_lockup_amount_sat,
Expand Down
6 changes: 4 additions & 2 deletions lib/core/src/persist/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,15 @@ impl Persister {
}

/// Used for receive chain swaps, when fees are accepted and thus the agreed received amount is known
///
/// Can also be used to erase a previously persisted accepted amount in case of failure to accept.
pub(crate) fn update_accepted_receiver_amount(
&self,
swap_id: &str,
accepted_receiver_amount_sat: u64,
accepted_receiver_amount_sat: Option<u64>,
) -> Result<(), PaymentError> {
log::info!(
"Updating chain swap {swap_id}: accepted_receiver_amount_sat = {accepted_receiver_amount_sat}"
"Updating chain swap {swap_id}: accepted_receiver_amount_sat = {accepted_receiver_amount_sat:?}"
);
let mut con: Connection = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;
Expand Down
12 changes: 9 additions & 3 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2651,10 +2651,16 @@ impl LiquidSdk {
PaymentError::InvalidOrExpiredFees
);

self.swapper
.accept_zero_amount_chain_swap_quote(&swap_id, server_lockup_quote.to_sat())?;
self.persister
.update_accepted_receiver_amount(&swap_id, payer_amount_sat - fees_sat)?;
.update_accepted_receiver_amount(&swap_id, Some(payer_amount_sat - fees_sat))?;
self.swapper
.accept_zero_amount_chain_swap_quote(&swap_id, server_lockup_quote.to_sat())
.inspect_err(|e| {
error!("Failed to accept zero-amount swap {swap_id} quote: {e} - trying to erase the accepted receiver amount...");
let _ = self
.persister
.update_accepted_receiver_amount(&swap_id, None);
})?;
self.chain_swap_handler.update_swap_info(&ChainSwapUpdate {
swap_id,
to_state: Pending,
Expand Down

0 comments on commit a020ed5

Please sign in to comment.