From c9d492d155140bdd286ad7d10531cd45de08f412 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Tue, 22 Dec 2020 17:08:17 +1100 Subject: [PATCH] Only wait for xmr-lock until t1 expired --- swap/src/bob/swap.rs | 31 +++++++++++++++++++++++++------ xmr-btc/src/alice.rs | 10 ++-------- xmr-btc/src/bitcoin.rs | 23 +++++++++++++++++++---- xmr-btc/src/bob.rs | 20 +++++++++++--------- 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index 596603df..b2b55c82 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -227,13 +227,32 @@ where { event_loop_handle.dial().await?; - // todo: watch until t1, not indefinitely - let msg2 = event_loop_handle.recv_message2().await?; - let state4 = state3 - .watch_for_lock_xmr(monero_wallet.as_ref(), msg2) - .await?; + let msg2_watcher = event_loop_handle.recv_message2(); + let t1_timeout = state3.wait_for_t1(bitcoin_wallet.as_ref()); - BobState::XmrLocked(state4) + select! { + msg2 = msg2_watcher => { + + let xmr_lock_watcher = state3.clone() + .watch_for_lock_xmr(monero_wallet.as_ref(), msg2?); + let t1_timeout = state3.wait_for_t1(bitcoin_wallet.as_ref()); + + select! { + state4 = xmr_lock_watcher => { + BobState::XmrLocked(state4?) + }, + _ = t1_timeout => { + let state4 = state3.t1_expired(); + BobState::T1Expired(state4) + } + } + + }, + _ = t1_timeout => { + let state4 = state3.t1_expired(); + BobState::T1Expired(state4) + } + } } else { let state4 = state3.t1_expired(); BobState::T1Expired(state4) diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index bf2af24b..94c30acf 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -28,7 +28,7 @@ use std::{ use tokio::{sync::Mutex, time::timeout}; use tracing::{error, info}; pub mod message; -use crate::bitcoin::{BlockHeight, TransactionBlockHeight, current_epoch}; +use crate::bitcoin::{current_epoch, wait_for_t1, BlockHeight, TransactionBlockHeight}; pub use message::{Message, Message0, Message1, Message2}; #[derive(Debug)] @@ -684,13 +684,7 @@ impl State3 { where W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, { - let tx_id = self.tx_lock.txid(); - let tx_lock_height = bitcoin_wallet.transaction_block_height(tx_id).await; - - let t1_timeout = - poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + self.refund_timelock); - t1_timeout.await; - Ok(()) + wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await } pub async fn current_epoch(&self, bitcoin_wallet: &W) -> Result diff --git a/xmr-btc/src/bitcoin.rs b/xmr-btc/src/bitcoin.rs index e6d316e3..733b4a0d 100644 --- a/xmr-btc/src/bitcoin.rs +++ b/xmr-btc/src/bitcoin.rs @@ -11,10 +11,10 @@ use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::str::FromStr; +use crate::Epoch; pub use bitcoin::{util::psbt::PartiallySignedTransaction, *}; pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund}; -use crate::Epoch; // TODO: Configurable tx-fee (note: parties have to agree prior to swapping) // Current reasoning: @@ -245,15 +245,14 @@ where } } - pub async fn current_epoch( bitcoin_wallet: &W, refund_timelock: u32, punish_timelock: u32, lock_tx_id: ::bitcoin::Txid, ) -> anyhow::Result - where - W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, +where + W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, { let current_block_height = bitcoin_wallet.block_height().await; let t0 = bitcoin_wallet.transaction_block_height(lock_tx_id).await; @@ -266,3 +265,19 @@ pub async fn current_epoch( (false, false) => Ok(Epoch::T2), } } + +pub async fn wait_for_t1( + bitcoin_wallet: &W, + refund_timelock: u32, + lock_tx_id: ::bitcoin::Txid, +) -> Result<()> +where + W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, +{ + let tx_lock_height = bitcoin_wallet.transaction_block_height(lock_tx_id).await; + + let t1_timeout = + poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + refund_timelock); + t1_timeout.await; + Ok(()) +} diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index 86f85963..d0aa4049 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -34,12 +34,13 @@ use tracing::error; pub mod message; use crate::{ - bitcoin::{BlockHeight, GetRawTransaction, Network, TransactionBlockHeight}, + bitcoin::{ + current_epoch, wait_for_t1, BlockHeight, GetRawTransaction, Network, TransactionBlockHeight, + }, monero::{CreateWalletForOutput, WatchForTransfer}, }; use ::bitcoin::{Transaction, Txid}; pub use message::{Message, Message0, Message1, Message2, Message3}; -use crate::bitcoin::current_epoch; #[allow(clippy::large_enum_variant)] #[derive(Debug)] @@ -622,6 +623,13 @@ impl State3 { }) } + pub async fn wait_for_t1(&self, bitcoin_wallet: &W) -> Result<()> + where + W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, + { + wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await + } + pub fn t1_expired(&self) -> State4 { State4 { A: self.A, @@ -783,13 +791,7 @@ impl State4 { where W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, { - let tx_id = self.tx_lock.txid(); - let tx_lock_height = bitcoin_wallet.transaction_block_height(tx_id).await; - - let t1_timeout = - poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + self.refund_timelock); - t1_timeout.await; - Ok(()) + wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await } pub async fn current_epoch(&self, bitcoin_wallet: &W) -> Result