Skip to content

Commit a70a583

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 a635241 commit a70a583

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ impl Responder {
267267
reply_path: self.reply_path
268268
})
269269
}
270+
271+
/// Creates the appropriate [`ResponseInstruction`] for a given response.
272+
pub fn respond_with_reply_path<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
273+
ResponseInstruction::WithReplyPath(OnionMessageResponse {
274+
message: response,
275+
reply_path: self.reply_path
276+
})
277+
}
270278
}
271279

272280
/// This struct contains the information needed to reply to a received message.
@@ -277,6 +285,8 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
277285

278286
/// `ResponseInstruction` represents instructions for responding to received messages.
279287
pub enum ResponseInstruction<T: OnionMessageContents> {
288+
/// Indicates that a response should be sent including a reply path for the receiver to respond back.
289+
WithReplyPath(OnionMessageResponse<T>),
280290
/// Indicates that a response should be sent without including a reply path for the receiver to respond back.
281291
WithoutReplyPath(OnionMessageResponse<T>),
282292
/// Indicates that there's no response to send back.
@@ -837,6 +847,24 @@ where
837847
.map_err(|_| SendError::PathNotFound)
838848
}
839849

850+
fn create_blinded_path(&self) -> Result<BlindedPath, SendError> {
851+
let recipient = self.node_signer
852+
.get_node_id(Recipient::Node)
853+
.map_err(|_| SendError::GetNodeIdFailed)?;
854+
let secp_ctx = &self.secp_ctx;
855+
856+
let peers = self.message_recipients.lock().unwrap()
857+
.iter()
858+
.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
859+
.map(|(node_id, _)| *node_id)
860+
.collect();
861+
862+
self.message_router
863+
.create_blinded_paths(recipient, peers, secp_ctx)
864+
.and_then(|paths| paths.into_iter().next().ok_or(()))
865+
.map_err(|_| SendError::PathNotFound)
866+
}
867+
840868
fn enqueue_onion_message<T: OnionMessageContents>(
841869
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
842870
log_suffix: fmt::Arguments
@@ -892,16 +920,26 @@ where
892920
pub fn handle_onion_message_response<T: OnionMessageContents>(
893921
&self, response: ResponseInstruction<T>, message_type: &str, path_id: Option<[u8; 32]>
894922
) {
895-
if let ResponseInstruction::WithoutReplyPath(response) = response {
896-
let _ = self.find_path_and_enqueue_onion_message(
897-
response.message, Destination::BlindedPath(response.reply_path), None,
898-
format_args!(
899-
"when responding to {} onion message with path_id {:02x?}",
900-
message_type,
901-
path_id
902-
)
903-
);
904-
}
923+
let (response, reply_path) = match response {
924+
ResponseInstruction::WithReplyPath(response) => (response, self.create_blinded_path().ok()),
925+
ResponseInstruction::WithoutReplyPath(response) => (response, None),
926+
ResponseInstruction::NoResponse => {
927+
log_trace!(self.logger,
928+
"Missing reply path when responding to {} onion message with path_id {:02x?}",
929+
message_type, path_id);
930+
return
931+
}
932+
};
933+
934+
let _ = self.find_path_and_enqueue_onion_message(
935+
response.message, Destination::BlindedPath(response.reply_path), reply_path,
936+
format_args!(
937+
"when responding to {} onion message with path_id {:02x?}",
938+
message_type,
939+
path_id
940+
)
941+
);
942+
905943
}
906944

907945
#[cfg(test)]

0 commit comments

Comments
 (0)