Skip to content

Commit 7308281

Browse files
committed
Refactor MessageRouter::create_blinded_paths
Using compact blinded paths isn't always necessary or desirable. For instance, reply paths are communicated via onion messages where space isn't a premium unlike in QR codes. Additionally, long-lived paths could become invalid if the channel associated with the SCID is closed. Refactor MessageRouter::create_blinded_paths into two methods: one for compact blinded paths and one for normal blinded paths.
1 parent 9854bef commit 7308281

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ impl MessageRouter for FuzzRouter {
120120
}
121121

122122
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
123+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
124+
) -> Result<Vec<BlindedPath>, ()> {
125+
unreachable!()
126+
}
127+
128+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
123129
&self, _recipient: PublicKey, _peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
124130
) -> Result<Vec<BlindedPath>, ()> {
125131
unreachable!()

fuzz/src/full_stack.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ impl MessageRouter for FuzzRouter {
158158
}
159159

160160
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
161+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
162+
) -> Result<Vec<BlindedPath>, ()> {
163+
unreachable!()
164+
}
165+
166+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
161167
&self, _recipient: PublicKey, _peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
162168
) -> Result<Vec<BlindedPath>, ()> {
163169
unreachable!()

fuzz/src/onion_message.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl MessageRouter for TestMessageRouter {
8989
}
9090

9191
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
92+
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
93+
) -> Result<Vec<BlindedPath>, ()> {
94+
unreachable!()
95+
}
96+
97+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
9298
&self, _recipient: PublicKey, _peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
9399
) -> Result<Vec<BlindedPath>, ()> {
94100
unreachable!()

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8547,8 +8547,8 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
85478547
///
85488548
/// # Privacy
85498549
///
8550-
/// Uses [`MessageRouter::create_blinded_paths`] to construct a [`BlindedPath`] for the offer.
8551-
/// However, if one is not found, uses a one-hop [`BlindedPath`] with
8550+
/// Uses [`MessageRouter::create_compact_blinded_paths`] to construct a [`BlindedPath`] for the
8551+
/// offer. However, if one is not found, uses a one-hop [`BlindedPath`] with
85528552
/// [`ChannelManager::get_our_node_id`] as the introduction node instead. In the latter case,
85538553
/// the node must be announced, otherwise, there is no way to find a path to the introduction in
85548554
/// order to send the [`InvoiceRequest`].
@@ -8606,8 +8606,8 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
86068606
///
86078607
/// # Privacy
86088608
///
8609-
/// Uses [`MessageRouter::create_blinded_paths`] to construct a [`BlindedPath`] for the refund.
8610-
/// However, if one is not found, uses a one-hop [`BlindedPath`] with
8609+
/// Uses [`MessageRouter::create_compact_blinded_paths`] to construct a [`BlindedPath`] for the
8610+
/// refund. However, if one is not found, uses a one-hop [`BlindedPath`] with
86118611
/// [`ChannelManager::get_our_node_id`] as the introduction node instead. In the latter case,
86128612
/// the node must be announced, otherwise, there is no way to find a path to the introduction in
86138613
/// order to send the [`Bolt12Invoice`].
@@ -8988,7 +8988,7 @@ where
89888988
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
89898989
}
89908990

8991-
/// Creates a blinded path by delegating to [`MessageRouter::create_blinded_paths`].
8991+
/// Creates a blinded path by delegating to [`MessageRouter::create_compact_blinded_paths`].
89928992
///
89938993
/// Errors if the `MessageRouter` errors or returns an empty `Vec`.
89948994
fn create_blinded_path(&self) -> Result<BlindedPath, ()> {
@@ -9010,7 +9010,7 @@ where
90109010
.collect::<Vec<_>>();
90119011

90129012
self.router
9013-
.create_blinded_paths(recipient, peers, secp_ctx)
9013+
.create_compact_blinded_paths(recipient, peers, secp_ctx)
90149014
.and_then(|paths| paths.into_iter().next().ok_or(()))
90159015
}
90169016

