mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-23 22:01:21 -05:00
Introduced Message 4 sent by Alice to replace message 2 response
This commit is contained in:
parent
edb93624f3
commit
124d6f1ebb
@ -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 futures::prelude::*;
|
||||
use libp2p::{
|
||||
@ -42,12 +42,14 @@ pub enum AliceToBob {
|
||||
/// All responses are empty
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
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.
|
||||
pub enum Response {
|
||||
Message4,
|
||||
Message5,
|
||||
}
|
||||
|
||||
@ -63,6 +65,9 @@ pub struct Message1Protocol;
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct Message2Protocol;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct Message4Protocol;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
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 {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
b"/xmr/btc/message5/1.0.0"
|
||||
|
@ -5,6 +5,7 @@ pub use self::{
|
||||
message0::Message0,
|
||||
message1::Message1,
|
||||
message2::Message2,
|
||||
message4::Message4,
|
||||
state::*,
|
||||
swap::{run, run_until},
|
||||
swap_response::*,
|
||||
@ -37,6 +38,7 @@ pub mod event_loop;
|
||||
mod message0;
|
||||
mod message1;
|
||||
mod message2;
|
||||
mod message4;
|
||||
mod message5;
|
||||
pub mod state;
|
||||
mod steps;
|
||||
|
101
swap/src/protocol/alice/message4.rs
Normal file
101
swap/src/protocol/alice/message4.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -74,11 +74,12 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<Request, Response>> for B
|
||||
},
|
||||
..
|
||||
} => {
|
||||
let Request::Message5(msg) = request;
|
||||
debug!("Received message 5");
|
||||
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);
|
||||
if let Request::Message5(msg) = request {
|
||||
debug!("Received message 5");
|
||||
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);
|
||||
}
|
||||
}
|
||||
RequestResponseEvent::Message {
|
||||
message: RequestResponseMessage::Response { .. },
|
||||
|
@ -40,7 +40,7 @@ pub struct Behaviour {
|
||||
|
||||
impl Behaviour {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -86,8 +86,9 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<Request, Response>> for B
|
||||
message: RequestResponseMessage::Response { response, .. },
|
||||
..
|
||||
} => {
|
||||
let Response::Message5 = response;
|
||||
self.events.push_back(OutEvent::Msg);
|
||||
if let Response::Message5 = response {
|
||||
self.events.push_back(OutEvent::Msg);
|
||||
}
|
||||
}
|
||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
||||
error!("Inbound failure: {:?}", error);
|
||||
|
Loading…
Reference in New Issue
Block a user