Skip to content

Commit dca58c3

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

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
}
@@ -582,6 +581,7 @@ impl ReadableArgs<u32> for PaymentParameters {
582581
route_hints: clear_route_hints,
583582
node_id: payee_pubkey.ok_or(DecodeError::InvalidValue)?,
584583
features: features.and_then(|f| f.bolt11()),
584+
final_cltv_expiry_delta: final_cltv_expiry_delta.0.unwrap(),
585585
}
586586
};
587587
Ok(Self {
@@ -591,7 +591,6 @@ impl ReadableArgs<u32> for PaymentParameters {
591591
max_channel_saturation_power_of_half: _init_tlv_based_struct_field!(max_channel_saturation_power_of_half, (default_value, unused)),
592592
expiry_time,
593593
previously_failed_channels: previously_failed_channels.unwrap_or(Vec::new()),
594-
final_cltv_expiry_delta: _init_tlv_based_struct_field!(final_cltv_expiry_delta, (default_value, unused)),
595594
})
596595
}
597596
}
@@ -604,13 +603,12 @@ impl PaymentParameters {
604603
/// provided.
605604
pub fn from_node_id(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32) -> Self {
606605
Self {
607-
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![], features: None },
606+
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![], features: None, final_cltv_expiry_delta },
608607
expiry_time: None,
609608
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
610609
max_path_count: DEFAULT_MAX_PATH_COUNT,
611610
max_channel_saturation_power_of_half: 2,
612611
previously_failed_channels: Vec::new(),
613-
final_cltv_expiry_delta,
614612
}
615613
}
616614

@@ -629,8 +627,12 @@ impl PaymentParameters {
629627
pub fn with_bolt11_features(self, features: InvoiceFeatures) -> Result<Self, ()> {
630628
match self.payee {
631629
Payee::Blinded { .. } => Err(()),
632-
Payee::Clear { route_hints, node_id, .. } =>
633-
Ok(Self { payee: Payee::Clear { route_hints, node_id, features: Some(features) }, ..self })
630+
Payee::Clear { route_hints, node_id, final_cltv_expiry_delta, .. } =>
631+
Ok(Self {
632+
payee: Payee::Clear {
633+
route_hints, node_id, features: Some(features), final_cltv_expiry_delta
634+
}, ..self
635+
})
634636
}
635637
}
636638

@@ -641,8 +643,12 @@ impl PaymentParameters {
641643
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {
642644
match self.payee {
643645
Payee::Blinded { .. } => Err(()),
644-
Payee::Clear { node_id, features, .. } =>
645-
Ok(Self { payee: Payee::Clear { route_hints, node_id, features }, ..self })
646+
Payee::Clear { node_id, features, final_cltv_expiry_delta, .. } =>
647+
Ok(Self {
648+
payee: Payee::Clear {
649+
route_hints, node_id, features, final_cltv_expiry_delta,
650+
}, ..self
651+
})
646652
}
647653
}
648654

@@ -704,6 +710,8 @@ pub enum Payee {
704710
///
705711
/// [`for_keysend`]: PaymentParameters::for_keysend
706712
features: Option<InvoiceFeatures>,
713+
/// The minimum CLTV delta at the end of the route. This value must not be zero.
714+
final_cltv_expiry_delta: u32,
707715
},
708716
}
709717

@@ -732,6 +740,12 @@ impl Payee {
732740
Self::Blinded { features, .. } => features.as_ref().map(|f| FeaturesRef::Bolt12(f)),
733741
}
734742
}
743+
fn final_cltv_expiry_delta(&self) -> Option<u32> {
744+
match self {
745+
Self::Clear { final_cltv_expiry_delta, .. } => Some(*final_cltv_expiry_delta),
746+
_ => None,
747+
}
748+
}
735749
}
736750

737751
enum FeaturesRef<'a> {
@@ -1241,7 +1255,8 @@ where L::Target: Logger {
12411255
_ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
12421256

12431257
}
1244-
if payment_params.max_total_cltv_expiry_delta <= payment_params.final_cltv_expiry_delta {
1258+
let final_cltv_expiry_delta = payment_params.payee.final_cltv_expiry_delta().unwrap_or(0);
1259+
if payment_params.max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
12451260
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});
12461261
}
12471262

@@ -1473,9 +1488,9 @@ where L::Target: Logger {
14731488
// In order to already account for some of the privacy enhancing random CLTV
14741489
// expiry delta offset we add on top later, we subtract a rough estimate
14751490
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
1476-
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta)
1491+
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
14771492
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
1478-
.unwrap_or(payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta);
1493+
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
14791494
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
14801495
.saturating_add($candidate.cltv_expiry_delta());
14811496
let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
@@ -2165,7 +2180,7 @@ where L::Target: Logger {
21652180
}).collect::<Vec<_>>();
21662181
// Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
21672182
// applicable for the previous hop.
2168-
path.iter_mut().rev().fold(payment_params.final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2183+
path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
21692184
core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
21702185
});
21712186
selected_paths.push(path);

0 commit comments

Comments
 (0)