From 8866e60323173190f70e572366dd626703301d7f Mon Sep 17 00:00:00 2001 From: patrini32 <171664803+patrini32@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:37:55 +0300 Subject: [PATCH] Use transaction created by check_for_tx_cancel and delete construct_tx_cancel --- swap/src/cli/cancel_and_refund.rs | 13 ++----------- swap/src/protocol/bob/state.rs | 17 +++++++---------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/swap/src/cli/cancel_and_refund.rs b/swap/src/cli/cancel_and_refund.rs index f21a765f..b6095664 100644 --- a/swap/src/cli/cancel_and_refund.rs +++ b/swap/src/cli/cancel_and_refund.rs @@ -65,22 +65,13 @@ pub async fn cancel( Ok((txid, state)) } Err(err) => { - if state6 - .check_for_tx_cancel(bitcoin_wallet.as_ref()) - .await - .is_ok() - { + if let Ok(tx) = state6.check_for_tx_cancel(bitcoin_wallet.as_ref()).await { // Alice already cancelled, so we are out-of-sync with Alice. - let txid = state6 - // Construct tx_cancel without broadcasting to the network, because swap has already been cancelled by Alice. - .construct_tx_cancel() - .expect("Error when constructing tx_cancel") - .txid(); let state = BobState::BtcCancelled(state6); // Set state to cancelled to sync with Alice. db.insert_latest_state(swap_id, state.clone().into()) .await?; tracing::info!("Cancel transaction has already been confirmed on chain. The swap has therefore already been cancelled by Alice."); - return Ok((txid, state)); + return Ok((tx.txid(), state)); } if let Ok(error_code) = parse_rpc_error_code(&err) { tracing::debug!(%error_code, "parse rpc error"); diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index d5152d35..38871dd0 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -701,9 +701,12 @@ impl State6 { let tx = bitcoin_wallet.get_raw_transaction(tx_punish.txid()).await?; Ok(tx) } - pub fn construct_tx_cancel(&self) -> Result { - // Just construct the tx_cancel without broadcasting. - bitcoin::TxCancel::new( + + pub async fn submit_tx_cancel( + &self, + bitcoin_wallet: &bitcoin::Wallet, + ) -> Result<(Txid, Subscription)> { + let transaction = bitcoin::TxCancel::new( &self.tx_lock, self.cancel_timelock, self.A, @@ -711,13 +714,7 @@ impl State6 { self.tx_cancel_fee, )? .complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone()) - .context("Failed to complete Bitcoin cancel transaction") - } - pub async fn submit_tx_cancel( - &self, - bitcoin_wallet: &bitcoin::Wallet, - ) -> Result<(Txid, Subscription)> { - let transaction = self.construct_tx_cancel()?; + .context("Failed to complete Bitcoin cancel transaction")?; let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;