Skip to content

logging every sent receive onion message #2642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl OffersMessageHandler for TestOffersMessageHandler {
}
}

#[derive(Debug)]
struct TestCustomMessage {}

const CUSTOM_MESSAGE_TYPE: u64 = 4242;
Expand Down Expand Up @@ -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 = "\
Expand Down
4 changes: 1 addition & 3 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
Expand Down
4 changes: 1 addition & 3 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
Expand Down
1 change: 1 addition & 0 deletions lightning/src/onion_message/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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> {
Expand Down Expand Up @@ -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
)?;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down
19 changes: 18 additions & 1 deletion lightning/src/onion_message/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`].
///
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/onion_message/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/util/ser_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>,
)*
Expand Down