Skip to content

Commit de4c0d0

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 8af1a8a commit de4c0d0

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ impl Responder {
273273
path_id: self.path_id,
274274
})
275275
}
276+
277+
/// Creates the appropriate [`ResponseInstruction`] for a given response.
278+
pub fn respond_with_reply_path<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
279+
ResponseInstruction::WithReplyPath(OnionMessageResponse {
280+
message: response,
281+
reply_path: self.reply_path,
282+
path_id: self.path_id,
283+
})
284+
}
276285
}
277286

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

285294
/// `ResponseInstruction` represents instructions for responding to received messages.
286295
pub enum ResponseInstruction<T: OnionMessageContents> {
296+
/// Indicates that a response should be sent including a reply path for
297+
/// the recipient to respond back.
298+
WithReplyPath(OnionMessageResponse<T>),
287299
/// Indicates that a response should be sent without including a reply path
288300
/// for the recipient to respond back.
289301
WithoutReplyPath(OnionMessageResponse<T>),
@@ -926,6 +938,24 @@ where
926938
.map_err(|_| SendError::PathNotFound)
927939
}
928940

941+
fn create_blinded_path(&self) -> Result<BlindedPath, SendError> {
942+
let recipient = self.node_signer
943+
.get_node_id(Recipient::Node)
944+
.map_err(|_| SendError::GetNodeIdFailed)?;
945+
let secp_ctx = &self.secp_ctx;
946+
947+
let peers = self.message_recipients.lock().unwrap()
948+
.iter()
949+
.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
950+
.map(|(node_id, _)| *node_id)
951+
.collect();
952+
953+
self.message_router
954+
.create_blinded_paths(recipient, peers, secp_ctx)
955+
.and_then(|paths| paths.into_iter().next().ok_or(()))
956+
.map_err(|_| SendError::PathNotFound)
957+
}
958+
929959
fn enqueue_onion_message<T: OnionMessageContents>(
930960
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
931961
log_suffix: fmt::Arguments
@@ -982,16 +1012,26 @@ where
9821012
pub fn handle_onion_message_response<T: OnionMessageContents>(
9831013
&self, response: ResponseInstruction<T>
9841014
) {
985-
if let ResponseInstruction::WithoutReplyPath(response) = response {
986-
let _ = self.find_path_and_enqueue_onion_message(
987-
response.message, Destination::BlindedPath(response.reply_path), None,
988-
format_args!(
989-
"when responding to {} onion message with path_id {:02x?}",
990-
T::msg_type(),
991-
response.path_id
992-
)
993-
);
994-
}
1015+
let (response, reply_path) = match response {
1016+
ResponseInstruction::WithReplyPath(response) => (response, self.create_blinded_path().ok()),
1017+
ResponseInstruction::WithoutReplyPath(response) => (response, None),
1018+
ResponseInstruction::NoResponse => {
1019+
log_trace!(self.logger,
1020+
"Missing reply path when responding to {} onion message",
1021+
T::msg_type()
1022+
);
1023+
return
1024+
}
1025+
};
1026+
1027+
let _ = self.find_path_and_enqueue_onion_message(
1028+
response.message, Destination::BlindedPath(response.reply_path), reply_path,
1029+
format_args!(
1030+
"when responding to {} onion message with path_id {:02x?}",
1031+
T::msg_type(),
1032+
response.path_id
1033+
)
1034+
);
9951035
}
9961036

9971037
#[cfg(test)]

0 commit comments

Comments
 (0)