From f9f17b0e23055bfb3e7131995197112472eef9de Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 29 Jan 2021 15:17:43 +1100 Subject: [PATCH] Do not consider transfer proof sent until it's ack'd --- swap/src/protocol/alice.rs | 4 ++-- swap/src/protocol/alice/event_loop.rs | 15 ++++++++++++++- swap/src/protocol/alice/transfer_proof.rs | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index cb9144cb..87c750fa 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -236,7 +236,7 @@ pub enum OutEvent { msg: Box, bob_peer_id: PeerId, }, - TransferProof, + TransferProofAcknowledged, EncryptedSignature(EncryptedSignature), } @@ -289,7 +289,7 @@ impl From for OutEvent { impl From for OutEvent { fn from(event: transfer_proof::OutEvent) -> Self { match event { - transfer_proof::OutEvent::Msg => OutEvent::TransferProof, + transfer_proof::OutEvent::Acknowledged => OutEvent::TransferProofAcknowledged, } } } diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 7dcc37a5..4caf21d5 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -45,6 +45,7 @@ pub struct EventLoopHandle { send_message0: Sender<(ResponseChannel, alice::Message0)>, send_message1: Sender<(ResponseChannel, alice::Message1)>, send_transfer_proof: Sender<(PeerId, TransferProof)>, + recv_transfer_proof_ack: Receiver<()>, } impl EventLoopHandle { @@ -124,6 +125,11 @@ impl EventLoopHandle { pub async fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) -> Result<()> { let _ = self.send_transfer_proof.send((bob, msg)).await?; + + self.recv_transfer_proof_ack + .recv() + .await + .ok_or_else(|| anyhow!("Failed to receive transfer proof ack from Bob"))?; Ok(()) } } @@ -141,6 +147,7 @@ pub struct EventLoop { send_message0: Receiver<(ResponseChannel, alice::Message0)>, send_message1: Receiver<(ResponseChannel, alice::Message1)>, send_transfer_proof: Receiver<(PeerId, TransferProof)>, + recv_transfer_proof_ack: Sender<()>, } impl EventLoop { @@ -169,6 +176,7 @@ impl EventLoop { let send_message0 = Channels::new(); let send_message1 = Channels::new(); let send_transfer_proof = Channels::new(); + let recv_transfer_proof_ack = Channels::new(); let driver = EventLoop { swarm, @@ -182,6 +190,7 @@ impl EventLoop { send_message0: send_message0.receiver, send_message1: send_message1.receiver, send_transfer_proof: send_transfer_proof.receiver, + recv_transfer_proof_ack: recv_transfer_proof_ack.sender, }; let handle = EventLoopHandle { @@ -195,6 +204,7 @@ impl EventLoop { send_message0: send_message0.sender, send_message1: send_message1.sender, send_transfer_proof: send_transfer_proof.sender, + recv_transfer_proof_ack: recv_transfer_proof_ack.receiver, }; Ok((driver, handle)) @@ -217,7 +227,10 @@ impl EventLoop { OutEvent::Message2 { msg, bob_peer_id : _} => { let _ = self.recv_message2.send(*msg).await; } - OutEvent::TransferProof => trace!("Bob ack'd receiving the transfer proof"), + OutEvent::TransferProofAcknowledged => { + trace!("Bob acknowledged transfer proof"); + let _ = self.recv_transfer_proof_ack.send(()).await; + } OutEvent::EncryptedSignature(msg) => { let _ = self.recv_encrypted_signature.send(msg).await; } diff --git a/swap/src/protocol/alice/transfer_proof.rs b/swap/src/protocol/alice/transfer_proof.rs index d39a4084..422fa782 100644 --- a/swap/src/protocol/alice/transfer_proof.rs +++ b/swap/src/protocol/alice/transfer_proof.rs @@ -25,7 +25,7 @@ pub struct TransferProof { #[derive(Debug, Copy, Clone)] pub enum OutEvent { - Msg, + Acknowledged, } /// A `NetworkBehaviour` that represents sending the Monero transfer proof to @@ -88,7 +88,7 @@ impl NetworkBehaviourEventProcess> for B .. } => { if let Response::TransferProof = response { - self.events.push_back(OutEvent::Msg); + self.events.push_back(OutEvent::Acknowledged); } } RequestResponseEvent::InboundFailure { error, .. } => {