]> git.bitcoin.ninja Git - rust-lightning/commitdiff
OnionMessenger: support generating peer connection events.
authorValentine Wallace <vwallace@protonmail.com>
Fri, 22 Mar 2024 19:58:33 +0000 (15:58 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Thu, 9 May 2024 19:29:34 +0000 (15:29 -0400)
Useful if we are in the mode of interception OMs for offline peers, so
users know when to re-inject intercepted OMs.

lightning/src/events/mod.rs
lightning/src/onion_message/messenger.rs

index 0fe1ae5834838f35024ef95bf114cbc42abc463c..09c52bd2a3f6fe0e29e61664bf76bc39868f8eac 100644 (file)
@@ -1053,6 +1053,18 @@ pub enum Event {
                /// The onion message intended to be forwarded to `peer_node_id`.
                message: msgs::OnionMessage,
        },
+       /// Indicates that an onion message supporting peer has come online and it may
+       /// be time to forward any onion messages that were previously intercepted for
+       /// them. This event will only be generated if the `OnionMessenger` was
+       /// initialized with
+       /// [`OnionMessenger::new_with_offline_peer_interception`], see its docs.
+       ///
+       /// [`OnionMessenger::new_with_offline_peer_interception`]: crate::onion_message::messenger::OnionMessenger::new_with_offline_peer_interception
+       OnionMessagePeerConnected {
+               /// The node id of the peer we just connected to, who advertises support for
+               /// onion messages.
+               peer_node_id: PublicKey,
+       }
 }
 
 impl Writeable for Event {
@@ -1304,6 +1316,12 @@ impl Writeable for Event {
                                        (0, peer_node_id, required),
                                        (2, message, required),
                                });
+                       },
+                       &Event::OnionMessagePeerConnected { ref peer_node_id } => {
+                               39u8.write(writer)?;
+                               write_tlv_fields!(writer, {
+                                       (0, peer_node_id, required),
+                               });
                        }
                        // 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
@@ -1728,6 +1746,17 @@ impl MaybeReadable for Event {
                                };
                                f()
                        },
+                       39u8 => {
+                               let mut f = || {
+                                       _init_and_read_len_prefixed_tlv_fields!(reader, {
+                                               (0, peer_node_id, required),
+                                       });
+                                       Ok(Some(Event::OnionMessagePeerConnected {
+                                               peer_node_id: peer_node_id.0.unwrap()
+                                       }))
+                               };
+                               f()
+                       },
                        // 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.
index 87faef9288da438b94a4367ac3d9c1f3f23174ec..d956b392a39631a6ce2ca761d0b457e7bb252da0 100644 (file)
@@ -1140,6 +1140,11 @@ where
                                .entry(*their_node_id)
                                .or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()))
                                .mark_connected();
+                       if self.intercept_messages_for_offline_peers {
+                               self.pending_events.lock().unwrap().push(
+                                       Event::OnionMessagePeerConnected { peer_node_id: *their_node_id }
+                               );
+                       }
                } else {
                        self.message_recipients.lock().unwrap().remove(their_node_id);
                }