From: shaavan Date: Mon, 25 Mar 2024 08:42:47 +0000 (+0530) Subject: Introduce ResponseInstructions for OnionMessage Handling X-Git-Tag: v0.0.124-beta~127^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=e640951f9532a0b90099e04bca03db37e115ee1c;p=rust-lightning Introduce ResponseInstructions for OnionMessage Handling - 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. --- diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 1d7a730fa..647239743 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -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(self, response: T) -> ResponseInstruction { + 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 { + message: T, + reply_path: BlindedPath, + path_id: Option<[u8; 32]>, +} + +/// `ResponseInstruction` represents instructions for responding to received messages. +pub enum ResponseInstruction { + /// Indicates that a response should be sent without including a reply path + /// for the recipient to respond back. + WithoutReplyPath(OnionMessageResponse), + /// 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