2021-02-01 08:17:53 +00:00
|
|
|
pub mod testutils;
|
|
|
|
|
2021-03-04 06:02:03 +00:00
|
|
|
use bob::cancel::Error;
|
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::SlowCancelConfig;
|
2021-02-01 08:17:53 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
|
|
|
testutils::setup_test(SlowCancelConfig, |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 08:17:53 +00:00
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
let alice_swap = ctx.alice_next_swap().await;
|
|
|
|
let _ = tokio::spawn(alice::run(alice_swap));
|
|
|
|
|
|
|
|
let bob_state = bob_swap.await??;
|
2021-02-01 11:32:54 +00:00
|
|
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
2021-02-01 08:17:53 +00:00
|
|
|
|
|
|
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
2021-02-01 11:32:54 +00:00
|
|
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
2021-02-01 08:17:53 +00:00
|
|
|
|
2021-02-03 23:43:02 +00:00
|
|
|
// Bob tries but fails to manually cancel
|
2021-02-01 08:17:53 +00:00
|
|
|
let result = 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 08:17:53 +00:00
|
|
|
)
|
2021-03-16 06:08:19 +00:00
|
|
|
.await?
|
2021-02-01 08:17:53 +00:00
|
|
|
.err()
|
|
|
|
.unwrap();
|
|
|
|
|
2021-03-04 06:02:03 +00:00
|
|
|
assert!(matches!(result, Error::CancelTimelockNotExpiredYet));
|
2021-02-01 08:17:53 +00:00
|
|
|
|
|
|
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
2021-02-01 11:32:54 +00:00
|
|
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
2021-02-01 08:17:53 +00:00
|
|
|
|
2021-02-03 23:43:02 +00:00
|
|
|
// Bob tries but fails to manually refund
|
2021-02-01 08:17:53 +00:00
|
|
|
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 08:17:53 +00:00
|
|
|
)
|
2021-03-16 06:08:19 +00:00
|
|
|
.await?
|
2021-02-01 08:17:53 +00:00
|
|
|
.err()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
2021-02-01 11:32:54 +00:00
|
|
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
2021-03-16 06:08:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-02-01 08:17:53 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|