@@ -16,7 +16,7 @@ use crate::ln::features::{ChannelFeatures, NodeFeatures};
16
16
use crate :: ln:: msgs;
17
17
use crate :: ln:: types:: { PaymentHash , PaymentPreimage } ;
18
18
use crate :: routing:: gossip:: NetworkUpdate ;
19
- use crate :: routing:: router:: { Path , RouteHop , RouteParameters } ;
19
+ use crate :: routing:: router:: { Path , RouteHop , RouteParameters , RouteParametersV2 } ;
20
20
use crate :: sign:: NodeSigner ;
21
21
use crate :: util:: errors:: { self , APIError } ;
22
22
use crate :: util:: logger:: Logger ;
@@ -381,6 +381,74 @@ pub(crate) fn set_max_path_length(
381
381
Ok ( ( ) )
382
382
}
383
383
384
+ pub ( crate ) fn set_max_path_length_v2 (
385
+ route_params : & mut RouteParametersV2 , recipient_onion : & RecipientOnionFields ,
386
+ keysend_preimage : Option < PaymentPreimage > , best_block_height : u32 ,
387
+ ) -> Result < ( ) , ( ) > {
388
+ const PAYLOAD_HMAC_LEN : usize = 32 ;
389
+ let unblinded_intermed_payload_len = msgs:: OutboundOnionPayload :: Forward {
390
+ short_channel_id : 42 ,
391
+ amt_to_forward : TOTAL_BITCOIN_SUPPLY_SATOSHIS ,
392
+ outgoing_cltv_value : route_params. user_params . max_total_cltv_expiry_delta ,
393
+ }
394
+ . serialized_length ( )
395
+ . saturating_add ( PAYLOAD_HMAC_LEN ) ;
396
+
397
+ const OVERPAY_ESTIMATE_MULTIPLER : u64 = 3 ;
398
+ let final_value_msat_with_overpay_buffer = core:: cmp:: max (
399
+ route_params. final_value_msat . saturating_mul ( OVERPAY_ESTIMATE_MULTIPLER ) ,
400
+ MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY ,
401
+ ) ;
402
+
403
+ let blinded_tail_opt = route_params
404
+ . invoice_params
405
+ . payee
406
+ . blinded_route_hints ( )
407
+ . iter ( )
408
+ . max_by_key ( |path| path. inner_blinded_path ( ) . serialized_length ( ) )
409
+ . map ( |largest_path| BlindedTailHopIter {
410
+ hops : largest_path. blinded_hops ( ) . iter ( ) ,
411
+ blinding_point : largest_path. blinding_point ( ) ,
412
+ final_value_msat : final_value_msat_with_overpay_buffer,
413
+ excess_final_cltv_expiry_delta : 0 ,
414
+ } ) ;
415
+
416
+ let unblinded_route_hop = RouteHop {
417
+ pubkey : PublicKey :: from_slice ( & [ 2 ; 33 ] ) . unwrap ( ) ,
418
+ node_features : NodeFeatures :: empty ( ) ,
419
+ short_channel_id : 42 ,
420
+ channel_features : ChannelFeatures :: empty ( ) ,
421
+ fee_msat : final_value_msat_with_overpay_buffer,
422
+ cltv_expiry_delta : route_params. user_params . max_total_cltv_expiry_delta ,
423
+ maybe_announced_channel : false ,
424
+ } ;
425
+ let mut num_reserved_bytes: usize = 0 ;
426
+ let build_payloads_res = build_onion_payloads_callback (
427
+ core:: iter:: once ( & unblinded_route_hop) ,
428
+ blinded_tail_opt,
429
+ final_value_msat_with_overpay_buffer,
430
+ & recipient_onion,
431
+ best_block_height,
432
+ & keysend_preimage,
433
+ |_, payload| {
434
+ num_reserved_bytes = num_reserved_bytes
435
+ . saturating_add ( payload. serialized_length ( ) )
436
+ . saturating_add ( PAYLOAD_HMAC_LEN ) ;
437
+ } ,
438
+ ) ;
439
+ debug_assert ! ( build_payloads_res. is_ok( ) ) ;
440
+
441
+ let max_path_length = 1300usize
442
+ . checked_sub ( num_reserved_bytes)
443
+ . map ( |p| p / unblinded_intermed_payload_len)
444
+ . and_then ( |l| u8:: try_from ( l. saturating_add ( 1 ) ) . ok ( ) )
445
+ . ok_or ( ( ) ) ?;
446
+
447
+ route_params. user_params . max_path_length =
448
+ core:: cmp:: min ( max_path_length, route_params. user_params . max_path_length ) ;
449
+ Ok ( ( ) )
450
+ }
451
+
384
452
/// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
385
453
/// the hops can be of variable length.
386
454
pub ( crate ) const ONION_DATA_LEN : usize = 20 * 65 ;
0 commit comments