Add a utility trait in `router` to get the fees along a given path
authorMatt Corallo <git@bluematt.me>
Wed, 27 Oct 2021 18:24:44 +0000 (18:24 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 27 Oct 2021 20:43:18 +0000 (20:43 +0000)
lightning/src/routing/router.rs

index 545ff9f24ce386520ec630dab31f13b75b9c08ae..0d11dc1a61a1c4567a9a5fdb3e092f38696b4000 100644 (file)
@@ -78,17 +78,26 @@ pub struct Route {
        pub payee: Option<Payee>,
 }
 
+pub(crate) trait RoutePath {
+       /// Gets the fees for a given path, excluding any excess paid to the recipient.
+       fn get_path_fees(&self) -> u64;
+}
+impl RoutePath for Vec<RouteHop> {
+       fn get_path_fees(&self) -> u64 {
+               // Do not count last hop of each path since that's the full value of the payment
+               self.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[])
+                       .iter().map(|hop| &hop.fee_msat)
+                       .sum()
+       }
+}
+
 impl Route {
        /// Returns the total amount of fees paid on this [`Route`].
        ///
        /// This doesn't include any extra payment made to the recipient, which can happen in excess of
        /// the amount passed to [`find_route`]'s `params.final_value_msat`.
        pub fn get_total_fees(&self) -> u64 {
-               // Do not count last hop of each path since that's the full value of the payment
-               return self.paths.iter()
-                       .flat_map(|path| path.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[]))
-                       .map(|hop| &hop.fee_msat)
-                       .sum();
+               self.paths.iter().map(|path| path.get_path_fees()).sum()
        }
 
        /// Returns the total amount paid on this [`Route`], excluding the fees.