Use const TIMEOUT instead of an argument

This commit is contained in:
Tobin C. Harding 2020-10-22 14:12:49 +11:00
parent cc61079826
commit 6e34f9c978
8 changed files with 134 additions and 111 deletions

View File

@ -7,7 +7,7 @@ use libp2p::{
NetworkBehaviour, PeerId, NetworkBehaviour, PeerId,
}; };
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::{thread, time::Duration}; use std::thread;
use tracing::debug; use tracing::debug;
mod amounts; mod amounts;
@ -18,7 +18,7 @@ use self::{amounts::*, message0::*, message1::*};
use crate::{ use crate::{
network::{ network::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
request_response::{AliceToBob, TIMEOUT}, request_response::AliceToBob,
transport, TokioExecutor, transport, TokioExecutor,
}, },
SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
@ -27,6 +27,7 @@ use xmr_btc::{alice::State0, bob, monero};
pub type Swarm = libp2p::Swarm<Alice>; pub type Swarm = libp2p::Swarm<Alice>;
// FIXME: This whole function is horrible, needs total re-write.
#[allow(unused_assignments)] // Due to the mutable message0? #[allow(unused_assignments)] // Due to the mutable message0?
pub async fn swap( pub async fn swap(
listen: Multiaddr, listen: Multiaddr,
@ -132,7 +133,7 @@ fn new_swarm(listen: Multiaddr) -> Result<Swarm> {
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
ConnectionEstablished(PeerId), ConnectionEstablished(PeerId),
Request(amounts::OutEvent), Request(amounts::OutEvent), // Not-uniform with Bob on purpose, ready for adding Xmr event.
Message0(bob::Message0), Message0(bob::Message0),
Message1 { Message1 {
msg: bob::Message1, msg: bob::Message1,
@ -200,10 +201,12 @@ impl Alice {
self.amounts.send(channel, msg); self.amounts.send(channel, msg);
} }
/// Message0 gets sent within the network layer using this state0.
pub fn set_state0(&mut self, state: State0) { pub fn set_state0(&mut self, state: State0) {
let _ = self.message0.set_state(state); let _ = self.message0.set_state(state);
} }
/// Send Message1 to Bob in response to receiving his Message1.
pub fn send_message1( pub fn send_message1(
&mut self, &mut self,
channel: ResponseChannel<AliceToBob>, channel: ResponseChannel<AliceToBob>,
@ -216,13 +219,12 @@ impl Alice {
impl Default for Alice { impl Default for Alice {
fn default() -> Self { fn default() -> Self {
let identity = Keypair::generate_ed25519(); let identity = Keypair::generate_ed25519();
let timeout = Duration::from_secs(TIMEOUT);
Self { Self {
pt: PeerTracker::default(), pt: PeerTracker::default(),
amounts: Amounts::new(timeout), amounts: Amounts::default(),
message0: Message0::new(timeout), message0: Message0::default(),
message1: Message1::new(timeout), message1: Message1::default(),
identity, identity,
} }
} }

View File

@ -14,7 +14,7 @@ use std::{
}; };
use tracing::{debug, error}; 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)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
@ -35,20 +35,6 @@ pub struct Amounts {
} }
impl 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. /// Alice always sends her messages as a response to a request from Bob.
pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: AliceToBob) { pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: AliceToBob) {
self.rr.send_response(channel, msg); 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts { impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts {
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {

View File

@ -15,7 +15,7 @@ use std::{
}; };
use tracing::error; 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}; use xmr_btc::{alice::State0, bob};
#[derive(Debug)] #[derive(Debug)]
@ -36,21 +36,6 @@ pub struct Message0 {
} }
impl 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<()> { pub fn set_state(&mut self, state: State0) -> Result<()> {
if self.state.is_some() { if self.state.is_some() {
bail!("Trying to set state a second time"); 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 { impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {

View File

@ -13,7 +13,7 @@ use std::{
}; };
use tracing::error; 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; use xmr_btc::bob;
#[derive(Debug)] #[derive(Debug)]
@ -37,20 +37,6 @@ pub struct Message1 {
} }
impl 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<AliceToBob>, msg: xmr_btc::alice::Message1) { pub fn send(&mut self, channel: ResponseChannel<AliceToBob>, msg: xmr_btc::alice::Message1) {
let msg = AliceToBob::Message1(msg); let msg = AliceToBob::Message1(msg);
self.rr.send_response(channel, msg); self.rr.send_response(channel, msg);
@ -111,3 +97,20 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
} }
} }
} }
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(),
}
}
}

View File

@ -7,7 +7,7 @@ use futures::{
}; };
use libp2p::{core::identity::Keypair, Multiaddr, NetworkBehaviour, PeerId}; use libp2p::{core::identity::Keypair, Multiaddr, NetworkBehaviour, PeerId};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::{process, thread, time::Duration}; use std::{process, thread};
use tracing::{debug, info}; use tracing::{debug, info};
mod amounts; mod amounts;
@ -18,10 +18,9 @@ use self::{amounts::*, message0::*, message1::*};
use crate::{ use crate::{
network::{ network::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
request_response::TIMEOUT,
transport, TokioExecutor, transport, TokioExecutor,
}, },
Cmd, Rsp, PUNISH_TIMELOCK, REFUND_TIMELOCK, Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
}; };
use xmr_btc::{ use xmr_btc::{
alice, alice,
@ -29,6 +28,7 @@ use xmr_btc::{
bob::{self, State0}, bob::{self, State0},
}; };
// FIXME: This whole function is horrible, needs total re-write.
pub async fn swap<W>( pub async fn swap<W>(
btc: u64, btc: u64,
addr: Multiaddr, addr: Multiaddr,
@ -52,9 +52,9 @@ where
swarm.request_amounts(alice.clone(), btc); swarm.request_amounts(alice.clone(), btc);
let (btc, xmr) = match swarm.next().await { let (btc, xmr) = match swarm.next().await {
OutEvent::Amounts(amounts::OutEvent::Amounts(p)) => { OutEvent::Amounts(amounts) => {
debug!("Got amounts from Alice: {:?}", p); debug!("Got amounts from Alice: {:?}", amounts);
let cmd = Cmd::VerifyAmounts(p); let cmd = Cmd::VerifyAmounts(amounts);
cmd_tx.try_send(cmd)?; cmd_tx.try_send(cmd)?;
let response = rsp_rx.next().await; let response = rsp_rx.next().await;
if response == Some(Rsp::Abort) { if response == Some(Rsp::Abort) {
@ -63,7 +63,7 @@ where
} }
info!("User verified amounts, continuing with swap ..."); info!("User verified amounts, continuing with swap ...");
(p.btc, p.xmr) (amounts.btc, amounts.xmr)
} }
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
}; };
@ -131,7 +131,7 @@ fn new_swarm() -> Result<Swarm> {
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
ConnectionEstablished(PeerId), ConnectionEstablished(PeerId),
Amounts(amounts::OutEvent), Amounts(SwapAmounts),
Message0(alice::Message0), Message0(alice::Message0),
Message1(alice::Message1), Message1(alice::Message1),
} }
@ -148,7 +148,9 @@ impl From<peer_tracker::OutEvent> for OutEvent {
impl From<amounts::OutEvent> for OutEvent { impl From<amounts::OutEvent> for OutEvent {
fn from(event: amounts::OutEvent) -> Self { 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 { impl Default for Bob {
fn default() -> Bob { fn default() -> Bob {
let identity = Keypair::generate_ed25519(); let identity = Keypair::generate_ed25519();
let timeout = Duration::from_secs(TIMEOUT);
Self { Self {
pt: PeerTracker::default(), pt: PeerTracker::default(),
amounts: Amounts::new(timeout), amounts: Amounts::default(),
message0: Message0::new(timeout), message0: Message0::default(),
message1: Message1::new(timeout), message1: Message1::default(),
identity, identity,
} }
} }

View File

@ -15,7 +15,7 @@ use std::{
use tracing::error; use tracing::error;
use crate::{ use crate::{
network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}, network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT},
SwapAmounts, SwapAmounts,
}; };
@ -35,20 +35,6 @@ pub struct Amounts {
} }
impl 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<RequestId> { pub fn request_amounts(&mut self, alice: PeerId, btc: ::bitcoin::Amount) -> Result<RequestId> {
let msg = BobToAlice::AmountsFromBtc(btc); let msg = BobToAlice::AmountsFromBtc(btc);
let id = self.rr.send_request(&alice, msg); 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts { impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts {
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {

View File

@ -13,7 +13,7 @@ use std::{
}; };
use tracing::error; 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}; use xmr_btc::{alice, bob};
#[derive(Debug)] #[derive(Debug)]
@ -32,20 +32,6 @@ pub struct Message0 {
} }
impl 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) { pub fn send(&mut self, alice: PeerId, msg: bob::Message0) {
let msg = BobToAlice::Message0(msg); let msg = BobToAlice::Message0(msg);
let _id = self.rr.send_request(&alice, 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 { impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {

View File

@ -13,7 +13,7 @@ use std::{
}; };
use tracing::error; 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}; use xmr_btc::{alice, bob};
#[derive(Debug)] #[derive(Debug)]
@ -32,20 +32,6 @@ pub struct Message1 {
} }
impl 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) { pub fn send(&mut self, alice: PeerId, msg: bob::Message1) {
let msg = BobToAlice::Message1(msg); let msg = BobToAlice::Message1(msg);
let _id = self.rr.send_request(&alice, 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message1 { impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>> for Message1 {
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {