Skip to content

Commit 257700b

Browse files
committed
OnionMessageHandler::release_pending_connections
An OnionMessageHandler may buffer messages that can't be sent because the recipient is not a peer. Expand the interface with a method for returning any nodes that fall into this category so that an Event::ConnectionNeeded can be generated. Also, implement the new interface for OnionMessenger.
1 parent a892b7c commit 257700b

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

lightning/src/ln/msgs.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,17 @@ pub trait OnionMessageHandler {
15801580
/// drop and refuse to forward onion messages to this peer.
15811581
fn peer_disconnected(&self, their_node_id: &PublicKey);
15821582

1583+
/// Returns nodes that have messages buffered for calls to [`Self::next_onion_message_for_peer`]
1584+
/// but aren't connected as peers. Used to determine if an [`Event::ConnectionNeeded`] should be
1585+
/// generated.
1586+
///
1587+
/// Subsequent calls should not include the same nodes unless it's appropriate to generate
1588+
/// another event (e.g., if the implementation drops the buffered messages after a timeout but
1589+
/// more messages are then buffered).
1590+
///
1591+
/// [`Event::ConnectionNeeded`]: crate::events::Event::ConnectionNeeded
1592+
fn release_pending_connections(&self) -> Vec<NodeAnnouncement> { vec![] }
1593+
15831594
// Handler information:
15841595
/// Gets the node feature flags which this handler itself supports. All available handlers are
15851596
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]

lightning/src/onion_message/messenger.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
2222
#[cfg(not(c_bindings))]
2323
use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
2424
use crate::ln::features::{InitFeatures, NodeFeatures};
25-
use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler};
25+
use crate::ln::msgs::{self, NodeAnnouncement, OnionMessage, OnionMessageHandler};
2626
use crate::ln::onion_utils;
2727
use crate::ln::peer_handler::IgnoringMessageHandler;
2828
use crate::routing::gossip::{NetworkGraph, NodeId};
@@ -167,21 +167,21 @@ enum OnionMessageBuffer {
167167
ConnectedPeer(VecDeque<OnionMessage>),
168168

169169
/// Messages for a node that is not yet connected.
170-
PendingConnection(VecDeque<OnionMessage>),
170+
PendingConnection(VecDeque<OnionMessage>, Option<NodeAnnouncement>),
171171
}
172172

173173
impl OnionMessageBuffer {
174174
fn pending_messages(&self) -> &VecDeque<OnionMessage> {
175175
match self {
176176
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
177-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
177+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
178178
}
179179
}
180180

181181
fn enqueue_message(&mut self, message: OnionMessage) {
182182
let pending_messages = match self {
183183
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
184-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
184+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
185185
};
186186

187187
pending_messages.push_back(message);
@@ -190,7 +190,7 @@ impl OnionMessageBuffer {
190190
fn dequeue_message(&mut self) -> Option<OnionMessage> {
191191
let pending_messages = match self {
192192
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
193-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
193+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
194194
};
195195

196196
pending_messages.pop_front()
@@ -200,14 +200,14 @@ impl OnionMessageBuffer {
200200
fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
201201
let pending_messages = match self {
202202
OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
203-
OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
203+
OnionMessageBuffer::PendingConnection(pending_messages, _) => pending_messages,
204204
};
205205

206206
core::mem::take(pending_messages)
207207
}
208208

209209
fn mark_connected(&mut self) {
210-
if let OnionMessageBuffer::PendingConnection(pending_messages) = self {
210+
if let OnionMessageBuffer::PendingConnection(pending_messages, _) = self {
211211
let mut new_pending_messages = VecDeque::new();
212212
core::mem::swap(pending_messages, &mut new_pending_messages);
213213
*self = OnionMessageBuffer::ConnectedPeer(new_pending_messages);
@@ -640,22 +640,36 @@ where
640640
},
641641
};
642642

643-
log_trace!(self.logger, "Sending onion message {}", log_suffix);
644-
645-
if let Err(e) = self.send_onion_message(path, contents, reply_path) {
646-
if let SendError::InvalidFirstHop(first_node_id, onion_message) = e {
647-
// TODO: Check NetworkGraph for node id and onion message support before enqueuing
648-
self.message_buffers.lock().unwrap()
649-
.entry(first_node_id)
650-
.or_insert_with(|| OnionMessageBuffer::PendingConnection(VecDeque::new()))
651-
.enqueue_message(onion_message);
643+
match self.send_onion_message(path, contents, reply_path) {
644+
Err(SendError::InvalidFirstHop(first_node_id, onion_message)) => {
645+
let network_graph = self.network_graph.deref().read_only();
646+
let node_announcement = network_graph
647+
.node(&NodeId::from_pubkey(&first_node_id))
648+
.and_then(|node_info| node_info.announcement_info.as_ref())
649+
.and_then(|announcement_info| announcement_info.announcement_message.as_ref());
650+
651+
if let Some(node_announcement) = node_announcement {
652+
if node_announcement.contents.features.supports_onion_messages() {
653+
self.message_buffers.lock().unwrap()
654+
.entry(first_node_id)
655+
.or_insert_with(|| OnionMessageBuffer::PendingConnection(VecDeque::new(), Some(node_announcement.clone())))
656+
.enqueue_message(onion_message);
657+
log_trace!(
658+
self.logger, "Buffered onion message waiting on peer connection {}: {:?}",
659+
log_suffix, first_node_id
660+
);
661+
return;
662+
}
663+
}
652664
log_trace!(
653-
self.logger, "Buffered onion message waiting on peer connection {}: {:?}",
654-
log_suffix, first_node_id
665+
self.logger, "Failed sending onion message {}: {:?}", log_suffix,
666+
SendError::InvalidFirstHop(first_node_id, onion_message)
655667
);
656-
} else {
668+
},
669+
Err(e) => {
657670
log_trace!(self.logger, "Failed sending onion message {}: {:?}", log_suffix, e);
658-
}
671+
},
672+
Ok(_) => log_trace!(self.logger, "Sending onion message {}", log_suffix),
659673
}
660674
}
661675

@@ -782,6 +796,18 @@ where
782796
self.message_buffers.lock().unwrap().remove(their_node_id);
783797
}
784798

799+
fn release_pending_connections(&self) -> Vec<NodeAnnouncement> {
800+
self.message_buffers.lock().unwrap()
801+
.values_mut()
802+
.filter_map(|recipient| match recipient {
803+
OnionMessageBuffer::PendingConnection(_, node_announcement) => {
804+
node_announcement.take()
805+
},
806+
_ => None,
807+
})
808+
.collect()
809+
}
810+
785811
fn provided_node_features(&self) -> NodeFeatures {
786812
let mut features = NodeFeatures::empty();
787813
features.set_onion_messages_optional();

0 commit comments

Comments
 (0)