Skip to content

Commit 6574335

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 a73e36c commit 6574335

File tree

6 files changed

+74
-154
lines changed

6 files changed

+74
-154
lines changed

fuzz/src/chanmon_consistency.rs

+5-3
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

+5-3
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use lightning::onion_message::async_payments::{
1919
AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc,
2020
};
2121
use lightning::onion_message::messenger::{
22-
CustomOnionMessageHandler, Destination, MessageRouter, MessageSendInstructions,
23-
OnionMessagePath, OnionMessenger, Responder, ResponseInstruction,
22+
BlindedPathType, CustomOnionMessageHandler, Destination, MessageRouter,
23+
MessageSendInstructions, OnionMessagePath, OnionMessenger, Responder, ResponseInstruction,
2424
};
2525
use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler};
2626
use lightning::onion_message::packet::OnionMessageContents;
@@ -100,8 +100,8 @@ impl MessageRouter for TestMessageRouter {
100100
}
101101

102102
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
103-
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
104-
_secp_ctx: &Secp256k1<T>,
103+
&self, _recipient: PublicKey, _context: MessageContext, _blinded_path: BlindedPathType,
104+
_peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
105105
) -> Result<Vec<BlindedMessagePath>, ()> {
106106
unreachable!()
107107
}

lightning/src/ln/channelmanager.rs

