mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
The first message is actually a quote or rate request
This commit is contained in:
parent
c117885dad
commit
9ded728879
@ -9,11 +9,11 @@ group Negotiation
|
|||||||
Bob --> Alice: Establish connection
|
Bob --> Alice: Establish connection
|
||||||
|
|
||||||
group Request Response Channel
|
group Request Response Channel
|
||||||
Bob -> Alice: Swap request
|
Bob -> Alice: Quote request
|
||||||
note left: Btc Amount\naddress hints
|
note left: Btc Amount\naddress hints
|
||||||
note right: Alice stores peer id & address hints\nto contact Bob
|
note right: Alice stores peer id & address hints\nto contact Bob
|
||||||
|
|
||||||
Alice --> Bob: Swap response
|
Alice --> Bob: Quote response
|
||||||
note right: Exact XMR amount
|
note right: Exact XMR amount
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -13,9 +13,9 @@ pub use self::{
|
|||||||
behaviour::{Behaviour, OutEvent},
|
behaviour::{Behaviour, OutEvent},
|
||||||
event_loop::{EventLoop, EventLoopHandle},
|
event_loop::{EventLoop, EventLoopHandle},
|
||||||
execution_setup::Message1,
|
execution_setup::Message1,
|
||||||
|
quote_response::*,
|
||||||
state::*,
|
state::*,
|
||||||
swap::{run, run_until},
|
swap::{run, run_until},
|
||||||
swap_response::*,
|
|
||||||
transfer_proof::TransferProof,
|
transfer_proof::TransferProof,
|
||||||
};
|
};
|
||||||
pub use execution_setup::Message3;
|
pub use execution_setup::Message3;
|
||||||
@ -24,10 +24,10 @@ mod behaviour;
|
|||||||
mod encrypted_signature;
|
mod encrypted_signature;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution_setup;
|
mod execution_setup;
|
||||||
|
mod quote_response;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
mod steps;
|
mod steps;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
mod swap_response;
|
|
||||||
mod transfer_proof;
|
mod transfer_proof;
|
||||||
|
|
||||||
pub struct Swap {
|
pub struct Swap {
|
||||||
|
@ -2,10 +2,10 @@ use crate::{
|
|||||||
network::{peer_tracker, peer_tracker::PeerTracker},
|
network::{peer_tracker, peer_tracker::PeerTracker},
|
||||||
protocol::{
|
protocol::{
|
||||||
alice::{
|
alice::{
|
||||||
encrypted_signature, execution_setup, swap_response, transfer_proof, State0, State3,
|
encrypted_signature, execution_setup, quote_response, transfer_proof, QuoteResponse,
|
||||||
SwapResponse, TransferProof,
|
State0, State3, TransferProof,
|
||||||
},
|
},
|
||||||
bob::{EncryptedSignature, SwapRequest},
|
bob::{EncryptedSignature, QuoteRequest},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
@ -15,9 +15,9 @@ use tracing::{debug, info};
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
ConnectionEstablished(PeerId),
|
ConnectionEstablished(PeerId),
|
||||||
SwapRequest {
|
QuoteRequest {
|
||||||
msg: SwapRequest,
|
msg: QuoteRequest,
|
||||||
channel: ResponseChannel<SwapResponse>,
|
channel: ResponseChannel<QuoteResponse>,
|
||||||
bob_peer_id: PeerId,
|
bob_peer_id: PeerId,
|
||||||
},
|
},
|
||||||
ExecutionSetupDone {
|
ExecutionSetupDone {
|
||||||
@ -43,21 +43,21 @@ impl From<peer_tracker::OutEvent> for OutEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<swap_response::OutEvent> for OutEvent {
|
impl From<quote_response::OutEvent> for OutEvent {
|
||||||
fn from(event: swap_response::OutEvent) -> Self {
|
fn from(event: quote_response::OutEvent) -> Self {
|
||||||
use crate::protocol::alice::swap_response::OutEvent::*;
|
use crate::protocol::alice::quote_response::OutEvent::*;
|
||||||
match event {
|
match event {
|
||||||
MsgReceived {
|
MsgReceived {
|
||||||
msg,
|
msg,
|
||||||
channel,
|
channel,
|
||||||
bob_peer_id,
|
bob_peer_id,
|
||||||
} => OutEvent::SwapRequest {
|
} => OutEvent::QuoteRequest {
|
||||||
msg,
|
msg,
|
||||||
channel,
|
channel,
|
||||||
bob_peer_id,
|
bob_peer_id,
|
||||||
},
|
},
|
||||||
ResponseSent => OutEvent::ResponseSent,
|
ResponseSent => OutEvent::ResponseSent,
|
||||||
Failure(err) => OutEvent::Failure(err.context("Swap Request/Response failure")),
|
Failure(err) => OutEvent::Failure(err.context("Quote Request/Response failure")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,21 +108,20 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
|
|||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
pt: PeerTracker,
|
pt: PeerTracker,
|
||||||
swap_response: swap_response::Behaviour,
|
quote_response: quote_response::Behaviour,
|
||||||
execution_setup: execution_setup::Behaviour,
|
execution_setup: execution_setup::Behaviour,
|
||||||
transfer_proof: transfer_proof::Behaviour,
|
transfer_proof: transfer_proof::Behaviour,
|
||||||
encrypted_signature: encrypted_signature::Behaviour,
|
encrypted_signature: encrypted_signature::Behaviour,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
/// Alice always sends her messages as a response to a request from Bob.
|
pub fn send_quote_response(
|
||||||
pub fn send_swap_response(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
channel: ResponseChannel<SwapResponse>,
|
channel: ResponseChannel<QuoteResponse>,
|
||||||
swap_response: SwapResponse,
|
quote_response: QuoteResponse,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.swap_response.send(channel, swap_response)?;
|
self.quote_response.send(channel, quote_response)?;
|
||||||
info!("Sent swap response");
|
info!("Sent quote response");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ use crate::{
|
|||||||
network::{transport, TokioExecutor},
|
network::{transport, TokioExecutor},
|
||||||
protocol::{
|
protocol::{
|
||||||
alice,
|
alice,
|
||||||
alice::{Behaviour, Builder, OutEvent, State0, State3, SwapResponse, TransferProof},
|
alice::{Behaviour, Builder, OutEvent, QuoteResponse, State0, State3, TransferProof},
|
||||||
bob::{EncryptedSignature, SwapRequest},
|
bob::{EncryptedSignature, QuoteRequest},
|
||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
},
|
},
|
||||||
seed::Seed,
|
seed::Seed,
|
||||||
@ -157,8 +157,8 @@ impl EventLoop {
|
|||||||
OutEvent::ConnectionEstablished(alice) => {
|
OutEvent::ConnectionEstablished(alice) => {
|
||||||
debug!("Connection Established with {}", alice);
|
debug!("Connection Established with {}", alice);
|
||||||
}
|
}
|
||||||
OutEvent::SwapRequest { msg, channel, bob_peer_id } => {
|
OutEvent::QuoteRequest { msg, channel, bob_peer_id } => {
|
||||||
let _ = self.handle_swap_request(msg, channel, bob_peer_id).await;
|
let _ = self.handle_quote_request(msg, channel, bob_peer_id).await;
|
||||||
}
|
}
|
||||||
OutEvent::ExecutionSetupDone{bob_peer_id, state3} => {
|
OutEvent::ExecutionSetupDone{bob_peer_id, state3} => {
|
||||||
let _ = self.handle_execution_setup_done(bob_peer_id, *state3).await;
|
let _ = self.handle_execution_setup_done(bob_peer_id, *state3).await;
|
||||||
@ -188,23 +188,23 @@ impl EventLoop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_swap_request(
|
async fn handle_quote_request(
|
||||||
&mut self,
|
&mut self,
|
||||||
swap_request: SwapRequest,
|
quote_request: QuoteRequest,
|
||||||
channel: ResponseChannel<SwapResponse>,
|
channel: ResponseChannel<QuoteResponse>,
|
||||||
bob_peer_id: PeerId,
|
bob_peer_id: PeerId,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// 1. Check if acceptable request
|
// 1. Check if acceptable request
|
||||||
// 2. Send response
|
// 2. Send response
|
||||||
|
|
||||||
let btc_amount = swap_request.btc_amount;
|
let btc_amount = quote_request.btc_amount;
|
||||||
let xmr_amount = btc_amount.as_btc() * RATE as f64;
|
let xmr_amount = btc_amount.as_btc() * RATE as f64;
|
||||||
let xmr_amount = monero::Amount::from_monero(xmr_amount)?;
|
let xmr_amount = monero::Amount::from_monero(xmr_amount)?;
|
||||||
let swap_response = SwapResponse { xmr_amount };
|
let quote_response = QuoteResponse { xmr_amount };
|
||||||
|
|
||||||
self.swarm
|
self.swarm
|
||||||
.send_swap_response(channel, swap_response)
|
.send_quote_response(channel, quote_response)
|
||||||
.context("Failed to send swap response")?;
|
.context("Failed to send quote response")?;
|
||||||
|
|
||||||
// 3. Start setup execution
|
// 3. Start setup execution
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
monero,
|
monero,
|
||||||
network::request_response::{CborCodec, Swap, TIMEOUT},
|
network::request_response::{CborCodec, Swap, TIMEOUT},
|
||||||
protocol::bob::SwapRequest,
|
protocol::bob::QuoteRequest,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
@ -18,8 +18,8 @@ use tracing::debug;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
MsgReceived {
|
MsgReceived {
|
||||||
msg: SwapRequest,
|
msg: QuoteRequest,
|
||||||
channel: ResponseChannel<SwapResponse>,
|
channel: ResponseChannel<QuoteResponse>,
|
||||||
bob_peer_id: PeerId,
|
bob_peer_id: PeerId,
|
||||||
},
|
},
|
||||||
ResponseSent,
|
ResponseSent,
|
||||||
@ -27,12 +27,12 @@ pub enum OutEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct SwapResponse {
|
pub struct QuoteResponse {
|
||||||
pub xmr_amount: monero::Amount,
|
pub xmr_amount: monero::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
impl From<RequestResponseEvent<QuoteRequest, QuoteResponse>> for OutEvent {
|
||||||
fn from(event: RequestResponseEvent<SwapRequest, SwapResponse>) -> Self {
|
fn from(event: RequestResponseEvent<QuoteRequest, QuoteResponse>) -> Self {
|
||||||
match event {
|
match event {
|
||||||
RequestResponseEvent::Message {
|
RequestResponseEvent::Message {
|
||||||
peer,
|
peer,
|
||||||
@ -42,7 +42,7 @@ impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
|||||||
},
|
},
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
debug!("Received swap request from {}", peer);
|
debug!("Received quote request from {}", peer);
|
||||||
OutEvent::MsgReceived {
|
OutEvent::MsgReceived {
|
||||||
msg: request,
|
msg: request,
|
||||||
channel,
|
channel,
|
||||||
@ -70,19 +70,19 @@ impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
|||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
rr: RequestResponse<CborCodec<Swap, SwapRequest, SwapResponse>>,
|
rr: RequestResponse<CborCodec<Swap, QuoteRequest, QuoteResponse>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
/// Alice always sends her messages as a response to a request from Bob.
|
/// Alice always sends her messages as a response to a request from Bob.
|
||||||
pub fn send(
|
pub fn send(
|
||||||
&mut self,
|
&mut self,
|
||||||
channel: ResponseChannel<SwapResponse>,
|
channel: ResponseChannel<QuoteResponse>,
|
||||||
msg: SwapResponse,
|
msg: QuoteResponse,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.rr
|
self.rr
|
||||||
.send_response(channel, msg)
|
.send_response(channel, msg)
|
||||||
.map_err(|_| anyhow!("Sending swap response failed"))
|
.map_err(|_| anyhow!("Sending quote response failed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -23,10 +23,10 @@ pub use self::{
|
|||||||
cancel::cancel,
|
cancel::cancel,
|
||||||
encrypted_signature::EncryptedSignature,
|
encrypted_signature::EncryptedSignature,
|
||||||
event_loop::{EventLoop, EventLoopHandle},
|
event_loop::{EventLoop, EventLoopHandle},
|
||||||
|
quote_request::*,
|
||||||
refund::refund,
|
refund::refund,
|
||||||
state::*,
|
state::*,
|
||||||
swap::{run, run_until},
|
swap::{run, run_until},
|
||||||
swap_request::*,
|
|
||||||
};
|
};
|
||||||
pub use execution_setup::{Message0, Message2, Message4};
|
pub use execution_setup::{Message0, Message2, Message4};
|
||||||
use libp2p::request_response::ResponseChannel;
|
use libp2p::request_response::ResponseChannel;
|
||||||
@ -35,10 +35,10 @@ pub mod cancel;
|
|||||||
mod encrypted_signature;
|
mod encrypted_signature;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution_setup;
|
mod execution_setup;
|
||||||
|
mod quote_request;
|
||||||
pub mod refund;
|
pub mod refund;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
mod swap_request;
|
|
||||||
mod transfer_proof;
|
mod transfer_proof;
|
||||||
|
|
||||||
pub struct Swap {
|
pub struct Swap {
|
||||||
@ -209,7 +209,7 @@ impl Builder {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
ConnectionEstablished(PeerId),
|
ConnectionEstablished(PeerId),
|
||||||
SwapResponse(alice::SwapResponse),
|
QuoteResponse(alice::QuoteResponse),
|
||||||
ExecutionSetupDone(Result<Box<State2>>),
|
ExecutionSetupDone(Result<Box<State2>>),
|
||||||
TransferProof {
|
TransferProof {
|
||||||
msg: Box<TransferProof>,
|
msg: Box<TransferProof>,
|
||||||
@ -230,12 +230,12 @@ impl From<peer_tracker::OutEvent> for OutEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<swap_request::OutEvent> for OutEvent {
|
impl From<quote_request::OutEvent> for OutEvent {
|
||||||
fn from(event: swap_request::OutEvent) -> Self {
|
fn from(event: quote_request::OutEvent) -> Self {
|
||||||
use swap_request::OutEvent::*;
|
use quote_request::OutEvent::*;
|
||||||
match event {
|
match event {
|
||||||
MsgReceived(swap_response) => OutEvent::SwapResponse(swap_response),
|
MsgReceived(quote_response) => OutEvent::QuoteResponse(quote_response),
|
||||||
Failure(err) => OutEvent::Failure(err.context("Failre with Swap Request")),
|
Failure(err) => OutEvent::Failure(err.context("Failure with Quote Request")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -278,17 +278,17 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
|
|||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
pt: PeerTracker,
|
pt: PeerTracker,
|
||||||
swap_request: swap_request::Behaviour,
|
quote_request: quote_request::Behaviour,
|
||||||
execution_setup: execution_setup::Behaviour,
|
execution_setup: execution_setup::Behaviour,
|
||||||
transfer_proof: transfer_proof::Behaviour,
|
transfer_proof: transfer_proof::Behaviour,
|
||||||
encrypted_signature: encrypted_signature::Behaviour,
|
encrypted_signature: encrypted_signature::Behaviour,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
/// Sends a swap request to Alice to negotiate the swap.
|
/// Sends a quote request to Alice to retrieve the rate.
|
||||||
pub fn send_swap_request(&mut self, alice: PeerId, swap_request: SwapRequest) {
|
pub fn send_quote_request(&mut self, alice: PeerId, quote_request: QuoteRequest) {
|
||||||
let _id = self.swap_request.send(alice, swap_request);
|
let _id = self.quote_request.send(alice, quote_request);
|
||||||
info!("Requesting swap from: {}", alice);
|
info!("Requesting quote from: {}", alice);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_execution_setup(
|
pub fn start_execution_setup(
|
||||||
|
@ -3,8 +3,8 @@ use crate::{
|
|||||||
bitcoin::EncryptedSignature,
|
bitcoin::EncryptedSignature,
|
||||||
network::{transport::SwapTransport, TokioExecutor},
|
network::{transport::SwapTransport, TokioExecutor},
|
||||||
protocol::{
|
protocol::{
|
||||||
alice::{SwapResponse, TransferProof},
|
alice::{QuoteResponse, TransferProof},
|
||||||
bob::{Behaviour, OutEvent, State0, State2, SwapRequest},
|
bob::{Behaviour, OutEvent, QuoteRequest, State0, State2},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
@ -35,22 +35,22 @@ impl<T> Default for Channels<T> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EventLoopHandle {
|
pub struct EventLoopHandle {
|
||||||
recv_swap_response: Receiver<SwapResponse>,
|
recv_quote_response: Receiver<QuoteResponse>,
|
||||||
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<()>,
|
||||||
send_swap_request: Sender<SwapRequest>,
|
send_quote_request: Sender<QuoteRequest>,
|
||||||
send_encrypted_signature: Sender<EncryptedSignature>,
|
send_encrypted_signature: Sender<EncryptedSignature>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoopHandle {
|
impl EventLoopHandle {
|
||||||
pub async fn recv_swap_response(&mut self) -> Result<SwapResponse> {
|
pub async fn recv_quote_response(&mut self) -> Result<QuoteResponse> {
|
||||||
self.recv_swap_response
|
self.recv_quote_response
|
||||||
.recv()
|
.recv()
|
||||||
.await
|
.await
|
||||||
.ok_or_else(|| anyhow!("Failed to receive swap response from Alice"))
|
.ok_or_else(|| anyhow!("Failed to receive quote response from Alice"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn execution_setup(&mut self, state0: State0) -> Result<State2> {
|
pub async fn execution_setup(&mut self, state0: State0) -> Result<State2> {
|
||||||
@ -83,8 +83,8 @@ impl EventLoopHandle {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_swap_request(&mut self, swap_request: SwapRequest) -> Result<()> {
|
pub async fn send_quote_request(&mut self, quote_request: QuoteRequest) -> Result<()> {
|
||||||
let _ = self.send_swap_request.send(swap_request).await?;
|
let _ = self.send_quote_request.send(quote_request).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,13 +103,13 @@ 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,
|
||||||
recv_swap_response: Sender<SwapResponse>,
|
recv_quote_response: Sender<QuoteResponse>,
|
||||||
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_swap_request: Receiver<SwapRequest>,
|
send_quote_request: Receiver<QuoteRequest>,
|
||||||
send_encrypted_signature: Receiver<EncryptedSignature>,
|
send_encrypted_signature: Receiver<EncryptedSignature>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,37 +130,37 @@ impl EventLoop {
|
|||||||
|
|
||||||
swarm.add_address(alice_peer_id, alice_addr);
|
swarm.add_address(alice_peer_id, alice_addr);
|
||||||
|
|
||||||
let swap_response = Channels::new();
|
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_swap_request = Channels::new();
|
let send_quote_request = Channels::new();
|
||||||
let send_encrypted_signature = Channels::new();
|
let send_encrypted_signature = Channels::new();
|
||||||
|
|
||||||
let event_loop = EventLoop {
|
let event_loop = EventLoop {
|
||||||
swarm,
|
swarm,
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
recv_swap_response: swap_response.sender,
|
recv_quote_response: 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,
|
||||||
send_swap_request: send_swap_request.receiver,
|
send_quote_request: send_quote_request.receiver,
|
||||||
send_encrypted_signature: send_encrypted_signature.receiver,
|
send_encrypted_signature: send_encrypted_signature.receiver,
|
||||||
};
|
};
|
||||||
|
|
||||||
let handle = EventLoopHandle {
|
let handle = EventLoopHandle {
|
||||||
recv_swap_response: swap_response.receiver,
|
recv_quote_response: 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,
|
||||||
send_swap_request: send_swap_request.sender,
|
send_quote_request: send_quote_request.sender,
|
||||||
send_encrypted_signature: send_encrypted_signature.sender,
|
send_encrypted_signature: send_encrypted_signature.sender,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -175,8 +175,8 @@ impl EventLoop {
|
|||||||
OutEvent::ConnectionEstablished(peer_id) => {
|
OutEvent::ConnectionEstablished(peer_id) => {
|
||||||
let _ = self.conn_established.send(peer_id).await;
|
let _ = self.conn_established.send(peer_id).await;
|
||||||
}
|
}
|
||||||
OutEvent::SwapResponse(msg) => {
|
OutEvent::QuoteResponse(msg) => {
|
||||||
let _ = self.recv_swap_response.send(msg).await;
|
let _ = self.recv_quote_response.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;
|
||||||
@ -213,9 +213,9 @@ impl EventLoop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
swap_request = self.send_swap_request.recv().fuse() => {
|
quote_request = self.send_quote_request.recv().fuse() => {
|
||||||
if let Some(swap_request) = swap_request {
|
if let Some(quote_request) = quote_request {
|
||||||
self.swarm.send_swap_request(self.alice_peer_id, swap_request);
|
self.swarm.send_quote_request(self.alice_peer_id, quote_request);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
option = self.start_execution_setup.recv().fuse() => {
|
option = self.start_execution_setup.recv().fuse() => {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
network::request_response::{CborCodec, Swap, TIMEOUT},
|
network::request_response::{CborCodec, Swap, TIMEOUT},
|
||||||
protocol::alice::SwapResponse,
|
protocol::alice::QuoteResponse,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
@ -15,14 +15,14 @@ use std::time::Duration;
|
|||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||||
pub struct SwapRequest {
|
pub struct QuoteRequest {
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
pub btc_amount: bitcoin::Amount,
|
pub btc_amount: bitcoin::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
MsgReceived(SwapResponse),
|
MsgReceived(QuoteResponse),
|
||||||
Failure(Error),
|
Failure(Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,12 +31,12 @@ pub enum OutEvent {
|
|||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Behaviour {
|
pub struct Behaviour {
|
||||||
rr: RequestResponse<CborCodec<Swap, SwapRequest, SwapResponse>>,
|
rr: RequestResponse<CborCodec<Swap, QuoteRequest, QuoteResponse>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
pub fn send(&mut self, alice: PeerId, swap_request: SwapRequest) -> Result<RequestId> {
|
pub fn send(&mut self, alice: PeerId, quote_request: QuoteRequest) -> Result<RequestId> {
|
||||||
let id = self.rr.send_request(&alice, swap_request);
|
let id = self.rr.send_request(&alice, quote_request);
|
||||||
|
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
@ -59,8 +59,8 @@ impl Default for Behaviour {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
impl From<RequestResponseEvent<QuoteRequest, QuoteResponse>> for OutEvent {
|
||||||
fn from(event: RequestResponseEvent<SwapRequest, SwapResponse>) -> Self {
|
fn from(event: RequestResponseEvent<QuoteRequest, QuoteResponse>) -> Self {
|
||||||
match event {
|
match event {
|
||||||
RequestResponseEvent::Message {
|
RequestResponseEvent::Message {
|
||||||
message: RequestResponseMessage::Request { .. },
|
message: RequestResponseMessage::Request { .. },
|
||||||
@ -71,7 +71,7 @@ impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
|||||||
message: RequestResponseMessage::Response { response, .. },
|
message: RequestResponseMessage::Response { response, .. },
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
debug!("Received swap response from {}", peer);
|
debug!("Received quote response from {}", peer);
|
||||||
OutEvent::MsgReceived(response)
|
OutEvent::MsgReceived(response)
|
||||||
}
|
}
|
||||||
RequestResponseEvent::InboundFailure { error, .. } => {
|
RequestResponseEvent::InboundFailure { error, .. } => {
|
||||||
@ -81,7 +81,7 @@ impl From<RequestResponseEvent<SwapRequest, SwapResponse>> for OutEvent {
|
|||||||
OutEvent::Failure(anyhow!("Outbound failure: {:?}", error))
|
OutEvent::Failure(anyhow!("Outbound failure: {:?}", error))
|
||||||
}
|
}
|
||||||
RequestResponseEvent::ResponseSent { .. } => {
|
RequestResponseEvent::ResponseSent { .. } => {
|
||||||
OutEvent::Failure(anyhow!("Bob does not send a swap response to Alice"))
|
OutEvent::Failure(anyhow!("Bob does not send a quote response to Alice"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,7 +5,7 @@ use crate::{
|
|||||||
execution_params::ExecutionParams,
|
execution_params::ExecutionParams,
|
||||||
monero,
|
monero,
|
||||||
protocol::{
|
protocol::{
|
||||||
bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
|
bob::{self, event_loop::EventLoopHandle, state::*, QuoteRequest},
|
||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -375,14 +375,14 @@ pub async fn negotiate(
|
|||||||
) -> Result<bob::state::State2> {
|
) -> Result<bob::state::State2> {
|
||||||
tracing::trace!("Starting negotiate");
|
tracing::trace!("Starting negotiate");
|
||||||
event_loop_handle
|
event_loop_handle
|
||||||
.send_swap_request(SwapRequest {
|
.send_quote_request(QuoteRequest {
|
||||||
btc_amount: amounts.btc,
|
btc_amount: amounts.btc,
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// TODO: Use this once Bob's CLI is modified to only pass xmr amount in
|
// TODO: Use this once Bob's CLI is modified to only pass xmr amount in
|
||||||
// argument.
|
// argument.
|
||||||
let _swap_response = event_loop_handle.recv_swap_response().await?;
|
let _quote_response = event_loop_handle.recv_quote_response().await?;
|
||||||
|
|
||||||
let state2 = event_loop_handle.execution_setup(state0).await?;
|
let state2 = event_loop_handle.execution_setup(state0).await?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user