Skip to content

Commit cbc25fb

Browse files
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.
1 parent 4dba7a3 commit cbc25fb

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::prelude::*;
1616

1717
use bitcoin::hashes::hmac::Hmac;
1818
use bitcoin::hashes::sha256::Hash as Sha256;
19-
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
19+
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NextMessageHop, NodeIdLookUp};
2020
use crate::blinded_path::utils;
2121
use crate::io;
2222
use crate::io::Cursor;
@@ -25,8 +25,10 @@ use crate::ln::msgs::DecodeError;
2525
use crate::ln::{PaymentHash, onion_utils};
2626
use crate::offers::nonce::Nonce;
2727
use crate::onion_message::packet::ControlTlvs;
28+
use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
2829
use crate::sign::{EntropySource, NodeSigner, Recipient};
2930
use crate::crypto::streams::ChaChaPolyReadAdapter;
31+
use crate::util::scid_utils;
3032
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
3133

3234
use core::mem;
@@ -81,6 +83,35 @@ impl BlindedMessagePath {
8183
).map_err(|_| ())?,
8284
}))
8385
}
86+
87+
/// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
88+
/// short channel id from a channel in `network_graph` leading to the introduction node.
89+
///
90+
/// While this may result in a smaller encoding, there is a trade off in that the path may
91+
/// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore,
92+
/// calling this may not be suitable for long-lived blinded paths.
93+
pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) {
94+
if let IntroductionNode::NodeId(pubkey) = &self.0.introduction_node {
95+
let node_id = NodeId::from_pubkey(pubkey);
96+
if let Some(node_info) = network_graph.node(&node_id) {
97+
if let Some((scid, channel_info)) = node_info
98+
.channels
99+
.iter()
100+
.filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info)))
101+
.min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid))
102+
{
103+
let direction = if node_id == channel_info.node_one {
104+
Direction::NodeOne
105+
} else {
106+
debug_assert_eq!(node_id, channel_info.node_two);
107+
Direction::NodeTwo
108+
};
109+
self.0.introduction_node =
110+
IntroductionNode::DirectedShortChannelId(direction, scid);
111+
}
112+
}
113+
}
114+
}
84115
}
85116

86117
/// An intermediate node, and possibly a short channel id leading to the next node.

lightning/src/blinded_path/mod.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use core::ops::Deref;
1919
use crate::ln::msgs::DecodeError;
2020
use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph};
2121
use crate::util::ser::{Readable, Writeable, Writer};
22-
use crate::util::scid_utils;
2322

2423
use crate::io;
2524
use crate::prelude::*;
@@ -139,35 +138,6 @@ impl BlindedPath {
139138
},
140139
}
141140
}
142-
143-
/// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
144-
/// short channel id from a channel in `network_graph` leading to the introduction node.
145-
///
146-
/// While this may result in a smaller encoding, there is a trade off in that the path may
147-
/// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore,
148-
/// calling this may not be suitable for long-lived blinded paths.
149-
pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) {
150-
if let IntroductionNode::NodeId(pubkey) = &self.introduction_node {
151-
let node_id = NodeId::from_pubkey(pubkey);
152-
if let Some(node_info) = network_graph.node(&node_id) {
153-
if let Some((scid, channel_info)) = node_info
154-
.channels
155-
.iter()
156-
.filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info)))
157-
.min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid))
158-
{
159-
let direction = if node_id == channel_info.node_one {
160-
Direction::NodeOne
161-
} else {
162-
debug_assert_eq!(node_id, channel_info.node_two);
163-
Direction::NodeTwo
164-
};
165-
self.introduction_node =
166-
IntroductionNode::DirectedShortChannelId(direction, scid);
167-
}
168-
}
169-
}
170-
}
171141
}
172142

173143
impl Writeable for BlindedPath {

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ pub trait MessageRouter {
457457
///
458458
/// Implementations using additional intermediate nodes are responsible for using a
459459
/// [`ForwardNode`] with `Some` short channel id, if possible. Similarly, implementations should
460-
/// call [`BlindedPath::use_compact_introduction_node`].
460+
/// call [`BlindedMessagePath::use_compact_introduction_node`].
461461
///
462462
/// The provided implementation simply delegates to [`MessageRouter::create_blinded_paths`],
463463
/// ignoring the short channel ids.
@@ -565,7 +565,7 @@ where
565565

566566
if compact_paths {
567567
for path in &mut paths {
568-
path.0.use_compact_introduction_node(&network_graph);
568+
path.use_compact_introduction_node(&network_graph);
569569
}
570570
}
571571

0 commit comments

Comments
 (0)