lightning/src/onion_message/messenger.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
9898
/// # })
9999
/// # }
100100
/// # fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
101+
/// # &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
102+
/// # ) -> Result<Vec<BlindedPath>, ()> {
103+
/// # unreachable!()
104+
/// # }
105+
/// # fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
101106
/// # &self, _recipient: PublicKey, _peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>
102107
/// # ) -> Result<Vec<BlindedPath>, ()> {
103108
/// # unreachable!()
@@ -340,6 +345,14 @@ pub trait MessageRouter {
340345
/// direct peers with the `recipient`.
341346
fn create_blinded_paths<
342347
T: secp256k1::Signing + secp256k1::Verification
348+
>(
349+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
350+
) -> Result<Vec<BlindedPath>, ()>;
351+
352+
/// Creates [`BlindedPath`]s to the `recipient` node. The nodes in `peers` are assumed to be
353+
/// direct peers with the `recipient`.
354+
fn create_compact_blinded_paths<
355+
T: secp256k1::Signing + secp256k1::Verification
343356
>(
344357
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
345358
) -> Result<Vec<BlindedPath>, ()>;
@@ -369,7 +382,7 @@ where
369382
I: Iterator<Item = ForwardNode>,
370383
T: secp256k1::Signing + secp256k1::Verification
371384
>(
372-
&self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>,
385+
&self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>, compact_paths: bool
373386
) -> Result<Vec<BlindedPath>, ()> {
374387
// Limit the number of blinded paths that are computed.
375388
const MAX_PATHS: usize = 3;
@@ -417,8 +430,11 @@ where
417430
}
418431
},
419432
}?;
420-
for path in &mut paths {
421-
path.use_compact_introduction_node(&network_graph);
433+
434+
if compact_paths {
435+
for path in &mut paths {
436+
path.use_compact_introduction_node(&network_graph);
437+
}
422438
}
423439

424440
Ok(paths)
@@ -466,10 +482,21 @@ where
466482

467483
fn create_blinded_paths<
468484
T: secp256k1::Signing + secp256k1::Verification
485+
>(
486+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
487+
) -> Result<Vec<BlindedPath>, ()> {
488+
let peers = peers
489+
.into_iter()
490+
.map(|node_id| ForwardNode { node_id, short_channel_id: None });
491+
self.create_blinded_paths_from_iter(recipient, peers, secp_ctx, false)
492+
}
493+
494+
fn create_compact_blinded_paths<
495+
T: secp256k1::Signing + secp256k1::Verification
469496
>(
470497
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
471498
) -> Result<Vec<BlindedPath>, ()> {
472-
self.create_blinded_paths_from_iter(recipient, peers.into_iter(), secp_ctx)
499+
self.create_blinded_paths_from_iter(recipient, peers.into_iter(), secp_ctx, true)
473500
}
474501
}
475502

lightning/src/routing/router.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,18 @@ impl< G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref,
172172
fn create_blinded_paths<
173173
T: secp256k1::Signing + secp256k1::Verification
174174
> (
175-
&self, recipient: PublicKey, peers: Vec<message::ForwardNode>, secp_ctx: &Secp256k1<T>,
175+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
176176
) -> Result<Vec<BlindedPath>, ()> {
177177
self.message_router.create_blinded_paths(recipient, peers, secp_ctx)
178178
}
179+
180+
fn create_compact_blinded_paths<
181+
T: secp256k1::Signing + secp256k1::Verification
182+
> (
183+
&self, recipient: PublicKey, peers: Vec<message::ForwardNode>, secp_ctx: &Secp256k1<T>,
184+
) -> Result<Vec<BlindedPath>, ()> {
185+
self.message_router.create_compact_blinded_paths(recipient, peers, secp_ctx)
186+
}
179187
}
180188

181189
/// A trait defining behavior for routing a payment.

lightning/src/util/test_utils.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,18 @@ impl<'a> MessageRouter for TestRouter<'a> {
247247
fn create_blinded_paths<
248248
T: secp256k1::Signing + secp256k1::Verification
249249
>(
250-
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
250+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
251251
) -> Result<Vec<BlindedPath>, ()> {
252252
self.router.create_blinded_paths(recipient, peers, secp_ctx)
253253
}
254+
255+
fn create_compact_blinded_paths<
256+
T: secp256k1::Signing + secp256k1::Verification
257+
>(
258+
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
259+
) -> Result<Vec<BlindedPath>, ()> {
260+
self.router.create_compact_blinded_paths(recipient, peers, secp_ctx)
261+
}
254262
}
255263

256264
impl<'a> Drop for TestRouter<'a> {
@@ -282,10 +290,16 @@ impl<'a> MessageRouter for TestMessageRouter<'a> {
282290
}
283291

284292
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
285-
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
293+
&self, recipient: PublicKey, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
286294
) -> Result<Vec<BlindedPath>, ()> {
287295
self.inner.create_blinded_paths(recipient, peers, secp_ctx)
288296
}
297+
298+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
299+
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
300+
) -> Result<Vec<BlindedPath>, ()> {
301+
self.inner.create_compact_blinded_paths(recipient, peers, secp_ctx)
302+
}
289303
}
290304

291305
pub struct OnlyReadsKeysInterface {}

0 commit comments

Comments
 (0)