Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach offer duration to quote #1662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion swap/src/asb/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use std::convert::{Infallible, TryInto};
use std::fmt::Debug;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use uuid::Uuid;

Expand All @@ -44,6 +45,7 @@ where
latest_rate: LR,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
external_redeem_address: Option<bitcoin::Address>,

swap_sender: mpsc::Sender<Swap>,
Expand Down Expand Up @@ -77,6 +79,7 @@ where
latest_rate: LR,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
external_redeem_address: Option<bitcoin::Address>,
) -> Result<(Self, mpsc::Receiver<Swap>)> {
let swap_channel = MpscChannels::default();
Expand All @@ -91,6 +94,7 @@ where
swap_sender: swap_channel.sender,
min_buy,
max_buy,
max_swap_timeout,
external_redeem_address,
recv_encrypted_signature: Default::default(),
inflight_encrypted_signatures: Default::default(),
Expand Down Expand Up @@ -186,7 +190,7 @@ where
tracing::warn!(%peer, "Ignoring spot price request: {}", error);
}
SwarmEvent::Behaviour(OutEvent::QuoteRequested { channel, peer }) => {
let quote = match self.make_quote(self.min_buy, self.max_buy).await {
let quote = match self.make_quote(self.min_buy, self.max_buy, self.max_swap_timeout).await {
Ok(quote) => quote,
Err(error) => {
tracing::warn!(%peer, "Failed to make quote: {:#}", error);
Expand Down Expand Up @@ -322,6 +326,7 @@ where
&mut self,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
) -> Result<BidQuote> {
let ask_price = self
.latest_rate
Expand Down Expand Up @@ -351,6 +356,7 @@ where
price: ask_price,
min_quantity: bitcoin::Amount::ZERO,
max_quantity: bitcoin::Amount::ZERO,
valid_duration: Some(max_swap_timeout),
});
}

Expand All @@ -363,13 +369,15 @@ where
price: ask_price,
min_quantity: min_buy,
max_quantity: max_bitcoin_for_monero,
valid_duration: Some(max_swap_timeout),
});
}

Ok(BidQuote {
price: ask_price,
min_quantity: min_buy,
max_quantity: max_buy,
valid_duration: Some(max_swap_timeout),
})
}

