mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
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:
parent
e266fb07ef
commit
22bdc08c83
@ -27,7 +27,7 @@ use swap::env::{Config, GetConfig};
|
|||||||
use swap::network::quote::BidQuote;
|
use swap::network::quote::BidQuote;
|
||||||
use swap::network::swarm;
|
use swap::network::swarm;
|
||||||
use swap::protocol::bob;
|
use swap::protocol::bob;
|
||||||
use swap::protocol::bob::{Builder, EventLoop};
|
use swap::protocol::bob::{EventLoop, Swap};
|
||||||
use swap::seed::Seed;
|
use swap::seed::Seed;
|
||||||
use swap::{bitcoin, cli, env, monero};
|
use swap::{bitcoin, cli, env, monero};
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
@ -105,17 +105,16 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
db.insert_peer_id(swap_id, alice_peer_id).await?;
|
db.insert_peer_id(swap_id, alice_peer_id).await?;
|
||||||
|
|
||||||
let swap = Builder::new(
|
let swap = Swap::new(
|
||||||
db,
|
db,
|
||||||
swap_id,
|
swap_id,
|
||||||
bitcoin_wallet.clone(),
|
bitcoin_wallet,
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
env_config,
|
env_config,
|
||||||
event_loop_handle,
|
event_loop_handle,
|
||||||
receive_monero_address,
|
receive_monero_address,
|
||||||
)
|
send_bitcoin,
|
||||||
.with_init_params(send_bitcoin)
|
);
|
||||||
.build()?;
|
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
result = event_loop => {
|
result = event_loop => {
|
||||||
@ -182,16 +181,15 @@ async fn main() -> Result<()> {
|
|||||||
EventLoop::new(swap_id, swarm, alice_peer_id, bitcoin_wallet.clone())?;
|
EventLoop::new(swap_id, swarm, alice_peer_id, bitcoin_wallet.clone())?;
|
||||||
let handle = tokio::spawn(event_loop.run());
|
let handle = tokio::spawn(event_loop.run());
|
||||||
|
|
||||||
let swap = Builder::new(
|
let swap = Swap::from_db(
|
||||||
db,
|
db,
|
||||||
swap_id,
|
swap_id,
|
||||||
bitcoin_wallet.clone(),
|
bitcoin_wallet,
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
env_config,
|
env_config,
|
||||||
event_loop_handle,
|
event_loop_handle,
|
||||||
receive_monero_address,
|
receive_monero_address,
|
||||||
)
|
)?;
|
||||||
.build()?;
|
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
event_loop_result = handle => {
|
event_loop_result = handle => {
|
||||||
|
@ -30,71 +30,50 @@ pub struct Swap {
|
|||||||
pub receive_monero_address: monero::Address,
|
pub receive_monero_address: monero::Address,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Builder {
|
impl Swap {
|
||||||
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 {
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
db: Database,
|
db: Database,
|
||||||
swap_id: Uuid,
|
id: Uuid,
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
monero_wallet: Arc<monero::Wallet>,
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
env_config: env::Config,
|
env_config: env::Config,
|
||||||
event_loop_handle: EventLoopHandle,
|
event_loop_handle: EventLoopHandle,
|
||||||
receive_monero_address: monero::Address,
|
receive_monero_address: monero::Address,
|
||||||
|
btc_amount: bitcoin::Amount,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
swap_id,
|
state: BobState::Started { btc_amount },
|
||||||
|
event_loop_handle,
|
||||||
db,
|
db,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
monero_wallet,
|
monero_wallet,
|
||||||
init_params: InitParams::None,
|
|
||||||
env_config,
|
env_config,
|
||||||
event_loop_handle,
|
id,
|
||||||
receive_monero_address,
|
receive_monero_address,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_init_params(self, btc_amount: bitcoin::Amount) -> Self {
|
pub fn from_db(
|
||||||
Self {
|
db: Database,
|
||||||
init_params: InitParams::New { btc_amount },
|
id: Uuid,
|
||||||
..self
|
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> {
|
Ok(Self {
|
||||||
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 {
|
|
||||||
state,
|
state,
|
||||||
event_loop_handle: self.event_loop_handle,
|
event_loop_handle,
|
||||||
db: self.db,
|
db,
|
||||||
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
bitcoin_wallet,
|
||||||
monero_wallet: self.monero_wallet.clone(),
|
monero_wallet,
|
||||||
id: self.swap_id,
|
env_config,
|
||||||
env_config: self.env_config,
|
id,
|
||||||
receive_monero_address: self.receive_monero_address,
|
receive_monero_address,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -394,22 +394,41 @@ struct BobParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BobParams {
|
impl BobParams {
|
||||||
pub async fn builder(
|
pub fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, bob::EventLoop)> {
|
||||||
&self,
|
let (event_loop, handle) = self.new_eventloop(swap_id)?;
|
||||||
event_loop_handle: bob::EventLoopHandle,
|
let db = Database::open(&self.db_path)?;
|
||||||
swap_id: Uuid,
|
|
||||||
) -> Result<bob::Builder> {
|
|
||||||
let receive_address = self.monero_wallet.get_main_address();
|
|
||||||
|
|
||||||
Ok(bob::Builder::new(
|
let swap = bob::Swap::from_db(
|
||||||
Database::open(&self.db_path.clone().as_path()).unwrap(),
|
db,
|
||||||
swap_id,
|
swap_id,
|
||||||
self.bitcoin_wallet.clone(),
|
self.bitcoin_wallet.clone(),
|
||||||
self.monero_wallet.clone(),
|
self.monero_wallet.clone(),
|
||||||
self.env_config,
|
self.env_config,
|
||||||
event_loop_handle,
|
handle,
|
||||||
receive_address,
|
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)> {
|
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) {
|
pub async fn bob_swap(&mut self) -> (bob::Swap, BobApplicationHandle) {
|
||||||
let swap_id = Uuid::new_v4();
|
let (swap, event_loop) = self.bob_params.new_swap(self.btc_amount).unwrap();
|
||||||
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();
|
|
||||||
|
|
||||||
// ensure the wallet is up to date for concurrent swap tests
|
// ensure the wallet is up to date for concurrent swap tests
|
||||||
swap.bitcoin_wallet.sync().await.unwrap();
|
swap.bitcoin_wallet.sync().await.unwrap();
|
||||||
@ -520,15 +529,7 @@ impl TestContext {
|
|||||||
) -> (bob::Swap, BobApplicationHandle) {
|
) -> (bob::Swap, BobApplicationHandle) {
|
||||||
join_handle.abort();
|
join_handle.abort();
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = self.bob_params.new_eventloop(swap_id).unwrap();
|
let (swap, event_loop) = self.bob_params.new_swap_from_db(swap_id).unwrap();
|
||||||
|
|
||||||
let swap = self
|
|
||||||
.bob_params
|
|
||||||
.builder(event_loop_handle, swap_id)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let join_handle = tokio::spawn(event_loop.run());
|
let join_handle = tokio::spawn(event_loop.run());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user