Skip to content

Commit 6e768b7

Browse files
committed
f: handle Trampoline nodes in Path calculations
1 parent f4914f4 commit 6e768b7

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lightning/src/routing/router.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,22 @@ pub struct Path {
493493
impl Path {
494494
/// Gets the fees for a given path, excluding any excess paid to the recipient.
495495
pub fn fee_msat(&self) -> u64 {
496+
let route_fee = self.hops.split_last()
497+
.map_or(0, |(_, path_prefix)| path_prefix.iter().map(|hop| hop.fee_msat).sum());
498+
let trampoline_fee = self.trampoline_hops.iter().map(|hop| hop.fee_msat).sum::<u64>();
496499
match &self.blinded_tail {
497-
Some(_) => self.hops.iter().map(|hop| hop.fee_msat).sum::<u64>(),
500+
Some(_) => {
501+
if self.trampoline_hops.is_empty() {
502+
route_fee + trampoline_fee + self.hops.last().map_or(0, |hop| hop.fee_msat)
503+
} else {
504+
// exclude last RouteHop because its value is the outgoing amount to the
505+
// Trampoline entry point
506+
route_fee + trampoline_fee
507+
}
508+
},
498509
None => {
499510
// Do not count last hop of each path since that's the full value of the payment
500-
self.hops.split_last().map_or(0,
501-
|(_, path_prefix)| path_prefix.iter().map(|hop| hop.fee_msat).sum())
511+
route_fee + trampoline_fee
502512
}
503513
}
504514
}
@@ -507,15 +517,29 @@ impl Path {
507517
pub fn final_value_msat(&self) -> u64 {
508518
match &self.blinded_tail {
509519
Some(blinded_tail) => blinded_tail.final_value_msat,
510-
None => self.hops.last().map_or(0, |hop| hop.fee_msat)
520+
None => {
521+
if let Some(last_trampoline) = self.trampoline_hops.last() {
522+
// Note that this scenario should never occur until as we don't support
523+
// unblinded Trampoline payments
524+
return last_trampoline.fee_msat;
525+
}
526+
self.hops.last().map_or(0, |hop| hop.fee_msat)
527+
}
511528
}
512529
}
513530

514531
/// Gets the final hop's CLTV expiry delta.
515532
pub fn final_cltv_expiry_delta(&self) -> Option<u32> {
516533
match &self.blinded_tail {
517534
Some(_) => None,
518-
None => self.hops.last().map(|hop| hop.cltv_expiry_delta)
535+
None => {
536+
if let Some(last_trampoline) = self.trampoline_hops.last() {
537+
// Note that this scenario should never occur until as we don't support
538+
// unblinded Trampoline payments
539+
return Some(last_trampoline.cltv_expiry_delta);
540+
}
541+
self.hops.last().map(|hop| hop.cltv_expiry_delta)
542+
}
519543
}
520544
}
521545
}

0 commit comments

Comments
 (0)