From 8263e41faa83d7eaeed396f519bc6ee97dad48c8 Mon Sep 17 00:00:00 2001 From: shaavan Date: Tue, 13 Aug 2024 15:03:56 +0530 Subject: [PATCH] Remove `create_compact_blinded_paths` 1. The `params` parameter now allows specifying the type of blinded path to be created, enabling a single function to handle the processing for both compact and full-length blinded paths. 2. This change renders the `create_compact_blinded_paths` function redundant, leading to its removal. 3. With this commit, the blinded path creation process is streamlined to a single function capable of creating any type of blinded path. --- fuzz/src/chanmon_consistency.rs | 10 ++-- fuzz/src/full_stack.rs | 10 ++-- fuzz/src/onion_message.rs | 10 ++-- lightning/src/ln/channelmanager.rs | 51 +++++++------------ lightning/src/onion_message/messenger.rs | 65 ++---------------------- lightning/src/routing/router.rs | 12 +---- lightning/src/util/test_utils.rs | 20 +------- 7 files changed, 43 insertions(+), 135 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 5b48f121acd..cc6abfc6620 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -33,7 +33,7 @@ use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash as TraitImport; use bitcoin::WPubkeyHash; -use lightning::blinded_path::message::{BlindedMessagePath, MessageContext}; +use lightning::blinded_path::message::{BlindedMessagePath, ForwardNode, MessageContext}; use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs}; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; @@ -58,7 +58,9 @@ use lightning::ln::script::ShutdownScript; use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice}; use lightning::offers::invoice_request::UnsignedInvoiceRequest; -use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath}; +use lightning::onion_message::messenger::{ + BlindedPathParams, Destination, MessageRouter, OnionMessagePath, +}; use lightning::routing::router::{InFlightHtlcs, Path, Route, RouteHop, RouteParameters, Router}; use lightning::sign::{ EntropySource, InMemorySigner, KeyMaterial, NodeSigner, Recipient, SignerProvider, @@ -139,8 +141,8 @@ impl MessageRouter for FuzzRouter { } fn create_blinded_paths( - &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, - _secp_ctx: &Secp256k1, + &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 dbdd484e483..c87ea233ba5 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -30,7 +30,7 @@ use bitcoin::hashes::Hash as _; use bitcoin::hex::FromHex; use bitcoin::WPubkeyHash; -use lightning::blinded_path::message::{BlindedMessagePath, MessageContext}; +use lightning::blinded_path::message::{BlindedMessagePath, ForwardNode, MessageContext}; use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs}; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; @@ -51,7 +51,9 @@ use lightning::ln::script::ShutdownScript; use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice}; use lightning::offers::invoice_request::UnsignedInvoiceRequest; -use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath}; +use lightning::onion_message::messenger::{ + BlindedPathParams, Destination, MessageRouter, OnionMessagePath, +}; use lightning::routing::gossip::{NetworkGraph, P2PGossipSync}; use lightning::routing::router::{ InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router, @@ -176,8 +178,8 @@ impl MessageRouter for FuzzRouter { } fn create_blinded_paths( - &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, - _secp_ctx: &Secp256k1, + &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 7d5b86b0294..b5567ea971a 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -5,7 +5,7 @@ use bitcoin::secp256k1::ecdsa::RecoverableSignature; use bitcoin::secp256k1::schnorr; use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use lightning::blinded_path::message::{BlindedMessagePath, MessageContext, OffersContext}; +use lightning::blinded_path::message::{BlindedMessagePath, ForwardNode, MessageContext, OffersContext}; use lightning::blinded_path::EmptyNodeIdLookUp; use lightning::ln::features::InitFeatures; use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler}; @@ -16,8 +16,8 @@ use lightning::onion_message::async_payments::{ AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc, }; use lightning::onion_message::messenger::{ - CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, - PendingOnionMessage, Responder, ResponseInstruction, + BlindedPathParams, CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, + OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, }; use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler}; use lightning::onion_message::packet::OnionMessageContents; @@ -96,8 +96,8 @@ impl MessageRouter for TestMessageRouter { } fn create_blinded_paths( - &self, _params: BlindedPathParams _recipient: PublicKey, _context: MessageContext, _peers: Vec, - _secp_ctx: &Secp256k1, + &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 403ffefea85..f9d93982041 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -2481,9 +2481,7 @@ const MAX_NO_CHANNEL_PEERS: usize = 250; /// short-lived, while anything with a greater expiration is considered long-lived. /// /// Using [`ChannelManager::create_offer_builder`] or [`ChannelManager::create_refund_builder`], -/// will included a [`BlindedMessagePath`] created using: -/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and -/// - [`MessageRouter::create_blinded_paths`] when long-lived. +/// will included a [`BlindedMessagePath`] created using [`MessageRouter::create_blinded_paths`]. /// /// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select /// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to @@ -9312,40 +9310,27 @@ where .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap())) .filter(|(_, peer)| peer.is_connected) .filter(|(_, peer)| peer.latest_features.supports_onion_messages()) - .map(|(node_id, _)| *node_id) - .collect::>(); - - self.router - .create_blinded_paths(params, recipient, MessageContext::Offers(context), peers, secp_ctx) - .and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(())) - } - - /// Creates a collection of blinded paths by delegating to - /// [`MessageRouter::create_compact_blinded_paths`]. - /// - /// Errors if the `MessageRouter` errors. - #[allow(unused)] - fn create_compact_blinded_paths(&self, context: OffersContext) -> Result, ()> { - let recipient = self.get_our_node_id(); - let secp_ctx = &self.secp_ctx; - - let peers = self.per_peer_state.read().unwrap() - .iter() - .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap())) - .filter(|(_, peer)| peer.is_connected) - .filter(|(_, peer)| peer.latest_features.supports_onion_messages()) - .map(|(node_id, peer)| ForwardNode { - node_id: *node_id, - short_channel_id: peer.channel_by_id - .iter() - .filter(|(_, channel)| channel.context().is_usable()) - .min_by_key(|(_, channel)| channel.context().channel_creation_height) - .and_then(|(_, channel)| channel.context().get_short_channel_id()), + .map(|(node_id, peer)| { + if params.is_compact { + ForwardNode { + node_id: *node_id, + short_channel_id: peer.channel_by_id + .iter() + .filter(|(_, channel)| channel.context().is_usable()) + .min_by_key(|(_, channel)| channel.context().channel_creation_height) + .and_then(|(_, channel)| channel.context().get_short_channel_id()), + } + } else { + ForwardNode { + node_id: *node_id, + short_channel_id: None, + } + } }) .collect::>(); self.router - .create_compact_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 7c4f21bce56..be575d7e066 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -175,7 +175,7 @@ for OnionMessenger where /// # }) /// # } /// # fn create_blinded_paths( -/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1 +/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec, _secp_ctx: &Secp256k1 /// # ) -> Result, ()> { /// # unreachable!() /// # } @@ -461,40 +461,8 @@ pub trait MessageRouter { fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, params: BlindedPathParams, 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 - /// assumed to be direct peers with the `recipient`. - /// - /// Compact blinded paths use short channel ids instead of pubkeys for a smaller serialization, - /// which is beneficial when a QR code is used to transport the data. The SCID is passed using a - /// [`ForwardNode`] but may be `None` for graceful degradation. - /// - /// Implementations using additional intermediate nodes are responsible for using a - /// [`ForwardNode`] with `Some` short channel id, if possible. Similarly, implementations should - /// call [`BlindedMessagePath::use_compact_introduction_node`]. - /// - /// The provided implementation simply delegates to [`MessageRouter::create_blinded_paths`], - /// ignoring the short channel ids. - fn create_compact_blinded_paths< - T: secp256k1::Signing + secp256k1::Verification - >( - &self, recipient: PublicKey, context: MessageContext, - peers: Vec, secp_ctx: &Secp256k1, - ) -> Result, ()> { - let peers = peers - .into_iter() - .map(|ForwardNode { node_id, short_channel_id: _ }| node_id) - .collect(); - - // 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) - } } /// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. @@ -628,24 +596,8 @@ where T: secp256k1::Signing + secp256k1::Verification >( 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(params, network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx) - } - - pub(crate) fn create_compact_blinded_paths< - T: secp256k1::Signing + secp256k1::Verification - >( - network_graph: &G, recipient: PublicKey, context: MessageContext, peers: Vec, entropy_source: &ES, secp_ctx: &Secp256k1, ) -> Result, ()> { - 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) } } @@ -664,19 +616,10 @@ where fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, params: BlindedPathParams, 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(params, &self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } - - fn create_compact_blinded_paths< - T: secp256k1::Signing + secp256k1::Verification - >( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, - ) -> Result, ()> { - Self::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) - } - } /// A path for sending an [`OnionMessage`]. @@ -1251,7 +1194,7 @@ where let peers = self.message_recipients.lock().unwrap() .iter() .filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_))) - .map(|(node_id, _ )| *node_id) + .map(|(node_id, _)| ForwardNode { node_id: *node_id, short_channel_id: None }) .collect::>(); self.message_router diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 46210b0af8a..6aefc5fa9bc 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -12,7 +12,7 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1, self}; use crate::blinded_path::{BlindedHop, Direction, IntroductionNode}; -use crate::blinded_path::message::{self, BlindedMessagePath, MessageContext}; +use crate::blinded_path::message::{ForwardNode, BlindedMessagePath, MessageContext}; use crate::blinded_path::payment::{BlindedPaymentPath, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs, self}; use crate::ln::{PaymentHash, PaymentPreimage}; use crate::ln::channel_state::ChannelDetails; @@ -196,18 +196,10 @@ impl< G: Deref>, L: Deref, ES: Deref, S: Deref, SP: Siz fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification > ( - &self, params: BlindedPathParams, 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(params, &self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } - - fn create_compact_blinded_paths< - T: secp256k1::Signing + secp256k1::Verification - > ( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, - ) -> Result, ()> { - DefaultMessageRouter::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) - } } /// A trait defining behavior for routing a payment. diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index aa717a8d83c..d8e7213da7b 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -270,18 +270,9 @@ impl<'a> MessageRouter for TestRouter<'a> { T: secp256k1::Signing + secp256k1::Verification >( &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, - peers: Vec, secp_ctx: &Secp256k1, - ) -> Result, ()> { - self.router.create_blinded_paths(params, recipient, context, peers, secp_ctx) - } - - fn create_compact_blinded_paths< - T: secp256k1::Signing + secp256k1::Verification - >( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.router.create_compact_blinded_paths(recipient, context, peers, secp_ctx) + self.router.create_blinded_paths(params, recipient, context, peers, secp_ctx) } } @@ -315,16 +306,9 @@ impl<'a> MessageRouter for TestMessageRouter<'a> { fn create_blinded_paths( &self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, - peers: Vec, secp_ctx: &Secp256k1, - ) -> Result, ()> { - self.inner.create_blinded_paths(params, recipient, context, peers, secp_ctx) - } - - fn create_compact_blinded_paths( - &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.inner.create_compact_blinded_paths(recipient, context, peers, secp_ctx) + self.inner.create_blinded_paths(params, recipient, context, peers, secp_ctx) } }