From b62ef9c2d93afab6fe4992368da9af7992c10a01 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 27 Jan 2021 14:25:45 +1100 Subject: [PATCH] Harmonizing naming --- swap/src/protocol/alice/event_loop.rs | 98 ++++++++++----------- swap/src/protocol/bob/event_loop.rs | 119 +++++++++++++------------- swap/src/protocol/bob/swap.rs | 3 +- 3 files changed, 112 insertions(+), 108 deletions(-) diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 424a3c74..b4168788 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -36,16 +36,16 @@ impl Default for Channels { #[derive(Debug)] pub struct EventLoopHandle { - msg0: Receiver<(bob::Message0, ResponseChannel)>, - msg1: Receiver<(bob::Message1, ResponseChannel)>, - msg2: Receiver, - r_encrypted_signature: Receiver, + recv_message0: Receiver<(bob::Message0, ResponseChannel)>, + recv_message1: Receiver<(bob::Message1, ResponseChannel)>, + recv_message2: Receiver, + recv_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)>, - s_transfer_proof: Sender<(PeerId, TransferProof)>, + send_message0: Sender<(ResponseChannel, alice::Message0)>, + send_message1: Sender<(ResponseChannel, alice::Message1)>, + send_transfer_proof: Sender<(PeerId, TransferProof)>, } impl EventLoopHandle { @@ -57,28 +57,28 @@ impl EventLoopHandle { } pub async fn recv_message0(&mut self) -> Result<(bob::Message0, ResponseChannel)> { - self.msg0 + self.recv_message0 .recv() .await .ok_or_else(|| anyhow!("Failed to receive message 0 from Bob")) } pub async fn recv_message1(&mut self) -> Result<(bob::Message1, ResponseChannel)> { - self.msg1 + self.recv_message1 .recv() .await .ok_or_else(|| anyhow!("Failed to receive message 1 from Bob")) } pub async fn recv_message2(&mut self) -> Result { - self.msg2 + self.recv_message2 .recv() .await .ok_or_else(|| anyhow!("Failed to receive message 2 from Bob")) } pub async fn recv_encrypted_signature(&mut self) -> Result { - self.r_encrypted_signature + self.recv_encrypted_signature .recv() .await .ok_or_else(|| anyhow!("Failed to receive Bitcoin encrypted signature from Bob")) @@ -110,7 +110,7 @@ impl EventLoopHandle { channel: ResponseChannel, msg: alice::Message0, ) -> Result<()> { - let _ = self.send_msg0.send((channel, msg)).await?; + let _ = self.send_message0.send((channel, msg)).await?; Ok(()) } @@ -119,12 +119,12 @@ impl EventLoopHandle { channel: ResponseChannel, msg: alice::Message1, ) -> Result<()> { - let _ = self.send_msg1.send((channel, msg)).await?; + let _ = self.send_message1.send((channel, msg)).await?; Ok(()) } pub async fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) -> Result<()> { - let _ = self.s_transfer_proof.send((bob, msg)).await?; + let _ = self.send_transfer_proof.send((bob, msg)).await?; Ok(()) } } @@ -132,16 +132,16 @@ impl EventLoopHandle { #[allow(missing_debug_implementations)] pub struct EventLoop { swarm: libp2p::Swarm, - msg0: Sender<(bob::Message0, ResponseChannel)>, - msg1: Sender<(bob::Message1, ResponseChannel)>, - msg2: Sender, - r_encrypted_signature: Sender, + recv_message0: Sender<(bob::Message0, ResponseChannel)>, + recv_message1: Sender<(bob::Message1, ResponseChannel)>, + recv_message2: Sender, + recv_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)>, - s_transfer_proof: Receiver<(PeerId, TransferProof)>, + send_message0: Receiver<(ResponseChannel, alice::Message0)>, + send_message1: Receiver<(ResponseChannel, alice::Message1)>, + send_transfer_proof: Receiver<(PeerId, TransferProof)>, } impl EventLoop { @@ -160,42 +160,42 @@ impl EventLoop { Swarm::listen_on(&mut swarm, listen.clone()) .with_context(|| format!("Address is not supported: {:#}", listen))?; - let msg0 = Channels::new(); - let msg1 = Channels::new(); - let msg2 = Channels::new(); - let r_encrypted_signature = Channels::new(); + let recv_message0 = Channels::new(); + let recv_message1 = Channels::new(); + let recv_message2 = Channels::new(); + let recv_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 s_transfer_proof = Channels::new(); + let send_message0 = Channels::new(); + let send_message1 = Channels::new(); + let send_transfer_proof = Channels::new(); let driver = EventLoop { swarm, - msg0: msg0.sender, - msg1: msg1.sender, - msg2: msg2.sender, - r_encrypted_signature: r_encrypted_signature.sender, + recv_message0: recv_message0.sender, + recv_message1: recv_message1.sender, + recv_message2: recv_message2.sender, + recv_encrypted_signature: recv_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, - s_transfer_proof: s_transfer_proof.receiver, + send_message0: send_message0.receiver, + send_message1: send_message1.receiver, + send_transfer_proof: send_transfer_proof.receiver, }; let handle = EventLoopHandle { - msg0: msg0.receiver, - msg1: msg1.receiver, - msg2: msg2.receiver, - r_encrypted_signature: r_encrypted_signature.receiver, + recv_message0: recv_message0.receiver, + recv_message1: recv_message1.receiver, + recv_message2: recv_message2.receiver, + recv_encrypted_signature: recv_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, - s_transfer_proof: s_transfer_proof.sender, + send_message0: send_message0.sender, + send_message1: send_message1.sender, + send_transfer_proof: send_transfer_proof.sender, }; Ok((driver, handle)) @@ -210,17 +210,17 @@ impl EventLoop { let _ = self.conn_established.send(alice).await; } OutEvent::Message0 { msg, channel } => { - let _ = self.msg0.send((*msg, channel)).await; + let _ = self.recv_message0.send((*msg, channel)).await; } OutEvent::Message1 { msg, channel } => { - let _ = self.msg1.send((msg, channel)).await; + let _ = self.recv_message1.send((msg, channel)).await; } OutEvent::Message2 { msg, bob_peer_id : _} => { - let _ = self.msg2.send(*msg).await; + let _ = self.recv_message2.send(*msg).await; } OutEvent::TransferProof => trace!("Bob ack'd receiving the transfer proof"), OutEvent::EncryptedSignature(msg) => { - let _ = self.r_encrypted_signature.send(msg).await; + let _ = self.recv_encrypted_signature.send(msg).await; } OutEvent::Request(event) => { let _ = self.request.send(*event).await; @@ -232,17 +232,17 @@ impl EventLoop { self.swarm.send_swap_response(channel, swap_response); } }, - msg0 = self.send_msg0.next().fuse() => { + msg0 = self.send_message0.next().fuse() => { if let Some((channel, msg)) = msg0 { self.swarm.send_message0(channel, msg); } }, - msg1 = self.send_msg1.next().fuse() => { + msg1 = self.send_message1.next().fuse() => { if let Some((channel, msg)) = msg1 { self.swarm.send_message1(channel, msg); } }, - transfer_proof = self.s_transfer_proof.next().fuse() => { + transfer_proof = self.send_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/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index d379d03f..eb117432 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -37,43 +37,43 @@ impl Default for Channels { #[derive(Debug)] pub struct EventLoopHandle { - swap_response: Receiver, - msg0: Receiver, - msg1: Receiver, - r_transfer_proof: Receiver, + recv_swap_response: Receiver, + recv_message0: Receiver, + recv_message1: Receiver, + recv_transfer_proof: Receiver, conn_established: Receiver, dial_alice: Sender<()>, send_swap_request: Sender, - send_msg0: Sender, - send_msg1: Sender, - send_msg2: Sender, - s_encrypted_signature: Sender, + send_message0: Sender, + send_message1: Sender, + send_message2: Sender, + send_encrypted_signature: Sender, } impl EventLoopHandle { pub async fn recv_swap_response(&mut self) -> Result { - self.swap_response + self.recv_swap_response .recv() .await .ok_or_else(|| anyhow!("Failed to receive swap response from Alice")) } pub async fn recv_message0(&mut self) -> Result { - self.msg0 + self.recv_message0 .recv() .await .ok_or_else(|| anyhow!("Failed to receive message 0 from Alice")) } pub async fn recv_message1(&mut self) -> Result { - self.msg1 + self.recv_message1 .recv() .await .ok_or_else(|| anyhow!("Failed to receive message 1 from Alice")) } pub async fn recv_transfer_proof(&mut self) -> Result { - self.r_transfer_proof + self.recv_transfer_proof .recv() .await .ok_or_else(|| anyhow!("Failed to receive transfer proof from Alice")) @@ -99,22 +99,25 @@ impl EventLoopHandle { } pub async fn send_message0(&mut self, msg: bob::Message0) -> Result<()> { - let _ = self.send_msg0.send(msg).await?; + let _ = self.send_message0.send(msg).await?; Ok(()) } pub async fn send_message1(&mut self, msg: bob::Message1) -> Result<()> { - let _ = self.send_msg1.send(msg).await?; + let _ = self.send_message1.send(msg).await?; Ok(()) } pub async fn send_message2(&mut self, msg: bob::Message2) -> Result<()> { - let _ = self.send_msg2.send(msg).await?; + let _ = self.send_message2.send(msg).await?; Ok(()) } - pub async fn send_message3(&mut self, tx_redeem_encsig: EncryptedSignature) -> Result<()> { - let _ = self.s_encrypted_signature.send(tx_redeem_encsig).await?; + pub async fn send_encrypted_signature( + &mut self, + tx_redeem_encsig: EncryptedSignature, + ) -> Result<()> { + let _ = self.send_encrypted_signature.send(tx_redeem_encsig).await?; Ok(()) } } @@ -123,17 +126,17 @@ impl EventLoopHandle { pub struct EventLoop { swarm: libp2p::Swarm, alice_peer_id: PeerId, - swap_response: Sender, - msg0: Sender, - msg1: Sender, - r_transfer_proof: Sender, - conn_established: Sender, + recv_swap_response: Sender, + recv_message0: Sender, + recv_message1: Sender, + recv_transfer_proof: Sender, dial_alice: Receiver<()>, + conn_established: Sender, send_swap_request: Receiver, - send_msg0: Receiver, - send_msg1: Receiver, - send_msg2: Receiver, - s_encrypted_signature: Receiver, + send_message0: Receiver, + send_message1: Receiver, + send_message2: Receiver, + send_encrypted_signature: Receiver, } impl EventLoop { @@ -153,45 +156,45 @@ impl EventLoop { swarm.add_address(alice_peer_id.clone(), alice_addr); let swap_response = Channels::new(); - let msg0 = Channels::new(); - let msg1 = Channels::new(); - let r_transfer_proof = Channels::new(); - let conn_established = Channels::new(); + let recv_message0 = Channels::new(); + let recv_message1 = Channels::new(); + let recv_transfer_proof = Channels::new(); let dial_alice = Channels::new(); + let conn_established = Channels::new(); let send_swap_request = Channels::new(); - let send_msg0 = Channels::new(); - let send_msg1 = Channels::new(); - let send_msg2 = Channels::new(); - let s_encrypted_signature = Channels::new(); + let send_message0 = Channels::new(); + let send_message1 = Channels::new(); + let send_message2 = Channels::new(); + let send_encrypted_signature = Channels::new(); let event_loop = EventLoop { swarm, alice_peer_id, - swap_response: swap_response.sender, - msg0: msg0.sender, - msg1: msg1.sender, - r_transfer_proof: r_transfer_proof.sender, + recv_swap_response: swap_response.sender, + recv_message0: recv_message0.sender, + recv_message1: recv_message1.sender, + recv_transfer_proof: recv_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, - s_encrypted_signature: s_encrypted_signature.receiver, + send_message0: send_message0.receiver, + send_message1: send_message1.receiver, + send_message2: send_message2.receiver, + send_encrypted_signature: send_encrypted_signature.receiver, }; let handle = EventLoopHandle { - swap_response: swap_response.receiver, - msg0: msg0.receiver, - msg1: msg1.receiver, - r_transfer_proof: r_transfer_proof.receiver, + recv_swap_response: swap_response.receiver, + recv_message0: recv_message0.receiver, + recv_message1: recv_message1.receiver, + recv_transfer_proof: recv_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, - s_encrypted_signature: s_encrypted_signature.sender, + send_message0: send_message0.sender, + send_message1: send_message1.sender, + send_message2: send_message2.sender, + send_encrypted_signature: send_encrypted_signature.sender, }; Ok((event_loop, handle)) @@ -206,17 +209,17 @@ impl EventLoop { let _ = self.conn_established.send(peer_id).await; } OutEvent::SwapResponse(msg) => { - let _ = self.swap_response.send(msg).await; + let _ = self.recv_swap_response.send(msg).await; }, OutEvent::Message0(msg) => { - let _ = self.msg0.send(*msg).await; + let _ = self.recv_message0.send(*msg).await; } OutEvent::Message1(msg) => { - let _ = self.msg1.send(*msg).await; + let _ = self.recv_message1.send(*msg).await; } OutEvent::Message2 => info!("Alice acknowledged message 2 received"), OutEvent::TransferProof(msg) => { - let _ = self.r_transfer_proof.send(*msg).await; + let _ = self.recv_transfer_proof.send(*msg).await; } OutEvent::EncryptedSignature => info!("Alice acknowledged encrypted signature received"), } @@ -243,23 +246,23 @@ impl EventLoop { } }, - msg0 = self.send_msg0.next().fuse() => { + msg0 = self.send_message0.next().fuse() => { if let Some(msg) = msg0 { self.swarm.send_message0(self.alice_peer_id.clone(), msg); } } - msg1 = self.send_msg1.next().fuse() => { + msg1 = self.send_message1.next().fuse() => { if let Some(msg) = msg1 { self.swarm.send_message1(self.alice_peer_id.clone(), msg); } }, - msg2 = self.send_msg2.next().fuse() => { + msg2 = self.send_message2.next().fuse() => { if let Some(msg) = msg2 { self.swarm.send_message2(self.alice_peer_id.clone(), msg); } }, - encrypted_signature = self.s_encrypted_signature.next().fuse() => { + encrypted_signature = self.send_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/swap.rs b/swap/src/protocol/bob/swap.rs index 5e13fad9..767372be 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -233,7 +233,8 @@ where let state4_clone = state.clone(); - let enc_sig_sent_watcher = event_loop_handle.send_message3(tx_redeem_encsig); + let enc_sig_sent_watcher = + event_loop_handle.send_encrypted_signature(tx_redeem_encsig); let bitcoin_wallet = bitcoin_wallet.clone(); let cancel_timelock_expires = state4_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());