Introduce ResponseInstructions for OnionMessage Handling
authorshaavan <shaavan.github@gmail.com>
Mon, 25 Mar 2024 08:42:47 +0000 (14:12 +0530)
committershaavan <shaavan.github@gmail.com>
Fri, 3 May 2024 11:56:19 +0000 (17:26 +0530)
- Currently, handle_message (or handle_custom_message) only exposes
  the generated response for an OnionMessage, lacking the necessary
  reply_path for asynchronous responses.

- This commit introduces a new flow for OnionMessage handling.

- Instead of solely taking the message as input, handle_message now accepts
  an Optional `responder`, returning a `ResponseInstruction` containing both
  the response and the reply_path.

- `ResponseInstruction` utilizes different enum variants to indicate how
  `handle_onion_message_response` should handle the response.

- This enhancement enables exposing the reply_path alongside the response
  and allows for more complex response mechanisms, such as responding with
  an added reply_path.

- The commit introduces the foundational framework (structs & enums) for this new flow.

lightning/src/onion_message/messenger.rs

index 1d7a730fa3625126097fd6d25de150e5ba46c742..647239743c3eff0ec4593e47ac332a493bd0f2a8 100644 (file)
@@ -246,6 +246,51 @@ impl OnionMessageRecipient {
        }
 }
 
+
+/// The `Responder` struct creates an appropriate [`ResponseInstruction`]
+/// for responding to a message.
+pub struct Responder {
+       /// The path along which a response can be sent.
+       reply_path: BlindedPath,
+       path_id: Option<[u8; 32]>
+}
+
+impl Responder {
+       /// Creates a new [`Responder`] instance with the provided reply path.
+       fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
+               Responder {
+                       reply_path,
+                       path_id,
+               }
+       }
+
+       /// Creates the appropriate [`ResponseInstruction`] for a given response.
+       pub fn respond<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
+               ResponseInstruction::WithoutReplyPath(OnionMessageResponse {
+                       message: response,
+                       reply_path: self.reply_path,
+                       path_id: self.path_id,
+               })
+       }
+}
+
+/// This struct contains the information needed to reply to a received message.
+#[allow(unused)]
+pub struct OnionMessageResponse<T: OnionMessageContents> {
+       message: T,
+       reply_path: BlindedPath,
+       path_id: Option<[u8; 32]>,
+}
+
+/// `ResponseInstruction` represents instructions for responding to received messages.
+pub enum ResponseInstruction<T: OnionMessageContents> {
+       /// Indicates that a response should be sent without including a reply path
+       /// for the recipient to respond back.
+       WithoutReplyPath(OnionMessageResponse<T>),
+       /// Indicates that there's no response to send back.
+       NoResponse,
+}
+
 /// An [`OnionMessage`] for [`OnionMessenger`] to send.
 ///
 /// These are obtained when released from [`OnionMessenger`]'s handlers after which they are