diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index a9f72a86..2e12cf4e 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -59,8 +59,8 @@ pub struct SwapFactory { alice_address: Multiaddr, alice_peer_id: PeerId, - pub bitcoin_wallet: Arc, - pub monero_wallet: Arc, + bitcoin_wallet: Arc, + monero_wallet: Arc, } impl SwapFactory { @@ -89,7 +89,7 @@ impl SwapFactory { } pub async fn new_swap( - &self, + self, swap_amounts: SwapAmounts, config: Config, ) -> Result<(bob::Swap, bob::EventLoop)> { @@ -114,9 +114,9 @@ impl SwapFactory { )) } - pub async fn resume(&self) -> Result<(bob::Swap, bob::EventLoop)> { + pub async fn resume(self) -> Result<(bob::Swap, bob::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::Bob(state) = db.get_state(self.swap_id)? { state.into() diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 2b070154..01a4fa80 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -33,8 +33,10 @@ pub struct TestContext { alice_bitcoin_wallet: Arc, alice_monero_wallet: Arc, - bob_swap_factory: bob::SwapFactory, + bob_swap_factory: Option, bob_starting_balances: StartingBalances, + bob_bitcoin_wallet: Arc, + bob_monero_wallet: Arc, } impl TestContext { @@ -52,9 +54,11 @@ impl TestContext { swap } - pub async fn new_swap_as_bob(&self) -> bob::Swap { + pub async fn new_swap_as_bob(&mut self) -> bob::Swap { let (swap, event_loop) = self .bob_swap_factory + .take() + .unwrap() .new_swap(self.swap_amounts, Config::regtest()) .await .unwrap(); @@ -78,8 +82,14 @@ impl TestContext { swap } - pub async fn recover_bob_from_db(&self) -> bob::Swap { - let (swap, event_loop) = self.bob_swap_factory.resume().await.unwrap(); + pub async fn recover_bob_from_db(&mut self) -> bob::Swap { + let (swap, event_loop) = self + .bob_swap_factory + .take() + .unwrap() + .resume() + .await + .unwrap(); tokio::spawn(async move { event_loop.run().await }); @@ -154,39 +164,25 @@ impl TestContext { }; let lock_tx_bitcoin_fee = self - .bob_swap_factory - .bitcoin_wallet + .bob_bitcoin_wallet .transaction_fee(lock_tx_id) .await .unwrap(); - let btc_balance_after_swap = self - .bob_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap(); assert_eq!( btc_balance_after_swap, self.bob_starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee ); // Ensure that Bob's balance is refreshed as we use a newly created wallet - self.bob_swap_factory - .monero_wallet + self.bob_monero_wallet .as_ref() .inner .refresh() .await .unwrap(); - let xmr_balance_after_swap = self - .bob_swap_factory - .monero_wallet - .as_ref() - .get_balance() - .await - .unwrap(); + let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap(); assert_eq!( xmr_balance_after_swap, self.bob_starting_balances.xmr + self.swap_amounts.xmr @@ -200,19 +196,12 @@ impl TestContext { panic!("Bob in unexpected state"); }; let lock_tx_bitcoin_fee = self - .bob_swap_factory - .bitcoin_wallet + .bob_bitcoin_wallet .transaction_fee(lock_tx_id) .await .unwrap(); - let btc_balance_after_swap = self - .bob_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap(); let alice_submitted_cancel = btc_balance_after_swap == self.bob_starting_balances.btc @@ -228,13 +217,7 @@ impl TestContext { // Since we cannot be sure who submitted it we have to assert accordingly assert!(alice_submitted_cancel || bob_submitted_cancel); - let xmr_balance_after_swap = self - .bob_swap_factory - .monero_wallet - .as_ref() - .get_balance() - .await - .unwrap(); + let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap(); assert_eq!(xmr_balance_after_swap, self.bob_starting_balances.xmr); } @@ -246,31 +229,18 @@ impl TestContext { }; let lock_tx_bitcoin_fee = self - .bob_swap_factory - .bitcoin_wallet + .bob_bitcoin_wallet .transaction_fee(lock_tx_id) .await .unwrap(); - let btc_balance_after_swap = self - .bob_swap_factory - .bitcoin_wallet - .as_ref() - .balance() - .await - .unwrap(); + let btc_balance_after_swap = self.bob_bitcoin_wallet.as_ref().balance().await.unwrap(); assert_eq!( btc_balance_after_swap, self.bob_starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee ); - let xmr_balance_after_swap = self - .bob_swap_factory - .monero_wallet - .as_ref() - .get_balance() - .await - .unwrap(); + let xmr_balance_after_swap = self.bob_monero_wallet.as_ref().get_balance().await.unwrap(); assert_eq!(xmr_balance_after_swap, self.bob_starting_balances.xmr); } } @@ -342,8 +312,8 @@ where Seed::random().unwrap(), tempdir().unwrap().path().to_path_buf(), Uuid::new_v4(), - bob_bitcoin_wallet, - bob_monero_wallet, + bob_bitcoin_wallet.clone(), + bob_monero_wallet.clone(), alice_swap_factory.listen_address(), alice_swap_factory.peer_id(), ); @@ -354,8 +324,10 @@ where alice_starting_balances, alice_bitcoin_wallet, alice_monero_wallet, - bob_swap_factory, + bob_swap_factory: Some(bob_swap_factory), bob_starting_balances, + bob_bitcoin_wallet, + bob_monero_wallet, }; testfn(test).await