Skip to content

Commit 6c071c2

Browse files
committed
Enforce max total CLTV delta limit after adding offsets.
1 parent e9434db commit 6c071c2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_rou
15731573
let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
15741574
let random_seed_bytes = keys_manager.get_secure_random_bytes();
15751575
let route = get_route(
1576-
&origin_node.node.get_our_node_id(), &payment_params, origin_node.network_graph,
1576+
&origin_node.node.get_our_node_id(), &payment_params, origin_node.network_graph,
15771577
None, recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer, &random_seed_bytes).unwrap();
15781578
assert_eq!(route.paths.len(), 1);
15791579
assert_eq!(route.paths[0].len(), expected_route.len());

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)