Minor import optimizations

This commit is contained in:
Thomas Eizinger 2021-03-04 11:40:28 +11:00
parent 6d9b21cb47
commit bc176bc4fb
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
10 changed files with 35 additions and 46 deletions

View File

@ -1,10 +1,9 @@
mod amounts;
pub mod command;
pub mod config;
pub mod fixed_rate;
pub mod kraken;
mod amounts;
pub use amounts::Rate;
pub trait LatestRate {

View File

@ -7,14 +7,13 @@ pub use wallet::Wallet;
pub use wallet_rpc::{WalletRpc, WalletRpcProcess};
use crate::bitcoin;
use ::bitcoin::hashes::core::fmt::Formatter;
use anyhow::Result;
use rand::{CryptoRng, RngCore};
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
use rust_decimal::prelude::*;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt::Display;
use std::fmt;
use std::ops::{Add, Mul, Sub};
use std::str::FromStr;
@ -137,8 +136,8 @@ impl From<Amount> for u64 {
}
}
impl Display for Amount {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut decimal = Decimal::from(self.0);
decimal
.set_scale(12)

View File

@ -3,8 +3,8 @@ pub mod request_response;
pub mod spot_price;
pub mod transport;
use futures::prelude::*;
use libp2p::core::Executor;
use std::future::Future;
use std::pin::Pin;
use tokio::runtime::Handle;

View File

@ -17,13 +17,13 @@ pub enum OutEvent {
/// peers we only ever connect to a single counterparty. Peer Tracker tracks
/// that connection.
#[derive(Default, Debug)]
pub struct PeerTracker {
pub struct Behaviour {
connected: Option<(PeerId, Multiaddr)>,
address_of_peer: HashMap<PeerId, Multiaddr>,
events: VecDeque<OutEvent>,
}
impl PeerTracker {
impl Behaviour {
/// Return whether we are connected to the given peer.
pub fn is_connected(&self, peer_id: &PeerId) -> bool {
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 OutEvent = OutEvent;

View File

@ -6,7 +6,7 @@ use libp2p::request_response::{
};
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
/// a spot price.
@ -27,18 +27,17 @@ impl ProtocolName for SpotPriceProtocol {
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SpotPriceRequest {
pub struct Request {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SpotPriceResponse {
pub struct Response {
pub xmr: monero::Amount,
}
pub type Behaviour =
RequestResponse<CborCodec<SpotPriceProtocol, SpotPriceRequest, SpotPriceResponse>>;
pub type Behaviour = RequestResponse<CborCodec<SpotPriceProtocol, Request, Response>>;
/// Constructs a new instance of the `spot-price` behaviour to be used by Alice.
///

View File

@ -1,6 +1,5 @@
use crate::execution_params::ExecutionParams;
use crate::network::peer_tracker::PeerTracker;
use crate::network::spot_price::{SpotPriceRequest, SpotPriceResponse};
use crate::network::spot_price::{Request, Response};
use crate::network::{peer_tracker, spot_price};
use crate::protocol::alice::{
encrypted_signature, execution_setup, transfer_proof, State0, State3, TransferProof,
@ -17,8 +16,8 @@ use tracing::debug;
pub enum OutEvent {
ConnectionEstablished(PeerId),
SpotPriceRequested {
msg: SpotPriceRequest,
channel: ResponseChannel<SpotPriceResponse>,
msg: Request,
channel: ResponseChannel<Response>,
peer: PeerId,
},
ExecutionSetupDone {
@ -124,7 +123,7 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
#[behaviour(out_event = "OutEvent", event_process = false)]
#[allow(missing_debug_implementations)]
pub struct Behaviour {
pt: PeerTracker,
pt: peer_tracker::Behaviour,
spot_price: spot_price::Behaviour,
execution_setup: execution_setup::Behaviour,
transfer_proof: transfer_proof::Behaviour,
@ -146,8 +145,8 @@ impl Default for Behaviour {
impl Behaviour {
pub fn send_spot_price(
&mut self,
channel: ResponseChannel<SpotPriceResponse>,
response: SpotPriceResponse,
channel: ResponseChannel<Response>,
response: Response,
) -> Result<()> {
self.spot_price
.send_response(channel, response)

View File

@ -2,7 +2,7 @@ use crate::asb::LatestRate;
use crate::database::Database;
use crate::execution_params::ExecutionParams;
use crate::monero::BalanceTooLow;
use crate::network::spot_price::SpotPriceResponse;
use crate::network::spot_price::Response;
use crate::network::{transport, TokioExecutor};
use crate::protocol::alice;
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(_) => {},
Err(e) => {
// if we can't respond, the peer probably just disconnected so it is not a huge deal, only log this on debug

View File

@ -1,10 +1,7 @@
use crate::database::Database;
use crate::execution_params::ExecutionParams;
use crate::network::peer_tracker::{self, PeerTracker};
use crate::network::spot_price;
use crate::network::spot_price::{SpotPriceRequest, SpotPriceResponse};
use crate::network::{peer_tracker, spot_price};
use crate::protocol::alice::TransferProof;
use crate::protocol::bob;
use crate::{bitcoin, monero};
use anyhow::{anyhow, Error, Result};
use libp2p::core::Multiaddr;
@ -33,7 +30,7 @@ mod transfer_proof;
pub struct Swap {
pub state: BobState,
pub event_loop_handle: bob::EventLoopHandle,
pub event_loop_handle: EventLoopHandle,
pub db: Database,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
@ -52,7 +49,7 @@ pub struct Builder {
init_params: InitParams,
execution_params: ExecutionParams,
event_loop_handle: bob::EventLoopHandle,
event_loop_handle: EventLoopHandle,
receive_monero_address: ::monero::Address,
}
@ -70,7 +67,7 @@ impl Builder {
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
execution_params: ExecutionParams,
event_loop_handle: bob::EventLoopHandle,
event_loop_handle: EventLoopHandle,
receive_monero_address: ::monero::Address,
) -> 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 {
InitParams::New { btc_amount } => BobState::Started { btc_amount },
InitParams::None => self.db.get_state(self.swap_id)?.try_into_bob()?.into(),
@ -114,7 +111,7 @@ impl Builder {
#[derive(Debug)]
pub enum OutEvent {
ConnectionEstablished(PeerId),
SpotPriceReceived(SpotPriceResponse),
SpotPriceReceived(spot_price::Response),
ExecutionSetupDone(Result<Box<State2>>),
TransferProof {
msg: Box<TransferProof>,
@ -208,7 +205,7 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
#[behaviour(out_event = "OutEvent", event_process = false)]
#[allow(missing_debug_implementations)]
pub struct Behaviour {
pt: PeerTracker,
pt: peer_tracker::Behaviour,
spot_price: spot_price::Behaviour,
execution_setup: execution_setup::Behaviour,
transfer_proof: transfer_proof::Behaviour,
@ -228,7 +225,7 @@ impl Default for 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);
}

View File

@ -1,5 +1,5 @@
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::protocol::alice::TransferProof;
use crate::protocol::bob::{Behaviour, OutEvent, State0, State2};
@ -34,13 +34,13 @@ impl<T> Default for Channels<T> {
#[derive(Debug)]
pub struct EventLoopHandle {
recv_spot_price: Receiver<SpotPriceResponse>,
recv_spot_price: Receiver<Response>,
start_execution_setup: Sender<State0>,
done_execution_setup: Receiver<Result<State2>>,
recv_transfer_proof: Receiver<TransferProof>,
conn_established: Receiver<PeerId>,
dial_alice: Sender<()>,
request_spot_price: Sender<SpotPriceRequest>,
request_spot_price: Sender<Request>,
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> {
let _ = self
.request_spot_price
.send(SpotPriceRequest { btc })
.await?;
let _ = self.request_spot_price.send(Request { btc }).await?;
let response = self
.recv_spot_price
@ -104,8 +101,8 @@ pub struct EventLoop {
swarm: libp2p::Swarm<Behaviour>,
bitcoin_wallet: Arc<bitcoin::Wallet>,
alice_peer_id: PeerId,
request_spot_price: Receiver<SpotPriceRequest>,
recv_spot_price: Sender<SpotPriceResponse>,
request_spot_price: Receiver<Request>,
recv_spot_price: Sender<Response>,
start_execution_setup: Receiver<State0>,
done_execution_setup: Sender<Result<State2>>,
recv_transfer_proof: Sender<TransferProof>,

View File

@ -1,5 +1,4 @@
use anyhow::Result;
use atty::{self};
use tracing::{info, subscriber};
use tracing_log::LogTracer;
use tracing_subscriber::filter::LevelFilter;