Introduced Message 4 sent by Alice to replace message 2 response

This commit is contained in:
Franck Royer 2021-01-22 11:13:28 +11:00
parent edb93624f3
commit 124d6f1ebb
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 127 additions and 11 deletions

View File

@ -1,4 +1,4 @@
use crate::protocol::{alice, bob, bob::Message5}; use crate::protocol::{alice, alice::Message4, bob, bob::Message5};
use async_trait::async_trait; use async_trait::async_trait;
use futures::prelude::*; use futures::prelude::*;
use libp2p::{ use libp2p::{
@ -42,12 +42,14 @@ pub enum AliceToBob {
/// All responses are empty /// All responses are empty
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Request { pub enum Request {
Message5(Message5), Message4(Box<Message4>),
Message5(Box<Message5>),
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, Serialize, Deserialize)]
/// Response are only used for acknowledgement purposes. /// Response are only used for acknowledgement purposes.
pub enum Response { pub enum Response {
Message4,
Message5, Message5,
} }
@ -63,6 +65,9 @@ pub struct Message1Protocol;
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct Message2Protocol; pub struct Message2Protocol;
#[derive(Debug, Clone, Copy, Default)]
pub struct Message4Protocol;
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct Message5Protocol; pub struct Message5Protocol;
@ -90,6 +95,12 @@ impl ProtocolName for Message2Protocol {
} }
} }
impl ProtocolName for Message4Protocol {
fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message4/1.0.0"
}
}
impl ProtocolName for Message5Protocol { impl ProtocolName for Message5Protocol {
fn protocol_name(&self) -> &[u8] { fn protocol_name(&self) -> &[u8] {
b"/xmr/btc/message5/1.0.0" b"/xmr/btc/message5/1.0.0"

View File

@ -5,6 +5,7 @@ pub use self::{
message0::Message0, message0::Message0,
message1::Message1, message1::Message1,
message2::Message2, message2::Message2,
message4::Message4,
state::*, state::*,
swap::{run, run_until}, swap::{run, run_until},
swap_response::*, swap_response::*,
@ -37,6 +38,7 @@ pub mod event_loop;
mod message0; mod message0;
mod message1; mod message1;
mod message2; mod message2;
mod message4;
mod message5; mod message5;
pub mod state; pub mod state;
mod steps; mod steps;

View File

@ -0,0 +1,101 @@
use crate::{
monero,
network::request_response::{Message4Protocol, OneShotCodec, Request, Response, TIMEOUT},
};
use libp2p::{
request_response::{
handler::RequestProtocol, ProtocolSupport, RequestResponse, RequestResponseConfig,
RequestResponseEvent, RequestResponseMessage,
},
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
NetworkBehaviour, PeerId,
};
use serde::{Deserialize, Serialize};
use std::{
collections::VecDeque,
task::{Context, Poll},
time::Duration,
};
use tracing::error;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message4 {
pub tx_lock_proof: monero::TransferProof,
}
#[derive(Debug, Copy, Clone)]
pub enum OutEvent {
Msg,
}
/// A `NetworkBehaviour` that represents sending message 4 to Bob.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", poll_method = "poll")]
#[allow(missing_debug_implementations)]
pub struct Behaviour {
rr: RequestResponse<OneShotCodec<Message4Protocol>>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
}
impl Behaviour {
pub fn send(&mut self, bob: PeerId, msg: Message4) {
let msg = Request::Message4(Box::new(msg));
let _id = self.rr.send_request(&bob, msg);
}
fn poll(
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<OneShotCodec<Message4Protocol>>, OutEvent>>
{
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
Poll::Pending
}
}
impl Default for Behaviour {
fn default() -> Self {
let timeout = Duration::from_secs(TIMEOUT);
let mut config = RequestResponseConfig::default();
config.set_request_timeout(timeout);
Self {
rr: RequestResponse::new(
OneShotCodec::default(),
vec![(Message4Protocol, ProtocolSupport::Full)],
config,
),
events: Default::default(),
}
}
}
impl NetworkBehaviourEventProcess<RequestResponseEvent<Request, Response>> for Behaviour {
fn inject_event(&mut self, event: RequestResponseEvent<Request, Response>) {
match event {
RequestResponseEvent::Message {
message: RequestResponseMessage::Request { .. },
..
} => panic!("Alice should never get a message 4 request from Bob"),
RequestResponseEvent::Message {
message: RequestResponseMessage::Response { response, .. },
..
} => {
if let Response::Message4 = response {
self.events.push_back(OutEvent::Msg);
}
}
RequestResponseEvent::InboundFailure { error, .. } => {
error!("Inbound failure: {:?}", error);
}
RequestResponseEvent::OutboundFailure { error, .. } => {
error!("Outbound failure: {:?}", error);
}
}
}
}

View File

@ -74,11 +74,12 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<Request, Response>> for B
}, },
.. ..
} => { } => {
let Request::Message5(msg) = request; if let Request::Message5(msg) = request {
debug!("Received message 5"); 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.
let _ = self.rr.send_response(channel, Response::Message5); let _ = self.rr.send_response(channel, Response::Message5);
}
} }
RequestResponseEvent::Message { RequestResponseEvent::Message {
message: RequestResponseMessage::Response { .. }, message: RequestResponseMessage::Response { .. },

View File

@ -40,7 +40,7 @@ pub struct Behaviour {
impl Behaviour { impl Behaviour {
pub fn send(&mut self, alice: PeerId, msg: Message5) { pub fn send(&mut self, alice: PeerId, msg: Message5) {
let msg = Request::Message5(msg); let msg = Request::Message5(Box::new(msg));
let _id = self.rr.send_request(&alice, msg); let _id = self.rr.send_request(&alice, msg);
} }
@ -86,8 +86,9 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<Request, Response>> for B
message: RequestResponseMessage::Response { response, .. }, message: RequestResponseMessage::Response { response, .. },
.. ..
} => { } => {
let Response::Message5 = response; if let Response::Message5 = response {
self.events.push_back(OutEvent::Msg); self.events.push_back(OutEvent::Msg);
}
} }
RequestResponseEvent::InboundFailure { error, .. } => { RequestResponseEvent::InboundFailure { error, .. } => {
error!("Inbound failure: {:?}", error); error!("Inbound failure: {:?}", error);