Skip to content

Commit f4834de

Browse files
Implement buffering onion messages for peers.
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.
1 parent b28783a commit f4834de

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lightning/src/ln/peer_handler.rs

+15
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ impl Peer {
411411
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
412412
}
413413

414+
/// Determines if we should push an onion message onto a peer's outbound buffer. This is checked
415+
/// every time the peer's buffer may have been drained.
416+
fn should_buffer_onion_message(&self) -> bool {
417+
self.pending_outbound_buffer.is_empty()
418+
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
419+
}
420+
414421
/// Determines if we should push additional gossip broadcast messages onto a peer's outbound
415422
/// buffer. This is checked every time the peer's buffer may have been drained.
416423
fn should_buffer_gossip_broadcast(&self) -> bool {
@@ -766,6 +773,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
766773

767774
fn do_attempt_write_data(&self, descriptor: &mut Descriptor, peer: &mut Peer) {
768775
while !peer.awaiting_write_event {
776+
if peer.should_buffer_onion_message() {
777+
if let Some(peer_node_id) = peer.their_node_id {
778+
if let Some(next_onion_message) =
779+
self.message_handler.onion_message_handler.next_onion_message_for_peer(peer_node_id) {
780+
self.enqueue_message(peer, &next_onion_message);
781+
}
782+
}
783+
}
769784
if peer.should_buffer_gossip_broadcast() {
770785
if let Some(msg) = peer.gossip_broadcast_buffer.pop_front() {
771786
peer.pending_outbound_buffer.push_back(msg);

0 commit comments

Comments
 (0)