Return socket addresses from DefaultMessageRouter
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 14 Nov 2023 23:08:26 +0000 (17:08 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 6 Dec 2023 14:47:34 +0000 (08:47 -0600)
When there isn't a direct connection with the Destination of an
OnionMessage, look up socket addresses from the NetworkGraph. This is
used to signal to OnionMessenger that a direct connection is needed to
send the message.

lightning/src/onion_message/messenger.rs

index eb309b622e817766fa53a3707287c1f0ec85b33d..9135157b8154639ed6f9a0368e3833889c9d52a1 100644 (file)
@@ -25,7 +25,7 @@ use crate::ln::features::{InitFeatures, NodeFeatures};
 use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler, SocketAddress};
 use crate::ln::onion_utils;
 use crate::ln::peer_handler::IgnoringMessageHandler;
-use crate::routing::gossip::NetworkGraph;
+use crate::routing::gossip::{NetworkGraph, NodeId};
 pub use super::packet::OnionMessageContents;
 use super::packet::ParsedOnionMessageContents;
 use super::offers::OffersMessageHandler;
@@ -282,10 +282,24 @@ where
        fn find_path(
                &self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
        ) -> Result<OnionMessagePath, ()> {
-               if peers.contains(&destination.first_node()) {
+               let first_node = destination.first_node();
+               if peers.contains(&first_node) {
                        Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses: None })
                } else {
-                       Err(())
+                       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 addresses = Some(node_announcement.addresses.clone());
+                                       Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses })
+                               },
+                               _ => Err(()),
+                       }
                }
        }
 }