From 81c6147a9ec573abbf5b0cbe434e86626cc37112 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 13 Sep 2023 14:13:05 -0500 Subject: [PATCH] Generalize respond_with_onion_message OnionMessenger can send onion message responses from its handlers using respond_with_onion_message, which finds a path to the destination and enqueues the response for sending. Generalize this as it can be used not only for responses but for initial sends as well. --- fuzz/src/onion_message.rs | 4 +- lightning/src/onion_message/messenger.rs | 55 +++++++++++------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index d2e35cd4..67938aa7 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -216,9 +216,9 @@ mod tests { 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)); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Responding to onion message with path_id None".to_string())), Some(&1)); + "Sending onion message when responding to onion message with path_id None".to_string())), Some(&1)); assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), - "Failed responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1)); + "Failed sending onion message when responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1)); } let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000"; diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 6299d617..626e2b05 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -29,6 +29,7 @@ use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload use crate::util::logger::Logger; use crate::util::ser::Writeable; +use core::fmt; use core::ops::Deref; use crate::io; use crate::sync::{Arc, Mutex}; @@ -469,52 +470,31 @@ where } } - fn respond_with_onion_message( - &self, response: OnionMessageContents, path_id: Option<[u8; 32]>, - reply_path: Option + fn find_path_and_enqueue_onion_message( + &self, contents: OnionMessageContents, destination: Destination, + log_suffix: fmt::Arguments ) { let sender = match self.node_signer.get_node_id(Recipient::Node) { Ok(node_id) => node_id, Err(_) => { - log_warn!( - self.logger, "Unable to retrieve node id when responding to onion message with \ - path_id {:02x?}", path_id - ); + log_warn!(self.logger, "Unable to retrieve node id {}", log_suffix); return; } }; let peers = self.pending_messages.lock().unwrap().keys().copied().collect(); - - let destination = match reply_path { - Some(reply_path) => Destination::BlindedPath(reply_path), - None => { - log_trace!( - self.logger, "Missing reply path when responding to onion message with path_id \ - {:02x?}", path_id - ); - return; - }, - }; - let path = match self.message_router.find_path(sender, peers, destination) { Ok(path) => path, Err(()) => { - log_trace!( - self.logger, "Failed to find path when responding to onion message with \ - path_id {:02x?}", path_id - ); + log_trace!(self.logger, "Failed to find path {}", log_suffix); return; }, }; - log_trace!(self.logger, "Responding to onion message with path_id {:02x?}", path_id); + log_trace!(self.logger, "Sending onion message {}", log_suffix); - if let Err(e) = self.send_onion_message(path, response, None) { - log_trace!( - self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}", - path_id, e - ); + if let Err(e) = self.send_onion_message(path, contents, None) { + log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e); return; } } @@ -587,7 +567,22 @@ where }, }; if let Some(response) = response { - self.respond_with_onion_message(response, path_id, reply_path); + match reply_path { + Some(reply_path) => { + self.find_path_and_enqueue_onion_message( + response, Destination::BlindedPath(reply_path), format_args!( + "when responding to onion message with path_id {:02x?}", path_id + ) + ); + }, + None => { + log_trace!( + self.logger, + "Missing reply path when responding to onion message with path_id {:02x?}", + path_id + ); + }, + } } }, Ok(PeeledOnion::Forward(next_node_id, onion_message)) => { -- 2.30.2