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.
This commit is contained in:
Valentine Wallace 2024-08-09 12:50:19 -07:00
parent 4dba7a314c
commit cbc25fbcf0
No known key found for this signature in database
GPG key ID: FD3E106A2CE099B4
3 changed files with 34 additions and 33 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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);
}
}