From 22bdc08c838c99790fb7a7fa39636e5dc479163d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 16 Apr 2021 12:00:11 +1000 Subject: [PATCH] Get rid of Bob's swap Builder Doesn't serve any purpose. We are better of just having two constructors. --- swap/src/bin/swap.rs | 18 +++++----- swap/src/protocol/bob.rs | 69 ++++++++++++++------------------------- swap/tests/harness/mod.rs | 63 +++++++++++++++++------------------ 3 files changed, 64 insertions(+), 86 deletions(-) diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index c155ba57..d5368915 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -27,7 +27,7 @@ use swap::env::{Config, GetConfig}; use swap::network::quote::BidQuote; use swap::network::swarm; use swap::protocol::bob; -use swap::protocol::bob::{Builder, EventLoop}; +use swap::protocol::bob::{EventLoop, Swap}; use swap::seed::Seed; use swap::{bitcoin, cli, env, monero}; use tracing::{debug, error, info, warn}; @@ -105,17 +105,16 @@ async fn main() -> Result<()> { db.insert_peer_id(swap_id, alice_peer_id).await?; - let swap = Builder::new( + let swap = Swap::new( db, swap_id, - bitcoin_wallet.clone(), + bitcoin_wallet, Arc::new(monero_wallet), env_config, event_loop_handle, receive_monero_address, - ) - .with_init_params(send_bitcoin) - .build()?; + send_bitcoin, + ); tokio::select! { result = event_loop => { @@ -182,16 +181,15 @@ async fn main() -> Result<()> { EventLoop::new(swap_id, swarm, alice_peer_id, bitcoin_wallet.clone())?; let handle = tokio::spawn(event_loop.run()); - let swap = Builder::new( + let swap = Swap::from_db( db, swap_id, - bitcoin_wallet.clone(), + bitcoin_wallet, Arc::new(monero_wallet), env_config, event_loop_handle, receive_monero_address, - ) - .build()?; + )?; tokio::select! { event_loop_result = handle => { diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index aeae1f00..fbcc7dd1 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -30,71 +30,50 @@ pub struct Swap { pub receive_monero_address: monero::Address, } -pub struct Builder { - swap_id: Uuid, - db: Database, - - bitcoin_wallet: Arc, - monero_wallet: Arc, - - init_params: InitParams, - env_config: env::Config, - - event_loop_handle: EventLoopHandle, - - receive_monero_address: monero::Address, -} - -enum InitParams { - None, - New { btc_amount: bitcoin::Amount }, -} - -impl Builder { +impl Swap { #[allow(clippy::too_many_arguments)] pub fn new( db: Database, - swap_id: Uuid, + id: Uuid, bitcoin_wallet: Arc, monero_wallet: Arc, env_config: env::Config, event_loop_handle: EventLoopHandle, receive_monero_address: monero::Address, + btc_amount: bitcoin::Amount, ) -> Self { Self { - swap_id, + state: BobState::Started { btc_amount }, + event_loop_handle, db, bitcoin_wallet, monero_wallet, - init_params: InitParams::None, env_config, - event_loop_handle, + id, receive_monero_address, } } - pub fn with_init_params(self, btc_amount: bitcoin::Amount) -> Self { - Self { - init_params: InitParams::New { btc_amount }, - ..self - } - } + pub fn from_db( + db: Database, + id: Uuid, + bitcoin_wallet: Arc, + monero_wallet: Arc, + env_config: env::Config, + event_loop_handle: EventLoopHandle, + receive_monero_address: monero::Address, + ) -> Result { + let state = db.get_state(id)?.try_into_bob()?.into(); - pub fn build(self) -> Result { - let state = match self.init_params { - InitParams::New { btc_amount } => BobState::Started { btc_amount }, - InitParams::None => self.db.get_state(self.swap_id)?.try_into_bob()?.into(), - }; - - Ok(Swap { + Ok(Self { state, - event_loop_handle: self.event_loop_handle, - db: self.db, - bitcoin_wallet: self.bitcoin_wallet.clone(), - monero_wallet: self.monero_wallet.clone(), - id: self.swap_id, - env_config: self.env_config, - receive_monero_address: self.receive_monero_address, + event_loop_handle, + db, + bitcoin_wallet, + monero_wallet, + env_config, + id, + receive_monero_address, }) } } diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 8264b527..4888a4ce 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -394,22 +394,41 @@ struct BobParams { } impl BobParams { - pub async fn builder( - &self, - event_loop_handle: bob::EventLoopHandle, - swap_id: Uuid, - ) -> Result { - let receive_address = self.monero_wallet.get_main_address(); + pub fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, bob::EventLoop)> { + let (event_loop, handle) = self.new_eventloop(swap_id)?; + let db = Database::open(&self.db_path)?; - Ok(bob::Builder::new( - Database::open(&self.db_path.clone().as_path()).unwrap(), + let swap = bob::Swap::from_db( + db, swap_id, self.bitcoin_wallet.clone(), self.monero_wallet.clone(), self.env_config, - event_loop_handle, - receive_address, - )) + handle, + self.monero_wallet.get_main_address(), + )?; + + Ok((swap, event_loop)) + } + + pub fn new_swap(&self, btc_amount: bitcoin::Amount) -> Result<(bob::Swap, bob::EventLoop)> { + let swap_id = Uuid::new_v4(); + + let (event_loop, handle) = self.new_eventloop(swap_id)?; + let db = Database::open(&self.db_path)?; + + let swap = bob::Swap::new( + db, + swap_id, + self.bitcoin_wallet.clone(), + self.monero_wallet.clone(), + self.env_config, + handle, + self.monero_wallet.get_main_address(), + btc_amount, + ); + + Ok((swap, event_loop)) } pub fn new_eventloop(&self, swap_id: Uuid) -> Result<(bob::EventLoop, bob::EventLoopHandle)> { @@ -493,17 +512,7 @@ impl TestContext { } pub async fn bob_swap(&mut self) -> (bob::Swap, BobApplicationHandle) { - let swap_id = Uuid::new_v4(); - let (event_loop, event_loop_handle) = self.bob_params.new_eventloop(swap_id).unwrap(); - - let swap = self - .bob_params - .builder(event_loop_handle, swap_id) - .await - .unwrap() - .with_init_params(self.btc_amount) - .build() - .unwrap(); + let (swap, event_loop) = self.bob_params.new_swap(self.btc_amount).unwrap(); // ensure the wallet is up to date for concurrent swap tests swap.bitcoin_wallet.sync().await.unwrap(); @@ -520,15 +529,7 @@ impl TestContext { ) -> (bob::Swap, BobApplicationHandle) { join_handle.abort(); - let (event_loop, event_loop_handle) = self.bob_params.new_eventloop(swap_id).unwrap(); - - let swap = self - .bob_params - .builder(event_loop_handle, swap_id) - .await - .unwrap() - .build() - .unwrap(); + let (swap, event_loop) = self.bob_params.new_swap_from_db(swap_id).unwrap(); let join_handle = tokio::spawn(event_loop.run());