mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-13 00:35:37 -04:00
fixup! Add the list-sellers
command to the CLI
This commit is contained in:
parent
dcf5923d3e
commit
adc5e926ce
1 changed files with 78 additions and 26 deletions
|
@ -4,11 +4,13 @@ use crate::rendezvous::XmrBtcNamespace;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use libp2p::multiaddr::Protocol;
|
use libp2p::multiaddr::Protocol;
|
||||||
|
use libp2p::ping::{Ping, PingConfig, PingEvent};
|
||||||
use libp2p::rendezvous::{Namespace, Rendezvous};
|
use libp2p::rendezvous::{Namespace, Rendezvous};
|
||||||
use libp2p::request_response::{RequestResponseEvent, RequestResponseMessage};
|
use libp2p::request_response::{RequestResponseEvent, RequestResponseMessage};
|
||||||
use libp2p::swarm::SwarmEvent;
|
use libp2p::swarm::SwarmEvent;
|
||||||
use libp2p::{identity, rendezvous, Multiaddr, PeerId, Swarm};
|
use libp2p::{identity, rendezvous, Multiaddr, PeerId, Swarm};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
pub async fn list_sellers(
|
pub async fn list_sellers(
|
||||||
rendezvous_node_peer_id: PeerId,
|
rendezvous_node_peer_id: PeerId,
|
||||||
|
@ -20,6 +22,11 @@ pub async fn list_sellers(
|
||||||
let behaviour = Behaviour {
|
let behaviour = Behaviour {
|
||||||
rendezvous: Rendezvous::new(identity.clone(), rendezvous::Config::default()),
|
rendezvous: Rendezvous::new(identity.clone(), rendezvous::Config::default()),
|
||||||
quote: quote::cli(),
|
quote: quote::cli(),
|
||||||
|
ping: Ping::new(
|
||||||
|
PingConfig::new()
|
||||||
|
.with_keep_alive(false)
|
||||||
|
.with_interval(Duration::from_secs(3_154_000_000)),
|
||||||
|
),
|
||||||
};
|
};
|
||||||
let mut swarm = swarm::cli(identity, tor_socks5_port, behaviour).await?;
|
let mut swarm = swarm::cli(identity, tor_socks5_port, behaviour).await?;
|
||||||
|
|
||||||
|
@ -46,6 +53,7 @@ pub struct Seller {
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
Rendezvous(rendezvous::Event),
|
Rendezvous(rendezvous::Event),
|
||||||
Quote(quote::OutEvent),
|
Quote(quote::OutEvent),
|
||||||
|
Ping(PingEvent),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<rendezvous::Event> for OutEvent {
|
impl From<rendezvous::Event> for OutEvent {
|
||||||
|
@ -66,6 +74,7 @@ impl From<quote::OutEvent> for OutEvent {
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
pub rendezvous: Rendezvous,
|
pub rendezvous: Rendezvous,
|
||||||
pub quote: quote::Behaviour,
|
pub quote: quote::Behaviour,
|
||||||
|
pub ping: Ping,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -74,6 +83,11 @@ enum QuoteStatus {
|
||||||
Received(BidQuote),
|
Received(BidQuote),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
WaitForDiscovery,
|
||||||
|
WaitForQuoteCompletion,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct EventLoop {
|
pub struct EventLoop {
|
||||||
swarm: Swarm<Behaviour>,
|
swarm: Swarm<Behaviour>,
|
||||||
rendezvous_peer_id: PeerId,
|
rendezvous_peer_id: PeerId,
|
||||||
|
@ -81,6 +95,7 @@ pub struct EventLoop {
|
||||||
namespace: XmrBtcNamespace,
|
namespace: XmrBtcNamespace,
|
||||||
asb_address: HashMap<PeerId, Multiaddr>,
|
asb_address: HashMap<PeerId, Multiaddr>,
|
||||||
asb_quote_status: HashMap<PeerId, QuoteStatus>,
|
asb_quote_status: HashMap<PeerId, QuoteStatus>,
|
||||||
|
state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoop {
|
impl EventLoop {
|
||||||
|
@ -97,6 +112,7 @@ impl EventLoop {
|
||||||
namespace,
|
namespace,
|
||||||
asb_address: Default::default(),
|
asb_address: Default::default(),
|
||||||
asb_quote_status: Default::default(),
|
asb_quote_status: Default::default(),
|
||||||
|
state: State::WaitForDiscovery,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,20 +139,32 @@ impl EventLoop {
|
||||||
self.asb_address.insert(peer_id, address.clone());
|
self.asb_address.insert(peer_id, address.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SwarmEvent::UnreachableAddr { error, address, .. }
|
SwarmEvent::UnreachableAddr { peer_id, error, address, .. } => {
|
||||||
| SwarmEvent::UnknownPeerUnreachableAddr { error, address, .. } => {
|
|
||||||
if address == self.rendezvous_addr {
|
if address == self.rendezvous_addr {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
"Failed to connect to rendezvous point at {}: {}",
|
"Failed to connect to rendezvous point at {}: {}",
|
||||||
address,
|
address,
|
||||||
error
|
error
|
||||||
);
|
);
|
||||||
todo!("Better error handling, return with error")
|
|
||||||
|
// if the rendezvous node is unreachable we just stop
|
||||||
|
return Vec::new();
|
||||||
|
} else {
|
||||||
|
tracing::debug!(
|
||||||
|
"Failed to connect to peer at {}: {}",
|
||||||
|
address,
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
// if a different peer than the rendezvous node is unreachable (i.e. a seller) we remove that seller from the quote status state
|
||||||
|
self.asb_quote_status.remove(&peer_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::Rendezvous(
|
SwarmEvent::Behaviour(OutEvent::Rendezvous(
|
||||||
rendezvous::Event::Discovered { registrations, .. },
|
rendezvous::Event::Discovered { registrations, .. },
|
||||||
)) => {
|
)) => {
|
||||||
|
self.state = State::WaitForQuoteCompletion;
|
||||||
|
|
||||||
for registration in registrations {
|
for registration in registrations {
|
||||||
let peer = registration.record.peer_id();
|
let peer = registration.record.peer_id();
|
||||||
for address in registration.record.addresses() {
|
for address in registration.record.addresses() {
|
||||||
|
@ -175,10 +203,21 @@ impl EventLoop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RequestResponseEvent::OutboundFailure { peer, error, .. } => {
|
RequestResponseEvent::OutboundFailure { peer, error, .. } => {
|
||||||
|
if peer == self.rendezvous_peer_id {
|
||||||
|
tracing::debug!(%peer, "Outbound failure when communicating with rendezvous node: {:#}", error);
|
||||||
|
} else {
|
||||||
tracing::debug!(%peer, "Ignoring seller, because unable to request quote: {:#}", error);
|
tracing::debug!(%peer, "Ignoring seller, because unable to request quote: {:#}", error);
|
||||||
self.asb_quote_status.remove(&peer);
|
self.asb_quote_status.remove(&peer);
|
||||||
}
|
}
|
||||||
RequestResponseEvent::InboundFailure { .. } => unreachable!(),
|
}
|
||||||
|
RequestResponseEvent::InboundFailure { peer, error, .. } => {
|
||||||
|
if peer == self.rendezvous_peer_id {
|
||||||
|
tracing::debug!(%peer, "Inbound failure when communicating with rendezvous node: {:#}", error);
|
||||||
|
} else {
|
||||||
|
tracing::debug!(%peer, "Ignoring seller, because unable to request quote: {:#}", error);
|
||||||
|
self.asb_quote_status.remove(&peer);
|
||||||
|
}
|
||||||
|
},
|
||||||
RequestResponseEvent::ResponseSent { .. } => unreachable!()
|
RequestResponseEvent::ResponseSent { .. } => unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,6 +226,11 @@ impl EventLoop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match self.state {
|
||||||
|
State::WaitForDiscovery => {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
State::WaitForQuoteCompletion => {
|
||||||
let all_quotes_fetched = self
|
let all_quotes_fetched = self
|
||||||
.asb_quote_status
|
.asb_quote_status
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -213,6 +257,14 @@ impl EventLoop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StillPending {}
|
struct StillPending {}
|
||||||
|
|
||||||
|
impl From<PingEvent> for OutEvent {
|
||||||
|
fn from(event: PingEvent) -> Self {
|
||||||
|
OutEvent::Ping(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue