Skip to content

Commit 75443b1

Browse files
committed
Include any recipient overpayment amounts in the route fee limit
If the user told us to limit their total fee exposure, we should do so including any potential overpayment to the recipient, which is ultimately a part of the "fee" as far as the user is concerned.
1 parent 63b3628 commit 75443b1

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lightning/src/routing/router.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,21 +2629,22 @@ where L::Target: Logger {
26292629
// Make sure we would never create a route with more paths than we allow.
26302630
debug_assert!(paths.len() <= payment_params.max_path_count.into());
26312631

2632-
// Make sure we would never create a route whose total fees exceed max_total_routing_fee_msat.
2633-
if let Some(max_total_routing_fee_msat) = route_params.max_total_routing_fee_msat {
2634-
if paths.iter().map(|p| p.fee_msat()).sum::<u64>() > max_total_routing_fee_msat {
2635-
return Err(LightningError{err: format!("Failed to find route that adheres to the maximum total fee limit of {}msat",
2636-
max_total_routing_fee_msat), action: ErrorAction::IgnoreError});
2637-
}
2638-
}
2639-
26402632
if let Some(node_features) = payment_params.payee.node_features() {
26412633
for path in paths.iter_mut() {
26422634
path.hops.last_mut().unwrap().node_features = node_features.clone();
26432635
}
26442636
}
26452637

26462638
let route = Route { paths, route_params: Some(route_params.clone()) };
2639+
2640+
// Make sure we would never create a route whose total fees exceed max_total_routing_fee_msat.
2641+
if let Some(max_total_routing_fee_msat) = route_params.max_total_routing_fee_msat {
2642+
if route.get_total_fees() > max_total_routing_fee_msat {
2643+
return Err(LightningError{err: format!("Failed to find route that adheres to the maximum total fee limit of {}msat",
2644+
max_total_routing_fee_msat), action: ErrorAction::IgnoreError});
2645+
}
2646+
}
2647+
26472648
log_info!(logger, "Got route: {}", log_route!(route));
26482649
Ok(route)
26492650
}
@@ -3267,11 +3268,22 @@ mod tests {
32673268
excess_data: Vec::new()
32683269
});
32693270

3270-
// Now check that we'll find a path if the htlc_minimum is overrun substantially.
3271+
// Now check that we'll fail to find a path if we fail to find a path if the htlc_minimum
3272+
// is overrun. Note that the fees are actually calculated on 3*payment amount as that's
3273+
// what we try to find a route for, so this test only just happens to work out to exactly
3274+
// the fee limit.
32713275
let mut route_params = RouteParameters::from_payment_params_and_value(
32723276
payment_params.clone(), 5_000);
3273-
// TODO: This can even overrun the fee limit set by the recipient!
32743277
route_params.max_total_routing_fee_msat = Some(9_999);
3278+
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id,
3279+
&route_params, &network_graph.read_only(), None, Arc::clone(&logger), &scorer,
3280+
&Default::default(), &random_seed_bytes) {
3281+
assert_eq!(err, "Failed to find route that adheres to the maximum total fee limit of 9999msat");
3282+
} else { panic!(); }
3283+
3284+
let mut route_params = RouteParameters::from_payment_params_and_value(
3285+
payment_params.clone(), 5_000);
3286+
route_params.max_total_routing_fee_msat = Some(10_000);
32753287
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
32763288
Arc::clone(&logger), &scorer, &Default::default(), &random_seed_bytes).unwrap();
32773289
assert_eq!(route.get_total_fees(), 10_000);

0 commit comments

Comments
 (0)