Skip to content

Commit c244c78

Browse files
authored
Merge pull request #1358 from TheBlueMatt/2022-03-max-cltv
Make `max_total_cltv_expiry_delta` include the final CLTV
2 parents 699ac83 + bb4413c commit c244c78

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lightning/src/ln/functional_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6411,7 +6411,8 @@ fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
64116411
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
64126412
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known());
64136413

6414-
let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], vec![], 100000000, 500000001);
6414+
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], vec![], 100000000, 0);
6415+
route.paths[0].last_mut().unwrap().cltv_expiry_delta = 500000001;
64156416
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::RouteError { ref err },
64166417
assert_eq!(err, &"Channel CLTV overflowed?"));
64176418
}

lightning/src/routing/router.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ where L::Target: Logger {
670670
}
671671
}
672672
}
673+
if payment_params.max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
674+
return Err(LightningError{err: "Can't find a route where the maximum total CLTV expiry delta is below the final CLTV expiry.".to_owned(), action: ErrorAction::IgnoreError});
675+
}
673676

674677
// The general routing idea is the following:
675678
// 1. Fill first/last hops communicated by the caller.
@@ -866,9 +869,9 @@ where L::Target: Logger {
866869
// In order to already account for some of the privacy enhancing random CLTV
867870
// expiry delta offset we add on top later, we subtract a rough estimate
868871
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
869-
let max_total_cltv_expiry_delta = payment_params.max_total_cltv_expiry_delta
872+
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
870873
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
871-
.unwrap_or(payment_params.max_total_cltv_expiry_delta);
874+
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
872875
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
873876
.checked_add($candidate.cltv_expiry_delta())
874877
.unwrap_or(u32::max_value());
@@ -5093,15 +5096,15 @@ mod tests {
50935096
.with_max_total_cltv_expiry_delta(feasible_max_total_cltv_delta);
50945097
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
50955098
let random_seed_bytes = keys_manager.get_secure_random_bytes();
5096-
let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
5099+
let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
50975100
let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
50985101
assert_ne!(path.len(), 0);
50995102

51005103
// But not if we exclude all paths on the basis of their accumulated CLTV delta
51015104
let fail_max_total_cltv_delta = 23;
51025105
let fail_payment_params = PaymentParameters::from_node_id(nodes[6]).with_route_hints(last_hops(&nodes))
51035106
.with_max_total_cltv_expiry_delta(fail_max_total_cltv_delta);
5104-
match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes)
5107+
match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes)
51055108
{
51065109
Err(LightningError { err, .. } ) => {
51075110
assert_eq!(err, "Failed to find a path to the given destination");
@@ -5436,7 +5439,7 @@ mod benches {
54365439
let mut routes = Vec::new();
54375440
let mut route_endpoints = Vec::new();
54385441
let mut seed: usize = 0xdeadbeef;
5439-
'load_endpoints: for _ in 0..100 {
5442+
'load_endpoints: for _ in 0..150 {
54405443
loop {
54415444
seed *= 0xdeadbeef;
54425445
let src = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
@@ -5468,6 +5471,15 @@ mod benches {
54685471
}
54695472
}
54705473

5474+
// Because we've changed channel scores, its possible we'll take different routes to the
5475+
// selected destinations, possibly causing us to fail because, eg, the newly-selected path
5476+
// requires a too-high CLTV delta.
5477+
route_endpoints.retain(|(first_hop, params, amt)| {
5478+
get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, 42, &DummyLogger{}, &scorer, &random_seed_bytes).is_ok()
5479+
});
5480+
route_endpoints.truncate(100);
5481+
assert_eq!(route_endpoints.len(), 100);
5482+
54715483
// ...then benchmark finding paths between the nodes we learned.
54725484
let mut idx = 0;
54735485
bench.iter(|| {

0 commit comments

Comments
 (0)