2021-03-24 06:49:19 +00:00
|
|
|
pub mod harness;
|
2021-02-01 07:13:08 +00:00
|
|
|
|
2021-04-27 04:51:53 +00:00
|
|
|
use harness::alice_run_until::is_xmr_lock_transaction_sent;
|
2021-03-24 06:49:19 +00:00
|
|
|
use harness::bob_run_until::is_btc_locked;
|
|
|
|
use harness::FastCancelConfig;
|
2021-06-25 03:40:33 +00:00
|
|
|
use swap::asb::FixedRate;
|
2021-04-27 04:51:53 +00:00
|
|
|
use swap::protocol::alice::AliceState;
|
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-06-25 03:40:33 +00:00
|
|
|
use swap::{asb, cli};
|
2021-02-01 07:13:08 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2021-04-27 04:51:53 +00:00
|
|
|
async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
|
2021-03-24 06:49:19 +00:00
|
|
|
harness::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;
|
2021-04-16 01:48:15 +00:00
|
|
|
let bob_swap_id = bob_swap.id;
|
2021-03-16 06:08:19 +00:00
|
|
|
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-05-05 03:43:46 +00:00
|
|
|
let alice_swap = tokio::spawn(alice::run_until(
|
|
|
|
alice_swap,
|
|
|
|
is_xmr_lock_transaction_sent,
|
|
|
|
FixedRate::default(),
|
|
|
|
));
|
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 { .. }));
|
|
|
|
|
2021-04-27 04:51:53 +00:00
|
|
|
let alice_state = alice_swap.await??;
|
|
|
|
assert!(matches!(
|
|
|
|
alice_state,
|
|
|
|
AliceState::XmrLockTransactionSent { .. }
|
|
|
|
));
|
|
|
|
|
2021-04-08 08:56:26 +00:00
|
|
|
let (bob_swap, bob_join_handle) = ctx
|
|
|
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
|
|
|
.await;
|
2021-02-01 07:13:08 +00:00
|
|
|
|
2021-03-31 01:24:57 +00:00
|
|
|
// Ensure cancel timelock is expired
|
2021-12-20 03:58:11 +00:00
|
|
|
if let BobState::BtcLocked { state3, .. } = bob_swap.state.clone() {
|
2021-03-23 02:49:57 +00:00
|
|
|
bob_swap
|
|
|
|
.bitcoin_wallet
|
|
|
|
.subscribe_to(state3.tx_lock)
|
|
|
|
.await
|
|
|
|
.wait_until_confirmed_with(state3.cancel_timelock)
|
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-09-01 23:16:25 +00:00
|
|
|
let (_, state) = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
|
2021-02-01 07:13:08 +00:00
|
|
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
|
|
|
|
2021-04-08 08:56:26 +00:00
|
|
|
let (bob_swap, bob_join_handle) = ctx
|
|
|
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
|
|
|
.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-09-01 23:16:25 +00:00
|
|
|
let bob_state = cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).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-04-27 04:51:53 +00:00
|
|
|
// manually refund ALice's swap
|
|
|
|
ctx.restart_alice().await;
|
|
|
|
let alice_swap = ctx.alice_next_swap().await;
|
2021-06-25 03:40:33 +00:00
|
|
|
let alice_state = asb::refund(
|
2021-04-27 04:51:53 +00:00
|
|
|
alice_swap.swap_id,
|
|
|
|
alice_swap.bitcoin_wallet,
|
|
|
|
alice_swap.monero_wallet,
|
|
|
|
alice_swap.db,
|
|
|
|
)
|
2021-09-01 23:16:25 +00:00
|
|
|
.await?;
|
2021-04-27 04:51:53 +00:00
|
|
|
|
2021-03-16 08:24:41 +00:00
|
|
|
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
|
|
|
}
|