From 318b72e5f41f8241994a0d3eb46d985cfa178fb6 Mon Sep 17 00:00:00 2001 From: shaavan Date: Tue, 13 Aug 2024 13:54:59 +0530 Subject: [PATCH] Introduce `params` Field in `create_blinded_paths` 1. Added a `params` field to the `create_blinded_paths` function, allowing users to specify Blinded Path Parameters directly. 2. This change simplifies the back-end by eliminating the need for multiple `create_blinded_path` variants tailored to different types of blinded paths. 3. In upcoming commits, the `create_blinded_path_using_absolute_expiry` and `create_compact_blinded_path` functions will be removed as they become redundant. --- fuzz/src/chanmon_consistency.rs | 2 +- fuzz/src/full_stack.rs | 2 +- fuzz/src/onion_message.rs | 2 +- lightning/src/ln/channelmanager.rs | 34 +++++++++++----- lightning/src/onion_message/messenger.rs | 51 ++++++++++++++---------- lightning/src/routing/router.rs | 6 +-- lightning/src/util/test_utils.rs | 9 +++-- 7 files changed, 65 insertions(+), 41 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index a34df1d5a07..5b48f121acd 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -139,7 +139,7 @@ impl MessageRouter for FuzzRouter { } fn create_blinded_paths( - &self, _recipient: PublicKey, _context: MessageContext, _peers: Vec, + &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1, ) -> Result, ()> { unreachable!() diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 57aa0632bf4..dbdd484e483 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -176,7 +176,7 @@ impl MessageRouter for FuzzRouter { } fn create_blinded_paths( - &self, _recipient: PublicKey, _context: MessageContext, _peers: Vec, + &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1, ) -> Result, ()> { unreachable!() diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 0de791c555a..7d5b86b0294 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -96,7 +96,7 @@ impl MessageRouter for TestMessageRouter { } fn create_blinded_paths( - &self, _recipient: PublicKey, _context: MessageContext, _peers: Vec, + &self, _params: BlindedPathParams _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1, ) -> Result, ()> { unreachable!() diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 642900b0114..44ca990e8c9 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -70,7 +70,7 @@ use crate::offers::parse::Bolt12SemanticError; use crate::offers::refund::{Refund, RefundBuilder}; use crate::offers::signer; use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler}; -use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, Responder, ResponseInstruction}; +use crate::onion_message::messenger::{new_pending_onion_message, BlindedPathParams, Destination, MessageRouter, PendingOnionMessage, Responder, ResponseInstruction, PATHS_PLACEHOLDER}; use crate::onion_message::offers::{OffersMessage, OffersMessageHandler}; use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider}; use crate::sign::ecdsa::EcdsaChannelSigner; @@ -8818,12 +8818,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => { let nonce = Nonce::from_entropy_source(entropy); let context = OffersContext::InvoiceRequest { nonce }; let path = $self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry) - .and_then(|paths| paths.into_iter().next().ok_or(())) - .map_err(|_| Bolt12SemanticError::MissingPaths)?; + .and_then(|paths| paths.into_iter().next().ok_or(())) + .map_err(|_| Bolt12SemanticError::MissingPaths)?; let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx) .chain_hash($self.chain_hash) .path(path); - + let builder = match absolute_expiry { None => builder, Some(absolute_expiry) => builder.absolute_expiry(absolute_expiry), @@ -8890,10 +8890,10 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => { let nonce = Nonce::from_entropy_source(entropy); let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None }; + let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry)) .and_then(|paths| paths.into_iter().next().ok_or(())) .map_err(|_| Bolt12SemanticError::MissingPaths)?; - let builder = RefundBuilder::deriving_payer_id( node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id )? @@ -9025,8 +9025,12 @@ where let invoice_request = builder.build_and_sign()?; let hmac = signer::hmac_for_payment_id(payment_id, nonce, expanded_key); + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: false, + }; let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }; - let reply_paths = self.create_blinded_paths(context) + let reply_paths = self.create_blinded_paths(params, context) .map_err(|_| Bolt12SemanticError::MissingPaths)?; let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self); @@ -9129,11 +9133,15 @@ where )?; let builder: InvoiceBuilder = builder.into(); let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?; - + + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: false, + }; let context = OffersContext::InboundPayment { payment_hash: invoice.payment_hash(), }; - let reply_paths = self.create_blinded_paths(context) + let reply_paths = self.create_blinded_paths(params, context) .map_err(|_| Bolt12SemanticError::MissingPaths)?; let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap(); @@ -9280,7 +9288,11 @@ where if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry { self.create_compact_blinded_paths(context) } else { - self.create_blinded_paths(context) + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: false + }; + self.create_blinded_paths(params, context) } } @@ -9301,7 +9313,7 @@ where /// [`MessageRouter::create_blinded_paths`]. /// /// Errors if the `MessageRouter` errors. - fn create_blinded_paths(&self, context: OffersContext) -> Result, ()> { + fn create_blinded_paths(&self, params: BlindedPathParams, context: OffersContext) -> Result, ()> { let recipient = self.get_our_node_id(); let secp_ctx = &self.secp_ctx; @@ -9314,7 +9326,7 @@ where .collect::>(); self.router - .create_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx) + .create_blinded_paths(params, recipient, MessageContext::Offers(context), peers, secp_ctx) .and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(())) } diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 05cc83406e2..c8a36eb9ac9 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -151,7 +151,7 @@ for OnionMessenger where /// # use lightning::blinded_path::message::{BlindedMessagePath, ForwardNode, MessageContext}; /// # use lightning::sign::{EntropySource, KeysManager}; /// # use lightning::ln::peer_handler::IgnoringMessageHandler; -/// # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath, OnionMessenger}; +/// # use lightning::onion_message::messenger::{BlindedPathParams, Destination, MessageRouter, OnionMessagePath, OnionMessenger}; /// # use lightning::onion_message::packet::OnionMessageContents; /// # use lightning::util::logger::{Logger, Record}; /// # use lightning::util::ser::{Writeable, Writer}; @@ -175,7 +175,7 @@ for OnionMessenger where /// # }) /// # } /// # fn create_blinded_paths( -/// # &self, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1 +/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1 /// # ) -> Result, ()> { /// # unreachable!() /// # } @@ -421,7 +421,7 @@ pub struct PendingOnionMessage { /// /// PATHS_PLACEHOLDER` is temporarily used as a default value in situations /// where a path index is required but has not yet been assigned or initialized. -pub const PATHS_PLACEHOLDER: usize = 0; +pub const PATHS_PLACEHOLDER: usize = 3; /// Represents the types of [`BlindedPath`] that can be created. /// @@ -461,7 +461,7 @@ pub trait MessageRouter { fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, + &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()>; /// Creates compact [`BlindedMessagePath`]s to the `recipient` node. The nodes in `peers` are @@ -487,7 +487,13 @@ pub trait MessageRouter { .into_iter() .map(|ForwardNode { node_id, short_channel_id: _ }| node_id) .collect(); - self.create_blinded_paths(recipient, context, peers, secp_ctx) + + // This parameter is a placeholder. This function is removed in the subsequent commits. + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: true, + }; + self.create_blinded_paths(params, recipient, context, peers, secp_ctx) } } @@ -522,12 +528,9 @@ where I: ExactSizeIterator, T: secp256k1::Signing + secp256k1::Verification >( - network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I, - entropy_source: &ES, secp_ctx: &Secp256k1, compact_paths: bool, + params: BlindedPathParams, network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I, + entropy_source: &ES, secp_ctx: &Secp256k1 ) -> Result, ()> { - // Limit the number of blinded paths that are computed. - const MAX_PATHS: usize = 3; - // Ensure peers have at least three channels so that it is more difficult to infer the // recipient's node_id. const MIN_PEER_CHANNELS: usize = 3; @@ -564,7 +567,7 @@ where .map(|(peer, _, _)| { BlindedMessagePath::new(&[peer], recipient, context.clone(), &**entropy_source, secp_ctx) }) - .take(MAX_PATHS) + .take(params.paths) .collect::, _>>(); let mut paths = match paths { @@ -579,7 +582,7 @@ where }, }?; - if compact_paths { + if params.is_compact { for path in &mut paths { path.use_compact_introduction_node(&network_graph); } @@ -624,13 +627,13 @@ where pub(crate) fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - network_graph: &G, recipient: PublicKey, context: MessageContext, + params: BlindedPathParams, network_graph: &G, recipient: PublicKey, context: MessageContext, peers: Vec, entropy_source: &ES, secp_ctx: &Secp256k1, ) -> Result, ()> { let peers = peers .into_iter() .map(|node_id| ForwardNode { node_id, short_channel_id: None }); - Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, false) + Self::create_blinded_paths_from_iter(params, network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx) } pub(crate) fn create_compact_blinded_paths< @@ -639,7 +642,11 @@ where network_graph: &G, recipient: PublicKey, context: MessageContext, peers: Vec, entropy_source: &ES, secp_ctx: &Secp256k1, ) -> Result, ()> { - Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, true) + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: true, + }; + Self::create_blinded_paths_from_iter(params, network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx) } } @@ -657,9 +664,9 @@ where fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, + &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - Self::create_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) + Self::create_blinded_paths(params, &self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } fn create_compact_blinded_paths< @@ -1235,7 +1242,7 @@ where .map_err(|_| SendError::PathNotFound) } - fn create_blinded_path(&self, context: MessageContext) -> Result { + fn create_blinded_path(&self, params: BlindedPathParams, context: MessageContext) -> Result { let recipient = self.node_signer .get_node_id(Recipient::Node) .map_err(|_| SendError::GetNodeIdFailed)?; @@ -1248,7 +1255,7 @@ where .collect::>(); self.message_router - .create_blinded_paths(recipient, context, peers, secp_ctx) + .create_blinded_paths(params, recipient, context, peers, secp_ctx) .and_then(|paths| paths.into_iter().next().ok_or(())) .map_err(|_| SendError::PathNotFound) } @@ -1346,7 +1353,11 @@ where let message_type = response.message.msg_type(); let reply_path = if let Some(context) = context { - match self.create_blinded_path(context) { + let params = BlindedPathParams { + paths: PATHS_PLACEHOLDER, + is_compact: false, + }; + match self.create_blinded_path(params, context) { Ok(reply_path) => Some(reply_path), Err(err) => { log_trace!( diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 18699eaa2fd..46210b0af8a 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -21,7 +21,7 @@ use crate::ln::features::{BlindedHopFeatures, Bolt11InvoiceFeatures, Bolt12Invoi use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT}; use crate::ln::onion_utils; use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice}; -use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, OnionMessagePath}; +use crate::onion_message::messenger::{BlindedPathParams, DefaultMessageRouter, Destination, MessageRouter, OnionMessagePath}; use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId}; use crate::routing::scoring::{ChannelUsage, LockableScore, ScoreLookUp}; use crate::sign::EntropySource; @@ -196,9 +196,9 @@ impl< G: Deref>, L: Deref, ES: Deref, S: Deref, SP: Siz fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification > ( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, + &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - DefaultMessageRouter::create_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) + DefaultMessageRouter::create_blinded_paths(params, &self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } fn create_compact_blinded_paths< diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 3f2226d741d..aa717a8d83c 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -20,6 +20,7 @@ use crate::chain::chainmonitor; use crate::chain::channelmonitor; use crate::chain::channelmonitor::MonitorEvent; use crate::chain::transaction::OutPoint; +use crate::onion_message::messenger::BlindedPathParams; use crate::routing::router::{CandidateRouteHop, FirstHopCandidate, PublicHopCandidate, PrivateHopCandidate}; use crate::sign; use crate::events; @@ -268,10 +269,10 @@ impl<'a> MessageRouter for TestRouter<'a> { fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, + &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.router.create_blinded_paths(recipient, context, peers, secp_ctx) + self.router.create_blinded_paths(params, recipient, context, peers, secp_ctx) } fn create_compact_blinded_paths< @@ -313,10 +314,10 @@ impl<'a> MessageRouter for TestMessageRouter<'a> { } fn create_blinded_paths( - &self, recipient: PublicKey, context: MessageContext, + &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.inner.create_blinded_paths(recipient, context, peers, secp_ctx) + self.inner.create_blinded_paths(params, recipient, context, peers, secp_ctx) } fn create_compact_blinded_paths(