diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index eb5edd8d..4a017b65 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -4,7 +4,7 @@ use rand::{CryptoRng, RngCore}; use crate::{ bitcoin, - bitcoin::{BroadcastSignedTransaction, GetRawTransaction}, + bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction}, bob, monero, monero::{ImportOutput, Transfer}, transport::SendReceive, @@ -18,7 +18,7 @@ pub use message::{Message, Message0, Message1, Message2, UnexpectedMessage}; pub async fn next_state< R: RngCore + CryptoRng, - B: GetRawTransaction + BroadcastSignedTransaction, + B: WatchForRawTransaction + BroadcastSignedTransaction, M: ImportOutput + Transfer, T: SendReceive, >( @@ -48,7 +48,6 @@ pub async fn next_state< State::State2(state2) => { let bob_message2: bob::Message2 = transport.receive_message().await?.try_into()?; let state3 = state2.receive(bob_message2)?; - tokio::time::delay_for(std::time::Duration::new(5, 0)).await; Ok(state3.into()) } State::State3(state3) => { @@ -394,11 +393,11 @@ pub struct State3 { impl State3 { pub async fn watch_for_lock_btc(self, bitcoin_wallet: &W) -> Result where - W: bitcoin::GetRawTransaction, + W: bitcoin::WatchForRawTransaction, { tracing::info!("{}", self.tx_lock.txid()); let tx = bitcoin_wallet - .get_raw_transaction(self.tx_lock.txid()) + .watch_for_raw_transaction(self.tx_lock.txid()) .await?; tracing::info!("{}", tx.txid()); @@ -581,7 +580,7 @@ impl State5 { // watch for refund on btc, recover s_b and refund xmr pub async fn refund_xmr(self, bitcoin_wallet: &B, monero_wallet: &M) -> Result<()> where - B: GetRawTransaction, + B: WatchForRawTransaction, M: ImportOutput, { let tx_cancel = bitcoin::TxCancel::new( @@ -595,7 +594,9 @@ impl State5 { let tx_refund_encsig = self.a.encsign(self.S_b_bitcoin.clone(), tx_refund.digest()); - let tx_refund_candidate = bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?; + let tx_refund_candidate = bitcoin_wallet + .watch_for_raw_transaction(tx_refund.txid()) + .await?; let tx_refund_sig = tx_refund.extract_signature_by_key(tx_refund_candidate, self.a.public())?; diff --git a/xmr-btc/src/bitcoin.rs b/xmr-btc/src/bitcoin.rs index e903bb75..1a6dec22 100644 --- a/xmr-btc/src/bitcoin.rs +++ b/xmr-btc/src/bitcoin.rs @@ -188,8 +188,8 @@ pub trait BroadcastSignedTransaction { } #[async_trait] -pub trait GetRawTransaction { - async fn get_raw_transaction(&self, txid: Txid) -> Result; +pub trait WatchForRawTransaction { + async fn watch_for_raw_transaction(&self, txid: Txid) -> Result; } pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result { diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index d8bdd18d..b9109e09 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -1,7 +1,8 @@ use crate::{ alice, bitcoin::{ - self, BroadcastSignedTransaction, BuildTxLockPsbt, GetRawTransaction, SignTxLock, TxCancel, + self, BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, TxCancel, + WatchForRawTransaction, }, monero, monero::{CheckTransfer, ImportOutput}, @@ -22,7 +23,7 @@ pub use message::{Message, Message0, Message1, Message2, Message3, UnexpectedMes pub async fn next_state< R: RngCore + CryptoRng, - B: GetRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction, + B: WatchForRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction, M: ImportOutput + CheckTransfer, T: SendReceive, >( @@ -500,12 +501,14 @@ impl State4 { pub async fn watch_for_redeem_btc(self, bitcoin_wallet: &W) -> Result where - W: GetRawTransaction, + W: WatchForRawTransaction, { let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address); let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin.clone(), tx_redeem.digest()); - let tx_redeem_candidate = bitcoin_wallet.get_raw_transaction(tx_redeem.txid()).await?; + let tx_redeem_candidate = bitcoin_wallet + .watch_for_raw_transaction(tx_redeem.txid()) + .await?; let tx_redeem_sig = tx_redeem.extract_signature_by_key(tx_redeem_candidate, self.b.public())?; diff --git a/xmr-btc/tests/wallet/bitcoin.rs b/xmr-btc/tests/wallet/bitcoin.rs index 57d936b0..524baaa4 100644 --- a/xmr-btc/tests/wallet/bitcoin.rs +++ b/xmr-btc/tests/wallet/bitcoin.rs @@ -6,7 +6,7 @@ use reqwest::Url; use std::time::Duration; use tokio::time; use xmr_btc::bitcoin::{ - BroadcastSignedTransaction, BuildTxLockPsbt, GetRawTransaction, SignTxLock, TxLock, + BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, TxLock, WatchForRawTransaction, }; #[derive(Debug)] @@ -107,13 +107,13 @@ impl BroadcastSignedTransaction for Wallet { } #[async_trait] -impl GetRawTransaction for Wallet { - async fn get_raw_transaction(&self, txid: Txid) -> Result { - // TODO: put into loop instead of delaying - time::delay_for(Duration::from_millis(5000)).await; - let tx = self.0.get_raw_transaction(txid).await?; - tracing::info!("{}", tx.txid()); - - Ok(tx) +impl WatchForRawTransaction for Wallet { + async fn watch_for_raw_transaction(&self, txid: Txid) -> Result { + loop { + if let Ok(tx) = self.0.get_raw_transaction(txid).await { + return Ok(tx); + } + time::delay_for(Duration::from_millis(200)).await; + } } }