From e54d26b26c45ae774705a5ce18e270df06f448ea Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 17 Mar 2021 14:42:21 +1100 Subject: [PATCH] Improve log messages by including PeerId --- swap/src/protocol/alice/behaviour.rs | 73 ++++++++++--------- .../src/protocol/alice/encrypted_signature.rs | 26 ++++--- swap/src/protocol/alice/event_loop.rs | 8 +- swap/src/protocol/alice/execution_setup.rs | 4 +- swap/src/protocol/alice/transfer_proof.rs | 38 +++++----- 5 files changed, 83 insertions(+), 66 deletions(-) diff --git a/swap/src/protocol/alice/behaviour.rs b/swap/src/protocol/alice/behaviour.rs index 97a20f1c..5c621d7b 100644 --- a/swap/src/protocol/alice/behaviour.rs +++ b/swap/src/protocol/alice/behaviour.rs @@ -28,14 +28,17 @@ pub enum OutEvent { bob_peer_id: PeerId, state3: Box, }, - TransferProofAcknowledged, + TransferProofAcknowledged(PeerId), EncryptedSignature { msg: Box, channel: ResponseChannel<()>, peer: PeerId, }, ResponseSent, // Same variant is used for all messages as no processing is done - Failure(Error), + Failure { + peer: PeerId, + error: Error, + }, } impl From for OutEvent { @@ -62,23 +65,20 @@ impl From for OutEvent { } => OutEvent::SpotPriceRequested { msg, channel, peer }, spot_price::OutEvent::Message { message: RequestResponseMessage::Response { .. }, - .. - } => OutEvent::Failure(anyhow!( - "Alice is only meant to hand out spot prices, not receive them" - )), - spot_price::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent, - spot_price::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure(anyhow!( - "spot_price protocol with peer {} failed due to {:?}", peer, - error - )), - spot_price::OutEvent::OutboundFailure { peer, error, .. } => { - OutEvent::Failure(anyhow!( - "spot_price protocol with peer {} failed due to {:?}", - peer, - error - )) - } + } => OutEvent::Failure { + error: anyhow!("Alice is only meant to hand out spot prices, not receive them"), + peer, + }, + spot_price::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent, + spot_price::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure { + error: anyhow!("spot_price protocol failed due to {:?}", error), + peer, + }, + spot_price::OutEvent::OutboundFailure { peer, error, .. } => OutEvent::Failure { + error: anyhow!("spot_price protocol failed due to {:?}", error), + peer, + }, } } } @@ -92,21 +92,20 @@ impl From for OutEvent { } => OutEvent::QuoteRequested { channel, peer }, quote::OutEvent::Message { message: RequestResponseMessage::Response { .. }, - .. - } => OutEvent::Failure(anyhow!( - "Alice is only meant to hand out quotes, not receive them" - )), + peer, + } => OutEvent::Failure { + error: anyhow!("Alice is only meant to hand out quotes, not receive them"), + peer, + }, quote::OutEvent::ResponseSent { .. } => OutEvent::ResponseSent, - quote::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure(anyhow!( - "quote protocol with peer {} failed due to {:?}", + quote::OutEvent::InboundFailure { peer, error, .. } => OutEvent::Failure { + error: anyhow!("quote protocol failed due to {:?}", error), peer, - error - )), - quote::OutEvent::OutboundFailure { peer, error, .. } => OutEvent::Failure(anyhow!( - "quote protocol with peer {} failed due to {:?}", + }, + quote::OutEvent::OutboundFailure { peer, error, .. } => OutEvent::Failure { + error: anyhow!("quote protocol failed due to {:?}", error), peer, - error - )), + }, } } } @@ -122,7 +121,7 @@ impl From for OutEvent { bob_peer_id, state3: Box::new(state3), }, - Failure(err) => OutEvent::Failure(err), + Failure { peer, error } => OutEvent::Failure { peer, error }, } } } @@ -131,8 +130,11 @@ impl From for OutEvent { fn from(event: transfer_proof::OutEvent) -> Self { use crate::protocol::alice::transfer_proof::OutEvent::*; match event { - Acknowledged => OutEvent::TransferProofAcknowledged, - Failure(err) => OutEvent::Failure(err.context("Failure with Transfer Proof")), + Acknowledged(peer) => OutEvent::TransferProofAcknowledged(peer), + Failure { peer, error } => OutEvent::Failure { + peer, + error: error.context("Failure with Transfer Proof"), + }, } } } @@ -147,7 +149,10 @@ impl From for OutEvent { peer, }, AckSent => OutEvent::ResponseSent, - Failure(err) => OutEvent::Failure(err.context("Failure with Encrypted Signature")), + Failure { peer, error } => OutEvent::Failure { + peer, + error: error.context("Failure with Encrypted Signature"), + }, } } } diff --git a/swap/src/protocol/alice/encrypted_signature.rs b/swap/src/protocol/alice/encrypted_signature.rs index f7dee187..1cf1f14c 100644 --- a/swap/src/protocol/alice/encrypted_signature.rs +++ b/swap/src/protocol/alice/encrypted_signature.rs @@ -17,7 +17,10 @@ pub enum OutEvent { peer: PeerId, }, AckSent, - Failure(Error), + Failure { + peer: PeerId, + error: Error, + }, } /// A `NetworkBehaviour` that represents receiving the Bitcoin encrypted @@ -73,14 +76,19 @@ impl From> for OutEvent { } RequestResponseEvent::Message { message: RequestResponseMessage::Response { .. }, - .. - } => OutEvent::Failure(anyhow!("Alice should not get a Response")), - RequestResponseEvent::InboundFailure { error, .. } => { - OutEvent::Failure(anyhow!("Inbound failure: {:?}", error)) - } - RequestResponseEvent::OutboundFailure { error, .. } => { - OutEvent::Failure(anyhow!("Outbound failure: {:?}", error)) - } + peer, + } => OutEvent::Failure { + peer, + error: anyhow!("Alice should not get a Response"), + }, + RequestResponseEvent::InboundFailure { error, peer, .. } => OutEvent::Failure { + peer, + error: anyhow!("Inbound failure: {:?}", error), + }, + RequestResponseEvent::OutboundFailure { error, peer, .. } => OutEvent::Failure { + peer, + error: anyhow!("Outbound failure: {:?}", error), + }, RequestResponseEvent::ResponseSent { .. } => OutEvent::AckSent, } } diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 41cbb910..aecae67f 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -158,8 +158,8 @@ where OutEvent::ExecutionSetupDone{bob_peer_id, state3} => { let _ = self.handle_execution_setup_done(bob_peer_id, *state3).await; } - OutEvent::TransferProofAcknowledged => { - trace!("Bob acknowledged transfer proof"); + OutEvent::TransferProofAcknowledged(peer) => { + trace!(%peer, "Bob acknowledged transfer proof"); } OutEvent::EncryptedSignature{ msg, channel, peer } => { match self.recv_encrypted_signature.remove(&peer) { @@ -177,8 +177,8 @@ where } } OutEvent::ResponseSent => {} - OutEvent::Failure(err) => { - error!("Communication error: {:#}", err); + OutEvent::Failure {peer, error} => { + error!(%peer, "Communication error: {:#}", error); } } }, diff --git a/swap/src/protocol/alice/execution_setup.rs b/swap/src/protocol/alice/execution_setup.rs index d5c50e38..b2ec5162 100644 --- a/swap/src/protocol/alice/execution_setup.rs +++ b/swap/src/protocol/alice/execution_setup.rs @@ -29,7 +29,7 @@ pub struct Message3 { #[derive(Debug)] pub enum OutEvent { Done { bob_peer_id: PeerId, state3: State3 }, - Failure(Error), + Failure { peer: PeerId, error: Error }, } impl From> for OutEvent { @@ -39,7 +39,7 @@ impl From> for OutEvent { bob_peer_id, state3, }, - BehaviourOutEvent::Inbound(_, Err(e)) => OutEvent::Failure(e), + BehaviourOutEvent::Inbound(peer, Err(e)) => OutEvent::Failure { peer, error: e }, BehaviourOutEvent::Outbound(..) => unreachable!("Alice only supports inbound"), } } diff --git a/swap/src/protocol/alice/transfer_proof.rs b/swap/src/protocol/alice/transfer_proof.rs index 9bf8ab50..2d03add8 100644 --- a/swap/src/protocol/alice/transfer_proof.rs +++ b/swap/src/protocol/alice/transfer_proof.rs @@ -16,8 +16,8 @@ pub struct TransferProof { #[derive(Debug)] pub enum OutEvent { - Acknowledged, - Failure(Error), + Acknowledged(PeerId), + Failure { peer: PeerId, error: Error }, } /// A `NetworkBehaviour` that represents sending the Monero transfer proof to @@ -56,23 +56,27 @@ impl From> for OutEvent { match event { RequestResponseEvent::Message { message: RequestResponseMessage::Request { .. }, - .. - } => OutEvent::Failure(anyhow!( - "Alice should never get a transfer proof request from Bob" - )), + peer, + } => OutEvent::Failure { + peer, + error: anyhow!("Alice should never get a transfer proof request from Bob"), + }, RequestResponseEvent::Message { message: RequestResponseMessage::Response { .. }, - .. - } => OutEvent::Acknowledged, - RequestResponseEvent::InboundFailure { error, .. } => { - OutEvent::Failure(anyhow!("Inbound failure: {:?}", error)) - } - RequestResponseEvent::OutboundFailure { error, .. } => { - OutEvent::Failure(anyhow!("Outbound failure: {:?}", error)) - } - RequestResponseEvent::ResponseSent { .. } => { - OutEvent::Failure(anyhow!("Alice should not send a response")) - } + peer, + } => OutEvent::Acknowledged(peer), + RequestResponseEvent::InboundFailure { error, peer, .. } => OutEvent::Failure { + peer, + error: anyhow!("Inbound failure: {:?}", error), + }, + RequestResponseEvent::OutboundFailure { error, peer, .. } => OutEvent::Failure { + peer, + error: anyhow!("Outbound failure: {:?}", error), + }, + RequestResponseEvent::ResponseSent { peer, .. } => OutEvent::Failure { + peer, + error: anyhow!("Alice should not send a response"), + }, } } }