Introduce helper function for mapping RequestResponseEvent

Decomposing a RequestResponseEvent is quite verbose. We can introduce
a helper function that does the matching for us and delegates to
specific `From` implementations for the protocol specific bits.
This commit is contained in:
Thomas Eizinger 2021-03-18 16:59:51 +11:00
parent 73f30320a6
commit 9d0b9abde0
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
5 changed files with 114 additions and 108 deletions

View File

@ -3,6 +3,7 @@ use crate::network::request_response::CborCodec;
use libp2p::core::ProtocolName;
use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage,
};
use serde::{Deserialize, Serialize};
@ -17,6 +18,8 @@ impl ProtocolName for BidQuoteProtocol {
}
}
pub type Message = RequestResponseMessage<(), BidQuote>;
/// Represents a quote for buying XMR.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BidQuote {

View File

@ -3,6 +3,7 @@ use crate::{bitcoin, monero};
use libp2p::core::ProtocolName;
use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage,
};
use serde::{Deserialize, Serialize};
@ -39,6 +40,8 @@ pub struct Response {
pub type Behaviour = RequestResponse<CborCodec<SpotPriceProtocol, Request, Response>>;
pub type Message = RequestResponseMessage<Request, Response>;
/// Constructs a new instance of the `spot-price` behaviour to be used by Alice.
///
/// Alice only supports inbound connections, i.e. providing spot prices for BTC

View File

@ -7,7 +7,7 @@ use crate::protocol::alice::{
use crate::protocol::bob::EncryptedSignature;
use crate::{bitcoin, monero};
use anyhow::{anyhow, Error, Result};
use libp2p::request_response::{RequestResponseMessage, ResponseChannel};
use libp2p::request_response::{RequestResponseEvent, RequestResponseMessage, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
use rand::{CryptoRng, RngCore};
use tracing::debug;
@ -16,7 +16,7 @@ use tracing::debug;
pub enum OutEvent {
ConnectionEstablished(PeerId),
SpotPriceRequested {
msg: spot_price::Request,
request: spot_price::Request,
channel: ResponseChannel<spot_price::Response>,
peer: PeerId,
},
@ -51,62 +51,68 @@ impl From<peer_tracker::OutEvent> for OutEvent {
}
}
impl OutEvent {
fn unexpected_response(peer: PeerId) -> OutEvent {
OutEvent::Failure {
peer,
error: anyhow!("Unexpected response received"),
}
}
}
impl From<(PeerId, quote::Message)> for OutEvent {
fn from((peer, message): (PeerId, quote::Message)) -> Self {
match message {
quote::Message::Request { channel, .. } => OutEvent::QuoteRequested { channel, peer },
quote::Message::Response { .. } => OutEvent::unexpected_response(peer),
}
}
}
impl From<(PeerId, spot_price::Message)> for OutEvent {
fn from((peer, message): (PeerId, spot_price::Message)) -> Self {
match message {
spot_price::Message::Request {
request, channel, ..
} => OutEvent::SpotPriceRequested {
request,
channel,
peer,
},
spot_price::Message::Response { .. } => OutEvent::unexpected_response(peer),
}
}
}
impl From<spot_price::OutEvent> for OutEvent {
fn from(event: spot_price::OutEvent) -> Self {
match event {
spot_price::OutEvent::Message {
peer,
message:
RequestResponseMessage::Request {
channel,
request: msg,
..
},
} => OutEvent::SpotPriceRequested { msg, channel, peer },
spot_price::OutEvent::Message {
message: RequestResponseMessage::Response { .. },
peer,
} => OutEvent::Failure {
error: anyhow!("Alice is only meant to hand out spot prices, not receive them"),
peer,
},
spot_price::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent,
spot_price::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("spot_price protocol failed due to {:?}", error),
peer,
},
spot_price::OutEvent::OutboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("spot_price protocol failed due to {:?}", error),
peer,
},
}
map_rr_event_to_outevent(event)
}
}
impl From<quote::OutEvent> for OutEvent {
fn from(event: quote::OutEvent) -> Self {
match event {
quote::OutEvent::Message {
peer,
message: RequestResponseMessage::Request { channel, .. },
} => OutEvent::QuoteRequested { channel, peer },
quote::OutEvent::Message {
message: RequestResponseMessage::Response { .. },
peer,
} => OutEvent::Failure {
error: anyhow!("Alice is only meant to hand out quotes, not receive them"),
peer,
},
quote::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent,
quote::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("quote protocol failed due to {:?}", error),
peer,
},
quote::OutEvent::OutboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("quote protocol failed due to {:?}", error),
peer,
},
}
map_rr_event_to_outevent(event)
}
}
fn map_rr_event_to_outevent<I, O>(event: RequestResponseEvent<I, O>) -> OutEvent
where
OutEvent: From<(PeerId, RequestResponseMessage<I, O>)>,
{
use RequestResponseEvent::*;
match event {
Message { message, peer, .. } => OutEvent::from((peer, message)),
ResponseSent { .. } => OutEvent::ResponseSent,
InboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("protocol failed due to {:?}", error),
peer,
},
OutboundFailure { peer, error, .. } => OutEvent::Failure {
error: anyhow!("protocol failed due to {:?}", error),
peer,
},
}
}

