@@ -220,7 +220,7 @@ impl<T: Time> PaymentInfo<T> {
220
220
fn new ( ) -> Self {
221
221
PaymentInfo {
222
222
attempts : PaymentAttempts :: new ( ) ,
223
- paths : vec ! [ vec! [ ] ] ,
223
+ paths : vec ! [ ] ,
224
224
}
225
225
}
226
226
}
@@ -234,11 +234,10 @@ struct AccountForInFlightHtlcs<'a, S: Score> {
234
234
235
235
impl < ' a , S : Score > Score for AccountForInFlightHtlcs < ' a , S > {
236
236
fn channel_penalty_msat ( & self , short_channel_id : u64 , source : & NodeId , target : & NodeId , usage : ChannelUsage ) -> u64 {
237
- if let Some ( used_liqudity) = self . inflight_htlcs . get ( & ( short_channel_id, target < source ) ) {
237
+ if let Some ( used_liqudity) = self . inflight_htlcs . get ( & ( short_channel_id, source > target ) ) {
238
238
let usage = ChannelUsage {
239
- amount_msat : usage. amount_msat ,
240
239
inflight_htlc_msat : usage. inflight_htlc_msat + used_liqudity,
241
- effective_capacity : usage . effective_capacity ,
240
+ ..usage
242
241
} ;
243
242
244
243
self . scorer . channel_penalty_msat ( short_channel_id, source, target, usage)
@@ -503,22 +502,22 @@ where
503
502
if let Some ( payment_info) = self . payment_cache . lock ( ) . unwrap ( ) . get ( & payment_hash) {
504
503
let mut total_inflight_map: HashMap < ( u64 , bool ) , u64 > = HashMap :: new ( ) ;
505
504
506
- for hops in & payment_info. paths {
507
- if hops . len ( ) < 1 { break } ;
505
+ for path in & payment_info. paths {
506
+ if path . is_empty ( ) { break } ;
508
507
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
509
508
// that is held up. However, the `hops` array, which is a path returned by `find_route` in
510
509
// the router excludes the payer node. In the following lines, the payer's information is
511
510
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
512
511
// in our sliding window of two.
513
512
let our_node_id: PublicKey = self . payer . node_id ( ) ;
514
513
let reversed_hops_with_payer = core:: iter:: once ( ( 0u64 , our_node_id) ) . chain (
515
- hops . split_last ( ) . unwrap ( ) . 1 . iter ( ) . map ( |hop| ( hop. fee_msat , hop. pubkey ) ) ) . rev ( ) ;
516
- let mut cumulative_msat = hops . last ( ) . unwrap ( ) . fee_msat ;
514
+ path . split_last ( ) . unwrap ( ) . 1 . iter ( ) . map ( |hop| ( hop. fee_msat , hop. pubkey ) ) ) . rev ( ) ;
515
+ let mut cumulative_msat = path . last ( ) . unwrap ( ) . fee_msat ;
517
516
518
517
// Taking the reversed vector from above, we zip it with just the reversed hops list to
519
518
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
520
519
// the total amount sent.
521
- for ( next_hop, prev_hop) in hops . iter ( ) . rev ( ) . zip ( reversed_hops_with_payer) {
520
+ for ( next_hop, prev_hop) in path . iter ( ) . rev ( ) . zip ( reversed_hops_with_payer) {
522
521
cumulative_msat += prev_hop. 0 ;
523
522
total_inflight_map
524
523
. entry ( ( next_hop. short_channel_id , next_hop. pubkey < prev_hop. 1 ) )
@@ -533,19 +532,16 @@ where
533
532
}
534
533
} ;
535
534
536
- let route = {
537
- let scorer = AccountForInFlightHtlcs { scorer : & mut self . scorer . lock ( ) , inflight_htlcs } ;
538
-
539
- self . router . find_route (
535
+ let route = self . router . find_route (
540
536
& payer, & params, & payment_hash, Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) ,
541
- & scorer
542
- ) . map_err ( |e| PaymentError :: Routing ( e) ) ?
543
- } ;
537
+ & AccountForInFlightHtlcs { scorer : & mut self . scorer . lock ( ) , inflight_htlcs }
538
+ ) . map_err ( |e| PaymentError :: Routing ( e) ) ?;
539
+
544
540
545
541
match send_payment ( & route) {
546
542
Ok ( payment_id) => {
547
543
for hops in route. paths {
548
- self . process_path_inflight_htlcs ( payment_hash, hops) ;
544
+ self . process_path_inflight_htlcs ( payment_hash, & hops) ;
549
545
}
550
546
Ok ( payment_id)
551
547
} ,
@@ -564,20 +560,21 @@ where
564
560
}
565
561
} ,
566
562
PaymentSendFailure :: PartialFailure { failed_paths_retry, payment_id, results } => {
567
- // The ones that are `Ok()`s + `MonitorUpdateFailed` in `results` need to be used to update the map
568
- // get the index of them and run to `route` to get the values we need to care about.
563
+ // If a `PartialFailure` event returns a result that is either `Ok()` or an error
564
+ // with `MonitorUpdateFailed`, it means that the HTLCs for our payment is now
565
+ // either inflight, or being retried.
566
+ //
567
+ // Since the `results` field returned within `PartialFailure` are in the same
568
+ // order as our route hops, we can use the index for each `result` to look up its
569
+ // path in the `route` we found.
569
570
for ( idx, result) in results. iter ( ) . enumerate ( ) {
570
571
match result {
571
572
Ok ( _) => {
572
- if let Some ( failed_path) = route. paths . get ( idx) {
573
- self . process_path_inflight_htlcs ( payment_hash, failed_path. to_vec ( ) ) ;
574
- }
573
+ self . process_path_inflight_htlcs ( payment_hash, & route. paths [ idx] ) ;
575
574
}
576
575
Err ( err) => match err {
577
576
APIError :: MonitorUpdateFailed => {
578
- if let Some ( failed_path) = route. paths . get ( idx) {
579
- self . process_path_inflight_htlcs ( payment_hash, failed_path. to_vec ( ) ) ;
580
- }
577
+ self . process_path_inflight_htlcs ( payment_hash, & route. paths [ idx] )
581
578
}
582
579
_ => { }
583
580
}
@@ -605,23 +602,21 @@ where
605
602
606
603
// Takes in a path to have its information stored in `payment_cache`. This is done for paths
607
604
// that are pending retry.
608
- fn process_path_inflight_htlcs ( & self , payment_hash : PaymentHash , hops : Vec < RouteHop > ) {
609
- let payment_info_route_hops: Vec < PaymentInfoRouteHop > = hops. iter ( ) . map ( |h| PaymentInfoRouteHop { pubkey : h. pubkey , short_channel_id : h. short_channel_id , fee_msat : h. fee_msat } ) . collect ( ) ;
605
+ fn process_path_inflight_htlcs ( & self , payment_hash : PaymentHash , hops : & Vec < RouteHop > ) {
606
+ let payment_info_route_hops: Vec < PaymentInfoRouteHop > = hops. iter ( )
607
+ . map ( |h| PaymentInfoRouteHop { pubkey : h. pubkey , short_channel_id : h. short_channel_id , fee_msat : h. fee_msat } )
608
+ . collect ( ) ;
610
609
611
610
self . payment_cache . lock ( ) . unwrap ( ) . entry ( payment_hash)
612
- . or_insert ( PaymentInfo {
613
- attempts : PaymentAttempts {
614
- count : 1 ,
615
- first_attempted_at : T :: now ( )
616
- } ,
617
- paths : vec ! [ ]
618
- } )
611
+ . or_insert ( PaymentInfo :: new ( ) )
619
612
. paths . push ( payment_info_route_hops) ;
620
613
}
621
614
622
615
// Find the path we want to remove in `payment_cache`. If it doesn't exist, do nothing.
623
616
fn remove_path_inflight_htlcs ( & self , payment_hash : PaymentHash , hops : & Vec < RouteHop > ) {
624
- let payment_info_route_hops: Vec < PaymentInfoRouteHop > = hops. iter ( ) . map ( |h| PaymentInfoRouteHop { pubkey : h. pubkey , short_channel_id : h. short_channel_id , fee_msat : h. fee_msat } ) . collect ( ) ;
617
+ let payment_info_route_hops: Vec < PaymentInfoRouteHop > = hops. iter ( )
618
+ . map ( |h| PaymentInfoRouteHop { pubkey : h. pubkey , short_channel_id : h. short_channel_id , fee_msat : h. fee_msat } )
619
+ . collect ( ) ;
625
620
626
621
self . payment_cache . lock ( ) . unwrap ( ) . entry ( payment_hash)
627
622
. and_modify ( |payment_info| {
@@ -636,13 +631,7 @@ where
636
631
) -> Result < ( ) , ( ) > {
637
632
let attempts = self . payment_cache . lock ( ) . unwrap ( ) . entry ( payment_hash)
638
633
. and_modify ( |info| info. attempts . count += 1 )
639
- . or_insert ( PaymentInfo {
640
- attempts : PaymentAttempts {
641
- count : 1 ,
642
- first_attempted_at : T :: now ( )
643
- } ,
644
- paths : vec ! [ vec![ ] ]
645
- } ) . attempts ;
634
+ . or_insert ( { PaymentInfo :: new ( ) } ) . attempts ;
646
635
647
636
if !self . retry . is_retryable_now ( & attempts) {
648
637
log_trace ! ( self . logger, "Payment {} exceeded maximum attempts; not retrying ({})" , log_bytes!( payment_hash. 0 ) , attempts) ;
0 commit comments