Get rid of Bob's swap Builder

Doesn't serve any purpose. We are better of just having two
constructors.
This commit is contained in:
Thomas Eizinger 2021-04-16 12:00:11 +10:00
parent e266fb07ef
commit 22bdc08c83
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 64 additions and 86 deletions

View File

@ -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 => {

View File

@ -30,71 +30,50 @@ pub struct Swap {
pub receive_monero_address: monero::Address,
}
pub struct Builder {
swap_id: Uuid,
db: Database,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
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<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
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<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
event_loop_handle: EventLoopHandle,
receive_monero_address: monero::Address,
) -> Result<Self> {
let state = db.get_state(id)?.try_into_bob()?.into();
pub fn build(self) -> Result<Swap> {
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,
})
}
}

View File

@ -394,22 +394,41 @@ struct BobParams {
}
impl BobParams {
pub async fn builder(
&self,
event_loop_handle: bob::EventLoopHandle,
swap_id: Uuid,
) -> Result<bob::Builder> {
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());