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.
This commit is contained in:
Tobin C. Harding 2020-10-27 10:13:59 +11:00
parent 194a19cf1d
commit b22f265cf3
4 changed files with 23 additions and 27 deletions

View File

@ -71,11 +71,17 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
fn inject_event(&mut self, event: RequestResponseEvent<BobToAlice, AliceToBob>) {
match event {
RequestResponseEvent::Message {
message: RequestResponseMessage::Request { request, .. },
message:
RequestResponseMessage::Request {
request, channel, ..
},
..
} => match request {
BobToAlice::Message2(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),
},

View File

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

View File

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

View File

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