Add --force flag for cancel and refund

This commit is contained in:
Daniel Karzel 2021-02-01 22:32:54 +11:00
parent 02f8eb7f18
commit c930ad84a4
8 changed files with 118 additions and 22 deletions

View file

@ -34,6 +34,7 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await
.unwrap()
@ -50,6 +51,7 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
bob_swap.execution_params,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await
.unwrap()

View file

@ -14,10 +14,10 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
tokio::spawn(alice_handle);
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
assert!(matches!(bob_state, BobState::BtcLocked {..}));
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob manually cancels
let result = bob::cancel(
@ -25,6 +25,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await
.unwrap()
@ -34,7 +35,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
assert!(matches!(result, CancelError::CancelTimelockNotExpiredYet));
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob manually refunds
bob::refund(
@ -43,6 +44,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
bob_swap.execution_params,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await
.unwrap()
@ -50,7 +52,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
.unwrap();
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
})
.await;
}

View file

@ -0,0 +1,54 @@
pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState};
use testutils::{bob_run_until::is_btc_locked, SlowCancelConfig};
#[tokio::test]
async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() {
testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;
let alice_handle = alice::run(alice_swap);
tokio::spawn(alice_handle);
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob forces a cancel that will fail
let is_error = bob::cancel(
bob_swap.swap_id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
true,
)
.await
.is_err();
assert!(is_error);
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob forces a refund that will fail
let is_error = bob::refund(
bob_swap.swap_id,
bob_swap.state,
bob_swap.execution_params,
bob_swap.bitcoin_wallet,
bob_swap.db,
true,
)
.await
.is_err();
assert!(is_error);
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
})
.await;
}