From 1b2f476cae70a69b08336dbdd5f97ae54c744ccd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 1 Apr 2021 17:28:38 +1100 Subject: [PATCH] Have `--force` flag only override the timelock check It might very well be that the cancel transaction is already published. If that is the case, there is no point in failing the command. We simply transition to cancel and exit normally. The reason this comes up now is because Alice now properly waits for the cancel timelock as well and publishes the cancel transaction first. Ultimately, she should not do that because there is no benefit to her unless she can also publish the punish transaction. --- swap/src/bin/swap.rs | 3 --- swap/src/protocol/bob/cancel.rs | 24 +++++++----------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index ec731880..479d2b21 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -239,9 +239,6 @@ async fn main() -> Result<()> { "The Cancel Transaction cannot be published yet, \ because the timelock has not expired. Please try again later." ), - Err(bob::cancel::Error::CancelTxAlreadyPublished) => { - warn!("The Cancel Transaction has already been published.") - } } } Command::Refund { diff --git a/swap/src/protocol/bob/cancel.rs b/swap/src/protocol/bob/cancel.rs index 42c08ecf..ee764e79 100644 --- a/swap/src/protocol/bob/cancel.rs +++ b/swap/src/protocol/bob/cancel.rs @@ -9,8 +9,6 @@ use uuid::Uuid; pub enum Error { #[error("The cancel timelock has not expired yet.")] CancelTimelockNotExpiredYet, - #[error("The cancel transaction has already been published.")] - CancelTxAlreadyPublished, } pub async fn cancel( @@ -48,23 +46,15 @@ pub async fn cancel( if let ExpiredTimelocks::None = state6.expired_timelock(bitcoin_wallet.as_ref()).await? { return Ok(Err(Error::CancelTimelockNotExpiredYet)); } - - if state6 - .check_for_tx_cancel(bitcoin_wallet.as_ref()) - .await - .is_ok() - { - tracing::debug!(%swap_id, "Cancel transaction has already been published"); - - let state = BobState::BtcCancelled(state6); - let db_state = state.into(); - db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?; - - return Ok(Err(Error::CancelTxAlreadyPublished)); - } } - let txid = state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await?; + let txid = if let Ok(tx) = state6.check_for_tx_cancel(bitcoin_wallet.as_ref()).await { + tracing::debug!(%swap_id, "Cancel transaction has already been published"); + + tx.txid() + } else { + state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await? + }; let state = BobState::BtcCancelled(state6); let db_state = state.clone().into();