@@ -829,6 +829,20 @@ where L::Target: Logger {
829
829
// - when we want to stop looking for new paths.
830
830
let mut already_collected_value_msat = 0 ;
831
831
832
+ // We only consider paths shorter than our maximum length estimate.
833
+ // In the legacy onion format, the maximum number of hops used to be a fixed value of 20.
834
+ // However, in the TLV onion format, there is no fixed maximum length, but the `hop_payloads`
835
+ // field is always 1300 bytes. As the `tlv_payload` for each hop may vary in length, we have to
836
+ // estimate how many hops the route may have so that it actually fits the `hop_payloads` field.
837
+ //
838
+ // We estimate 3+32 (payload length and HMAC) + 1+8 (amt_to_forward) + 1+4 (outgoing_cltv_value) +
839
+ // 1+8 (short_channel_id) = 58 bytes for each intermediate hop and 3+32
840
+ // (payload length and HMAC) + 1+8 (amt_to_forward) + 1+4 (outgoing_cltv_value) + 1+32+8
841
+ // (payment_secret and total_msat) = 90 bytes for the final hop.
842
+ // Since the length of the potentially included `payment_metadata` is unkown to us, we round
843
+ // down from (1300-90) / 58 = 20.86... just to arrive again at a conservative estimate of 20.
844
+ const MAX_PATH_LENGTH_ESTIMATE : usize = 20 ;
845
+
832
846
for ( _, channels) in first_hop_targets. iter_mut ( ) {
833
847
// Sort the first_hops channels to the same node(s) in priority order of which channel we'd
834
848
// most like to use.
@@ -1360,6 +1374,12 @@ where L::Target: Logger {
1360
1374
break ' path_walk;
1361
1375
}
1362
1376
1377
+ // If the path reaches the estimated maximum feasible length, skip this path
1378
+ // and continue path construction.
1379
+ if ordered_hops. len ( ) == MAX_PATH_LENGTH_ESTIMATE {
1380
+ continue ' path_construction;
1381
+ }
1382
+
1363
1383
new_entry = match dist. remove ( & ordered_hops. last ( ) . unwrap ( ) . 0 . node_id ) {
1364
1384
Some ( payment_hop) => payment_hop,
1365
1385
// We can't arrive at None because, if we ever add an entry to targets,
0 commit comments