mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-06-28 15:42:29 +00:00
![Daniel Karzel](/assets/img/avatar_default.png)
In the production code it is a weird indirection that we load the state and then pass in the state and the database. In the tests we have one additional load by doing it inside the command, but loading from the db is not expensive.
62 lines
2.1 KiB
Rust
62 lines
2.1 KiB
Rust
pub mod harness;
|
|
|
|
use harness::bob_run_until::is_btc_locked;
|
|
use harness::FastCancelConfig;
|
|
use swap::protocol::bob::BobState;
|
|
use swap::protocol::{alice, bob};
|
|
|
|
#[tokio::test]
|
|
async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
|
harness::setup_test(FastCancelConfig, |mut ctx| async move {
|
|
let (bob_swap, bob_join_handle) = ctx.bob_swap().await;
|
|
let bob_swap_id = bob_swap.id;
|
|
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
|
|
|
|
let alice_swap = ctx.alice_next_swap().await;
|
|
let alice_swap = tokio::spawn(alice::run(alice_swap));
|
|
|
|
let bob_state = bob_swap.await??;
|
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
|
|
|
let (bob_swap, bob_join_handle) = ctx
|
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
|
.await;
|
|
|
|
// Ensure cancel timelock is expired
|
|
if let BobState::BtcLocked(state3) = bob_swap.state.clone() {
|
|
bob_swap
|
|
.bitcoin_wallet
|
|
.subscribe_to(state3.tx_lock)
|
|
.await
|
|
.wait_until_confirmed_with(state3.cancel_timelock)
|
|
.await?;
|
|
} else {
|
|
panic!("Bob in unexpected state {}", bob_swap.state);
|
|
}
|
|
|
|
// Bob manually cancels
|
|
bob_join_handle.abort();
|
|
let (_, state) =
|
|
bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
|
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
|
|
|
let (bob_swap, bob_join_handle) = ctx
|
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
|
.await;
|
|
assert!(matches!(bob_swap.state, BobState::BtcCancelled { .. }));
|
|
|
|
// Bob manually refunds
|
|
bob_join_handle.abort();
|
|
let bob_state =
|
|
bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
|
|
|
|
ctx.assert_bob_refunded(bob_state).await;
|
|
|
|
let alice_state = alice_swap.await??;
|
|
ctx.assert_alice_refunded(alice_state).await;
|
|
|
|
Ok(())
|
|
})
|
|
.await
|
|
}
|