Send back an empty response to Message2

Alice does not respond with anything when receiving message 2 from Bob. We don't
want to leave Bob's request/response protocol waiting so send an empty response
back.
pull/28/head
Tobin C. Harding 4 years ago
parent 194a19cf1d
commit b22f265cf3

@ -71,11 +71,17 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) { fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event { match event {
RequestResponseEvent::Message { RequestResponseEvent::Message {
message: RequestResponseMessage::Request { request, .. }, message:
RequestResponseMessage::Request {
request, channel, ..
},
.. ..
} => match request { } => match request {
BobToAlice::Message2(msg) => { BobToAlice::Message2(msg) => {
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.
let msg = AliceToBob::EmptyResponse;
self.rr.send_response(channel, msg);
} }
other => debug!("got request: {:?}", other), other => debug!("got request: {:?}", other),
}, },

@ -21,7 +21,7 @@ use crate::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
transport, TokioExecutor, transport, TokioExecutor,
}, },
Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, Cmd, Never, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
}; };
use xmr_btc::{ use xmr_btc::{
alice, alice,
@ -134,7 +134,6 @@ pub enum OutEvent {
Amounts(SwapAmounts), Amounts(SwapAmounts),
Message0(alice::Message0), Message0(alice::Message0),
Message1(alice::Message1), Message1(alice::Message1),
Message2(alice::Message2),
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -171,11 +170,9 @@ impl From<message1::OutEvent> for OutEvent {
} }
} }
impl From<message2::OutEvent> for OutEvent { impl From<Never> for OutEvent {
fn from(event: message2::OutEvent) -> Self { fn from(_: Never) -> Self {
match event { panic!("this never happens")
message2::OutEvent::Msg(msg) => OutEvent::Message2(msg),
}
} }
} }

@ -7,28 +7,23 @@ use libp2p::{
NetworkBehaviour, PeerId, NetworkBehaviour, PeerId,
}; };
use std::{ use std::{
collections::VecDeque,
task::{Context, Poll}, task::{Context, Poll},
time::Duration, time::Duration,
}; };
use tracing::{debug, error}; use tracing::{debug, error};
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT}; use crate::{
use xmr_btc::{alice, bob}; network::request_response::{AliceToBob, BobToAlice, Codec, Protocol, TIMEOUT},
Never,
#[derive(Debug)] };
pub enum OutEvent { use xmr_btc::bob;
Msg(alice::Message2),
}
/// A `NetworkBehaviour` that represents sending message 2 to Alice. /// A `NetworkBehaviour` that represents sending message 2 to Alice.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", poll_method = "poll")] #[behaviour(out_event = "Never", poll_method = "poll")]
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Message2 { pub struct Message2 {
rr: RequestResponse<Codec>, rr: RequestResponse<Codec>,
#[behaviour(ignore)]
events: VecDeque<OutEvent>,
} }
impl Message2 { impl Message2 {
@ -37,15 +32,13 @@ impl Message2 {
let _id = self.rr.send_request(&alice, msg); let _id = self.rr.send_request(&alice, msg);
} }
// TODO: Do we need a custom implementation if we are not bubbling any out
// events?
fn poll( fn poll(
&mut self, &mut self,
_: &mut Context<'_>, _: &mut Context<'_>,
_: &mut impl PollParameters, _: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, OutEvent>> { ) -> Poll<NetworkBehaviourAction<RequestProtocol<Codec>, Never>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
Poll::Pending Poll::Pending
} }
} }
@ -62,7 +55,6 @@ impl Default for Message2 {
vec![(Protocol, ProtocolSupport::Full)], vec![(Protocol, ProtocolSupport::Full)],
config, config,
), ),
events: Default::default(),
} }
} }
} }
@ -78,8 +70,8 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
message: RequestResponseMessage::Response { response, .. }, message: RequestResponseMessage::Response { response, .. },
.. ..
} => match response { } => match response {
AliceToBob::Message2(msg) => self.events.push_back(OutEvent::Msg(msg)), AliceToBob::EmptyResponse => debug!("Alice correctly responded to message 2"),
other => debug!("got response: {:?}", other), other => debug!("unexpected response: {:?}", other),
}, },
RequestResponseEvent::InboundFailure { error, .. } => { RequestResponseEvent::InboundFailure { error, .. } => {
error!("Inbound failure: {:?}", error); error!("Inbound failure: {:?}", error);

@ -35,6 +35,7 @@ pub enum AliceToBob {
Amounts(SwapAmounts), Amounts(SwapAmounts),
Message0(alice::Message0), Message0(alice::Message0),
Message1(alice::Message1), Message1(alice::Message1),
EmptyResponse, // This is sent back as response to Message2 from Bob.
Message2(alice::Message2), Message2(alice::Message2),
} }

Loading…
Cancel
Save