From b21dc03ed0c27e707146b38dd4898d10637fe00e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 19 Jan 2021 15:03:30 +1100 Subject: [PATCH] `alice::SwapFactory` should be consumed once a swap is returned --- swap/src/protocol/alice.rs | 6 +- swap/tests/happy_path.rs | 2 +- swap/tests/happy_path_restart_alice.rs | 2 +- .../happy_path_restart_bob_after_comm.rs | 2 +- .../happy_path_restart_bob_before_comm.rs | 2 +- swap/tests/punish.rs | 2 +- swap/tests/refund_restart_alice.rs | 2 +- swap/tests/testutils/mod.rs | 64 ++++++++----------- 8 files changed, 37 insertions(+), 45 deletions(-) diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 347b99be..69eccbf0 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -92,7 +92,7 @@ impl SwapFactory { } } - pub async fn new_swap(&self, swap_amounts: SwapAmounts) -> Result<(Swap, EventLoop)> { + pub async fn new_swap(self, swap_amounts: SwapAmounts) -> Result<(Swap, EventLoop)> { let initial_state = init_alice_state( swap_amounts.btc, swap_amounts.xmr, @@ -123,9 +123,9 @@ impl SwapFactory { )) } - pub async fn resume(&self) -> Result<(Swap, EventLoop)> { + pub async fn resume(self) -> Result<(Swap, EventLoop)> { // reopen the existing database - let db = Database::open(self.db_path.clone().as_path())?; + let db = Database::open(self.db_path.as_path())?; let resume_state = if let database::Swap::Alice(state) = db.get_state(self.swap_id)? { state.into() diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index 700c5d05..57789eed 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -7,7 +7,7 @@ pub mod testutils; #[tokio::test] async fn happy_path() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index dcd39f8e..1f298818 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -4,7 +4,7 @@ pub mod testutils; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index a1c04034..93382734 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -4,7 +4,7 @@ pub mod testutils; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index 231ed1e5..bd190b4c 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -7,7 +7,7 @@ pub mod testutils; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 9aea25bd..cc7289c4 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -9,7 +9,7 @@ pub mod testutils; /// the encsig and fail to refund or redeem. Alice punishes. #[tokio::test] async fn alice_punishes_if_bob_never_acts_after_fund() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 111d1cfa..55fc2abe 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -6,7 +6,7 @@ pub mod testutils; /// then also refunds. #[tokio::test] async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { - testutils::setup_test(|ctx| async move { + testutils::setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index ac32ff9c..2b070154 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -27,16 +27,22 @@ pub struct StartingBalances { pub struct TestContext { swap_amounts: SwapAmounts, - alice_swap_factory: alice::SwapFactory, + + alice_swap_factory: Option, alice_starting_balances: StartingBalances, + alice_bitcoin_wallet: Arc, + alice_monero_wallet: Arc, + bob_swap_factory: bob::SwapFactory, bob_starting_balances: StartingBalances, } impl TestContext { - pub async fn new_swap_as_alice(&self) -> alice::Swap { + pub async fn new_swap_as_alice(&mut self) -> alice::Swap { let (swap, mut event_loop) = self .alice_swap_factory + .take() + .unwrap() .new_swap(self.swap_amounts) .await .unwrap(); @@ -58,8 +64,14 @@ impl TestContext { swap } - pub async fn recover_alice_from_db(&self) -> alice::Swap { - let (swap, mut event_loop) = self.alice_swap_factory.resume().await.unwrap(); + pub async fn recover_alice_from_db(&mut self) -> alice::Swap { + let (swap, mut event_loop) = self + .alice_swap_factory + .take() + .unwrap() + .resume() + .await + .unwrap(); tokio::spawn(async move { event_loop.run().await }); @@ -77,13 +89,7 @@ impl TestContext { pub async fn assert_alice_redeemed(&self, state: AliceState) { assert!(matches!(state, AliceState::BtcRedeemed)); - let btc_balance_after_swap = self - .alice_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); assert_eq!( btc_balance_after_swap, self.alice_starting_balances.btc + self.swap_amounts.btc @@ -91,8 +97,7 @@ impl TestContext { ); let xmr_balance_after_swap = self - .alice_swap_factory - .monero_wallet + .alice_monero_wallet .as_ref() .get_balance() .await @@ -103,26 +108,18 @@ impl TestContext { pub async fn assert_alice_refunded(&self, state: AliceState) { assert!(matches!(state, AliceState::XmrRefunded)); - let btc_balance_after_swap = self - .alice_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc); // Ensure that Alice's balance is refreshed as we use a newly created wallet - self.alice_swap_factory - .monero_wallet + self.alice_monero_wallet .as_ref() .inner .refresh() .await .unwrap(); let xmr_balance_after_swap = self - .alice_swap_factory - .monero_wallet + .alice_monero_wallet .as_ref() .get_balance() .await @@ -133,13 +130,7 @@ impl TestContext { pub async fn assert_alice_punished(&self, state: AliceState) { assert!(matches!(state, AliceState::BtcPunished)); - let btc_balance_after_swap = self - .alice_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap(); assert_eq!( btc_balance_after_swap, self.alice_starting_balances.btc + self.swap_amounts.btc @@ -147,8 +138,7 @@ impl TestContext { ); let xmr_balance_after_swap = self - .alice_swap_factory - .monero_wallet + .alice_monero_wallet .as_ref() .get_balance() .await @@ -327,8 +317,8 @@ where Seed::random().unwrap(), config, Uuid::new_v4(), - alice_bitcoin_wallet, - alice_monero_wallet, + alice_bitcoin_wallet.clone(), + alice_monero_wallet.clone(), tempdir().unwrap().path().to_path_buf(), listen_address, ) @@ -360,8 +350,10 @@ where let test = TestContext { swap_amounts, - alice_swap_factory, + alice_swap_factory: Some(alice_swap_factory), alice_starting_balances, + alice_bitcoin_wallet, + alice_monero_wallet, bob_swap_factory, bob_starting_balances, };