From bc176bc4fbdee9ec4b5b904cecadb8dfa5284a9d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 4 Mar 2021 11:40:28 +1100 Subject: [PATCH] Minor import optimizations --- swap/src/asb.rs | 3 +-- swap/src/monero.rs | 9 ++++----- swap/src/network.rs | 2 +- swap/src/network/peer_tracker.rs | 6 +++--- swap/src/network/spot_price.rs | 9 ++++----- swap/src/protocol/alice/behaviour.rs | 13 ++++++------- swap/src/protocol/alice/event_loop.rs | 4 ++-- swap/src/protocol/bob.rs | 19 ++++++++----------- swap/src/protocol/bob/event_loop.rs | 15 ++++++--------- swap/src/trace.rs | 1 - 10 files changed, 35 insertions(+), 46 deletions(-) diff --git a/swap/src/asb.rs b/swap/src/asb.rs index 77006526..78ac0dde 100644 --- a/swap/src/asb.rs +++ b/swap/src/asb.rs @@ -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 { diff --git a/swap/src/monero.rs b/swap/src/monero.rs index 45229ea7..d086cfa7 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -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 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) diff --git a/swap/src/network.rs b/swap/src/network.rs index dd9c88eb..67f39f0e 100644 --- a/swap/src/network.rs +++ b/swap/src/network.rs @@ -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; diff --git a/swap/src/network/peer_tracker.rs b/swap/src/network/peer_tracker.rs index 26d6e172..36a536ce 100644 --- a/swap/src/network/peer_tracker.rs +++ b/swap/src/network/peer_tracker.rs @@ -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, events: VecDeque, } -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; diff --git a/swap/src/network/spot_price.rs b/swap/src/network/spot_price.rs index f02b0487..3fcd9373 100644 --- a/swap/src/network/spot_price.rs +++ b/swap/src/network/spot_price.rs @@ -6,7 +6,7 @@ use libp2p::request_response::{ }; use serde::{Deserialize, Serialize}; -pub type OutEvent = RequestResponseEvent; +pub type OutEvent = RequestResponseEvent; /// 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>; +pub type Behaviour = RequestResponse>; /// Constructs a new instance of the `spot-price` behaviour to be used by Alice. /// diff --git a/swap/src/protocol/alice/behaviour.rs b/swap/src/protocol/alice/behaviour.rs index 643f2d92..87b65981 100644 --- a/swap/src/protocol/alice/behaviour.rs +++ b/swap/src/protocol/alice/behaviour.rs @@ -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, + msg: Request, + channel: ResponseChannel, peer: PeerId, }, ExecutionSetupDone { @@ -124,7 +123,7 @@ impl From 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, - response: SpotPriceResponse, + channel: ResponseChannel, + response: Response, ) -> Result<()> { self.spot_price .send_response(channel, response) diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 2e91dee8..9f8cd9bc 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -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 diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 92a45f17..afc952fc 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -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, pub monero_wallet: Arc, @@ -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, monero_wallet: Arc, 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 { + pub fn build(self) -> Result { 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>), TransferProof { msg: Box, @@ -208,7 +205,7 @@ impl From 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); } diff --git a/swap/src/protocol/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index 78d8c286..54f1298d 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -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 Default for Channels { #[derive(Debug)] pub struct EventLoopHandle { - recv_spot_price: Receiver, + recv_spot_price: Receiver, start_execution_setup: Sender, done_execution_setup: Receiver>, recv_transfer_proof: Receiver, conn_established: Receiver, dial_alice: Sender<()>, - request_spot_price: Sender, + request_spot_price: Sender, send_encrypted_signature: Sender, } @@ -75,10 +75,7 @@ impl EventLoopHandle { } pub async fn request_spot_price(&mut self, btc: bitcoin::Amount) -> Result { - 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, bitcoin_wallet: Arc, alice_peer_id: PeerId, - request_spot_price: Receiver, - recv_spot_price: Sender, + request_spot_price: Receiver, + recv_spot_price: Sender, start_execution_setup: Receiver, done_execution_setup: Sender>, recv_transfer_proof: Sender, diff --git a/swap/src/trace.rs b/swap/src/trace.rs index 7f55c3da..34880b33 100644 --- a/swap/src/trace.rs +++ b/swap/src/trace.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use atty::{self}; use tracing::{info, subscriber}; use tracing_log::LogTracer; use tracing_subscriber::filter::LevelFilter;