mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-25 23:49:31 -05:00
Minor import optimizations
This commit is contained in:
parent
6d9b21cb47
commit
bc176bc4fb
@ -1,10 +1,9 @@
|
|||||||
|
mod amounts;
|
||||||
pub mod command;
|
pub mod command;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod fixed_rate;
|
pub mod fixed_rate;
|
||||||
pub mod kraken;
|
pub mod kraken;
|
||||||
|
|
||||||
mod amounts;
|
|
||||||
|
|
||||||
pub use amounts::Rate;
|
pub use amounts::Rate;
|
||||||
|
|
||||||
pub trait LatestRate {
|
pub trait LatestRate {
|
||||||
|
@ -7,14 +7,13 @@ pub use wallet::Wallet;
|
|||||||
pub use wallet_rpc::{WalletRpc, WalletRpcProcess};
|
pub use wallet_rpc::{WalletRpc, WalletRpcProcess};
|
||||||
|
|
||||||
use crate::bitcoin;
|
use crate::bitcoin;
|
||||||
use ::bitcoin::hashes::core::fmt::Formatter;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rand::{CryptoRng, RngCore};
|
use rand::{CryptoRng, RngCore};
|
||||||
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
|
use rust_decimal::prelude::*;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Display;
|
use std::fmt;
|
||||||
use std::ops::{Add, Mul, Sub};
|
use std::ops::{Add, Mul, Sub};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
@ -137,8 +136,8 @@ impl From<Amount> for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Amount {
|
impl fmt::Display for Amount {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut decimal = Decimal::from(self.0);
|
let mut decimal = Decimal::from(self.0);
|
||||||
decimal
|
decimal
|
||||||
.set_scale(12)
|
.set_scale(12)
|
||||||
|
@ -3,8 +3,8 @@ pub mod request_response;
|
|||||||
pub mod spot_price;
|
pub mod spot_price;
|
||||||
pub mod transport;
|
pub mod transport;
|
||||||
|
|
||||||
use futures::prelude::*;
|
|
||||||
use libp2p::core::Executor;
|
use libp2p::core::Executor;
|
||||||
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use tokio::runtime::Handle;
|
use tokio::runtime::Handle;
|
||||||
|
|
||||||
|
@ -17,13 +17,13 @@ pub enum OutEvent {
|
|||||||
/// peers we only ever connect to a single counterparty. Peer Tracker tracks
|
/// peers we only ever connect to a single counterparty. Peer Tracker tracks
|
||||||
/// that connection.
|
/// that connection.
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct PeerTracker {
|
pub struct Behaviour {
|
||||||
connected: Option<(PeerId, Multiaddr)>,
|
connected: Option<(PeerId, Multiaddr)>,
|
||||||
address_of_peer: HashMap<PeerId, Multiaddr>,
|
address_of_peer: HashMap<PeerId, Multiaddr>,
|
||||||
events: VecDeque<OutEvent>,
|
events: VecDeque<OutEvent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PeerTracker {
|
impl Behaviour {
|
||||||
/// Return whether we are connected to the given peer.
|
/// Return whether we are connected to the given peer.
|
||||||
pub fn is_connected(&self, peer_id: &PeerId) -> bool {
|
pub fn is_connected(&self, peer_id: &PeerId) -> bool {
|
||||||
if let Some((connected_peer_id, _)) = &self.connected {
|
if let Some((connected_peer_id, _)) = &self.connected {
|
||||||
@ -56,7 +56,7 @@ impl PeerTracker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NetworkBehaviour for PeerTracker {
|
impl NetworkBehaviour for Behaviour {
|
||||||
type ProtocolsHandler = DummyProtocolsHandler;
|
type ProtocolsHandler = DummyProtocolsHandler;
|
||||||
type OutEvent = OutEvent;
|
type OutEvent = OutEvent;
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ use libp2p::request_response::{
|
|||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub type OutEvent = RequestResponseEvent<SpotPriceRequest, SpotPriceResponse>;
|
pub type OutEvent = RequestResponseEvent<Request, Response>;
|
||||||
|
|
||||||
/// The spot price protocol allows parties to **initiate** a trade by requesting
|
/// The spot price protocol allows parties to **initiate** a trade by requesting
|
||||||
/// a spot price.
|
/// a spot price.
|
||||||
@ -27,18 +27,17 @@ impl ProtocolName for SpotPriceProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct SpotPriceRequest {
|
pub struct Request {
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
pub btc: bitcoin::Amount,
|
pub btc: bitcoin::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct SpotPriceResponse {
|
pub struct Response {
|
||||||
pub xmr: monero::Amount,
|
pub xmr: monero::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Behaviour =
|
pub type Behaviour = RequestResponse<CborCodec<SpotPriceProtocol, Request, Response>>;
|
||||||
RequestResponse<CborCodec<SpotPriceProtocol, SpotPriceRequest, SpotPriceResponse>>;
|
|
||||||
|
|
||||||
/// Constructs a new instance of the `spot-price` behaviour to be used by Alice.
|
/// Constructs a new instance of the `spot-price` behaviour to be used by Alice.
|
||||||
///
|
///
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::execution_params::ExecutionParams;
|
use crate::execution_params::ExecutionParams;
|
||||||
use crate::network::peer_tracker::PeerTracker;
|
use crate::network::spot_price::{Request, Response};
|
||||||
use crate::network::spot_price::{SpotPriceRequest, SpotPriceResponse};
|
|
||||||
use crate::network::{peer_tracker, spot_price};
|
use crate::network::{peer_tracker, spot_price};
|
||||||
use crate::protocol::alice::{
|
use crate::protocol::alice::{
|
||||||
encrypted_signature, execution_setup, transfer_proof, State0, State3, TransferProof,
|
encrypted_signature, execution_setup, transfer_proof, State0, State3, TransferProof,
|
||||||
@ -17,8 +16,8 @@ use tracing::debug;
|
|||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
ConnectionEstablished(PeerId),
|
ConnectionEstablished(PeerId),
|
||||||
SpotPriceRequested {
|
SpotPriceRequested {
|
||||||
msg: SpotPriceRequest,
|
msg: Request,
|
||||||
channel: ResponseChannel<SpotPriceResponse>,
|
channel: ResponseChannel<Response>,
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
},
|
},
|
||||||
ExecutionSetupDone {
|
ExecutionSetupDone {
|
||||||
@ -124,7 +123,7 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
|
|||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
pt: PeerTracker,
|
pt: peer_tracker::Behaviour,
|
||||||
spot_price: spot_price::Behaviour,
|
spot_price: spot_price::Behaviour,
|
||||||
execution_setup: execution_setup::Behaviour,
|
execution_setup: execution_setup::Behaviour,
|
||||||
transfer_proof: transfer_proof::Behaviour,
|
transfer_proof: transfer_proof::Behaviour,
|
||||||
@ -146,8 +145,8 @@ impl Default for Behaviour {
|
|||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
pub fn send_spot_price(
|
pub fn send_spot_price(
|
||||||
&mut self,
|
&mut self,
|
||||||
channel: ResponseChannel<SpotPriceResponse>,
|
channel: ResponseChannel<Response>,
|
||||||
response: SpotPriceResponse,
|
response: Response,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.spot_price
|
self.spot_price
|
||||||
.send_response(channel, response)
|
.send_response(channel, response)
|
||||||
|
@ -2,7 +2,7 @@ use crate::asb::LatestRate;
|
|||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use crate::execution_params::ExecutionParams;
|
use crate::execution_params::ExecutionParams;
|
||||||
use crate::monero::BalanceTooLow;
|
use crate::monero::BalanceTooLow;
|
||||||
use crate::network::spot_price::SpotPriceResponse;
|
use crate::network::spot_price::Response;
|
||||||
use crate::network::{transport, TokioExecutor};
|
use crate::network::{transport, TokioExecutor};
|
||||||
use crate::protocol::alice;
|
use crate::protocol::alice;
|
||||||
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State3, Swap, TransferProof};
|
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State3, Swap, TransferProof};
|
||||||
@ -171,7 +171,7 @@ where
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.swarm.send_spot_price(channel, SpotPriceResponse { xmr }) {
|
match self.swarm.send_spot_price(channel, Response { xmr }) {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// if we can't respond, the peer probably just disconnected so it is not a huge deal, only log this on debug
|
// if we can't respond, the peer probably just disconnected so it is not a huge deal, only log this on debug
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use crate::execution_params::ExecutionParams;
|
use crate::execution_params::ExecutionParams;
|
||||||
use crate::network::peer_tracker::{self, PeerTracker};
|
use crate::network::{peer_tracker, spot_price};
|
||||||
use crate::network::spot_price;
|
|
||||||
use crate::network::spot_price::{SpotPriceRequest, SpotPriceResponse};
|
|
||||||
use crate::protocol::alice::TransferProof;
|
use crate::protocol::alice::TransferProof;
|
||||||
use crate::protocol::bob;
|
|
||||||
use crate::{bitcoin, monero};
|
use crate::{bitcoin, monero};
|
||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
use libp2p::core::Multiaddr;
|
use libp2p::core::Multiaddr;
|
||||||
@ -33,7 +30,7 @@ mod transfer_proof;
|
|||||||
|
|
||||||
pub struct Swap {
|
pub struct Swap {
|
||||||
pub state: BobState,
|
pub state: BobState,
|
||||||
pub event_loop_handle: bob::EventLoopHandle,
|
pub event_loop_handle: EventLoopHandle,
|
||||||
pub db: Database,
|
pub db: Database,
|
||||||
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
pub monero_wallet: Arc<monero::Wallet>,
|
pub monero_wallet: Arc<monero::Wallet>,
|
||||||
@ -52,7 +49,7 @@ pub struct Builder {
|
|||||||
init_params: InitParams,
|
init_params: InitParams,
|
||||||
execution_params: ExecutionParams,
|
execution_params: ExecutionParams,
|
||||||
|
|
||||||
event_loop_handle: bob::EventLoopHandle,
|
event_loop_handle: EventLoopHandle,
|
||||||
|
|
||||||
receive_monero_address: ::monero::Address,
|
receive_monero_address: ::monero::Address,
|
||||||
}
|
}
|
||||||
@ -70,7 +67,7 @@ impl Builder {
|
|||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
monero_wallet: Arc<monero::Wallet>,
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
execution_params: ExecutionParams,
|
execution_params: ExecutionParams,
|
||||||
event_loop_handle: bob::EventLoopHandle,
|
event_loop_handle: EventLoopHandle,
|
||||||
receive_monero_address: ::monero::Address,
|
receive_monero_address: ::monero::Address,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -92,7 +89,7 @@ impl Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<bob::Swap> {
|
pub fn build(self) -> Result<Swap> {
|
||||||
let state = match self.init_params {
|
let state = match self.init_params {
|
||||||
InitParams::New { btc_amount } => BobState::Started { btc_amount },
|
InitParams::New { btc_amount } => BobState::Started { btc_amount },
|
||||||
InitParams::None => self.db.get_state(self.swap_id)?.try_into_bob()?.into(),
|
InitParams::None => self.db.get_state(self.swap_id)?.try_into_bob()?.into(),
|
||||||
@ -114,7 +111,7 @@ impl Builder {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
ConnectionEstablished(PeerId),
|
ConnectionEstablished(PeerId),
|
||||||
SpotPriceReceived(SpotPriceResponse),
|
SpotPriceReceived(spot_price::Response),
|
||||||
ExecutionSetupDone(Result<Box<State2>>),
|
ExecutionSetupDone(Result<Box<State2>>),
|
||||||
TransferProof {
|
TransferProof {
|
||||||
msg: Box<TransferProof>,
|
msg: Box<TransferProof>,
|
||||||
@ -208,7 +205,7 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
|
|||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
pt: PeerTracker,
|
pt: peer_tracker::Behaviour,
|
||||||
spot_price: spot_price::Behaviour,
|
spot_price: spot_price::Behaviour,
|
||||||
execution_setup: execution_setup::Behaviour,
|
execution_setup: execution_setup::Behaviour,
|
||||||
transfer_proof: transfer_proof::Behaviour,
|
transfer_proof: transfer_proof::Behaviour,
|
||||||
@ -228,7 +225,7 @@ impl Default for Behaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
pub fn request_spot_price(&mut self, alice: PeerId, request: SpotPriceRequest) {
|
pub fn request_spot_price(&mut self, alice: PeerId, request: spot_price::Request) {
|
||||||
let _ = self.spot_price.send_request(&alice, request);
|
let _ = self.spot_price.send_request(&alice, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::bitcoin::EncryptedSignature;
|
use crate::bitcoin::EncryptedSignature;
|
||||||
use crate::network::spot_price::{SpotPriceRequest, SpotPriceResponse};
|
use crate::network::spot_price::{Request, Response};
|
||||||
use crate::network::{transport, TokioExecutor};
|
use crate::network::{transport, TokioExecutor};
|
||||||
use crate::protocol::alice::TransferProof;
|
use crate::protocol::alice::TransferProof;
|
||||||
use crate::protocol::bob::{Behaviour, OutEvent, State0, State2};
|
use crate::protocol::bob::{Behaviour, OutEvent, State0, State2};
|
||||||
@ -34,13 +34,13 @@ impl<T> Default for Channels<T> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EventLoopHandle {
|
pub struct EventLoopHandle {
|
||||||
recv_spot_price: Receiver<SpotPriceResponse>,
|
recv_spot_price: Receiver<Response>,
|
||||||
start_execution_setup: Sender<State0>,
|
start_execution_setup: Sender<State0>,
|
||||||
done_execution_setup: Receiver<Result<State2>>,
|
done_execution_setup: Receiver<Result<State2>>,
|
||||||
recv_transfer_proof: Receiver<TransferProof>,
|
recv_transfer_proof: Receiver<TransferProof>,
|
||||||
conn_established: Receiver<PeerId>,
|
conn_established: Receiver<PeerId>,
|
||||||
dial_alice: Sender<()>,
|
dial_alice: Sender<()>,
|
||||||
request_spot_price: Sender<SpotPriceRequest>,
|
request_spot_price: Sender<Request>,
|
||||||
send_encrypted_signature: Sender<EncryptedSignature>,
|
send_encrypted_signature: Sender<EncryptedSignature>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,10 +75,7 @@ impl EventLoopHandle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn request_spot_price(&mut self, btc: bitcoin::Amount) -> Result<monero::Amount> {
|
pub async fn request_spot_price(&mut self, btc: bitcoin::Amount) -> Result<monero::Amount> {
|
||||||
let _ = self
|
let _ = self.request_spot_price.send(Request { btc }).await?;
|
||||||
.request_spot_price
|
|
||||||
.send(SpotPriceRequest { btc })
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let response = self
|
let response = self
|
||||||
.recv_spot_price
|
.recv_spot_price
|
||||||
@ -104,8 +101,8 @@ pub struct EventLoop {
|
|||||||
swarm: libp2p::Swarm<Behaviour>,
|
swarm: libp2p::Swarm<Behaviour>,
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
alice_peer_id: PeerId,
|
alice_peer_id: PeerId,
|
||||||
request_spot_price: Receiver<SpotPriceRequest>,
|
request_spot_price: Receiver<Request>,
|
||||||
recv_spot_price: Sender<SpotPriceResponse>,
|
recv_spot_price: Sender<Response>,
|
||||||
start_execution_setup: Receiver<State0>,
|
start_execution_setup: Receiver<State0>,
|
||||||
done_execution_setup: Sender<Result<State2>>,
|
done_execution_setup: Sender<Result<State2>>,
|
||||||
recv_transfer_proof: Sender<TransferProof>,
|
recv_transfer_proof: Sender<TransferProof>,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use atty::{self};
|
|
||||||
use tracing::{info, subscriber};
|
use tracing::{info, subscriber};
|
||||||
use tracing_log::LogTracer;
|
use tracing_log::LogTracer;
|
||||||
use tracing_subscriber::filter::LevelFilter;
|
use tracing_subscriber::filter::LevelFilter;
|
||||||
|
Loading…
Reference in New Issue
Block a user