From dd40f2cf6255a74d900cd205ef9f1e8eb2455103 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Tue, 4 Jun 2024 10:37:23 +0200 Subject: [PATCH] tests: Add integration test for happy path if Bob goes offline while Alice redeems --- ...ath_bob_offline_while_alice_redeems_btc.rs | 45 +++++++++++++++++++ swap/tests/harness/mod.rs | 4 ++ 2 files changed, 49 insertions(+) create mode 100644 swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs diff --git a/swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs b/swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs new file mode 100644 index 00000000..32882d75 --- /dev/null +++ b/swap/tests/happy_path_bob_offline_while_alice_redeems_btc.rs @@ -0,0 +1,45 @@ +pub mod harness; + +use tokio::join; +use swap::asb::FixedRate; +use swap::protocol::{alice, bob}; +use swap::protocol::bob::BobState; +use crate::harness::bob_run_until::{is_encsig_sent}; + +#[tokio::test] +async fn given_bob_restarts_while_alice_redeems_btc() { + harness::setup_test(harness::SlowCancelConfig, |mut ctx| async move { + let (bob_swap, bob_handle) = ctx.bob_swap().await; + let swap_id = bob_swap.id; + + let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_encsig_sent)); + + let alice_swap = ctx.alice_next_swap().await; + let alice_swap = tokio::spawn(alice::run(alice_swap, FixedRate::default())); + + let (bob_state, alice_state) = join!(bob_swap, alice_swap); + ctx.assert_alice_redeemed(alice_state??).await; + assert!(matches!(bob_state??, BobState::EncSigSent { .. })); + + + let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_handle, swap_id).await; + + if let BobState::EncSigSent(state4) = bob_swap.state.clone() { + bob_swap + .bitcoin_wallet + .subscribe_to(state4.tx_lock) + .await + .wait_until_confirmed_with(state4.cancel_timelock) + .await?; + } else { + panic!("Bob in unexpected state {}", bob_swap.state); + } + + // Restart Bob + let bob_state = bob::run(bob_swap).await?; + ctx.assert_bob_redeemed(bob_state).await; + + Ok(()) + }) + .await; +} \ No newline at end of file diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index c099376d..025b7e42 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -996,6 +996,10 @@ pub mod alice_run_until { pub fn is_encsig_learned(state: &AliceState) -> bool { matches!(state, AliceState::EncSigLearned { .. }) } + + pub fn is_btc_redeemed(state: &AliceState) -> bool { + matches!(state, AliceState::BtcRedeemed { .. }) + } } pub mod bob_run_until {