From 70220d15812141a52e85944cf305c55f7e238ce1 Mon Sep 17 00:00:00 2001 From: patrini32 <171664803+patrini32@users.noreply.github.com> Date: Wed, 5 Jun 2024 18:07:43 +0300 Subject: [PATCH] Add comments about logic --- swap/src/cli/cancel_and_refund.rs | 19 ++++++++++--------- swap/src/protocol/bob/state.rs | 2 +- ...punishes_after_bob_dead_and_bob_cancels.rs | 6 +++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/swap/src/cli/cancel_and_refund.rs b/swap/src/cli/cancel_and_refund.rs index 9ac26d97..630fb9db 100644 --- a/swap/src/cli/cancel_and_refund.rs +++ b/swap/src/cli/cancel_and_refund.rs @@ -19,7 +19,7 @@ pub async fn cancel_and_refund( Ok(s) => s, Err(e) => bail!(e), }; - if matches!(state, BobState::BtcRefunded { .. }) { + if matches!(state, BobState::BtcRefunded { .. }) { //Print success message only if refund succeeded. tracing::info!("Refund transaction submitted"); } Ok(state) @@ -65,17 +65,17 @@ pub async fn cancel( Err(err) => { if let Ok(error_code) = parse_rpc_error_code(&err) { if error_code == i64::from(RpcErrorCode::RpcVerifyError) { - if let ExpiredTimelocks::None { .. } = - state6.expired_timelock(bitcoin_wallet.as_ref()).await? + if let ExpiredTimelocks::None { .. } = // Check if timelock haven't expired. + state6.expired_timelock(bitcoin_wallet.as_ref()).await? { tracing::debug!(%error_code, "parse rpc error"); tracing::info!("General error trying to submit cancel transaction"); - } else { + } else { // Timelock expired and network rejected tx_cancel, so we are out-of-sync with Alice. (I assume that there's no other states under these conditions, am I right?) let txid = state6 - .construct_tx_cancel() + .construct_tx_cancel() // Construct tx_cancel without broadcasting to the network, because swap has already been cancelled by Alice. .expect("Error when constructing tx_cancel") .txid(); - let state = BobState::BtcCancelled(state6); + 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."); @@ -121,14 +121,14 @@ pub async fn refund( Err(error) => { if let Ok(error_code) = parse_rpc_error_code(&error) { if error_code == i64::from(RpcErrorCode::RpcVerifyError) { - if let ExpiredTimelocks::None { .. } = + if let ExpiredTimelocks::None { .. } = // Check if timelock haven't expired. state6.expired_timelock(bitcoin_wallet.as_ref()).await? { bail!(error); - } else { + } else { // Timelock expired and network rejected refund, so we are out-of-sync with Alice. (I assume that there's no other states under these conditions, am I right?) let state = BobState::BtcPunished { tx_lock_id: state6.tx_lock_id(), - }; + }; // Set state to punished. db.insert_latest_state(swap_id, state.clone().into()) .await?; @@ -137,6 +137,7 @@ pub async fn refund( } } } + bail!(error); } } let state = BobState::BtcRefunded(state6); diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index a1fd750a..20c177fe 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -663,7 +663,7 @@ impl State6 { Ok(tx) } - pub fn construct_tx_cancel(&self) -> Result { + pub fn construct_tx_cancel(&self) -> Result { // Just construct the tx_cancel without broadcasting. bitcoin::TxCancel::new( &self.tx_lock, self.cancel_timelock, diff --git a/swap/tests/alice_manually_punishes_after_bob_dead_and_bob_cancels.rs b/swap/tests/alice_manually_punishes_after_bob_dead_and_bob_cancels.rs index 739df338..f1e8ad1d 100644 --- a/swap/tests/alice_manually_punishes_after_bob_dead_and_bob_cancels.rs +++ b/swap/tests/alice_manually_punishes_after_bob_dead_and_bob_cancels.rs @@ -73,10 +73,10 @@ async fn alice_manually_punishes_after_bob_dead_and_bob_cancels() { let (bob_swap, _) = ctx .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) .await; - assert!(matches!(bob_swap.state, BobState::BtcLocked { .. })); + assert!(matches!(bob_swap.state, BobState::BtcLocked { .. })); // Bob is out of sync with Alice. let state = - cli::cancel_and_refund(bob_swap_id, bob_swap.bitcoin_wallet, bob_swap.db).await?; - ctx.assert_bob_punished(state).await; + cli::cancel_and_refund(bob_swap_id, bob_swap.bitcoin_wallet, bob_swap.db).await?; // Bob tries to cancel and refund. + ctx.assert_bob_punished(state).await; // Bob should be in punished state now. (This PR adds that behaviour.) Ok(()) }) .await;