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-01-27 03:14:20 +00:00
|
|
|
alice::{SwapResponse, TransferProof},
|
2021-02-04 00:39:15 +00:00
|
|
|
bob::{Behaviour, OutEvent, State0, State2, SwapRequest},
|
2021-01-14 02:23:03 +00:00
|
|
|
},
|
|
|
|
};
|
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-02-03 05:45:43 +00:00
|
|
|
use std::sync::Arc;
|
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>,
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: Sender<SwapRequest>,
|
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-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"))
|
|
|
|
}
|
|
|
|
|
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<()> {
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
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-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: Sender<SwapResponse>,
|
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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: Receiver<SwapRequest>,
|
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-01-14 02:23:03 +00:00
|
|
|
let swap_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-01-14 02:23:03 +00:00
|
|
|
let send_swap_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-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: swap_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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: send_swap_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-01-27 03:25:45 +00:00
|
|
|
recv_swap_response: swap_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-01-14 02:23:03 +00:00
|
|
|
send_swap_request: send_swap_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
|
|
|
}
|
|
|
|
|
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
|
|
|
},
|
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 => {}
|
|
|
|
OutEvent::Failure(err) => {
|
|
|
|
error!("Communication error: {:#}", err)
|
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
}
|