2021-01-14 02:23:03 +00:00
|
|
|
use crate::{
|
|
|
|
bitcoin::EncryptedSignature,
|
|
|
|
network::{transport::SwapTransport, TokioExecutor},
|
|
|
|
protocol::{
|
|
|
|
alice,
|
2021-01-27 03:14:20 +00:00
|
|
|
alice::{SwapResponse, TransferProof},
|
2021-01-14 02:23:03 +00:00
|
|
|
bob::{self, Behaviour, OutEvent, SwapRequest},
|
|
|
|
},
|
|
|
|
};
|
2020-12-10 02:49:38 +00:00
|
|
|
use anyhow::{anyhow, 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-01-15 05:58:16 +00:00
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2020-12-18 06:39:04 +00:00
|
|
|
use tracing::{debug, error, info};
|
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-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: Receiver<SwapResponse>,
|
|
|
|
recv_message0: Receiver<alice::Message0>,
|
|
|
|
recv_message1: Receiver<alice::Message1>,
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: Sender<SwapRequest>,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_message0: Sender<bob::Message0>,
|
|
|
|
send_message1: Sender<bob::Message1>,
|
|
|
|
send_message2: Sender<bob::Message2>,
|
|
|
|
send_encrypted_signature: Sender<EncryptedSignature>,
|
2021-01-29 04:13:40 +00:00
|
|
|
recv_encrypted_signature_ack: Receiver<()>,
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
impl EventLoopHandle {
|
2021-01-14 02:23:03 +00:00
|
|
|
pub async fn recv_swap_response(&mut self) -> Result<SwapResponse> {
|
2021-01-27 03:25:45 +00:00
|
|
|
self.recv_swap_response
|
2021-01-14 02:23:03 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("Failed to receive swap response from Alice"))
|
|
|
|
}
|
|
|
|
|
2020-12-09 03:10:24 +00:00
|
|
|
pub async fn recv_message0(&mut self) -> Result<alice::Message0> {
|
2021-01-27 03:25:45 +00:00
|
|
|
self.recv_message0
|
2020-12-09 03:10:24 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
2021-01-14 02:23:03 +00:00
|
|
|
.ok_or_else(|| anyhow!("Failed to receive message 0 from Alice"))
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recv_message1(&mut self) -> Result<alice::Message1> {
|
2021-01-27 03:25:45 +00:00
|
|
|
self.recv_message1
|
2020-12-09 03:10:24 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
2021-01-14 02:23:03 +00:00
|
|
|
.ok_or_else(|| anyhow!("Failed to receive message 1 from Alice"))
|
2020-12-09 03:10:24 +00:00
|
|
|
}
|
|
|
|
|
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<()> {
|
|
|
|
debug!("Attempt to dial Alice");
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
pub async fn send_swap_request(&mut self, swap_request: SwapRequest) -> Result<()> {
|
|
|
|
let _ = self.send_swap_request.send(swap_request).await?;
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
pub async fn send_message0(&mut self, msg: bob::Message0) -> Result<()> {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.send_message0.send(msg).await?;
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
pub async fn send_message1(&mut self, msg: bob::Message1) -> Result<()> {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.send_message1.send(msg).await?;
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
pub async fn send_message2(&mut self, msg: bob::Message2) -> Result<()> {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.send_message2.send(msg).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?;
|
|
|
|
|
|
|
|
self.recv_encrypted_signature_ack
|
|
|
|
.recv()
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("Failed to receive encrypted signature ack from Alice"))?;
|
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>,
|
2020-12-22 02:47:05 +00:00
|
|
|
alice_peer_id: PeerId,
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: Sender<SwapResponse>,
|
|
|
|
recv_message0: Sender<alice::Message0>,
|
|
|
|
recv_message1: Sender<alice::Message1>,
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: Receiver<SwapRequest>,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_message0: Receiver<bob::Message0>,
|
|
|
|
send_message1: Receiver<bob::Message1>,
|
|
|
|
send_message2: Receiver<bob::Message2>,
|
|
|
|
send_encrypted_signature: Receiver<EncryptedSignature>,
|
2021-01-29 04:13:40 +00:00
|
|
|
recv_encrypted_signature_ack: Sender<()>,
|
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,
|
|
|
|
) -> 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-01-14 02:23:03 +00:00
|
|
|
let swap_response = Channels::new();
|
2021-01-27 03:25:45 +00:00
|
|
|
let recv_message0 = Channels::new();
|
|
|
|
let recv_message1 = Channels::new();
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
let send_swap_request = Channels::new();
|
2021-01-27 03:25:45 +00:00
|
|
|
let send_message0 = Channels::new();
|
|
|
|
let send_message1 = Channels::new();
|
|
|
|
let send_message2 = Channels::new();
|
|
|
|
let send_encrypted_signature = Channels::new();
|
2021-01-29 04:13:40 +00:00
|
|
|
let recv_encrypted_signature_ack = 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-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: swap_response.sender,
|
|
|
|
recv_message0: recv_message0.sender,
|
|
|
|
recv_message1: recv_message1.sender,
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: send_swap_request.receiver,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_message0: send_message0.receiver,
|
|
|
|
send_message1: send_message1.receiver,
|
|
|
|
send_message2: send_message2.receiver,
|
|
|
|
send_encrypted_signature: send_encrypted_signature.receiver,
|
2021-01-29 04:13:40 +00:00
|
|
|
recv_encrypted_signature_ack: recv_encrypted_signature_ack.sender,
|
2020-12-09 03:10:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
let handle = EventLoopHandle {
|
2021-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: swap_response.receiver,
|
|
|
|
recv_message0: recv_message0.receiver,
|
|
|
|
recv_message1: recv_message1.receiver,
|
|
|
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: send_swap_request.sender,
|
2021-01-27 03:25:45 +00:00
|
|
|
send_message0: send_message0.sender,
|
|
|
|
send_message1: send_message1.sender,
|
|
|
|
send_message2: send_message2.sender,
|
|
|
|
send_encrypted_signature: send_encrypted_signature.sender,
|
2021-01-29 04:13:40 +00:00
|
|
|
recv_encrypted_signature_ack: recv_encrypted_signature_ack.receiver,
|
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
|
|
|
}
|
|
|
|
|
2020-12-09 03:10:24 +00:00
|
|
|
pub async fn run(mut self) {
|
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-01-14 02:23:03 +00:00
|
|
|
OutEvent::SwapResponse(msg) => {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.recv_swap_response.send(msg).await;
|
2021-01-14 02:23:03 +00:00
|
|
|
},
|
2020-12-09 07:08:26 +00:00
|
|
|
OutEvent::Message0(msg) => {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.recv_message0.send(*msg).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
OutEvent::Message1(msg) => {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.recv_message1.send(*msg).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2021-01-22 03:21:27 +00:00
|
|
|
OutEvent::Message2 => info!("Alice acknowledged message 2 received"),
|
2021-01-27 03:14:20 +00:00
|
|
|
OutEvent::TransferProof(msg) => {
|
2021-01-27 03:25:45 +00:00
|
|
|
let _ = self.recv_transfer_proof.send(*msg).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2021-01-29 04:13:40 +00:00
|
|
|
OutEvent::EncryptedSignatureAcknowledged => {
|
|
|
|
debug!("Alice acknowledged encrypted signature");
|
|
|
|
let _ = self.recv_encrypted_signature_ack.send(()).await;
|
|
|
|
}
|
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) {
|
|
|
|
debug!("Already connected to Alice: {}", peer_id);
|
|
|
|
let _ = self.conn_established.send(peer_id).await;
|
|
|
|
} else {
|
|
|
|
info!("dialing alice: {}", peer_id);
|
|
|
|
if let Err(err) = libp2p::Swarm::dial(&mut self.swarm, &peer_id) {
|
|
|
|
error!("Could not dial alice: {}", err);
|
|
|
|
// TODO(Franck): If Dial fails then we should report it.
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-15 05:58:16 +00:00
|
|
|
swap_request = self.send_swap_request.recv().fuse() => {
|
2021-01-14 02:23:03 +00:00
|
|
|
if let Some(swap_request) = swap_request {
|
2021-01-15 05:58:16 +00:00
|
|
|
self.swarm.send_swap_request(self.alice_peer_id, swap_request);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-15 05:58:16 +00:00
|
|
|
msg0 = self.send_message0.recv().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg0 {
|
2021-01-15 05:58:16 +00:00
|
|
|
self.swarm.send_message0(self.alice_peer_id, msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2020-12-07 02:31:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 05:58:16 +00:00
|
|
|
msg1 = self.send_message1.recv().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg1 {
|
2021-01-15 05:58:16 +00:00
|
|
|
self.swarm.send_message1(self.alice_peer_id, msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-15 05:58:16 +00:00
|
|
|
msg2 = self.send_message2.recv().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg2 {
|
2021-01-15 05:58:16 +00:00
|
|
|
self.swarm.send_message2(self.alice_peer_id, msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
}
|