From 91d4d5da25b8bc4150d8def514e1748dcc4a5739 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 14 Dec 2020 21:30:45 +1100 Subject: [PATCH] Cleanup swap execution to not return EventLoopHandle, refactor both_refund test --- swap/src/alice/swap.rs | 22 ++++++++++------------ swap/tests/alice_safe_restart.rs | 4 ++-- swap/tests/punish.rs | 2 +- swap/tests/refund.rs | 26 ++++++++++++++------------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index a87a07ba..c6a4cfd4 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -125,12 +125,10 @@ impl From<&AliceState> for state::Alice { AliceState::BtcRefunded { .. } => Alice::SwapComplete, AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()), AliceState::XmrRefunded => Alice::SwapComplete, - // TODO(Franck): it may be more efficient to store the fact that we already want to - // abort AliceState::Cancelling { state3 } => Alice::Cancelling(state3.clone()), AliceState::Punished => Alice::SwapComplete, AliceState::SafelyAborted => Alice::SwapComplete, - // todo: we may want to swap recovering from swaps that have not been negotiated + // TODO: Potentially add support to recover swaps that are not Negotiated AliceState::Started { .. } => { panic!("Alice attempted to save swap before being negotiated") } @@ -223,7 +221,7 @@ pub async fn swap( config: Config, swap_id: Uuid, db: Database, -) -> Result<(AliceState, EventLoopHandle)> { +) -> Result { run_until( state, is_complete, @@ -247,7 +245,7 @@ pub async fn recover( ) -> Result { let db_swap = db.get_state(swap_id)?; let start_state = AliceState::try_from(db_swap)?; - let (state, _) = swap( + let state = swap( start_state, event_loop_handle, bitcoin_wallet, @@ -297,10 +295,10 @@ pub async fn run_until( swap_id: Uuid, db: Database, // TODO: Remove EventLoopHandle! -) -> Result<(AliceState, EventLoopHandle)> { +) -> Result { info!("Current state:{}", state); if is_target_state(&state) { - Ok((state, event_loop_handle)) + Ok(state) } else { match state { AliceState::Started { amounts, state0 } => { @@ -607,7 +605,7 @@ pub async fn run_until( let db_state = (&state).into(); db.insert_latest_state(swap_id, Swap::Alice(db_state)) .await?; - Ok((state, event_loop_handle)) + Ok(state) } AliceState::BtcPunishable { tx_refund, state3 } => { let signed_tx_punish = build_bitcoin_punish_transaction( @@ -675,10 +673,10 @@ pub async fn run_until( } } } - AliceState::XmrRefunded => Ok((AliceState::XmrRefunded, event_loop_handle)), - AliceState::BtcRedeemed => Ok((AliceState::BtcRedeemed, event_loop_handle)), - AliceState::Punished => Ok((AliceState::Punished, event_loop_handle)), - AliceState::SafelyAborted => Ok((AliceState::SafelyAborted, event_loop_handle)), + AliceState::XmrRefunded => Ok(AliceState::XmrRefunded), + AliceState::BtcRedeemed => Ok(AliceState::BtcRedeemed), + AliceState::Punished => Ok(AliceState::Punished), + AliceState::SafelyAborted => Ok(AliceState::SafelyAborted), } } } diff --git a/swap/tests/alice_safe_restart.rs b/swap/tests/alice_safe_restart.rs index 7bd19ea2..eabc5403 100644 --- a/swap/tests/alice_safe_restart.rs +++ b/swap/tests/alice_safe_restart.rs @@ -87,7 +87,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let alice_swap_id = Uuid::new_v4(); - let (alice_state, _) = alice::swap::run_until( + let alice_state = alice::swap::run_until( start_state, alice::swap::is_encsig_learned, alice_event_loop_handle, @@ -209,7 +209,7 @@ async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { let alice_swap_id = Uuid::new_v4(); - let (alice_state, _) = alice::swap::run_until( + let alice_state = alice::swap::run_until( start_state, alice::swap::is_xmr_locked, alice_event_loop_handle, diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 5254a10d..5262b17f 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -99,7 +99,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await }); // Wait until alice has locked xmr and bob has locked btc - let ((alice_state, _), bob_state) = try_join(alice_fut, bob_btc_locked_fut).await.unwrap(); + let (alice_state, bob_state) = try_join(alice_fut, bob_btc_locked_fut).await.unwrap(); assert!(matches!(alice_state, AliceState::Punished)); let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state { diff --git a/swap/tests/refund.rs b/swap/tests/refund.rs index d8e533dd..4c492e77 100644 --- a/swap/tests/refund.rs +++ b/swap/tests/refund.rs @@ -42,8 +42,8 @@ async fn both_refund() { let ( alice_state, - mut alice_swarm_driver, - alice_swarm_handle, + mut alice_event_loop, + alice_event_loop_handle, alice_btc_wallet, alice_xmr_wallet, ) = init_alice( @@ -57,9 +57,9 @@ async fn both_refund() { ) .await; - let (bob_state, bob_swarm_driver, bob_swarm_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) = + let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) = init_bob( - alice_multiaddr, + alice_multiaddr.clone(), &bitcoind, &monero, btc_to_swap, @@ -71,7 +71,7 @@ async fn both_refund() { let bob_fut = bob::swap::swap( bob_state, - bob_swarm_handle, + bob_event_loop_handle, bob_db, bob_btc_wallet.clone(), bob_xmr_wallet.clone(), @@ -79,7 +79,7 @@ async fn both_refund() { Uuid::new_v4(), ); - tokio::spawn(async move { bob_swarm_driver.run().await }); + tokio::spawn(async move { bob_event_loop.run().await }); let alice_swap_id = Uuid::new_v4(); let alice_db_datadir = tempdir().unwrap(); @@ -88,7 +88,7 @@ async fn both_refund() { let alice_xmr_locked_fut = alice::swap::run_until( alice_state, alice::swap::is_xmr_locked, - alice_swarm_handle, + alice_event_loop_handle, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), Config::regtest(), @@ -96,11 +96,10 @@ async fn both_refund() { alice_db, ); - tokio::spawn(async move { alice_swarm_driver.run().await }); + tokio::spawn(async move { alice_event_loop.run().await }); // Wait until alice has locked xmr and bob has locked btc - let (bob_state, (alice_state, alice_swarm_handle)) = - try_join(bob_fut, alice_xmr_locked_fut).await.unwrap(); + let (bob_state, alice_state) = try_join(bob_fut, alice_xmr_locked_fut).await.unwrap(); let bob_state4 = if let BobState::BtcRefunded(state4) = bob_state { state4 @@ -109,10 +108,12 @@ async fn both_refund() { }; let alice_db = Database::open(alice_db_datadir.path()).unwrap(); + let (mut alice_event_loop, alice_event_loop_handle) = + testutils::init_alice_eventloop(alice_multiaddr); - let (alice_state, _) = alice::swap::swap( + let alice_state = alice::swap::swap( alice_state, - alice_swarm_handle, + alice_event_loop_handle, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), Config::regtest(), @@ -121,6 +122,7 @@ async fn both_refund() { ) .await .unwrap(); + tokio::spawn(async move { alice_event_loop.run().await }); assert!(matches!(alice_state, AliceState::XmrRefunded));