Implement buffering onion messages for peers.
authorValentine Wallace <vwallace@protonmail.com>
Thu, 25 Aug 2022 18:30:29 +0000 (14:30 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 26 Aug 2022 23:03:07 +0000 (19:03 -0400)
In this commit, we check if a peer's outbound buffer has room for onion
messages, and if so pulls them from an implementer of a new trait,
OnionMessageProvider.

Makes sure channel messages are prioritized over OMs, and OMs are prioritized
over gossip.

The onion_message module remains private until further rate limiting is added.

lightning/src/ln/peer_handler.rs

index 6da0bae27d0f0f00be701c31666976edebf11552..1258026e17ec123f9e28b9047dbf65b225a8f3db 100644 (file)
@@ -411,6 +411,13 @@ impl Peer {
                        && self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
        }
 
+       /// Determines if we should push an onion message onto a peer's outbound buffer. This is checked
+       /// every time the peer's buffer may have been drained.
+       fn should_buffer_onion_message(&self) -> bool {
+               self.pending_outbound_buffer.is_empty()
+                       && self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
+       }
+
        /// Determines if we should push additional gossip broadcast messages onto a peer's outbound
        /// buffer. This is checked every time the peer's buffer may have been drained.
        fn should_buffer_gossip_broadcast(&self) -> bool {
@@ -766,6 +773,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
 
        fn do_attempt_write_data(&self, descriptor: &mut Descriptor, peer: &mut Peer) {
                while !peer.awaiting_write_event {
+                       if peer.should_buffer_onion_message() {
+                               if let Some(peer_node_id) = peer.their_node_id {
+                                       if let Some(next_onion_message) =
+                                               self.message_handler.onion_message_handler.next_onion_message_for_peer(peer_node_id) {
+                                                       self.enqueue_message(peer, &next_onion_message);
+                                       }
+                               }
+                       }
                        if peer.should_buffer_gossip_broadcast() {
                                if let Some(msg) = peer.gossip_broadcast_buffer.pop_front() {
                                        peer.pending_outbound_buffer.push_back(msg);