2021-01-17 20:50:49 -05:00
|
|
|
use swap::protocol::{alice, alice::AliceState, bob};
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
pub mod testutils;
|
|
|
|
|
2021-01-17 20:50:49 -05:00
|
|
|
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
|
|
|
|
/// then also refunds.
|
2020-12-14 01:24:23 -05:00
|
|
|
#[tokio::test]
|
2020-12-17 19:46:00 -05:00
|
|
|
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
2021-01-18 23:03:30 -05:00
|
|
|
testutils::setup_test(|mut ctx| async move {
|
2021-01-19 18:40:40 -05:00
|
|
|
let alice_swap = ctx.new_swap_as_alice().await;
|
|
|
|
let bob_swap = ctx.new_swap_as_bob().await;
|
2021-01-17 20:50:49 -05:00
|
|
|
|
2021-01-18 03:56:43 -05:00
|
|
|
let bob = bob::run(bob_swap);
|
|
|
|
let bob_handle = tokio::spawn(bob);
|
2021-01-17 20:50:49 -05:00
|
|
|
|
2021-01-18 03:56:43 -05:00
|
|
|
let alice_state = alice::run_until(alice_swap, alice::swap::is_xmr_locked)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-01-17 20:50:49 -05:00
|
|
|
assert!(matches!(alice_state, AliceState::XmrLocked {..}));
|
|
|
|
|
|
|
|
// Alice does not act, Bob refunds
|
2021-01-18 03:56:43 -05:00
|
|
|
let bob_state = bob_handle.await.unwrap();
|
2021-01-17 20:50:49 -05:00
|
|
|
|
|
|
|
// Once bob has finished Alice is restarted and refunds as well
|
2021-01-19 18:40:40 -05:00
|
|
|
let alice_swap = ctx.recover_alice_from_db().await;
|
2021-01-18 03:56:43 -05:00
|
|
|
assert!(matches!(alice_swap.state, AliceState::XmrLocked {..}));
|
2021-01-17 20:50:49 -05:00
|
|
|
|
2021-01-18 03:56:43 -05:00
|
|
|
let alice_state = alice::run(alice_swap).await.unwrap();
|
2021-01-17 20:50:49 -05:00
|
|
|
|
|
|
|
// TODO: The test passes like this, but the assertion should be done after Bob
|
|
|
|
// refunded, not at the end because this can cause side-effects!
|
|
|
|
// We have to properly wait for the refund tx's finality inside the assertion,
|
|
|
|
// which requires storing the refund_tx_id in the the state!
|
2021-01-19 18:40:40 -05:00
|
|
|
ctx.assert_bob_refunded(bob_state.unwrap()).await;
|
|
|
|
ctx.assert_alice_refunded(alice_state).await;
|
2021-01-17 20:50:49 -05:00
|
|
|
})
|
|
|
|
.await;
|
2020-12-14 01:24:23 -05:00
|
|
|
}
|