Skip to content

Commit 8168e7a

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 8168e7a

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 45 additions & 11 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,17 +1012,21 @@ 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 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-
}
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 => return,
1019+
};
1020+
1021+
let message_type = response.message.msg_type();
1022+
let _ = self.find_path_and_enqueue_onion_message(
1023+
response.message, Destination::BlindedPath(response.reply_path), reply_path,
1024+
format_args!(
1025+
"when responding to {} onion message with path_id {:02x?}",
1026+
message_type,
1027+
response.path_id
1028+
)
1029+
);
9961030
}
9971031

9981032
#[cfg(test)]

0 commit comments

Comments
 (0)