mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-07 14:02:32 -04:00
Disallow concurrent swaps
This commit is contained in:
parent
7d2b7bee92
commit
849e6e7a14
1 changed files with 38 additions and 16 deletions
|
@ -22,6 +22,11 @@ use structopt::lazy_static::lazy_static;
|
||||||
use tokio::sync::broadcast::Receiver;
|
use tokio::sync::broadcast::Receiver;
|
||||||
use tracing::{debug_span, Instrument};
|
use tracing::{debug_span, Instrument};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref SWAP_MUTEX: Mutex<Option<Uuid>> = Mutex::new(None);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
|
@ -161,7 +166,7 @@ impl Request {
|
||||||
.context("Could not get Tor SOCKS5 port")?,
|
.context("Could not get Tor SOCKS5 port")?,
|
||||||
behaviour,
|
behaviour,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
swarm.behaviour_mut().add_address(seller_peer_id, seller);
|
swarm.behaviour_mut().add_address(seller_peer_id, seller);
|
||||||
|
|
||||||
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
||||||
|
@ -182,7 +187,7 @@ impl Request {
|
||||||
|| bitcoin_wallet.sync(),
|
|| bitcoin_wallet.sync(),
|
||||||
estimate_fee,
|
estimate_fee,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(error) => match error.downcast::<ZeroQuoteReceived>() {
|
Err(error) => match error.downcast::<ZeroQuoteReceived>() {
|
||||||
|
@ -382,7 +387,7 @@ impl Request {
|
||||||
.context("Could not get Tor SOCKS5 port")?,
|
.context("Could not get Tor SOCKS5 port")?,
|
||||||
behaviour,
|
behaviour,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let our_peer_id = swarm.local_peer_id();
|
let our_peer_id = swarm.local_peer_id();
|
||||||
|
|
||||||
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
||||||
|
@ -417,7 +422,7 @@ impl Request {
|
||||||
event_loop_handle,
|
event_loop_handle,
|
||||||
monero_receive_address,
|
monero_receive_address,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
event_loop_result = handle => {
|
event_loop_result = handle => {
|
||||||
|
@ -442,7 +447,7 @@ impl Request {
|
||||||
Arc::clone(bitcoin_wallet),
|
Arc::clone(bitcoin_wallet),
|
||||||
Arc::clone(&context.db),
|
Arc::clone(&context.db),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
"result": state,
|
"result": state,
|
||||||
|
@ -470,7 +475,7 @@ impl Request {
|
||||||
.context("Could not get Tor SOCKS5 port")?,
|
.context("Could not get Tor SOCKS5 port")?,
|
||||||
identity,
|
identity,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
for seller in &sellers {
|
for seller in &sellers {
|
||||||
match seller.status {
|
match seller.status {
|
||||||
|
@ -558,10 +563,26 @@ impl Request {
|
||||||
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
||||||
// If the swap ID is set, we add it to the span
|
// If the swap ID is set, we add it to the span
|
||||||
let call_span = debug_span!(
|
let call_span = debug_span!(
|
||||||
"call",
|
"cmd",
|
||||||
method = ?self.cmd,
|
method = ?self.cmd,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(swap_id) = self.has_lockable_swap_id() {
|
||||||
|
println!("taking lock for swap_id: {}", swap_id);
|
||||||
|
let mut guard = SWAP_MUTEX.try_lock().context("Another swap is already running")?;
|
||||||
|
if guard.is_some() {
|
||||||
|
bail!("Another swap is already running");
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = guard.insert(swap_id.clone());
|
||||||
|
|
||||||
|
let result = self.handle_cmd(context).instrument(call_span).await;
|
||||||
|
guard.take();
|
||||||
|
|
||||||
|
println!("releasing lock for swap_id: {}", swap_id);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
self.handle_cmd(context).instrument(call_span).await
|
self.handle_cmd(context).instrument(call_span).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -584,15 +605,15 @@ pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
|
||||||
sync: FS,
|
sync: FS,
|
||||||
estimate_fee: FFE,
|
estimate_fee: FFE,
|
||||||
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
|
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
|
||||||
where
|
where
|
||||||
TB: Future<Output = Result<bitcoin::Amount>>,
|
TB: Future<Output = Result<bitcoin::Amount>>,
|
||||||
FB: Fn() -> TB,
|
FB: Fn() -> TB,
|
||||||
TMG: Future<Output = Result<bitcoin::Amount>>,
|
TMG: Future<Output = Result<bitcoin::Amount>>,
|
||||||
FMG: Fn() -> TMG,
|
FMG: Fn() -> TMG,
|
||||||
TS: Future<Output = Result<()>>,
|
TS: Future<Output = Result<()>>,
|
||||||
FS: Fn() -> TS,
|
FS: Fn() -> TS,
|
||||||
FFE: Fn(bitcoin::Amount) -> TFE,
|
FFE: Fn(bitcoin::Amount) -> TFE,
|
||||||
TFE: Future<Output = Result<bitcoin::Amount>>,
|
TFE: Future<Output = Result<bitcoin::Amount>>,
|
||||||
{
|
{
|
||||||
tracing::debug!("Requesting quote");
|
tracing::debug!("Requesting quote");
|
||||||
let bid_quote = bid_quote.await?;
|
let bid_quote = bid_quote.await?;
|
||||||
|
@ -667,3 +688,4 @@ where
|
||||||
|
|
||||||
Ok((btc_swap_amount, fees))
|
Ok((btc_swap_amount, fees))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue