From: Valentine Wallace Date: Fri, 22 Mar 2024 19:58:33 +0000 (-0400) Subject: OnionMessenger: support generating peer connection events. X-Git-Tag: v0.0.124-beta~125^2~7 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=1c28cc071300005d9766fca3a402246973a811a5;p=rust-lightning OnionMessenger: support generating peer connection events. Useful if we are in the mode of interception OMs for offline peers, so users know when to re-inject intercepted OMs. --- diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index 0fe1ae583..09c52bd2a 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -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. diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 87faef928..d956b392a 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -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); }