Buffer onion messages requiring a connection
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 7 Nov 2023 14:17:46 +0000 (08:17 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 6 Dec 2023 14:47:33 +0000 (08:47 -0600)
MessageRouter::find_path returns a path to use when sending an onion
message. If the first node on the path is not connected or does not
support onion messages, sending will fail with InvalidFirstHop. Instead
of failing outright, buffer the message for later sending once the first
node is a connected peer.

lightning/src/onion_message/messenger.rs

index 8d98e284e954b15032232b04e2fe7d4156339b7f..c84cfc12079b6e299db056f2676813f74e0eaf37 100644 (file)
@@ -313,6 +313,9 @@ pub enum SendSuccess {
        /// The message was buffered and will be sent once it is processed by
        /// [`OnionMessageHandler::next_onion_message_for_peer`].
        Buffered,
+       /// The message was buffered and will be sent once the node is connected as a peer and it is
+       /// processed by [`OnionMessageHandler::next_onion_message_for_peer`].
+       BufferedAwaitingConnection(PublicKey),
 }
 
 /// Errors that may occur when [sending an onion message].
@@ -328,8 +331,6 @@ pub enum SendError {
        /// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded
        /// hops.
        TooFewBlindedHops,
-       /// Our next-hop peer was offline or does not support onion message forwarding.
-       InvalidFirstHop,
        /// A path from the sender to the destination could not be found by the [`MessageRouter`].
        PathNotFound,
        /// Onion message contents must have a TLV type >= 64.
@@ -612,6 +613,12 @@ where
                        Ok(SendSuccess::Buffered) => {
                                log_trace!(self.logger, "Buffered onion message {}", log_suffix);
                        },
+                       Ok(SendSuccess::BufferedAwaitingConnection(node_id)) => {
+                               log_trace!(
+                                       self.logger, "Buffered onion message waiting on peer connection {}: {:?}",
+                                       log_suffix, node_id
+                               );
+                       },
                }
 
                result
@@ -649,7 +656,11 @@ where
                }
 
                match message_buffers.entry(first_node_id) {
-                       hash_map::Entry::Vacant(_) => Err(SendError::InvalidFirstHop),
+                       hash_map::Entry::Vacant(e) => {
+                               e.insert(OnionMessageBuffer::PendingConnection(VecDeque::new()))
+                                       .enqueue_message(onion_message);
+                               Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
+                       },
                        hash_map::Entry::Occupied(mut e) => {
                                e.get_mut().enqueue_message(onion_message);
                                Ok(SendSuccess::Buffered)