From d85c0ce57ccf136583a4b7e89a4d8d96e9546a7b Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Tue, 16 Mar 2021 17:18:57 +1100 Subject: [PATCH] Re-introduce punish test --- .github/workflows/ci.yml | 1 + bors.toml | 3 ++- swap/tests/punish.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 swap/tests/punish.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 85b798f1..517febb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,6 +118,7 @@ jobs: bob_refunds_using_cancel_and_refund_command, bob_refunds_using_cancel_and_refund_command_timelock_not_expired, bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force, + punish ] runs-on: ubuntu-latest steps: diff --git a/bors.toml b/bors.toml index ea398644..9375744a 100644 --- a/bors.toml +++ b/bors.toml @@ -10,5 +10,6 @@ status = [ "docker_tests (happy_path_restart_bob_before_comm)", "docker_tests (bob_refunds_using_cancel_and_refund_command)", "docker_tests (bob_refunds_using_cancel_and_refund_command_timelock_not_expired_force)", - "docker_tests (bob_refunds_using_cancel_and_refund_command_timelock_not_expired)" + "docker_tests (bob_refunds_using_cancel_and_refund_command_timelock_not_expired)", + "docker_tests (punish)" ] diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs new file mode 100644 index 00000000..758f8e98 --- /dev/null +++ b/swap/tests/punish.rs @@ -0,0 +1,37 @@ +pub mod testutils; + +use swap::protocol::bob::BobState; +use swap::protocol::{alice, bob}; +use testutils::bob_run_until::is_btc_locked; +use testutils::FastPunishConfig; + +/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice +/// the encsig and fail to refund or redeem. Alice punishes. +#[tokio::test] +async fn alice_punishes_if_bob_never_acts_after_fund() { + testutils::setup_test(FastPunishConfig, |mut ctx| async move { + let (bob_swap, bob_join_handle) = ctx.bob_swap().await; + let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked)); + + let alice_swap = ctx.alice_next_swap().await; + let alice_swap = tokio::spawn(alice::run(alice_swap)); + + let bob_state = bob_swap.await??; + assert!(matches!(bob_state, BobState::BtcLocked { .. })); + + let alice_state = alice_swap.await??; + ctx.assert_alice_punished(alice_state).await; + + // 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; + assert!(matches!(bob_swap.state, BobState::BtcLocked { .. })); + + let bob_state = bob::run(bob_swap).await?; + + ctx.assert_bob_punished(bob_state).await; + + Ok(()) + }) + .await; +}