Skip to content

Commit c6595f3

Browse files
Account for Path::blinded_tail when adding a shadow cltv offset
1 parent 90a6003 commit c6595f3

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

lightning/src/routing/router.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,8 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
21062106
let network_nodes = network_graph.nodes();
21072107

21082108
for path in route.paths.iter_mut() {
2109+
if path.blinded_tail.as_ref().map_or(false, |tail| tail.path.blinded_hops.len() > 1) { continue }
2110+
21092111
let mut shadow_ctlv_expiry_delta_offset: u32 = 0;
21102112

21112113
// Remember the last three nodes of the random walk and avoid looping back on them.
@@ -2174,7 +2176,10 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
21742176
shadow_ctlv_expiry_delta_offset = cmp::min(shadow_ctlv_expiry_delta_offset, max_path_offset);
21752177

21762178
// Add 'shadow' CLTV offset to the final hop
2177-
if let Some(last_hop) = path.hops.last_mut() {
2179+
if let Some(tail) = path.blinded_tail.as_mut() {
2180+
tail.cltv_expiry_delta = tail.cltv_expiry_delta
2181+
.checked_add(shadow_ctlv_expiry_delta_offset).unwrap_or(tail.cltv_expiry_delta);
2182+
} else if let Some(last_hop) = path.hops.last_mut() {
21782183
last_hop.cltv_expiry_delta = last_hop.cltv_expiry_delta
21792184
.checked_add(shadow_ctlv_expiry_delta_offset).unwrap_or(last_hop.cltv_expiry_delta);
21802185
}
@@ -5802,6 +5807,45 @@ mod tests {
58025807
assert_eq!(*inflight_htlcs.0.get(&(42, true)).unwrap(), 301);
58035808
assert_eq!(*inflight_htlcs.0.get(&(43, false)).unwrap(), 201);
58045809
}
5810+
5811+
#[test]
5812+
fn blinded_path_cltv_shadow_offset() {
5813+
// Don't add a shadow offset to blinded paths with more than 1 hop.
5814+
let mut route = Route { paths: vec![Path {
5815+
hops: vec![RouteHop {
5816+
pubkey: ln_test_utils::pubkey(42),
5817+
node_features: NodeFeatures::empty(),
5818+
short_channel_id: 42,
5819+
channel_features: ChannelFeatures::empty(),
5820+
fee_msat: 100,
5821+
cltv_expiry_delta: 0,
5822+
}],
5823+
blinded_tail: Some(BlindedTail {
5824+
path: BlindedPath {
5825+
introduction_node_id: ln_test_utils::pubkey(43),
5826+
blinding_point: ln_test_utils::pubkey(44),
5827+
blinded_hops: vec![
5828+
BlindedHop { blinded_node_id: ln_test_utils::pubkey(45), encrypted_payload: Vec::new() },
5829+
BlindedHop { blinded_node_id: ln_test_utils::pubkey(46), encrypted_payload: Vec::new() }
5830+
],
5831+
},
5832+
intro_node_scid: 43,
5833+
fee_msat: 1,
5834+
cltv_expiry_delta: 0,
5835+
final_value_msat: 200,
5836+
}),
5837+
}], payment_params: None};
5838+
5839+
let payment_params = PaymentParameters::from_node_id(ln_test_utils::pubkey(47), 18);
5840+
let (_, network_graph, _, _, _) = build_line_graph();
5841+
add_random_cltv_offset(&mut route, &payment_params, &network_graph.read_only(), &[0; 32]);
5842+
assert_eq!(route.paths[0].blinded_tail.as_ref().unwrap().cltv_expiry_delta, 0);
5843+
5844+
// Add a shadow offset if we're sending to a 1-hop blinded path.
5845+
route.paths[0].blinded_tail.as_mut().unwrap().path.blinded_hops.pop();
5846+
add_random_cltv_offset(&mut route, &payment_params, &network_graph.read_only(), &[0; 32]);
5847+
assert_eq!(route.paths[0].blinded_tail.as_ref().unwrap().cltv_expiry_delta, 40);
5848+
}
58055849
}
58065850

58075851
#[cfg(all(test, not(feature = "no-std")))]

0 commit comments

Comments
 (0)