Rename bob::Message2 to Message5

As per the proposed changed in the sequence diagram.
The aim is to have a unique terminology per message instead of having
the same name for 2 consequent messages that share the same behaviour.

Note that the aim is to remove the shared `RequestResponse` behaviours.
This commit is contained in:
Franck Royer 2021-01-22 10:12:36 +11:00
parent 91fe18a796
commit 9a5e35c1bd
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
10 changed files with 58 additions and 57 deletions

View File

@ -1,4 +1,4 @@
use crate::protocol::{alice, bob}; use crate::protocol::{alice, bob, bob::Message5};
use async_trait::async_trait; use async_trait::async_trait;
use futures::prelude::*; use futures::prelude::*;
use libp2p::{ use libp2p::{
@ -25,7 +25,7 @@ pub enum BobToAlice {
Message0(Box<bob::Message0>), Message0(Box<bob::Message0>),
Message1(bob::Message1), Message1(bob::Message1),
Message2(bob::Message2), Message2(bob::Message2),
Message3(bob::Message3), Message5(Message5),
} }
/// Messages Alice sends to Bob. /// Messages Alice sends to Bob.
@ -35,7 +35,7 @@ pub enum AliceToBob {
Message0(Box<alice::Message0>), Message0(Box<alice::Message0>),
Message1(Box<alice::Message1>), Message1(Box<alice::Message1>),
Message2(alice::Message2), Message2(alice::Message2),
Message3, // empty response Message5, // empty response
} }
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
@ -51,7 +51,7 @@ pub struct Message1Protocol;
pub struct Message2Protocol; pub struct Message2Protocol;
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct Message3Protocol; pub struct Message5Protocol;
impl ProtocolName for Swap { impl ProtocolName for Swap {
fn protocol_name(&self) -> &[u8] { fn protocol_name(&self) -> &[u8] {
@ -77,9 +77,9 @@ impl ProtocolName for Message2Protocol {
} }
} }
impl ProtocolName for Message3Protocol { impl ProtocolName for Message5Protocol {
fn protocol_name(&self) -> &[u8] { fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message3/1.0.0" b"/xmr/btc/message5/1.0.0"
} }
} }

View File

