Skip to content

Commit

Permalink
Remove create_compact_blinded_paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shaavan committed Aug 16, 2024
1 parent 27ca6ca commit 6fa28be
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 131 deletions.
8 changes: 5 additions & 3 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -140,8 +142,8 @@ impl MessageRouter for FuzzRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext,
_peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
unreachable!()
}
Expand Down
8 changes: 5 additions & 3 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -177,8 +179,8 @@ impl MessageRouter for FuzzRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext,
_peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
unreachable!()
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,8 +96,8 @@ impl MessageRouter for TestMessageRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _params: BlindedPathParams _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext,
_peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
unreachable!()
}
Expand Down
51 changes: 18 additions & 33 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<Vec<_>>();

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<Vec<BlindedPath>, ()> {
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::<Vec<_>>();

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(()))
}

Expand Down
65 changes: 4 additions & 61 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
/// # })
/// # }
/// # fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<ForwardNode>, _secp_ctx: &Secp256k1<T>
/// # ) -> Result<Vec<BlindedPath>, ()> {
/// # unreachable!()
/// # }
Expand Down Expand Up @@ -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<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()>;

/// 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<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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`].
Expand Down Expand Up @@ -628,24 +596,8 @@ where
T: secp256k1::Signing + secp256k1::Verification
>(
params: BlindedPathParams, network_graph: &G, recipient: PublicKey, context: MessageContext,
peers: Vec<PublicKey>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<ForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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)
}
}
Expand All @@ -664,19 +616,10 @@ where
fn create_blinded_paths<
T: secp256k1::Signing + secp256k1::Verification
>(
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
Self::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx)
}

}

/// A path for sending an [`OnionMessage`].
Expand Down Expand Up @@ -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::<Vec<_>>();

self.message_router
Expand Down
12 changes: 2 additions & 10 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -195,18 +195,10 @@ impl< G: Deref<Target = NetworkGraph<L>>, 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<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<message::ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
DefaultMessageRouter::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx)
}
}

/// A trait defining behavior for routing a payment.
Expand Down
20 changes: 2 additions & 18 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,9 @@ impl<'a> MessageRouter for TestRouter<'a> {
T: secp256k1::Signing + secp256k1::Verification
>(
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext,
peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
self.router.create_compact_blinded_paths(recipient, context, peers, secp_ctx)
self.router.create_blinded_paths(params, recipient, context, peers, secp_ctx)
}
}

Expand Down Expand Up @@ -316,16 +307,9 @@ impl<'a> MessageRouter for TestMessageRouter<'a> {

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext,
peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
self.inner.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<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
self.inner.create_compact_blinded_paths(recipient, context, peers, secp_ctx)
self.inner.create_blinded_paths(params, recipient, context, peers, secp_ctx)
}
}

Expand Down

0 comments on commit 6fa28be

Please sign in to comment.