From c9adbde5d570b82dc18ee2b8a5f55db7f08ebfa1 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 1 Feb 2021 18:13:08 +1100 Subject: [PATCH] Add test for Bob's manual cancel and refund --- .github/workflows/ci.yml | 1 + ...refunds_using_cancel_and_refund_command.rs | 66 +++++++++++++++++++ .../happy_path_restart_bob_after_comm.rs | 2 +- ...h_restart_bob_after_lock_proof_received.rs | 2 +- .../happy_path_restart_bob_before_comm.rs | 2 +- swap/tests/punish.rs | 2 +- swap/tests/testutils/mod.rs | 6 +- 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 swap/tests/bob_refunds_using_cancel_and_refund_command.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af1d0e69..ebe60e3e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -134,6 +134,7 @@ jobs: punish, refund_restart_alice_cancelled, refund_restart_alice, + bob_refunds_using_cancel_and_refund_command, ] runs-on: ubuntu-latest steps: diff --git a/swap/tests/bob_refunds_using_cancel_and_refund_command.rs b/swap/tests/bob_refunds_using_cancel_and_refund_command.rs new file mode 100644 index 00000000..04b33df1 --- /dev/null +++ b/swap/tests/bob_refunds_using_cancel_and_refund_command.rs @@ -0,0 +1,66 @@ +pub mod testutils; + +use swap::protocol::{alice, bob, bob::BobState}; +use testutils::{bob_run_until::is_btc_locked, FastCancelConfig}; + +#[tokio::test] +async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() { + testutils::setup_test(FastCancelConfig, |mut ctx| async move { + let (alice_swap, _) = ctx.new_swap_as_alice().await; + let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; + + let alice_handle = alice::run(alice_swap); + tokio::spawn(alice_handle); + + let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap(); + + assert!(matches!(bob_state, BobState::BtcLocked { .. })); + + let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + + // Ensure Bob's timelock is expired + if let BobState::BtcLocked(state3) = bob_swap.state.clone() { + state3 + .wait_for_cancel_timelock_to_expire(bob_swap.bitcoin_wallet.as_ref()) + .await + .unwrap(); + } else { + panic!("Bob in unexpected state {}", bob_swap.state); + } + + // Bob manually cancels + let (_, state) = bob::cancel( + bob_swap.swap_id, + bob_swap.state, + bob_swap.bitcoin_wallet, + bob_swap.db, + ) + .await + .unwrap() + .unwrap(); + assert!(matches!(state, BobState::BtcCancelled { .. })); + + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + assert!(matches!(bob_swap.state, BobState::BtcCancelled { .. })); + + // Bob manually refunds + let bob_state = bob::refund( + bob_swap.swap_id, + bob_swap.state, + bob_swap.execution_params, + bob_swap.bitcoin_wallet, + bob_swap.db, + ) + .await + .unwrap() + .unwrap(); + + ctx.assert_bob_refunded(bob_state).await; + + // TODO: Alice hangs indefinitely waiting for Blob's acknowledge on + // sending the transfer proof + // let alice_state = alice_swap_handle.await.unwrap().unwrap(); + // ctx.assert_alice_refunded(alice_state).await; + }) + .await; +} diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 87a3b861..6cd08c64 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -16,7 +16,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { assert!(matches!(bob_state, BobState::EncSigSent { .. })); - let bob_swap = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; assert!(matches!(bob_swap.state, BobState::EncSigSent { .. })); let bob_state = bob::run(bob_swap).await.unwrap(); diff --git a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs index 7101c0ab..d335acc9 100644 --- a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs +++ b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs @@ -18,7 +18,7 @@ async fn given_bob_restarts_after_lock_proof_received_resume_swap() { assert!(matches!(bob_state, BobState::XmrLockProofReceived { .. })); - let bob_swap = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; assert!(matches!( bob_swap.state, BobState::XmrLockProofReceived { .. } diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index fd5f8979..7c72adf9 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -16,7 +16,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { assert!(matches!(bob_state, BobState::XmrLocked { .. })); - let bob_swap = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; assert!(matches!(bob_swap.state, BobState::XmrLocked { .. })); let bob_state = bob::run(bob_swap).await.unwrap(); diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 61de0577..ea29a95a 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -23,7 +23,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { // Restart Bob after Alice punished to ensure Bob transitions to // punished and does not run indefinitely - let bob_swap = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await; assert!(matches!(bob_swap.state, BobState::BtcLocked { .. })); let bob_state = bob::run(bob_swap).await.unwrap(); diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 847af388..91a7949f 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -145,14 +145,14 @@ impl TestContext { pub async fn stop_and_resume_bob_from_db( &mut self, join_handle: BobEventLoopJoinHandle, - ) -> bob::Swap { + ) -> (bob::Swap, BobEventLoopJoinHandle) { join_handle.0.abort(); let (swap, event_loop) = self.bob_params.builder().build().await.unwrap(); - tokio::spawn(async move { event_loop.run().await }); + let join_handle = tokio::spawn(async move { event_loop.run().await }); - swap + (swap, BobEventLoopJoinHandle(join_handle)) } pub async fn assert_alice_redeemed(&self, state: AliceState) {