From 2695331686f41f1ba75de0692fa1bf2ae0792e3a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 22 Aug 2024 01:35:18 +0000 Subject: [PATCH] Pull the guts of `ResponseInstruction` into a new enum In the coming commits we'll use the `ResponseInstruction` enum's contents for all messages, allowing message senders to rely on the in-`OnionMessegner` reply path selection logic. In order to do so and avoid users confusing the new `MessageSendInstructions` for `ResponseInstruction`, we leave `ResponseInstruction` as a now-unconstructible struct which wraps a `MessageSendInstructions`. --- lightning/src/onion_message/messenger.rs | 54 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 3d83e58a8..8df16599e 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -342,8 +342,8 @@ impl OnionMessageRecipient { } -/// The `Responder` struct creates an appropriate [`ResponseInstruction`] -/// for responding to a message. +/// The `Responder` struct creates an appropriate [`ResponseInstruction`] for responding to a +/// message. #[derive(Clone, Debug, Eq, PartialEq)] pub struct Responder { /// The path along which a response can be sent. @@ -362,41 +362,59 @@ impl Responder { } } - /// Creates a [`ResponseInstruction::WithoutReplyPath`] for a given response. + /// Creates a [`ResponseInstruction`] for responding without including a reply path. /// /// Use when the recipient doesn't need to send back a reply to us. pub fn respond(self) -> ResponseInstruction { - ResponseInstruction::WithoutReplyPath { + ResponseInstruction { send_path: self.reply_path, + context: None, } } - /// Creates a [`ResponseInstruction::WithReplyPath`] for a given response. + /// Creates a [`ResponseInstruction`] for responding including a reply path. /// /// Use when the recipient needs to send back a reply to us. pub fn respond_with_reply_path(self, context: MessageContext) -> ResponseInstruction { - ResponseInstruction::WithReplyPath { + ResponseInstruction { send_path: self.reply_path, - context: context, + context: Some(context), } } } -/// `ResponseInstruction` represents instructions for responding to received messages. -pub enum ResponseInstruction { - /// Indicates that a response should be sent including a reply path for - /// the recipient to respond back. +/// Instructions for how and where to send the response to an onion message. +pub struct ResponseInstruction { + send_path: BlindedMessagePath, + context: Option, +} + +impl ResponseInstruction { + fn into_instructions(self) -> MessageSendInstructions { + let send_path = self.send_path; + match self.context { + Some(context) => MessageSendInstructions::WithReplyPath { send_path, context }, + None => MessageSendInstructions::WithoutReplyPath { send_path }, + } + } +} + +/// Instructions for how and where to send a message. +#[derive(Clone)] +pub enum MessageSendInstructions { + /// Indicates that a message should be sent including a reply path for the recipient to + /// respond. WithReplyPath { - /// The path over which we'll send our reply. + /// The destination where we need to send our message. send_path: BlindedMessagePath, /// The context to include in the reply path we'll give the recipient so they can respond /// to us. context: MessageContext, }, - /// Indicates that a response should be sent without including a reply path - /// for the recipient to respond back. + /// Indicates that a message should be sent without including a reply path, preventing the + /// recipient from responding. WithoutReplyPath { - /// The path over which we'll send our reply. + /// The destination where we need to send our message. send_path: BlindedMessagePath, } } @@ -1323,9 +1341,9 @@ where pub fn handle_onion_message_response( &self, response: T, instructions: ResponseInstruction, ) -> Result, SendError> { - let (response_path, context) = match instructions { - ResponseInstruction::WithReplyPath { send_path, context } => (send_path, Some(context)), - ResponseInstruction::WithoutReplyPath { send_path } => (send_path, None), + let (response_path, context) = match instructions.into_instructions() { + MessageSendInstructions::WithReplyPath { send_path, context } => (send_path, Some(context)), + MessageSendInstructions::WithoutReplyPath { send_path } => (send_path, None), }; let message_type = response.msg_type(); -- 2.39.5