From 7d307e581a842d674c067461bc74ca343fd2b818 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Fri, 16 Oct 2020 11:25:41 +1100 Subject: [PATCH] Fail the swap early if Bitcoin TxLock is never published This helps distinguish between the case where the refund timelock is reached before the bitcoin is locked and the case where the refund timelock is reached after the bitcoin is locked and before Alice sends over the transfer proof for locking up the monero. In the first case we can abort without doing anything, but in the second case we must instruct the caller to refund the bitcoin. --- xmr-btc/src/lib.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/xmr-btc/src/lib.rs b/xmr-btc/src/lib.rs index f1412366..c24ed222 100644 --- a/xmr-btc/src/lib.rs +++ b/xmr-btc/src/lib.rs @@ -145,6 +145,8 @@ where Gen::new_boxed(|co| async move { let swap_result: Result<(), SwapFailed> = async { let btc_has_expired = bitcoin_time_is_gte(bitcoin_ledger, refund_timelock).shared(); + let poll_until_btc_has_expired = poll_until(btc_has_expired.clone()).shared(); + futures::pin_mut!(poll_until_btc_has_expired); if btc_has_expired.clone().await { return Err(SwapFailed::BeforeBtcLock); @@ -152,8 +154,15 @@ where co.yield_(Action::LockBitcoin(tx_lock.clone())).await; - let poll_until_btc_has_expired = poll_until(btc_has_expired).shared(); - futures::pin_mut!(poll_until_btc_has_expired); + match select( + bitcoin_ledger.watch_for_raw_transaction(tx_lock.txid()), + poll_until_btc_has_expired.clone(), + ) + .await + { + Either::Left(_) => {} + Either::Right(_) => return Err(SwapFailed::BeforeBtcLock), + } let transfer_proof = match select( network.receive_transfer_proof(),