@@ -11,11 +11,12 @@ use crate::blinded_path::BlindedHop;
11
11
use crate :: crypto:: chacha20:: ChaCha20 ;
12
12
use crate :: crypto:: streams:: ChaChaReader ;
13
13
use crate :: ln:: channelmanager:: { HTLCSource , RecipientOnionFields } ;
14
+ use crate :: ln:: features:: { ChannelFeatures , NodeFeatures } ;
14
15
use crate :: ln:: msgs;
15
16
use crate :: ln:: types:: { PaymentHash , PaymentPreimage } ;
16
17
use crate :: ln:: wire:: Encode ;
17
18
use crate :: routing:: gossip:: NetworkUpdate ;
18
- use crate :: routing:: router:: { Path , RouteHop } ;
19
+ use crate :: routing:: router:: { Path , Payee , RouteHop , RouteParameters , MAX_PATH_LENGTH_ESTIMATE } ;
19
20
use crate :: sign:: NodeSigner ;
20
21
use crate :: util:: errors:: { self , APIError } ;
21
22
use crate :: util:: logger:: Logger ;
@@ -310,6 +311,81 @@ where
310
311
Ok ( ( cur_value_msat, cur_cltv) )
311
312
}
312
313
314
+ pub ( crate ) fn set_max_path_length (
315
+ route_params : & mut RouteParameters , recipient_onion : & RecipientOnionFields ,
316
+ keysend_preimage : Option < PaymentPreimage > , best_block_height : u32 ,
317
+ ) -> Result < ( ) , ( ) > {
318
+ const PAYLOAD_HMAC_LEN : usize = 32 ;
319
+ let unblinded_intermed_payload_len = msgs:: OutboundOnionPayload :: Forward {
320
+ short_channel_id : 42 ,
321
+ amt_to_forward : u64:: max_value ( ) ,
322
+ outgoing_cltv_value : u32:: max_value ( ) ,
323
+ }
324
+ . serialized_length ( )
325
+ . saturating_add ( PAYLOAD_HMAC_LEN ) ;
326
+
327
+ let ( cltv_expiry_delta, num_reserved_hops, blinded_tail_opt) =
328
+ match & route_params. payment_params . payee {
329
+ Payee :: Blinded { route_hints, .. } => {
330
+ let ( blinded_payinfo, largest_path) = route_hints
331
+ . iter ( )
332
+ . max_by ( |( _, path_a) , ( _, path_b) | {
333
+ path_a. serialized_length ( ) . cmp ( & path_b. serialized_length ( ) )
334
+ } )
335
+ . ok_or ( ( ) ) ?;
336
+ let blinded_tail = BlindedTailHopIter {
337
+ hops : largest_path. blinded_hops . iter ( ) ,
338
+ blinding_point : largest_path. blinding_point ,
339
+ final_value_msat : route_params. final_value_msat ,
340
+ excess_final_cltv_expiry_delta : 0 ,
341
+ } ;
342
+ (
343
+ blinded_payinfo. cltv_expiry_delta as u32 ,
344
+ largest_path. blinded_hops . len ( ) ,
345
+ Some ( blinded_tail) ,
346
+ )
347
+ } ,
348
+ Payee :: Clear { final_cltv_expiry_delta, .. } => {
349
+ ( * final_cltv_expiry_delta, 1 as usize , None )
350
+ } ,
351
+ } ;
352
+
353
+ let unblinded_route_hop = RouteHop {
354
+ pubkey : PublicKey :: from_slice ( & [ 2 ; 33 ] ) . unwrap ( ) ,
355
+ node_features : NodeFeatures :: empty ( ) ,
356
+ short_channel_id : 42 ,
357
+ channel_features : ChannelFeatures :: empty ( ) ,
358
+ fee_msat : route_params. final_value_msat ,
359
+ cltv_expiry_delta,
360
+ maybe_announced_channel : false ,
361
+ } ;
362
+ let mut num_reserved_bytes: usize = 0 ;
363
+ let build_payloads_res = build_onion_payloads_callback (
364
+ core:: iter:: once ( & unblinded_route_hop) ,
365
+ blinded_tail_opt,
366
+ route_params. final_value_msat ,
367
+ & recipient_onion,
368
+ best_block_height,
369
+ & keysend_preimage,
370
+ |_, payload| {
371
+ num_reserved_bytes = num_reserved_bytes
372
+ . saturating_add ( payload. serialized_length ( ) )
373
+ . saturating_add ( PAYLOAD_HMAC_LEN ) ;
374
+ } ,
375
+ ) ;
376
+ debug_assert ! ( build_payloads_res. is_ok( ) ) ;
377
+
378
+ let max_path_length = 1300usize
379
+ . checked_sub ( num_reserved_bytes)
380
+ . map ( |p| p / unblinded_intermed_payload_len)
381
+ . and_then ( |l| u8:: try_from ( l. saturating_add ( num_reserved_hops) ) . ok ( ) )
382
+ . ok_or ( ( ) ) ?;
383
+
384
+ route_params. payment_params . max_path_length =
385
+ core:: cmp:: min ( max_path_length, MAX_PATH_LENGTH_ESTIMATE ) ;
386
+ Ok ( ( ) )
387
+ }
388
+
313
389
/// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
314
390
/// the hops can be of variable length.
315
391
pub ( crate ) const ONION_DATA_LEN : usize = 20 * 65 ;
0 commit comments