2021-02-01 07:13:08 +00:00
|
|
|
pub mod testutils;
|
|
|
|
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::protocol::bob::BobState;
|
2021-03-16 06:08:19 +00:00
|
|
|
use swap::protocol::{alice, bob};
|
2021-03-04 00:28:58 +00:00
|
|
|
use testutils::bob_run_until::is_btc_locked;
|
|
|
|
use testutils::FastCancelConfig;
|
2021-02-01 07:13:08 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
|
|
|
testutils::setup_test(FastCancelConfig, |mut ctx| async move {
|
2021-03-16 06:08:19 +00:00
|
|
|
let (bob_swap, bob_join_handle) = ctx.bob_swap().await;
|
|
|
|
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
|
2021-02-01 07:13:08 +00:00
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
let alice_swap = ctx.alice_next_swap().await;
|
2021-03-16 08:24:41 +00:00
|
|
|
let alice_swap = tokio::spawn(alice::run(alice_swap));
|
2021-02-01 07:13:08 +00:00
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
let bob_state = bob_swap.await??;
|
2021-02-01 07:13:08 +00:00
|
|
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
|
|
|
|
|
|
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
|
|
|
|
|
|
|
// Ensure Bob's timelock is expired
|
|
|
|
if let BobState::BtcLocked(state3) = bob_swap.state.clone() {
|
|
|
|
state3
|
|
|
|
.wait_for_cancel_timelock_to_expire(bob_swap.bitcoin_wallet.as_ref())
|
2021-03-16 06:08:19 +00:00
|
|
|
.await?;
|
2021-02-01 07:13:08 +00:00
|
|
|
} else {
|
|
|
|
panic!("Bob in unexpected state {}", bob_swap.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob manually cancels
|
2021-02-09 00:15:55 +00:00
|
|
|
bob_join_handle.abort();
|
2021-02-01 07:13:08 +00:00
|
|
|
let (_, state) = bob::cancel(
|
|
|
|
bob_swap.swap_id,
|
|
|
|
bob_swap.state,
|
|
|
|
bob_swap.bitcoin_wallet,
|
|
|
|
bob_swap.db,
|
2021-02-01 11:32:54 +00:00
|
|
|
false,
|
2021-02-01 07:13:08 +00:00
|
|
|
)
|
2021-03-16 06:08:19 +00:00
|
|
|
.await??;
|
2021-02-01 07:13:08 +00:00
|
|
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
|
|
|
|
2021-02-09 00:15:55 +00:00
|
|
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
2021-02-01 07:13:08 +00:00
|
|
|
assert!(matches!(bob_swap.state, BobState::BtcCancelled { .. }));
|
|
|
|
|
|
|
|
// Bob manually refunds
|
2021-02-09 00:15:55 +00:00
|
|
|
bob_join_handle.abort();
|
2021-02-01 07:13:08 +00:00
|
|
|
let bob_state = bob::refund(
|
|
|
|
bob_swap.swap_id,
|
|
|
|
bob_swap.state,
|
|
|
|
bob_swap.bitcoin_wallet,
|
|
|
|
bob_swap.db,
|
2021-02-01 11:32:54 +00:00
|
|
|
false,
|
2021-02-01 07:13:08 +00:00
|
|
|
)
|
2021-03-16 06:08:19 +00:00
|
|
|
.await??;
|
2021-02-01 07:13:08 +00:00
|
|
|
|
|
|
|
ctx.assert_bob_refunded(bob_state).await;
|
2021-03-16 06:08:19 +00:00
|
|
|
|
2021-03-16 08:24:41 +00:00
|
|
|
let alice_state = alice_swap.await??;
|
|
|
|
ctx.assert_alice_refunded(alice_state).await;
|
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
Ok(())
|
2021-02-01 07:13:08 +00:00
|
|
|
})
|
2021-03-16 06:08:19 +00:00
|
|
|
.await
|
2021-02-01 07:13:08 +00:00
|
|
|
}
|