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.
This commit is contained in:
Jeffrey Czyz 2023-09-13 14:13:05 -05:00
parent cfe6b952a8
commit 81c6147a9e
No known key found for this signature in database
GPG key ID: 3A4E08275D5E96D2
2 changed files with 27 additions and 32 deletions

View file

@ -216,9 +216,9 @@ mod tests {
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), 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".to_string())), Some(&1));
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), 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(), 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"; let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";

View file

@ -29,6 +29,7 @@ use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload
use crate::util::logger::Logger; use crate::util::logger::Logger;
use crate::util::ser::Writeable; use crate::util::ser::Writeable;
use core::fmt;
use core::ops::Deref; use core::ops::Deref;
use crate::io; use crate::io;
use crate::sync::{Arc, Mutex}; use crate::sync::{Arc, Mutex};
@ -469,52 +470,31 @@ where
} }
} }
fn respond_with_onion_message<T: CustomOnionMessageContents>( fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
&self, response: OnionMessageContents<T>, path_id: Option<[u8; 32]>, &self, contents: OnionMessageContents<T>, destination: Destination,
reply_path: Option<BlindedPath> log_suffix: fmt::Arguments
) { ) {
let sender = match self.node_signer.get_node_id(Recipient::Node) { let sender = match self.node_signer.get_node_id(Recipient::Node) {
Ok(node_id) => node_id, Ok(node_id) => node_id,
Err(_) => { Err(_) => {
log_warn!( log_warn!(self.logger, "Unable to retrieve node id {}", log_suffix);
self.logger, "Unable to retrieve node id when responding to onion message with \
path_id {:02x?}", path_id
);
return; return;
} }
}; };
let peers = self.pending_messages.lock().unwrap().keys().copied().collect(); 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) { let path = match self.message_router.find_path(sender, peers, destination) {
Ok(path) => path, Ok(path) => path,
Err(()) => { Err(()) => {
log_trace!( log_trace!(self.logger, "Failed to find path {}", log_suffix);
self.logger, "Failed to find path when responding to onion message with \
path_id {:02x?}", path_id
);
return; 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) { if let Err(e) = self.send_onion_message(path, contents, None) {
log_trace!( log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e);
self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}",
path_id, e
);
return; return;
} }
} }
@ -587,7 +567,22 @@ where
}, },
}; };
if let Some(response) = response { 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)) => { Ok(PeeledOnion::Forward(next_node_id, onion_message)) => {