Skip to content

Commit f7d4237

Browse files
Move final_cltv_expiry_delta from PaymentParams to Payee::Clear
Since blinded pay params won't have this value.
1 parent ab0fbf2 commit f7d4237

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, No
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::InvoiceFeatures;
4747
use crate::routing::gossip::NetworkGraph;
48-
use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters, Router};
48+
use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteHop, RouteParameters, Router};
4949
use crate::routing::scoring::ProbabilisticScorer;
5050
use crate::ln::msgs;
5151
use crate::ln::onion_utils;
@@ -7061,8 +7061,10 @@ impl Readable for HTLCSource {
70617061
return Err(DecodeError::InvalidValue);
70627062
}
70637063
if let Some(params) = payment_params.as_mut() {
7064-
if params.final_cltv_expiry_delta == 0 {
7065-
params.final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
7064+
if let Payee::Clear { ref mut final_cltv_expiry_delta, .. } = params.payee {
7065+
if final_cltv_expiry_delta == &0 {
7066+
*final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
7067+
}
70667068
}
70677069
}
70687070
Ok(HTLCSource::OutboundRoute {

lightning/src/routing/router.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl Writeable for RouteParameters {
442442
(2, self.final_value_msat, required),
443443
// LDK versions prior to 0.0.114 had the `final_cltv_expiry_delta` parameter in
444444
// `RouteParameters` directly. For compatibility, we write it here.
445-
(4, self.payment_params.final_cltv_expiry_delta, required),
445+
(4, self.payment_params.payee.final_cltv_expiry_delta(), option),
446446
});
447447
Ok(())
448448
}
@@ -453,11 +453,13 @@ impl Readable for RouteParameters {
453453
_init_and_read_tlv_fields!(reader, {
454454
(0, payment_params, (required: ReadableArgs, 0)),
455455
(2, final_value_msat, required),
456-
(4, final_cltv_expiry_delta, required),
456+
(4, final_cltv_delta, option),
457457
});
458458
let mut payment_params: PaymentParameters = payment_params.0.unwrap();
459-
if payment_params.final_cltv_expiry_delta == 0 {
460-
payment_params.final_cltv_expiry_delta = final_cltv_expiry_delta.0.unwrap();
459+
if let Payee::Clear { ref mut final_cltv_expiry_delta, .. } = payment_params.payee {
460+
if final_cltv_expiry_delta == &0 {
461+
*final_cltv_expiry_delta = final_cltv_delta.ok_or(DecodeError::InvalidValue)?;
462+
}
461463
}
462464
Ok(Self {
463465
payment_params,
@@ -526,9 +528,6 @@ pub struct PaymentParameters {
526528
/// payment to fail. Future attempts for the same payment shouldn't be relayed through any of
527529
/// these SCIDs.
528530
pub previously_failed_channels: Vec<u64>,
529-
530-
/// The minimum CLTV delta at the end of the route. This value must not be zero.
531-
pub final_cltv_expiry_delta: u32,
532531
}
533532

534533
impl Writeable for PaymentParameters {
@@ -549,7 +548,7 @@ impl Writeable for PaymentParameters {
549548
(6, self.expiry_time, option),
550549
(7, self.previously_failed_channels, vec_type),
551550
(8, *blinded_hints, optional_vec),
552-
(9, self.final_cltv_expiry_delta, required),
551+
(9, self.payee.final_cltv_expiry_delta(), option),
553552
});
554553
Ok(())
555554
}
@@ -579,6 +578,7 @@ impl ReadableArgs<u32> for PaymentParameters {
579578
route_hints: clear_route_hints,
580579
node_id: payee_pubkey.ok_or(DecodeError::InvalidValue)?,
581580
features,
581+
final_cltv_expiry_delta: final_cltv_expiry_delta.0.unwrap(),
582582
}
583583
};
584584
Ok(Self {
@@ -588,7 +588,6 @@ impl ReadableArgs<u32> for PaymentParameters {
588588
max_channel_saturation_power_of_half: _init_tlv_based_struct_field!(max_channel_saturation_power_of_half, (default_value, unused)),
589589
expiry_time,
590590
previously_failed_channels: previously_failed_channels.unwrap_or(Vec::new()),
591-
final_cltv_expiry_delta: _init_tlv_based_struct_field!(final_cltv_expiry_delta, (default_value, unused)),
592591
})
593592
}
594593
}
@@ -601,13 +600,12 @@ impl PaymentParameters {
601600
/// provided.
602601
pub fn from_node_id(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32) -> Self {
603602
Self {
604-
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![], features: None },
603+
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![], features: None, final_cltv_expiry_delta },
605604
expiry_time: None,
606605
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
607606
max_path_count: DEFAULT_MAX_PATH_COUNT,
608607
max_channel_saturation_power_of_half: 2,
609608
previously_failed_channels: Vec::new(),
610-
final_cltv_expiry_delta,
611609
}
612610
}
613611

@@ -626,8 +624,12 @@ impl PaymentParameters {
626624
pub fn with_bolt11_features(self, features: InvoiceFeatures) -> Result<Self, ()> {
627625
match self.payee {
628626
Payee::Blinded(_) => Err(()),
629-
Payee::Clear { route_hints, node_id, .. } =>
630-
Ok(Self { payee: Payee::Clear { route_hints, node_id, features: Some(features) }, ..self })
627+
Payee::Clear { route_hints, node_id, final_cltv_expiry_delta, .. } =>
628+
Ok(Self {
629+
payee: Payee::Clear {
630+
route_hints, node_id, features: Some(features), final_cltv_expiry_delta
631+
}, ..self
632+
})
631633
}
632634
}
633635

@@ -638,8 +640,12 @@ impl PaymentParameters {
638640
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {
639641
match self.payee {
640642
Payee::Blinded(_) => Err(()),
641-
Payee::Clear { node_id, features, .. } =>
642-
Ok(Self { payee: Payee::Clear { route_hints, node_id, features }, ..self })
643+
Payee::Clear { node_id, features, final_cltv_expiry_delta, .. } =>
644+
Ok(Self {
645+
payee: Payee::Clear {
646+
route_hints, node_id, features, final_cltv_expiry_delta,
647+
}, ..self
648+
})
643649
}
644650
}
645651

@@ -692,6 +698,8 @@ pub enum Payee {
692698
///
693699
/// [`for_keysend`]: PaymentParameters::for_keysend
694700
features: Option<InvoiceFeatures>,
701+
/// The minimum CLTV delta at the end of the route. This value must not be zero.
702+
final_cltv_expiry_delta: u32,
695703
},
696704
}
697705

@@ -708,6 +716,12 @@ impl Payee {
708716
_ => &None,
709717
}
710718
}
719+
fn final_cltv_expiry_delta(&self) -> Option<u32> {
720+
match self {
721+
Self::Clear { final_cltv_expiry_delta, .. } => Some(*final_cltv_expiry_delta),
722+
_ => None,
723+
}
724+
}
711725
}
712726

