@ -1,6 +1,6 @@
use crate ::bitcoin ::EncryptedSignature ;
use crate ::bitcoin ::EncryptedSignature ;
use crate ::network ::spot_price::{ Request , Response } ;
use crate ::network ::quote::BidQuote ;
use crate ::network ::{ transport, TokioExecutor } ;
use crate ::network ::{ spot_price, transport, TokioExecutor } ;
use crate ::protocol ::alice ::TransferProof ;
use crate ::protocol ::alice ::TransferProof ;
use crate ::protocol ::bob ::{ Behaviour , OutEvent , State0 , State2 } ;
use crate ::protocol ::bob ::{ Behaviour , OutEvent , State0 , State2 } ;
use crate ::{ bitcoin , monero } ;
use crate ::{ bitcoin , monero } ;
@ -34,14 +34,16 @@ impl<T> Default for Channels<T> {
#[ derive(Debug) ]
#[ derive(Debug) ]
pub struct EventLoopHandle {
pub struct EventLoopHandle {
recv_spot_price : Receiver < Response > ,
start_execution_setup : Sender < State0 > ,
start_execution_setup : Sender < State0 > ,
done_execution_setup : Receiver < Result < State2 > > ,
done_execution_setup : Receiver < Result < State2 > > ,
recv_transfer_proof : Receiver < TransferProof > ,
recv_transfer_proof : Receiver < TransferProof > ,
conn_established : Receiver < PeerId > ,
conn_established : Receiver < PeerId > ,
dial_alice : Sender < ( ) > ,
dial_alice : Sender < ( ) > ,
request_spot_price : Sender < Request > ,
send_encrypted_signature : Sender < EncryptedSignature > ,
send_encrypted_signature : Sender < EncryptedSignature > ,
request_spot_price : Sender < spot_price ::Request > ,
recv_spot_price : Receiver < spot_price ::Response > ,
request_quote : Sender < ( ) > ,
recv_quote : Receiver < BidQuote > ,
}
}
impl EventLoopHandle {
impl EventLoopHandle {
@ -75,7 +77,10 @@ impl EventLoopHandle {
}
}
pub async fn request_spot_price ( & mut self , btc : bitcoin ::Amount ) -> Result < monero ::Amount > {
pub async fn request_spot_price ( & mut self , btc : bitcoin ::Amount ) -> Result < monero ::Amount > {
let _ = self . request_spot_price . send ( Request { btc } ) . await ? ;
let _ = self
. request_spot_price
. send ( spot_price ::Request { btc } )
. await ? ;
let response = self
let response = self
. recv_spot_price
. recv_spot_price
@ -86,6 +91,18 @@ impl EventLoopHandle {
Ok ( response . xmr )
Ok ( response . xmr )
}
}
pub async fn request_quote ( & mut self ) -> Result < BidQuote > {
let _ = self . request_quote . send ( ( ) ) . await ? ;
let quote = self
. recv_quote
. recv ( )
. await
. ok_or_else ( | | anyhow ! ( "Failed to receive quote from Alice" ) ) ? ;
Ok ( quote )
}
pub async fn send_encrypted_signature (
pub async fn send_encrypted_signature (
& mut self ,
& mut self ,
tx_redeem_encsig : EncryptedSignature ,
tx_redeem_encsig : EncryptedSignature ,
@ -101,14 +118,16 @@ pub struct EventLoop {
swarm : libp2p ::Swarm < Behaviour > ,
swarm : libp2p ::Swarm < Behaviour > ,
bitcoin_wallet : Arc < bitcoin ::Wallet > ,
bitcoin_wallet : Arc < bitcoin ::Wallet > ,
alice_peer_id : PeerId ,
alice_peer_id : PeerId ,
request_spot_price : Receiver < Request> ,
request_spot_price : Receiver < spot_price:: Request> ,
recv_spot_price : Sender < Response> ,
recv_spot_price : Sender < spot_price:: Response> ,
start_execution_setup : Receiver < State0 > ,
start_execution_setup : Receiver < State0 > ,
done_execution_setup : Sender < Result < State2 > > ,
done_execution_setup : Sender < Result < State2 > > ,
recv_transfer_proof : Sender < TransferProof > ,
recv_transfer_proof : Sender < TransferProof > ,
dial_alice : Receiver < ( ) > ,
dial_alice : Receiver < ( ) > ,
conn_established : Sender < PeerId > ,
conn_established : Sender < PeerId > ,
send_encrypted_signature : Receiver < EncryptedSignature > ,
send_encrypted_signature : Receiver < EncryptedSignature > ,
request_quote : Receiver < ( ) > ,
recv_quote : Sender < BidQuote > ,
}
}
impl EventLoop {
impl EventLoop {
@ -133,38 +152,44 @@ impl EventLoop {
swarm . add_address ( alice_peer_id , alice_addr ) ;
swarm . add_address ( alice_peer_id , alice_addr ) ;
let quote_response = Channels ::new ( ) ;
let start_execution_setup = Channels ::new ( ) ;
let start_execution_setup = Channels ::new ( ) ;
let done_execution_setup = Channels ::new ( ) ;
let done_execution_setup = Channels ::new ( ) ;
let recv_transfer_proof = Channels ::new ( ) ;
let recv_transfer_proof = Channels ::new ( ) ;
let dial_alice = Channels ::new ( ) ;
let dial_alice = Channels ::new ( ) ;
let conn_established = Channels ::new ( ) ;
let conn_established = Channels ::new ( ) ;
let send_quote_request = Channels ::new ( ) ;
let send_encrypted_signature = Channels ::new ( ) ;
let send_encrypted_signature = Channels ::new ( ) ;
let request_spot_price = Channels ::new ( ) ;
let recv_spot_price = Channels ::new ( ) ;
let request_quote = Channels ::new ( ) ;
let recv_quote = Channels ::new ( ) ;
let event_loop = EventLoop {
let event_loop = EventLoop {
swarm ,
swarm ,
alice_peer_id ,
alice_peer_id ,
bitcoin_wallet ,
bitcoin_wallet ,
recv_spot_price : quote_response . sender ,
start_execution_setup : start_execution_setup . receiver ,
start_execution_setup : start_execution_setup . receiver ,
done_execution_setup : done_execution_setup . sender ,
done_execution_setup : done_execution_setup . sender ,
recv_transfer_proof : recv_transfer_proof . sender ,
recv_transfer_proof : recv_transfer_proof . sender ,
conn_established : conn_established . sender ,
conn_established : conn_established . sender ,
dial_alice : dial_alice . receiver ,
dial_alice : dial_alice . receiver ,
request_spot_price : send_quote_request . receiver ,
send_encrypted_signature : send_encrypted_signature . receiver ,
send_encrypted_signature : send_encrypted_signature . receiver ,
request_spot_price : request_spot_price . receiver ,
recv_spot_price : recv_spot_price . sender ,
request_quote : request_quote . receiver ,
recv_quote : recv_quote . sender ,
} ;
} ;
let handle = EventLoopHandle {
let handle = EventLoopHandle {
recv_spot_price : quote_response . receiver ,
start_execution_setup : start_execution_setup . sender ,
start_execution_setup : start_execution_setup . sender ,
done_execution_setup : done_execution_setup . receiver ,
done_execution_setup : done_execution_setup . receiver ,
recv_transfer_proof : recv_transfer_proof . receiver ,
recv_transfer_proof : recv_transfer_proof . receiver ,
conn_established : conn_established . receiver ,
conn_established : conn_established . receiver ,
dial_alice : dial_alice . sender ,
dial_alice : dial_alice . sender ,
request_spot_price : send_quote_request . sender ,
send_encrypted_signature : send_encrypted_signature . sender ,
send_encrypted_signature : send_encrypted_signature . sender ,
request_spot_price : request_spot_price . sender ,
recv_spot_price : recv_spot_price . receiver ,
request_quote : request_quote . sender ,
recv_quote : recv_quote . receiver ,
} ;
} ;
Ok ( ( event_loop , handle ) )
Ok ( ( event_loop , handle ) )
@ -181,6 +206,9 @@ impl EventLoop {
OutEvent ::SpotPriceReceived ( msg ) = > {
OutEvent ::SpotPriceReceived ( msg ) = > {
let _ = self . recv_spot_price . send ( msg ) . await ;
let _ = self . recv_spot_price . send ( msg ) . await ;
} ,
} ,
OutEvent ::QuoteReceived ( msg ) = > {
let _ = self . recv_quote . send ( msg ) . await ;
} ,
OutEvent ::ExecutionSetupDone ( res ) = > {
OutEvent ::ExecutionSetupDone ( res ) = > {
let _ = self . done_execution_setup . send ( res . map ( | state | * state ) ) . await ;
let _ = self . done_execution_setup . send ( res . map ( | state | * state ) ) . await ;
}
}
@ -212,9 +240,14 @@ impl EventLoop {
}
}
}
}
} ,
} ,
quote_request = self . request_spot_price . recv ( ) . fuse ( ) = > {
spot_price_request = self . request_spot_price . recv ( ) . fuse ( ) = > {
if let Some ( quote_request ) = quote_request {
if let Some ( request ) = spot_price_request {
self . swarm . request_spot_price ( self . alice_peer_id , quote_request ) ;
self . swarm . request_spot_price ( self . alice_peer_id , request ) ;
}
} ,
quote_request = self . request_quote . recv ( ) . fuse ( ) = > {
if quote_request . is_some ( ) {
self . swarm . request_quote ( self . alice_peer_id ) ;
}
}
} ,
} ,
option = self . start_execution_setup . recv ( ) . fuse ( ) = > {
option = self . start_execution_setup . recv ( ) . fuse ( ) = > {