diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 4ad7f196..6611ad43 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -6,7 +6,7 @@ use libp2p::{ request_response::ResponseChannel, NetworkBehaviour, PeerId, }; -use rand::{CryptoRng, RngCore}; +use rand::rngs::OsRng; use std::{thread, time::Duration}; use tracing::debug; @@ -21,21 +21,20 @@ use crate::{ request_response::{AliceToBob, TIMEOUT}, transport, TokioExecutor, }, - SwapParams, PUNISH_TIMELOCK, REFUND_TIMELOCK, + SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, }; use xmr_btc::{alice::State0, bob, monero}; pub type Swarm = libp2p::Swarm; #[allow(unused_assignments)] // Due to the mutable message0? -pub async fn swap( +pub async fn swap( listen: Multiaddr, - rng: &mut R, redeem_address: ::bitcoin::Address, punish_address: ::bitcoin::Address, ) -> Result<()> { let mut message0: Option = None; - let mut last_amounts: Option = None; + let mut last_amounts: Option = None; let mut swarm = new_swarm(listen)?; @@ -65,10 +64,12 @@ pub async fn swap( None => unreachable!("should have amounts by here"), }; - // FIXME: Too many `bitcoin` crates/modules. let xmr = monero::Amount::from_piconero(xmr.as_piconero()); + // TODO: This should be the Amount exported by xmr_btc. let btc = ::bitcoin::Amount::from_sat(btc.as_sat()); + // TODO: Pass this in using + let rng = &mut OsRng; let state0 = State0::new( rng, btc, @@ -195,8 +196,8 @@ impl Alice { } /// Alice always sends her messages as a response to a request from Bob. - pub fn send_amounts(&mut self, channel: ResponseChannel, p: SwapParams) { - let msg = AliceToBob::Amounts(p); + pub fn send_amounts(&mut self, channel: ResponseChannel, amounts: SwapAmounts) { + let msg = AliceToBob::Amounts(amounts); self.amounts.send(channel, msg); } @@ -228,15 +229,15 @@ impl Default for Alice { } } -// TODO: Check that this is correct. -fn calculate_amounts(btc: ::bitcoin::Amount) -> SwapParams { +fn calculate_amounts(btc: ::bitcoin::Amount) -> SwapAmounts { const XMR_PER_BTC: u64 = 100; // TODO: Get this from an exchange. + // TODO: Check that this is correct. // XMR uses 12 zerose BTC uses 8. let picos = (btc.as_sat() * 10000) * XMR_PER_BTC; let xmr = monero::Amount::from_piconero(picos); - SwapParams { btc, xmr } + SwapAmounts { btc, xmr } } #[cfg(test)] @@ -251,7 +252,7 @@ mod tests { let btc = ::bitcoin::Amount::from_sat(ONE_BTC); let want = monero::Amount::from_piconero(HUNDRED_XMR); - let SwapParams { xmr: got, .. } = calculate_amounts(btc); + let SwapAmounts { xmr: got, .. } = calculate_amounts(btc); assert_eq!(got, want); } } diff --git a/swap/src/bob.rs b/swap/src/bob.rs index 53eec7d0..d3422b5d 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -72,6 +72,7 @@ where let xmr = xmr_btc::monero::Amount::from_piconero(xmr.as_piconero()); let btc = ::bitcoin::Amount::from_sat(btc.as_sat()); + // TODO: Pass this in using let rng = &mut OsRng; let state0 = State0::new( rng, diff --git a/swap/src/bob/amounts.rs b/swap/src/bob/amounts.rs index 6d7ed714..fcdb74e0 100644 --- a/swap/src/bob/amounts.rs +++ b/swap/src/bob/amounts.rs @@ -16,12 +16,12 @@ use tracing::error; use crate::{ network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}, - SwapParams, + SwapAmounts, }; #[derive(Debug)] pub enum OutEvent { - Amounts(SwapParams), + Amounts(SwapAmounts), } /// A `NetworkBehaviour` that represents getting the amounts of an XMR/BTC swap. diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 9f98db70..99778b79 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -9,7 +9,7 @@ pub mod network; pub const ONE_BTC: u64 = 100_000_000; -const REFUND_TIMELOCK: u32 = 10; // FIXME: What should this be? +const REFUND_TIMELOCK: u32 = 10; // Relative timelock, this is number of blocks. TODO: What should it be? const PUNISH_TIMELOCK: u32 = 20; // FIXME: What should this be? pub type Never = std::convert::Infallible; @@ -17,7 +17,7 @@ pub type Never = std::convert::Infallible; /// Commands sent from Bob to the main task. #[derive(Clone, Copy, Debug)] pub enum Cmd { - VerifyAmounts(SwapParams), + VerifyAmounts(SwapAmounts), } /// Responses send from the main task back to Bob. @@ -27,9 +27,9 @@ pub enum Rsp { Abort, } -/// XMR/BTC swap parameters. +/// XMR/BTC swap amounts. #[derive(Copy, Clone, Debug, Serialize, Deserialize)] -pub struct SwapParams { +pub struct SwapAmounts { /// Amount of BTC to swap. #[serde(with = "::bitcoin::util::amount::serde::as_sat")] pub btc: ::bitcoin::Amount, @@ -38,7 +38,7 @@ pub struct SwapParams { pub xmr: xmr_btc::monero::Amount, } -impl Display for SwapParams { +impl Display for SwapAmounts { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, diff --git a/swap/src/main.rs b/swap/src/main.rs index f5b60f42..7f427a8d 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -16,7 +16,6 @@ use anyhow::{bail, Result}; use futures::{channel::mpsc, StreamExt}; use libp2p::Multiaddr; use log::LevelFilter; -use rand::rngs::OsRng; use std::{io, io::Write, process}; use structopt::StructOpt; use tracing::info; @@ -26,10 +25,13 @@ mod cli; mod trace; use cli::Options; -use swap::{alice, bitcoin::Wallet, bob, Cmd, Rsp, SwapParams}; +use swap::{alice, bitcoin::Wallet, bob, Cmd, Rsp, SwapAmounts}; use xmr_btc::bitcoin::BuildTxLockPsbt; + // TODO: Add root seed file instead of generating new seed each run. +// TODO: Remove all instances of the todo! macro +// TODO: Add a config file with these in it. // Alice's address and port until we have a config file. pub const PORT: u16 = 9876; // Arbitrarily chosen. pub const ADDR: &str = "127.0.0.1"; @@ -97,7 +99,7 @@ async fn swap_as_alice( redeem: bitcoin::Address, punish: bitcoin::Address, ) -> Result<()> { - alice::swap(addr, &mut OsRng, redeem, punish).await + alice::swap(addr, redeem, punish).await } async fn swap_as_bob( @@ -132,10 +134,10 @@ where } } -fn verify(p: SwapParams) -> Rsp { +fn verify(amounts: SwapAmounts) -> Rsp { let mut s = String::new(); println!("Got rate from Alice for XMR/BTC swap\n"); - println!("{}", p); + println!("{}", amounts); print!("Would you like to continue with this swap [y/N]: "); let _ = io::stdout().flush(); io::stdin() diff --git a/swap/src/network/peer_tracker.rs b/swap/src/network/peer_tracker.rs index ca3a67e5..08e97077 100644 --- a/swap/src/network/peer_tracker.rs +++ b/swap/src/network/peer_tracker.rs @@ -25,8 +25,7 @@ pub struct PeerTracker { } impl PeerTracker { - /// Returns an arbitrary connected counterparty. - /// This is useful if we are connected to a single other node. + /// Returns the peer id of counterparty if we are connected. pub fn counterparty_peer_id(&self) -> Option { if let Some((id, _)) = &self.connected { return Some(id.clone()); @@ -34,8 +33,7 @@ impl PeerTracker { None } - /// Returns an arbitrary connected counterparty. - /// This is useful if we are connected to a single other node. + /// Returns the multiaddr of counterparty if we are connected. pub fn counterparty_addr(&self) -> Option { if let Some((_, addr)) = &self.connected { return Some(addr.clone()); diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index 1b45a9dd..8e9f44b0 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -7,7 +7,7 @@ use libp2p::{ use serde::{Deserialize, Serialize}; use std::{fmt::Debug, io}; -use crate::SwapParams; +use crate::SwapAmounts; use xmr_btc::{alice, bob, monero}; /// Time to wait for a response back once we send a request. @@ -28,7 +28,7 @@ pub enum BobToAlice { #[derive(Clone, Debug, Serialize, Deserialize)] #[allow(clippy::large_enum_variant)] pub enum AliceToBob { - Amounts(SwapParams), + Amounts(SwapAmounts), Message0(alice::Message0), Message1(alice::Message1), }