]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Unparameterize HashMap from InFlightHtlcs initializer
authorjurvis <hello@jurvis.co>
Fri, 4 Nov 2022 02:48:22 +0000 (19:48 -0700)
committerjurvis <hello@jurvis.co>
Thu, 17 Nov 2022 01:49:29 +0000 (17:49 -0800)
lightning-invoice/src/payment.rs
lightning/src/routing/router.rs

index a1c187915b98a81cb6f78d582458bbc3e4524dd6..680e97dbe01c0515746a0496cff7136d0e058276 100644 (file)
@@ -145,7 +145,6 @@ use crate::prelude::*;
 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
 use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
 use lightning::ln::msgs::LightningError;
-use lightning::routing::gossip::NodeId;
 use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, Router};
 use lightning::util::errors::APIError;
 use lightning::util::events::{Event, EventHandler};
@@ -717,38 +716,15 @@ where
        /// This function should be called whenever we need information about currently used up liquidity
        /// across payments.
        fn create_inflight_map(&self) -> InFlightHtlcs {
-               let mut total_inflight_map: HashMap<(u64, bool), u64> = HashMap::new();
-               // Make an attempt at finding existing payment information from `payment_cache`. If it
-               // does not exist, it probably is a fresh payment and we can just return an empty
-               // HashMap.
+               let mut total_inflight_map = InFlightHtlcs::new();
+               // Make an attempt at finding existing payment information from `payment_cache`.
                for payment_info in self.payment_cache.lock().unwrap().values() {
                        for path in &payment_info.paths {
-                               if path.is_empty() { break };
-                               // total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
-                               // that is held up. However, the `hops` array, which is a path returned by `find_route` in
-                               // the router excludes the payer node. In the following lines, the payer's information is
-                               // hardcoded with an inflight value of 0 so that we can correctly represent the first hop
-                               // in our sliding window of two.
-                               let our_node_id: PublicKey = self.payer.node_id();
-                               let reversed_hops_with_payer = path.iter().rev().skip(1)
-                                       .map(|hop| hop.pubkey)
-                                       .chain(core::iter::once(our_node_id));
-                               let mut cumulative_msat = 0;
-
-                               // Taking the reversed vector from above, we zip it with just the reversed hops list to
-                               // work "backwards" of the given path, since the last hop's `fee_msat` actually represents
-                               // the total amount sent.
-                               for (next_hop, prev_hop) in path.iter().rev().zip(reversed_hops_with_payer) {
-                                       cumulative_msat += next_hop.fee_msat;
-                                       total_inflight_map
-                                               .entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
-                                               .and_modify(|used_liquidity_msat| *used_liquidity_msat += cumulative_msat)
-                                               .or_insert(cumulative_msat);
-                               }
+                               total_inflight_map.process_path(path, self.payer.node_id());
                        }
                }
 
-               InFlightHtlcs::new(total_inflight_map)
+               total_inflight_map
        }
 }
 
index cffd9ddb0a581ea2e26a53ee3f305a8c7f81bbf7..fdaffeab23328b59b3b8f83c86a73fdc8ebc8317 100644 (file)
@@ -48,10 +48,32 @@ pub struct InFlightHtlcs(HashMap<(u64, bool), u64>);
 pub struct InFlightHtlcs(pub HashMap<(u64, bool), u64>);
 
 impl InFlightHtlcs {
-       /// Create a new `InFlightHtlcs` via a mapping from:
-       /// (short_channel_id, source_pubkey < target_pubkey) -> used_liquidity_msat
-       pub fn new(inflight_map: HashMap<(u64, bool), u64>) -> Self {
-               InFlightHtlcs(inflight_map)
+       /// Constructs an empty `InFlightHtlcs`.
+       pub fn new() -> Self { InFlightHtlcs(HashMap::new()) }
+
+       /// Takes in a path with payer's node id and adds the path's details to `InFlightHtlcs`.
+       pub fn process_path(&mut self, path: &[RouteHop], payer_node_id: PublicKey) {
+               if path.is_empty() { return };
+               // total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
+               // that is held up. However, the `hops` array, which is a path returned by `find_route` in
+               // the router excludes the payer node. In the following lines, the payer's information is
+               // hardcoded with an inflight value of 0 so that we can correctly represent the first hop
+               // in our sliding window of two.
+               let reversed_hops_with_payer = path.iter().rev().skip(1)
+                       .map(|hop| hop.pubkey)
+                       .chain(core::iter::once(payer_node_id));
+               let mut cumulative_msat = 0;
+
+               // Taking the reversed vector from above, we zip it with just the reversed hops list to
+               // work "backwards" of the given path, since the last hop's `fee_msat` actually represents
+               // the total amount sent.
+               for (next_hop, prev_hop) in path.iter().rev().zip(reversed_hops_with_payer) {
+                       cumulative_msat += next_hop.fee_msat;
+                       self.0
+                               .entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
+                               .and_modify(|used_liquidity_msat| *used_liquidity_msat += cumulative_msat)
+                               .or_insert(cumulative_msat);
+               }
        }
 
        /// Returns liquidity in msat given the public key of the HTLC source, target, and short channel