From: Valentine Wallace Date: Thu, 21 Mar 2024 17:56:54 +0000 (-0400) Subject: Support generating events when an OM for an offline peer is received. X-Git-Tag: v0.0.124-beta~125^2~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e8f7fe1919f43de9c7da88f8fbe1854d9e88c7e4;p=rust-lightning Support generating events when an OM for an offline peer is received. Docs will be added in upcoming commits. --- diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index f6e7f7164..0fe1ae583 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -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. diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 1d7a730fa..87faef928 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -175,6 +175,8 @@ where message_router: MR, offers_handler: OMH, custom_handler: CMH, + intercept_messages_for_offline_peers: bool, + pending_events: Mutex>, } /// [`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,