+/// [`OnionMessage`]s buffered to be sent.
+enum OnionMessageRecipient {
+ /// Messages for a node connected as a peer.
+ ConnectedPeer(VecDeque<OnionMessage>),
+
+ /// Messages for a node that is not yet connected, which are dropped after [`MAX_TIMER_TICKS`]
+ /// and tracked here.
+ PendingConnection(VecDeque<OnionMessage>, Option<Vec<SocketAddress>>, usize),
+}
+
+impl OnionMessageRecipient {
+ fn pending_connection(addresses: Vec<SocketAddress>) -> Self {
+ Self::PendingConnection(VecDeque::new(), Some(addresses), 0)
+ }
+
+ fn pending_messages(&self) -> &VecDeque<OnionMessage> {
+ match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ }
+ }
+
+ fn enqueue_message(&mut self, message: OnionMessage) {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ };
+
+ pending_messages.push_back(message);
+ }
+
+ fn dequeue_message(&mut self) -> Option<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => {
+ debug_assert!(false);
+ pending_messages
+ },
+ };
+
+ pending_messages.pop_front()
+ }
+
+ #[cfg(test)]
+ fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ };
+
+ core::mem::take(pending_messages)
+ }
+
+ fn mark_connected(&mut self) {
+ if let OnionMessageRecipient::PendingConnection(pending_messages, _, _) = self {
+ let mut new_pending_messages = VecDeque::new();
+ core::mem::swap(pending_messages, &mut new_pending_messages);
+ *self = OnionMessageRecipient::ConnectedPeer(new_pending_messages);
+ }
+ }
+
+ fn is_connected(&self) -> bool {
+ match self {
+ OnionMessageRecipient::ConnectedPeer(..) => true,
+ OnionMessageRecipient::PendingConnection(..) => false,
+ }
+ }
+}
+
+/// An [`OnionMessage`] for [`OnionMessenger`] to send.
+///
+/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are
+/// enqueued for sending.
+#[cfg(not(c_bindings))]
+pub struct PendingOnionMessage<T: OnionMessageContents> {
+ /// The message contents to send in an [`OnionMessage`].
+ pub contents: T,
+
+ /// The destination of the message.
+ pub destination: Destination,
+
+ /// A reply path to include in the [`OnionMessage`] for a response.
+ pub reply_path: Option<BlindedPath>,
+}
+
+#[cfg(c_bindings)]
+/// An [`OnionMessage`] for [`OnionMessenger`] to send.
+///
+/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are
+/// enqueued for sending.
+pub type PendingOnionMessage<T> = (T, Destination, Option<BlindedPath>);
+
+pub(crate) fn new_pending_onion_message<T: OnionMessageContents>(
+ contents: T, destination: Destination, reply_path: Option<BlindedPath>
+) -> PendingOnionMessage<T> {
+ #[cfg(not(c_bindings))]
+ return PendingOnionMessage { contents, destination, reply_path };
+ #[cfg(c_bindings)]
+ return (contents, destination, reply_path);
+}
+
+/// A trait defining behavior for routing an [`OnionMessage`].
+pub trait MessageRouter {
+ /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
+ fn find_path(
+ &self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+ ) -> Result<OnionMessagePath, ()>;
+
+ /// Creates [`BlindedPath`]s to the `recipient` node. The nodes in `peers` are assumed to be
+ /// direct peers with the `recipient`.
+ fn create_blinded_paths<
+ ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
+ >(
+ &self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
+ secp_ctx: &Secp256k1<T>
+ ) -> Result<Vec<BlindedPath>, ()>;
+}
+
+/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
+pub struct DefaultMessageRouter<G: Deref<Target=NetworkGraph<L>>, L: Deref>
+where
+ L::Target: Logger,
+{
+ network_graph: G,
+}
+
+impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> DefaultMessageRouter<G, L>
+where
+ L::Target: Logger,
+{
+ /// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
+ pub fn new(network_graph: G) -> Self {
+ Self { network_graph }
+ }
+}
+
+impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> MessageRouter for DefaultMessageRouter<G, L>
+where
+ L::Target: Logger,
+{
+ fn find_path(
+ &self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+ ) -> Result<OnionMessagePath, ()> {
+ let first_node = destination.first_node();
+ if peers.contains(&first_node) {
+ Ok(OnionMessagePath {
+ intermediate_nodes: vec![], destination, first_node_addresses: None
+ })
+ } else {
+ let network_graph = self.network_graph.deref().read_only();
+ let node_announcement = network_graph
+ .node(&NodeId::from_pubkey(&first_node))
+ .and_then(|node_info| node_info.announcement_info.as_ref())
+ .and_then(|announcement_info| announcement_info.announcement_message.as_ref())
+ .map(|node_announcement| &node_announcement.contents);
+
+ match node_announcement {
+ Some(node_announcement) if node_announcement.features.supports_onion_messages() => {
+ let first_node_addresses = Some(node_announcement.addresses.clone());
+ Ok(OnionMessagePath {
+ intermediate_nodes: vec![], destination, first_node_addresses
+ })
+ },
+ _ => Err(()),
+ }
+ }
+ }
+
+ fn create_blinded_paths<
+ ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
+ >(
+ &self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
+ secp_ctx: &Secp256k1<T>
+ ) -> Result<Vec<BlindedPath>, ()> {
+ // Limit the number of blinded paths that are computed.
+ const MAX_PATHS: usize = 3;
+
+ // Ensure peers have at least three channels so that it is more difficult to infer the
+ // recipient's node_id.
+ const MIN_PEER_CHANNELS: usize = 3;
+
+ let network_graph = self.network_graph.deref().read_only();
+ let paths = peers.iter()
+ // Limit to peers with announced channels
+ .filter(|pubkey|
+ network_graph
+ .node(&NodeId::from_pubkey(pubkey))
+ .map(|info| &info.channels[..])
+ .map(|channels| channels.len() >= MIN_PEER_CHANNELS)
+ .unwrap_or(false)
+ )
+ .map(|pubkey| vec![*pubkey, recipient])
+ .map(|node_pks| BlindedPath::new_for_message(&node_pks, entropy_source, secp_ctx))
+ .take(MAX_PATHS)
+ .collect::<Result<Vec<_>, _>>();
+
+ match paths {
+ Ok(paths) if !paths.is_empty() => Ok(paths),
+ _ => {
+ if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) {
+ BlindedPath::one_hop_for_message(recipient, entropy_source, secp_ctx)
+ .map(|path| vec![path])
+ } else {
+ Err(())
+ }
+ },
+ }
+ }
+}
+
+/// A path for sending an [`OnionMessage`].