Skip to content

Commit fce86c9

Browse files
committed
Introduce const MEDIAN_HOP_CLTV_EXPIRY_DELTA.
1 parent d250f76 commit fce86c9

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lightning/src/routing/router.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ impl_writeable_tlv_based!(RouteParameters, {
177177
/// Maximum total CTLV difference we allow for a full payment path.
178178
pub const DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA: u32 = 1008;
179179

180+
/// The median hop CLTV expiry delta currently seen in the network.
181+
const MEDIAN_HOP_CLTV_EXPIRY_DELTA: u32 = 40;
182+
180183
/// The recipient of a payment.
181184
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
182185
pub struct PaymentParameters {
@@ -259,7 +262,6 @@ impl PaymentParameters {
259262
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
260263
pub struct RouteHint(pub Vec<RouteHintHop>);
261264

262-
263265
impl Writeable for RouteHint {
264266
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
265267
(self.0.len() as u64).write(writer)?;
@@ -861,10 +863,12 @@ where L::Target: Logger {
861863
let contributes_sufficient_value = available_value_contribution_msat >= minimal_value_contribution_msat;
862864

863865
// Do not consider candidates that exceed the maximum total cltv expiry limit.
864-
// We subtract 2*40 here in order to account for some of the privacy-enhancing
865-
// random CLTV expiry delta offset we add on top later.
866+
// In order to already account for some of the privacy enhancing random CLTV
867+
// expiry delta offset we add on top later, we subtract a rough estimate
868+
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
866869
let max_total_cltv_expiry_delta = payment_params.max_total_cltv_expiry_delta
867-
.checked_sub(2*40).unwrap_or(payment_params.max_total_cltv_expiry_delta);
870+
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
871+
.unwrap_or(payment_params.max_total_cltv_expiry_delta);
868872
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
869873
.checked_add($candidate.cltv_expiry_delta())
870874
.unwrap_or(u32::max_value());
@@ -1592,26 +1596,27 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
15921596
}
15931597
}
15941598
} else {
1595-
// If the entire path is private, choose a random offset from multiples of 40,
1596-
// which is the most prevalent cltv_expiry_delta currently used in the network
1599+
// If the entire path is private, choose a random offset from multiples of
1600+
// MEDIAN_HOP_CLTV_EXPIRY_DELTA
15971601
let mut prng = ChaCha20::new(random_seed_bytes, &[0u8; 8]);
15981602
let mut random_bytes = [0u8; 4];
15991603
prng.process_in_place(&mut random_bytes);
16001604
let random_walk_length = u32::from_be_bytes(random_bytes).wrapping_rem(3).wrapping_add(1);
1601-
shadow_ctlv_expiry_delta_offset = random_walk_length.wrapping_mul(40);
1605+
shadow_ctlv_expiry_delta_offset = random_walk_length.wrapping_mul(MEDIAN_HOP_CLTV_EXPIRY_DELTA);
16021606
}
16031607

16041608
// Limit the total offset to reduce the worst-case locked liquidity timevalue
16051609
const MAX_SHADOW_CLTV_EXPIRY_DELTA_OFFSET: u32 = 3*144;
16061610
shadow_ctlv_expiry_delta_offset = cmp::min(shadow_ctlv_expiry_delta_offset, MAX_SHADOW_CLTV_EXPIRY_DELTA_OFFSET);
16071611

16081612
// Limit the offset so we never exceed the max_total_cltv_expiry_delta. To improve plausibility,
1609-
// we choose the limit to be the largest possible multiple of 40.
1613+
// we choose the limit to be the largest possible multiple of MEDIAN_HOP_CLTV_EXPIRY_DELTA.
16101614
let mut max_path_offset = payment_params.max_total_cltv_expiry_delta
16111615
.checked_sub(path.iter().map(|h| h.cltv_expiry_delta).sum())
16121616
.unwrap_or(0);
1613-
max_path_offset = max_path_offset.wrapping_sub(max_path_offset.wrapping_rem(40))
1614-
.max(max_path_offset.wrapping_rem(40));
1617+
max_path_offset = max_path_offset
1618+
.wrapping_sub(max_path_offset.wrapping_rem(MEDIAN_HOP_CLTV_EXPIRY_DELTA))
1619+
.max(max_path_offset.wrapping_rem(MEDIAN_HOP_CLTV_EXPIRY_DELTA));
16151620
shadow_ctlv_expiry_delta_offset = cmp::min(shadow_ctlv_expiry_delta_offset, max_path_offset);
16161621

16171622
// Add 'shadow' CLTV offset to the final hop

0 commit comments

Comments
 (0)