Skip to content

Commit

Permalink
feat: add database locking to make send atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse committed Oct 31, 2024
1 parent 63e4b47 commit 131e5bf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
43 changes: 43 additions & 0 deletions lib/core/src/persist/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,49 @@ impl Persister {

Ok(())
}

pub(crate) fn set_send_swap_lockup_tx_id(
&self,
swap_id: &str,
lockup_tx_id: &str,
) -> Result<(), PaymentError> {
let con = self.get_connection()?;

let row_count = con
.execute(
"UPDATE send_swaps
SET lockup_tx_id = :lockup_tx_id
WHERE id = :id AND lockup_tx_id IS NULL",
named_params! {
":id": swap_id,
":lockup_tx_id": lockup_tx_id,
},
)
.map_err(|_| PaymentError::PersistError)?;
match row_count {
1 => Ok(()),
_ => Err(PaymentError::AlreadyClaimed),
}
}

pub(crate) fn unset_send_swap_lockup_tx_id(
&self,
swap_id: &str,
lockup_tx_id: &str,
) -> Result<(), PaymentError> {
let con = self.get_connection()?;
con.execute(
"UPDATE send_swaps
SET lockup_tx_id = NULL
WHERE id = :id AND lockup_tx_id = :lockup_tx_id",
named_params! {
":id": swap_id,
":lockup_tx_id": lockup_tx_id,
},
)
.map_err(|_| PaymentError::PersistError)?;
Ok(())
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
3 changes: 1 addition & 2 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,7 @@ impl LiquidSdk {
let create_response = swap.get_boltz_create_response()?;
self.try_lockup(&swap, &create_response).await?;

let accept_zero_conf = create_response.accept_zero_conf;
self.wait_for_payment(Swap::Send(swap), accept_zero_conf)
self.wait_for_payment(Swap::Send(swap), create_response.accept_zero_conf)
.await
.map(|payment| SendPaymentResponse { payment })
}
Expand Down
25 changes: 19 additions & 6 deletions lib/core/src/send_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,31 @@ impl SendSwapHandler {
create_response.expected_amount,
)
.await?;
let lockup_tx_id = lockup_tx.txid().to_string();

info!("broadcasting lockup tx {}", lockup_tx.txid());
let lockup_tx_id = self
self.persister
.set_send_swap_lockup_tx_id(swap_id, &lockup_tx_id)?;

info!("Broadcasting lockup tx {lockup_tx_id} for Send swap {swap_id}",);
let broadcast_result = self
.chain_service
.lock()
.await
.broadcast(&lockup_tx, Some(swap_id))
.await?
.to_string();
.await;

info!("Successfully broadcast lockup tx for Send Swap {swap_id}. Lockup tx id: {lockup_tx_id}");
Ok(lockup_tx)
match broadcast_result {
Ok(_) => {
info!("Successfully broadcast lockup tx for Send Swap {swap_id}. Lockup tx id: {lockup_tx_id}");
Ok(lockup_tx)
}
Err(err) => {
debug!("Could not broadcast lockup tx for Send Swap {swap_id}: {err:?}");
self.persister
.unset_send_swap_lockup_tx_id(swap_id, &lockup_tx_id)?;
Err(err.into())
}
}
}

/// Transitions a Send swap to a new state
Expand Down

0 comments on commit 131e5bf

Please sign in to comment.