Skip to content

Commit 517a154

Browse files
committed
Add a utility trait in router to get the fees along a given path
1 parent 59659d3 commit 517a154

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lightning/src/routing/router.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,26 @@ pub struct Route {
7878
pub payee: Option<Payee>,
7979
}
8080

81+
pub(crate) trait RoutePath {
82+
/// Gets the fees for a given path, excluding any excess paid to the recipient.
83+
fn get_path_fees(&self) -> u64;
84+
}
85+
impl RoutePath for Vec<RouteHop> {
86+
fn get_path_fees(&self) -> u64 {
87+
// Do not count last hop of each path since that's the full value of the payment
88+
self.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[])
89+
.iter().map(|hop| &hop.fee_msat)
90+
.sum()
91+
}
92+
}
93+
8194
impl Route {
8295
/// Returns the total amount of fees paid on this [`Route`].
8396
///
8497
/// This doesn't include any extra payment made to the recipient, which can happen in excess of
8598
/// the amount passed to [`find_route`]'s `params.final_value_msat`.
8699
pub fn get_total_fees(&self) -> u64 {
87-
// Do not count last hop of each path since that's the full value of the payment
88-
return self.paths.iter()
89-
.flat_map(|path| path.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[]))
90-
.map(|hop| &hop.fee_msat)
91-
.sum();
100+
self.paths.iter().map(|path| path.get_path_fees()).sum()
92101
}
93102

94103
/// Returns the total amount paid on this [`Route`], excluding the fees.

0 commit comments

Comments
 (0)