From 44c4b5dceaa024b550dcf8cd4c973c655aa2de5c Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 21 Jan 2021 11:20:57 +1100 Subject: [PATCH] Remove newlines from import statements to avoid problems Rust fmt automatically groups the imports (from top to bottom) as `pub use` `use crate` and `use`. There is no need to introduce sections which cause annoyance when auto importing using the IDE. --- monero-harness/src/lib.rs | 9 ++++----- swap/src/bitcoin.rs | 16 +++++++--------- swap/src/bitcoin/transactions.rs | 9 ++++----- swap/src/bitcoin/wallet.rs | 17 ++++++++--------- swap/src/cli.rs | 3 +-- swap/src/config.rs | 1 - swap/src/database.rs | 1 - swap/src/main.rs | 1 - swap/src/monero.rs | 8 +++----- swap/src/monero/wallet.rs | 9 ++++----- swap/src/protocol/alice.rs | 19 +++++++++---------- swap/src/protocol/alice/event_loop.rs | 13 ++++++------- swap/src/protocol/alice/message0.rs | 11 +++++------ swap/src/protocol/alice/message1.rs | 9 ++++----- swap/src/protocol/alice/message2.rs | 11 +++++------ swap/src/protocol/alice/message3.rs | 9 ++++----- swap/src/protocol/alice/state.rs | 23 +++++++++++------------ swap/src/protocol/alice/swap.rs | 21 ++++++++++----------- swap/src/protocol/bob/message0.rs | 11 +++++------ swap/src/protocol/bob/message1.rs | 11 +++++------ swap/src/protocol/bob/message2.rs | 9 ++++----- swap/src/protocol/bob/message3.rs | 9 ++++----- swap/src/protocol/bob/state.rs | 21 ++++++++++----------- swap/src/protocol/bob/swap.rs | 15 +++++++-------- 24 files changed, 120 insertions(+), 146 deletions(-) diff --git a/monero-harness/src/lib.rs b/monero-harness/src/lib.rs index 067b69da..353ebceb 100644 --- a/monero-harness/src/lib.rs +++ b/monero-harness/src/lib.rs @@ -24,11 +24,6 @@ pub mod image; pub mod rpc; -use anyhow::{anyhow, bail, Result}; -use std::time::Duration; -use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs}; -use tokio::time; - use crate::{ image::{ MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, MONEROD_RPC_PORT, WALLET_RPC_PORT, @@ -38,6 +33,10 @@ use crate::{ wallet::{self, GetAddress, Refreshed, Transfer}, }, }; +use anyhow::{anyhow, bail, Result}; +use std::time::Duration; +use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs}; +use tokio::time; /// How often we mine a block. const BLOCK_TIME_SECS: u64 = 1; diff --git a/swap/src/bitcoin.rs b/swap/src/bitcoin.rs index ea03470e..a7ae1e0c 100644 --- a/swap/src/bitcoin.rs +++ b/swap/src/bitcoin.rs @@ -1,26 +1,24 @@ +pub use crate::bitcoin::{ + timelocks::Timelock, + transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund}, +}; +use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks}; use ::bitcoin::{ hashes::{hex::ToHex, Hash}, secp256k1, util::psbt::PartiallySignedTransaction, SigHash, }; +pub use ::bitcoin::{util::amount::Amount, Address, Network, Transaction, Txid}; use anyhow::{anyhow, bail, Result}; use async_trait::async_trait; use ecdsa_fun::{adaptor::Adaptor, fun::Point, nonce::Deterministic, ECDSA}; +pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; use miniscript::{Descriptor, Segwitv0}; use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::str::FromStr; - -use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks}; - -pub use crate::bitcoin::{ - timelocks::Timelock, - transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund}, -}; -pub use ::bitcoin::{util::amount::Amount, Address, Network, Transaction, Txid}; -pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; pub use wallet::Wallet; pub mod timelocks; diff --git a/swap/src/bitcoin/transactions.rs b/swap/src/bitcoin/transactions.rs index d8d2c059..6ac7c9de 100644 --- a/swap/src/bitcoin/transactions.rs +++ b/swap/src/bitcoin/transactions.rs @@ -1,3 +1,7 @@ +use crate::bitcoin::{ + build_shared_output_descriptor, timelocks::Timelock, verify_sig, Address, Amount, + BuildTxLockPsbt, GetNetwork, PublicKey, Transaction, TX_FEE, +}; use ::bitcoin::{ util::{bip143::SigHashCache, psbt::PartiallySignedTransaction}, OutPoint, SigHash, SigHashType, TxIn, TxOut, Txid, @@ -8,11 +12,6 @@ use miniscript::{Descriptor, NullCtx}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use crate::bitcoin::{ - build_shared_output_descriptor, timelocks::Timelock, verify_sig, Address, Amount, - BuildTxLockPsbt, GetNetwork, PublicKey, Transaction, TX_FEE, -}; - #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct TxLock { inner: Transaction, diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 6cbc777b..cecfd62e 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -1,12 +1,3 @@ -use ::bitcoin::{util::psbt::PartiallySignedTransaction, Txid}; -use anyhow::{Context, Result}; -use async_trait::async_trait; -use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; -use bitcoin_harness::{bitcoind_rpc::PsbtBase64, BitcoindRpcApi}; -use reqwest::Url; -use std::time::Duration; -use tokio::time::interval; - use crate::{ bitcoin::{ timelocks::BlockHeight, Address, Amount, BroadcastSignedTransaction, BuildTxLockPsbt, @@ -15,6 +6,14 @@ use crate::{ }, config::Config, }; +use ::bitcoin::{util::psbt::PartiallySignedTransaction, Txid}; +use anyhow::{Context, Result}; +use async_trait::async_trait; +use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; +use bitcoin_harness::{bitcoind_rpc::PsbtBase64, BitcoindRpcApi}; +use reqwest::Url; +use std::time::Duration; +use tokio::time::interval; #[derive(Debug)] pub struct Wallet { diff --git a/swap/src/cli.rs b/swap/src/cli.rs index b762edd0..a2a48a48 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -1,9 +1,8 @@ +use crate::{bitcoin, monero}; use libp2p::{core::Multiaddr, PeerId}; use url::Url; use uuid::Uuid; -use crate::{bitcoin, monero}; - #[derive(structopt::StructOpt, Debug)] pub struct Options { // TODO: Default value should points to proper configuration folder in home folder diff --git a/swap/src/config.rs b/swap/src/config.rs index 8ef318c2..b23cdc43 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -1,5 +1,4 @@ pub mod seed; - use crate::bitcoin::Timelock; use conquer_once::Lazy; use std::time::Duration; diff --git a/swap/src/database.rs b/swap/src/database.rs index fa7a8b8a..6b314f14 100644 --- a/swap/src/database.rs +++ b/swap/src/database.rs @@ -5,7 +5,6 @@ use uuid::Uuid; mod alice; mod bob; - pub use alice::Alice; pub use bob::Bob; diff --git a/swap/src/main.rs b/swap/src/main.rs index fdad3b55..e02ab6f9 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -12,7 +12,6 @@ )] #![forbid(unsafe_code)] #![allow(non_snake_case)] - use crate::cli::{Command, Options, Resume}; use anyhow::{Context, Result}; use prettytable::{row, Table}; diff --git a/swap/src/monero.rs b/swap/src/monero.rs index 499e248d..a4490466 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -1,8 +1,11 @@ pub mod wallet; +use crate::bitcoin; use ::bitcoin::hashes::core::fmt::Formatter; +pub use ::monero::{Network, PrivateKey, PublicKey}; use anyhow::Result; use async_trait::async_trait; +pub use curve25519_dalek::scalar::Scalar; use rand::{CryptoRng, RngCore}; use rust_decimal::{ prelude::{FromPrimitive, ToPrimitive}, @@ -14,11 +17,6 @@ use std::{ ops::{Add, Mul, Sub}, str::FromStr, }; - -use crate::bitcoin; - -pub use ::monero::{Network, PrivateKey, PublicKey}; -pub use curve25519_dalek::scalar::Scalar; pub use wallet::Wallet; pub const PICONERO_OFFSET: u64 = 1_000_000_000_000; diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 7f5dec6c..2f26568d 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -1,3 +1,7 @@ +use crate::monero::{ + Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicViewKey, Transfer, + TransferProof, TxHash, WatchForTransfer, +}; use ::monero::{Address, Network, PrivateKey, PublicKey}; use anyhow::Result; use async_trait::async_trait; @@ -12,11 +16,6 @@ use std::{ use tracing::info; use url::Url; -use crate::monero::{ - Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicViewKey, Transfer, - TransferProof, TxHash, WatchForTransfer, -}; - #[derive(Debug)] pub struct Wallet { pub inner: wallet::Client, diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index e15d55a7..096d39c7 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -1,5 +1,14 @@ //! Run an XMR/BTC swap in the role of Alice. //! Alice holds XMR and wishes receive BTC. +pub use self::{ + event_loop::{EventLoop, EventLoopHandle}, + message0::Message0, + message1::Message1, + message2::Message2, + state::*, + swap::{run, run_until}, + swap_response::*, +}; use crate::{ bitcoin, config::Config, @@ -25,16 +34,6 @@ use std::{path::PathBuf, sync::Arc}; use tracing::{debug, info}; use uuid::Uuid; -pub use self::{ - event_loop::{EventLoop, EventLoopHandle}, - message0::Message0, - message1::Message1, - message2::Message2, - state::*, - swap::{run, run_until}, - swap_response::*, -}; - pub mod event_loop; mod message0; mod message1; diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 6a643c63..a648d1a6 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -1,10 +1,3 @@ -use anyhow::{anyhow, Context, Result}; -use futures::FutureExt; -use libp2p::{ - core::Multiaddr, futures::StreamExt, request_response::ResponseChannel, PeerId, Swarm, -}; -use tokio::sync::mpsc::{Receiver, Sender}; - use crate::{ network::{request_response::AliceToBob, transport::SwapTransport, TokioExecutor}, protocol::{ @@ -13,6 +6,12 @@ use crate::{ bob, }, }; +use anyhow::{anyhow, Context, Result}; +use futures::FutureExt; +use libp2p::{ + core::Multiaddr, futures::StreamExt, request_response::ResponseChannel, PeerId, Swarm, +}; +use tokio::sync::mpsc::{Receiver, Sender}; #[allow(missing_debug_implementations)] pub struct Channels { diff --git a/swap/src/protocol/alice/message0.rs b/swap/src/protocol/alice/message0.rs index 76e57c1b..a58d95eb 100644 --- a/swap/src/protocol/alice/message0.rs +++ b/swap/src/protocol/alice/message0.rs @@ -1,3 +1,8 @@ +use crate::{ + bitcoin, monero, + network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}, + protocol::bob, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -14,12 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - bitcoin, monero, - network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}, - protocol::bob, -}; - #[derive(Debug)] pub enum OutEvent { Msg { diff --git a/swap/src/protocol/alice/message1.rs b/swap/src/protocol/alice/message1.rs index c04faa9a..95050905 100644 --- a/swap/src/protocol/alice/message1.rs +++ b/swap/src/protocol/alice/message1.rs @@ -1,3 +1,7 @@ +use crate::{ + network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}, + protocol::bob, +}; use ecdsa_fun::{adaptor::EncryptedSignature, Signature}; use libp2p::{ request_response::{ @@ -15,11 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}, - protocol::bob, -}; - #[derive(Debug)] pub enum OutEvent { Msg { diff --git a/swap/src/protocol/alice/message2.rs b/swap/src/protocol/alice/message2.rs index 9b92758e..d383ff11 100644 --- a/swap/src/protocol/alice/message2.rs +++ b/swap/src/protocol/alice/message2.rs @@ -1,3 +1,8 @@ +use crate::{ + monero, + network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}, + protocol::bob, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -14,12 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - monero, - network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}, - protocol::bob, -}; - #[derive(Debug)] pub enum OutEvent { Msg { diff --git a/swap/src/protocol/alice/message3.rs b/swap/src/protocol/alice/message3.rs index d5f6b563..9c85cc96 100644 --- a/swap/src/protocol/alice/message3.rs +++ b/swap/src/protocol/alice/message3.rs @@ -1,3 +1,7 @@ +use crate::{ + network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, + protocol::bob, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -13,11 +17,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, - protocol::bob, -}; - #[derive(Debug)] pub enum OutEvent { Msg(bob::Message3), diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 89f43d52..7c318772 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -1,15 +1,3 @@ -use anyhow::{anyhow, Context, Result}; -use ecdsa_fun::{ - adaptor::{Adaptor, EncryptedSignature}, - nonce::Deterministic, -}; -use libp2p::request_response::ResponseChannel; -use rand::{CryptoRng, RngCore}; -use serde::{Deserialize, Serialize}; -use sha2::Sha256; -use std::fmt; -use tracing::info; - use crate::{ bitcoin, bitcoin::{ @@ -22,6 +10,17 @@ use crate::{ protocol::{alice, bob}, ExpiredTimelocks, SwapAmounts, }; +use anyhow::{anyhow, Context, Result}; +use ecdsa_fun::{ + adaptor::{Adaptor, EncryptedSignature}, + nonce::Deterministic, +}; +use libp2p::request_response::ResponseChannel; +use rand::{CryptoRng, RngCore}; +use serde::{Deserialize, Serialize}; +use sha2::Sha256; +use std::fmt; +use tracing::info; #[derive(Debug)] pub enum AliceState { diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 0b7ffae8..e753a54d 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -1,16 +1,5 @@ //! Run an XMR/BTC swap in the role of Alice. //! Alice holds XMR and wishes receive BTC. -use anyhow::{bail, Result}; -use async_recursion::async_recursion; -use futures::{ - future::{select, Either}, - pin_mut, -}; -use rand::{CryptoRng, RngCore}; -use std::sync::Arc; -use tracing::{error, info}; -use uuid::Uuid; - use crate::{ bitcoin, bitcoin::{TransactionBlockHeight, WaitForTransactionFinality, WatchForRawTransaction}, @@ -35,6 +24,16 @@ use crate::{ }, ExpiredTimelocks, }; +use anyhow::{bail, Result}; +use async_recursion::async_recursion; +use futures::{ + future::{select, Either}, + pin_mut, +}; +use rand::{CryptoRng, RngCore}; +use std::sync::Arc; +use tracing::{error, info}; +use uuid::Uuid; trait Rng: RngCore + CryptoRng + Send {} diff --git a/swap/src/protocol/bob/message0.rs b/swap/src/protocol/bob/message0.rs index d1b2a57a..3e956fe8 100644 --- a/swap/src/protocol/bob/message0.rs +++ b/swap/src/protocol/bob/message0.rs @@ -1,3 +1,8 @@ +use crate::{ + bitcoin, monero, + network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}, + protocol::{alice, bob}, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -14,12 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - bitcoin, monero, - network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}, - protocol::{alice, bob}, -}; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message0 { pub(crate) B: bitcoin::PublicKey, diff --git a/swap/src/protocol/bob/message1.rs b/swap/src/protocol/bob/message1.rs index fbae5ace..f783af1d 100644 --- a/swap/src/protocol/bob/message1.rs +++ b/swap/src/protocol/bob/message1.rs @@ -1,3 +1,8 @@ +use crate::{ + bitcoin, + network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}, + protocol::alice, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -14,12 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - bitcoin, - network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}, - protocol::alice, -}; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message1 { pub(crate) tx_lock: bitcoin::TxLock, diff --git a/swap/src/protocol/bob/message2.rs b/swap/src/protocol/bob/message2.rs index 62c55147..edda820e 100644 --- a/swap/src/protocol/bob/message2.rs +++ b/swap/src/protocol/bob/message2.rs @@ -1,3 +1,7 @@ +use crate::{ + network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}, + protocol::alice, +}; use ecdsa_fun::Signature; use libp2p::{ request_response::{ @@ -15,11 +19,6 @@ use std::{ }; use tracing::{debug, error}; -use crate::{ - network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}, - protocol::alice, -}; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message2 { pub(crate) tx_punish_sig: Signature, diff --git a/swap/src/protocol/bob/message3.rs b/swap/src/protocol/bob/message3.rs index 7c9e2df6..f9703ad1 100644 --- a/swap/src/protocol/bob/message3.rs +++ b/swap/src/protocol/bob/message3.rs @@ -1,3 +1,7 @@ +use crate::{ + bitcoin::EncryptedSignature, + network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, +}; use libp2p::{ request_response::{ handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig, @@ -14,11 +18,6 @@ use std::{ }; use tracing::error; -use crate::{ - bitcoin::EncryptedSignature, - network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, -}; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message3 { pub tx_redeem_encsig: EncryptedSignature, diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index 5c8d348a..9e0de62e 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -1,14 +1,3 @@ -use anyhow::{anyhow, Result}; -use ecdsa_fun::{ - adaptor::{Adaptor, EncryptedSignature}, - nonce::Deterministic, - Signature, -}; -use rand::{CryptoRng, RngCore}; -use serde::{Deserialize, Serialize}; -use sha2::Sha256; -use std::fmt; - use crate::{ bitcoin::{ self, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, @@ -21,6 +10,16 @@ use crate::{ protocol::{alice, bob}, ExpiredTimelocks, SwapAmounts, }; +use anyhow::{anyhow, Result}; +use ecdsa_fun::{ + adaptor::{Adaptor, EncryptedSignature}, + nonce::Deterministic, + Signature, +}; +use rand::{CryptoRng, RngCore}; +use serde::{Deserialize, Serialize}; +use sha2::Sha256; +use std::fmt; #[derive(Debug, Clone)] pub enum BobState { diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 208c978c..5cf65b5d 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -1,11 +1,3 @@ -use anyhow::{bail, Result}; -use async_recursion::async_recursion; -use rand::{rngs::OsRng, CryptoRng, RngCore}; -use std::sync::Arc; -use tokio::select; -use tracing::{debug, info}; -use uuid::Uuid; - use crate::{ bitcoin, config::Config, @@ -14,6 +6,13 @@ use crate::{ protocol::bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest}, ExpiredTimelocks, SwapAmounts, }; +use anyhow::{bail, Result}; +use async_recursion::async_recursion; +use rand::{rngs::OsRng, CryptoRng, RngCore}; +use std::sync::Arc; +use tokio::select; +use tracing::{debug, info}; +use uuid::Uuid; pub fn is_complete(state: &BobState) -> bool { matches!(