@@ -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
534533impl 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
737751enum FeaturesRef < ' a > {
@@ -1256,7 +1270,8 @@ where L::Target: Logger {
12561270 _ => return Err ( LightningError { err : "Routing to blinded paths isn't supported yet" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
12571271
12581272 }
1259- if payment_params. max_total_cltv_expiry_delta <= payment_params. final_cltv_expiry_delta {
1273+ let final_cltv_expiry_delta = payment_params. payee . final_cltv_expiry_delta ( ) . unwrap_or ( 0 ) ;
1274+ if payment_params. max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
12601275 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 } ) ;
12611276 }
12621277
@@ -1487,9 +1502,9 @@ where L::Target: Logger {
14871502 // In order to already account for some of the privacy enhancing random CLTV
14881503 // expiry delta offset we add on top later, we subtract a rough estimate
14891504 // (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
1490- let max_total_cltv_expiry_delta = ( payment_params. max_total_cltv_expiry_delta - payment_params . final_cltv_expiry_delta)
1505+ let max_total_cltv_expiry_delta = ( payment_params. max_total_cltv_expiry_delta - final_cltv_expiry_delta)
14911506 . checked_sub( 2 * MEDIAN_HOP_CLTV_EXPIRY_DELTA )
1492- . unwrap_or( payment_params. max_total_cltv_expiry_delta - payment_params . final_cltv_expiry_delta) ;
1507+ . unwrap_or( payment_params. max_total_cltv_expiry_delta - final_cltv_expiry_delta) ;
14931508 let hop_total_cltv_delta = ( $next_hops_cltv_delta as u32 )
14941509 . saturating_add( $candidate. cltv_expiry_delta( ) ) ;
14951510 let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
@@ -2179,7 +2194,7 @@ where L::Target: Logger {
21792194 } ) . collect :: < Vec < _ > > ( ) ;
21802195 // Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
21812196 // applicable for the previous hop.
2182- path. iter_mut ( ) . rev ( ) . fold ( payment_params . final_cltv_expiry_delta , |prev_cltv_expiry_delta, hop| {
2197+ path. iter_mut ( ) . rev ( ) . fold ( final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
21832198 core:: mem:: replace ( & mut hop. as_mut ( ) . unwrap ( ) . cltv_expiry_delta , prev_cltv_expiry_delta)
21842199 } ) ;
21852200 selected_paths. push ( path) ;
0 commit comments