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.
pull/340/head
Thomas Eizinger 3 years ago
parent 73f30320a6
commit 9d0b9abde0
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

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

@ -3,6 +3,7 @@ use crate::{bitcoin, monero};
use libp2p::core::ProtocolName; use libp2p::core::ProtocolName;
use libp2p::request_response::{ use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent, ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -39,6 +40,8 @@ pub struct Response {
pub type Behaviour = RequestResponse<CborCodec<SpotPriceProtocol, Request, 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. /// 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 /// Alice only supports inbound connections, i.e. providing spot prices for BTC

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

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

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

Loading…
Cancel
Save