]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Support generating events when an OM for an offline peer is received.
authorValentine Wallace <vwallace@protonmail.com>
Thu, 21 Mar 2024 17:56:54 +0000 (13:56 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Thu, 9 May 2024 19:29:29 +0000 (15:29 -0400)
Docs will be added in upcoming commits.

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

index f6e7f7164874f0de97fb56a8dc3c4294d2206c94..0fe1ae5834838f35024ef95bf114cbc42abc463c 100644 (file)
@@ -1041,6 +1041,18 @@ pub enum Event {
        ///
        /// [`ChannelHandshakeConfig::negotiate_anchors_zero_fee_htlc_tx`]: crate::util::config::ChannelHandshakeConfig::negotiate_anchors_zero_fee_htlc_tx
        BumpTransaction(BumpTransactionEvent),
+       /// We received an onion message that is intended to be forwarded to a peer
+       /// that is currently offline. 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
+       OnionMessageIntercepted {
+               /// The node id of the offline peer.
+               peer_node_id: PublicKey,
+               /// The onion message intended to be forwarded to `peer_node_id`.
+               message: msgs::OnionMessage,
+       },
 }
 
 impl Writeable for Event {
@@ -1286,6 +1298,13 @@ impl Writeable for Event {
                                35u8.write(writer)?;
                                // Never write ConnectionNeeded events as buffered onion messages aren't serialized.
                        },
+                       &Event::OnionMessageIntercepted { ref peer_node_id, ref message } => {
+                               37u8.write(writer)?;
+                               write_tlv_fields!(writer, {
+                                       (0, peer_node_id, required),
+                                       (2, message, 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
                        // data via `write_tlv_fields`.
@@ -1697,6 +1716,18 @@ impl MaybeReadable for Event {
                        },
                        // Note that we do not write a length-prefixed TLV for ConnectionNeeded events.
                        35u8 => Ok(None),
+                       37u8 => {
+                               let mut f = || {
+                                       _init_and_read_len_prefixed_tlv_fields!(reader, {
+                                               (0, peer_node_id, required),
+                                               (2, message, required),
+                                       });
+                                       Ok(Some(Event::OnionMessageIntercepted {
+                                               peer_node_id: peer_node_id.0.unwrap(), message: message.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 1d7a730fa3625126097fd6d25de150e5ba46c742..87faef9288da438b94a4367ac3d9c1f3f23174ec 100644 (file)
@@ -175,6 +175,8 @@ where
        message_router: MR,
        offers_handler: OMH,
        custom_handler: CMH,
+       intercept_messages_for_offline_peers: bool,
+       pending_events: Mutex<Vec<Event>>,
 }
 
 /// [`OnionMessage`]s buffered to be sent.
@@ -796,6 +798,28 @@ where
        pub fn new(
                entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL, message_router: MR,
                offers_handler: OMH, custom_handler: CMH
+       ) -> Self {
+               Self::new_inner(
+                       entropy_source, node_signer, logger, node_id_lookup, message_router,
+                       offers_handler, custom_handler, false
+               )
+       }
+
+       ///
+       pub fn new_with_offline_peer_interception(
+               entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL,
+               message_router: MR, offers_handler: OMH, custom_handler: CMH
+       ) -> Self {
+               Self::new_inner(
+                       entropy_source, node_signer, logger, node_id_lookup, message_router,
+                       offers_handler, custom_handler, true
+               )
+       }
+
+       fn new_inner(
+               entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL,
+               message_router: MR, offers_handler: OMH, custom_handler: CMH,
+               intercept_messages_for_offline_peers: bool
        ) -> Self {
                let mut secp_ctx = Secp256k1::new();
                secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
@@ -809,6 +833,8 @@ where
                        message_router,
                        offers_handler,
                        custom_handler,
+                       intercept_messages_for_offline_peers,
+                       pending_events: Mutex::new(Vec::new()),
                }
        }
 
@@ -1004,6 +1030,11 @@ where
                                }
                        }
                }
+               let mut events = Vec::new();
+               core::mem::swap(&mut *self.pending_events.lock().unwrap(), &mut events);
+               for ev in events {
+                       handler.handle_event(ev);
+               }
        }
 }
 
@@ -1081,6 +1112,13 @@ where
                                                e.get_mut().enqueue_message(onion_message);
                                                log_trace!(logger, "Forwarding an onion message to peer {}", next_node_id);
                                        },
+                                       _ if self.intercept_messages_for_offline_peers => {
+                                               self.pending_events.lock().unwrap().push(
+                                                       Event::OnionMessageIntercepted {
+                                                               peer_node_id: next_node_id, message: onion_message
+                                                       }
+                                               );
+                                       },
                                        _ => {
                                                log_trace!(
                                                        logger,