]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a `MessageSendInstructions::ForReply` 2024-08-bindings-om
authorMatt Corallo <git@bluematt.me>
Thu, 22 Aug 2024 01:31:07 +0000 (01:31 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 22 Aug 2024 22:39:46 +0000 (22:39 +0000)
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

lightning/src/onion_message/messenger.rs

index d9e36f4132526d6fb2ddd97994c687614889cb96..7d215e365be4cce4413182cb141187724e05c9b2 100644 (file)
@@ -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,
                }
        }
@@ -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),
                }
        }
@@ -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 }
        }
 }
 
@@ -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`].
@@ -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) => {
@@ -1171,7 +1177,8 @@ where
                                        }
                                }
                        },
-                       MessageSendInstructions::WithoutReplyPath { destination } =>
+                       MessageSendInstructions::WithoutReplyPath { destination }
+                               |MessageSendInstructions::ForReply { instructions: ResponseInstruction { destination, context: None } } =>
                                (destination, None),
                };