Skip to content

Commit

Permalink
Introduce params Field in create_blinded_paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shaavan committed Aug 19, 2024
1 parent 2356ce0 commit 318b72e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 41 deletions.
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl MessageRouter for FuzzRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl MessageRouter for FuzzRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
&self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl MessageRouter for TestMessageRouter {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
&self, _params: BlindedPathParams _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>,
_secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
unreachable!()
Expand Down
34 changes: 23 additions & 11 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
)?
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -9129,11 +9133,15 @@ where
)?;
let builder: InvoiceBuilder<DerivedSigningPubkey> = 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();
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -9301,7 +9313,7 @@ where
/// [`MessageRouter::create_blinded_paths`].
///
/// Errors if the `MessageRouter` errors.
fn create_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
fn create_blinded_paths(&self, params: BlindedPathParams, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
let recipient = self.get_our_node_id();
let secp_ctx = &self.secp_ctx;

Expand All @@ -9314,7 +9326,7 @@ where
.collect::<Vec<_>>();

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

Expand Down
51 changes: 31 additions & 20 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> 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};
Expand All @@ -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, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
/// # &self, _params: BlindedPathParams, _recipient: PublicKey, _context: MessageContext, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>
/// # ) -> Result<Vec<BlindedMessagePath>, ()> {
/// # unreachable!()
/// # }
Expand Down Expand Up @@ -421,7 +421,7 @@ pub struct PendingOnionMessage<T: OnionMessageContents> {
///
/// 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.
///
Expand Down Expand Up @@ -461,7 +461,7 @@ pub trait MessageRouter {
fn create_blinded_paths<
T: secp256k1::Signing + secp256k1::Verification
>(
&self, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()>;

/// Creates compact [`BlindedMessagePath`]s to the `recipient` node. The nodes in `peers` are
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -522,12 +528,9 @@ where
I: ExactSizeIterator<Item = ForwardNode>,
T: secp256k1::Signing + secp256k1::Verification
>(
network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I,
entropy_source: &ES, secp_ctx: &Secp256k1<T>, compact_paths: bool,
params: BlindedPathParams, network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I,
entropy_source: &ES, secp_ctx: &Secp256k1<T>
) -> Result<Vec<BlindedMessagePath>, ()> {
// 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;
Expand Down Expand Up @@ -564,7 +567,7 @@ where
.map(|(peer, _, _)| {
BlindedMessagePath::new(&[peer], recipient, context.clone(), &**entropy_source, secp_ctx)
})
.take(MAX_PATHS)
.take(params.paths)
.collect::<Result<Vec<_>, _>>();

let mut paths = match paths {
Expand All @@ -579,7 +582,7 @@ where
},
}?;

if compact_paths {
if params.is_compact {
for path in &mut paths {
path.use_compact_introduction_node(&network_graph);
}
Expand Down Expand Up @@ -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<PublicKey>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<
Expand All @@ -639,7 +642,11 @@ where
network_graph: &G, recipient: PublicKey, context: MessageContext,
peers: Vec<ForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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)
}
}

Expand All @@ -657,9 +664,9 @@ where
fn create_blinded_paths<
T: secp256k1::Signing + secp256k1::Verification
>(
&self, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<
Expand Down Expand Up @@ -1235,7 +1242,7 @@ where
.map_err(|_| SendError::PathNotFound)
}

fn create_blinded_path(&self, context: MessageContext) -> Result<BlindedMessagePath, SendError> {
fn create_blinded_path(&self, params: BlindedPathParams, context: MessageContext) -> Result<BlindedMessagePath, SendError> {
let recipient = self.node_signer
.get_node_id(Recipient::Node)
.map_err(|_| SendError::GetNodeIdFailed)?;
Expand All @@ -1248,7 +1255,7 @@ where
.collect::<Vec<_>>();

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)
}
Expand Down Expand Up @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -196,9 +196,9 @@ impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Siz
fn create_blinded_paths<
T: secp256k1::Signing + secp256k1::Verification
> (
&self, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<
Expand Down
9 changes: 5 additions & 4 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<
Expand Down Expand Up @@ -313,10 +314,10 @@ impl<'a> MessageRouter for TestMessageRouter<'a> {
}

fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
&self, recipient: PublicKey, context: MessageContext,
&self, params: BlindedPathParams, recipient: PublicKey, context: MessageContext,
peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedMessagePath>, ()> {
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<T: secp256k1::Signing + secp256k1::Verification>(
Expand Down

0 comments on commit 318b72e

Please sign in to comment.