2021-01-14 02:23:03 +00:00
|
|
|
use crate::{
|
|
|
|
bitcoin::EncryptedSignature,
|
|
|
|
network::{transport::SwapTransport, TokioExecutor},
|
|
|
|
protocol::{
|
|
|
|
alice,
|
2021-01-22 03:21:27 +00:00
|
|
|
alice::{Message4, SwapResponse},
|
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};
|
2020-12-09 07:08:26 +00:00
|
|
|
use tokio::{
|
|
|
|
stream::StreamExt,
|
|
|
|
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-14 02:23:03 +00:00
|
|
|
swap_response: Receiver<SwapResponse>,
|
2020-12-10 02:46:48 +00:00
|
|
|
msg0: Receiver<alice::Message0>,
|
|
|
|
msg1: Receiver<alice::Message1>,
|
2021-01-22 03:21:27 +00:00
|
|
|
msg4: Receiver<Message4>,
|
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>,
|
2020-12-22 02:47:05 +00:00
|
|
|
send_msg0: Sender<bob::Message0>,
|
|
|
|
send_msg1: Sender<bob::Message1>,
|
|
|
|
send_msg2: Sender<bob::Message2>,
|
|
|
|
send_msg3: Sender<EncryptedSignature>,
|
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> {
|
|
|
|
self.swap_response
|
|
|
|
.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> {
|
|
|
|
self.msg0
|
|
|
|
.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> {
|
|
|
|
self.msg1
|
|
|
|
.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-22 03:21:27 +00:00
|
|
|
pub async fn recv_message4(&mut self) -> Result<Message4> {
|
|
|
|
self.msg4
|
2020-12-09 03:10:24 +00:00
|
|
|
.recv()
|
|
|
|
.await
|
2021-01-22 03:21:27 +00:00
|
|
|
.ok_or_else(|| anyhow!("Failed to receive message 4 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<()> {
|
|
|
|
let _ = self.send_msg0.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<()> {
|
|
|
|
let _ = self.send_msg1.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<()> {
|
|
|
|
let _ = self.send_msg2.send(msg).await?;
|
2020-12-09 03:10:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
pub async fn send_message3(&mut self, tx_redeem_encsig: EncryptedSignature) -> Result<()> {
|
|
|
|
let _ = self.send_msg3.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>,
|
2020-12-22 02:47:05 +00:00
|
|
|
alice_peer_id: PeerId,
|
2021-01-14 02:23:03 +00:00
|
|
|
swap_response: Sender<SwapResponse>,
|
2020-12-10 02:46:48 +00:00
|
|
|
msg0: Sender<alice::Message0>,
|
|
|
|
msg1: Sender<alice::Message1>,
|
2021-01-22 03:21:27 +00:00
|
|
|
msg4: Sender<Message4>,
|
2020-12-10 02:46:48 +00:00
|
|
|
conn_established: Sender<PeerId>,
|
2020-12-22 02:47:05 +00:00
|
|
|
dial_alice: Receiver<()>,
|
2021-01-14 02:23:03 +00:00
|
|
|
send_swap_request: Receiver<SwapRequest>,
|
2020-12-22 02:47:05 +00:00
|
|
|
send_msg0: Receiver<bob::Message0>,
|
|
|
|
send_msg1: Receiver<bob::Message1>,
|
|
|
|
send_msg2: Receiver<bob::Message2>,
|
|
|
|
send_msg3: 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,
|
|
|
|
) -> 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();
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
swarm.add_address(alice_peer_id.clone(), alice_addr);
|
|
|
|
|
2021-01-14 02:23:03 +00:00
|
|
|
let swap_response = Channels::new();
|
2020-12-09 03:10:24 +00:00
|
|
|
let msg0 = Channels::new();
|
|
|
|
let msg1 = Channels::new();
|
2021-01-22 03:21:27 +00:00
|
|
|
let msg4 = Channels::new();
|
2020-12-09 03:10:24 +00:00
|
|
|
let conn_established = Channels::new();
|
|
|
|
let dial_alice = Channels::new();
|
2021-01-14 02:23:03 +00:00
|
|
|
let send_swap_request = Channels::new();
|
2020-12-09 03:10:24 +00:00
|
|
|
let send_msg0 = Channels::new();
|
|
|
|
let send_msg1 = Channels::new();
|
|
|
|
let send_msg2 = Channels::new();
|
|
|
|
let send_msg3 = Channels::new();
|
|
|
|
|
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-14 02:23:03 +00:00
|
|
|
swap_response: swap_response.sender,
|
2020-12-09 03:10:24 +00:00
|
|
|
msg0: msg0.sender,
|
|
|
|
msg1: msg1.sender,
|
2021-01-22 03:21:27 +00:00
|
|
|
msg4: msg4.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,
|
2020-12-09 03:10:24 +00:00
|
|
|
send_msg0: send_msg0.receiver,
|
|
|
|
send_msg1: send_msg1.receiver,
|
|
|
|
send_msg2: send_msg2.receiver,
|
|
|
|
send_msg3: send_msg3.receiver,
|
|
|
|
};
|
|
|
|
|
2020-12-10 02:19:18 +00:00
|
|
|
let handle = EventLoopHandle {
|
2021-01-14 02:23:03 +00:00
|
|
|
swap_response: swap_response.receiver,
|
2020-12-09 03:10:24 +00:00
|
|
|
msg0: msg0.receiver,
|
|
|
|
msg1: msg1.receiver,
|
2021-01-22 03:21:27 +00:00
|
|
|
msg4: msg4.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,
|
2020-12-09 03:10:24 +00:00
|
|
|
send_msg0: send_msg0.sender,
|
|
|
|
send_msg1: send_msg1.sender,
|
|
|
|
send_msg2: send_msg2.sender,
|
|
|
|
send_msg3: send_msg3.sender,
|
|
|
|
};
|
|
|
|
|
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) => {
|
|
|
|
let _ = self.swap_response.send(msg).await;
|
|
|
|
},
|
2020-12-09 07:08:26 +00:00
|
|
|
OutEvent::Message0(msg) => {
|
2020-12-23 04:06:43 +00:00
|
|
|
let _ = self.msg0.send(*msg).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
OutEvent::Message1(msg) => {
|
2020-12-23 04:06:43 +00:00
|
|
|
let _ = self.msg1.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"),
|
|
|
|
OutEvent::Message4(msg) => {
|
|
|
|
let _ = self.msg4.send(*msg).await;
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2021-01-21 23:12:36 +00:00
|
|
|
OutEvent::Message5 => info!("Alice acknowledged message 5 received"),
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
2020-12-22 02:50:28 +00:00
|
|
|
option = self.dial_alice.next().fuse() => {
|
|
|
|
if option.is_some() {
|
2020-12-22 02:47:05 +00:00
|
|
|
let peer_id = self.alice_peer_id.clone();
|
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-14 02:23:03 +00:00
|
|
|
swap_request = self.send_swap_request.next().fuse() => {
|
|
|
|
if let Some(swap_request) = swap_request {
|
|
|
|
self.swarm.send_swap_request(self.alice_peer_id.clone(), swap_request);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
msg0 = self.send_msg0.next().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg0 {
|
|
|
|
self.swarm.send_message0(self.alice_peer_id.clone(), msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
2020-12-07 02:31:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:08:26 +00:00
|
|
|
msg1 = self.send_msg1.next().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg1 {
|
|
|
|
self.swarm.send_message1(self.alice_peer_id.clone(), msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
msg2 = self.send_msg2.next().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(msg) = msg2 {
|
|
|
|
self.swarm.send_message2(self.alice_peer_id.clone(), msg);
|
2020-12-09 07:08:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
msg3 = self.send_msg3.next().fuse() => {
|
2020-12-22 02:47:05 +00:00
|
|
|
if let Some(tx_redeem_encsig) = msg3 {
|
|
|
|
self.swarm.send_message3(self.alice_peer_id.clone(), 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
|
|
|
}
|
|
|
|
}
|