2021-01-14 02:23:03 +00:00
|
|
|
use crate::{
|
2021-02-03 05:45:43 +00:00
|
|
|
bitcoin,
|
2021-01-14 02:23:03 +00:00
|
|
|
bitcoin::EncryptedSignature,
|
|
|
|
network::{transport::SwapTransport, TokioExecutor},
|
|
|
|
protocol::{
|
2021-02-12 06:05:06 +00:00
|
|
|
alice::{QuoteResponse, TransferProof},
|
|
|
|
bob::{Behaviour, OutEvent, QuoteRequest, State0, State2},
|
2021-01-14 02:23:03 +00:00
|
|
|
},
|
|
|
|
};
|
2021-02-26 06:05:19 +00:00
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
2020-12-09 07:08:26 +00:00
|
|
|
use futures::FutureExt;
|
2020-12-07 02:31:14 +00:00
|
|
|
use libp2p::{core::Multiaddr, PeerId};
|
2021-02-26 05:11:14 +00:00
|
|
|
use std::{convert::Infallible, sync::Arc};
|
2021-01-15 05:58:16 +00:00
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2021-02-26 06:05:19 +00:00
|
|
|
use tracing::{debug, error, trace};
|
2021-01-05 03:08:36 +00:00
|
|
|
|
2020-12-23 03:33:29 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-07 02:31:14 +00:00
|
|
|
pub struct Channels<T> {
|
|
|
|
sender: Sender<T>,
|
|
|
|
receiver: Receiver<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Channels<T> {
|
|
|
|
pub fn new() -> Channels<T> {
|
|
|
|
let (sender, receiver) = tokio::sync::mpsc::channel(100);
|
|
|
|
Channels { sender, receiver }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for Channels<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:33:29 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-10 02:19:18 +00:00
|
|
|
pub struct EventLoopHandle {
|
2021-02-12 06:05:06 +00:00
|
|
|
recv_quote_response: Receiver<QuoteResponse>,
|
2021-02-03 05:45:43 +00:00
|
|
|
start_execution_setup: Sender<State0>,
|
|
|
|
done_execution_setup: Receiver<Result<State2>>,
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_transfer_proof: Receiver<TransferProof>,
|
2020-12-10 02:46:48 +00:00
|
|
|
conn_established: Receiver<PeerId>,
|
2020-12-22 02:47:05 +00:00
|
|
|
dial_alice: Sender<()>,
|
2021-02-12 06:05:06 +00:00
|
|
|
send_quote_request: Sender<QuoteRequest>,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_encrypted_signature: Sender<EncryptedSignature>,
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
impl EventLoopHandle {
|
2021-02-12 06:05:06 +00:00
|
|
|
pub async fn recv_quote_response(&mut self) -> Result<QuoteResponse> {
|
|
|
|
self.recv_quote_response
|
2021-01-14 02:23:03 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
2021-02-12 06:05:06 +00:00
|
|
|
.ok_or_else(|| anyhow!("Failed to receive quote response from Alice"))
|
2021-01-14 02:23:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 05:45:43 +00:00
|
|
|
pub async fn execution_setup(&mut self, state0: State0) -> Result<State2> {
|
|
|
|
let _ = self.start_execution_setup.send(state0).await?;
|
|
|
|
|
|
|
|
self.done_execution_setup
|
|
|
|
.recv()
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("Failed to setup execution with Alice"))?
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:14:20 +00:00
|
|
|
pub async fn recv_transfer_proof(&mut self) -> Result<TransferProof> {
|
2021-01-27 03:25:45 +00:00
|
|
|
self.recv_transfer_proof
|
2020-12-09 03:10:24 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
2021-01-27 03:14:20 +00:00
|
|
|
.ok_or_else(|| anyhow!("Failed to receive transfer proof from Alice"))
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 06:39:04 +00:00
|
|
|
/// Dials other party and wait for the connection to be established.
|
|
|
|
/// Do nothing if we are already connected
|
2020-12-22 02:47:05 +00:00
|
|
|
pub async fn dial(&mut self) -> Result<()> {
|
|
|
|
let _ = self.dial_alice.send(()).await?;
|
2020-12-18 06:39:04 +00:00
|
|
|
|
|
|
|
self.conn_established
|
|
|
|
.recv()
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("Failed to receive connection established from Alice"))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:05:06 +00:00
|
|
|
pub async fn send_quote_request(&mut self, quote_request: QuoteRequest) -> Result<()> {
|
|
|
|
let _ = self.send_quote_request.send(quote_request).await?;
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:25:45 +00:00
|
|
|
pub async fn send_encrypted_signature(
|
|
|
|
&mut self,
|
|
|
|
tx_redeem_encsig: EncryptedSignature,
|
|
|
|
) -> Result<()> {
|
2021-01-29 04:13:40 +00:00
|
|
|
self.send_encrypted_signature.send(tx_redeem_encsig).await?;
|
|
|
|
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:33:29 +00:00
|
|
|
#[allow(missing_debug_implementations)]
|
2020-12-10 02:19:18 +00:00
|
|
|
pub struct EventLoop {
|
2020-12-10 02:46:48 +00:00
|
|
|
swarm: libp2p::Swarm<Behaviour>,
|
2021-02-03 05:45:43 +00:00
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
2020-12-22 02:47:05 +00:00
|
|
|
alice_peer_id: PeerId,
|
2021-02-12 06:05:06 +00:00
|
|
|
recv_quote_response: Sender<QuoteResponse>,
|
2021-02-03 05:45:43 +00:00
|
|
|
start_execution_setup: Receiver<State0>,
|
|
|
|
done_execution_setup: Sender<Result<State2>>,
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_transfer_proof: Sender<TransferProof>,
|
2020-12-22 02:47:05 +00:00
|
|
|
dial_alice: Receiver<()>,
|
2021-01-27 03:25:45 +00:00
|
|
|
conn_established: Sender<PeerId>,
|
2021-02-12 06:05:06 +00:00
|
|
|
send_quote_request: Receiver<QuoteRequest>,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_encrypted_signature: Receiver<EncryptedSignature>,
|
2020-12-07 02:31:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
impl EventLoop {
|
2020-12-22 02:47:05 +00:00
|
|
|
pub fn new(
|
|
|
|
transport: SwapTransport,
|
|
|
|
behaviour: Behaviour,
|
2021-01-18 09:41:58 +00:00
|
|
|
peer_id: PeerId,
|
2020-12-22 02:47:05 +00:00
|
|
|
alice_peer_id: PeerId,
|
|
|
|
alice_addr: Multiaddr,
|
2021-02-03 05:45:43 +00:00
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
2020-12-22 02:47:05 +00:00
|
|
|
) -> Result<(Self, EventLoopHandle)> {
|
2021-01-18 09:41:58 +00:00
|
|
|
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
|
2020-12-07 02:31:14 +00:00
|
|
|
.executor(Box::new(TokioExecutor {
|
|
|
|
handle: tokio::runtime::Handle::current(),
|
|
|
|
}))
|
|
|
|
.build();
|
|
|
|
|
2021-01-15 05:58:16 +00:00
|
|
|
swarm.add_address(alice_peer_id, alice_addr);
|
2020-12-22 02:47:05 +00:00
|
|
|
|
2021-02-12 06:05:06 +00:00
|
|
|
let quote_response = Channels::new();
|
2021-02-03 05:45:43 +00:00
|
|
|
let start_execution_setup = Channels::new();
|
|
|
|
let done_execution_setup = Channels::new();
|
2021-01-27 03:25:45 +00:00
|
|
|
let recv_transfer_proof = Channels::new();
|
2020-12-09 03:10:24 +00:00
|
|
|
let dial_alice = Channels::new();
|
2021-01-27 03:25:45 +00:00
|
|
|
let conn_established = Channels::new();
|
2021-02-12 06:05:06 +00:00
|
|
|
let send_quote_request = Channels::new();
|
2021-01-27 03:25:45 +00:00
|
|
|
let send_encrypted_signature = Channels::new();
|
2020-12-09 03:10:24 +00:00
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
let event_loop = EventLoop {
|
2020-12-07 02:31:14 +00:00
|
|
|
swarm,
|
2020-12-22 02:47:05 +00:00
|
|
|
alice_peer_id,
|
2021-02-03 05:45:43 +00:00
|
|
|
bitcoin_wallet,
|
2021-02-12 06:05:06 +00:00
|
|
|
recv_quote_response: quote_response.sender,
|
2021-02-03 05:45:43 +00:00
|
|
|
start_execution_setup: start_execution_setup.receiver,
|
|
|
|
done_execution_setup: done_execution_setup.sender,
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_transfer_proof: recv_transfer_proof.sender,
|
2020-12-09 03:10:24 +00:00
|
|
|
conn_established: conn_established.sender,
|
|
|
|
dial_alice: dial_alice.receiver,
|
2021-02-12 06:05:06 +00:00
|
|
|
send_quote_request: send_quote_request.receiver,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_encrypted_signature: send_encrypted_signature.receiver,
|
2020-12-09 03:10:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
let handle = EventLoopHandle {
|
2021-02-12 06:05:06 +00:00
|
|
|
recv_quote_response: quote_response.receiver,
|
2021-02-03 05:45:43 +00:00
|
|
|
start_execution_setup: start_execution_setup.sender,
|
|
|
|
done_execution_setup: done_execution_setup.receiver,
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_transfer_proof: recv_transfer_proof.receiver,
|
2020-12-09 03:10:24 +00:00
|
|
|
conn_established: conn_established.receiver,
|
|
|
|
dial_alice: dial_alice.sender,
|
2021-02-12 06:05:06 +00:00
|
|
|
send_quote_request: send_quote_request.sender,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_encrypted_signature: send_encrypted_signature.sender,
|
2020-12-09 03:10:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
Ok((event_loop, handle))
|
2020-12-07 02:31:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
pub async fn run(mut self) -> Result<Infallible> {
|
2020-12-07 02:31:14 +00:00
|
|
|
loop {
|
2020-12-09 07:08:26 +00:00
|
|
|
tokio::select! {
|
|
|
|
swarm_event = self.swarm.next().fuse() => {
|
|
|
|
match swarm_event {
|
2020-12-18 06:39:04 +00:00
|
|
|
OutEvent::ConnectionEstablished(peer_id) => {
|
|
|
|
let _ = self.conn_established.send(peer_id).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2021-02-12 06:05:06 +00:00
|
|
|
OutEvent::QuoteResponse(msg) => {
|
|
|
|
let _ = self.recv_quote_response.send(msg).await;
|
2021-01-14 02:23:03 +00:00
|
|
|
},
|
2021-02-03 05:45:43 +00:00
|
|
|
OutEvent::ExecutionSetupDone(res) => {
|
|
|
|
let _ = self.done_execution_setup.send(res.map(|state|*state)).await;
|
|
|
|
}
|
2021-02-05 05:30:43 +00:00
|
|
|
OutEvent::TransferProof{ msg, channel }=> {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.recv_transfer_proof.send(*msg).await;
|
2021-02-05 05:30:43 +00:00
|
|
|
// Send back empty response so that the request/response protocol completes.
|
|
|
|
if let Err(error) = self.swarm.transfer_proof.send_ack(channel) {
|
|
|
|
error!("Failed to send Transfer Proof ack: {:?}", error);
|
|
|
|
}
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2021-01-29 04:13:40 +00:00
|
|
|
OutEvent::EncryptedSignatureAcknowledged => {
|
|
|
|
debug!("Alice acknowledged encrypted signature");
|
|
|
|
}
|
2021-02-05 05:30:43 +00:00
|
|
|
OutEvent::ResponseSent => {}
|
2021-02-26 05:02:44 +00:00
|
|
|
OutEvent::CommunicationError(err) => {
|
2021-02-26 05:11:14 +00:00
|
|
|
bail!("Communication error: {:#}", err)
|
2021-02-05 05:30:43 +00:00
|
|
|
}
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-15 05:58:16 +00:00
|
|
|
option = self.dial_alice.recv().fuse() => {
|
2020-12-22 02:50:28 +00:00
|
|
|
if option.is_some() {
|
2021-01-15 05:58:16 +00:00
|
|
|
let peer_id = self.alice_peer_id;
|
2020-12-18 06:39:04 +00:00
|
|
|
if self.swarm.pt.is_connected(&peer_id) {
|
2021-02-26 06:03:07 +00:00
|
|
|
trace!("Already connected to Alice at {}", peer_id);
|
2020-12-18 06:39:04 +00:00
|
|
|
let _ = self.conn_established.send(peer_id).await;
|
|
|
|
} else {
|
2021-02-26 06:05:19 +00:00
|
|
|
debug!("Dialing alice at {}", peer_id);
|
|
|
|
libp2p::Swarm::dial(&mut self.swarm, &peer_id).context("failed to dial alice")?;
|
2020-12-18 06:39:04 +00:00
|
|
|
}
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2021-02-12 06:05:06 +00:00
|
|
|
quote_request = self.send_quote_request.recv().fuse() => {
|
|
|
|
if let Some(quote_request) = quote_request {
|
|
|
|
self.swarm.send_quote_request(self.alice_peer_id, quote_request);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2021-02-03 05:45:43 +00:00
|
|
|
option = self.start_execution_setup.recv().fuse() => {
|
|
|
|
if let Some(state0) = option {
|
|
|
|
let _ = self
|
|
|
|
.swarm
|
|
|
|
.start_execution_setup(self.alice_peer_id, state0, self.bitcoin_wallet.clone());
|
|
|
|
}
|
|
|
|
},
|
2021-01-15 05:58:16 +00:00
|
|
|
encrypted_signature = self.send_encrypted_signature.recv().fuse() => {
|
2021-01-27 03:14:20 +00:00
|
|
|
if let Some(tx_redeem_encsig) = encrypted_signature {
|
2021-01-15 05:58:16 +00:00
|
|
|
self.swarm.send_encrypted_signature(self.alice_peer_id, tx_redeem_encsig);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 02:31:14 +00:00
|
|
|
}
|
|
|
|
}
|