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 Sep 5, 2024
1 parent 2db0a82 commit 18f18ef
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 92 deletions.
4 changes: 2 additions & 2 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, MessageContext, MessageForwardNode};
use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs};
use lightning::chain;
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
Expand Down Expand Up @@ -144,7 +144,7 @@ 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>,
_peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
}
Expand Down
4 changes: 2 additions & 2 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, MessageContext, MessageForwardNode};
use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs};
use lightning::chain;
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
Expand Down Expand Up @@ -179,7 +179,7 @@ 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>,
_peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
}
Expand Down
6 changes: 4 additions & 2 deletions fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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, MessageContext, MessageForwardNode, OffersContext,
};
use lightning::blinded_path::EmptyNodeIdLookUp;
use lightning::ln::features::InitFeatures;
use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
Expand Down Expand Up @@ -97,7 +99,7 @@ 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>,
_peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
}
Expand Down
24 changes: 19 additions & 5 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use bitcoin::secp256k1::Secp256k1;
use bitcoin::{secp256k1, Sequence};

use crate::events::FundingInfo;
use crate::blinded_path::message::{MessageContext, OffersContext};
use crate::blinded_path::message::{MessageContext, MessageForwardNode, OffersContext};
use crate::blinded_path::NodeIdLookUp;
use crate::blinded_path::message::BlindedMessagePath;
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12OfferContext, Bolt12RefundContext, PaymentConstraints, PaymentContext, ReceiveTlvs};
Expand Down Expand Up @@ -2499,9 +2499,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
Expand Down Expand Up @@ -9384,7 +9382,23 @@ 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)
.map(|(node_id, peer)| {
if params.is_compact {
MessageForwardNode {
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 {
MessageForwardNode {
node_id: *node_id,
short_channel_id: None,
}
}
})
.collect::<Vec<_>>();

self.router
Expand Down
58 changes: 4 additions & 54 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<MessageForwardNode>, _secp_ctx: &Secp256k1<T>
/// # ) -> Result<Vec<BlindedMessagePath>, ()> {
/// # unreachable!()
/// # }
Expand Down Expand Up @@ -475,36 +475,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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()>;

/// 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 [`MessageForwardNode`] but may be `None` for graceful degradation.
///
/// Implementations using additional intermediate nodes are responsible for using a
/// [`MessageForwardNode`] 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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
let peers = peers
.into_iter()
.map(|MessageForwardNode { node_id, short_channel_id: _ }| node_id)
.collect();

let params = BlindedPathParams::new(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 @@ -638,21 +610,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<BlindedMessagePath>, ()> {
let peers = peers
.into_iter()
.map(|node_id| MessageForwardNode { 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<MessageForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
let params = BlindedPathParams::new(true);
Self::create_blinded_paths_from_iter(params, network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx)
}
}
Expand All @@ -671,19 +630,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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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 @@ -1275,7 +1225,7 @@ where
let peers = self.message_recipients.lock().unwrap()
.iter()
.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
.map(|(node_id, _ )| *node_id)
.map(|(node_id, _)| MessageForwardNode { node_id: *node_id, short_channel_id: None })
.collect::<Vec<_>>();

self.message_router
Expand Down
10 changes: 1 addition & 9 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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 @@ -277,18 +277,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<BlindedMessagePath>, ()> {
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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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 @@ -320,16 +311,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<BlindedMessagePath>, ()> {
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<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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 18f18ef

Please sign in to comment.