Skip to content

Commit 3131cba

Browse files
committed
Consider maximum path length during path finding.
1 parent edd3030 commit 3131cba

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lightning/src/routing/router.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct Route {
6868
/// last RouteHop in each path must be the same.
6969
/// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
7070
/// destination. Thus, this must always be at least length one. While the maximum length of any
71-
/// given path is variable, keeping the length of any path to less than 20 should currently
71+
/// given path is variable, keeping the length of any path to less than 40 should currently
7272
/// ensure it is viable.
7373
pub paths: Vec<Vec<RouteHop>>,
7474
/// The `payment_params` parameter passed to [`find_route`].
@@ -1476,6 +1476,21 @@ where L::Target: Logger {
14761476
}
14771477

14781478
// Step (5).
1479+
// We only consider paths shorter than our maximum length estimate.
1480+
// In the legacy onion format, the maximum number of hops used to be a fixed value of 20.
1481+
// However, in the TLV onion format, there is no fixed maximum length, but the `hop_payloads`
1482+
// field is always 1300 bytes. As the `tlv_payload` for each hop may vary in length, we have to
1483+
// estimate how many hops the route may have so that it actually fits the `hop_payloads` field.
1484+
//
1485+
// We estimate 2+8 (amt_to_forward) + 2+4 (outgoing_cltv_value) + 2+8 (short_channel_id) = 26
1486+
// bytes for each intermediate hop and 2+8 (amt_to_forward) + 2+4 (outgoing_cltv_value) +
1487+
// 2+32+8 (payment_secret and total_msat) = 58 bytes for the final hop.
1488+
// Since the length of the potentially included `payment_metadata` is unkown to us, we
1489+
// generously round down from (1300-58) / 26 = 47 to arrive at a conservative estimate of a
1490+
// feasible maximum path length of 40 hops.
1491+
const MAX_PATH_LENGTH_ESTIMATE: usize = 40;
1492+
payment_paths.retain(|path| path.hops.len() <= MAX_PATH_LENGTH_ESTIMATE);
1493+
14791494
if payment_paths.len() == 0 {
14801495
return Err(LightningError{err: "Failed to find a path to the given destination".to_owned(), action: ErrorAction::IgnoreError});
14811496
}

0 commit comments

Comments
 (0)