From 8fd2620b83eac808cea62733190a9ca538695493 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 27 Jan 2021 14:14:20 +1100 Subject: [PATCH] Improve names for messages 4 and 5 --- docs/sequence.puml | 4 +- swap/src/network/request_response.rs | 22 +++++----- swap/src/protocol/alice.rs | 36 +++++++-------- .../{message5.rs => encrypted_signature.rs} | 26 ++++++----- swap/src/protocol/alice/event_loop.rs | 44 +++++++++---------- swap/src/protocol/alice/state.rs | 17 +++---- swap/src/protocol/alice/steps.rs | 6 +-- .../alice/{message4.rs => transfer_proof.rs} | 21 ++++----- swap/src/protocol/bob.rs | 39 ++++++++-------- .../{message5.rs => encrypted_signature.rs} | 26 +++++------ swap/src/protocol/bob/event_loop.rs | 42 +++++++++--------- swap/src/protocol/bob/state.rs | 22 ++++------ swap/src/protocol/bob/swap.rs | 8 ++-- .../bob/{message4.rs => transfer_proof.rs} | 21 ++++----- 14 files changed, 168 insertions(+), 166 deletions(-) rename swap/src/protocol/alice/{message5.rs => encrypted_signature.rs} (78%) rename swap/src/protocol/alice/{message4.rs => transfer_proof.rs} (79%) rename swap/src/protocol/bob/{message5.rs => encrypted_signature.rs} (76%) rename swap/src/protocol/bob/{message4.rs => transfer_proof.rs} (82%) diff --git a/docs/sequence.puml b/docs/sequence.puml index 189856db..11638d4b 100644 --- a/docs/sequence.puml +++ b/docs/sequence.puml @@ -46,10 +46,10 @@ group Execution Alice ->> Monero: Lock - Alice -> Bob: Message4 + Alice -> Bob: TransferProof note right: xmr lock tx transfer proof\nThis can be removed if Bob watches the blockchain. - Bob -> Alice: Message5 + Bob -> Alice: EncryptedSignature note left: redeem tx enc sig S_a Alice ->> Bitcoin: Redeem diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index 420e6594..fa5158d5 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -1,4 +1,4 @@ -use crate::protocol::{alice, alice::Message4, bob, bob::Message5}; +use crate::protocol::{alice, alice::TransferProof, bob, bob::EncryptedSignature}; use async_trait::async_trait; use futures::prelude::*; use libp2p::{ @@ -40,15 +40,15 @@ pub enum AliceToBob { /// All responses are empty #[derive(Clone, Debug, Serialize, Deserialize)] pub enum Request { - Message4(Box), - Message5(Box), + TransferProof(Box), + EncryptedSignature(Box), } #[derive(Clone, Copy, Debug, Serialize, Deserialize)] /// Response are only used for acknowledgement purposes. pub enum Response { - Message4, - Message5, + TransferProof, + EncryptedSignature, } #[derive(Debug, Clone, Copy, Default)] @@ -64,10 +64,10 @@ pub struct Message1Protocol; pub struct Message2Protocol; #[derive(Debug, Clone, Copy, Default)] -pub struct Message4Protocol; +pub struct TransferProofProtocol; #[derive(Debug, Clone, Copy, Default)] -pub struct Message5Protocol; +pub struct EncryptedSignatureProtocol; impl ProtocolName for Swap { fn protocol_name(&self) -> &[u8] { @@ -93,15 +93,15 @@ impl ProtocolName for Message2Protocol { } } -impl ProtocolName for Message4Protocol { +impl ProtocolName for TransferProofProtocol { fn protocol_name(&self) -> &[u8] { - b"/xmr/btc/message4/1.0.0" + b"/xmr/btc/transfer_proof/1.0.0" } } -impl ProtocolName for Message5Protocol { +impl ProtocolName for EncryptedSignatureProtocol { fn protocol_name(&self) -> &[u8] { - b"/xmr/btc/message5/1.0.0" + b"/xmr/btc/encrypted_signature/1.0.0" } } diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 34827dc8..22af28df 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -4,10 +4,10 @@ pub use self::{ event_loop::{EventLoop, EventLoopHandle}, message0::Message0, message1::Message1, - message4::Message4, state::*, swap::{run, run_until}, swap_response::*, + transfer_proof::TransferProof, }; use crate::{ bitcoin, @@ -21,7 +21,7 @@ use crate::{ transport::build, Seed as NetworkSeed, }, - protocol::{bob, bob::Message5, SwapAmounts}, + protocol::{bob, bob::EncryptedSignature, SwapAmounts}, seed::Seed, }; use anyhow::{bail, Result}; @@ -33,16 +33,16 @@ use std::{path::PathBuf, sync::Arc}; use tracing::{debug, info}; use uuid::Uuid; +mod encrypted_signature; pub mod event_loop; mod message0; mod message1; mod message2; -mod message4; -mod message5; pub mod state; mod steps; pub mod swap; mod swap_response; +mod transfer_proof; pub struct Swap { pub state: AliceState, @@ -236,8 +236,8 @@ pub enum OutEvent { msg: Box, bob_peer_id: PeerId, }, - Message4, - Message5(Message5), + TransferProof, + EncryptedSignature(EncryptedSignature), } impl From for OutEvent { @@ -286,18 +286,18 @@ impl From for OutEvent { } } -impl From for OutEvent { - fn from(event: message4::OutEvent) -> Self { +impl From for OutEvent { + fn from(event: transfer_proof::OutEvent) -> Self { match event { - message4::OutEvent::Msg => OutEvent::Message4, + transfer_proof::OutEvent::Msg => OutEvent::TransferProof, } } } -impl From for OutEvent { - fn from(event: message5::OutEvent) -> Self { +impl From for OutEvent { + fn from(event: encrypted_signature::OutEvent) -> Self { match event { - message5::OutEvent::Msg(msg) => OutEvent::Message5(msg), + encrypted_signature::OutEvent::Msg(msg) => OutEvent::EncryptedSignature(msg), } } } @@ -312,8 +312,8 @@ pub struct Behaviour { message0: message0::Behaviour, message1: message1::Behaviour, message2: message2::Behaviour, - message4: message4::Behaviour, - message5: message5::Behaviour, + transfer_proof: transfer_proof::Behaviour, + encrypted_signature: encrypted_signature::Behaviour, } impl Behaviour { @@ -339,9 +339,9 @@ impl Behaviour { debug!("Sent Message1"); } - /// Send Message4 to Bob. - pub fn send_message4(&mut self, bob: PeerId, msg: Message4) { - self.message4.send(bob, msg); - debug!("Sent Message 4"); + /// Send Transfer Proof to Bob. + pub fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) { + self.transfer_proof.send(bob, msg); + debug!("Sent Transfer Proof"); } } diff --git a/swap/src/protocol/alice/message5.rs b/swap/src/protocol/alice/encrypted_signature.rs similarity index 78% rename from swap/src/protocol/alice/message5.rs rename to swap/src/protocol/alice/encrypted_signature.rs index f285de7c..b62cf1de 100644 --- a/swap/src/protocol/alice/message5.rs +++ b/swap/src/protocol/alice/encrypted_signature.rs @@ -1,6 +1,8 @@ use crate::{ - network::request_response::{Message5Protocol, OneShotCodec, Request, Response, TIMEOUT}, - protocol::bob::Message5, + network::request_response::{ + EncryptedSignatureProtocol, OneShotCodec, Request, Response, TIMEOUT, + }, + protocol::bob::EncryptedSignature, }; use libp2p::{ request_response::{ @@ -19,15 +21,16 @@ use tracing::{debug, error}; #[derive(Debug)] pub enum OutEvent { - Msg(Message5), + Msg(EncryptedSignature), } -/// A `NetworkBehaviour` that represents receiving of message 5 from Bob. +/// A `NetworkBehaviour` that represents receiving the Bitcoin encrypted +/// signature from Bob. #[derive(NetworkBehaviour)] #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Behaviour { - rr: RequestResponse>, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -37,8 +40,9 @@ impl Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>, OutEvent>> - { + ) -> Poll< + NetworkBehaviourAction>, OutEvent>, + > { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -56,7 +60,7 @@ impl Default for Behaviour { Self { rr: RequestResponse::new( OneShotCodec::default(), - vec![(Message5Protocol, ProtocolSupport::Full)], + vec![(EncryptedSignatureProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), @@ -74,11 +78,11 @@ impl NetworkBehaviourEventProcess> for B }, .. } => { - if let Request::Message5(msg) = request { - debug!("Received message 5"); + if let Request::EncryptedSignature(msg) = request { + debug!("Received encrypted signature"); self.events.push_back(OutEvent::Msg(*msg)); // Send back empty response so that the request/response protocol completes. - let _ = self.rr.send_response(channel, Response::Message5); + let _ = self.rr.send_response(channel, Response::EncryptedSignature); } } RequestResponseEvent::Message { diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 2d0c0a7b..424a3c74 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -2,9 +2,9 @@ use crate::{ network::{request_response::AliceToBob, transport::SwapTransport, TokioExecutor}, protocol::{ alice, - alice::{Behaviour, Message4, OutEvent, SwapResponse}, + alice::{Behaviour, OutEvent, SwapResponse, TransferProof}, bob, - bob::Message5, + bob::EncryptedSignature, }, }; use anyhow::{anyhow, Context, Result}; @@ -39,13 +39,13 @@ pub struct EventLoopHandle { msg0: Receiver<(bob::Message0, ResponseChannel)>, msg1: Receiver<(bob::Message1, ResponseChannel)>, msg2: Receiver, - msg5: Receiver, + r_encrypted_signature: Receiver, request: Receiver, conn_established: Receiver, send_swap_response: Sender<(ResponseChannel, SwapResponse)>, send_msg0: Sender<(ResponseChannel, alice::Message0)>, send_msg1: Sender<(ResponseChannel, alice::Message1)>, - send_msg4: Sender<(PeerId, Message4)>, + s_transfer_proof: Sender<(PeerId, TransferProof)>, } impl EventLoopHandle { @@ -77,8 +77,8 @@ impl EventLoopHandle { .ok_or_else(|| anyhow!("Failed to receive message 2 from Bob")) } - pub async fn recv_message5(&mut self) -> Result { - self.msg5 + pub async fn recv_encrypted_signature(&mut self) -> Result { + self.r_encrypted_signature .recv() .await .ok_or_else(|| anyhow!("Failed to receive Bitcoin encrypted signature from Bob")) @@ -123,8 +123,8 @@ impl EventLoopHandle { Ok(()) } - pub async fn send_message4(&mut self, bob: PeerId, msg: Message4) -> Result<()> { - let _ = self.send_msg4.send((bob, msg)).await?; + pub async fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) -> Result<()> { + let _ = self.s_transfer_proof.send((bob, msg)).await?; Ok(()) } } @@ -135,13 +135,13 @@ pub struct EventLoop { msg0: Sender<(bob::Message0, ResponseChannel)>, msg1: Sender<(bob::Message1, ResponseChannel)>, msg2: Sender, - msg5: Sender, + r_encrypted_signature: Sender, request: Sender, conn_established: Sender, send_swap_response: Receiver<(ResponseChannel, SwapResponse)>, send_msg0: Receiver<(ResponseChannel, alice::Message0)>, send_msg1: Receiver<(ResponseChannel, alice::Message1)>, - send_msg4: Receiver<(PeerId, Message4)>, + s_transfer_proof: Receiver<(PeerId, TransferProof)>, } impl EventLoop { @@ -163,39 +163,39 @@ impl EventLoop { let msg0 = Channels::new(); let msg1 = Channels::new(); let msg2 = Channels::new(); - let msg5 = Channels::new(); + let r_encrypted_signature = Channels::new(); let request = Channels::new(); let conn_established = Channels::new(); let send_swap_response = Channels::new(); let send_msg0 = Channels::new(); let send_msg1 = Channels::new(); - let send_msg4 = Channels::new(); + let s_transfer_proof = Channels::new(); let driver = EventLoop { swarm, msg0: msg0.sender, msg1: msg1.sender, msg2: msg2.sender, - msg5: msg5.sender, + r_encrypted_signature: r_encrypted_signature.sender, request: request.sender, conn_established: conn_established.sender, send_swap_response: send_swap_response.receiver, send_msg0: send_msg0.receiver, send_msg1: send_msg1.receiver, - send_msg4: send_msg4.receiver, + s_transfer_proof: s_transfer_proof.receiver, }; let handle = EventLoopHandle { msg0: msg0.receiver, msg1: msg1.receiver, msg2: msg2.receiver, - msg5: msg5.receiver, + r_encrypted_signature: r_encrypted_signature.receiver, request: request.receiver, conn_established: conn_established.receiver, send_swap_response: send_swap_response.sender, send_msg0: send_msg0.sender, send_msg1: send_msg1.sender, - send_msg4: send_msg4.sender, + s_transfer_proof: s_transfer_proof.sender, }; Ok((driver, handle)) @@ -218,9 +218,9 @@ impl EventLoop { OutEvent::Message2 { msg, bob_peer_id : _} => { let _ = self.msg2.send(*msg).await; } - OutEvent::Message4 => trace!("Bob ack'd message 4"), - OutEvent::Message5(msg) => { - let _ = self.msg5.send(msg).await; + OutEvent::TransferProof => trace!("Bob ack'd receiving the transfer proof"), + OutEvent::EncryptedSignature(msg) => { + let _ = self.r_encrypted_signature.send(msg).await; } OutEvent::Request(event) => { let _ = self.request.send(*event).await; @@ -242,9 +242,9 @@ impl EventLoop { self.swarm.send_message1(channel, msg); } }, - msg4 = self.send_msg4.next().fuse() => { - if let Some((bob_peer_id, msg)) = msg4 { - self.swarm.send_message4(bob_peer_id, msg); + transfer_proof = self.s_transfer_proof.next().fuse() => { + if let Some((bob_peer_id, msg)) = transfer_proof { + self.swarm.send_transfer_proof(bob_peer_id, msg); } }, } diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 13145eb2..c88e23c0 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -8,13 +8,10 @@ use crate::{ }, monero, monero::CreateWalletForOutput, - protocol::{alice, alice::Message4, bob, bob::Message5, SwapAmounts}, + protocol::{alice, alice::TransferProof, bob, bob::EncryptedSignature, SwapAmounts}, }; use anyhow::{anyhow, Context, Result}; -use ecdsa_fun::{ - adaptor::{Adaptor, EncryptedSignature}, - nonce::Deterministic, -}; +use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; use libp2p::PeerId; use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; @@ -42,7 +39,7 @@ pub enum AliceState { state3: Box, }, EncSigLearned { - encrypted_signature: EncryptedSignature, + encrypted_signature: bitcoin::EncryptedSignature, state3: Box, }, BtcRedeemed, @@ -475,13 +472,13 @@ pub struct State5 { } impl State5 { - pub fn next_message(&self) -> Message4 { - Message4 { + pub fn next_message(&self) -> TransferProof { + TransferProof { tx_lock_proof: self.tx_lock_proof.clone(), } } - pub fn receive(self, msg: Message5) -> State6 { + pub fn receive(self, msg: EncryptedSignature) -> State6 { State6 { a: self.a, B: self.B, @@ -553,7 +550,7 @@ pub struct State6 { tx_lock: bitcoin::TxLock, tx_punish_sig_bob: bitcoin::Signature, - tx_redeem_encsig: EncryptedSignature, + tx_redeem_encsig: bitcoin::EncryptedSignature, lock_xmr_fee: monero::Amount, } diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index ea0274d6..37de2b34 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -12,7 +12,7 @@ use crate::{ monero::Transfer, protocol::{ alice, - alice::{event_loop::EventLoopHandle, Message4, SwapResponse}, + alice::{event_loop::EventLoopHandle, SwapResponse, TransferProof}, SwapAmounts, }, }; @@ -132,7 +132,7 @@ where // Otherwise Alice might publish the lock tx twice! event_loop_handle - .send_message4(bob_peer_id, Message4 { + .send_transfer_proof(bob_peer_id, TransferProof { tx_lock_proof: transfer_proof, }) .await?; @@ -144,7 +144,7 @@ pub async fn wait_for_bitcoin_encrypted_signature( event_loop_handle: &mut EventLoopHandle, ) -> Result { let msg3 = event_loop_handle - .recv_message5() + .recv_encrypted_signature() .await .context("Failed to receive Bitcoin encrypted signature from Bob")?; diff --git a/swap/src/protocol/alice/message4.rs b/swap/src/protocol/alice/transfer_proof.rs similarity index 79% rename from swap/src/protocol/alice/message4.rs rename to swap/src/protocol/alice/transfer_proof.rs index 040a3f65..a9bc32b4 100644 --- a/swap/src/protocol/alice/message4.rs +++ b/swap/src/protocol/alice/transfer_proof.rs @@ -1,6 +1,6 @@ use crate::{ monero, - network::request_response::{Message4Protocol, OneShotCodec, Request, Response, TIMEOUT}, + network::request_response::{OneShotCodec, Request, Response, TransferProofProtocol, TIMEOUT}, }; use libp2p::{ request_response::{ @@ -19,7 +19,7 @@ use std::{ use tracing::error; #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Message4 { +pub struct TransferProof { pub tx_lock_proof: monero::TransferProof, } @@ -28,19 +28,20 @@ pub enum OutEvent { Msg, } -/// A `NetworkBehaviour` that represents sending message 4 to Bob. +/// A `NetworkBehaviour` that represents sending the Monero transfer proof to +/// Bob. #[derive(NetworkBehaviour)] #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Behaviour { - rr: RequestResponse>, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } impl Behaviour { - pub fn send(&mut self, bob: PeerId, msg: Message4) { - let msg = Request::Message4(Box::new(msg)); + pub fn send(&mut self, bob: PeerId, msg: TransferProof) { + let msg = Request::TransferProof(Box::new(msg)); let _id = self.rr.send_request(&bob, msg); } @@ -48,7 +49,7 @@ impl Behaviour { &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 +68,7 @@ impl Default for Behaviour { Self { rr: RequestResponse::new( OneShotCodec::default(), - vec![(Message4Protocol, ProtocolSupport::Full)], + vec![(TransferProofProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), @@ -81,12 +82,12 @@ impl NetworkBehaviourEventProcess> for B RequestResponseEvent::Message { message: RequestResponseMessage::Request { .. }, .. - } => panic!("Alice should never get a message 4 request from Bob"), + } => panic!("Alice should never get a transfer proof request from Bob"), RequestResponseEvent::Message { message: RequestResponseMessage::Response { response, .. }, .. } => { - if let Response::Message4 = response { + if let Response::TransferProof = response { self.events.push_back(OutEvent::Msg); } } diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 9361b02b..f4ca2e52 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -2,7 +2,6 @@ //! Bob holds BTC and wishes receive XMR. use crate::{ bitcoin, - bitcoin::EncryptedSignature, config::Config, database, database::Database, @@ -22,26 +21,26 @@ use tracing::{debug, info}; use uuid::Uuid; pub use self::{ + encrypted_signature::EncryptedSignature, event_loop::{EventLoop, EventLoopHandle}, message0::Message0, message1::Message1, message2::Message2, - message5::Message5, state::*, swap::{run, run_until}, swap_request::*, }; -use crate::protocol::alice::Message4; +use crate::protocol::alice::TransferProof; +mod encrypted_signature; pub mod event_loop; mod message0; mod message1; mod message2; -mod message4; -mod message5; pub mod state; pub mod swap; mod swap_request; +mod transfer_proof; pub struct Swap { pub state: BobState, @@ -213,8 +212,8 @@ pub enum OutEvent { Message0(Box), Message1(Box), Message2, - Message4(Box), - Message5, + TransferProof(Box), + EncryptedSignature, } impl From for OutEvent { @@ -257,18 +256,18 @@ impl From for OutEvent { } } -impl From for OutEvent { - fn from(event: message4::OutEvent) -> Self { +impl From for OutEvent { + fn from(event: transfer_proof::OutEvent) -> Self { match event { - message4::OutEvent::Msg(msg) => OutEvent::Message4(Box::new(msg)), + transfer_proof::OutEvent::Msg(msg) => OutEvent::TransferProof(Box::new(msg)), } } } -impl From for OutEvent { - fn from(event: message5::OutEvent) -> Self { +impl From for OutEvent { + fn from(event: encrypted_signature::OutEvent) -> Self { match event { - message5::OutEvent::Msg => OutEvent::Message5, + encrypted_signature::OutEvent::Msg => OutEvent::EncryptedSignature, } } } @@ -283,8 +282,8 @@ pub struct Behaviour { message0: message0::Behaviour, message1: message1::Behaviour, message2: message2::Behaviour, - message4: message4::Behaviour, - message5: message5::Behaviour, + transfer_proof: transfer_proof::Behaviour, + encrypted_signature: encrypted_signature::Behaviour, } impl Behaviour { @@ -313,9 +312,13 @@ impl Behaviour { } /// Sends Bob's fourth message to Alice. - pub fn send_message3(&mut self, alice: PeerId, tx_redeem_encsig: EncryptedSignature) { - let msg = Message5 { tx_redeem_encsig }; - self.message5.send(alice, msg); + pub fn send_encrypted_signature( + &mut self, + alice: PeerId, + tx_redeem_encsig: bitcoin::EncryptedSignature, + ) { + let msg = EncryptedSignature { tx_redeem_encsig }; + self.encrypted_signature.send(alice, msg); debug!("Sent Message3"); } diff --git a/swap/src/protocol/bob/message5.rs b/swap/src/protocol/bob/encrypted_signature.rs similarity index 76% rename from swap/src/protocol/bob/message5.rs rename to swap/src/protocol/bob/encrypted_signature.rs index 385fcfae..ab891feb 100644 --- a/swap/src/protocol/bob/message5.rs +++ b/swap/src/protocol/bob/encrypted_signature.rs @@ -1,6 +1,5 @@ -use crate::{ - bitcoin::EncryptedSignature, - network::request_response::{Message5Protocol, OneShotCodec, Request, Response, TIMEOUT}, +use crate::network::request_response::{ + EncryptedSignatureProtocol, OneShotCodec, Request, Response, TIMEOUT, }; use libp2p::{ request_response::{ @@ -19,8 +18,8 @@ use std::{ use tracing::error; #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Message5 { - pub tx_redeem_encsig: EncryptedSignature, +pub struct EncryptedSignature { + pub tx_redeem_encsig: crate::bitcoin::EncryptedSignature, } #[derive(Debug, Copy, Clone)] @@ -28,19 +27,19 @@ pub enum OutEvent { Msg, } -/// A `NetworkBehaviour` that represents sending message 5 to Alice. +/// A `NetworkBehaviour` that represents sending encrypted signature to Alice. #[derive(NetworkBehaviour)] #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Behaviour { - rr: RequestResponse>, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } impl Behaviour { - pub fn send(&mut self, alice: PeerId, msg: Message5) { - let msg = Request::Message5(Box::new(msg)); + pub fn send(&mut self, alice: PeerId, msg: EncryptedSignature) { + let msg = Request::EncryptedSignature(Box::new(msg)); let _id = self.rr.send_request(&alice, msg); } @@ -48,8 +47,9 @@ impl Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>, OutEvent>> - { + ) -> Poll< + NetworkBehaviourAction>, OutEvent>, + > { if let Some(event) = self.events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } @@ -67,7 +67,7 @@ impl Default for Behaviour { Self { rr: RequestResponse::new( OneShotCodec::default(), - vec![(Message5Protocol, ProtocolSupport::Full)], + vec![(EncryptedSignatureProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), @@ -86,7 +86,7 @@ impl NetworkBehaviourEventProcess> for B message: RequestResponseMessage::Response { response, .. }, .. } => { - if let Response::Message5 = response { + if let Response::EncryptedSignature = response { self.events.push_back(OutEvent::Msg); } } diff --git a/swap/src/protocol/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index 16409ea9..d379d03f 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -3,7 +3,7 @@ use crate::{ network::{transport::SwapTransport, TokioExecutor}, protocol::{ alice, - alice::{Message4, SwapResponse}, + alice::{SwapResponse, TransferProof}, bob::{self, Behaviour, OutEvent, SwapRequest}, }, }; @@ -40,14 +40,14 @@ pub struct EventLoopHandle { swap_response: Receiver, msg0: Receiver, msg1: Receiver, - msg4: Receiver, + r_transfer_proof: Receiver, conn_established: Receiver, dial_alice: Sender<()>, send_swap_request: Sender, send_msg0: Sender, send_msg1: Sender, send_msg2: Sender, - send_msg3: Sender, + s_encrypted_signature: Sender, } impl EventLoopHandle { @@ -72,11 +72,11 @@ impl EventLoopHandle { .ok_or_else(|| anyhow!("Failed to receive message 1 from Alice")) } - pub async fn recv_message4(&mut self) -> Result { - self.msg4 + pub async fn recv_transfer_proof(&mut self) -> Result { + self.r_transfer_proof .recv() .await - .ok_or_else(|| anyhow!("Failed to receive message 4 from Alice")) + .ok_or_else(|| anyhow!("Failed to receive transfer proof from Alice")) } /// Dials other party and wait for the connection to be established. @@ -114,7 +114,7 @@ impl EventLoopHandle { } pub async fn send_message3(&mut self, tx_redeem_encsig: EncryptedSignature) -> Result<()> { - let _ = self.send_msg3.send(tx_redeem_encsig).await?; + let _ = self.s_encrypted_signature.send(tx_redeem_encsig).await?; Ok(()) } } @@ -126,14 +126,14 @@ pub struct EventLoop { swap_response: Sender, msg0: Sender, msg1: Sender, - msg4: Sender, + r_transfer_proof: Sender, conn_established: Sender, dial_alice: Receiver<()>, send_swap_request: Receiver, send_msg0: Receiver, send_msg1: Receiver, send_msg2: Receiver, - send_msg3: Receiver, + s_encrypted_signature: Receiver, } impl EventLoop { @@ -155,14 +155,14 @@ impl EventLoop { let swap_response = Channels::new(); let msg0 = Channels::new(); let msg1 = Channels::new(); - let msg4 = Channels::new(); + let r_transfer_proof = Channels::new(); let conn_established = Channels::new(); let dial_alice = Channels::new(); let send_swap_request = Channels::new(); let send_msg0 = Channels::new(); let send_msg1 = Channels::new(); let send_msg2 = Channels::new(); - let send_msg3 = Channels::new(); + let s_encrypted_signature = Channels::new(); let event_loop = EventLoop { swarm, @@ -170,28 +170,28 @@ impl EventLoop { swap_response: swap_response.sender, msg0: msg0.sender, msg1: msg1.sender, - msg4: msg4.sender, + r_transfer_proof: r_transfer_proof.sender, conn_established: conn_established.sender, dial_alice: dial_alice.receiver, send_swap_request: send_swap_request.receiver, send_msg0: send_msg0.receiver, send_msg1: send_msg1.receiver, send_msg2: send_msg2.receiver, - send_msg3: send_msg3.receiver, + s_encrypted_signature: s_encrypted_signature.receiver, }; let handle = EventLoopHandle { swap_response: swap_response.receiver, msg0: msg0.receiver, msg1: msg1.receiver, - msg4: msg4.receiver, + r_transfer_proof: r_transfer_proof.receiver, conn_established: conn_established.receiver, dial_alice: dial_alice.sender, send_swap_request: send_swap_request.sender, send_msg0: send_msg0.sender, send_msg1: send_msg1.sender, send_msg2: send_msg2.sender, - send_msg3: send_msg3.sender, + s_encrypted_signature: s_encrypted_signature.sender, }; Ok((event_loop, handle)) @@ -215,10 +215,10 @@ impl EventLoop { let _ = self.msg1.send(*msg).await; } OutEvent::Message2 => info!("Alice acknowledged message 2 received"), - OutEvent::Message4(msg) => { - let _ = self.msg4.send(*msg).await; + OutEvent::TransferProof(msg) => { + let _ = self.r_transfer_proof.send(*msg).await; } - OutEvent::Message5 => info!("Alice acknowledged message 5 received"), + OutEvent::EncryptedSignature => info!("Alice acknowledged encrypted signature received"), } }, option = self.dial_alice.next().fuse() => { @@ -259,9 +259,9 @@ impl EventLoop { self.swarm.send_message2(self.alice_peer_id.clone(), msg); } }, - msg3 = self.send_msg3.next().fuse() => { - if let Some(tx_redeem_encsig) = msg3 { - self.swarm.send_message3(self.alice_peer_id.clone(), tx_redeem_encsig); + encrypted_signature = self.s_encrypted_signature.next().fuse() => { + if let Some(tx_redeem_encsig) = encrypted_signature { + self.swarm.send_encrypted_signature(self.alice_peer_id.clone(), tx_redeem_encsig); } } } diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index 0498e81e..74f09ca2 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -9,14 +9,10 @@ use crate::{ config::Config, monero, monero::{monero_private_key, TransferProof}, - protocol::{alice, bob, bob::Message5, SwapAmounts}, + protocol::{alice, bob, bob::EncryptedSignature, SwapAmounts}, }; use anyhow::{anyhow, Result}; -use ecdsa_fun::{ - adaptor::{Adaptor, EncryptedSignature}, - nonce::Deterministic, - Signature, -}; +use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic, Signature}; use monero_harness::rpc::wallet::BlockHeight; use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; @@ -244,7 +240,7 @@ pub struct State2 { pub punish_address: bitcoin::Address, pub tx_lock: bitcoin::TxLock, pub tx_cancel_sig_a: Signature, - pub tx_refund_encsig: EncryptedSignature, + pub tx_refund_encsig: bitcoin::EncryptedSignature, pub min_monero_confirmations: u32, } @@ -313,7 +309,7 @@ pub struct State3 { punish_address: bitcoin::Address, pub tx_lock: bitcoin::TxLock, pub tx_cancel_sig_a: Signature, - pub tx_refund_encsig: EncryptedSignature, + pub tx_refund_encsig: bitcoin::EncryptedSignature, pub min_monero_confirmations: u32, } @@ -433,19 +429,19 @@ pub struct State4 { punish_address: bitcoin::Address, pub tx_lock: bitcoin::TxLock, pub tx_cancel_sig_a: Signature, - pub tx_refund_encsig: EncryptedSignature, + pub tx_refund_encsig: bitcoin::EncryptedSignature, pub monero_wallet_restore_blockheight: u32, } impl State4 { - pub fn next_message(&self) -> Message5 { + pub fn next_message(&self) -> EncryptedSignature { let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address); let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin, tx_redeem.digest()); - Message5 { tx_redeem_encsig } + EncryptedSignature { tx_redeem_encsig } } - pub fn tx_redeem_encsig(&self) -> EncryptedSignature { + pub fn tx_redeem_encsig(&self) -> bitcoin::EncryptedSignature { let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address); self.b.encsign(self.S_a_bitcoin, tx_redeem.digest()) } @@ -615,7 +611,7 @@ pub struct State5 { pub redeem_address: bitcoin::Address, punish_address: bitcoin::Address, pub tx_lock: bitcoin::TxLock, - tx_refund_encsig: EncryptedSignature, + tx_refund_encsig: bitcoin::EncryptedSignature, tx_cancel_sig: Signature, pub monero_wallet_restore_blockheight: u32, } diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 2f1e21bd..5e13fad9 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -130,7 +130,7 @@ where { event_loop_handle.dial().await?; - let msg4_watcher = event_loop_handle.recv_message4(); + let transfer_proof_watcher = event_loop_handle.recv_transfer_proof(); let cancel_timelock_expires = state3.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref()); @@ -143,11 +143,11 @@ where monero_wallet.inner.block_height().await?; select! { - msg4 = msg4_watcher => { - let msg4 = msg4?; + transfer_proof = transfer_proof_watcher => { + let transfer_proof = transfer_proof?; BobState::XmrLockProofReceived { state: state3, - lock_transfer_proof: msg4.tx_lock_proof, + lock_transfer_proof: transfer_proof.tx_lock_proof, monero_wallet_restore_blockheight } }, diff --git a/swap/src/protocol/bob/message4.rs b/swap/src/protocol/bob/transfer_proof.rs similarity index 82% rename from swap/src/protocol/bob/message4.rs rename to swap/src/protocol/bob/transfer_proof.rs index 80a1866d..ee6fc147 100644 --- a/swap/src/protocol/bob/message4.rs +++ b/swap/src/protocol/bob/transfer_proof.rs @@ -1,6 +1,6 @@ use crate::{ - network::request_response::{Message4Protocol, OneShotCodec, Request, Response, TIMEOUT}, - protocol::alice::Message4, + network::request_response::{OneShotCodec, Request, Response, TransferProofProtocol, TIMEOUT}, + protocol::alice::TransferProof, }; use libp2p::{ request_response::{ @@ -19,15 +19,16 @@ use tracing::{debug, error}; #[derive(Debug)] pub enum OutEvent { - Msg(Message4), + Msg(TransferProof), } -/// A `NetworkBehaviour` that represents receiving of message 4 from Alice. +/// A `NetworkBehaviour` that represents receiving the transfer proof from +/// Alice. #[derive(NetworkBehaviour)] #[behaviour(out_event = "OutEvent", poll_method = "poll")] #[allow(missing_debug_implementations)] pub struct Behaviour { - rr: RequestResponse>, + rr: RequestResponse>, #[behaviour(ignore)] events: VecDeque, } @@ -37,7 +38,7 @@ impl Behaviour { &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)); @@ -56,7 +57,7 @@ impl Default for Behaviour { Self { rr: RequestResponse::new( OneShotCodec::default(), - vec![(Message4Protocol, ProtocolSupport::Full)], + vec![(TransferProofProtocol, ProtocolSupport::Full)], config, ), events: Default::default(), @@ -74,11 +75,11 @@ impl NetworkBehaviourEventProcess> for B }, .. } => { - if let Request::Message4(msg) = request { - debug!("Received message 4"); + if let Request::TransferProof(msg) = request { + debug!("Received Transfer Proof"); self.events.push_back(OutEvent::Msg(*msg)); // Send back empty response so that the request/response protocol completes. - let _ = self.rr.send_response(channel, Response::Message4); + let _ = self.rr.send_response(channel, Response::TransferProof); } } RequestResponseEvent::Message {