From 6e34f9c978fdfd5374afa34308cecb479b37bacd Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 22 Oct 2020 14:12:49 +1100 Subject: [PATCH] Use const TIMEOUT instead of an argument --- swap/src/alice.rs | 16 +++++++++------- swap/src/alice/amounts.rs | 34 +++++++++++++++++++--------------- swap/src/alice/message0.rs | 35 +++++++++++++++++++---------------- swap/src/alice/message1.rs | 33 ++++++++++++++++++--------------- swap/src/bob.rs | 27 ++++++++++++++------------- swap/src/bob/amounts.rs | 34 +++++++++++++++++++--------------- swap/src/bob/message0.rs | 33 ++++++++++++++++++--------------- swap/src/bob/message1.rs | 33 ++++++++++++++++++--------------- 8 files changed, 134 insertions(+), 111 deletions(-) diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 79c1060e..09423290 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -7,7 +7,7 @@ use libp2p::{ NetworkBehaviour, PeerId, }; use rand::rngs::OsRng; -use std::{thread, time::Duration}; +use std::thread; use tracing::debug; mod amounts; @@ -18,7 +18,7 @@ use self::{amounts::*, message0::*, message1::*}; use crate::{ network::{ peer_tracker::{self, PeerTracker}, - request_response::{AliceToBob, TIMEOUT}, + request_response::AliceToBob, transport, TokioExecutor, }, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, @@ -27,6 +27,7 @@ use xmr_btc::{alice::State0, bob, monero}; pub type Swarm = libp2p::Swarm; +// FIXME: This whole function is horrible, needs total re-write. #[allow(unused_assignments)] // Due to the mutable message0? pub async fn swap( listen: Multiaddr, @@ -132,7 +133,7 @@ fn new_swarm(listen: Multiaddr) -> Result { #[derive(Debug)] pub enum OutEvent { ConnectionEstablished(PeerId), - Request(amounts::OutEvent), + Request(amounts::OutEvent), // Not-uniform with Bob on purpose, ready for adding Xmr event. Message0(bob::Message0), Message1 { msg: bob::Message1, @@ -200,10 +201,12 @@ impl Alice { self.amounts.send(channel, msg); } + /// Message0 gets sent within the network layer using this state0. pub fn set_state0(&mut self, state: State0) { let _ = self.message0.set_state(state); } + /// Send Message1 to Bob in response to receiving his Message1. pub fn send_message1( &mut self, channel: ResponseChannel, @@ -216,13 +219,12 @@ impl Alice { impl Default for Alice { fn default() -> Self { let identity = Keypair::generate_ed25519(); - let timeout = Duration::from_secs(TIMEOUT); Self { pt: PeerTracker::default(), - amounts: Amounts::new(timeout), - message0: Message0::new(timeout), - message1: Message1::new(timeout), + amounts: Amounts::default(), + message0: Message0::default(), + message1: Message1::default(), identity, } } diff --git a/swap/src/alice/amounts.rs b/swap/src/alice/amounts.rs index fb4872bc..e48fccfb 100644 --- a/swap/src/alice/amounts.rs +++ b/swap/src/alice/amounts.rs @@ -14,7 +14,7 @@ use std::{ }; use tracing::{debug, error}; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; #[derive(Debug)] pub enum OutEvent { @@ -35,20 +35,6 @@ pub struct Amounts { } impl Amounts { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - } - } - /// Alice always sends her messages as a response to a request from Bob. pub fn send(&mut self, channel: ResponseChannel, msg: AliceToBob) { self.rr.send_response(channel, msg); @@ -79,6 +65,24 @@ impl Amounts { } } +impl Default for Amounts { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + } + } +} + impl NetworkBehaviourEventProcess> for Amounts { fn inject_event(&mut self, event: RequestResponseEvent) { match event { diff --git a/swap/src/alice/message0.rs b/swap/src/alice/message0.rs index ec85d57a..77b8d5ab 100644 --- a/swap/src/alice/message0.rs +++ b/swap/src/alice/message0.rs @@ -15,7 +15,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use xmr_btc::{alice::State0, bob}; #[derive(Debug)] @@ -36,21 +36,6 @@ pub struct Message0 { } impl Message0 { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - state: None, - } - } - pub fn set_state(&mut self, state: State0) -> Result<()> { if self.state.is_some() { bail!("Trying to set state a second time"); @@ -73,6 +58,24 @@ impl Message0 { } } +impl Default for Message0 { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + state: None, + } + } +} + impl NetworkBehaviourEventProcess> for Message0 { fn inject_event(&mut self, event: RequestResponseEvent) { match event { diff --git a/swap/src/alice/message1.rs b/swap/src/alice/message1.rs index 4995e99a..bb4b368a 100644 --- a/swap/src/alice/message1.rs +++ b/swap/src/alice/message1.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use xmr_btc::bob; #[derive(Debug)] @@ -37,20 +37,6 @@ pub struct Message1 { } impl Message1 { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - } - } - pub fn send(&mut self, channel: ResponseChannel, msg: xmr_btc::alice::Message1) { let msg = AliceToBob::Message1(msg); self.rr.send_response(channel, msg); @@ -111,3 +97,20 @@ impl NetworkBehaviourEventProcess> } } } + +impl Default for Message1 { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + } + } +} diff --git a/swap/src/bob.rs b/swap/src/bob.rs index d3422b5d..22c339ee 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -7,7 +7,7 @@ use futures::{ }; use libp2p::{core::identity::Keypair, Multiaddr, NetworkBehaviour, PeerId}; use rand::rngs::OsRng; -use std::{process, thread, time::Duration}; +use std::{process, thread}; use tracing::{debug, info}; mod amounts; @@ -18,10 +18,9 @@ use self::{amounts::*, message0::*, message1::*}; use crate::{ network::{ peer_tracker::{self, PeerTracker}, - request_response::TIMEOUT, transport, TokioExecutor, }, - Cmd, Rsp, PUNISH_TIMELOCK, REFUND_TIMELOCK, + Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, }; use xmr_btc::{ alice, @@ -29,6 +28,7 @@ use xmr_btc::{ bob::{self, State0}, }; +// FIXME: This whole function is horrible, needs total re-write. pub async fn swap( btc: u64, addr: Multiaddr, @@ -52,9 +52,9 @@ where swarm.request_amounts(alice.clone(), btc); let (btc, xmr) = match swarm.next().await { - OutEvent::Amounts(amounts::OutEvent::Amounts(p)) => { - debug!("Got amounts from Alice: {:?}", p); - let cmd = Cmd::VerifyAmounts(p); + OutEvent::Amounts(amounts) => { + debug!("Got amounts from Alice: {:?}", amounts); + let cmd = Cmd::VerifyAmounts(amounts); cmd_tx.try_send(cmd)?; let response = rsp_rx.next().await; if response == Some(Rsp::Abort) { @@ -63,7 +63,7 @@ where } info!("User verified amounts, continuing with swap ..."); - (p.btc, p.xmr) + (amounts.btc, amounts.xmr) } other => panic!("unexpected event: {:?}", other), }; @@ -131,7 +131,7 @@ fn new_swarm() -> Result { #[derive(Debug)] pub enum OutEvent { ConnectionEstablished(PeerId), - Amounts(amounts::OutEvent), + Amounts(SwapAmounts), Message0(alice::Message0), Message1(alice::Message1), } @@ -148,7 +148,9 @@ impl From for OutEvent { impl From for OutEvent { fn from(event: amounts::OutEvent) -> Self { - OutEvent::Amounts(event) + match event { + amounts::OutEvent::Amounts(amounts) => OutEvent::Amounts(amounts), + } } } @@ -216,13 +218,12 @@ impl Bob { impl Default for Bob { fn default() -> Bob { let identity = Keypair::generate_ed25519(); - let timeout = Duration::from_secs(TIMEOUT); Self { pt: PeerTracker::default(), - amounts: Amounts::new(timeout), - message0: Message0::new(timeout), - message1: Message1::new(timeout), + amounts: Amounts::default(), + message0: Message0::default(), + message1: Message1::default(), identity, } } diff --git a/swap/src/bob/amounts.rs b/swap/src/bob/amounts.rs index fcdb74e0..f47f3086 100644 --- a/swap/src/bob/amounts.rs +++ b/swap/src/bob/amounts.rs @@ -15,7 +15,7 @@ use std::{ use tracing::error; use crate::{ - network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}, + network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}, SwapAmounts, }; @@ -35,20 +35,6 @@ pub struct Amounts { } impl Amounts { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - } - } - pub fn request_amounts(&mut self, alice: PeerId, btc: ::bitcoin::Amount) -> Result { let msg = BobToAlice::AmountsFromBtc(btc); let id = self.rr.send_request(&alice, msg); @@ -69,6 +55,24 @@ impl Amounts { } } +impl Default for Amounts { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + } + } +} + impl NetworkBehaviourEventProcess> for Amounts { fn inject_event(&mut self, event: RequestResponseEvent) { match event { diff --git a/swap/src/bob/message0.rs b/swap/src/bob/message0.rs index fd20c841..3f283d69 100644 --- a/swap/src/bob/message0.rs +++ b/swap/src/bob/message0.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use xmr_btc::{alice, bob}; #[derive(Debug)] @@ -32,20 +32,6 @@ pub struct Message0 { } impl Message0 { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - } - } - pub fn send(&mut self, alice: PeerId, msg: bob::Message0) { let msg = BobToAlice::Message0(msg); let _id = self.rr.send_request(&alice, msg); @@ -64,6 +50,23 @@ impl Message0 { } } +impl Default for Message0 { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + } + } +} + impl NetworkBehaviourEventProcess> for Message0 { fn inject_event(&mut self, event: RequestResponseEvent) { match event { diff --git a/swap/src/bob/message1.rs b/swap/src/bob/message1.rs index 43d24850..979957be 100644 --- a/swap/src/bob/message1.rs +++ b/swap/src/bob/message1.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use xmr_btc::{alice, bob}; #[derive(Debug)] @@ -32,20 +32,6 @@ pub struct Message1 { } impl Message1 { - pub fn new(timeout: Duration) -> Self { - let mut config = RequestResponseConfig::default(); - config.set_request_timeout(timeout); - - Self { - rr: RequestResponse::new( - Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], - config, - ), - events: Default::default(), - } - } - pub fn send(&mut self, alice: PeerId, msg: bob::Message1) { let msg = BobToAlice::Message1(msg); let _id = self.rr.send_request(&alice, msg); @@ -64,6 +50,23 @@ impl Message1 { } } +impl Default for Message1 { + fn default() -> Self { + let timeout = Duration::from_secs(TIMEOUT); + let mut config = RequestResponseConfig::default(); + config.set_request_timeout(timeout); + + Self { + rr: RequestResponse::new( + Codec::default(), + vec![(Protocol, ProtocolSupport::Full)], + config, + ), + events: Default::default(), + } + } +} + impl NetworkBehaviourEventProcess> for Message1 { fn inject_event(&mut self, event: RequestResponseEvent) { match event {