Use an option for the local port

We can wrap the local port in an option and pass None when we are not using Tor.
This reduces code duplication.
This commit is contained in:
Tobin C. Harding 2020-10-28 08:38:10 +11:00
parent 7f3aa644a0
commit 5e35904101
2 changed files with 18 additions and 55 deletions

View File

@ -1,6 +1,6 @@
//! Run an XMR/BTC swap in the role of Alice. //! Run an XMR/BTC swap in the role of Alice.
//! Alice holds XMR and wishes receive BTC. //! Alice holds XMR and wishes receive BTC.
use anyhow::Result; use anyhow::{bail, Result};
use libp2p::{ use libp2p::{
core::{identity::Keypair, Multiaddr}, core::{identity::Keypair, Multiaddr},
request_response::ResponseChannel, request_response::ResponseChannel,
@ -28,35 +28,13 @@ use xmr_btc::{alice::State0, bob, monero};
pub type Swarm = libp2p::Swarm<Alice>; pub type Swarm = libp2p::Swarm<Alice>;
#[cfg(not(feature = "tor"))]
pub async fn swap( pub async fn swap(
listen: Multiaddr, listen: Multiaddr,
local_port: Option<u16>,
redeem_address: ::bitcoin::Address, redeem_address: ::bitcoin::Address,
punish_address: ::bitcoin::Address, punish_address: ::bitcoin::Address,
) -> Result<()> { ) -> Result<()> {
let swarm = new_swarm(listen)?; let mut swarm = new_swarm(listen, local_port)?;
_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 message0: bob::Message0; let message0: bob::Message0;
let mut last_amounts: Option<SwapAmounts> = None; let mut last_amounts: Option<SwapAmounts> = None;
@ -129,8 +107,7 @@ async fn _swap(
Ok(()) Ok(())
} }
#[cfg(feature = "tor")] fn new_swarm(listen: Multiaddr, port: Option<u16>) -> Result<Swarm> {
fn new_swarm(listen: Multiaddr, port: u16) -> Result<Swarm> {
use anyhow::Context as _; use anyhow::Context as _;
let behaviour = Alice::default(); let behaviour = Alice::default();
@ -138,7 +115,18 @@ fn new_swarm(listen: Multiaddr, port: u16) -> Result<Swarm> {
let local_key_pair = behaviour.identity(); let local_key_pair = behaviour.identity();
let local_peer_id = behaviour.peer_id(); 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()) let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone())
.executor(Box::new(TokioExecutor { .executor(Box::new(TokioExecutor {
@ -154,31 +142,6 @@ fn new_swarm(listen: Multiaddr, port: u16) -> Result<Swarm> {
Ok(swarm) Ok(swarm)
} }
#[cfg(not(feature = "tor"))]
fn new_swarm(listen: Multiaddr) -> Result<Swarm> {
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)] #[allow(clippy::large_enum_variant)]
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {

View File

@ -143,11 +143,11 @@ async fn swap_as_alice(
) -> Result<()> { ) -> Result<()> {
#[cfg(not(feature = "tor"))] #[cfg(not(feature = "tor"))]
{ {
alice::swap(addr, redeem, punish).await alice::swap(addr, None, redeem, punish).await
} }
#[cfg(feature = "tor")] #[cfg(feature = "tor")]
{ {
alice::swap(addr, PORT, redeem, punish).await alice::swap(addr, Some(PORT), redeem, punish).await
} }
} }