Move DefaultMessageRouter::create_blinded_paths
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 22 May 2024 19:43:05 +0000 (14:43 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 5 Jun 2024 22:04:14 +0000 (17:04 -0500)
An upcoming change to the MessageRouter trait will require reusing
DefaultMessageRouter::create_blinded_paths. Move the code to a utility
function so facilitate this.

lightning/src/onion_message/messenger.rs

index eb4a837feb6ad0326b5fd4822f8f922212ed3166..5004986632567f10f1b2f37321e2ca5932a8c8fa 100644 (file)
@@ -449,50 +449,12 @@ where
        pub fn new(network_graph: G, entropy_source: ES) -> Self {
                Self { network_graph, entropy_source }
        }
        pub fn new(network_graph: G, entropy_source: ES) -> Self {
                Self { network_graph, entropy_source }
        }
-}
-
-impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
-where
-       L::Target: Logger,
-       ES::Target: EntropySource,
-{
-       fn find_path(
-               &self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
-       ) -> Result<OnionMessagePath, ()> {
-               let network_graph = self.network_graph.deref().read_only();
-               destination.resolve(&network_graph);
-
-               let first_node = match destination.first_node() {
-                       Some(first_node) => first_node,
-                       None => return Err(()),
-               };
-
-               if peers.contains(&first_node) || sender == first_node {
-                       Ok(OnionMessagePath {
-                               intermediate_nodes: vec![], destination, first_node_addresses: None
-                       })
-               } else {
-                       let node_details = network_graph
-                               .node(&NodeId::from_pubkey(&first_node))
-                               .and_then(|node_info| node_info.announcement_info.as_ref())
-                               .map(|announcement_info| (announcement_info.features(), announcement_info.addresses()));
-
-                       match node_details {
-                               Some((features, addresses)) if features.supports_onion_messages() && addresses.len() > 0 => {
-                                       let first_node_addresses = Some(addresses.clone());
-                                       Ok(OnionMessagePath {
-                                               intermediate_nodes: vec![], destination, first_node_addresses
-                                       })
-                               },
-                               _ => Err(()),
-                       }
-               }
-       }
 
 
-       fn create_blinded_paths<
+       fn create_blinded_paths_from_iter<
+               I: Iterator<Item = ForwardNode>,
                T: secp256k1::Signing + secp256k1::Verification
        >(
                T: secp256k1::Signing + secp256k1::Verification
        >(
-               &self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
+               &self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>,
        ) -> Result<Vec<BlindedPath>, ()> {
                // Limit the number of blinded paths that are computed.
                const MAX_PATHS: usize = 3;
        ) -> Result<Vec<BlindedPath>, ()> {
                // Limit the number of blinded paths that are computed.
                const MAX_PATHS: usize = 3;
@@ -505,7 +467,7 @@ where
                let is_recipient_announced =
                        network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
 
                let is_recipient_announced =
                        network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
 
-               let mut peer_info = peers.into_iter()
+               let mut peer_info = peers
                        // Limit to peers with announced channels
                        .filter_map(|peer|
                                network_graph
                        // Limit to peers with announced channels
                        .filter_map(|peer|
                                network_graph
@@ -548,6 +510,53 @@ where
        }
 }
 
        }
 }
 
+impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
+where
+       L::Target: Logger,
+       ES::Target: EntropySource,
+{
+       fn find_path(
+               &self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
+       ) -> Result<OnionMessagePath, ()> {
+               let network_graph = self.network_graph.deref().read_only();
+               destination.resolve(&network_graph);
+
+               let first_node = match destination.first_node() {
+                       Some(first_node) => first_node,
+                       None => return Err(()),
+               };
+
+               if peers.contains(&first_node) || sender == first_node {
+                       Ok(OnionMessagePath {
+                               intermediate_nodes: vec![], destination, first_node_addresses: None
+                       })
+               } else {
+                       let node_details = network_graph
+                               .node(&NodeId::from_pubkey(&first_node))
+                               .and_then(|node_info| node_info.announcement_info.as_ref())
+                               .map(|announcement_info| (announcement_info.features(), announcement_info.addresses()));
+
+                       match node_details {
+                               Some((features, addresses)) if features.supports_onion_messages() && addresses.len() > 0 => {
+                                       let first_node_addresses = Some(addresses.clone());
+                                       Ok(OnionMessagePath {
+                                               intermediate_nodes: vec![], destination, first_node_addresses
+                                       })
+                               },
+                               _ => Err(()),
+                       }
+               }
+       }
+
+       fn create_blinded_paths<
+               T: secp256k1::Signing + secp256k1::Verification
+       >(
+               &self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
+       ) -> Result<Vec<BlindedPath>, ()> {
+               self.create_blinded_paths_from_iter(recipient, peers.into_iter(), secp_ctx)
+       }
+}
+
 /// A path for sending an [`OnionMessage`].
 #[derive(Clone)]
 pub struct OnionMessagePath {
 /// A path for sending an [`OnionMessage`].
 #[derive(Clone)]
 pub struct OnionMessagePath {