mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-05-03 15:24:53 -04:00
Add isolated test for list_sellers
function
This commit is contained in:
parent
e642f5c148
commit
348fca0827
5 changed files with 182 additions and 6 deletions
|
@ -16,3 +16,6 @@ pub use recovery::redeem::{redeem, Finality};
|
||||||
pub use recovery::refund::refund;
|
pub use recovery::refund::refund;
|
||||||
pub use recovery::safely_abort::safely_abort;
|
pub use recovery::safely_abort::safely_abort;
|
||||||
pub use recovery::{cancel, refund};
|
pub use recovery::{cancel, refund};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub use network::rendezous;
|
||||||
|
|
|
@ -170,7 +170,7 @@ pub mod behaviour {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod rendezous {
|
pub mod rendezous {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
|
165
swap/src/cli.rs
165
swap/src/cli.rs
|
@ -12,3 +12,168 @@ pub use cancel::cancel;
|
||||||
pub use event_loop::{EventLoop, EventLoopHandle};
|
pub use event_loop::{EventLoop, EventLoopHandle};
|
||||||
pub use list_sellers::list_sellers;
|
pub use list_sellers::list_sellers;
|
||||||
pub use refund::refund;
|
pub use refund::refund;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::asb;
|
||||||
|
use crate::cli::list_sellers::Seller;
|
||||||
|
use crate::network::quote;
|
||||||
|
use crate::network::quote::BidQuote;
|
||||||
|
use crate::network::rendezvous::XmrBtcNamespace;
|
||||||
|
use crate::network::test::{new_swarm, SwarmExt};
|
||||||
|
use futures::StreamExt;
|
||||||
|
use libp2p::multiaddr::Protocol;
|
||||||
|
use libp2p::request_response::RequestResponseEvent;
|
||||||
|
use libp2p::swarm::{AddressScore, NetworkBehaviourEventProcess};
|
||||||
|
use libp2p::{identity, Multiaddr, PeerId};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn list_sellers_should_report_all_registered_asbs_with_a_quote() {
|
||||||
|
let namespace = XmrBtcNamespace::Mainnet;
|
||||||
|
let (rendezvous_address, rendezvous_peer_id) = setup_rendezvous_point().await;
|
||||||
|
let expected_seller_1 =
|
||||||
|
setup_asb(rendezvous_peer_id, rendezvous_address.clone(), namespace).await;
|
||||||
|
let expected_seller_2 =
|
||||||
|
setup_asb(rendezvous_peer_id, rendezvous_address.clone(), namespace).await;
|
||||||
|
|
||||||
|
let list_sellers = list_sellers(
|
||||||
|
rendezvous_peer_id,
|
||||||
|
rendezvous_address,
|
||||||
|
namespace,
|
||||||
|
0,
|
||||||
|
identity::Keypair::generate_ed25519(),
|
||||||
|
);
|
||||||
|
let sellers = tokio::time::timeout(Duration::from_secs(15), list_sellers)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
HashSet::<Seller>::from_iter(sellers),
|
||||||
|
HashSet::<Seller>::from_iter([expected_seller_1, expected_seller_2])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup_rendezvous_point() -> (Multiaddr, PeerId) {
|
||||||
|
let mut rendezvous_node = new_swarm(|_, identity| RendezvousPointBehaviour {
|
||||||
|
rendezvous: libp2p::rendezvous::Rendezvous::new(
|
||||||
|
identity,
|
||||||
|
libp2p::rendezvous::Config::default(),
|
||||||
|
),
|
||||||
|
ping: Default::default(),
|
||||||
|
});
|
||||||
|
let rendezvous_address = rendezvous_node.listen_on_tcp_localhost().await;
|
||||||
|
let rendezvous_peer_id = *rendezvous_node.local_peer_id();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
rendezvous_node.next().await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(rendezvous_address, rendezvous_peer_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup_asb(
|
||||||
|
rendezvous_peer_id: PeerId,
|
||||||
|
rendezvous_address: Multiaddr,
|
||||||
|
namespace: XmrBtcNamespace,
|
||||||
|
) -> Seller {
|
||||||
|
let static_quote = BidQuote {
|
||||||
|
price: bitcoin::Amount::from_sat(1337),
|
||||||
|
min_quantity: bitcoin::Amount::from_sat(42),
|
||||||
|
max_quantity: bitcoin::Amount::from_sat(9001),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut asb = new_swarm(|_, identity| StaticQuoteAsbBehaviour {
|
||||||
|
rendezvous: asb::rendezous::Behaviour::new(
|
||||||
|
identity,
|
||||||
|
rendezvous_peer_id,
|
||||||
|
rendezvous_address,
|
||||||
|
namespace,
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
ping: Default::default(),
|
||||||
|
quote: quote::asb(),
|
||||||
|
static_quote,
|
||||||
|
registered: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
let asb_address = asb.listen_on_tcp_localhost().await;
|
||||||
|
asb.add_external_address(asb_address.clone(), AddressScore::Infinite);
|
||||||
|
|
||||||
|
let asb_peer_id = *asb.local_peer_id();
|
||||||
|
|
||||||
|
// avoid race condition where `list_sellers` tries to discover before we are
|
||||||
|
// registered block this function until we are registered
|
||||||
|
while !asb.behaviour().registered {
|
||||||
|
asb.next().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
asb.next().await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Seller {
|
||||||
|
multiaddr: asb_address.with(Protocol::P2p(asb_peer_id.into())),
|
||||||
|
quote: static_quote,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(libp2p::NetworkBehaviour)]
|
||||||
|
struct StaticQuoteAsbBehaviour {
|
||||||
|
rendezvous: asb::rendezous::Behaviour,
|
||||||
|
// Support `Ping` as a workaround until https://github.com/libp2p/rust-libp2p/issues/2109 is fixed.
|
||||||
|
ping: libp2p::ping::Ping,
|
||||||
|
quote: quote::Behaviour,
|
||||||
|
|
||||||
|
#[behaviour(ignore)]
|
||||||
|
static_quote: BidQuote,
|
||||||
|
#[behaviour(ignore)]
|
||||||
|
registered: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NetworkBehaviourEventProcess<libp2p::rendezvous::Event> for StaticQuoteAsbBehaviour {
|
||||||
|
fn inject_event(&mut self, event: libp2p::rendezvous::Event) {
|
||||||
|
if let libp2p::rendezvous::Event::Registered { .. } = event {
|
||||||
|
self.registered = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for StaticQuoteAsbBehaviour {
|
||||||
|
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {}
|
||||||
|
}
|
||||||
|
impl NetworkBehaviourEventProcess<quote::OutEvent> for StaticQuoteAsbBehaviour {
|
||||||
|
fn inject_event(&mut self, event: quote::OutEvent) {
|
||||||
|
if let RequestResponseEvent::Message {
|
||||||
|
message: quote::Message::Request { channel, .. },
|
||||||
|
..
|
||||||
|
} = event
|
||||||
|
{
|
||||||
|
self.quote
|
||||||
|
.send_response(channel, self.static_quote)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(libp2p::NetworkBehaviour)]
|
||||||
|
struct RendezvousPointBehaviour {
|
||||||
|
rendezvous: libp2p::rendezvous::Rendezvous,
|
||||||
|
// Support `Ping` as a workaround until https://github.com/libp2p/rust-libp2p/issues/2109 is fixed.
|
||||||
|
ping: libp2p::ping::Ping,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NetworkBehaviourEventProcess<libp2p::rendezvous::Event> for RendezvousPointBehaviour {
|
||||||
|
fn inject_event(&mut self, _: libp2p::rendezvous::Event) {}
|
||||||
|
}
|
||||||
|
impl NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for RendezvousPointBehaviour {
|
||||||
|
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::rendezvous::XmrBtcNamespace;
|
use crate::network::rendezvous::XmrBtcNamespace;
|
||||||
use crate::network::{quote, swarm};
|
use crate::network::{quote, swarm};
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use libp2p::multiaddr::Protocol;
|
use libp2p::multiaddr::Protocol;
|
||||||
use libp2p::ping::{Ping, PingConfig, PingEvent};
|
use libp2p::ping::{Ping, PingConfig, PingEvent};
|
||||||
|
@ -32,7 +32,13 @@ pub async fn list_sellers(
|
||||||
};
|
};
|
||||||
let mut swarm = swarm::cli(identity, tor_socks5_port, behaviour).await?;
|
let mut swarm = swarm::cli(identity, tor_socks5_port, behaviour).await?;
|
||||||
|
|
||||||
let _ = swarm.dial_addr(rendezvous_node_addr.clone());
|
swarm
|
||||||
|
.behaviour_mut()
|
||||||
|
.quote
|
||||||
|
.add_address(&rendezvous_node_peer_id, rendezvous_node_addr.clone());
|
||||||
|
swarm
|
||||||
|
.dial(&rendezvous_node_peer_id)
|
||||||
|
.context("Failed to dial rendezvous node")?;
|
||||||
|
|
||||||
let event_loop = EventLoop::new(
|
let event_loop = EventLoop::new(
|
||||||
swarm,
|
swarm,
|
||||||
|
@ -46,7 +52,7 @@ pub async fn list_sellers(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
|
||||||
pub struct Seller {
|
pub struct Seller {
|
||||||
#[serde_as(as = "DisplayFromStr")]
|
#[serde_as(as = "DisplayFromStr")]
|
||||||
pub multiaddr: Multiaddr,
|
pub multiaddr: Multiaddr,
|
||||||
|
@ -87,6 +93,7 @@ enum QuoteStatus {
|
||||||
Received(BidQuote),
|
Received(BidQuote),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
enum State {
|
enum State {
|
||||||
WaitForDiscovery,
|
WaitForDiscovery,
|
||||||
WaitForQuoteCompletion,
|
WaitForQuoteCompletion,
|
||||||
|
@ -264,6 +271,7 @@ impl EventLoop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct StillPending {}
|
struct StillPending {}
|
||||||
|
|
||||||
impl From<PingEvent> for OutEvent {
|
impl From<PingEvent> for OutEvent {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
const PROTOCOL: &str = "/comit/xmr/btc/bid-quote/1.0.0";
|
const PROTOCOL: &str = "/comit/xmr/btc/bid-quote/1.0.0";
|
||||||
pub type OutEvent = RequestResponseEvent<(), BidQuote>;
|
pub type OutEvent = RequestResponseEvent<(), BidQuote>;
|
||||||
type Message = RequestResponseMessage<(), BidQuote>;
|
pub type Message = RequestResponseMessage<(), BidQuote>;
|
||||||
|
|
||||||
pub type Behaviour = RequestResponse<JsonPullCodec<BidQuoteProtocol, BidQuote>>;
|
pub type Behaviour = RequestResponse<JsonPullCodec<BidQuoteProtocol, BidQuote>>;
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ impl ProtocolName for BidQuoteProtocol {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a quote for buying XMR.
|
/// Represents a quote for buying XMR.
|
||||||
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct BidQuote {
|
pub struct BidQuote {
|
||||||
/// The price at which the maker is willing to buy at.
|
/// The price at which the maker is willing to buy at.
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue