Skip to content

Commit

Permalink
Add a MessageSendInstructions::ForReply
Browse files Browse the repository at this point in the history
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 lightningdevkit#3178
  • Loading branch information
TheBlueMatt committed Aug 22, 2024
1 parent eb46eac commit f43e85e
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl Responder {
/// Use when the recipient doesn't need to send back a reply to us.
pub fn respond(self) -> ResponseInstruction {
ResponseInstruction {
send_path: self.reply_path,
destination: Destination::BlindedPath(self.reply_path),
context: None,
}
}
Expand All @@ -377,7 +377,7 @@ impl Responder {
/// Use when the recipient needs to send back a reply to us.
pub fn respond_with_reply_path(self, context: MessageContext) -> ResponseInstruction {
ResponseInstruction {
send_path: self.reply_path,
destination: Destination::BlindedPath(self.reply_path),
context: Some(context),
}
}
Expand All @@ -386,17 +386,16 @@ impl Responder {
/// Instructions for how and where to send the response to an onion message.
#[derive(Clone)]
pub struct ResponseInstruction {
send_path: BlindedMessagePath,
/// The destination in a response is always a [`Destination::BlindedPath`] but using a
/// [`Destination`] rather than an explicit [`BlindedMessagePath`] simplifies the logic in
/// [`OnionMessenger::send_onion_message_internal`] somewhat.
destination: Destination,
context: Option<MessageContext>,
}

impl ResponseInstruction {
fn into_instructions(self) -> MessageSendInstructions {
let destination = Destination::BlindedPath(self.send_path);
match self.context {
Some(context) => MessageSendInstructions::WithReplyPath { destination, context },
None => MessageSendInstructions::WithoutReplyPath { destination },
}
MessageSendInstructions::ForReply { instructions: self }
}
}

Expand Down Expand Up @@ -425,7 +424,12 @@ pub enum MessageSendInstructions {
WithoutReplyPath {
/// The destination where we need to send our message.
destination: Destination,
}
},
/// Indicates that a message is being sent as a reply to a received message.
ForReply {
/// The instructions provided by the [`Responder`].
instructions: ResponseInstruction,
},
}

/// A trait defining behavior for routing an [`OnionMessage`].
Expand Down Expand Up @@ -1158,7 +1162,9 @@ where
let (destination, reply_path) = match instructions {
MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path } =>
(destination, Some(reply_path)),
MessageSendInstructions::WithReplyPath { destination, context } => {
MessageSendInstructions::WithReplyPath { destination, context }
|MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: Some(context) } } =>
{
match self.create_blinded_path(context) {
Ok(reply_path) => (destination, Some(reply_path)),
Err(err) => {
Expand All @@ -1171,7 +1177,8 @@ where
}
}
},
MessageSendInstructions::WithoutReplyPath { destination } =>
MessageSendInstructions::WithoutReplyPath { destination }
|MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: None } } =>
(destination, None),
};

Expand Down

0 comments on commit f43e85e

Please sign in to comment.