From cea1af1e1aa01dfe1b5421eb24c1ed2a28425bda Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Mon, 26 Oct 2020 12:03:14 +1100 Subject: [PATCH] Take bitcoin_tx_lock_timeout as argument to action generators --- xmr-btc/src/alice.rs | 6 +++++- xmr-btc/src/bob.rs | 11 +++++------ xmr-btc/tests/on_chain.rs | 11 ++++++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index 06d0bca9..95e12ed6 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -58,6 +58,9 @@ pub trait ReceiveBitcoinRedeemEncsig { /// /// This is called post handshake, after all the keys, addresses and most of the /// signatures have been exchanged. +/// +/// The argument `bitcoin_tx_lock_timeout` is used to determine how long we will +/// wait for Bob, the counterparty, to lock up the bitcoin. pub fn action_generator( mut network: N, bitcoin_client: Arc, @@ -80,6 +83,7 @@ pub fn action_generator( tx_cancel_sig_bob, .. }: State3, + bitcoin_tx_lock_timeout: u64, ) -> GenBoxed where N: ReceiveBitcoinRedeemEncsig + Send + Sync + 'static, @@ -124,7 +128,7 @@ where Gen::new_boxed(|co| async move { let swap_result: Result<(), SwapFailed> = async { timeout( - Duration::from_secs(bob::SECS_TO_ACT), + Duration::from_secs(bitcoin_tx_lock_timeout), bitcoin_client.watch_for_raw_transaction(tx_lock.txid()), ) .await diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index 9b6efb36..ac1180c9 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -35,11 +35,6 @@ pub mod message; use crate::monero::{CreateWalletForOutput, WatchForTransfer}; pub use message::{Message, Message0, Message1, Message2, Message3}; -// TODO: Replace this with something configurable, such as an function argument. -/// Time that Bob has to publish the Bitcoin lock transaction before both -/// parties will abort, in seconds. -pub const SECS_TO_ACT: u64 = 60; - #[allow(clippy::large_enum_variant)] #[derive(Debug)] pub enum Action { @@ -63,6 +58,9 @@ pub trait ReceiveTransferProof { /// /// This is called post handshake, after all the keys, addresses and most of the /// signatures have been exchanged. +/// +/// The argument `bitcoin_tx_lock_timeout` is used to determine how long we will +/// wait for Bob, the caller of this function, to lock up the bitcoin. pub fn action_generator( mut network: N, monero_client: Arc, @@ -84,6 +82,7 @@ pub fn action_generator( tx_refund_encsig, .. }: State2, + bitcoin_tx_lock_timeout: u64, ) -> GenBoxed where N: ReceiveTransferProof + Send + Sync + 'static, @@ -124,7 +123,7 @@ where co.yield_(Action::LockBtc(tx_lock.clone())).await; timeout( - Duration::from_secs(SECS_TO_ACT), + Duration::from_secs(bitcoin_tx_lock_timeout), bitcoin_client.watch_for_raw_transaction(tx_lock.txid()), ) .await diff --git a/xmr-btc/tests/on_chain.rs b/xmr-btc/tests/on_chain.rs index 6ef6e28c..8cac4c57 100644 --- a/xmr-btc/tests/on_chain.rs +++ b/xmr-btc/tests/on_chain.rs @@ -25,6 +25,9 @@ use xmr_btc::{ monero::{CreateWalletForOutput, Transfer, TransferProof}, }; +/// Time given to Bob to get the Bitcoin lock transaction included in a block. +const BITCOIN_TX_LOCK_TIMEOUT: u64 = 5; + type AliceNetwork = Network; type BobNetwork = Network; @@ -87,7 +90,12 @@ async fn swap_as_alice( behaviour: AliceBehaviour, state: alice::State3, ) -> Result<()> { - let mut action_generator = alice::action_generator(network, bitcoin_wallet.clone(), state); + let mut action_generator = alice::action_generator( + network, + bitcoin_wallet.clone(), + state, + BITCOIN_TX_LOCK_TIMEOUT, + ); loop { let state = action_generator.async_resume().await; @@ -150,6 +158,7 @@ async fn swap_as_bob( monero_wallet.clone(), bitcoin_wallet.clone(), state, + BITCOIN_TX_LOCK_TIMEOUT, ); loop {