View File

@ -105,8 +105,8 @@ where
OutEvent::ConnectionEstablished(alice) => {
debug!("Connection Established with {}", alice);
}
OutEvent::SpotPriceRequested { msg, channel, peer } => {
let btc = msg.btc;
OutEvent::SpotPriceRequested { request, channel, peer } => {
let btc = request.btc;
let xmr = match self.handle_spot_price_request(btc, self.monero_wallet.clone()).await {
Ok(xmr) => xmr,
Err(e) => {

View File

@ -7,7 +7,7 @@ use crate::{bitcoin, monero};
use anyhow::{anyhow, Error, Result};
pub use execution_setup::{Message0, Message2, Message4};
use libp2p::core::Multiaddr;
use libp2p::request_response::{RequestResponseMessage, ResponseChannel};
use libp2p::request_response::{RequestResponseEvent, RequestResponseMessage, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
use std::sync::Arc;
use tracing::debug;
@ -126,6 +126,30 @@ pub enum OutEvent {
CommunicationError(Error),
}
impl OutEvent {
fn unexpected_request() -> OutEvent {
OutEvent::CommunicationError(anyhow!("Unexpected request received"))
}
}
impl From<quote::Message> for OutEvent {
fn from(message: quote::Message) -> Self {
match message {
quote::Message::Request { .. } => OutEvent::unexpected_request(),
quote::Message::Response { response, .. } => OutEvent::QuoteReceived(response),
}
}
}
impl From<spot_price::Message> for OutEvent {
fn from(message: spot_price::Message) -> Self {
match message {
spot_price::Message::Request { .. } => OutEvent::unexpected_request(),
spot_price::Message::Response { response, .. } => OutEvent::SpotPriceReceived(response),
}
}
}
impl From<peer_tracker::OutEvent> for OutEvent {
fn from(event: peer_tracker::OutEvent) -> Self {
match event {
@ -138,65 +162,35 @@ impl From<peer_tracker::OutEvent> for OutEvent {
impl From<spot_price::OutEvent> for OutEvent {
fn from(event: spot_price::OutEvent) -> Self {
match event {
spot_price::OutEvent::Message {
message: RequestResponseMessage::Response { response, .. },
..
} => OutEvent::SpotPriceReceived(response),
spot_price::OutEvent::Message {
message: RequestResponseMessage::Request { .. },
..
} => OutEvent::CommunicationError(anyhow!(
"Bob is only meant to receive spot prices, not hand them out"
)),
spot_price::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent,
spot_price::OutEvent::InboundFailure { peer, error, .. } => {
OutEvent::CommunicationError(anyhow!(
"spot_price protocol with peer {} failed due to {:?}",
peer,
error
))
}
spot_price::OutEvent::OutboundFailure { peer, error, .. } => {
OutEvent::CommunicationError(anyhow!(
"spot_price protocol with peer {} failed due to {:?}",
peer,
error
))
}
}
map_rr_event_to_outevent(event)
}
}
impl From<quote::OutEvent> for OutEvent {
fn from(event: quote::OutEvent) -> Self {
match event {
quote::OutEvent::Message {
message: RequestResponseMessage::Response { response, .. },
..
} => OutEvent::QuoteReceived(response),
quote::OutEvent::Message {
message: RequestResponseMessage::Request { .. },
..
} => OutEvent::CommunicationError(anyhow!(
"Bob is only meant to receive quotes, not hand them out"
)),
quote::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent,
quote::OutEvent::InboundFailure { peer, error, .. } => {
OutEvent::CommunicationError(anyhow!(
"quote protocol with peer {} failed due to {:?}",
peer,
error
))
}
quote::OutEvent::OutboundFailure { peer, error, .. } => {
OutEvent::CommunicationError(anyhow!(
"quote protocol with peer {} failed due to {:?}",
peer,
error
))
}
}
map_rr_event_to_outevent(event)
}
}
fn map_rr_event_to_outevent<I, O>(event: RequestResponseEvent<I, O>) -> OutEvent
where
OutEvent: From<RequestResponseMessage<I, O>>,
{
use RequestResponseEvent::*;
match event {
Message { message, .. } => OutEvent::from(message),
ResponseSent { .. } => OutEvent::ResponseSent,
InboundFailure { peer, error, .. } => OutEvent::CommunicationError(anyhow!(
"protocol with peer {} failed due to {:?}",
peer,
error
)),
OutboundFailure { peer, error, .. } => OutEvent::CommunicationError(anyhow!(
"protocol with peer {} failed due to {:?}",
peer,
error
)),
}
}