From 8c9285f1f9ad135830166170ab1970b4a4747efe Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 18 Mar 2021 12:58:37 +1100 Subject: [PATCH] Inline step function because it has been reduced to a single statement --- swap/src/protocol/alice/steps.rs | 37 ++------------------------------ swap/src/protocol/alice/swap.rs | 37 +++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 45 deletions(-) diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 3d46dde0..914dc0c6 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -1,39 +1,6 @@ use crate::bitcoin; -use crate::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxLock, TxRefund}; -use anyhow::{bail, Context, Result}; - -pub async fn publish_cancel_transaction( - tx_lock: TxLock, - a: bitcoin::SecretKey, - B: bitcoin::PublicKey, - cancel_timelock: CancelTimelock, - tx_cancel_sig_bob: bitcoin::Signature, - bitcoin_wallet: &bitcoin::Wallet, -) -> Result<()> { - let tx_cancel = bitcoin::TxCancel::new(&tx_lock, cancel_timelock, a.public(), B); - - // If Bob hasn't yet broadcasted the tx cancel, we do it - if bitcoin_wallet - .get_raw_transaction(tx_cancel.txid()) - .await - .is_err() - { - // TODO(Franck): Maybe the cancel transaction is already mined, in this case, - // the broadcast will error out. - - let transaction = tx_cancel - .complete_as_alice(a, B, tx_cancel_sig_bob) - .context("Failed to complete Bitcoin cancel transaction")?; - - // TODO(Franck): Error handling is delicate, why can't we broadcast? - let (..) = bitcoin_wallet.broadcast(transaction, "cancel").await?; - - // TODO(Franck): Wait until transaction is mined and returned mined - // block height - } - - Ok(()) -} +use crate::bitcoin::{PunishTimelock, TxCancel, TxRefund}; +use anyhow::{bail, Result}; pub async fn wait_for_bitcoin_refund( tx_cancel: &TxCancel, diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 52c6ae92..218ae8cf 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -6,7 +6,7 @@ use crate::env::Config; use crate::monero_ext::ScalarExt; use crate::protocol::alice; use crate::protocol::alice::event_loop::EventLoopHandle; -use crate::protocol::alice::steps::{publish_cancel_transaction, wait_for_bitcoin_refund}; +use crate::protocol::alice::steps::wait_for_bitcoin_refund; use crate::protocol::alice::AliceState; use crate::{bitcoin, database, monero}; use anyhow::{bail, Context, Result}; @@ -259,15 +259,32 @@ async fn run_until_internal( state3, monero_wallet_restore_blockheight, } => { - publish_cancel_transaction( - state3.tx_lock.clone(), - state3.a.clone(), - state3.B, - state3.cancel_timelock, - state3.tx_cancel_sig_bob.clone(), - &bitcoin_wallet, - ) - .await?; + let tx_cancel = state3.tx_cancel(); + + // If Bob hasn't yet broadcasted the tx cancel, we do it + if bitcoin_wallet + .get_raw_transaction(tx_cancel.txid()) + .await + .is_err() + { + let transaction = tx_cancel + .complete_as_alice( + state3.a.clone(), + state3.B, + state3.tx_cancel_sig_bob.clone(), + ) + .context("Failed to complete Bitcoin cancel transaction")?; + + if let Err(e) = bitcoin_wallet.broadcast(transaction, "cancel").await { + tracing::debug!( + "Assuming transaction is already broadcasted because: {:#}", + e + ) + } + + // TODO(Franck): Wait until transaction is mined and + // returned mined block height + } let state = AliceState::BtcCancelled { state3,