From: Matt Corallo Date: Wed, 27 Oct 2021 18:24:44 +0000 (+0000) Subject: Add a utility trait in `router` to get the fees along a given path X-Git-Tag: v0.0.103~7^2~2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=517a1540e070a79cd1b61896a04be43a120750fd;p=rust-lightning Add a utility trait in `router` to get the fees along a given path --- 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.