From ff5e5221d244bbae7ef5b58f01018b8b4c731fec Mon Sep 17 00:00:00 2001 From: henghonglee Date: Tue, 8 Aug 2023 12:01:11 +0800 Subject: [PATCH] logging every sent and receive onion message Logs every sent + receive for P2P messages solves #2346 --- fuzz/src/onion_message.rs | 6 ++++-- lightning/src/offers/invoice.rs | 4 +--- lightning/src/offers/invoice_request.rs | 4 +--- .../src/onion_message/functional_tests.rs | 1 + lightning/src/onion_message/messenger.rs | 17 +++++++++++------ lightning/src/onion_message/offers.rs | 19 ++++++++++++++++++- lightning/src/onion_message/packet.rs | 2 +- lightning/src/util/ser_macros.rs | 2 +- 8 files changed, 38 insertions(+), 17 deletions(-) diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 54d15324f..de7b8b6b4 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -91,6 +91,7 @@ impl OffersMessageHandler for TestOffersMessageHandler { } } +#[derive(Debug)] struct TestCustomMessage {} const CUSTOM_MESSAGE_TYPE: u64 = 4242; @@ -265,9 +266,10 @@ mod tests { { let log_entries = logger.lines.lock().unwrap(); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Received an onion message with path_id None and a reply_path".to_string())), Some(&1)); + "Received an onion message with path_id None and a reply_path: Custom(TestCustomMessage)" + .to_string())), Some(&1)); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Sending onion message when responding to Custom onion message with path_id None".to_string())), Some(&1)); + "Sending onion message: TestCustomMessage".to_string())), Some(&1)); } let two_unblinded_hops_om = "\ diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 02d68b635..bb29c7616 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -720,8 +720,7 @@ impl Bolt12Invoice { self.contents.verify(TlvStream::new(&self.bytes), key, secp_ctx) } - #[cfg(test)] - pub(super) fn as_tlv_stream(&self) -> FullInvoiceTlvStreamRef { + pub(crate) fn as_tlv_stream(&self) -> FullInvoiceTlvStreamRef { let (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream, invoice_tlv_stream) = self.contents.as_tlv_stream(); let signature_tlv_stream = SignatureTlvStreamRef { @@ -1143,7 +1142,6 @@ impl_writeable!(FallbackAddress, { version, program }); type FullInvoiceTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, InvoiceTlvStream, SignatureTlvStream); -#[cfg(test)] type FullInvoiceTlvStreamRef<'a> = ( PayerTlvStreamRef<'a>, OfferTlvStreamRef<'a>, diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 9ddd741a1..4dd85b352 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -608,8 +608,7 @@ impl InvoiceRequest { }) } - #[cfg(test)] - fn as_tlv_stream(&self) -> FullInvoiceRequestTlvStreamRef { + pub(crate) fn as_tlv_stream(&self) -> FullInvoiceRequestTlvStreamRef { let (payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream) = self.contents.as_tlv_stream(); let signature_tlv_stream = SignatureTlvStreamRef { @@ -811,7 +810,6 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST type FullInvoiceRequestTlvStream = (PayerTlvStream, OfferTlvStream, InvoiceRequestTlvStream, SignatureTlvStream); -#[cfg(test)] type FullInvoiceRequestTlvStreamRef<'a> = ( PayerTlvStreamRef<'a>, OfferTlvStreamRef<'a>, diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 32a0fa3eb..c43b218df 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -381,6 +381,7 @@ fn reply_path() { fn invalid_custom_message_type() { let nodes = create_nodes(2); + #[derive(Debug)] struct InvalidCustomMessage{} impl OnionMessageContents for InvalidCustomMessage { fn tlv_type(&self) -> u64 { diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 0e88b1932..c7f01ae59 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -71,7 +71,7 @@ use crate::prelude::*; /// # use std::sync::Arc; /// # struct FakeLogger; /// # impl Logger for FakeLogger { -/// # fn log(&self, record: Record) { unimplemented!() } +/// # fn log(&self, record: Record) { println!("{:?}" , record); } /// # } /// # struct FakeMessageRouter {} /// # impl MessageRouter for FakeMessageRouter { @@ -97,7 +97,8 @@ use crate::prelude::*; /// &keys_manager, &keys_manager, logger, message_router, &offers_message_handler, /// &custom_message_handler /// ); -/// + +/// # #[derive(Debug)] /// # struct YourCustomMessage {} /// impl Writeable for YourCustomMessage { /// fn write(&self, w: &mut W) -> Result<(), io::Error> { @@ -517,6 +518,9 @@ where pub fn send_onion_message( &self, path: OnionMessagePath, contents: T, reply_path: Option ) -> Result<(), SendError> { + + log_trace!(self.logger, "Sending onion message: {:?}", contents); + let (first_node_id, onion_msg) = create_onion_message( &self.entropy_source, &self.node_signer, &self.secp_ctx, path, contents, reply_path )?; @@ -570,7 +574,7 @@ where }, }; - log_trace!(self.logger, "Sending onion message {}", log_suffix); + log_trace!(self.logger, "Sending onion message {}: {:?}", log_suffix, contents); if let Err(e) = self.send_onion_message(path, contents, reply_path) { log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e); @@ -629,9 +633,10 @@ where msg, &self.secp_ctx, &*self.node_signer, &*self.logger, &*self.custom_handler ) { Ok(PeeledOnion::Receive(message, path_id, reply_path)) => { - log_trace!(self.logger, - "Received an onion message with path_id {:02x?} and {} reply_path", - path_id, if reply_path.is_some() { "a" } else { "no" }); + log_trace!( + self.logger, + "Received an onion message with path_id {:02x?} and {} reply_path: {:?}", + path_id, if reply_path.is_some() { "a" } else { "no" }, message); match message { ParsedOnionMessageContents::Offers(msg) => { diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs index 533b4cb57..fb6ff7467 100644 --- a/lightning/src/onion_message/offers.rs +++ b/lightning/src/onion_message/offers.rs @@ -10,6 +10,7 @@ //! Message handling for BOLT 12 Offers. use core::convert::TryFrom; +use core::fmt; use crate::io::{self, Read}; use crate::ln::msgs::DecodeError; use crate::offers::invoice_error::InvoiceError; @@ -58,7 +59,7 @@ pub trait OffersMessageHandler { /// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`]. /// /// [`OnionMessage`]: crate::ln::msgs::OnionMessage -#[derive(Clone, Debug)] +#[derive(Clone)] pub enum OffersMessage { /// A request for a [`Bolt12Invoice`] for a particular [`Offer`]. /// @@ -92,6 +93,22 @@ impl OffersMessage { } } +impl fmt::Debug for OffersMessage { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + OffersMessage::InvoiceRequest(message) => { + write!(f, "{:?}", message.as_tlv_stream()) + } + OffersMessage::Invoice(message) => { + write!(f, "{:?}", message.as_tlv_stream()) + } + OffersMessage::InvoiceError(message) => { + write!(f, "{:?}", message) + } + } + } +} + impl OnionMessageContents for OffersMessage { fn tlv_type(&self) -> u64 { match self { diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index 37442ab31..5ce02c54d 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -147,7 +147,7 @@ impl Writeable for ParsedOnionMessageContents { } /// The contents of an onion message. -pub trait OnionMessageContents: Writeable { +pub trait OnionMessageContents: Writeable + core::fmt::Debug { /// Returns the TLV type identifying the message contents. MUST be >= 64. fn tlv_type(&self) -> u64; } diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 782e786ff..84d9f7a18 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -917,7 +917,7 @@ macro_rules! tlv_stream { #[cfg_attr(test, derive(PartialEq))] #[derive(Debug)] - pub(super) struct $nameref<'a> { + pub(crate) struct $nameref<'a> { $( pub(super) $field: Option, )* -- 2.39.5