diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 72a05b76..bbaf00ac 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -1,6 +1,6 @@ //! Run an XMR/BTC swap in the role of Alice. //! Alice holds XMR and wishes receive BTC. -use anyhow::Result; +use anyhow::{bail, Result}; use libp2p::{ core::{identity::Keypair, Multiaddr}, request_response::ResponseChannel, @@ -28,35 +28,13 @@ use xmr_btc::{alice::State0, bob, monero}; pub type Swarm = libp2p::Swarm; -#[cfg(not(feature = "tor"))] pub async fn swap( listen: Multiaddr, + local_port: Option, redeem_address: ::bitcoin::Address, punish_address: ::bitcoin::Address, ) -> Result<()> { - let swarm = new_swarm(listen)?; - _swap(redeem_address, punish_address, swarm).await -} -#[cfg(feature = "tor")] -pub async fn swap( - listen: Multiaddr, - local_port: u16, - redeem_address: ::bitcoin::Address, - punish_address: ::bitcoin::Address, -) -> Result<()> { - let swarm = new_swarm(listen, local_port)?; - _swap(redeem_address, punish_address, swarm).await -} - -// TODO: After we have done some testing replace all the 'panic's with log -// statements or error returns. - -// FIXME: This whole function is horrible, needs total re-write. -async fn _swap( - redeem_address: ::bitcoin::Address, - punish_address: ::bitcoin::Address, - mut swarm: Swarm, -) -> Result<()> { + let mut swarm = new_swarm(listen, local_port)?; let message0: bob::Message0; let mut last_amounts: Option = None; @@ -129,8 +107,7 @@ async fn _swap( Ok(()) } -#[cfg(feature = "tor")] -fn new_swarm(listen: Multiaddr, port: u16) -> Result { +fn new_swarm(listen: Multiaddr, port: Option) -> Result { use anyhow::Context as _; let behaviour = Alice::default(); @@ -138,7 +115,18 @@ fn new_swarm(listen: Multiaddr, port: u16) -> Result { let local_key_pair = behaviour.identity(); let local_peer_id = behaviour.peer_id(); - let transport = transport::build(local_key_pair, Some((listen.clone(), port)))?; + let transport; + #[cfg(feature = "tor")] + { + transport = match port { + Some(port) => transport::build(local_key_pair, Some((listen.clone(), port)))?, + None => bail!("Must supply local port"), + }; + } + #[cfg(not(feature = "tor"))] + { + transport = transport::build(local_key_pair)?; + } let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone()) .executor(Box::new(TokioExecutor { @@ -154,31 +142,6 @@ fn new_swarm(listen: Multiaddr, port: u16) -> Result { Ok(swarm) } -#[cfg(not(feature = "tor"))] -fn new_swarm(listen: Multiaddr) -> Result { - use anyhow::Context as _; - - let behaviour = Alice::default(); - - let local_key_pair = behaviour.identity(); - let local_peer_id = behaviour.peer_id(); - - let transport = transport::build(local_key_pair)?; - - let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone()) - .executor(Box::new(TokioExecutor { - handle: tokio::runtime::Handle::current(), - })) - .build(); - - Swarm::listen_on(&mut swarm, listen.clone()) - .with_context(|| format!("Address is not supported: {:#}", listen))?; - - info!("Initialized swarm: {}", local_peer_id); - - Ok(swarm) -} - #[allow(clippy::large_enum_variant)] #[derive(Debug)] pub enum OutEvent { diff --git a/swap/src/main.rs b/swap/src/main.rs index 76a05526..a6d830e6 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -143,11 +143,11 @@ async fn swap_as_alice( ) -> Result<()> { #[cfg(not(feature = "tor"))] { - alice::swap(addr, redeem, punish).await + alice::swap(addr, None, redeem, punish).await } #[cfg(feature = "tor")] { - alice::swap(addr, PORT, redeem, punish).await + alice::swap(addr, Some(PORT), redeem, punish).await } }