Generalize respond_with_onion_message
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 13 Sep 2023 19:13:05 +0000 (14:13 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 18 Oct 2023 22:09:27 +0000 (17:09 -0500)
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
lightning/src/onion_message/messenger.rs

index d2e35cd45cdbabc7745abd4f49c8cdf94e793362..67938aa7c39f3b3c16b920bc6098123a5d4dabbe 100644 (file)
@@ -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";
index 6299d6178bc96902070ce5ed2bff85e2ba1e1fe1..626e2b05840343394afa87816d4e9434a6b653ff 100644 (file)
@@ -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<T: CustomOnionMessageContents>(
-               &self, response: OnionMessageContents<T>, path_id: Option<[u8; 32]>,
-               reply_path: Option<BlindedPath>
+       fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
+               &self, contents: OnionMessageContents<T>, 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)) => {