Skip to content

Commit 81c6147

Browse files
committed
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.
1 parent cfe6b95 commit 81c6147

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

fuzz/src/onion_message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ mod tests {
216216
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
217217
"Received an onion message with path_id None and a reply_path".to_string())), Some(&1));
218218
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
219-
"Responding to onion message with path_id None".to_string())), Some(&1));
219+
"Sending onion message when responding to onion message with path_id None".to_string())), Some(&1));
220220
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
221-
"Failed responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
221+
"Failed sending onion message when responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
222222
}
223223

224224
let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";

lightning/src/onion_message/messenger.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload
2929
use crate::util::logger::Logger;
3030
use crate::util::ser::Writeable;
3131

32+
use core::fmt;
3233
use core::ops::Deref;
3334
use crate::io;
3435
use crate::sync::{Arc, Mutex};
@@ -469,52 +470,31 @@ where
469470
}
470471
}
471472

472-
fn respond_with_onion_message<T: CustomOnionMessageContents>(
473-
&self, response: OnionMessageContents<T>, path_id: Option<[u8; 32]>,
474-
reply_path: Option<BlindedPath>
473+
fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
474+
&self, contents: OnionMessageContents<T>, destination: Destination,
475+
log_suffix: fmt::Arguments
475476
) {
476477
let sender = match self.node_signer.get_node_id(Recipient::Node) {
477478
Ok(node_id) => node_id,
478479
Err(_) => {
479-
log_warn!(
480-
self.logger, "Unable to retrieve node id when responding to onion message with \
481-
path_id {:02x?}", path_id
482-
);
480+
log_warn!(self.logger, "Unable to retrieve node id {}", log_suffix);
483481
return;
484482
}
485483
};
486484

487485
let peers = self.pending_messages.lock().unwrap().keys().copied().collect();
488-
489-
let destination = match reply_path {
490-
Some(reply_path) => Destination::BlindedPath(reply_path),
491-
None => {
492-
log_trace!(
493-
self.logger, "Missing reply path when responding to onion message with path_id \
494-
{:02x?}", path_id
495-
);
496-
return;
497-
},
498-
};
499-
500486
let path = match self.message_router.find_path(sender, peers, destination) {
501487
Ok(path) => path,
502488
Err(()) => {
503-
log_trace!(
504-
self.logger, "Failed to find path when responding to onion message with \
505-
path_id {:02x?}", path_id
506-
);
489+
log_trace!(self.logger, "Failed to find path {}", log_suffix);
507490
return;
508491
},
509492
};
510493

511-
log_trace!(self.logger, "Responding to onion message with path_id {:02x?}", path_id);
494+
log_trace!(self.logger, "Sending onion message {}", log_suffix);
512495

513-
if let Err(e) = self.send_onion_message(path, response, None) {
514-
log_trace!(
515-
self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}",
516-
path_id, e
517-
);
496+
if let Err(e) = self.send_onion_message(path, contents, None) {
497+
log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e);
518498
return;
519499
}
520500
}
@@ -587,7 +567,22 @@ where
587567
},
588568
};
589569
if let Some(response) = response {
590-
self.respond_with_onion_message(response, path_id, reply_path);
570+
match reply_path {
571+
Some(reply_path) => {
572+
self.find_path_and_enqueue_onion_message(
573+
response, Destination::BlindedPath(reply_path), format_args!(
574+
"when responding to onion message with path_id {:02x?}", path_id
575+
)
576+
);
577+
},
578+
None => {
579+
log_trace!(
580+
self.logger,
581+
"Missing reply path when responding to onion message with path_id {:02x?}",
582+
path_id
583+
);
584+
},
585+
}
591586
}
592587
},
593588
Ok(PeeledOnion::Forward(next_node_id, onion_message)) => {

0 commit comments

Comments
 (0)