Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 480
Introduce ResponseInstructions for OnionMessage Handling#2907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -64,7 +64,7 @@ use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder}; | ||
| use crate::offers::offer::{Offer, OfferBuilder}; | ||
| use crate::offers::parse::Bolt12SemanticError; | ||
| use crate::offers::refund::{Refund, RefundBuilder}; | ||
| use crate::onion_message::messenger::{Destination, MessageRouter, PendingOnionMessage, new_pending_onion_message}; | ||
| use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, Responder, ResponseInstruction}; | ||
| use crate::onion_message::offers::{OffersMessage, OffersMessageHandler}; | ||
| use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider}; | ||
| use crate::sign::ecdsa::WriteableEcdsaChannelSigner; | ||
| @@ -75,6 +75,7 @@ use crate::util::string::UntrustedString; | ||
| use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter}; | ||
| use crate::util::logger::{Level, Logger, WithContext}; | ||
| use crate::util::errors::APIError; | ||
| #[cfg(not(c_bindings))] | ||
| use { | ||
| crate::offers::offer::DerivedMetadata, | ||
| @@ -10332,23 +10333,27 @@ where | ||
| R::Target: Router, | ||
| L::Target: Logger, | ||
| { | ||
| fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> { | ||
| fn handle_message(&self, message: OffersMessage, responder: Option<Responder>) -> ResponseInstruction<OffersMessage> { | ||
| let secp_ctx = &self.secp_ctx; | ||
| let expanded_key = &self.inbound_payment_key; | ||
| match message { | ||
| OffersMessage::InvoiceRequest(invoice_request) => { | ||
| let responder = match responder { | ||
| Some(responder) => responder, | ||
| None => return ResponseInstruction::NoResponse, | ||
| }; | ||
| let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats( | ||
| &invoice_request | ||
| ) { | ||
| Ok(amount_msats) => amount_msats, | ||
| Err(error) => return Some(OffersMessage::InvoiceError(error.into())), | ||
| Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())), | ||
| }; | ||
| let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) { | ||
| Ok(invoice_request) => invoice_request, | ||
| Err(()) => { | ||
| let error = Bolt12SemanticError::InvalidMetadata; | ||
| return Some(OffersMessage::InvoiceError(error.into())); | ||
| return responder.respond(OffersMessage::InvoiceError(error.into())); | ||
| }, | ||
| }; | ||
| @@ -10359,7 +10364,7 @@ where | ||
| Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret), | ||
| Err(()) => { | ||
| let error = Bolt12SemanticError::InvalidAmount; | ||
| return Some(OffersMessage::InvoiceError(error.into())); | ||
| return responder.respond(OffersMessage::InvoiceError(error.into())); | ||
| }, | ||
| }; | ||
| @@ -10373,7 +10378,7 @@ where | ||
| Ok(payment_paths) => payment_paths, | ||
| Err(()) => { | ||
| let error = Bolt12SemanticError::MissingPaths; | ||
| return Some(OffersMessage::InvoiceError(error.into())); | ||
| return responder.respond(OffersMessage::InvoiceError(error.into())); | ||
| }, | ||
| }; | ||
| @@ -10418,8 +10423,8 @@ where | ||
| }; | ||
shaavan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| match response { | ||
| Ok(invoice) => Some(OffersMessage::Invoice(invoice)), | ||
| Err(error) => Some(OffersMessage::InvoiceError(error.into())), | ||
| Ok(invoice) => return responder.respond(OffersMessage::Invoice(invoice)), | ||
| Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())), | ||
| } | ||
| }, | ||
| OffersMessage::Invoice(invoice) => { | ||
| @@ -10439,14 +10444,21 @@ where | ||
| } | ||
| }); | ||
| match response { | ||
| Ok(()) => None, | ||
| Err(e) => Some(OffersMessage::InvoiceError(e)), | ||
| match (responder, response) { | ||
| (Some(responder), Err(e)) => responder.respond(OffersMessage::InvoiceError(e)), | ||
| (None, Err(_)) => { | ||
| log_trace!( | ||
| self.logger, | ||
| "A response was generated, but there is no reply_path specified for sending the response." | ||
| ); | ||
| return ResponseInstruction::NoResponse; | ||
| } | ||
| _ => return ResponseInstruction::NoResponse, | ||
| } | ||
| }, | ||
| OffersMessage::InvoiceError(invoice_error) => { | ||
| log_trace!(self.logger, "Received invoice_error: {}", invoice_error); | ||
| None | ||
| return ResponseInstruction::NoResponse; | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -135,6 +135,7 @@ pub(super) const MAX_TIMER_TICKS: usize = 2; | ||
| /// # let your_custom_message_type = 42; | ||
| /// your_custom_message_type | ||
| /// } | ||
| /// fn msg_type(&self) -> &'static str { "YourCustomMessageType" } | ||
| /// } | ||
| /// // Send a custom onion message to a node id. | ||
| /// let destination = Destination::Node(destination_node_id); | ||
| @@ -246,6 +247,50 @@ impl OnionMessageRecipient { | ||
| } | ||
| } | ||
| /// The `Responder` struct creates an appropriate [`ResponseInstruction`] | ||
| /// for responding to a message. | ||
| pub struct Responder { | ||
| /// The path along which a response can be sent. | ||
| reply_path: BlindedPath, | ||
| path_id: Option<[u8; 32]> | ||
| } | ||
| impl Responder { | ||
| /// Creates a new [`Responder`] instance with the provided reply path. | ||
| fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self { | ||
| Responder { | ||
| reply_path, | ||
| path_id, | ||
| } | ||
| } | ||
| /// Creates the appropriate [`ResponseInstruction`] for a given response. | ||
| pub fn respond<T: OnionMessageContents>(self, response: T) -> ResponseInstruction<T> { | ||
| ResponseInstruction::WithoutReplyPath(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. | ||
| pub struct OnionMessageResponse<T: OnionMessageContents> { | ||
| message: T, | ||
| reply_path: BlindedPath, | ||
| path_id: Option<[u8; 32]>, | ||
| } | ||
| /// `ResponseInstruction` represents instructions for responding to received messages. | ||
| pub enum ResponseInstruction<T: OnionMessageContents> { | ||
shaavan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /// Indicates that a response should be sent without including a reply path | ||
| /// for the recipient to respond back. | ||
| WithoutReplyPath(OnionMessageResponse<T>), | ||
| /// Indicates that there's no response to send back. | ||
| NoResponse, | ||
| } | ||
| /// An [`OnionMessage`] for [`OnionMessenger`] to send. | ||
| /// | ||
| /// These are obtained when released from [`OnionMessenger`]'s handlers after which they are | ||
| @@ -546,7 +591,7 @@ pub trait CustomOnionMessageHandler { | ||
| /// Called with the custom message that was received, returning a response to send, if any. | ||
| /// | ||
| /// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`]. | ||
| fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage>; | ||
| fn handle_custom_message(&self, message: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage>; | ||
| /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the | ||
| /// message type is unknown. | ||
| @@ -933,19 +978,18 @@ where | ||
| } | ||
| fn handle_onion_message_response<T: OnionMessageContents>( | ||
| &self, response: Option<T>, reply_path: Option<BlindedPath>, log_suffix: fmt::Arguments | ||
| &self, response: ResponseInstruction<T> | ||
| ) { | ||
| if let Some(response) = response { | ||
| match reply_path { | ||
| Some(reply_path) => { | ||
| let _ = self.find_path_and_enqueue_onion_message( | ||
| response, Destination::BlindedPath(reply_path), None, log_suffix | ||
| ); | ||
| }, | ||
| None => { | ||
| log_trace!(self.logger, "Missing reply path {}", log_suffix); | ||
| }, | ||
| } | ||
| if let ResponseInstruction::WithoutReplyPath(response) = response { | ||
| let message_type = response.message.msg_type(); | ||
| let _ = self.find_path_and_enqueue_onion_message( | ||
| response.message, Destination::BlindedPath(response.reply_path), None, | ||
| format_args!( | ||
| "when responding with {} to an onion message with path_id {:02x?}", | ||
| message_type, | ||
| response.path_id | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| @@ -1029,22 +1073,18 @@ where | ||
| match message { | ||
| ParsedOnionMessageContents::Offers(msg) => { | ||
| let response = self.offers_handler.handle_message(msg); | ||
| self.handle_onion_message_response( | ||
| response, reply_path, format_args!( | ||
| "when responding to Offers onion message with path_id {:02x?}", | ||
| path_id | ||
| ) | ||
| let responder = reply_path.map( | ||
| |reply_path| Responder::new(reply_path, path_id) | ||
| ); | ||
| let response_instructions = self.offers_handler.handle_message(msg, responder); | ||
| self.handle_onion_message_response(response_instructions); | ||
| }, | ||
| ParsedOnionMessageContents::Custom(msg) => { | ||
| let response = self.custom_handler.handle_custom_message(msg); | ||
| self.handle_onion_message_response( | ||
| response, reply_path, format_args!( | ||
| "when responding to Custom onion message with path_id {:02x?}", | ||
| path_id | ||
| ) | ||
| let responder = reply_path.map( | ||
| |reply_path| Responder::new(reply_path, path_id) | ||
| ); | ||
| let response_instructions = self.custom_handler.handle_custom_message(msg, responder); | ||
| self.handle_onion_message_response(response_instructions); | ||
| }, | ||
| } | ||
| }, | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.