From: Valentine Wallace Date: Fri, 9 Aug 2024 19:50:19 +0000 (-0700) Subject: Move compact blinded path util to message paths only. X-Git-Tag: v0.0.124-beta~10^2~8 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=cbc25fbcf07b56fc0489a74513cf1641cbeb165f;p=rust-lightning Move compact blinded path util to message paths only. It's only used for message paths, so let's move it there to help make the BlindedPath struct private. --- diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index 0b575e842..08ef90a84 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -16,7 +16,7 @@ use crate::prelude::*; use bitcoin::hashes::hmac::Hmac; use bitcoin::hashes::sha256::Hash as Sha256; -use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp}; +use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NextMessageHop, NodeIdLookUp}; use crate::blinded_path::utils; use crate::io; use crate::io::Cursor; @@ -25,8 +25,10 @@ use crate::ln::msgs::DecodeError; use crate::ln::{PaymentHash, onion_utils}; use crate::offers::nonce::Nonce; use crate::onion_message::packet::ControlTlvs; +use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph}; use crate::sign::{EntropySource, NodeSigner, Recipient}; use crate::crypto::streams::ChaChaPolyReadAdapter; +use crate::util::scid_utils; use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer}; use core::mem; @@ -81,6 +83,35 @@ impl BlindedMessagePath { ).map_err(|_| ())?, })) } + + /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed + /// short channel id from a channel in `network_graph` leading to the introduction node. + /// + /// While this may result in a smaller encoding, there is a trade off in that the path may + /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore, + /// calling this may not be suitable for long-lived blinded paths. + pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) { + if let IntroductionNode::NodeId(pubkey) = &self.0.introduction_node { + let node_id = NodeId::from_pubkey(pubkey); + if let Some(node_info) = network_graph.node(&node_id) { + if let Some((scid, channel_info)) = node_info + .channels + .iter() + .filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info))) + .min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid)) + { + let direction = if node_id == channel_info.node_one { + Direction::NodeOne + } else { + debug_assert_eq!(node_id, channel_info.node_two); + Direction::NodeTwo + }; + self.0.introduction_node = + IntroductionNode::DirectedShortChannelId(direction, scid); + } + } + } + } } /// An intermediate node, and possibly a short channel id leading to the next node. diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 2ffb0ca9b..b2cb2c018 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -19,7 +19,6 @@ use core::ops::Deref; use crate::ln::msgs::DecodeError; use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph}; use crate::util::ser::{Readable, Writeable, Writer}; -use crate::util::scid_utils; use crate::io; use crate::prelude::*; @@ -139,35 +138,6 @@ impl BlindedPath { }, } } - - /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed - /// short channel id from a channel in `network_graph` leading to the introduction node. - /// - /// While this may result in a smaller encoding, there is a trade off in that the path may - /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore, - /// calling this may not be suitable for long-lived blinded paths. - pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) { - if let IntroductionNode::NodeId(pubkey) = &self.introduction_node { - let node_id = NodeId::from_pubkey(pubkey); - if let Some(node_info) = network_graph.node(&node_id) { - if let Some((scid, channel_info)) = node_info - .channels - .iter() - .filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info))) - .min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid)) - { - let direction = if node_id == channel_info.node_one { - Direction::NodeOne - } else { - debug_assert_eq!(node_id, channel_info.node_two); - Direction::NodeTwo - }; - self.introduction_node = - IntroductionNode::DirectedShortChannelId(direction, scid); - } - } - } - } } impl Writeable for BlindedPath { diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 69b87b7a1..2fde66b12 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -457,7 +457,7 @@ pub trait MessageRouter { /// /// Implementations using additional intermediate nodes are responsible for using a /// [`ForwardNode`] with `Some` short channel id, if possible. Similarly, implementations should - /// call [`BlindedPath::use_compact_introduction_node`]. + /// call [`BlindedMessagePath::use_compact_introduction_node`]. /// /// The provided implementation simply delegates to [`MessageRouter::create_blinded_paths`], /// ignoring the short channel ids. @@ -565,7 +565,7 @@ where if compact_paths { for path in &mut paths { - path.0.use_compact_introduction_node(&network_graph); + path.use_compact_introduction_node(&network_graph); } }