Skip to content

Commit

Permalink
chore: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
delta1 committed Dec 18, 2023
1 parent fb4a0e2 commit 82cbea4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
4 changes: 2 additions & 2 deletions swap/src/asb/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub struct Arguments {
#[derive(Debug, PartialEq, Eq)]
pub enum Command {
Start {
resume_only: bool
resume_only: bool,
},
History,
Config,
Expand Down Expand Up @@ -266,7 +266,7 @@ pub enum RawCommand {
long = "resume-only",
help = "For maintenance only. When set, no new swap requests will be accepted, but existing unfinished swaps will be resumed."
)]
resume_only: bool
resume_only: bool,
},
#[structopt(about = "Prints swap-id and the state of each swap ever made.")]
History,
Expand Down
9 changes: 6 additions & 3 deletions swap/src/asb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use std::time::Duration;
use uuid::Uuid;

pub mod transport {
use libp2p::core::transport::OptionalTransport;
use crate::network::tor_transport::TorDialOnlyTransport;
use super::*;
use crate::network::tor_transport::TorDialOnlyTransport;
use libp2p::core::transport::OptionalTransport;

/// Creates the libp2p transport for the ASB.
pub fn new(
Expand All @@ -47,7 +47,10 @@ pub mod transport {
None => OptionalTransport::none(),
};

let transport = maybe_tor_transport.or_transport(tcp_with_dns).or_transport(websocket_with_dns).boxed();
let transport = maybe_tor_transport
.or_transport(tcp_with_dns)
.or_transport(websocket_with_dns)
.boxed();

authenticate_and_multiplex(transport, identity)
}
Expand Down
44 changes: 30 additions & 14 deletions swap/src/bin/asb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,24 @@ async fn main() -> Result<()> {
Ok(_) => {
tracing::info!("Setting up Tor hidden service");
let ac =
register_tor_services(config.network.clone().listen, tor_client, &seed)
.await?;
register_tor_services(config.network.clone().listen, tor_client, &seed).await?;
Some(ac)
}
Err(_) => {
tracing::warn!("Tor not found. Running on clear net");
None
}
};
let tor_port = if _ac.is_some() { config.tor.socks5_port } else { 0u16 };
let proxy_string = if tor_port != 0u16 { format!("127.0.0.1:{}", tor_port) } else { "".to_string() };
let tor_port = if _ac.is_some() {
config.tor.socks5_port
} else {
0u16
};
let proxy_string = if tor_port != 0u16 {
format!("127.0.0.1:{}", tor_port)
} else {
"".to_string()
};
if proxy_string.is_empty() {
tracing::info!(%proxy_string, "Not using SOCKS5 proxy");
} else {
Expand Down Expand Up @@ -163,7 +170,8 @@ async fn main() -> Result<()> {
}
}

let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(%bitcoin_balance, "Bitcoin wallet balance");

Expand All @@ -181,8 +189,9 @@ async fn main() -> Result<()> {
env_config,
namespace,
&rendezvous_addrs,
tor_port
).await?;
tor_port,
)
.await?;

for listen in config.network.listen.clone() {
Swarm::listen_on(&mut swarm, listen.clone())
Expand Down Expand Up @@ -248,7 +257,8 @@ async fn main() -> Result<()> {
println!("{}", config_json);
}
Command::WithdrawBtc { amount, address } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;

let amount = match amount {
Some(amount) => amount,
Expand All @@ -271,20 +281,23 @@ async fn main() -> Result<()> {
let monero_balance = monero_wallet.get_balance().await?;
tracing::info!(%monero_balance);

let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(%bitcoin_balance);
tracing::info!(%bitcoin_balance, %monero_balance, "Current balance");
}
Command::Cancel { swap_id } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;

let (txid, _) = cancel(swap_id, Arc::new(bitcoin_wallet), db).await?;

tracing::info!("Cancel transaction successfully published with id {}", txid);
}
Command::Refund { swap_id } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let monero_wallet = init_monero_wallet(&config, env_config).await?;

refund(
Expand All @@ -298,7 +311,8 @@ async fn main() -> Result<()> {
tracing::info!("Monero successfully refunded");
}
Command::Punish { swap_id } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;

let (txid, _) = punish(swap_id, Arc::new(bitcoin_wallet), db).await?;

Expand All @@ -313,7 +327,8 @@ async fn main() -> Result<()> {
swap_id,
do_not_await_finality,
} => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;

let (txid, _) = redeem(
swap_id,
Expand All @@ -326,7 +341,8 @@ async fn main() -> Result<()> {
tracing::info!("Redeem transaction successfully published with id {}", txid);
}
Command::ExportBitcoinWallet => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let bitcoin_wallet =
init_bitcoin_wallet(&config, &seed, env_config, proxy_string).await?;
let wallet_export = bitcoin_wallet.wallet_export("asb").await?;
println!("{}", wallet_export.to_string())
}
Expand Down
30 changes: 21 additions & 9 deletions swap/src/bitcoin/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ impl Wallet {
err => err?,
};

let client = Client::new(electrum_rpc_url, electrum_socks5_proxy_string, env_config.bitcoin_sync_interval())?;
let client = Client::new(
electrum_rpc_url,
electrum_socks5_proxy_string,
env_config.bitcoin_sync_interval(),
)?;

let network = wallet.network();

Expand Down Expand Up @@ -725,24 +729,32 @@ pub struct Client {
}

impl Client {
fn new(electrum_rpc_url: Url, electrum_socks5_proxy_string: &str, interval: Duration) -> Result<Self> {
let mut config_builder = bdk::electrum_client::ConfigBuilder::default()
.retry(5);
fn new(
electrum_rpc_url: Url,
electrum_socks5_proxy_string: &str,
interval: Duration,
) -> Result<Self> {
let mut config_builder = bdk::electrum_client::ConfigBuilder::default().retry(5);
if electrum_socks5_proxy_string.is_empty().not() {
config_builder = config_builder
.socks5(Option::from(Socks5Config::new(electrum_socks5_proxy_string.to_string()))).unwrap() // use Tor with the Electrum client
.socks5(Option::from(Socks5Config::new(
electrum_socks5_proxy_string.to_string(),
)))
.unwrap() // use Tor with the Electrum client
}
let config = config_builder.build();
let electrum = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config.clone())
.context("Failed to initialize Electrum RPC client")?;
let electrum =
bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config.clone())
.context("Failed to initialize Electrum RPC client")?;
// Initially fetch the latest block for storing the height.
// We do not act on this subscription after this call.
let latest_block = electrum
.block_headers_subscribe()
.context("Failed to subscribe to header notifications")?;

let client = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config.clone())
.context("Failed to initialize Electrum RPC client")?;
let client =
bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config.clone())
.context("Failed to initialize Electrum RPC client")?;
let blockchain = ElectrumBlockchain::from(client);
let last_sync = Instant::now()
.checked_sub(interval)
Expand Down
2 changes: 1 addition & 1 deletion swap/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn asb<LR>(
env_config: env::Config,
namespace: XmrBtcNamespace,
rendezvous_addrs: &[Multiaddr],
tor_socks5_port: u16
tor_socks5_port: u16,
) -> Result<Swarm<asb::Behaviour<LR>>>
where
LR: LatestRate + Send + 'static + Debug + Clone,
Expand Down

0 comments on commit 82cbea4

Please sign in to comment.