Onion message routing to immediate peers.
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 19 Oct 2023 20:50:19 +0000 (15:50 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 20 Oct 2023 14:49:58 +0000 (09:49 -0500)
DefaultMessageRouter always fails. Update it so that it can route to a
directly connected peer. This is needed for an Offers minimum viable
product.

lightning/src/ln/channelmanager.rs
lightning/src/onion_message/messenger.rs

index 13b0a815ec148693491b35c43e91feea9e06399c..e71d7124aaafcfb16492006d7ad19bfc8ee90f58 100644 (file)
@@ -7325,6 +7325,11 @@ where
        /// the node must be announced. Otherwise, there is no way to find a path to the introduction
        /// node in order to send the [`InvoiceRequest`].
        ///
+       /// # Limitations
+       ///
+       /// Requires a direct connection to the introduction node in the responding [`InvoiceRequest`]'s
+       /// reply path.
+       ///
        /// [`Offer`]: crate::offers::offer::Offer
        /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
        pub fn create_offer_builder(
@@ -7365,6 +7370,11 @@ where
        /// node must be announced. Otherwise, there is no way to find a path to the introduction node
        /// in order to send the [`Bolt12Invoice`].
        ///
+       /// # Limitations
+       ///
+       /// Requires a direct connection to an introduction node in the responding
+       /// [`Bolt12Invoice::payment_paths`].
+       ///
        /// # Errors
        ///
        /// Errors if a duplicate `payment_id` is provided given the caveats in the aforementioned link
@@ -7372,6 +7382,7 @@ where
        ///
        /// [`Refund`]: crate::offers::refund::Refund
        /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
+       /// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
        pub fn create_refund_builder(
                &self, description: String, amount_msats: u64, absolute_expiry: Duration,
                payment_id: PaymentId, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
@@ -7428,6 +7439,12 @@ where
        /// node must be announced. Otherwise, there is no way to find a path to the introduction node
        /// in order to send the [`Bolt12Invoice`].
        ///
+       /// # Limitations
+       ///
+       /// Requires a direct connection to an introduction node in [`Offer::paths`] or to
+       /// [`Offer::signing_pubkey`], if empty. A similar restriction applies to the responding
+       /// [`Bolt12Invoice::payment_paths`].
+       ///
        /// # Errors
        ///
        /// Errors if a duplicate `payment_id` is provided given the caveats in the aforementioned link
@@ -7438,6 +7455,7 @@ where
        /// [`InvoiceRequest::payer_note`]: crate::offers::invoice_request::InvoiceRequest::payer_note
        /// [`InvoiceRequestBuilder`]: crate::offers::invoice_request::InvoiceRequestBuilder
        /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
+       /// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
        /// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
        pub fn pay_for_offer(
                &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
@@ -7507,6 +7525,11 @@ where
        /// [`BlindedPath`] containing the [`PaymentSecret`] needed to reconstruct the corresponding
        /// [`PaymentPreimage`].
        ///
+       /// # Limitations
+       ///
+       /// Requires a direct connection to an introduction node in [`Refund::paths`] or to
+       /// [`Refund::payer_id`], if empty.
+       ///
        /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
        pub fn request_refund_payment(&self, refund: &Refund) -> Result<(), Bolt12SemanticError> {
                let expanded_key = &self.inbound_payment_key;
index ba343ec6eae8a026f6df8ce7aeab07d9cd2f2549..723e105d044cf549cfd8f392e206782b38a479af 100644 (file)
@@ -177,14 +177,18 @@ pub trait MessageRouter {
        ) -> Result<OnionMessagePath, ()>;
 }
 
-/// A [`MessageRouter`] that always fails.
+/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
 pub struct DefaultMessageRouter;
 
 impl MessageRouter for DefaultMessageRouter {
        fn find_path(
-               &self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination
+               &self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
        ) -> Result<OnionMessagePath, ()> {
-               Err(())
+               if peers.contains(&destination.first_node()) {
+                       Ok(OnionMessagePath { intermediate_nodes: vec![], destination })
+               } else {
+                       Err(())
+               }
        }
 }
 
@@ -214,6 +218,13 @@ impl Destination {
                        Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => blinded_hops.len(),
                }
        }
+
+       fn first_node(&self) -> PublicKey {
+               match self {
+                       Destination::Node(node_id) => *node_id,
+                       Destination::BlindedPath(BlindedPath { introduction_node_id: node_id, .. }) => *node_id,
+               }
+       }
 }
 
 /// Errors that may occur when [sending an onion message].