Skip to content

Commit c789368

Browse files
committed
Unparameterize HashMap from InFlightHtlcs initializer
1 parent 838d486 commit c789368

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

lightning-invoice/src/payment.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ use crate::prelude::*;
145145
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
146146
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
147147
use lightning::ln::msgs::LightningError;
148-
use lightning::routing::gossip::NodeId;
149148
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, Router};
150149
use lightning::util::errors::APIError;
151150
use lightning::util::events::{Event, EventHandler};
@@ -712,38 +711,15 @@ where
712711
/// This function should be called whenever we need information about currently used up liquidity
713712
/// across payments.
714713
fn create_inflight_map(&self) -> InFlightHtlcs {
715-
let mut total_inflight_map: HashMap<(u64, bool), u64> = HashMap::new();
716-
// Make an attempt at finding existing payment information from `payment_cache`. If it
717-
// does not exist, it probably is a fresh payment and we can just return an empty
718-
// HashMap.
714+
let mut total_inflight_map = InFlightHtlcs::new();
715+
// Make an attempt at finding existing payment information from `payment_cache`.
719716
for payment_info in self.payment_cache.lock().unwrap().values() {
720717
for path in &payment_info.paths {
721-
if path.is_empty() { break };
722-
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
723-
// that is held up. However, the `hops` array, which is a path returned by `find_route` in
724-
// the router excludes the payer node. In the following lines, the payer's information is
725-
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
726-
// in our sliding window of two.
727-
let our_node_id: PublicKey = self.payer.node_id();
728-
let reversed_hops_with_payer = path.iter().rev().skip(1)
729-
.map(|hop| hop.pubkey)
730-
.chain(core::iter::once(our_node_id));
731-
let mut cumulative_msat = 0;
732-
733-
// Taking the reversed vector from above, we zip it with just the reversed hops list to
734-
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
735-
// the total amount sent.
736-
for (next_hop, prev_hop) in path.iter().rev().zip(reversed_hops_with_payer) {
737-
cumulative_msat += next_hop.fee_msat;
738-
total_inflight_map
739-
.entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
740-
.and_modify(|used_liquidity_msat| *used_liquidity_msat += cumulative_msat)
741-
.or_insert(cumulative_msat);
742-
}
718+
total_inflight_map.process_path(path, self.payer.node_id());
743719
}
744720
}
745721

746-
InFlightHtlcs::new(total_inflight_map)
722+
total_inflight_map
747723
}
748724
}
749725

lightning/src/routing/router.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,32 @@ pub struct InFlightHtlcs(HashMap<(u64, bool), u64>);
4848
pub struct InFlightHtlcs(pub HashMap<(u64, bool), u64>);
4949

5050
impl InFlightHtlcs {
51-
/// Create a new `InFlightHtlcs` via a mapping from:
52-
/// (short_channel_id, source_pubkey < target_pubkey) -> used_liquidity_msat
53-
pub fn new(inflight_map: HashMap<(u64, bool), u64>) -> Self {
54-
InFlightHtlcs(inflight_map)
51+
/// Constructs an empty `InFlightHtlcs`.
52+
pub fn new() -> Self { InFlightHtlcs(HashMap::new()) }
53+
54+
/// Takes in a path with payer's node id and adds the path's details to `InFlightHtlcs`.
55+
pub fn process_path(&mut self, path: &[RouteHop], payer_node_id: PublicKey) {
56+
if path.is_empty() { return };
57+
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
58+
// that is held up. However, the `hops` array, which is a path returned by `find_route` in
59+
// the router excludes the payer node. In the following lines, the payer's information is
60+
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
61+
// in our sliding window of two.
62+
let reversed_hops_with_payer = path.iter().rev().skip(1)
63+
.map(|hop| hop.pubkey)
64+
.chain(core::iter::once(payer_node_id));
65+
let mut cumulative_msat = 0;
66+
67+
// Taking the reversed vector from above, we zip it with just the reversed hops list to
68+
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
69+
// the total amount sent.
70+
for (next_hop, prev_hop) in path.iter().rev().zip(reversed_hops_with_payer) {
71+
cumulative_msat += next_hop.fee_msat;
72+
self.0
73+
.entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
74+
.and_modify(|used_liquidity_msat| *used_liquidity_msat += cumulative_msat)
75+
.or_insert(cumulative_msat);
76+
}
5577
}
5678

5779
/// Returns liquidity in msat given the public key of the HTLC source, target, and short channel

0 commit comments

Comments
 (0)