]> git.bitcoin.ninja Git - rust-lightning/commitdiff
logging every sent and receive onion message
authorhenghonglee <henghong.lee@gmail.com>
Tue, 8 Aug 2023 04:01:11 +0000 (12:01 +0800)
committerLalitmohansharma1 <lalit_ms@ar.iitr.ac.in>
Sat, 2 Dec 2023 18:54:14 +0000 (00:24 +0530)
Logs every sent + receive for P2P messages
solves #2346

fuzz/src/onion_message.rs
lightning/src/offers/invoice.rs
lightning/src/offers/invoice_request.rs
lightning/src/onion_message/functional_tests.rs
lightning/src/onion_message/messenger.rs
lightning/src/onion_message/offers.rs
lightning/src/onion_message/packet.rs
lightning/src/util/ser_macros.rs

index 54d15324f3b4e55878a8bf37b89d5777d6cb6f55..de7b8b6b4c612a412902720df613f768649205d4 100644 (file)
@@ -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 = "\
index 02d68b635363356736b727287f9cf6949c02be02..bb29c76164e1aa7422c5dddda07b8cbd3e7ac01f 100644 (file)
@@ -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>,
index 9ddd741a1d5428745e2bd1d840fa14eb4a1eee08..4dd85b352f708de598c60c89e91a3b1896ec81a4 100644 (file)
@@ -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>,
index 32a0fa3eb26fa3bdf5cff00509a81af819f3be23..c43b218df7b16015364966ed07882b79d29db325 100644 (file)
@@ -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 {
index 0e88b1932df826ea922c7f5f215b1c76fce1aa77..c7f01ae597862713ab1e3bde1d1f4c8b8f4b4a0d 100644 (file)
@@ -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<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -517,6 +518,9 @@ where
        pub fn send_onion_message<T: OnionMessageContents>(
                &self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>
        ) -> 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) => {
index 533b4cb571805c98be6d4235fef190de00d7ff7c..fb6ff746717ed1daeb731032ee2fd0145428b53f 100644 (file)
@@ -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 {
index 37442ab31efadc066ce639c95aee45124d2a309d..5ce02c54d08800b777a0c2c3a25f27ba1ffae462 100644 (file)
@@ -147,7 +147,7 @@ impl<T: OnionMessageContents> Writeable for ParsedOnionMessageContents<T> {
 }
 
 /// 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;
 }
index 782e786ff843eb08f5b5e0146decc9e8b1138af5..84d9f7a180bb301a1c18a136530a04ce75589a9e 100644 (file)
@@ -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<tlv_record_ref_type!($fieldty)>,
                        )*