Skip to content

Commit 9c724ca

Browse files
committed
Introduce ResponseInstruction::WithReplyPath variant.
1. Introduce a new function in OnionMessenger to create blinded paths. 2. Use it in handle_onion_message_response to create a reply_path for the right variant and use it in onion_message.
1 parent 5178f43 commit 9c724ca

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,31 @@ impl Responder {
265265
}
266266
}
267267

268-
/// Creates the appropriate [`ResponseInstruction`] for a given response.
268+
/// Creates a [`ResponseInstruction::WithoutReplyPath`] variant of [`ResponseInstruction`]
269+
/// for a given response.
270+
///
271+
/// [`ResponseInstruction::WithoutReplyPath`] indicates that the response should be sent
272+
/// without creating a reply path for the receiver to use to send back a response to us.
269273
pub fn respond<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
270274
ResponseInstruction::WithoutReplyPath(OnionMessageResponse {
271275
message: response,
272276
reply_path: self.reply_path,
273277
path_id: self.path_id,
274278
})
275279
}
280+
281+
/// Creates a [`ResponseInstruction::WithReplyPath`] variant of [`ResponseInstruction`]
282+
/// for a given response.
283+
///
284+
/// [`ResponseInstruction::WithReplyPath`] indicates that the response should be sent with
285+
/// a reply path for the receiver to use to send back a response to us.
286+
pub fn respond_with_reply_path<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
287+
ResponseInstruction::WithReplyPath(OnionMessageResponse {
288+
message: response,
289+
reply_path: self.reply_path,
290+
path_id: self.path_id,
291+
})
292+
}
276293
}
277294

278295
/// This struct contains the information needed to reply to a received message.
@@ -284,6 +301,9 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
284301

285302
/// `ResponseInstruction` represents instructions for responding to received messages.
286303
pub enum ResponseInstruction<T: OnionMessageContents> {
304+
/// Indicates that a response should be sent including a reply path for
305+
/// the recipient to respond back.
306+
WithReplyPath(OnionMessageResponse<T>),
287307
/// Indicates that a response should be sent without including a reply path
288308
/// for the recipient to respond back.
289309
WithoutReplyPath(OnionMessageResponse<T>),
@@ -926,6 +946,24 @@ where
926946
.map_err(|_| SendError::PathNotFound)
927947
}
928948

949+
fn create_blinded_path(&self) -> Result<BlindedPath, SendError> {
950+
let recipient = self.node_signer
951+
.get_node_id(Recipient::Node)
952+
.map_err(|_| SendError::GetNodeIdFailed)?;
953+
let secp_ctx = &self.secp_ctx;
954+
955+
let peers = self.message_recipients.lock().unwrap()
956+
.iter()
957+
.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
958+
.map(|(node_id, _)| *node_id)
959+
.collect();
960+
961+
self.message_router
962+
.create_blinded_paths(recipient, peers, secp_ctx)
963+
.and_then(|paths| paths.into_iter().next().ok_or(()))
964+
.map_err(|_| SendError::PathNotFound)
965+
}
966+
929967
fn enqueue_onion_message<T: OnionMessageContents>(
930968
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
931969
log_suffix: fmt::Arguments
@@ -982,17 +1020,21 @@ where
9821020
pub fn handle_onion_message_response<T: OnionMessageContents>(
9831021
&self, response: ResponseInstruction<T>
9841022
) {
985-
if let ResponseInstruction::WithoutReplyPath(response) = response {
986-
let message_type = response.message.msg_type();
987-
let _ = self.find_path_and_enqueue_onion_message(
988-
response.message, Destination::BlindedPath(response.reply_path), None,
989-
format_args!(
990-
"when responding with {} to an onion message with path_id {:02x?}",
991-
message_type,
992-
response.path_id
993-
)
994-
);
995-
}
1023+
let (response, reply_path) = match response {
1024+
ResponseInstruction::WithReplyPath(response) => (response, self.create_blinded_path().ok()),
1025+
ResponseInstruction::WithoutReplyPath(response) => (response, None),
1026+
ResponseInstruction::NoResponse => return,
1027+
};
1028+
1029+
let message_type = response.message.msg_type();
1030+
let _ = self.find_path_and_enqueue_onion_message(
1031+
response.message, Destination::BlindedPath(response.reply_path), reply_path,
1032+
format_args!(
1033+
"when responding with {} to an onion message with path_id {:02x?}",
1034+
message_type,
1035+
response.path_id
1036+
)
1037+
);
9961038
}
9971039

9981040
#[cfg(test)]

0 commit comments

Comments
 (0)