@@ -68,7 +68,7 @@ pub struct Route {
68
68
/// last RouteHop in each path must be the same.
69
69
/// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
70
70
/// 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
72
72
/// ensure it is viable.
73
73
pub paths : Vec < Vec < RouteHop > > ,
74
74
/// The `payment_params` parameter passed to [`find_route`].
@@ -1476,6 +1476,21 @@ where L::Target: Logger {
1476
1476
}
1477
1477
1478
1478
// 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
+
1479
1494
if payment_paths. len ( ) == 0 {
1480
1495
return Err ( LightningError { err : "Failed to find a path to the given destination" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1481
1496
}
0 commit comments