xmr-btc-swap/swap/tests/refund_restart_alice.rs

67 lines
2.2 KiB
Rust
Raw Normal View History

2020-12-14 01:24:23 -05:00
use rand::rngs::OsRng;
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]
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
2021-01-17 20:50:49 -05:00
testutils::test(|alice_harness, bob_harness| async move {
let alice = alice_harness.new_alice().await;
let bob = bob_harness.new_bob().await;
let bob_swap = bob::swap(
bob.state,
bob.event_loop_handle,
bob.db,
bob.bitcoin_wallet.clone(),
bob.monero_wallet.clone(),
OsRng,
bob.swap_id,
);
let bob_swap_handle = tokio::spawn(bob_swap);
let alice_state = alice::run_until(
alice.state,
alice::swap::is_xmr_locked,
2021-01-17 20:50:49 -05:00
alice.event_loop_handle,
alice.bitcoin_wallet.clone(),
alice.monero_wallet.clone(),
alice.config,
alice.swap_id,
alice.db,
)
2021-01-17 20:50:49 -05:00
.await
.unwrap();
assert!(matches!(alice_state, AliceState::XmrLocked {..}));
// Alice does not act, Bob refunds
let bob_state = bob_swap_handle.await.unwrap();
// Once bob has finished Alice is restarted and refunds as well
let alice = alice_harness.recover_alice_from_db().await;
assert!(matches!(alice.state, AliceState::XmrLocked {..}));
let alice_state = alice::swap(
alice.state,
alice.event_loop_handle,
alice.bitcoin_wallet.clone(),
alice.monero_wallet.clone(),
alice.config,
alice.swap_id,
alice.db,
)
.await
2021-01-17 20:50:49 -05:00
.unwrap();
// 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!
bob_harness.assert_refunded(bob_state.unwrap()).await;
alice_harness.assert_refunded(alice_state).await;
})
.await;
2020-12-14 01:24:23 -05:00
}