Skip to content

Commit 66e89b9

Browse files
committed
Address PR comments
1 parent 4bbd65d commit 66e89b9

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

lightning-invoice/src/payment.rs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<T: Time> PaymentInfo<T> {
220220
fn new() -> Self {
221221
PaymentInfo {
222222
attempts: PaymentAttempts::new(),
223-
paths: vec![vec![]],
223+
paths: vec![],
224224
}
225225
}
226226
}
@@ -234,11 +234,10 @@ struct AccountForInFlightHtlcs<'a, S: Score> {
234234

235235
impl<'a, S: Score> Score for AccountForInFlightHtlcs<'a, S> {
236236
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)) {
238238
let usage = ChannelUsage {
239-
amount_msat: usage.amount_msat,
240239
inflight_htlc_msat: usage.inflight_htlc_msat + used_liqudity,
241-
effective_capacity: usage.effective_capacity,
240+
..usage
242241
};
243242

244243
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
@@ -503,22 +502,22 @@ where
503502
if let Some(payment_info) = self.payment_cache.lock().unwrap().get(&payment_hash) {
504503
let mut total_inflight_map: HashMap<(u64, bool), u64> = HashMap::new();
505504

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 };
508507
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
509508
// that is held up. However, the `hops` array, which is a path returned by `find_route` in
510509
// the router excludes the payer node. In the following lines, the payer's information is
511510
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
512511
// in our sliding window of two.
513512
let our_node_id: PublicKey = self.payer.node_id();
514513
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;
517516

518517
// Taking the reversed vector from above, we zip it with just the reversed hops list to
519518
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
520519
// 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) {
522521
cumulative_msat += prev_hop.0;
523522
total_inflight_map
524523
.entry((next_hop.short_channel_id, next_hop.pubkey < prev_hop.1))
@@ -533,19 +532,16 @@ where
533532
}
534533
};
535534

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(
540536
&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+
544540

545541
match send_payment(&route) {
546542
Ok(payment_id) => {
547543
for hops in route.paths {
548-
self.process_path_inflight_htlcs(payment_hash, hops);
544+
self.process_path_inflight_htlcs(payment_hash, &hops);
549545
}
550546
Ok(payment_id)
551547
},
@@ -564,20 +560,21 @@ where
564560
}
565561
},
566562
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.
569570
for (idx, result) in results.iter().enumerate() {
570571
match result {
571572
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]);
575574
}
576575
Err(err) => match err {
577576
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])
581578
}
582579
_ => {}
583580
}
@@ -605,23 +602,21 @@ where
605602

606603
// Takes in a path to have its information stored in `payment_cache`. This is done for paths
607604
// 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();
610609

611610
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())
619612
.paths.push(payment_info_route_hops);
620613
}
621614

622615
// Find the path we want to remove in `payment_cache`. If it doesn't exist, do nothing.
623616
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();
625620

626621
self.payment_cache.lock().unwrap().entry(payment_hash)
627622
.and_modify(|payment_info| {
@@ -636,13 +631,7 @@ where
636631
) -> Result<(), ()> {
637632
let attempts = self.payment_cache.lock().unwrap().entry(payment_hash)
638633
.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;
646635

647636
if !self.retry.is_retryable_now(&attempts) {
648637
log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying ({})", log_bytes!(payment_hash.0), attempts);

0 commit comments

Comments
 (0)