713727
/// A list of hops along a payment path terminating with a channel to the recipient.
@@ -1174,7 +1188,8 @@ where L::Target: Logger {
11741188
_ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
11751189

11761190
}
1177-
if payment_params.max_total_cltv_expiry_delta <= payment_params.final_cltv_expiry_delta {
1191+
let final_cltv_expiry_delta = payment_params.payee.final_cltv_expiry_delta().unwrap_or(0);
1192+
if payment_params.max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
11781193
return Err(LightningError{err: "Can't find a route where the maximum total CLTV expiry delta is below the final CLTV expiry.".to_owned(), action: ErrorAction::IgnoreError});
11791194
}
11801195

@@ -1406,9 +1421,9 @@ where L::Target: Logger {
14061421
// In order to already account for some of the privacy enhancing random CLTV
14071422
// expiry delta offset we add on top later, we subtract a rough estimate
14081423
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
1409-
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta)
1424+
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
14101425
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
1411-
.unwrap_or(payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta);
1426+
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
14121427
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
14131428
.saturating_add($candidate.cltv_expiry_delta());
14141429
let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
@@ -2098,7 +2113,7 @@ where L::Target: Logger {
20982113
}).collect::<Vec<_>>();
20992114
// Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
21002115
// applicable for the previous hop.
2101-
path.iter_mut().rev().fold(payment_params.final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2116+
path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
21022117
core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
21032118
});
21042119
selected_paths.push(path);

0 commit comments

Comments
 (0)