From f5ca5faabf4933a552ddbbbbdf0c9cd6a0a1c324 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 8 Feb 2021 16:38:23 +1100 Subject: [PATCH] Process execution setup failure similarly to other failures By merging it in the failure event of the root behaviour. --- swap/src/protocol/alice/behaviour.rs | 6 ++++-- swap/src/protocol/alice/event_loop.rs | 10 +++++----- swap/src/protocol/alice/execution_setup.rs | 11 ++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/swap/src/protocol/alice/behaviour.rs b/swap/src/protocol/alice/behaviour.rs index 891ad108..14e46000 100644 --- a/swap/src/protocol/alice/behaviour.rs +++ b/swap/src/protocol/alice/behaviour.rs @@ -20,7 +20,7 @@ pub enum OutEvent { msg: SwapRequest, channel: ResponseChannel, }, - ExecutionSetupDone(anyhow::Result>), + ExecutionSetupDone(Box), TransferProofAcknowledged, EncryptedSignature { msg: Box, @@ -53,8 +53,10 @@ impl From for OutEvent { impl From for OutEvent { fn from(event: execution_setup::OutEvent) -> Self { + use crate::protocol::alice::execution_setup::OutEvent::*; match event { - execution_setup::OutEvent::Done(res) => OutEvent::ExecutionSetupDone(res.map(Box::new)), + Done(state3) => OutEvent::ExecutionSetupDone(Box::new(state3)), + Failure(err) => OutEvent::Failure(err), } } } diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index bc435a24..32d4da44 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -34,7 +34,7 @@ impl Default for Channels { #[derive(Debug)] pub struct EventLoopHandle { - done_execution_setup: Receiver>, + done_execution_setup: Receiver, recv_encrypted_signature: Receiver, recv_swap_request: Receiver<(SwapRequest, ResponseChannel)>, conn_established: Receiver, @@ -61,7 +61,7 @@ impl EventLoopHandle { self.done_execution_setup .recv() .await - .ok_or_else(|| anyhow!("Failed to setup execution with Bob"))? + .ok_or_else(|| anyhow!("Failed to setup execution with Bob")) } pub async fn recv_encrypted_signature(&mut self) -> Result { @@ -121,7 +121,7 @@ impl EventLoopHandle { pub struct EventLoop { swarm: libp2p::Swarm, start_execution_setup: Receiver<(PeerId, State0)>, - done_execution_setup: Sender>, + done_execution_setup: Sender, recv_encrypted_signature: Sender, recv_swap_request: Sender<(SwapRequest, ResponseChannel)>, conn_established: Sender, @@ -194,8 +194,8 @@ impl EventLoop { OutEvent::SwapRequest { msg, channel } => { let _ = self.recv_swap_request.send((msg, channel)).await; } - OutEvent::ExecutionSetupDone(res) => { - let _ = self.done_execution_setup.send(res.map(|state|*state)).await; + OutEvent::ExecutionSetupDone(state3) => { + let _ = self.done_execution_setup.send(*state3).await; } OutEvent::TransferProofAcknowledged => { trace!("Bob acknowledged transfer proof"); diff --git a/swap/src/protocol/alice/execution_setup.rs b/swap/src/protocol/alice/execution_setup.rs index 967ba26f..e05964a2 100644 --- a/swap/src/protocol/alice/execution_setup.rs +++ b/swap/src/protocol/alice/execution_setup.rs @@ -8,7 +8,7 @@ use crate::{ bob::{Message0, Message2, Message4}, }, }; -use anyhow::{Context, Error, Result}; +use anyhow::{Context, Error}; use libp2p::PeerId; use libp2p_async_await::BehaviourOutEvent; use serde::{Deserialize, Serialize}; @@ -32,14 +32,15 @@ pub struct Message3 { #[derive(Debug)] pub enum OutEvent { - Done(Result), + Done(State3), + Failure(Error), } -impl From> for OutEvent { +impl From> for OutEvent { fn from(event: BehaviourOutEvent) -> Self { match event { - BehaviourOutEvent::Inbound(_, Ok(State3)) => OutEvent::Done(Ok(State3)), - BehaviourOutEvent::Inbound(_, Err(e)) => OutEvent::Done(Err(e)), + BehaviourOutEvent::Inbound(_, Ok(State3)) => OutEvent::Done(State3), + BehaviourOutEvent::Inbound(_, Err(e)) => OutEvent::Failure(e), BehaviourOutEvent::Outbound(..) => unreachable!("Alice only supports inbound"), } }