@ -21,7 +21,7 @@ use crate::{
transport::build, transport::build,
Seed as NetworkSeed, Seed as NetworkSeed,
}, },
protocol::{bob, SwapAmounts}, protocol::{bob, bob::Message5, SwapAmounts},
seed::Seed, seed::Seed,
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
@ -37,7 +37,7 @@ pub mod event_loop;
mod message0; mod message0;
mod message1; mod message1;
mod message2; mod message2;
mod message3; mod message5;
pub mod state; pub mod state;
mod steps; mod steps;
pub mod swap; pub mod swap;
@ -235,7 +235,7 @@ pub enum OutEvent {
msg: bob::Message2, msg: bob::Message2,
channel: ResponseChannel<AliceToBob>, channel: ResponseChannel<AliceToBob>,
}, },
Message3(bob::Message3), Message5(Message5),
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -281,10 +281,10 @@ impl From<message2::OutEvent> for OutEvent {
} }
} }
impl From<message3::OutEvent> for OutEvent { impl From<message5::OutEvent> for OutEvent {
fn from(event: message3::OutEvent) -> Self { fn from(event: message5::OutEvent) -> Self {
match event { match event {
message3::OutEvent::Msg(msg) => OutEvent::Message3(msg), message5::OutEvent::Msg(msg) => OutEvent::Message5(msg),
} }
} }
} }
@ -299,7 +299,7 @@ pub struct Behaviour {
message0: message0::Behaviour, message0: message0::Behaviour,
message1: message1::Behaviour, message1: message1::Behaviour,
message2: message2::Behaviour, message2: message2::Behaviour,
message3: message3::Behaviour, message5: message5::Behaviour,
} }
impl Behaviour { impl Behaviour {

View File

@ -4,6 +4,7 @@ use crate::{
alice, alice,
alice::{Behaviour, OutEvent, SwapResponse}, alice::{Behaviour, OutEvent, SwapResponse},
bob, bob,
bob::Message5,
}, },
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
@ -37,7 +38,7 @@ pub struct EventLoopHandle {
msg0: Receiver<(bob::Message0, ResponseChannel<AliceToBob>)>, msg0: Receiver<(bob::Message0, ResponseChannel<AliceToBob>)>,
msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>, msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>,
msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>, msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>,
msg3: Receiver<bob::Message3>, msg5: Receiver<Message5>,
request: Receiver<crate::protocol::alice::swap_response::OutEvent>, request: Receiver<crate::protocol::alice::swap_response::OutEvent>,
conn_established: Receiver<PeerId>, conn_established: Receiver<PeerId>,
send_swap_response: Sender<(ResponseChannel<AliceToBob>, SwapResponse)>, send_swap_response: Sender<(ResponseChannel<AliceToBob>, SwapResponse)>,
@ -75,8 +76,8 @@ impl EventLoopHandle {
.ok_or_else(|| anyhow!("Failed o receive message 2 from Bob")) .ok_or_else(|| anyhow!("Failed o receive message 2 from Bob"))
} }
pub async fn recv_message3(&mut self) -> Result<bob::Message3> { pub async fn recv_message5(&mut self) -> Result<Message5> {
self.msg3 self.msg5
.recv() .recv()
.await .await
.ok_or_else(|| anyhow!("Failed to receive Bitcoin encrypted signature from Bob")) .ok_or_else(|| anyhow!("Failed to receive Bitcoin encrypted signature from Bob"))
@ -137,7 +138,7 @@ pub struct EventLoop {
msg0: Sender<(bob::Message0, ResponseChannel<AliceToBob>)>, msg0: Sender<(bob::Message0, ResponseChannel<AliceToBob>)>,
msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>, msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>,
msg2: Sender<(bob::Message2, ResponseChannel<AliceToBob>)>, msg2: Sender<(bob::Message2, ResponseChannel<AliceToBob>)>,
msg3: Sender<bob::Message3>, msg5: Sender<Message5>,
request: Sender<crate::protocol::alice::swap_response::OutEvent>, request: Sender<crate::protocol::alice::swap_response::OutEvent>,
conn_established: Sender<PeerId>, conn_established: Sender<PeerId>,
send_swap_response: Receiver<(ResponseChannel<AliceToBob>, SwapResponse)>, send_swap_response: Receiver<(ResponseChannel<AliceToBob>, SwapResponse)>,
@ -165,7 +166,7 @@ impl EventLoop {
let msg0 = Channels::new(); let msg0 = Channels::new();
let msg1 = Channels::new(); let msg1 = Channels::new();
let msg2 = Channels::new(); let msg2 = Channels::new();
let msg3 = Channels::new(); let msg5 = Channels::new();
let request = Channels::new(); let request = Channels::new();
let conn_established = Channels::new(); let conn_established = Channels::new();
let send_swap_response = Channels::new(); let send_swap_response = Channels::new();
@ -178,7 +179,7 @@ impl EventLoop {
msg0: msg0.sender, msg0: msg0.sender,
msg1: msg1.sender, msg1: msg1.sender,
msg2: msg2.sender, msg2: msg2.sender,
msg3: msg3.sender, msg5: msg5.sender,
request: request.sender, request: request.sender,
conn_established: conn_established.sender, conn_established: conn_established.sender,
send_swap_response: send_swap_response.receiver, send_swap_response: send_swap_response.receiver,
@ -191,7 +192,7 @@ impl EventLoop {
msg0: msg0.receiver, msg0: msg0.receiver,
msg1: msg1.receiver, msg1: msg1.receiver,
msg2: msg2.receiver, msg2: msg2.receiver,
msg3: msg3.receiver, msg5: msg5.receiver,
request: request.receiver, request: request.receiver,
conn_established: conn_established.receiver, conn_established: conn_established.receiver,
send_swap_response: send_swap_response.sender, send_swap_response: send_swap_response.sender,
@ -220,8 +221,8 @@ impl EventLoop {
OutEvent::Message2 { msg, channel } => { OutEvent::Message2 { msg, channel } => {
let _ = self.msg2.send((msg, channel)).await; let _ = self.msg2.send((msg, channel)).await;
} }
OutEvent::Message3(msg) => { OutEvent::Message5(msg) => {
let _ = self.msg3.send(msg).await; let _ = self.msg5.send(msg).await;
} }
OutEvent::Request(event) => { OutEvent::Request(event) => {
let _ = self.request.send(*event).await; let _ = self.request.send(*event).await;

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, network::request_response::{AliceToBob, BobToAlice, Codec, Message5Protocol, TIMEOUT},
protocol::bob, protocol::bob::Message5,
}; };
use libp2p::{ use libp2p::{
request_response::{ request_response::{
@ -19,15 +19,15 @@ use tracing::{debug, error};
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
Msg(bob::Message3), Msg(Message5),
} }
/// A `NetworkBehaviour` that represents receiving of message 3 from Bob. /// A `NetworkBehaviour` that represents receiving of message 5 from Bob.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", poll_method = "poll")] #[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Behaviour { pub struct Behaviour {
rr: RequestResponse<Codec<Message3Protocol>>, rr: RequestResponse<Codec<Message5Protocol>>,
#[behaviour(ignore)] #[behaviour(ignore)]
events: VecDeque<OutEvent>, events: VecDeque<OutEvent>,
} }
@ -37,7 +37,7 @@ impl Behaviour {
&mut self, &mut self,
_: &mut Context<'_>, _: &mut Context<'_>,
_: &mut impl PollParameters, _: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message3Protocol>>, OutEvent>> { ) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message5Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() { if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
} }
@ -55,7 +55,7 @@ impl Default for Behaviour {
Self { Self {
rr: RequestResponse::new( rr: RequestResponse::new(
Codec::default(), Codec::default(),
vec![(Message3Protocol, ProtocolSupport::Full)], vec![(Message5Protocol, ProtocolSupport::Full)],
config, config,
), ),
events: Default::default(), events: Default::default(),
@ -73,11 +73,11 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
}, },
.. ..
} => { } => {
if let BobToAlice::Message3(msg) = request { if let BobToAlice::Message5(msg) = request {
debug!("Received Message3"); debug!("Received message 5");
self.events.push_back(OutEvent::Msg(msg)); self.events.push_back(OutEvent::Msg(msg));
// Send back empty response so that the request/response protocol completes. // Send back empty response so that the request/response protocol completes.
self.rr.send_response(channel, AliceToBob::Message3); self.rr.send_response(channel, AliceToBob::Message5);
} }
} }
RequestResponseEvent::Message { RequestResponseEvent::Message {

View File

@ -9,7 +9,7 @@ use crate::{
monero, monero,
monero::CreateWalletForOutput, monero::CreateWalletForOutput,
network::request_response::AliceToBob, network::request_response::AliceToBob,
protocol::{alice, bob, SwapAmounts}, protocol::{alice, bob, bob::Message5, SwapAmounts},
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use ecdsa_fun::{ use ecdsa_fun::{
@ -482,7 +482,7 @@ impl State5 {
} }
} }
pub fn receive(self, msg: bob::Message3) -> State6 { pub fn receive(self, msg: Message5) -> State6 {
State6 { State6 {
a: self.a, a: self.a,
B: self.B, B: self.B,

View File

@ -146,7 +146,7 @@ pub async fn wait_for_bitcoin_encrypted_signature(
event_loop_handle: &mut EventLoopHandle, event_loop_handle: &mut EventLoopHandle,
) -> Result<EncryptedSignature> { ) -> Result<EncryptedSignature> {
let msg3 = event_loop_handle let msg3 = event_loop_handle
.recv_message3() .recv_message5()
.await .await
.context("Failed to receive Bitcoin encrypted signature from Bob")?; .context("Failed to receive Bitcoin encrypted signature from Bob")?;

View File

@ -26,7 +26,7 @@ pub use self::{
message0::Message0, message0::Message0,
message1::Message1, message1::Message1,
message2::Message2, message2::Message2,
message3::Message3, message5::Message5,
state::*, state::*,
swap::{run, run_until}, swap::{run, run_until},
swap_request::*, swap_request::*,
@ -36,7 +36,7 @@ pub mod event_loop;
mod message0; mod message0;
mod message1; mod message1;
mod message2; mod message2;
mod message3; mod message5;
pub mod state; pub mod state;
pub mod swap; pub mod swap;
mod swap_request; mod swap_request;
@ -211,7 +211,7 @@ pub enum OutEvent {
Message0(Box<alice::Message0>), Message0(Box<alice::Message0>),
Message1(Box<alice::Message1>), Message1(Box<alice::Message1>),
Message2(alice::Message2), Message2(alice::Message2),
Message3, Message5,
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -254,10 +254,10 @@ impl From<message2::OutEvent> for OutEvent {
} }
} }
impl From<message3::OutEvent> for OutEvent { impl From<message5::OutEvent> for OutEvent {
fn from(event: message3::OutEvent) -> Self { fn from(event: message5::OutEvent) -> Self {
match event { match event {
message3::OutEvent::Msg => OutEvent::Message3, message5::OutEvent::Msg => OutEvent::Message5,
} }
} }
} }
@ -272,7 +272,7 @@ pub struct Behaviour {
message0: message0::Behaviour, message0: message0::Behaviour,
message1: message1::Behaviour, message1: message1::Behaviour,
message2: message2::Behaviour, message2: message2::Behaviour,
message3: message3::Behaviour, message5: message5::Behaviour,
} }
impl Behaviour { impl Behaviour {
@ -302,8 +302,8 @@ impl Behaviour {
/// Sends Bob's fourth message to Alice. /// Sends Bob's fourth message to Alice.
pub fn send_message3(&mut self, alice: PeerId, tx_redeem_encsig: EncryptedSignature) { pub fn send_message3(&mut self, alice: PeerId, tx_redeem_encsig: EncryptedSignature) {
let msg = bob::Message3 { tx_redeem_encsig }; let msg = Message5 { tx_redeem_encsig };
self.message3.send(alice, msg); self.message5.send(alice, msg);
debug!("Sent Message3"); debug!("Sent Message3");
} }

View File

@ -217,7 +217,7 @@ impl EventLoop {
OutEvent::Message2(msg) => { OutEvent::Message2(msg) => {
let _ = self.msg2.send(msg).await; let _ = self.msg2.send(msg).await;
} }
OutEvent::Message3 => info!("Alice acknowledged message 3 received"), OutEvent::Message5 => info!("Alice acknowledged message 5 received"),
} }
}, },
option = self.dial_alice.next().fuse() => { option = self.dial_alice.next().fuse() => {

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
bitcoin::EncryptedSignature, bitcoin::EncryptedSignature,
network::request_response::{AliceToBob, BobToAlice, Codec, Message3Protocol, TIMEOUT}, network::request_response::{AliceToBob, BobToAlice, Codec, Message5Protocol, TIMEOUT},
}; };
use libp2p::{ use libp2p::{
request_response::{ request_response::{
@ -19,7 +19,7 @@ use std::{
use tracing::error; use tracing::error;
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message3 { pub struct Message5 {
pub tx_redeem_encsig: EncryptedSignature, pub tx_redeem_encsig: EncryptedSignature,
} }
@ -28,19 +28,19 @@ pub enum OutEvent {
Msg, Msg,
} }
/// A `NetworkBehaviour` that represents sending message 3 to Alice. /// A `NetworkBehaviour` that represents sending message 5 to Alice.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", poll_method = "poll")] #[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Behaviour { pub struct Behaviour {
rr: RequestResponse<Codec<Message3Protocol>>, rr: RequestResponse<Codec<Message5Protocol>>,
#[behaviour(ignore)] #[behaviour(ignore)]
events: VecDeque<OutEvent>, events: VecDeque<OutEvent>,
} }
impl Behaviour { impl Behaviour {
pub fn send(&mut self, alice: PeerId, msg: Message3) { pub fn send(&mut self, alice: PeerId, msg: Message5) {
let msg = BobToAlice::Message3(msg); let msg = BobToAlice::Message5(msg);
let _id = self.rr.send_request(&alice, msg); let _id = self.rr.send_request(&alice, msg);
} }
@ -48,7 +48,7 @@ impl Behaviour {
&mut self, &mut self,
_: &mut Context<'_>, _: &mut Context<'_>,
_: &mut impl PollParameters, _: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message3Protocol>>, OutEvent>> { ) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec<Message5Protocol>>, OutEvent>> {
if let Some(event) = self.events.pop_front() { if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
} }
@ -66,7 +66,7 @@ impl Default for Behaviour {
Self { Self {
rr: RequestResponse::new( rr: RequestResponse::new(
Codec::default(), Codec::default(),
vec![(Message3Protocol, ProtocolSupport::Full)], vec![(Message5Protocol, ProtocolSupport::Full)],
config, config,
), ),
events: Default::default(), events: Default::default(),
@ -85,7 +85,7 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
message: RequestResponseMessage::Response { response, .. }, message: RequestResponseMessage::Response { response, .. },
.. ..
} => { } => {
if let AliceToBob::Message3 = response { if let AliceToBob::Message5 = response {
self.events.push_back(OutEvent::Msg); self.events.push_back(OutEvent::Msg);
} }
} }

View File

@ -9,7 +9,7 @@ use crate::{
config::Config, config::Config,
monero, monero,
monero::{monero_private_key, TransferProof}, monero::{monero_private_key, TransferProof},
protocol::{alice, bob, SwapAmounts}, protocol::{alice, bob, bob::Message5, SwapAmounts},
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use ecdsa_fun::{ use ecdsa_fun::{
@ -438,11 +438,11 @@ pub struct State4 {
} }
impl State4 { impl State4 {
pub fn next_message(&self) -> bob::Message3 { pub fn next_message(&self) -> Message5 {
let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address); 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()); let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin, tx_redeem.digest());
bob::Message3 { tx_redeem_encsig } Message5 { tx_redeem_encsig }
} }
pub fn tx_redeem_encsig(&self) -> EncryptedSignature { pub fn tx_redeem_encsig(&self) -> EncryptedSignature {