Skip to content

Commit 4014246

Browse files
committed
Introduce a Unified Function Flow for Creating Blinded Paths
This refactor simplifies the codebase while ensuring that the existing functionality is maintained.
1 parent 9a981c1 commit 4014246

File tree

7 files changed

+72
-172
lines changed

7 files changed

+72
-172
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ use lightning::ln::script::ShutdownScript;
5858
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
5959
use lightning::offers::invoice::UnsignedBolt12Invoice;
6060
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
61-
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
61+
use lightning::onion_message::messenger::{
62+
BlindedPathType, Destination, MessageRouter, OnionMessagePath,
63+
};
6264
use lightning::routing::router::{InFlightHtlcs, Path, Route, RouteHop, RouteParameters, Router};
6365
use lightning::sign::{
6466
EntropySource, InMemorySigner, KeyMaterial, NodeSigner, Recipient, SignerProvider,
@@ -141,8 +143,8 @@ impl MessageRouter for FuzzRouter {
141143
}
142144

143145
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
144-
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
145-
_secp_ctx: &Secp256k1<T>,
146+
&self, _recipient: PublicKey, _context: MessageContext, _blinded_path: BlindedPathType,
147+
_peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
146148
) -> Result<Vec<BlindedMessagePath>, ()> {
147149
unreachable!()
148150
}

fuzz/src/full_stack.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ use lightning::ln::script::ShutdownScript;
5151
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
5252
use lightning::offers::invoice::UnsignedBolt12Invoice;
5353
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
54-
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
54+
use lightning::onion_message::messenger::{
55+
BlindedPathType, Destination, MessageRouter, OnionMessagePath,
56+
};
5557
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
5658
use lightning::routing::router::{
5759
InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router,
@@ -176,8 +178,8 @@ impl MessageRouter for FuzzRouter {
176178
}
177179

178180
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
179-
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
180-
_secp_ctx: &Secp256k1<T>,
181+
&self, _recipient: PublicKey, _context: MessageContext, _blinded_path: BlindedPathType,
182+
_peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
181183
) -> Result<Vec<BlindedMessagePath>, ()> {
182184
unreachable!()
183185
}

fuzz/src/onion_message.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use lightning::onion_message::async_payments::{
1818
AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc,
1919
};
2020
use lightning::onion_message::messenger::{
21-
CustomOnionMessageHandler, Destination, MessageRouter, MessageSendInstructions,
22-
OnionMessagePath, OnionMessenger, Responder, ResponseInstruction,
21+
BlindedPathType, CustomOnionMessageHandler, Destination, MessageRouter,
22+
MessageSendInstructions, OnionMessagePath, OnionMessenger, Responder, ResponseInstruction,
2323
};
2424
use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler};
2525
use lightning::onion_message::packet::OnionMessageContents;
@@ -98,8 +98,8 @@ impl MessageRouter for TestMessageRouter {
9898
}
9999

100100
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
101-
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
102-
_secp_ctx: &Secp256k1<T>,
101+
&self, _recipient: PublicKey, _context: MessageContext, _blinded_path: BlindedPathType,
102+
_peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
103103
) -> Result<Vec<BlindedMessagePath>, ()> {
104104
unreachable!()
105105
}

