Skip to content

Commit f0d7dbb

Browse files
committed
Create inflight map independent of payment hash
We want to track _across_ payment hashes, doesn’t make sense to only generate a map for the payment we are trying to send.
1 parent 2ee2a61 commit f0d7dbb

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

lightning-invoice/src/payment.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ where
495495

496496
let payer = self.payer.node_id();
497497
let first_hops = self.payer.first_hops();
498-
let inflight_htlcs = self.create_inflight_map(&payment_hash);
498+
let inflight_htlcs = self.create_inflight_map();
499499
let route = self.router.find_route(
500500
&payer, &params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
501501
&AccountForInFlightHtlcs { scorer: &mut self.scorer.lock(), inflight_htlcs })
@@ -621,7 +621,7 @@ where
621621

622622
let payer = self.payer.node_id();
623623
let first_hops = self.payer.first_hops();
624-
let inflight_htlcs = self.create_inflight_map(&payment_hash);
624+
let inflight_htlcs = self.create_inflight_map();
625625

626626
let route = self.router.find_route(
627627
&payer, &params, &payment_hash, Some(&first_hops.iter().collect::<Vec<_>>()),
@@ -667,13 +667,12 @@ where
667667
///
668668
/// This function should be called whenever we need information about currently used up liquidity
669669
/// across payments.
670-
pub fn create_inflight_map(&self, payment_hash: &PaymentHash) -> HashMap<(u64, bool), u64> {
670+
pub fn create_inflight_map(&self) -> HashMap<(u64, bool), u64> {
671+
let mut total_inflight_map: HashMap<(u64, bool), u64> = HashMap::new();
671672
// Make an attempt at finding existing payment information from `payment_cache`. If it
672673
// does not exist, it probably is a fresh payment and we can just return an empty
673674
// HashMap.
674-
if let Some(payment_info) = self.payment_cache.lock().unwrap().get(&payment_hash) {
675-
let mut total_inflight_map: HashMap<(u64, bool), u64> = HashMap::new();
676-
675+
for (key, payment_info) in self.payment_cache.lock().unwrap().iter() {
677676
for path in &payment_info.paths {
678677
if path.is_empty() { break };
679678
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
@@ -697,11 +696,9 @@ where
697696
.or_insert(cumulative_msat);
698697
}
699698
}
700-
701-
return total_inflight_map
702-
} else {
703-
return HashMap::new()
704699
}
700+
701+
total_inflight_map
705702
}
706703
}
707704

@@ -1570,7 +1567,7 @@ mod tests {
15701567

15711568
let payment_id = invoice_payer.pay_invoice(&invoice).unwrap();
15721569

1573-
let inflight_map = invoice_payer.create_inflight_map(&payment_hash.unwrap());
1570+
let inflight_map = invoice_payer.create_inflight_map();
15741571
// First path check
15751572
assert_eq!(inflight_map.get(&(0, true)).unwrap().clone(), 94);
15761573
assert_eq!(inflight_map.get(&(1, false)).unwrap().clone(), 84);
@@ -1583,7 +1580,7 @@ mod tests {
15831580
payment_id, payment_hash, path: route.paths[0].clone()
15841581
});
15851582

1586-
let inflight_map = invoice_payer.create_inflight_map(&payment_hash.unwrap());
1583+
let inflight_map = invoice_payer.create_inflight_map();
15871584

15881585
assert_eq!(inflight_map.get(&(0, true)), None);
15891586
assert_eq!(inflight_map.get(&(1, false)), None);

0 commit comments

Comments
 (0)