diff --git a/swap/src/alice.rs b/swap/src/alice.rs index dd095be3..1d45ca5e 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -98,6 +98,7 @@ pub async fn swap( let mut swarm = new_swarm(listen, local_port)?; let message0: bob::Message0; + let mut state0: Option = None; let mut last_amounts: Option = None; loop { @@ -112,6 +113,29 @@ pub async fn swap( // verification of message 0. last_amounts = Some(amounts); swarm.send_amounts(channel, amounts); + + let (xmr, btc) = match last_amounts { + Some(p) => (p.xmr, p.btc), + None => unreachable!("should have amounts by here"), + }; + + let redeem_address = bitcoin_wallet.as_ref().new_address().await?; + let punish_address = redeem_address.clone(); + + // TODO: Pass this in using + let rng = &mut OsRng; + let state = State0::new( + rng, + btc, + xmr, + REFUND_TIMELOCK, + PUNISH_TIMELOCK, + redeem_address, + punish_address, + ); + swarm.set_state0(state.clone()); + + state0 = Some(state) } OutEvent::Message0(msg) => { debug!("got message 0 from Bob"); @@ -127,28 +151,10 @@ pub async fn swap( }; } - let (xmr, btc) = match last_amounts { - Some(p) => (p.xmr, p.btc), - None => unreachable!("should have amounts by here"), - }; - - let redeem_address = bitcoin_wallet.as_ref().new_address().await?; - let punish_address = redeem_address.clone(); - - // TODO: Pass this in using - let rng = &mut OsRng; - let state0 = State0::new( - rng, - btc, - xmr, - REFUND_TIMELOCK, - PUNISH_TIMELOCK, - redeem_address, - punish_address, - ); - swarm.set_state0(state0.clone()); - - let state1 = state0.receive(message0).expect("failed to receive msg 0"); + let state1 = state0 + .expect("to be set") + .receive(message0) + .expect("failed to receive msg 0"); let (state2, channel) = match swarm.next().await { OutEvent::Message1 { msg, channel } => { diff --git a/swap/src/alice/amounts.rs b/swap/src/alice/amounts.rs index e0fb3bcd..85dd5b92 100644 --- a/swap/src/alice/amounts.rs +++ b/swap/src/alice/amounts.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; +use crate::network::request_response::{AliceToBob, AmountsProtocol, BobToAlice, Codec, TIMEOUT}; #[derive(Debug)] pub enum OutEvent { @@ -28,7 +28,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Amounts { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -43,7 +43,7 @@ impl Amounts { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -62,7 +62,7 @@ impl Default for Amounts { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(AmountsProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/alice/message0.rs b/swap/src/alice/message0.rs index 04c5222d..45bb248c 100644 --- a/swap/src/alice/message0.rs +++ b/swap/src/alice/message0.rs @@ -15,7 +15,7 @@ use std::{ }; use tracing::{error, info}; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}; use xmr_btc::{alice::State0, bob}; #[derive(Debug)] @@ -28,7 +28,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message0 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, #[behaviour(ignore)] @@ -49,7 +49,7 @@ impl Message0 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -67,7 +67,7 @@ impl Default for Message0 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message0Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/alice/message1.rs b/swap/src/alice/message1.rs index d9831439..11e578ad 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, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}; use xmr_btc::bob; #[derive(Debug)] @@ -31,7 +31,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message1 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -46,7 +46,7 @@ impl Message1 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -64,7 +64,7 @@ impl Default for Message1 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message1Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/alice/message2.rs b/swap/src/alice/message2.rs index 584a16bf..e1493ba4 100644 --- a/swap/src/alice/message2.rs +++ b/swap/src/alice/message2.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}; use xmr_btc::bob; #[derive(Debug)] @@ -31,7 +31,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message2 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -46,7 +46,7 @@ impl Message2 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -64,7 +64,7 @@ impl Default for Message2 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message2Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/alice/message3.rs b/swap/src/alice/message3.rs index 4ee197c3..48afcc5d 100644 --- a/swap/src/alice/message3.rs +++ b/swap/src/alice/message3.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}; use xmr_btc::bob; #[derive(Debug)] @@ -26,7 +26,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message3 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -36,7 +36,7 @@ impl Message3 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -54,7 +54,7 @@ impl Default for Message3 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message3Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/bob.rs b/swap/src/bob.rs index aefb3478..a09e99b6 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -90,7 +90,7 @@ pub async fn swap( swarm.request_amounts(alice.clone(), btc); - let (btc, xmr) = match swarm.next().await { + let (btc_amount, xmr) = match swarm.next().await { OutEvent::Amounts(amounts) => { debug!("Got amounts from Alice: {:?}", amounts); let cmd = Cmd::VerifyAmounts(amounts); @@ -107,13 +107,22 @@ pub async fn swap( other => panic!("unexpected event: {:?}", other), }; + swarm.request_amounts(alice.clone(), btc); + + match swarm.next().await { + OutEvent::Amounts(amounts) => { + debug!("Got amounts from Alice: {:?}", amounts); + } + other => panic!("unexpected event: {:?}", other), + }; + let refund_address = bitcoin_wallet.new_address().await?; // TODO: Pass this in using let rng = &mut OsRng; let state0 = State0::new( rng, - btc, + btc_amount, xmr, REFUND_TIMELOCK, PUNISH_TIMELOCK, diff --git a/swap/src/bob/amounts.rs b/swap/src/bob/amounts.rs index 6c4c0395..f2fd0266 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, TIMEOUT}, + network::request_response::{AliceToBob, AmountsProtocol, BobToAlice, Codec, TIMEOUT}, SwapAmounts, }; @@ -29,7 +29,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Amounts { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -46,7 +46,7 @@ impl Amounts { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -65,7 +65,7 @@ impl Default for Amounts { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(AmountsProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/bob/message0.rs b/swap/src/bob/message0.rs index ff6c9420..455b7452 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, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message0Protocol, TIMEOUT}; use xmr_btc::{alice, bob}; #[derive(Debug)] @@ -26,7 +26,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message0 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -41,7 +41,7 @@ impl Message0 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -59,7 +59,7 @@ impl Default for Message0 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message0Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/bob/message1.rs b/swap/src/bob/message1.rs index 0a821ec2..e3a67de9 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, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message1Protocol, TIMEOUT}; use xmr_btc::{alice, bob}; #[derive(Debug)] @@ -26,7 +26,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message1 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -41,7 +41,7 @@ impl Message1 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -59,7 +59,7 @@ impl Default for Message1 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message1Protocol, ProtocolSupport::Full)], config, ), events: Default::default(), diff --git a/swap/src/bob/message2.rs b/swap/src/bob/message2.rs index e90755b7..b141b240 100644 --- a/swap/src/bob/message2.rs +++ b/swap/src/bob/message2.rs @@ -13,7 +13,7 @@ use std::{ }; use tracing::error; -use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; +use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Message2Protocol, TIMEOUT}; use xmr_btc::{alice, bob}; #[derive(Debug)] @@ -26,7 +26,7 @@ pub enum OutEvent { #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message2 { - rr: RequestResponse, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -41,7 +41,7 @@ impl Message2 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, OutEvent>> { + ) -> Poll>, OutEvent>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -59,7 +59,7 @@ impl Default for Message2 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message2Protocol, ProtocolSupport::Full)], config, ), events: VecDeque::default(), diff --git a/swap/src/bob/message3.rs b/swap/src/bob/message3.rs index 53648460..02fcd0c4 100644 --- a/swap/src/bob/message3.rs +++ b/swap/src/bob/message3.rs @@ -13,7 +13,7 @@ use std::{ use tracing::{debug, error}; use crate::{ - network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}, + network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, Never, }; use xmr_btc::bob; @@ -23,7 +23,7 @@ use xmr_btc::bob; #[behaviour(out_event = "Never", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Message3 { - rr: RequestResponse, + rr: RequestResponse>, } impl Message3 { @@ -38,7 +38,7 @@ impl Message3 { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll, Never>> { + ) -> Poll>, Never>> { Poll::Pending } } @@ -52,7 +52,7 @@ impl Default for Message3 { Self { rr: RequestResponse::new( Codec::default(), - vec![(Protocol, ProtocolSupport::Full)], + vec![(Message3Protocol, ProtocolSupport::Full)], config, ), } diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index 6f033ae6..a5950e14 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -5,8 +5,7 @@ use libp2p::{ request_response::{ProtocolName, RequestResponseCodec}, }; use serde::{Deserialize, Serialize}; -use std::{fmt::Debug, io}; -use tracing::debug; +use std::{fmt::Debug, io, marker::PhantomData}; use crate::SwapAmounts; use xmr_btc::{alice, bob, monero}; @@ -45,20 +44,61 @@ pub enum AliceToBob { } #[derive(Debug, Clone, Copy, Default)] -pub struct Protocol; +pub struct AmountsProtocol; -impl ProtocolName for Protocol { +#[derive(Debug, Clone, Copy, Default)] +pub struct Message0Protocol; + +#[derive(Debug, Clone, Copy, Default)] +pub struct Message1Protocol; + +#[derive(Debug, Clone, Copy, Default)] +pub struct Message2Protocol; + +#[derive(Debug, Clone, Copy, Default)] +pub struct Message3Protocol; + +impl ProtocolName for AmountsProtocol { fn protocol_name(&self) -> &[u8] { - b"/xmr/btc/1.0.0" + b"/xmr/btc/amounts/1.0.0" + } +} + +impl ProtocolName for Message0Protocol { + fn protocol_name(&self) -> &[u8] { + b"/xmr/btc/message0/1.0.0" + } +} + +impl ProtocolName for Message1Protocol { + fn protocol_name(&self) -> &[u8] { + b"/xmr/btc/message1/1.0.0" + } +} + +impl ProtocolName for Message2Protocol { + fn protocol_name(&self) -> &[u8] { + b"/xmr/btc/message2/1.0.0" + } +} + +impl ProtocolName for Message3Protocol { + fn protocol_name(&self) -> &[u8] { + b"/xmr/btc/message3/1.0.0" } } #[derive(Clone, Copy, Debug, Default)] -pub struct Codec; +pub struct Codec

{ + phantom: PhantomData

, +} #[async_trait] -impl RequestResponseCodec for Codec { - type Protocol = Protocol; +impl

RequestResponseCodec for Codec

+where + P: Send + Sync + Clone + ProtocolName, +{ + type Protocol = P; type Request = BobToAlice; type Response = AliceToBob; @@ -109,11 +149,8 @@ impl RequestResponseCodec for Codec { where T: AsyncWrite + Unpin + Send, { - debug!("enter write_request"); - let bytes = serde_cbor::to_vec(&req).map_err(|e| { - tracing::debug!("serde write_request error: {:?}", e); - io::Error::new(io::ErrorKind::InvalidData, e) - })?; + let bytes = + serde_cbor::to_vec(&req).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; upgrade::write_one(io, &bytes).await?; diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 8a4c0a34..66c26857 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -13,7 +13,7 @@ async fn swap() { .with_env_filter( "swap=debug,hyper=off,reqwest=off,monero-harness=info,testcontainers=info,libp2p=debug", ) - .with_ansi(true) + .with_ansi(false) .set_default(); let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876"