diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index b790100ffa1..e9b17b0b45e 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -59,7 +59,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, @@ -140,8 +142,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 f594880c225..8df448cc726 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -52,7 +52,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, @@ -177,8 +179,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 79c7d7b4567..7512b10623d 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -19,7 +19,7 @@ use lightning::onion_message::messenger::{ CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, }; -use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler}; +use lightning::onion_message::offers::{BlindedPathParams, OffersMessage, OffersMessageHandler}; use lightning::onion_message::packet::OnionMessageContents; use lightning::sign::{EntropySource, KeyMaterial, NodeSigner, Recipient, SignerProvider}; use lightning::util::logger::Logger; @@ -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 c1cee36fa5e..fbd2c4a836d 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 [`BlindedPath`] created using: -/// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and -/// - [`MessageRouter::create_blinded_paths`] when long-lived. +/// will included a [`BlindedPath`] created using [`MessageRouter::create_blinded_paths`]. /// /// Using compact [`BlindedPath`]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 b3de8c1e50f..aa1bea6ed48 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 [`BlindedPath`]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 [`BlindedPath::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 30590cda613..7c55df7e761 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, BlindedPath, Direction, IntroductionNode}; -use crate::blinded_path::message::{self, MessageContext}; +use crate::blinded_path::message::{ForwardNode, MessageContext}; use crate::blinded_path::payment::{ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs, self}; use crate::ln::{PaymentHash, PaymentPreimage}; use crate::ln::channel_state::ChannelDetails; @@ -195,18 +195,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 c237b62fd4e..7244ec05e59 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -271,18 +271,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) } } @@ -316,16 +307,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) } }