Add Event::ConnectionNeeded for onion messages
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 9 Nov 2023 17:10:23 +0000 (11:10 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 6 Dec 2023 14:47:34 +0000 (08:47 -0600)
A MessageRouter may be unable to find a complete path to an onion
message's destination. This could because no such path exists or any
needs on a potential path don't support onion messages. Add an event
that indicates a connection with a node is needed in order to send the
message.

lightning/src/events/mod.rs

index 4e04a3634e10d3f9152ae6d074bb19365bf989a4..76e5f25c0e5f6eef87b2ff48102dd19acc239231 100644 (file)
@@ -530,6 +530,25 @@ pub enum Event {
                /// serialized prior to LDK version 0.0.117.
                sender_intended_total_msat: Option<u64>,
        },
+       /// Indicates that a peer connection with a node is needed in order to send an [`OnionMessage`].
+       ///
+       /// Typically, this happens when a [`MessageRouter`] is unable to find a complete path to a
+       /// [`Destination`]. Once a connection is established, any messages buffered by an
+       /// [`OnionMessageHandler`] may be sent.
+       ///
+       /// This event will not be generated for onion message forwards; only for sends including
+       /// replies. Handlers should connect to the node otherwise any buffered messages may be lost.
+       ///
+       /// [`OnionMessage`]: msgs::OnionMessage
+       /// [`MessageRouter`]: crate::onion_message::MessageRouter
+       /// [`Destination`]: crate::onion_message::Destination
+       /// [`OnionMessageHandler`]: crate::ln::msgs::OnionMessageHandler
+       ConnectionNeeded {
+               /// The node id for the node needing a connection.
+               node_id: PublicKey,
+               /// Sockets for connecting to the node.
+               addresses: Vec<msgs::SocketAddress>,
+       },
        /// Indicates a request for an invoice failed to yield a response in a reasonable amount of time
        /// or was explicitly abandoned by [`ChannelManager::abandon_payment`]. This may be for an
        /// [`InvoiceRequest`] sent for an [`Offer`] or for a [`Refund`] that hasn't been redeemed.
@@ -1190,6 +1209,10 @@ impl Writeable for Event {
                                        (0, payment_id, required),
                                })
                        },
+                       &Event::ConnectionNeeded { .. } => {
+                               35u8.write(writer)?;
+                               // Never write ConnectionNeeded events as buffered onion messages aren't serialized.
+                       },
                        // Note that, going forward, all new events must only write data inside of
                        // `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
                        // data via `write_tlv_fields`.
@@ -1200,8 +1223,7 @@ impl Writeable for Event {
 impl MaybeReadable for Event {
        fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, msgs::DecodeError> {
                match Readable::read(reader)? {
-                       // Note that we do not write a length-prefixed TLV for FundingGenerationReady events,
-                       // unlike all other events, thus we return immediately here.
+                       // Note that we do not write a length-prefixed TLV for FundingGenerationReady events.
                        0u8 => Ok(None),
                        1u8 => {
                                let f = || {
@@ -1588,6 +1610,8 @@ impl MaybeReadable for Event {
                                };
                                f()
                        },
+                       // Note that we do not write a length-prefixed TLV for ConnectionNeeded events.
+                       35u8 => Ok(None),
                        // Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
                        // Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
                        // reads.