Merge #405
405: Concurrent swaps with same peer r=da-kami a=da-kami Fixes #367 - [x] Concurrent swaps with same peer Not sure how much more time I should invest into this. We could just merge the current state and then do improvements on top...? Improvements: - [x] Think `// TODO: Remove unnecessary swap-id check` through and remove it - [x] Add concurrent swap test, multiple swaps with same Bob - [ ] Save swap messages without matching swap in execution in the database - [ ] Assert the balances in the new concurrent swap tests - [ ] ~~Add concurrent swap test, multiple swaps with different Bobs~~ - [ ] ~~Send swap-id in separate message, not on top of `Message0`~~ Co-authored-by: Daniel Karzel <daniel@comit.network>pull/407/head
commit
19766b9759
@ -0,0 +1,60 @@
|
||||
pub mod harness;
|
||||
|
||||
use harness::bob_run_until::is_xmr_locked;
|
||||
use harness::SlowCancelConfig;
|
||||
use swap::protocol::alice::AliceState;
|
||||
use swap::protocol::bob::BobState;
|
||||
use swap::protocol::{alice, bob};
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_bobs_after_xmr_lock_proof_sent() {
|
||||
harness::setup_test(SlowCancelConfig, |mut ctx| async move {
|
||||
let (bob_swap_1, bob_join_handle_1) = ctx.bob_swap().await;
|
||||
|
||||
let swap_id = bob_swap_1.swap_id;
|
||||
|
||||
let bob_swap_1 = tokio::spawn(bob::run_until(bob_swap_1, is_xmr_locked));
|
||||
|
||||
let alice_swap_1 = ctx.alice_next_swap().await;
|
||||
let alice_swap_1 = tokio::spawn(alice::run(alice_swap_1));
|
||||
|
||||
let bob_state_1 = bob_swap_1.await??;
|
||||
assert!(matches!(bob_state_1, BobState::XmrLocked { .. }));
|
||||
|
||||
// make sure bob_swap_1's event loop is gone
|
||||
bob_join_handle_1.abort();
|
||||
|
||||
let (bob_swap_2, bob_join_handle_2) = ctx.bob_swap().await;
|
||||
let bob_swap_2 = tokio::spawn(bob::run(bob_swap_2));
|
||||
|
||||
let alice_swap_2 = ctx.alice_next_swap().await;
|
||||
let alice_swap_2 = tokio::spawn(alice::run(alice_swap_2));
|
||||
|
||||
// The 2nd swap should ALWAYS finish successfully in this
|
||||
// scenario
|
||||
|
||||
let bob_state_2 = bob_swap_2.await??;
|
||||
assert!(matches!(bob_state_2, BobState::XmrRedeemed { .. }));
|
||||
|
||||
let alice_state_2 = alice_swap_2.await??;
|
||||
assert!(matches!(alice_state_2, AliceState::BtcRedeemed { .. }));
|
||||
|
||||
let (bob_swap_1, _) = ctx
|
||||
.stop_and_resume_bob_from_db(bob_join_handle_2, swap_id)
|
||||
.await;
|
||||
assert!(matches!(bob_swap_1.state, BobState::XmrLocked { .. }));
|
||||
|
||||
// The 1st (paused) swap ALWAYS finishes successfully in this
|
||||
// scenario, because it is ensured that Bob already received the
|
||||
// transfer proof.
|
||||
|
||||
let bob_state_1 = bob::run(bob_swap_1).await?;
|
||||
assert!(matches!(bob_state_1, BobState::XmrRedeemed { .. }));
|
||||
|
||||
let alice_state_1 = alice_swap_1.await??;
|
||||
assert!(matches!(alice_state_1, AliceState::BtcRedeemed { .. }));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await;
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
pub mod harness;
|
||||
|
||||
use harness::bob_run_until::is_btc_locked;
|
||||
use harness::SlowCancelConfig;
|
||||
use swap::protocol::alice::AliceState;
|
||||
use swap::protocol::bob::BobState;
|
||||
use swap::protocol::{alice, bob};
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_bobs_before_xmr_lock_proof_sent() {
|
||||
harness::setup_test(SlowCancelConfig, |mut ctx| async move {
|
||||
let (bob_swap_1, bob_join_handle_1) = ctx.bob_swap().await;
|
||||
|
||||
let swap_id = bob_swap_1.swap_id;
|
||||
|
||||
let bob_swap_1 = tokio::spawn(bob::run_until(bob_swap_1, is_btc_locked));
|
||||
|
||||
let alice_swap_1 = ctx.alice_next_swap().await;
|
||||
let alice_swap_1 = tokio::spawn(alice::run(alice_swap_1));
|
||||
|
||||
let bob_state_1 = bob_swap_1.await??;
|
||||
assert!(matches!(bob_state_1, BobState::BtcLocked(_)));
|
||||
|
||||
// make sure bob_swap_1's event loop is gone
|
||||
bob_join_handle_1.abort();
|
||||
|
||||
let (bob_swap_2, bob_join_handle_2) = ctx.bob_swap().await;
|
||||
let bob_swap_2 = tokio::spawn(bob::run(bob_swap_2));
|
||||
|
||||
let alice_swap_2 = ctx.alice_next_swap().await;
|
||||
let alice_swap_2 = tokio::spawn(alice::run(alice_swap_2));
|
||||
|
||||
// The 2nd swap ALWAYS finish successfully in this
|
||||
// scenario, but will receive an "unwanted" transfer proof that is ignored in
|
||||
// the event loop.
|
||||
|
||||
let bob_state_2 = bob_swap_2.await??;
|
||||
assert!(matches!(bob_state_2, BobState::XmrRedeemed { .. }));
|
||||
|
||||
let alice_state_2 = alice_swap_2.await??;
|
||||
assert!(matches!(alice_state_2, AliceState::BtcRedeemed { .. }));
|
||||
|
||||
let (bob_swap_1, _) = ctx
|
||||
.stop_and_resume_bob_from_db(bob_join_handle_2, swap_id)
|
||||
.await;
|
||||
assert!(matches!(bob_state_1, BobState::BtcLocked(_)));
|
||||
|
||||
// The 1st (paused) swap is expected to refund, because the transfer
|
||||
// proof is delivered to the wrong swap, and we currently don't store it in the
|
||||
// database for the other swap.
|
||||
|
||||
let bob_state_1 = bob::run(bob_swap_1).await?;
|
||||
assert!(matches!(bob_state_1, BobState::BtcRefunded { .. }));
|
||||
|
||||
let alice_state_1 = alice_swap_1.await??;
|
||||
assert!(matches!(alice_state_1, AliceState::XmrRefunded { .. }));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
pub mod harness;
|
||||
|
||||
use harness::SlowCancelConfig;
|
||||
use swap::protocol::bob;
|
||||
|
||||
#[tokio::test]
|
||||
async fn ensure_same_swap_id_for_alice_and_bob() {
|
||||
harness::setup_test(SlowCancelConfig, |mut ctx| async move {
|
||||
let (bob_swap, _) = ctx.bob_swap().await;
|
||||
let bob_swap_id = bob_swap.swap_id;
|
||||
let _ = tokio::spawn(bob::run(bob_swap));
|
||||
|
||||
// once Bob's swap is spawned we can retrieve Alice's swap and assert on the
|
||||
// swap ID
|
||||
let alice_swap = ctx.alice_next_swap().await;
|
||||
assert_eq!(alice_swap.swap_id, bob_swap_id);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await;
|
||||
}
|
Loading…
Reference in new issue