|
| 1 | +pub mod harness; |
| 2 | + |
| 3 | +use harness::alice_run_until::is_xmr_lock_transaction_sent; |
| 4 | +use harness::bob_run_until::is_xmr_locked; |
| 5 | +use harness::SlowCancelConfig; |
| 6 | +use swap::asb::FixedRate; |
| 7 | +use swap::protocol::alice::AliceState; |
| 8 | +use swap::protocol::bob::BobState; |
| 9 | +use swap::protocol::{alice, bob}; |
| 10 | + |
| 11 | +/// Test that Bob can detect the Monero lock transaction using the view key |
| 12 | +/// even when Alice goes offline and doesn't send the transfer proof. |
| 13 | +#[tokio::test] |
| 14 | +async fn given_alice_goes_offline_after_xmr_locked_bob_detects_xmr_via_view_key() { |
| 15 | + harness::setup_test(SlowCancelConfig, None, |mut ctx| async move { |
| 16 | + // Bob runs until he detects the Monero as having been locked |
| 17 | + let (bob_swap, bob_join_handle) = ctx.bob_swap().await; |
| 18 | + let bob_swap_id = bob_swap.id; |
| 19 | + let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_xmr_locked)); |
| 20 | + |
| 21 | + // Alice runs until the locks the Monero but before she sends the transfer proof |
| 22 | + let alice_swap = ctx.alice_next_swap().await; |
| 23 | + let alice_swap = tokio::spawn(alice::run_until( |
| 24 | + alice_swap, |
| 25 | + is_xmr_lock_transaction_sent, |
| 26 | + FixedRate::default(), |
| 27 | + )); |
| 28 | + |
| 29 | + // Assert that Alice is in correct state |
| 30 | + let alice_state = alice_swap.await??; |
| 31 | + assert!(matches!( |
| 32 | + alice_state, |
| 33 | + AliceState::XmrLockTransactionSent { .. } |
| 34 | + )); |
| 35 | + |
| 36 | + // Assert that Bob is in correct state |
| 37 | + let bob_state = bob_swap.await??; |
| 38 | + assert!( |
| 39 | + matches!(bob_state, BobState::XmrLocked(..)), |
| 40 | + "Bob should have detected XMR lock via view key, but state is: {:?}", |
| 41 | + bob_state |
| 42 | + ); |
| 43 | + |
| 44 | + // Resume the swap |
| 45 | + ctx.restart_alice().await; |
| 46 | + let alice_swap = ctx.alice_next_swap().await; |
| 47 | + let alice_swap = tokio::spawn(alice::run(alice_swap, FixedRate::default())); |
| 48 | + |
| 49 | + let (bob_swap, _) = ctx |
| 50 | + .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) |
| 51 | + .await; |
| 52 | + |
| 53 | + // Run the swap to completion |
| 54 | + let bob_state = bob::run(bob_swap).await?; |
| 55 | + ctx.assert_bob_redeemed(bob_state).await; |
| 56 | + |
| 57 | + let alice_state = alice_swap.await??; |
| 58 | + ctx.assert_alice_redeemed(alice_state).await; |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + }) |
| 62 | + .await; |
| 63 | +} |
0 commit comments