@ -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 ::{ RequestResponse Message, ResponseChannel } ;
use libp2p ::request_response ::{ RequestResponse Event, RequestResponse Message, 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 ! ( "Unex pect ed 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 : RequestResponse Message::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 ::Failur e {
match messag e {
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 ,
} ,
} ,
}
}
}
}
}