mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-24 06:59:36 -05:00
Use const TIMEOUT instead of an argument
This commit is contained in:
parent
cc61079826
commit
6e34f9c978
@ -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<Alice>;
|
||||
|
||||
// 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<Swarm> {
|
||||
#[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<AliceToBob>,
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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<AliceToBob>, 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<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts {
|
||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
||||
match event {
|
||||
|
@ -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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
|
||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
||||
match event {
|
||||
|
@ -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<AliceToBob>, msg: xmr_btc::alice::Message1) {
|
||||
let msg = AliceToBob::Message1(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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<W>(
|
||||
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<Swarm> {
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
ConnectionEstablished(PeerId),
|
||||
Amounts(amounts::OutEvent),
|
||||
Amounts(SwapAmounts),
|
||||
Message0(alice::Message0),
|
||||
Message1(alice::Message1),
|
||||
}
|
||||
@ -148,7 +148,9 @@ impl From<peer_tracker::OutEvent> for OutEvent {
|
||||
|
||||
impl From<amounts::OutEvent> 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,
|
||||
}
|
||||
}
|
||||
|
@ -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<RequestId> {
|
||||
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<RequestResponseEvent<BobToAlice, AliceToBob>> for Amounts {
|
||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
||||
match event {
|
||||
|
@ -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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message0 {
|
||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
||||
match event {
|
||||
|
@ -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<RequestResponseEvent<BobToAlice, AliceToBob>> for Message1 {
|
||||
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
|
||||
match event {
|
||||
|
Loading…
Reference in New Issue
Block a user