Expand Down
3 changes: 3 additions & 0 deletions swap/src/asb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ pub mod behaviour {
where
LR: LatestRate + Send + 'static,
{
#[allow(clippy::too_many_arguments)]
pub fn new(
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
latest_rate: LR,
resume_only: bool,
env_config: env::Config,
Expand All @@ -154,6 +156,7 @@ pub mod behaviour {
swap_setup: alice::Behaviour::new(
min_buy,
max_buy,
max_swap_timeout,
env_config,
latest_rate,
resume_only,
Expand Down
3 changes: 3 additions & 0 deletions swap/src/bin/asb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::convert::TryInto;
use std::env;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
use structopt::clap;
use structopt::clap::ErrorKind;
use swap::asb::command::{parse_args, Arguments, Command};
Expand Down Expand Up @@ -170,6 +171,7 @@ async fn main() -> Result<()> {
&seed,
config.maker.min_buy_btc,
config.maker.max_buy_btc,
Duration::from_secs(120),
kraken_rate.clone(),
resume_only,
env_config,
Expand Down Expand Up @@ -201,6 +203,7 @@ async fn main() -> Result<()> {
kraken_rate.clone(),
config.maker.min_buy_btc,
config.maker.max_buy_btc,
Duration::from_secs(120),
config.maker.external_bitcoin_redeem_address,
)
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions swap/src/bin/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ mod tests {
price: Amount::from_btc(0.001).unwrap(),
max_quantity: Amount::from_btc(btc).unwrap(),
min_quantity: Amount::ZERO,
valid_duration: Some(Duration::from_secs(120)),
}
}

Expand All @@ -411,6 +412,7 @@ mod tests {
price: Amount::from_btc(0.001).unwrap(),
max_quantity: Amount::max_value(),
min_quantity: Amount::from_btc(btc).unwrap(),
valid_duration: Some(Duration::from_secs(120)),
}
}

Expand Down
1 change: 1 addition & 0 deletions swap/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod tests {
price: bitcoin::Amount::from_sat(1337),
min_quantity: bitcoin::Amount::from_sat(42),
max_quantity: bitcoin::Amount::from_sat(9001),
valid_duration: Some(Duration::from_secs(120)),
};

let mut asb = new_swarm(|_, identity| {
Expand Down
2 changes: 2 additions & 0 deletions swap/src/cli/list_sellers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ mod tests {
price: Default::default(),
min_quantity: Default::default(),
max_quantity: Default::default(),
valid_duration: Some(Duration::from_secs(120)),
}),
},
];
Expand All @@ -359,6 +360,7 @@ mod tests {
price: Default::default(),
min_quantity: Default::default(),
max_quantity: Default::default(),
valid_duration: Some(Duration::from_secs(120)),
})
},
Seller {
Expand Down
2 changes: 2 additions & 0 deletions swap/src/network/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use libp2p::request_response::{
};
use libp2p::PeerId;
use serde::{Deserialize, Serialize};
use std::time::Duration;

const PROTOCOL: &str = "/comit/xmr/btc/bid-quote/1.0.0";
pub type OutEvent = RequestResponseEvent<(), BidQuote>;
Expand Down Expand Up @@ -35,6 +36,7 @@ pub struct BidQuote {
/// The maximum quantity the maker is willing to buy.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub max_quantity: bitcoin::Amount,
pub valid_duration: Option<Duration>,
}

#[derive(Clone, Copy, Debug, thiserror::Error)]
Expand Down
13 changes: 9 additions & 4 deletions swap/src/network/swap_setup/alice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct Behaviour<LR> {
events: VecDeque<OutEvent>,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
env_config: env::Config,

latest_rate: LR,
Expand All @@ -128,6 +129,7 @@ impl<LR> Behaviour<LR> {
pub fn new(
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
env_config: env::Config,
latest_rate: LR,
resume_only: bool,
Expand All @@ -136,6 +138,7 @@ impl<LR> Behaviour<LR> {
events: Default::default(),
min_buy,
max_buy,
max_swap_timeout,
env_config,
latest_rate,
resume_only,
Expand All @@ -154,6 +157,7 @@ where
Handler::new(
self.min_buy,
self.max_buy,
self.max_swap_timeout,
self.env_config,
self.latest_rate.clone(),
self.resume_only,
Expand Down Expand Up @@ -214,14 +218,15 @@ pub struct Handler<LR> {
latest_rate: LR,
resume_only: bool,

timeout: Duration,
max_swap_timeout: Duration,
keep_alive: KeepAlive,
}

impl<LR> Handler<LR> {
fn new(
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
env_config: env::Config,
latest_rate: LR,
resume_only: bool,
Expand All @@ -234,7 +239,7 @@ impl<LR> Handler<LR> {
env_config,
latest_rate,
resume_only,
timeout: Duration::from_secs(120),
max_swap_timeout,
keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(10)),
}
}
Expand Down Expand Up @@ -280,7 +285,7 @@ where
let latest_rate = self.latest_rate.latest_rate();
let env_config = self.env_config;

let protocol = tokio::time::timeout(self.timeout, async move {
let protocol = tokio::time::timeout(self.max_swap_timeout, async move {
let request = swap_setup::read_cbor_message::<SpotPriceRequest>(&mut substream)
.await
.context("Failed to read spot price request")?;
Expand Down Expand Up @@ -404,7 +409,7 @@ where
Ok((swap_id, state3))
});

let max_seconds = self.timeout.as_secs();
let max_seconds = self.max_swap_timeout.as_secs();
self.inbound_stream = OptionFuture::from(Some(
async move {
protocol.await.with_context(|| {
Expand Down
3 changes: 3 additions & 0 deletions swap/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use anyhow::Result;
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder};
use libp2p::{identity, Multiaddr, Swarm};
use std::fmt::Debug;
use std::time::Duration;

#[allow(clippy::too_many_arguments)]
pub fn asb<LR>(
seed: &Seed,
min_buy: bitcoin::Amount,
max_buy: bitcoin::Amount,
max_swap_timeout: Duration,
latest_rate: LR,
resume_only: bool,
env_config: env::Config,
Expand All @@ -38,6 +40,7 @@ where
let behaviour = asb::Behaviour::new(
min_buy,
max_buy,
max_swap_timeout,
latest_rate,
resume_only,
env_config,
Expand Down
2 changes: 2 additions & 0 deletions swap/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ async fn start_alice(
seed,
min_buy,
max_buy,
Duration::from_secs(120),
latest_rate,
resume_only,
env_config,
Expand All @@ -260,6 +261,7 @@ async fn start_alice(
FixedRate::default(),
min_buy,
max_buy,
Duration::from_secs(120),
None,
)
.unwrap();
Expand Down
Loading