238: Bob error handling r=thomaseizinger a=da-kami



Co-authored-by: Daniel Karzel <daniel@comit.network>
This commit is contained in:
bors[bot] 2021-03-01 01:31:24 +00:00 committed by GitHub
commit 5ddf41721e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 35 deletions

View File

@ -128,8 +128,16 @@ async fn main() -> Result<()> {
); );
let (swap, event_loop) = bob_factory.with_init_params(send_bitcoin).build().await?; let (swap, event_loop) = bob_factory.with_init_params(send_bitcoin).build().await?;
tokio::spawn(async move { event_loop.run().await }); let handle = tokio::spawn(async move { event_loop.run().await });
bob::run(swap).await?; let swap = bob::run(swap);
tokio::select! {
event_loop_result = handle => {
event_loop_result??;
},
swap_result = swap => {
swap_result?;
}
}
} }
Command::History => { Command::History => {
let mut table = Table::new(); let mut table = Table::new();
@ -169,9 +177,16 @@ async fn main() -> Result<()> {
execution_params, execution_params,
); );
let (swap, event_loop) = bob_factory.build().await?; let (swap, event_loop) = bob_factory.build().await?;
let handle = tokio::spawn(async move { event_loop.run().await });
tokio::spawn(async move { event_loop.run().await }); let swap = bob::run(swap);
bob::run(swap).await?; tokio::select! {
event_loop_result = handle => {
event_loop_result??;
},
swap_result = swap => {
swap_result?;
}
}
} }
Command::Cancel { Command::Cancel {
swap_id, swap_id,
@ -201,27 +216,33 @@ async fn main() -> Result<()> {
execution_params, execution_params,
); );
let (swap, event_loop) = bob_factory.build().await?; let (swap, event_loop) = bob_factory.build().await?;
let handle = tokio::spawn(async move { event_loop.run().await });
tokio::spawn(async move { event_loop.run().await }); let cancel = bob::cancel(
match bob::cancel(
swap.swap_id, swap.swap_id,
swap.state, swap.state,
swap.bitcoin_wallet, swap.bitcoin_wallet,
swap.db, swap.db,
force, force,
) );
.await?
{ tokio::select! {
Ok((txid, _)) => { event_loop_result = handle => {
info!("Cancel transaction successfully published with id {}", txid) event_loop_result??;
} },
Err(CancelError::CancelTimelockNotExpiredYet) => error!( cancel_result = cancel => {
"The Cancel Transaction cannot be published yet, \ match cancel_result? {
because the timelock has not expired. Please try again later." Ok((txid, _)) => {
), info!("Cancel transaction successfully published with id {}", txid)
Err(CancelError::CancelTxAlreadyPublished) => { }
warn!("The Cancel Transaction has already been published.") Err(CancelError::CancelTimelockNotExpiredYet) => error!(
"The Cancel Transaction cannot be published yet, \
because the timelock has not expired. Please try again later."
),
Err(CancelError::CancelTxAlreadyPublished) => {
warn!("The Cancel Transaction has already been published.")
}
}
} }
} }
} }
@ -254,19 +275,26 @@ async fn main() -> Result<()> {
); );
let (swap, event_loop) = bob_factory.build().await?; let (swap, event_loop) = bob_factory.build().await?;
tokio::spawn(async move { event_loop.run().await }); let handle = tokio::spawn(async move { event_loop.run().await });
bob::refund( let refund = bob::refund(
swap.swap_id, swap.swap_id,
swap.state, swap.state,
swap.execution_params, swap.execution_params,
swap.bitcoin_wallet, swap.bitcoin_wallet,
swap.db, swap.db,
force, force,
) );
.await??;
tokio::select! {
event_loop_result = handle => {
event_loop_result??;
},
refund_result = refund => {
refund_result??;
}
}
} }
}; };
Ok(()) Ok(())
} }

View File

@ -185,7 +185,7 @@ pub enum OutEvent {
}, },
EncryptedSignatureAcknowledged, EncryptedSignatureAcknowledged,
ResponseSent, // Same variant is used for all messages as no processing is done ResponseSent, // Same variant is used for all messages as no processing is done
Failure(Error), CommunicationError(Error),
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -203,7 +203,7 @@ impl From<quote_request::OutEvent> for OutEvent {
use quote_request::OutEvent::*; use quote_request::OutEvent::*;
match event { match event {
MsgReceived(quote_response) => OutEvent::QuoteResponse(quote_response), MsgReceived(quote_response) => OutEvent::QuoteResponse(quote_response),
Failure(err) => OutEvent::Failure(err.context("Failure with Quote Request")), Failure(err) => OutEvent::CommunicationError(err.context("Failure with Quote Request")),
} }
} }
} }
@ -225,7 +225,9 @@ impl From<transfer_proof::OutEvent> for OutEvent {
channel, channel,
}, },
AckSent => OutEvent::ResponseSent, AckSent => OutEvent::ResponseSent,
Failure(err) => OutEvent::Failure(err.context("Failure with Transfer Proof")), Failure(err) => {
OutEvent::CommunicationError(err.context("Failure with Transfer Proof"))
}
} }
} }
} }
@ -235,7 +237,9 @@ impl From<encrypted_signature::OutEvent> for OutEvent {
use encrypted_signature::OutEvent::*; use encrypted_signature::OutEvent::*;
match event { match event {
Acknowledged => OutEvent::EncryptedSignatureAcknowledged, Acknowledged => OutEvent::EncryptedSignatureAcknowledged,
Failure(err) => OutEvent::Failure(err.context("Failure with Encrypted Signature")), Failure(err) => {
OutEvent::CommunicationError(err.context("Failure with Encrypted Signature"))
}
} }
} }
} }

View File

@ -7,10 +7,10 @@ use crate::{
bob::{Behaviour, OutEvent, QuoteRequest, State0, State2}, bob::{Behaviour, OutEvent, QuoteRequest, State0, State2},
}, },
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, bail, Result};
use futures::FutureExt; use futures::FutureExt;
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use std::sync::Arc; use std::{convert::Infallible, sync::Arc};
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, error, info}; use tracing::{debug, error, info};
@ -167,7 +167,7 @@ impl EventLoop {
Ok((event_loop, handle)) Ok((event_loop, handle))
} }
pub async fn run(mut self) { pub async fn run(mut self) -> Result<Infallible> {
loop { loop {
tokio::select! { tokio::select! {
swarm_event = self.swarm.next().fuse() => { swarm_event = self.swarm.next().fuse() => {
@ -192,8 +192,8 @@ impl EventLoop {
debug!("Alice acknowledged encrypted signature"); debug!("Alice acknowledged encrypted signature");
} }
OutEvent::ResponseSent => {} OutEvent::ResponseSent => {}
OutEvent::Failure(err) => { OutEvent::CommunicationError(err) => {
error!("Communication error: {:#}", err) bail!("Communication error: {:#}", err)
} }
} }
}, },

View File

@ -9,6 +9,7 @@ use get_port::get_port;
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use monero_harness::{image, Monero}; use monero_harness::{image, Monero};
use std::{ use std::{
convert::Infallible,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
time::Duration, time::Duration,
@ -67,7 +68,7 @@ impl BobParams {
} }
} }
pub struct BobEventLoopJoinHandle(JoinHandle<()>); pub struct BobEventLoopJoinHandle(JoinHandle<Result<Infallible>>);
impl BobEventLoopJoinHandle { impl BobEventLoopJoinHandle {
pub fn abort(&self) { pub fn abort(&self) {