Skip to content

Commit 1f5fb57

Browse files
committed
Buffer onion messages requiring a connection
MessageRouter::find_path returns a path to use when sending an onion message. If the first node on the path is not connected or does not support onion messages, sending will fail with InvalidFirstHop. Instead of failing outright, buffer the message for later sending once the first node is a connected peer.
1 parent aab3970 commit 1f5fb57

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ pub enum SendSuccess {
311311
/// The message was buffered and will be sent once it is processed by
312312
/// [`OnionMessageHandler::next_onion_message_for_peer`].
313313
Buffered,
314+
/// The message was buffered and will be sent once the node is connected as a peer and it is
315+
/// processed by [`OnionMessageHandler::next_onion_message_for_peer`].
316+
BufferedAwaitingConnection(PublicKey),
314317
}
315318

316319
/// Errors that may occur when [sending an onion message].
@@ -326,8 +329,6 @@ pub enum SendError {
326329
/// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded
327330
/// hops.
328331
TooFewBlindedHops,
329-
/// Our next-hop peer was offline or does not support onion message forwarding.
330-
InvalidFirstHop,
331332
/// A path from the sender to the destination could not be found by the [`MessageRouter`].
332333
PathNotFound,
333334
/// Onion message contents must have a TLV type >= 64.
@@ -615,7 +616,11 @@ where
615616
}
616617

617618
match message_buffers.entry(first_node_id) {
618-
hash_map::Entry::Vacant(_) => Err(SendError::InvalidFirstHop),
619+
hash_map::Entry::Vacant(e) => {
620+
e.insert(OnionMessageBuffer::PendingConnection(VecDeque::new()))
621+
.enqueue_message(onion_message);
622+
Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
623+
},
619624
hash_map::Entry::Occupied(mut e) => {
620625
e.get_mut().enqueue_message(onion_message);
621626
Ok(SendSuccess::Buffered)
@@ -657,6 +662,12 @@ where
657662
Ok(SendSuccess::Buffered) => {
658663
log_trace!(self.logger, "Buffered onion message {}", log_suffix);
659664
},
665+
Ok(SendSuccess::BufferedAwaitingConnection(node_id)) => {
666+
log_trace!(
667+
self.logger, "Buffered onion message waiting on peer connection {}: {:?}",
668+
log_suffix, node_id
669+
);
670+
},
660671
}
661672
}
662673

0 commit comments

Comments
 (0)