mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-24 23:19:34 -05:00
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:
parent
7f3aa644a0
commit
5e35904101
@ -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<Alice>;
|
||||
|
||||
#[cfg(not(feature = "tor"))]
|
||||
pub async fn swap(
|
||||
listen: Multiaddr,
|
||||
local_port: Option<u16>,
|
||||
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<SwapAmounts> = None;
|
||||
|
||||
@ -129,8 +107,7 @@ async fn _swap(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "tor")]
|
||||
fn new_swarm(listen: Multiaddr, port: u16) -> Result<Swarm> {
|
||||
fn new_swarm(listen: Multiaddr, port: Option<u16>) -> Result<Swarm> {
|
||||
use anyhow::Context as _;
|
||||
|
||||
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_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<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)]
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user