Take bitcoin_tx_lock_timeout as argument to action generators

pull/22/head
Lucas Soriano del Pino 4 years ago
parent 1d21ae7e7a
commit cea1af1e1a

@ -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<N, B>(
mut network: N,
bitcoin_client: Arc<B>,
@ -80,6 +83,7 @@ pub fn action_generator<N, B>(
tx_cancel_sig_bob,
..
}: State3,
bitcoin_tx_lock_timeout: u64,
) -> GenBoxed<Action, (), ()>
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

@ -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<N, M, B>(
mut network: N,
monero_client: Arc<M>,
@ -84,6 +82,7 @@ pub fn action_generator<N, M, B>(
tx_refund_encsig,
..
}: State2,
bitcoin_tx_lock_timeout: u64,
) -> GenBoxed<Action, (), ()>
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

@ -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<EncryptedSignature>;
type BobNetwork = Network<TransferProof>;
@ -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 {

Loading…
Cancel
Save