From 517a1540e070a79cd1b61896a04be43a120750fd Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 27 Oct 2021 18:24:44 +0000 Subject: [PATCH] Add a utility trait in `router` to get the fees along a given path --- lightning/src/routing/router.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 545ff9f24..0d11dc1a6 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -78,17 +78,26 @@ pub struct Route { pub payee: Option, } +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 { + 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. -- 2.39.5