Skip to content

Commit d09e732

Browse files
committed
Add a MessageSendInstructions::ForReply
In order to allow onion message handlers to reply asynchronously without introducing a circular dependency graph, the message handlers need to be able to send replies described by `MessageSendInstructions`. This allows them to send replies via the normal message queuing (i.e. without making a function call to `OnionMessenger`). Here we enable that by adding a `MessageSendInstructions::ForReply` variant which holds `ReplyInstruction`s. Fixes #3178
1 parent 6160517 commit d09e732

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

lightning/src/onion_message/messenger.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl Responder {
367367
/// Use when the recipient doesn't need to send back a reply to us.
368368
pub fn respond(self) -> ResponseInstruction {
369369
ResponseInstruction {
370-
send_path: self.reply_path,
370+
destination: Destination::BlindedPath(self.reply_path),
371371
context: None,
372372
}
373373
}
@@ -377,7 +377,7 @@ impl Responder {
377377
/// Use when the recipient needs to send back a reply to us.
378378
pub fn respond_with_reply_path(self, context: MessageContext) -> ResponseInstruction {
379379
ResponseInstruction {
380-
send_path: self.reply_path,
380+
destination: Destination::BlindedPath(self.reply_path),
381381
context: Some(context),
382382
}
383383
}
@@ -386,17 +386,16 @@ impl Responder {
386386
/// Instructions for how and where to send the response to an onion message.
387387
#[derive(Clone)]
388388
pub struct ResponseInstruction {
389-
send_path: BlindedMessagePath,
389+
/// The destination in a response is always a [`Destination::BlindedPath`] but using a
390+
/// [`Destination`] rather than an explicit [`BlindedMessagePath`] simplifies the logic in
391+
/// [`OnionMessenger::send_onion_message_internal`] somewhat.
392+
destination: Destination,
390393
context: Option<MessageContext>,
391394
}
392395

393396
impl ResponseInstruction {
394397
fn into_instructions(self) -> MessageSendInstructions {
395-
let destination = Destination::BlindedPath(self.send_path);
396-
match self.context {
397-
Some(context) => MessageSendInstructions::WithReplyPath { destination, context },
398-
None => MessageSendInstructions::WithoutReplyPath { destination },
399-
}
398+
MessageSendInstructions::ForReply { instructions: self }
400399
}
401400
}
402401

@@ -425,7 +424,12 @@ pub enum MessageSendInstructions {
425424
WithoutReplyPath {
426425
/// The destination where we need to send our message.
427426
destination: Destination,
428-
}
427+
},
428+
/// Indicates that a message is being sent as a reply to a received message.
429+
ForReply {
430+
/// The instructions provided by the [`Responder`].
431+
instructions: ResponseInstruction,
432+
},
429433
}
430434

431435
/// A trait defining behavior for routing an [`OnionMessage`].
@@ -1158,7 +1162,9 @@ where
11581162
let (destination, reply_path) = match instructions {
11591163
MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path } =>
11601164
(destination, Some(reply_path)),
1161-
MessageSendInstructions::WithReplyPath { destination, context } => {
1165+
MessageSendInstructions::WithReplyPath { destination, context }
1166+
|MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: Some(context) } } =>
1167+
{
11621168
match self.create_blinded_path(context) {
11631169
Ok(reply_path) => (destination, Some(reply_path)),
11641170
Err(err) => {
@@ -1171,7 +1177,8 @@ where
11711177
}
11721178
}
11731179
},
1174-
MessageSendInstructions::WithoutReplyPath { destination } =>
1180+
MessageSendInstructions::WithoutReplyPath { destination }
1181+
|MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: None } } =>
11751182
(destination, None),
11761183
};
11771184

0 commit comments

Comments
 (0)