From f2a25ee49b0f26de4a09afedb034b38ae58ffaa4 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 21 Jan 2021 13:43:25 +1100 Subject: [PATCH] Move definitions out of lib.rs --- swap/src/bitcoin.rs | 3 ++- swap/src/bitcoin/timelocks.rs | 10 +++++++- swap/src/database/alice.rs | 3 +-- swap/src/database/bob.rs | 3 +-- swap/src/lib.rs | 34 ---------------------------- swap/src/main.rs | 14 +++++++----- swap/src/network/request_response.rs | 3 +-- swap/src/protocol.rs | 27 ++++++++++++++++++++++ swap/src/protocol/alice.rs | 3 +-- swap/src/protocol/alice/state.rs | 9 ++++---- swap/src/protocol/alice/steps.rs | 27 +++++++++++----------- swap/src/protocol/alice/swap.rs | 6 +++-- swap/src/protocol/bob.rs | 3 +-- swap/src/protocol/bob/state.rs | 11 +++++---- swap/src/protocol/bob/swap.rs | 7 ++++-- swap/tests/testutils/mod.rs | 3 +-- 16 files changed, 85 insertions(+), 81 deletions(-) diff --git a/swap/src/bitcoin.rs b/swap/src/bitcoin.rs index 9948e5aa..3511e395 100644 --- a/swap/src/bitcoin.rs +++ b/swap/src/bitcoin.rs @@ -10,7 +10,7 @@ pub use ::bitcoin::{util::amount::Amount, Address, Network, Transaction, Txid}; pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; pub use wallet::Wallet; -use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks}; +use crate::{bitcoin::timelocks::BlockHeight, config::Config}; use ::bitcoin::{ hashes::{hex::ToHex, Hash}, secp256k1, @@ -25,6 +25,7 @@ use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::str::FromStr; +use timelocks::ExpiredTimelocks; // TODO: Configurable tx-fee (note: parties have to agree prior to swapping) // Current reasoning: diff --git a/swap/src/bitcoin/timelocks.rs b/swap/src/bitcoin/timelocks.rs index 0f594afb..a4dc332c 100644 --- a/swap/src/bitcoin/timelocks.rs +++ b/swap/src/bitcoin/timelocks.rs @@ -1,6 +1,7 @@ -use serde::{Deserialize, Serialize}; use std::ops::Add; +use serde::{Deserialize, Serialize}; + /// Represent a timelock, expressed in relative block height as defined in /// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki). /// E.g. The timelock expires 10 blocks after the reference transaction is @@ -47,3 +48,10 @@ impl Add for BlockHeight { BlockHeight(self.0 + rhs.0) } } + +#[derive(Debug, Clone, Copy)] +pub enum ExpiredTimelocks { + None, + Cancel, + Punish, +} diff --git a/swap/src/database/alice.rs b/swap/src/database/alice.rs index 0562b299..939ff550 100644 --- a/swap/src/database/alice.rs +++ b/swap/src/database/alice.rs @@ -2,8 +2,7 @@ use crate::{ bitcoin::{EncryptedSignature, TxCancel, TxRefund}, monero, monero::monero_private_key, - protocol::{alice, alice::AliceState}, - SwapAmounts, + protocol::{alice, alice::AliceState, SwapAmounts}, }; use ::bitcoin::hashes::core::fmt::Display; use serde::{Deserialize, Serialize}; diff --git a/swap/src/database/bob.rs b/swap/src/database/bob.rs index e21bb8d2..89a66436 100644 --- a/swap/src/database/bob.rs +++ b/swap/src/database/bob.rs @@ -1,7 +1,6 @@ use crate::{ monero::TransferProof, - protocol::{bob, bob::BobState}, - SwapAmounts, + protocol::{bob, bob::BobState, SwapAmounts}, }; use ::bitcoin::hashes::core::fmt::Display; use monero_harness::rpc::wallet::BlockHeight; diff --git a/swap/src/lib.rs b/swap/src/lib.rs index e946623e..d85355a7 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -26,37 +26,3 @@ pub mod seed; pub mod trace; mod fs; - -use serde::{Deserialize, Serialize}; -use std::fmt::{self, Display}; - -/// XMR/BTC swap amounts. -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] -// TODO(Franck): review necessity of this struct -pub struct SwapAmounts { - /// Amount of BTC to swap. - #[serde(with = "::bitcoin::util::amount::serde::as_sat")] - pub btc: bitcoin::Amount, - /// Amount of XMR to swap. - #[serde(with = "monero::monero_amount")] - pub xmr: monero::Amount, -} - -// TODO: Display in XMR and BTC (not picos and sats). -impl Display for SwapAmounts { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{} sats for {} piconeros", - self.btc.as_sat(), - self.xmr.as_piconero() - ) - } -} - -#[derive(Debug, Clone, Copy)] -pub enum ExpiredTimelocks { - None, - Cancel, - Punish, -} diff --git a/swap/src/main.rs b/swap/src/main.rs index fdad3b55..0a9dd7d9 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -13,22 +13,24 @@ #![forbid(unsafe_code)] #![allow(non_snake_case)] -use crate::cli::{Command, Options, Resume}; +use std::sync::Arc; + use anyhow::{Context, Result}; use prettytable::{row, Table}; -use std::sync::Arc; use structopt::StructOpt; +use tracing::{info, log::LevelFilter}; +use uuid::Uuid; + use swap::{ bitcoin, config::Config, database::Database, monero, - protocol::{alice, bob, bob::Builder}, + protocol::{alice, bob, bob::Builder, SwapAmounts}, trace::init_tracing, - SwapAmounts, }; -use tracing::{info, log::LevelFilter}; -use uuid::Uuid; + +use crate::cli::{Command, Options, Resume}; mod cli; diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index a58be87f..1fd46224 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -1,3 +1,4 @@ +use crate::protocol::{alice, bob}; use async_trait::async_trait; use futures::prelude::*; use libp2p::{ @@ -8,8 +9,6 @@ use serde::{Deserialize, Serialize}; use std::{fmt::Debug, io, marker::PhantomData}; use tracing::debug; -use crate::protocol::{alice, bob}; - /// Time to wait for a response back once we send a request. pub const TIMEOUT: u64 = 3600; // One hour. diff --git a/swap/src/protocol.rs b/swap/src/protocol.rs index a2e13b79..e310ee44 100644 --- a/swap/src/protocol.rs +++ b/swap/src/protocol.rs @@ -1,3 +1,7 @@ +use crate::monero; +use bitcoin::hashes::core::{fmt, fmt::Display}; +use serde::{Deserialize, Serialize}; + pub mod alice; pub mod bob; @@ -6,3 +10,26 @@ pub struct StartingBalances { pub xmr: crate::monero::Amount, pub btc: bitcoin::Amount, } + +/// XMR/BTC swap amounts. +#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct SwapAmounts { + /// Amount of BTC to swap. + #[serde(with = "::bitcoin::util::amount::serde::as_sat")] + pub btc: bitcoin::Amount, + /// Amount of XMR to swap. + #[serde(with = "monero::monero_amount")] + pub xmr: crate::monero::Amount, +} + +// TODO: Display in XMR and BTC (not picos and sats). +impl Display for SwapAmounts { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} sats for {} piconeros", + self.btc.as_sat(), + self.xmr.as_piconero() + ) + } +} diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 096d39c7..51c7134b 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -21,9 +21,8 @@ use crate::{ transport::build, Seed as NetworkSeed, }, - protocol::bob, + protocol::{bob, SwapAmounts}, seed::Seed, - SwapAmounts, }; use anyhow::{bail, Result}; use libp2p::{ diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 7c318772..e838568f 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -1,14 +1,15 @@ use crate::{ bitcoin, bitcoin::{ - current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight, - TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction, + current_epoch, + timelocks::{ExpiredTimelocks, Timelock}, + wait_for_cancel_timelock_to_expire, GetBlockHeight, TransactionBlockHeight, TxCancel, + TxRefund, WatchForRawTransaction, }, monero, monero::CreateWalletForOutput, network::request_response::AliceToBob, - protocol::{alice, bob}, - ExpiredTimelocks, SwapAmounts, + protocol::{alice, bob, SwapAmounts}, }; use anyhow::{anyhow, Context, Result}; use ecdsa_fun::{ diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 1d593241..d6be5ae8 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -1,16 +1,3 @@ -use anyhow::{Context, Result}; -use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; -use futures::{ - future::{select, Either}, - pin_mut, -}; -use libp2p::request_response::ResponseChannel; -use rand::rngs::OsRng; -use sha2::Sha256; -use std::sync::Arc; -use tokio::time::timeout; -use tracing::{info, trace}; - use crate::{ bitcoin, bitcoin::{ @@ -27,9 +14,21 @@ use crate::{ protocol::{ alice, alice::{event_loop::EventLoopHandle, SwapResponse}, + SwapAmounts, }, - SwapAmounts, }; +use anyhow::{Context, Result}; +use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; +use futures::{ + future::{select, Either}, + pin_mut, +}; +use libp2p::request_response::ResponseChannel; +use rand::rngs::OsRng; +use sha2::Sha256; +use std::sync::Arc; +use tokio::time::timeout; +use tracing::{info, trace}; pub async fn negotiate( state0: alice::State0, diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 635a75e6..ecda7c63 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -2,7 +2,10 @@ //! Alice holds XMR and wishes receive BTC. use crate::{ bitcoin, - bitcoin::{TransactionBlockHeight, WaitForTransactionFinality, WatchForRawTransaction}, + bitcoin::{ + timelocks::ExpiredTimelocks, TransactionBlockHeight, WaitForTransactionFinality, + WatchForRawTransaction, + }, config::Config, database, database::Database, @@ -22,7 +25,6 @@ use crate::{ AliceState, }, }, - ExpiredTimelocks, }; use anyhow::{bail, Result}; use async_recursion::async_recursion; diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index fa4b53f4..20e8d2a2 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -11,9 +11,8 @@ use crate::{ peer_tracker::{self, PeerTracker}, transport::build, }, - protocol::{alice, bob}, + protocol::{alice, bob, SwapAmounts}, seed::Seed, - SwapAmounts, }; use anyhow::{bail, Result}; use libp2p::{core::Multiaddr, identity::Keypair, NetworkBehaviour, PeerId}; diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index e0e3e875..f06cfd17 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -1,14 +1,15 @@ use crate::{ bitcoin::{ - self, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, - BroadcastSignedTransaction, BuildTxLockPsbt, GetBlockHeight, GetNetwork, GetRawTransaction, - Transaction, TransactionBlockHeight, TxCancel, Txid, WatchForRawTransaction, + self, current_epoch, + timelocks::{ExpiredTimelocks, Timelock}, + wait_for_cancel_timelock_to_expire, BroadcastSignedTransaction, BuildTxLockPsbt, + GetBlockHeight, GetNetwork, GetRawTransaction, Transaction, TransactionBlockHeight, + TxCancel, Txid, WatchForRawTransaction, }, config::Config, monero, monero::{monero_private_key, TransferProof}, - protocol::{alice, bob}, - ExpiredTimelocks, SwapAmounts, + protocol::{alice, bob, SwapAmounts}, }; use anyhow::{anyhow, Result}; use ecdsa_fun::{ diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 6b522470..049ac52f 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -1,10 +1,13 @@ use crate::{ bitcoin, + bitcoin::timelocks::ExpiredTimelocks, config::Config, database::{Database, Swap}, monero, - protocol::bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest}, - ExpiredTimelocks, SwapAmounts, + protocol::{ + bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest}, + SwapAmounts, + }, }; use anyhow::{bail, Result}; use async_recursion::async_recursion; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 04b0a144..e21aa1a4 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -9,9 +9,8 @@ use swap::{ bitcoin, config::Config, monero, - protocol::{alice, alice::AliceState, bob, bob::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts}, seed::Seed, - SwapAmounts, }; use tempfile::tempdir; use testcontainers::{clients::Cli, Container};