Skip to content

Commit e640951

Browse files
committed
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.
1 parent bd3cc00 commit e640951

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,51 @@ impl OnionMessageRecipient {
246246
}
247247
}
248248

249+
250+
/// The `Responder` struct creates an appropriate [`ResponseInstruction`]
251+
/// for responding to a message.
252+
pub struct Responder {
253+
/// The path along which a response can be sent.
254+
reply_path: BlindedPath,
255+
path_id: Option<[u8; 32]>
256+
}
257+
258+
impl Responder {
259+
/// Creates a new [`Responder`] instance with the provided reply path.
260+
fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
261+
Responder {
262+
reply_path,
263+
path_id,
264+
}
265+
}
266+
267+
/// Creates the appropriate [`ResponseInstruction`] for a given response.
268+
pub fn respond<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
269+
ResponseInstruction::WithoutReplyPath(OnionMessageResponse {
270+
message: response,
271+
reply_path: self.reply_path,
272+
path_id: self.path_id,
273+
})
274+
}
275+
}
276+
277+
/// This struct contains the information needed to reply to a received message.
278+
#[allow(unused)]
279+
pub struct OnionMessageResponse<T: OnionMessageContents> {
280+
message: T,
281+
reply_path: BlindedPath,
282+
path_id: Option<[u8; 32]>,
283+
}
284+
285+
/// `ResponseInstruction` represents instructions for responding to received messages.
286+
pub enum ResponseInstruction<T: OnionMessageContents> {
287+
/// Indicates that a response should be sent without including a reply path
288+
/// for the recipient to respond back.
289+
WithoutReplyPath(OnionMessageResponse<T>),
290+
/// Indicates that there's no response to send back.
291+
NoResponse,
292+
}
293+
249294
/// An [`OnionMessage`] for [`OnionMessenger`] to send.
250295
///
251296
/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are

0 commit comments

Comments
 (0)