lightning/src/ln/channelmanager.rs

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,9 +2554,7 @@ const MAX_NO_CHANNEL_PEERS: usize = 250;
25542554
/// short-lived, while anything with a greater expiration is considered long-lived.
25552555
///
25562556
/// Using [`ChannelManager::create_offer_builder`] or [`ChannelManager::create_refund_builder`],
2557-
/// will included a [`BlindedMessagePath`] created using:
2558-
/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2559-
/// - [`MessageRouter::create_blinded_paths`] when long-lived.
2557+
/// will included a [`BlindedMessagePath`] created using [`MessageRouter::create_blinded_paths`].
25602558
///
25612559
/// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
25622560
/// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
@@ -9042,21 +9040,9 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
90429040
let nonce = Nonce::from_entropy_source(entropy);
90439041
let context = OffersContext::InvoiceRequest { nonce };
90449042
let builder = match blinded_path {
9045-
Some(BlindedPathType::Compact) => {
9043+
Some(blinded_path) => {
90469044
let path = $self
9047-
.create_compact_blinded_paths(context)
9048-
.and_then(|paths| paths.into_iter().next().ok_or(()))
9049-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
9050-
9051-
OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
9052-
.chain_hash($self.chain_hash)
9053-
.path(path)
9054-
}
9055-
9056-
Some(BlindedPathType::Full) => {
9057-
let context = MessageContext::Offers(context);
9058-
let path = $self
9059-
.create_blinded_paths(context)
9045+
.create_blinded_paths(context, blinded_path)
90609046
.and_then(|paths| paths.into_iter().next().ok_or(()))
90619047
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
90629048

@@ -9132,25 +9118,9 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
91329118
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None };
91339119

91349120
let builder = match blinded_path {
9135-
Some(BlindedPathType::Compact) => {
9136-
let path = $self
9137-
.create_compact_blinded_paths(context)
9138-
.and_then(|paths| paths.into_iter().next().ok_or(()))
9139-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
9140-
9141-
RefundBuilder::deriving_signing_pubkey(
9142-
node_id, expanded_key, nonce, secp_ctx,
9143-
amount_msats, payment_id,
9144-
)?
9145-
.chain_hash($self.chain_hash)
9146-
.absolute_expiry(absolute_expiry)
9147-
.path(path)
9148-
}
9149-
9150-
Some(BlindedPathType::Full) => {
9151-
let context = MessageContext::Offers(context);
9121+
Some(blinded_path) => {
91529122
let path = $self
9153-
.create_blinded_paths(context)
9123+
.create_blinded_paths(context, blinded_path)
91549124
.and_then(|paths| paths.into_iter().next().ok_or(()))
91559125
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
91569126

@@ -9299,7 +9269,7 @@ where
92999269
let context = MessageContext::Offers(
93009270
OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
93019271
);
9302-
let reply_paths = self.create_blinded_paths(context)
9272+
let reply_paths = self.create_blinded_paths(context, BlindedPathType::Full)
93039273
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
93049274

93059275
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
@@ -9421,7 +9391,7 @@ where
94219391
let context = MessageContext::Offers(OffersContext::InboundPayment {
94229392
payment_hash: invoice.payment_hash(), nonce, hmac
94239393
});
9424-
let reply_paths = self.create_blinded_paths(context)
9394+
let reply_paths = self.create_blinded_paths(context, BlindedPathType::Full)
94259395
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
94269396

94279397
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
@@ -9571,7 +9541,7 @@ where
95719541
/// [`MessageRouter::create_blinded_paths`].
95729542
///
95739543
/// Errors if the `MessageRouter` errors.
9574-
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()> {
9544+
fn create_blinded_paths(&self, context: MessageContext, blinded_path: BlindedPathType) -> Result<Vec<BlindedMessagePath>, ()> {
95759545
let recipient = self.get_our_node_id();
95769546
let secp_ctx = &self.secp_ctx;
95779547

@@ -9580,44 +9550,30 @@ where
95809550
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
95819551
.filter(|(_, peer)| peer.is_connected)
95829552
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9583-
.map(|(node_id, _)| {
9584-
MessageForwardNode {
9585-
node_id: *node_id,
9586-
short_channel_id: None,
9553+
.map(|(node_id, peer)| {
9554+
match blinded_path {
9555+
BlindedPathType::Full => {
9556+
MessageForwardNode {
9557+
node_id: *node_id,
9558+
short_channel_id: None,
9559+
}
9560+
}
9561+
BlindedPathType::Compact => {
9562+
MessageForwardNode {
9563+
node_id: *node_id,
9564+
short_channel_id: peer.channel_by_id
9565+
.iter()
9566+
.filter(|(_, channel)| channel.context().is_usable())
9567+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9568+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9569+
}
9570+
}
95879571
}
95889572
})
95899573
.collect::<Vec<_>>();
95909574

95919575
self.router
9592-
.create_blinded_paths(recipient, context, peers, secp_ctx)
9593-
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9594-
}
9595-
9596-
/// Creates a collection of blinded paths by delegating to
9597-
/// [`MessageRouter::create_compact_blinded_paths`].
9598-
///
9599-
/// Errors if the `MessageRouter` errors.
9600-
fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9601-
let recipient = self.get_our_node_id();
9602-
let secp_ctx = &self.secp_ctx;
9603-
9604-
let peers = self.per_peer_state.read().unwrap()
9605-
.iter()
9606-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9607-
.filter(|(_, peer)| peer.is_connected)
9608-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9609-
.map(|(node_id, peer)| MessageForwardNode {
9610-
node_id: *node_id,
9611-
short_channel_id: peer.channel_by_id
9612-
.iter()
9613-
.filter(|(_, channel)| channel.context().is_usable())
9614-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9615-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9616-
})
9617-
.collect::<Vec<_>>();
9618-
9619-
self.router
9620-
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9576+
.create_blinded_paths(recipient, MessageContext::Offers(context), blinded_path, peers, secp_ctx)
96219577
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
96229578
}
96239579

lightning/src/onion_message/messenger.rs

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
151151
/// # use lightning::blinded_path::message::{BlindedMessagePath, MessageForwardNode, MessageContext};
152152
/// # use lightning::sign::{EntropySource, KeysManager};
153153
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
154-
/// # use lightning::onion_message::messenger::{Destination, MessageRouter, MessageSendInstructions, OnionMessagePath, OnionMessenger};
154+
/// # use lightning::onion_message::messenger::{BlindedPathType, Destination, MessageRouter, MessageSendInstructions, OnionMessagePath, OnionMessenger};
155155
/// # use lightning::onion_message::packet::OnionMessageContents;
156156
/// # use lightning::util::logger::{Logger, Record};
157157
/// # use lightning::util::ser::{Writeable, Writer};
@@ -175,7 +175,7 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
175175
/// # })
176176
/// # }
177177
/// # fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
178-
/// # &self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>
178+
/// # &self, _recipient: PublicKey, _context: MessageContext, _blinded_path: BlindedPathType, _peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>
179179
/// # ) -> Result<Vec<BlindedMessagePath>, ()> {
180180
/// # unreachable!()
181181
/// # }
@@ -449,14 +449,9 @@ pub trait MessageRouter {
449449

450450
/// Creates [`BlindedMessagePath`]s to the `recipient` node. The nodes in `peers` are assumed to
451451
/// be direct peers with the `recipient`.
452-
fn create_blinded_paths<
453-
T: secp256k1::Signing + secp256k1::Verification
454-
>(
455-
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
456-
) -> Result<Vec<BlindedMessagePath>, ()>;
457-
458-
/// Creates compact [`BlindedMessagePath`]s to the `recipient` node. The nodes in `peers` are
459-
/// assumed to be direct peers with the `recipient`.
452+
///
453+
/// # Note of compact blinded path:
454+
/// User can decide to create compact blinded path by specifying the appropirate [`BlindedPathType`].
460455
///
461456
/// Compact blinded paths use short channel ids instead of pubkeys for a smaller serialization,
462457
/// which is beneficial when a QR code is used to transport the data. The SCID is passed using
@@ -465,24 +460,12 @@ pub trait MessageRouter {
465460
/// Implementations using additional intermediate nodes are responsible for using a
466461
/// [`MessageForwardNode`] with `Some` short channel id, if possible. Similarly, implementations
467462
/// should call [`BlindedMessagePath::use_compact_introduction_node`].
468-
///
469-
/// The provided implementation simply delegates to [`MessageRouter::create_blinded_paths`],
470-
/// ignoring the short channel ids.
471-
fn create_compact_blinded_paths<
463+
fn create_blinded_paths<
472464
T: secp256k1::Signing + secp256k1::Verification
473465
>(
474-
&self, recipient: PublicKey, context: MessageContext,
466+
&self, recipient: PublicKey, context: MessageContext, blinded_path: BlindedPathType,
475467
peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
476-
) -> Result<Vec<BlindedMessagePath>, ()> {
477-
let peers = peers
478-
.into_iter()
479-
.map(|mut node| {
480-
node.short_channel_id = None;
481-
node
482-
})
483-
.collect();
484-
self.create_blinded_paths(recipient, context, peers, secp_ctx)
485-
}
468+
) -> Result<Vec<BlindedMessagePath>, ()>;
486469
}
487470

488471
/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
@@ -516,8 +499,8 @@ where
516499
I: ExactSizeIterator<Item = MessageForwardNode>,
517500
T: secp256k1::Signing + secp256k1::Verification
518501
>(
519-
network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I,
520-
entropy_source: &ES, secp_ctx: &Secp256k1<T>, compact_paths: bool,
502+
network_graph: &G, recipient: PublicKey, context: MessageContext, blinded_path: BlindedPathType,
503+
peers: I, entropy_source: &ES, secp_ctx: &Secp256k1<T>
521504
) -> Result<Vec<BlindedMessagePath>, ()> {
522505
// Limit the number of blinded paths that are computed.
523506
const MAX_PATHS: usize = 3;
@@ -573,10 +556,13 @@ where
573556
},
574557
}?;
575558

576-
if compact_paths {
577-
for path in &mut paths {
578-
path.use_compact_introduction_node(&network_graph);
559+
match blinded_path {
560+
BlindedPathType::Compact => {
561+
for path in &mut paths {
562+
path.use_compact_introduction_node(&network_graph);
563+
}
579564
}
565+
BlindedPathType::Full => {}
580566
}
581567

582568
Ok(paths)
@@ -618,25 +604,10 @@ where
618604
pub(crate) fn create_blinded_paths<
619605
T: secp256k1::Signing + secp256k1::Verification
620606
>(
621-
network_graph: &G, recipient: PublicKey, context: MessageContext,
607+
network_graph: &G, recipient: PublicKey, context: MessageContext, blinded_path: BlindedPathType,
622608
peers: Vec<MessageForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
623609
) -> Result<Vec<BlindedMessagePath>, ()> {
624-
let peers = peers
625-
.into_iter()
626-
.map(|mut node| {
627-
node.short_channel_id = None;
628-
node
629-
});
630-
Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, false)
631-
}
632-
633-
pub(crate) fn create_compact_blinded_paths<
634-
T: secp256k1::Signing + secp256k1::Verification
635-
>(
636-
network_graph: &G, recipient: PublicKey, context: MessageContext,
637-
peers: Vec<MessageForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
638-
) -> Result<Vec<BlindedMessagePath>, ()> {
639-
Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, true)
610+
Self::create_blinded_paths_from_iter(network_graph, recipient, context, blinded_path, peers.into_iter(), entropy_source, secp_ctx)
640611
}
641612
}
642613

@@ -654,19 +625,11 @@ where
654625
fn create_blinded_paths<
655626
T: secp256k1::Signing + secp256k1::Verification
656627
>(
657-
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
658-
) -> Result<Vec<BlindedMessagePath>, ()> {
659-
Self::create_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx)
660-
}
661-
662-
fn create_compact_blinded_paths<
663-
T: secp256k1::Signing + secp256k1::Verification
664-
>(
665-
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
628+
&self, recipient: PublicKey, context: MessageContext, blinded_path: BlindedPathType,
629+
peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
666630
) -> Result<Vec<BlindedMessagePath>, ()> {
667-
Self::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx)
631+
Self::create_blinded_paths(&self.network_graph, recipient, context, blinded_path, peers, &self.entropy_source, secp_ctx)
668632
}
669-
670633
}
671634

672635
/// A path for sending an [`OnionMessage`].
@@ -1270,7 +1233,7 @@ where
12701233
.collect::<Vec<_>>();
12711234

12721235
self.message_router
1273-
.create_blinded_paths(recipient, context, peers, secp_ctx)
1236+
.create_blinded_paths(recipient, context, BlindedPathType::Full, peers, secp_ctx)
12741237
.and_then(|paths| paths.into_iter().next().ok_or(()))
12751238
.map_err(|_| SendError::PathNotFound)
12761239
}

0 commit comments

Comments
 (0)