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 d2cc904 commit d577b24
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 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,13 @@ impl Responder {
/// Instructions for how and where to send the response to an onion message.
#[derive(Clone)]
pub struct ResponseInstruction {
send_path: BlindedMessagePath,
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 +421,11 @@ pub enum MessageSendInstructions {
WithoutReplyPath {
/// The desination where we need to send our message.
destination: Destination,
}
},
/// Indicates that a message is being sent as a reply to a received message.
ForReply {
instructions: ResponseInstruction,
},
}

/// A trait defining behavior for routing an [`OnionMessage`].
Expand Down Expand Up @@ -1169,7 +1169,8 @@ 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 @@ -1182,7 +1183,8 @@ where
}
}
},
MessageSendInstructions::WithoutReplyPath { destination } =>
MessageSendInstructions::WithoutReplyPath { destination }|
MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: None } } =>
(destination, None),
};

Expand Down

0 comments on commit d577b24

Please sign in to comment.