Skip to content

Commit 3794bf6

Browse files
committed
Enforce max total CLTV delta limit after adding offsets.
1 parent f44e348 commit 3794bf6

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lightning/src/routing/router.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ where L::Target: Logger {
839839
let contributes_sufficient_value = available_value_contribution_msat >= minimal_value_contribution_msat;
840840

841841
// Do not consider candidates that exceed the maximum total cltv expiry limit.
842-
let max_total_cltv_expiry_delta = payment_params.max_total_cltv_expiry_delta;
842+
// We subtract 2*40 here in order to account for the privacy-enhancing random CLTV delta offset we add on top later
843+
let max_total_cltv_expiry_delta = payment_params.max_total_cltv_expiry_delta.checked_sub(80).unwrap_or(0);
843844
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
844845
.checked_add($candidate.cltv_expiry_delta())
845846
.unwrap_or(u32::max_value());
@@ -1560,13 +1561,12 @@ where L::Target: Logger {
15601561
}
15611562
}
15621563
} else {
1563-
// If the entire path is private, choose a random offset from multiples of 144, i.e., our
1564-
// default cltv_expiry_delta
1564+
// If the entire path is private, choose a random offset from multiples of 40, i.e., the most used cltv_expiry_delta
15651565
let mut prng = ChaCha20::new(random_seed_bytes, &[0u8; 8]);
15661566
let mut random_bytes = [0u8; 4];
15671567
prng.process_in_place(&mut random_bytes);
15681568
let random_walk_length = u32::from_be_bytes(random_bytes).wrapping_rem(3).wrapping_add(1);
1569-
shadow_ctlv_expiry_delta_offset = random_walk_length.wrapping_mul(144);
1569+
shadow_ctlv_expiry_delta_offset = random_walk_length.wrapping_mul(40);
15701570
}
15711571

15721572
// Limit the offset to reduce the payment failure probability
@@ -1579,6 +1579,10 @@ where L::Target: Logger {
15791579
if hop.pubkey != payment_params.payee_pubkey {
15801580
hop.cltv_expiry_delta = hop.cltv_expiry_delta
15811581
.checked_add(shadow_ctlv_expiry_delta_offset).unwrap_or(hop.cltv_expiry_delta);
1582+
let max_total_cltv_expiry_delta = payment_params.max_total_cltv_expiry_delta;
1583+
if hop.cltv_expiry_delta > max_total_cltv_expiry_delta {
1584+
return Err(LightningError{err: "Chosen path exceeds max total CLTV delta limit".to_owned(), action: ErrorAction::IgnoreError});
1585+
}
15821586
}
15831587
}
15841588
}

0 commit comments

Comments
 (0)