Skip to content

Commit

Permalink
Introduce ResponseInstruction::WithReplyPath variant.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shaavan committed Apr 23, 2024
1 parent 8af1a8a commit de4c0d0
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ impl Responder {
path_id: self.path_id,
})
}

/// Creates the appropriate [`ResponseInstruction`] for a given response.
pub fn respond_with_reply_path<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> {
ResponseInstruction::WithReplyPath(OnionMessageResponse {
message: response,
reply_path: self.reply_path,
path_id: self.path_id,
})
}
}

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

/// `ResponseInstruction` represents instructions for responding to received messages.
pub enum ResponseInstruction<T: OnionMessageContents> {
/// Indicates that a response should be sent including a reply path for
/// the recipient to respond back.
WithReplyPath(OnionMessageResponse<T>),
/// Indicates that a response should be sent without including a reply path
/// for the recipient to respond back.
WithoutReplyPath(OnionMessageResponse<T>),
Expand Down Expand Up @@ -926,6 +938,24 @@ where
.map_err(|_| SendError::PathNotFound)
}

fn create_blinded_path(&self) -> Result<BlindedPath, SendError> {
let recipient = self.node_signer
.get_node_id(Recipient::Node)
.map_err(|_| SendError::GetNodeIdFailed)?;
let secp_ctx = &self.secp_ctx;

let peers = self.message_recipients.lock().unwrap()
.iter()
.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
.map(|(node_id, _)| *node_id)
.collect();

self.message_router
.create_blinded_paths(recipient, peers, secp_ctx)
.and_then(|paths| paths.into_iter().next().ok_or(()))
.map_err(|_| SendError::PathNotFound)
}

fn enqueue_onion_message<T: OnionMessageContents>(
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
log_suffix: fmt::Arguments
Expand Down Expand Up @@ -982,16 +1012,26 @@ where
pub fn handle_onion_message_response<T: OnionMessageContents>(
&self, response: ResponseInstruction<T>
) {
if let ResponseInstruction::WithoutReplyPath(response) = response {
let _ = self.find_path_and_enqueue_onion_message(
response.message, Destination::BlindedPath(response.reply_path), None,
format_args!(
"when responding to {} onion message with path_id {:02x?}",
T::msg_type(),
response.path_id
)
);
}
let (response, reply_path) = match response {
ResponseInstruction::WithReplyPath(response) => (response, self.create_blinded_path().ok()),
ResponseInstruction::WithoutReplyPath(response) => (response, None),
ResponseInstruction::NoResponse => {
log_trace!(self.logger,
"Missing reply path when responding to {} onion message",
T::msg_type()
);
return
}
};

let _ = self.find_path_and_enqueue_onion_message(
response.message, Destination::BlindedPath(response.reply_path), reply_path,
format_args!(
"when responding to {} onion message with path_id {:02x?}",
T::msg_type(),
response.path_id
)
);
}

#[cfg(test)]
Expand Down

0 comments on commit de4c0d0

Please sign in to comment.