+35-75
Original file line numberDiff line numberDiff line change
@@ -2625,9 +2625,7 @@ const MAX_NO_CHANNEL_PEERS: usize = 250;
26252625
/// short-lived, while anything with a greater expiration is considered long-lived.
26262626
///
26272627
/// Using [`ChannelManager::create_offer_builder`] or [`ChannelManager::create_refund_builder`],
2628-
/// will included a [`BlindedMessagePath`] created using:
2629-
/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2630-
/// - [`MessageRouter::create_blinded_paths`] when long-lived.
2628+
/// will included a [`BlindedMessagePath`] created using [`MessageRouter::create_blinded_paths`].
26312629
///
26322630
/// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
26332631
/// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
@@ -4453,7 +4451,7 @@ where
44534451
let reply_paths = match self.create_blinded_paths(
44544452
MessageContext::AsyncPayments(
44554453
AsyncPaymentsContext::OutboundPayment { payment_id, nonce, hmac }
4456-
)
4454+
), BlindedPathType::Full
44574455
) {
44584456
Ok(paths) => paths,
44594457
Err(()) => {
@@ -9123,23 +9121,13 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
91239121
let secp_ctx = &$self.secp_ctx;
91249122

91259123
let nonce = Nonce::from_entropy_source(entropy);
9126-
let context = OffersContext::InvoiceRequest { nonce };
9124+
let context = MessageContext::Offers (
9125+
OffersContext::InvoiceRequest { nonce }
9126+
);
91279127
let builder = match blinded_path {
9128-
Some(BlindedPathType::Compact) => {
9129-
let path = $self
9130-
.create_compact_blinded_paths(context)
9131-
.and_then(|paths| paths.into_iter().next().ok_or(()))
9132-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
9133-
9134-
OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
9135-
.chain_hash($self.chain_hash)
9136-
.path(path)
9137-
}
9138-
9139-
Some(BlindedPathType::Full) => {
9140-
let context = MessageContext::Offers(context);
9128+
Some(blinded_path) => {
91419129
let path = $self
9142-
.create_blinded_paths(context)
9130+
.create_blinded_paths(context, blinded_path)
91439131
.and_then(|paths| paths.into_iter().next().ok_or(()))
91449132
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
91459133

@@ -9212,28 +9200,14 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
92129200
let secp_ctx = &$self.secp_ctx;
92139201

92149202
let nonce = Nonce::from_entropy_source(entropy);
9215-
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None };
9203+
let context = MessageContext::Offers (
9204+
OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
9205+
);
92169206

92179207
let builder = match blinded_path {
9218-
Some(BlindedPathType::Compact) => {
9208+
Some(blinded_path) => {
92199209
let path = $self
9220-
.create_compact_blinded_paths(context)
9221-
.and_then(|paths| paths.into_iter().next().ok_or(()))
9222-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
9223-
9224-
RefundBuilder::deriving_signing_pubkey(
9225-
node_id, expanded_key, nonce, secp_ctx,
9226-
amount_msats, payment_id,
9227-
)?
9228-
.chain_hash($self.chain_hash)
9229-
.absolute_expiry(absolute_expiry)
9230-
.path(path)
9231-
}
9232-
9233-
Some(BlindedPathType::Full) => {
9234-
let context = MessageContext::Offers(context);
9235-
let path = $self
9236-
.create_blinded_paths(context)
9210+
.create_blinded_paths(context, blinded_path)
92379211
.and_then(|paths| paths.into_iter().next().ok_or(()))
92389212
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
92399213

@@ -9383,7 +9357,7 @@ where
93839357
let context = MessageContext::Offers(
93849358
OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
93859359
);
9386-
let reply_paths = self.create_blinded_paths(context)
9360+
let reply_paths = self.create_blinded_paths(context, BlindedPathType::Full)
93879361
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
93889362

93899363
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
@@ -9505,7 +9479,7 @@ where
95059479
let context = MessageContext::Offers(OffersContext::InboundPayment {
95069480
payment_hash: invoice.payment_hash(), nonce, hmac
95079481
});
9508-
let reply_paths = self.create_blinded_paths(context)
9482+
let reply_paths = self.create_blinded_paths(context, BlindedPathType::Full)
95099483
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
95109484

95119485
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
@@ -9655,7 +9629,7 @@ where
96559629
/// [`MessageRouter::create_blinded_paths`].
96569630
///
96579631
/// Errors if the `MessageRouter` errors.
9658-
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()> {
9632+
fn create_blinded_paths(&self, context: MessageContext, blinded_path: BlindedPathType) -> Result<Vec<BlindedMessagePath>, ()> {
96599633
let recipient = self.get_our_node_id();
96609634
let secp_ctx = &self.secp_ctx;
96619635

@@ -9664,44 +9638,30 @@ where
96649638
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
96659639
.filter(|(_, peer)| peer.is_connected)
96669640
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9667-
.map(|(node_id, _)| {
9668-
MessageForwardNode {
9669-
node_id: *node_id,
9670-
short_channel_id: None,
9641+
.map(|(node_id, peer)| {
9642+
match blinded_path {
9643+
BlindedPathType::Full => {
9644+
MessageForwardNode {
9645+
node_id: *node_id,
9646+
short_channel_id: None,
9647+
}
9648+
}
9649+
BlindedPathType::Compact => {
9650+
MessageForwardNode {
9651+
node_id: *node_id,
9652+
short_channel_id: peer.channel_by_id
9653+
.iter()
9654+
.filter(|(_, channel)| channel.context().is_usable())
9655+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9656+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9657+
}
9658+
}
96719659
}
96729660
})
96739661
.collect::<Vec<_>>();
96749662

96759663
self.message_router
9676-
.create_blinded_paths(recipient, context, peers, secp_ctx)
9677-
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9678-
}
9679-
9680-
/// Creates a collection of blinded paths by delegating to
9681-
/// [`MessageRouter::create_compact_blinded_paths`].
9682-
///
9683-
/// Errors if the `MessageRouter` errors.
9684-
fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9685-
let recipient = self.get_our_node_id();
9686-
let secp_ctx = &self.secp_ctx;
9687-
9688-
let peers = self.per_peer_state.read().unwrap()
9689-
.iter()
9690-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9691-
.filter(|(_, peer)| peer.is_connected)
9692-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9693-
.map(|(node_id, peer)| MessageForwardNode {
9694-
node_id: *node_id,
9695-
short_channel_id: peer.channel_by_id
9696-
.iter()
9697-
.filter(|(_, channel)| channel.context().is_usable())
9698-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
9699-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
9700-
})
9701-
.collect::<Vec<_>>();
9702-
9703-
self.message_router
9704-
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9664+
.create_blinded_paths(recipient, context, blinded_path, peers, secp_ctx)
97059665
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
97069666
}
97079667

@@ -11088,7 +11048,7 @@ where
1108811048
nonce,
1108911049
hmac: Some(hmac)
1109011050
});
11091-
match self.create_blinded_paths(context) {
11051+
match self.create_blinded_paths(context, BlindedPathType::Full) {
1109211052
Ok(reply_paths) => match self.enqueue_invoice_request(invoice_request, reply_paths) {
1109311053
Ok(_) => {}
1109411054
Err(_) => {

0 commit comments

Comments
 (0)