Skip to content

Commit 1098208

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

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning/src/routing/router.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,20 @@ where L::Target: Logger {
829829
// - when we want to stop looking for new paths.
830830
let mut already_collected_value_msat = 0;
831831

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+
832846
for (_, channels) in first_hop_targets.iter_mut() {
833847
// Sort the first_hops channels to the same node(s) in priority order of which channel we'd
834848
// most like to use.
@@ -1360,6 +1374,12 @@ where L::Target: Logger {
13601374
break 'path_walk;
13611375
}
13621376

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+
13631383
new_entry = match dist.remove(&ordered_hops.last().unwrap().0.node_id) {
13641384
Some(payment_hop) => payment_hop,
13651385
// We can't arrive at None because, if we ever add an entry to targets,

0 commit comments

Comments
 (0)