1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
11 use crate::blinded_path::BlindedPath;
12 use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs};
13 use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentFailureReason};
14 use crate::ln::PaymentSecret;
15 use crate::ln::channelmanager;
16 use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
17 use crate::ln::features::BlindedHopFeatures;
18 use crate::ln::functional_test_utils::*;
20 use crate::ln::msgs::ChannelMessageHandler;
21 use crate::ln::onion_utils;
22 use crate::ln::onion_utils::INVALID_ONION_BLINDING;
23 use crate::ln::outbound_payment::Retry;
24 use crate::offers::invoice::BlindedPayInfo;
25 use crate::prelude::*;
26 use crate::routing::router::{Payee, PaymentParameters, RouteParameters};
27 use crate::util::config::UserConfig;
28 use crate::util::test_utils;
30 fn blinded_payment_path(
31 payment_secret: PaymentSecret, node_ids: Vec<PublicKey>,
32 channel_upds: &[&msgs::UnsignedChannelUpdate], keys_manager: &test_utils::TestKeysInterface
33 ) -> (BlindedPayInfo, BlindedPath) {
34 let mut intermediate_nodes = Vec::new();
35 for (node_id, chan_upd) in node_ids.iter().zip(channel_upds) {
36 intermediate_nodes.push(ForwardNode {
39 short_channel_id: chan_upd.short_channel_id,
40 payment_relay: PaymentRelay {
41 cltv_expiry_delta: chan_upd.cltv_expiry_delta,
42 fee_proportional_millionths: chan_upd.fee_proportional_millionths,
43 fee_base_msat: chan_upd.fee_base_msat,
45 payment_constraints: PaymentConstraints {
46 max_cltv_expiry: u32::max_value(),
47 htlc_minimum_msat: chan_upd.htlc_minimum_msat,
49 features: BlindedHopFeatures::empty(),
51 htlc_maximum_msat: chan_upd.htlc_maximum_msat,
54 let payee_tlvs = ReceiveTlvs {
56 payment_constraints: PaymentConstraints {
57 max_cltv_expiry: u32::max_value(),
58 htlc_minimum_msat: channel_upds.last().unwrap().htlc_minimum_msat,
61 let mut secp_ctx = Secp256k1::new();
62 BlindedPath::new_for_payment(
63 &intermediate_nodes[..], *node_ids.last().unwrap(), payee_tlvs,
64 channel_upds.last().unwrap().htlc_maximum_msat, TEST_FINAL_CLTV as u16, keys_manager, &secp_ctx
68 pub fn get_blinded_route_parameters(
69 amt_msat: u64, payment_secret: PaymentSecret, node_ids: Vec<PublicKey>,
70 channel_upds: &[&msgs::UnsignedChannelUpdate], keys_manager: &test_utils::TestKeysInterface
71 ) -> RouteParameters {
72 RouteParameters::from_payment_params_and_value(
73 PaymentParameters::blinded(vec![
74 blinded_payment_path(payment_secret, node_ids, channel_upds, keys_manager)
80 fn one_hop_blinded_path() {
81 do_one_hop_blinded_path(true);
82 do_one_hop_blinded_path(false);
85 fn do_one_hop_blinded_path(success: bool) {
86 let chanmon_cfgs = create_chanmon_cfgs(2);
87 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
88 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
89 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
90 let chan_upd = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.contents;
93 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
94 let payee_tlvs = ReceiveTlvs {
96 payment_constraints: PaymentConstraints {
97 max_cltv_expiry: u32::max_value(),
98 htlc_minimum_msat: chan_upd.htlc_minimum_msat,
101 let mut secp_ctx = Secp256k1::new();
102 let blinded_path = BlindedPath::one_hop_for_payment(
103 nodes[1].node.get_our_node_id(), payee_tlvs, TEST_FINAL_CLTV as u16,
104 &chanmon_cfgs[1].keys_manager, &secp_ctx
107 let route_params = RouteParameters::from_payment_params_and_value(
108 PaymentParameters::blinded(vec![blinded_path]),
111 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
112 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
113 check_added_monitors(&nodes[0], 1);
114 pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret);
116 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
118 fail_payment(&nodes[0], &[&nodes[1]], payment_hash);
123 fn mpp_to_one_hop_blinded_path() {
124 let chanmon_cfgs = create_chanmon_cfgs(4);
125 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
126 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
127 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
128 let mut secp_ctx = Secp256k1::new();
130 create_announced_chan_between_nodes(&nodes, 0, 1);
131 create_announced_chan_between_nodes(&nodes, 0, 2);
132 let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
133 create_announced_chan_between_nodes(&nodes, 2, 3).0.contents;
135 let amt_msat = 15_000_000;
136 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
137 let payee_tlvs = ReceiveTlvs {
139 payment_constraints: PaymentConstraints {
140 max_cltv_expiry: u32::max_value(),
141 htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
144 let blinded_path = BlindedPath::one_hop_for_payment(
145 nodes[3].node.get_our_node_id(), payee_tlvs, TEST_FINAL_CLTV as u16,
146 &chanmon_cfgs[3].keys_manager, &secp_ctx
149 let bolt12_features =
150 channelmanager::provided_bolt12_invoice_features(&UserConfig::default());
151 let route_params = RouteParameters::from_payment_params_and_value(
152 PaymentParameters::blinded(vec![blinded_path]).with_bolt12_features(bolt12_features).unwrap(),
155 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
156 check_added_monitors(&nodes[0], 2);
158 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
159 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
160 assert_eq!(events.len(), 2);
162 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
163 pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
164 Some(payment_secret), ev.clone(), false, None);
166 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
167 pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
168 Some(payment_secret), ev.clone(), true, None);
169 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
173 fn mpp_to_three_hop_blinded_paths() {
174 let chanmon_cfgs = create_chanmon_cfgs(6);
175 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
176 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
177 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
179 // Create this network topology so node 0 MPP's over 2 3-hop blinded paths:
185 create_announced_chan_between_nodes(&nodes, 0, 1);
186 create_announced_chan_between_nodes(&nodes, 0, 2);
187 let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
188 let chan_upd_2_4 = create_announced_chan_between_nodes(&nodes, 2, 4).0.contents;
189 let chan_upd_3_5 = create_announced_chan_between_nodes(&nodes, 3, 5).0.contents;
190 let chan_upd_4_5 = create_announced_chan_between_nodes(&nodes, 4, 5).0.contents;
192 let amt_msat = 15_000_000;
193 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[5], Some(amt_msat), None);
195 let path_1_params = get_blinded_route_parameters(
196 amt_msat, payment_secret, vec![
197 nodes[1].node.get_our_node_id(), nodes[3].node.get_our_node_id(),
198 nodes[5].node.get_our_node_id()
199 ], &[&chan_upd_1_3, &chan_upd_3_5], &chanmon_cfgs[5].keys_manager
201 let path_2_params = get_blinded_route_parameters(
202 amt_msat, payment_secret, vec![
203 nodes[2].node.get_our_node_id(), nodes[4].node.get_our_node_id(),
204 nodes[5].node.get_our_node_id()
205 ], &[&chan_upd_2_4, &chan_upd_4_5], &chanmon_cfgs[5].keys_manager
207 let pay_params = PaymentParameters::blinded(
209 path_1_params.payment_params.payee.blinded_route_hints()[0].clone(),
210 path_2_params.payment_params.payee.blinded_route_hints()[0].clone()
213 .with_bolt12_features(channelmanager::provided_bolt12_invoice_features(&UserConfig::default()))
215 RouteParameters::from_payment_params_and_value(pay_params, amt_msat)
218 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
219 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
220 check_added_monitors(&nodes[0], 2);
222 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3], &nodes[5]], &[&nodes[2], &nodes[4], &nodes[5]]];
223 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
224 assert_eq!(events.len(), 2);
226 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
227 pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
228 Some(payment_secret), ev.clone(), false, None);
230 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
231 pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
232 Some(payment_secret), ev.clone(), true, None);
233 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
236 enum ForwardCheckFail {
237 // Fail a check on the inbound onion payload. In this case, we underflow when calculating the
238 // outgoing cltv_expiry.
240 // The forwarding node's payload is encoded as a receive, i.e. the next hop HMAC is [0; 32].
241 ForwardPayloadEncodedAsReceive,
242 // Fail a check on the outbound channel. In this case, our next-hop peer is offline.
243 OutboundChannelCheck,
247 fn forward_checks_failure() {
248 do_forward_checks_failure(ForwardCheckFail::InboundOnionCheck, true);
249 do_forward_checks_failure(ForwardCheckFail::InboundOnionCheck, false);
250 do_forward_checks_failure(ForwardCheckFail::ForwardPayloadEncodedAsReceive, true);
251 do_forward_checks_failure(ForwardCheckFail::ForwardPayloadEncodedAsReceive, false);
252 do_forward_checks_failure(ForwardCheckFail::OutboundChannelCheck, true);
253 do_forward_checks_failure(ForwardCheckFail::OutboundChannelCheck, false);
256 fn do_forward_checks_failure(check: ForwardCheckFail, intro_fails: bool) {
257 // Ensure we'll fail backwards properly if a forwarding check fails on initial update_add
259 let chanmon_cfgs = create_chanmon_cfgs(4);
260 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
261 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
262 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
263 // We need the session priv to construct a bogus onion packet later.
264 *nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some([3; 32]);
265 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
266 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
267 let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
270 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
271 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
272 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(),
273 &[&chan_upd_1_2, &chan_upd_2_3], &chanmon_cfgs[3].keys_manager);
275 let route = get_route(&nodes[0], &route_params).unwrap();
276 node_cfgs[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
277 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
278 check_added_monitors(&nodes[0], 1);
280 macro_rules! cause_error {
281 ($src_node_idx: expr, $target_node_idx: expr, $update_add: expr) => {
283 ForwardCheckFail::InboundOnionCheck => {
284 $update_add.cltv_expiry = 10; // causes outbound CLTV expiry to underflow
286 ForwardCheckFail::ForwardPayloadEncodedAsReceive => {
287 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
288 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
289 let cur_height = nodes[0].best_block_info().1;
290 let (mut onion_payloads, ..) = onion_utils::build_onion_payloads(
291 &route.paths[0], amt_msat, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
292 // Remove the receive payload so the blinded forward payload is encoded as a final payload
293 // (i.e. next_hop_hmac == [0; 32])
294 onion_payloads.pop();
295 if $target_node_idx + 1 < nodes.len() {
296 onion_payloads.pop();
298 $update_add.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
300 ForwardCheckFail::OutboundChannelCheck => {
301 // The intro node will see that the next-hop peer is disconnected and fail the HTLC backwards.
302 nodes[$src_node_idx].node.peer_disconnected(&nodes[$target_node_idx].node.get_our_node_id());
308 let mut updates_0_1 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
309 let update_add = &mut updates_0_1.update_add_htlcs[0];
312 cause_error!(1, 2, update_add);
315 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
316 check_added_monitors!(nodes[1], 0);
317 do_commitment_signed_dance(&nodes[1], &nodes[0], &updates_0_1.commitment_signed, true, true);
320 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
321 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
322 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
323 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
324 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
328 expect_pending_htlcs_forwardable!(nodes[1]);
329 check_added_monitors!(nodes[1], 1);
331 let mut updates_1_2 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
332 let mut update_add = &mut updates_1_2.update_add_htlcs[0];
334 cause_error!(2, 3, update_add);
336 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add);
337 check_added_monitors!(nodes[2], 0);
338 do_commitment_signed_dance(&nodes[2], &nodes[1], &updates_1_2.commitment_signed, true, true);
340 let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
341 let update_malformed = &mut updates.update_fail_malformed_htlcs[0];
342 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
343 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
345 // Ensure the intro node will properly blind the error if its downstream node failed to do so.
346 update_malformed.sha256_of_onion = [1; 32];
347 update_malformed.failure_code = INVALID_ONION_BLINDING ^ 1;
348 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
349 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);
351 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
352 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
353 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
354 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
355 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
359 fn failed_backwards_to_intro_node() {
360 // Ensure the intro node will error backwards properly even if the downstream node did not blind
362 let chanmon_cfgs = create_chanmon_cfgs(3);
363 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
364 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
365 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
366 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
367 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
370 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
371 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
372 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
373 &chanmon_cfgs[2].keys_manager);
375 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
376 check_added_monitors(&nodes[0], 1);
378 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
379 assert_eq!(events.len(), 1);
380 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
381 let mut payment_event = SendEvent::from_event(ev);
383 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
384 check_added_monitors!(nodes[1], 0);
385 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false);
386 expect_pending_htlcs_forwardable!(nodes[1]);
387 check_added_monitors!(&nodes[1], 1);
389 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
390 assert_eq!(events.len(), 1);
391 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
392 let mut payment_event = SendEvent::from_event(ev);
394 // Ensure the final node fails to handle the HTLC.
395 payment_event.msgs[0].onion_routing_packet.hop_data[0] ^= 1;
396 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
397 check_added_monitors!(nodes[2], 0);
398 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event.commitment_msg, true, true);
399 nodes[2].node.process_pending_htlc_forwards();
401 let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
402 let mut update_malformed = &mut updates.update_fail_malformed_htlcs[0];
403 // Check that the final node encodes its failure correctly.
404 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
405 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
407 // Modify such the final hop does not correctly blind their error so we can ensure the intro node
408 // converts it to the correct error.
409 update_malformed.sha256_of_onion = [1; 32];
410 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
411 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);
413 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
414 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
415 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
416 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
417 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
420 enum ProcessPendingHTLCsCheck {
426 fn forward_fail_in_process_pending_htlc_fwds() {
427 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdPeerDisconnected, true);
428 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdPeerDisconnected, false);
429 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdChannelClosed, true);
430 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdChannelClosed, false);
432 fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck, intro_fails: bool) {
433 // Ensure the intro node will error backwards properly if the HTLC fails in
434 // process_pending_htlc_forwards.
435 let chanmon_cfgs = create_chanmon_cfgs(4);
436 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
437 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
438 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
439 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
440 let (chan_upd_1_2, chan_id_1_2) = {
441 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
442 (chan.0.contents, chan.2)
444 let (chan_upd_2_3, chan_id_2_3) = {
445 let chan = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
446 (chan.0.contents, chan.2)
450 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
451 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
452 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2, &chan_upd_2_3],
453 &chanmon_cfgs[2].keys_manager);
455 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
456 check_added_monitors(&nodes[0], 1);
458 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
459 assert_eq!(events.len(), 1);
460 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
461 let mut payment_event = SendEvent::from_event(ev);
463 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
464 check_added_monitors!(nodes[1], 0);
465 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false);
467 macro_rules! cause_error {
468 ($prev_node: expr, $curr_node: expr, $next_node: expr, $failed_chan_id: expr, $failed_scid: expr) => {
470 ProcessPendingHTLCsCheck::FwdPeerDisconnected => {
471 // Disconnect the next-hop peer so when we go to forward in process_pending_htlc_forwards, the
472 // intro node will error backwards.
473 $curr_node.node.peer_disconnected(&$next_node.node.get_our_node_id());
474 expect_pending_htlcs_forwardable!($curr_node);
475 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!($curr_node,
476 vec![HTLCDestination::NextHopChannel { node_id: Some($next_node.node.get_our_node_id()), channel_id: $failed_chan_id }]);
478 ProcessPendingHTLCsCheck::FwdChannelClosed => {
479 // Force close the next-hop channel so when we go to forward in process_pending_htlc_forwards,
480 // the intro node will error backwards.
481 $curr_node.node.force_close_broadcasting_latest_txn(&$failed_chan_id, &$next_node.node.get_our_node_id()).unwrap();
482 let events = $curr_node.node.get_and_clear_pending_events();
484 crate::events::Event::PendingHTLCsForwardable { .. } => {},
485 _ => panic!("Unexpected event {:?}", events),
488 crate::events::Event::ChannelClosed { .. } => {},
489 _ => panic!("Unexpected event {:?}", events),
492 $curr_node.node.process_pending_htlc_forwards();
493 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!($curr_node,
494 vec![HTLCDestination::UnknownNextHop { requested_forward_scid: $failed_scid }]);
495 check_closed_broadcast(&$curr_node, 1, true);
496 check_added_monitors!($curr_node, 1);
497 $curr_node.node.process_pending_htlc_forwards();
504 cause_error!(nodes[0], nodes[1], nodes[2], chan_id_1_2, chan_upd_1_2.short_channel_id);
505 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
506 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
507 check_added_monitors!(nodes[1], 1);
508 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
510 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
511 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
515 expect_pending_htlcs_forwardable!(nodes[1]);
516 check_added_monitors!(nodes[1], 1);
518 let mut updates_1_2 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
519 let mut update_add = &mut updates_1_2.update_add_htlcs[0];
520 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add);
521 check_added_monitors!(nodes[2], 0);
522 do_commitment_signed_dance(&nodes[2], &nodes[1], &updates_1_2.commitment_signed, true, true);
524 cause_error!(nodes[1], nodes[2], nodes[3], chan_id_2_3, chan_upd_2_3.short_channel_id);
525 check_added_monitors!(nodes[2], 1);
527 let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
528 let update_malformed = &mut updates.update_fail_malformed_htlcs[0];
529 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
530 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
532 // Ensure the intro node will properly blind the error if its downstream node failed to do so.
533 update_malformed.sha256_of_onion = [1; 32];
534 update_malformed.failure_code = INVALID_ONION_BLINDING ^ 1;
535 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
536 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);
538 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
539 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
540 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
541 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
542 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
546 fn blinded_intercept_payment() {
547 do_blinded_intercept_payment(true);
548 do_blinded_intercept_payment(false);
550 fn do_blinded_intercept_payment(intercept_node_fails: bool) {
551 let chanmon_cfgs = create_chanmon_cfgs(3);
552 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
553 let mut intercept_forwards_config = test_default_channel_config();
554 intercept_forwards_config.accept_intercept_htlcs = true;
555 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);
556 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
557 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
558 let (channel_id, chan_upd) = {
559 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
560 (chan.2, chan.0.contents)
564 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
565 let intercept_scid = nodes[1].node.get_intercept_scid();
566 let mut intercept_chan_upd = chan_upd;
567 intercept_chan_upd.short_channel_id = intercept_scid;
568 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
569 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&intercept_chan_upd],
570 &chanmon_cfgs[2].keys_manager);
572 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
573 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
574 check_added_monitors(&nodes[0], 1);
575 let payment_event = {
576 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
577 assert_eq!(events.len(), 1);
578 SendEvent::from_event(events.remove(0))
580 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
581 commitment_signed_dance!(nodes[1], nodes[0], &payment_event.commitment_msg, false, true);
583 let events = nodes[1].node.get_and_clear_pending_events();
584 assert_eq!(events.len(), 1);
585 let (intercept_id, expected_outbound_amount_msat) = match events[0] {
586 crate::events::Event::HTLCIntercepted {
587 intercept_id, payment_hash: pmt_hash,
588 requested_next_hop_scid: short_channel_id, expected_outbound_amount_msat, ..
590 assert_eq!(pmt_hash, payment_hash);
591 assert_eq!(short_channel_id, intercept_scid);
592 (intercept_id, expected_outbound_amount_msat)
597 if intercept_node_fails {
598 nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
599 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
600 nodes[1].node.process_pending_htlc_forwards();
601 let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
602 check_added_monitors!(&nodes[1], 1);
603 assert!(update_fail.update_fail_htlcs.len() == 1);
604 let fail_msg = update_fail.update_fail_htlcs[0].clone();
605 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
606 commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
607 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
608 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
612 nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
613 expect_pending_htlcs_forwardable!(nodes[1]);
615 let payment_event = {
617 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
618 assert_eq!(added_monitors.len(), 1);
619 added_monitors.clear();
621 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
622 assert_eq!(events.len(), 1);
623 SendEvent::from_event(events.remove(0))
625 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
626 commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
627 expect_pending_htlcs_forwardable!(nodes[2]);
629 expect_payment_claimable!(&nodes[2], payment_hash, payment_secret, amt_msat, None, nodes[2].node.get_our_node_id());
630 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
631 expect_payment_sent(&nodes[0], payment_preimage, Some(Some(1000)), true, true);
635 fn two_hop_blinded_path_success() {
636 let chanmon_cfgs = create_chanmon_cfgs(3);
637 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
638 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
639 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
640 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
641 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
644 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
645 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
646 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
647 &chanmon_cfgs[2].keys_manager);
649 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
650 check_added_monitors(&nodes[0], 1);
651 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], amt_msat, payment_hash, payment_secret);
652 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
656 fn three_hop_blinded_path_success() {
657 let chanmon_cfgs = create_chanmon_cfgs(5);
658 let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
659 let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
660 let mut nodes = create_network(5, &node_cfgs, &node_chanmgrs);
661 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
662 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
663 let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
664 let chan_upd_3_4 = create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 1_000_000, 0).0.contents;
666 // Get all our nodes onto the same height so payments don't fail for CLTV violations.
667 connect_blocks(&nodes[0], nodes[4].best_block_info().1 - nodes[0].best_block_info().1);
668 connect_blocks(&nodes[1], nodes[4].best_block_info().1 - nodes[1].best_block_info().1);
669 connect_blocks(&nodes[2], nodes[4].best_block_info().1 - nodes[2].best_block_info().1);
670 assert_eq!(nodes[4].best_block_info().1, nodes[3].best_block_info().1);
673 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[4], Some(amt_msat), None);
674 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
675 nodes.iter().skip(2).map(|n| n.node.get_our_node_id()).collect(),
676 &[&chan_upd_2_3, &chan_upd_3_4], &chanmon_cfgs[4].keys_manager);
678 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
679 check_added_monitors(&nodes[0], 1);
680 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2], &nodes[3], &nodes[4]]], amt_msat, payment_hash, payment_secret);
681 claim_payment(&nodes[0], &[&nodes[1], &nodes[2], &nodes[3], &nodes[4]], payment_preimage);
685 enum ReceiveCheckFail {
686 // The recipient fails the payment upon `PaymentClaimable`.
688 // Failure to decode the recipient's onion payload.
690 // The incoming HTLC did not satisfy our requirements; in this case it underpaid us according to
691 // the expected receive amount in the onion.
693 // The incoming HTLC errors when added to the Channel, in this case due to the HTLC being
694 // delivered out-of-order with a shutdown message.
696 // The HTLC is successfully added to the inbound channel but fails receive checks in
697 // process_pending_htlc_forwards.
698 ProcessPendingHTLCsCheck,
699 // The HTLC violates the `PaymentConstraints` contained within the receiver's encrypted payload.
704 fn multi_hop_receiver_fail() {
705 do_multi_hop_receiver_fail(ReceiveCheckFail::RecipientFail);
706 do_multi_hop_receiver_fail(ReceiveCheckFail::OnionDecodeFail);
707 do_multi_hop_receiver_fail(ReceiveCheckFail::ReceiveRequirements);
708 do_multi_hop_receiver_fail(ReceiveCheckFail::ChannelCheck);
709 do_multi_hop_receiver_fail(ReceiveCheckFail::ProcessPendingHTLCsCheck);
710 do_multi_hop_receiver_fail(ReceiveCheckFail::PaymentConstraints);
713 fn do_multi_hop_receiver_fail(check: ReceiveCheckFail) {
714 // Test that the receiver to a multihop blinded path fails back correctly.
715 let chanmon_cfgs = create_chanmon_cfgs(3);
716 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
717 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
718 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
719 // We need the session priv to construct an invalid onion packet later.
720 let session_priv = [3; 32];
721 *nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(session_priv);
722 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
723 let (chan_upd_1_2, chan_id_1_2) = {
724 let (chan_upd, _, channel_id, ..) = create_announced_chan_between_nodes_with_value(
725 &nodes, 1, 2, 1_000_000, 0
727 (chan_upd.contents, channel_id)
731 let excess_final_cltv_delta_opt = if check == ReceiveCheckFail::ProcessPendingHTLCsCheck {
732 // Set the final CLTV expiry too low to trigger the failure in process_pending_htlc_forwards.
733 Some(TEST_FINAL_CLTV as u16 - 2)
735 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), excess_final_cltv_delta_opt);
736 let mut route_params = get_blinded_route_parameters(amt_msat, payment_secret,
737 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
738 &chanmon_cfgs[2].keys_manager);
740 let route = if check == ReceiveCheckFail::ProcessPendingHTLCsCheck {
741 let mut route = get_route(&nodes[0], &route_params).unwrap();
742 // Set the final CLTV expiry too low to trigger the failure in process_pending_htlc_forwards.
743 route.paths[0].hops.last_mut().map(|h| h.cltv_expiry_delta += excess_final_cltv_delta_opt.unwrap() as u32);
744 route.paths[0].blinded_tail.as_mut().map(|bt| bt.excess_final_cltv_expiry_delta = excess_final_cltv_delta_opt.unwrap() as u32);
746 } else if check == ReceiveCheckFail::PaymentConstraints {
747 // Create a blinded path where the receiver's encrypted payload has an htlc_minimum_msat that is
748 // violated by `amt_msat`, and stick it in the route_params without changing the corresponding
749 // BlindedPayInfo (to ensure pathfinding still succeeds).
750 let high_htlc_min_bp = {
751 let mut high_htlc_minimum_upd = chan_upd_1_2.clone();
752 high_htlc_minimum_upd.htlc_minimum_msat = amt_msat + 1000;
753 let high_htlc_min_params = get_blinded_route_parameters(amt_msat, payment_secret,
754 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&high_htlc_minimum_upd],
755 &chanmon_cfgs[2].keys_manager);
756 if let Payee::Blinded { route_hints, .. } = high_htlc_min_params.payment_params.payee {
757 route_hints[0].1.clone()
760 if let Payee::Blinded { ref mut route_hints, .. } = route_params.payment_params.payee {
761 route_hints[0].1 = high_htlc_min_bp;
763 find_route(&nodes[0], &route_params).unwrap()
765 find_route(&nodes[0], &route_params).unwrap()
767 node_cfgs[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
768 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
769 check_added_monitors(&nodes[0], 1);
771 let mut payment_event_0_1 = {
772 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
773 assert_eq!(events.len(), 1);
774 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
775 SendEvent::from_event(ev)
777 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_0_1.msgs[0]);
778 check_added_monitors!(nodes[1], 0);
779 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event_0_1.commitment_msg, false, false);
780 expect_pending_htlcs_forwardable!(nodes[1]);
781 check_added_monitors!(&nodes[1], 1);
783 let mut payment_event_1_2 = {
784 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
785 assert_eq!(events.len(), 1);
786 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
787 SendEvent::from_event(ev)
791 ReceiveCheckFail::RecipientFail => {
792 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
793 check_added_monitors!(nodes[2], 0);
794 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
795 expect_pending_htlcs_forwardable!(nodes[2]);
796 check_payment_claimable(
797 &nodes[2].node.get_and_clear_pending_events()[0], payment_hash, payment_secret, amt_msat,
798 None, nodes[2].node.get_our_node_id()
800 nodes[2].node.fail_htlc_backwards(&payment_hash);
801 expect_pending_htlcs_forwardable_conditions(
802 nodes[2].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash }]
804 nodes[2].node.process_pending_htlc_forwards();
805 check_added_monitors!(nodes[2], 1);
807 ReceiveCheckFail::OnionDecodeFail => {
808 let session_priv = SecretKey::from_slice(&session_priv).unwrap();
809 let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
810 let cur_height = nodes[0].best_block_info().1;
811 let (mut onion_payloads, ..) = onion_utils::build_onion_payloads(
812 &route.paths[0], amt_msat, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
814 let update_add = &mut payment_event_1_2.msgs[0];
815 onion_payloads.last_mut().map(|p| {
816 if let msgs::OutboundOnionPayload::BlindedReceive { ref mut intro_node_blinding_point, .. } = p {
817 // The receiver should error if both the update_add blinding_point and the
818 // intro_node_blinding_point are set.
819 assert!(intro_node_blinding_point.is_none() && update_add.blinding_point.is_some());
820 *intro_node_blinding_point = Some(PublicKey::from_slice(&[2; 33]).unwrap());
823 update_add.onion_routing_packet = onion_utils::construct_onion_packet(
824 vec![onion_payloads.pop().unwrap()], vec![onion_keys.pop().unwrap()], [0; 32],
827 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update_add);
828 check_added_monitors!(nodes[2], 0);
829 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
831 ReceiveCheckFail::ReceiveRequirements => {
832 let update_add = &mut payment_event_1_2.msgs[0];
833 update_add.amount_msat -= 1;
834 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update_add);
835 check_added_monitors!(nodes[2], 0);
836 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
838 ReceiveCheckFail::ChannelCheck => {
839 nodes[2].node.close_channel(&chan_id_1_2, &nodes[1].node.get_our_node_id()).unwrap();
840 let node_2_shutdown = get_event_msg!(nodes[2], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
841 nodes[1].node.handle_shutdown(&nodes[2].node.get_our_node_id(), &node_2_shutdown);
842 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
844 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
845 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event_1_2.commitment_msg);
846 check_added_monitors!(nodes[2], 1);
848 nodes[2].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
849 commitment_signed_dance!(nodes[2], nodes[1], (), false, true, false, false);
851 ReceiveCheckFail::ProcessPendingHTLCsCheck => {
852 assert_eq!(payment_event_1_2.msgs[0].cltv_expiry, nodes[0].best_block_info().1 + 1 + excess_final_cltv_delta_opt.unwrap() as u32 + TEST_FINAL_CLTV);
853 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
854 check_added_monitors!(nodes[2], 0);
855 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
856 expect_pending_htlcs_forwardable!(nodes[2]);
857 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[2],
858 vec![HTLCDestination::FailedPayment { payment_hash }]);
859 check_added_monitors!(nodes[2], 1);
861 ReceiveCheckFail::PaymentConstraints => {
862 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
863 check_added_monitors!(nodes[2], 0);
864 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
868 let updates_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
869 assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
870 let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
871 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
872 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
873 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
874 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, true, false);
876 let updates_1_0 = if check == ReceiveCheckFail::ChannelCheck {
877 let events = nodes[1].node.get_and_clear_pending_msg_events();
878 assert_eq!(events.len(), 2);
879 events.into_iter().find_map(|ev| {
881 MessageSendEvent:: UpdateHTLCs { node_id, updates } => {
882 assert_eq!(node_id, nodes[0].node.get_our_node_id());
885 MessageSendEvent::SendClosingSigned { .. } => None,
889 } else { get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()) };
890 assert_eq!(updates_1_0.update_fail_htlcs.len(), 1);
891 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_1_0.update_fail_htlcs[0]);
892 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
893 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
894 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
898 fn blinded_path_retries() {
899 let chanmon_cfgs = create_chanmon_cfgs(4);
900 // Make one blinded path's fees slightly higher so they are tried in a deterministic order.
901 let mut higher_fee_chan_cfg = test_default_channel_config();
902 higher_fee_chan_cfg.channel_config.forwarding_fee_base_msat += 1;
903 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
904 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, Some(higher_fee_chan_cfg), None]);
905 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
907 // Create this network topology so nodes[0] has a blinded route hint to retry over.
913 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
914 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
915 let chan_1_3 = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0);
916 let chan_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
919 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
921 let pay_params = PaymentParameters::blinded(
923 blinded_payment_path(payment_secret,
924 vec![nodes[1].node.get_our_node_id(), nodes[3].node.get_our_node_id()], &[&chan_1_3.0.contents],
925 &chanmon_cfgs[3].keys_manager
927 blinded_payment_path(payment_secret,
928 vec![nodes[2].node.get_our_node_id(), nodes[3].node.get_our_node_id()], &[&chan_2_3.0.contents],
929 &chanmon_cfgs[3].keys_manager
933 .with_bolt12_features(channelmanager::provided_bolt12_invoice_features(&UserConfig::default()))
935 RouteParameters::from_payment_params_and_value(pay_params, amt_msat)
938 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(2)).unwrap();
939 check_added_monitors(&nodes[0], 1);
940 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]]], amt_msat, payment_hash, payment_secret);
942 macro_rules! fail_payment_back {
943 ($intro_node: expr) => {
944 nodes[3].node.fail_htlc_backwards(&payment_hash);
945 expect_pending_htlcs_forwardable_conditions(
946 nodes[3].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash }]
948 nodes[3].node.process_pending_htlc_forwards();
949 check_added_monitors!(nodes[3], 1);
951 let updates = get_htlc_update_msgs!(nodes[3], $intro_node.node.get_our_node_id());
952 assert_eq!(updates.update_fail_malformed_htlcs.len(), 1);
953 let update_malformed = &updates.update_fail_malformed_htlcs[0];
954 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
955 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
956 $intro_node.node.handle_update_fail_malformed_htlc(&nodes[3].node.get_our_node_id(), update_malformed);
957 do_commitment_signed_dance(&$intro_node, &nodes[3], &updates.commitment_signed, true, false);
959 let updates = get_htlc_update_msgs!($intro_node, nodes[0].node.get_our_node_id());
960 assert_eq!(updates.update_fail_htlcs.len(), 1);
961 nodes[0].node.handle_update_fail_htlc(&$intro_node.node.get_our_node_id(), &updates.update_fail_htlcs[0]);
962 do_commitment_signed_dance(&nodes[0], &$intro_node, &updates.commitment_signed, false, false);
964 let mut events = nodes[0].node.get_and_clear_pending_events();
965 assert_eq!(events.len(), 2);
967 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
968 assert_eq!(payment_hash, ev_payment_hash);
969 assert_eq!(payment_failed_permanently, false);
971 _ => panic!("Unexpected event"),
974 Event::PendingHTLCsForwardable { .. } => {},
975 _ => panic!("Unexpected event"),
977 nodes[0].node.process_pending_htlc_forwards();
981 fail_payment_back!(nodes[1]);
983 // Pass the retry along.
984 check_added_monitors!(nodes[0], 1);
985 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
986 assert_eq!(msg_events.len(), 1);
987 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], amt_msat, payment_hash, Some(payment_secret), msg_events.pop().unwrap(), true, None);
989 fail_payment_back!(nodes[2]);
990 let evs = nodes[0].node.get_and_clear_pending_events();
991 assert_eq!(evs.len(), 1);
993 Event::PaymentFailed { payment_hash: ev_payment_hash, reason, .. } => {
994 assert_eq!(ev_payment_hash, payment_hash);
995 // We have 1 retry attempt remaining, but we're out of blinded paths to try.
996 assert_eq!(reason, Some(PaymentFailureReason::RouteNotFound));