Skip to content

Commit 46c5d9e

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 c8fa7b6 commit 46c5d9e

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

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};
@@ -347,52 +348,31 @@ where
347348
}
348349
}
349350

350-
fn respond_with_onion_message<T: CustomOnionMessageContents>(
351-
&self, response: OnionMessageContents<T>, path_id: Option<[u8; 32]>,
352-
reply_path: Option<BlindedPath>
351+
fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
352+
&self, contents: OnionMessageContents<T>, destination: Destination,
353+
log_suffix: fmt::Arguments
353354
) {
354355
let sender = match self.node_signer.get_node_id(Recipient::Node) {
355356
Ok(node_id) => node_id,
356357
Err(_) => {
357-
log_warn!(
358-
self.logger, "Unable to retrieve node id when responding to onion message with \
359-
path_id {:02x?}", path_id
360-
);
358+
log_warn!(self.logger, "Unable to retrieve node id {}", log_suffix);
361359
return;
362360
}
363361
};
364362

365363
let peers = self.pending_messages.lock().unwrap().keys().copied().collect();
366-
367-
let destination = match reply_path {
368-
Some(reply_path) => Destination::BlindedPath(reply_path),
369-
None => {
370-
log_trace!(
371-
self.logger, "Missing reply path when responding to onion message with path_id \
372-
{:02x?}", path_id
373-
);
374-
return;
375-
},
376-
};
377-
378364
let path = match self.message_router.find_path(sender, peers, destination) {
379365
Ok(path) => path,
380366
Err(()) => {
381-
log_trace!(
382-
self.logger, "Failed to find path when responding to onion message with \
383-
path_id {:02x?}", path_id
384-
);
367+
log_trace!(self.logger, "Failed to find path {}", log_suffix);
385368
return;
386369
},
387370
};
388371

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

391-
if let Err(e) = self.send_onion_message(path, response, None) {
392-
log_trace!(
393-
self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}",
394-
path_id, e
395-
);
374+
if let Err(e) = self.send_onion_message(path, contents, None) {
375+
log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e);
396376
return;
397377
}
398378
}
@@ -493,7 +473,22 @@ where
493473
};
494474

495475
if let Some(response) = response {
496-
self.respond_with_onion_message(response, path_id, reply_path);
476+
match reply_path {
477+
Some(reply_path) => {
478+
self.find_path_and_enqueue_onion_message(
479+
response, Destination::BlindedPath(reply_path), format_args!(
480+
"when responding to onion message with path_id {:02x?}", path_id
481+
)
482+
);
483+
},
484+
None => {
485+
log_trace!(
486+
self.logger,
487+
"Missing reply path when responding to onion message with path_id {:02x?}",
488+
path_id
489+
);
490+
},
491+
}
497492
}
498493
},
499494
Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {

0 commit comments

Comments
 (0)