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, 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, &chanmon_cfgs[1].keys_manager, &secp_ctx
106 let route_params = RouteParameters::from_payment_params_and_value(
107 PaymentParameters::blinded(vec![blinded_path]),
110 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
111 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
112 check_added_monitors(&nodes[0], 1);
113 pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret);
115 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
117 fail_payment(&nodes[0], &[&nodes[1]], payment_hash);
122 fn mpp_to_one_hop_blinded_path() {
123 let chanmon_cfgs = create_chanmon_cfgs(4);
124 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
125 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
126 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
127 let mut secp_ctx = Secp256k1::new();
129 create_announced_chan_between_nodes(&nodes, 0, 1);
130 create_announced_chan_between_nodes(&nodes, 0, 2);
131 let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
132 create_announced_chan_between_nodes(&nodes, 2, 3).0.contents;
134 let amt_msat = 15_000_000;
135 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
136 let payee_tlvs = ReceiveTlvs {
138 payment_constraints: PaymentConstraints {
139 max_cltv_expiry: u32::max_value(),
140 htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
143 let blinded_path = BlindedPath::one_hop_for_payment(
144 nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx
147 let bolt12_features =
148 channelmanager::provided_bolt12_invoice_features(&UserConfig::default());
149 let route_params = RouteParameters::from_payment_params_and_value(
150 PaymentParameters::blinded(vec![blinded_path]).with_bolt12_features(bolt12_features).unwrap(),
153 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
154 check_added_monitors(&nodes[0], 2);
156 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
157 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
158 assert_eq!(events.len(), 2);
160 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
161 pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
162 Some(payment_secret), ev.clone(), false, None);
164 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
165 pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
166 Some(payment_secret), ev.clone(), true, None);
167 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
170 enum ForwardCheckFail {
171 // Fail a check on the inbound onion payload. In this case, we underflow when calculating the
172 // outgoing cltv_expiry.
174 // The forwarding node's payload is encoded as a receive, i.e. the next hop HMAC is [0; 32].
175 ForwardPayloadEncodedAsReceive,
176 // Fail a check on the outbound channel. In this case, our next-hop peer is offline.
177 OutboundChannelCheck,
181 fn forward_checks_failure() {
182 do_forward_checks_failure(ForwardCheckFail::InboundOnionCheck);
183 do_forward_checks_failure(ForwardCheckFail::ForwardPayloadEncodedAsReceive);
184 do_forward_checks_failure(ForwardCheckFail::OutboundChannelCheck);
187 fn do_forward_checks_failure(check: ForwardCheckFail) {
188 // Ensure we'll fail backwards properly if a forwarding check fails on initial update_add
190 let chanmon_cfgs = create_chanmon_cfgs(3);
191 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
192 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
193 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
194 // We need the session priv to construct a bogus onion packet later.
195 *nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some([3; 32]);
196 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
197 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
200 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
201 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
202 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
203 &chanmon_cfgs[2].keys_manager);
205 let route = get_route(&nodes[0], &route_params).unwrap();
206 node_cfgs[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
207 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
208 check_added_monitors(&nodes[0], 1);
210 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
211 assert_eq!(events.len(), 1);
212 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
213 let mut payment_event = SendEvent::from_event(ev);
215 let mut update_add = &mut payment_event.msgs[0];
217 ForwardCheckFail::InboundOnionCheck => {
218 update_add.cltv_expiry = 10; // causes outbound CLTV expiry to underflow
220 ForwardCheckFail::ForwardPayloadEncodedAsReceive => {
221 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
222 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
223 let cur_height = nodes[0].best_block_info().1;
224 let (mut onion_payloads, ..) = onion_utils::build_onion_payloads(
225 &route.paths[0], amt_msat, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
226 // Remove the receive payload so the blinded forward payload is encoded as a final payload
227 // (i.e. next_hop_hmac == [0; 32])
228 onion_payloads.pop();
229 update_add.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
231 ForwardCheckFail::OutboundChannelCheck => {
232 // The intro node will see that the next-hop peer is disconnected and fail the HTLC backwards.
233 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
236 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
237 check_added_monitors!(nodes[1], 0);
238 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, true, true);
240 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
241 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
242 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
243 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
244 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
248 fn failed_backwards_to_intro_node() {
249 // Ensure the intro node will error backwards properly even if the downstream node did not blind
251 let chanmon_cfgs = create_chanmon_cfgs(3);
252 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
253 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
254 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
255 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
256 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
259 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
260 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
261 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
262 &chanmon_cfgs[2].keys_manager);
264 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
265 check_added_monitors(&nodes[0], 1);
267 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
268 assert_eq!(events.len(), 1);
269 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
270 let mut payment_event = SendEvent::from_event(ev);
272 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
273 check_added_monitors!(nodes[1], 0);
274 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false);
275 expect_pending_htlcs_forwardable!(nodes[1]);
276 check_added_monitors!(&nodes[1], 1);
278 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
279 assert_eq!(events.len(), 1);
280 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
281 let mut payment_event = SendEvent::from_event(ev);
283 // Ensure the final node fails to handle the HTLC.
284 payment_event.msgs[0].onion_routing_packet.hop_data[0] ^= 1;
285 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
286 check_added_monitors!(nodes[2], 0);
287 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event.commitment_msg, true, true);
288 nodes[2].node.process_pending_htlc_forwards();
290 let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
291 let mut update_malformed = &mut updates.update_fail_malformed_htlcs[0];
292 // Check that the final node encodes its failure correctly.
293 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
294 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
296 // Modify such the final hop does not correctly blind their error so we can ensure the intro node
297 // converts it to the correct error.
298 update_malformed.sha256_of_onion = [1; 32];
299 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
300 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);
302 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
303 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
304 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
305 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
306 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
309 enum ProcessPendingHTLCsCheck {
315 fn forward_fail_in_process_pending_htlc_fwds() {
316 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdPeerDisconnected);
317 do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdChannelClosed);
319 fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck) {
320 // Ensure the intro node will error backwards properly if the HTLC fails in
321 // process_pending_htlc_forwards.
322 let chanmon_cfgs = create_chanmon_cfgs(3);
323 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
324 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
325 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
326 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
327 let (chan_upd_1_2, channel_id) = {
328 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
329 (chan.0.contents, chan.2)
333 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
334 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
335 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
336 &chanmon_cfgs[2].keys_manager);
338 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
339 check_added_monitors(&nodes[0], 1);
341 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
342 assert_eq!(events.len(), 1);
343 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
344 let mut payment_event = SendEvent::from_event(ev);
346 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
347 check_added_monitors!(nodes[1], 0);
348 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false);
351 ProcessPendingHTLCsCheck::FwdPeerDisconnected => {
352 // Disconnect the next-hop peer so when we go to forward in process_pending_htlc_forwards, the
353 // intro node will error backwards.
354 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
355 expect_pending_htlcs_forwardable!(nodes[1]);
356 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1],
357 vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id }]);
359 ProcessPendingHTLCsCheck::FwdChannelClosed => {
360 // Force close the next-hop channel so when we go to forward in process_pending_htlc_forwards,
361 // the intro node will error backwards.
362 nodes[1].node.force_close_broadcasting_latest_txn(&channel_id, &nodes[2].node.get_our_node_id()).unwrap();
363 let events = nodes[1].node.get_and_clear_pending_events();
365 crate::events::Event::PendingHTLCsForwardable { .. } => {},
366 _ => panic!("Unexpected event {:?}", events),
369 crate::events::Event::ChannelClosed { .. } => {},
370 _ => panic!("Unexpected event {:?}", events),
373 nodes[1].node.process_pending_htlc_forwards();
374 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1],
375 vec![HTLCDestination::UnknownNextHop { requested_forward_scid: chan_upd_1_2.short_channel_id }]);
376 check_closed_broadcast(&nodes[1], 1, true);
377 check_added_monitors!(nodes[1], 1);
378 nodes[1].node.process_pending_htlc_forwards();
382 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
383 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
384 check_added_monitors!(nodes[1], 1);
385 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
387 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
388 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
392 fn blinded_intercept_payment() {
393 do_blinded_intercept_payment(true);
394 do_blinded_intercept_payment(false);
396 fn do_blinded_intercept_payment(intercept_node_fails: bool) {
397 let chanmon_cfgs = create_chanmon_cfgs(3);
398 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
399 let mut intercept_forwards_config = test_default_channel_config();
400 intercept_forwards_config.accept_intercept_htlcs = true;
401 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);
402 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
403 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
404 let (channel_id, chan_upd) = {
405 let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
406 (chan.2, chan.0.contents)
410 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
411 let intercept_scid = nodes[1].node.get_intercept_scid();
412 let mut intercept_chan_upd = chan_upd;
413 intercept_chan_upd.short_channel_id = intercept_scid;
414 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
415 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&intercept_chan_upd],
416 &chanmon_cfgs[2].keys_manager);
418 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
419 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
420 check_added_monitors(&nodes[0], 1);
421 let payment_event = {
422 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
423 assert_eq!(events.len(), 1);
424 SendEvent::from_event(events.remove(0))
426 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
427 commitment_signed_dance!(nodes[1], nodes[0], &payment_event.commitment_msg, false, true);
429 let events = nodes[1].node.get_and_clear_pending_events();
430 assert_eq!(events.len(), 1);
431 let (intercept_id, expected_outbound_amount_msat) = match events[0] {
432 crate::events::Event::HTLCIntercepted {
433 intercept_id, payment_hash: pmt_hash,
434 requested_next_hop_scid: short_channel_id, expected_outbound_amount_msat, ..
436 assert_eq!(pmt_hash, payment_hash);
437 assert_eq!(short_channel_id, intercept_scid);
438 (intercept_id, expected_outbound_amount_msat)
443 if intercept_node_fails {
444 nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
445 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
446 nodes[1].node.process_pending_htlc_forwards();
447 let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
448 check_added_monitors!(&nodes[1], 1);
449 assert!(update_fail.update_fail_htlcs.len() == 1);
450 let fail_msg = update_fail.update_fail_htlcs[0].clone();
451 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
452 commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
453 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
454 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
458 nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
459 expect_pending_htlcs_forwardable!(nodes[1]);
461 let payment_event = {
463 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
464 assert_eq!(added_monitors.len(), 1);
465 added_monitors.clear();
467 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
468 assert_eq!(events.len(), 1);
469 SendEvent::from_event(events.remove(0))
471 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
472 commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
473 expect_pending_htlcs_forwardable!(nodes[2]);
475 expect_payment_claimable!(&nodes[2], payment_hash, payment_secret, amt_msat, None, nodes[2].node.get_our_node_id());
476 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
477 expect_payment_sent(&nodes[0], payment_preimage, Some(Some(1000)), true, true);
481 fn two_hop_blinded_path_success() {
482 let chanmon_cfgs = create_chanmon_cfgs(3);
483 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
484 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
485 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
486 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
487 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
490 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
491 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
492 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
493 &chanmon_cfgs[2].keys_manager);
495 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
496 check_added_monitors(&nodes[0], 1);
497 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], amt_msat, payment_hash, payment_secret);
498 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
502 fn three_hop_blinded_path_success() {
503 let chanmon_cfgs = create_chanmon_cfgs(5);
504 let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
505 let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
506 let mut nodes = create_network(5, &node_cfgs, &node_chanmgrs);
507 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
508 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
509 let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
510 let chan_upd_3_4 = create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 1_000_000, 0).0.contents;
513 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[4], Some(amt_msat), None);
514 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
515 nodes.iter().skip(2).map(|n| n.node.get_our_node_id()).collect(),
516 &[&chan_upd_2_3, &chan_upd_3_4], &chanmon_cfgs[4].keys_manager);
518 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
519 check_added_monitors(&nodes[0], 1);
520 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2], &nodes[3], &nodes[4]]], amt_msat, payment_hash, payment_secret);
521 claim_payment(&nodes[0], &[&nodes[1], &nodes[2], &nodes[3], &nodes[4]], payment_preimage);
525 enum ReceiveCheckFail {
526 // The recipient fails the payment upon `PaymentClaimable`.
528 // Failure to decode the recipient's onion payload.
530 // The incoming HTLC did not satisfy our requirements; in this case it underpaid us according to
531 // the expected receive amount in the onion.
533 // The incoming HTLC errors when added to the Channel, in this case due to the HTLC being
534 // delivered out-of-order with a shutdown message.
536 // The HTLC is successfully added to the inbound channel but fails receive checks in
537 // process_pending_htlc_forwards.
538 ProcessPendingHTLCsCheck,
539 // The HTLC violates the `PaymentConstraints` contained within the receiver's encrypted payload.
544 fn multi_hop_receiver_fail() {
545 do_multi_hop_receiver_fail(ReceiveCheckFail::RecipientFail);
546 do_multi_hop_receiver_fail(ReceiveCheckFail::OnionDecodeFail);
547 do_multi_hop_receiver_fail(ReceiveCheckFail::ReceiveRequirements);
548 do_multi_hop_receiver_fail(ReceiveCheckFail::ChannelCheck);
549 do_multi_hop_receiver_fail(ReceiveCheckFail::ProcessPendingHTLCsCheck);
550 do_multi_hop_receiver_fail(ReceiveCheckFail::PaymentConstraints);
553 fn do_multi_hop_receiver_fail(check: ReceiveCheckFail) {
554 // Test that the receiver to a multihop blinded path fails back correctly.
555 let chanmon_cfgs = create_chanmon_cfgs(3);
556 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
557 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
558 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
559 // We need the session priv to construct an invalid onion packet later.
560 let session_priv = [3; 32];
561 *nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(session_priv);
562 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
563 let (chan_upd_1_2, chan_id_1_2) = {
564 let (chan_upd, _, channel_id, ..) = create_announced_chan_between_nodes_with_value(
565 &nodes, 1, 2, 1_000_000, 0
567 (chan_upd.contents, channel_id)
571 let excess_final_cltv_delta_opt = if check == ReceiveCheckFail::ProcessPendingHTLCsCheck {
572 // Set the final CLTV expiry too low to trigger the failure in process_pending_htlc_forwards.
573 Some(TEST_FINAL_CLTV as u16 - 2)
575 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), excess_final_cltv_delta_opt);
576 let mut route_params = get_blinded_route_parameters(amt_msat, payment_secret,
577 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
578 &chanmon_cfgs[2].keys_manager);
580 let route = if check == ReceiveCheckFail::ProcessPendingHTLCsCheck {
581 let mut route = get_route(&nodes[0], &route_params).unwrap();
582 // Set the final CLTV expiry too low to trigger the failure in process_pending_htlc_forwards.
583 route.paths[0].hops.last_mut().map(|h| h.cltv_expiry_delta += excess_final_cltv_delta_opt.unwrap() as u32);
584 route.paths[0].blinded_tail.as_mut().map(|bt| bt.excess_final_cltv_expiry_delta = excess_final_cltv_delta_opt.unwrap() as u32);
586 } else if check == ReceiveCheckFail::PaymentConstraints {
587 // Create a blinded path where the receiver's encrypted payload has an htlc_minimum_msat that is
588 // violated by `amt_msat`, and stick it in the route_params without changing the corresponding
589 // BlindedPayInfo (to ensure pathfinding still succeeds).
590 let high_htlc_min_bp = {
591 let mut high_htlc_minimum_upd = chan_upd_1_2.clone();
592 high_htlc_minimum_upd.htlc_minimum_msat = amt_msat + 1000;
593 let high_htlc_min_params = get_blinded_route_parameters(amt_msat, payment_secret,
594 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&high_htlc_minimum_upd],
595 &chanmon_cfgs[2].keys_manager);
596 if let Payee::Blinded { route_hints, .. } = high_htlc_min_params.payment_params.payee {
597 route_hints[0].1.clone()
600 if let Payee::Blinded { ref mut route_hints, .. } = route_params.payment_params.payee {
601 route_hints[0].1 = high_htlc_min_bp;
603 find_route(&nodes[0], &route_params).unwrap()
605 find_route(&nodes[0], &route_params).unwrap()
607 node_cfgs[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
608 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
609 check_added_monitors(&nodes[0], 1);
611 let mut payment_event_0_1 = {
612 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
613 assert_eq!(events.len(), 1);
614 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
615 SendEvent::from_event(ev)
617 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_0_1.msgs[0]);
618 check_added_monitors!(nodes[1], 0);
619 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event_0_1.commitment_msg, false, false);
620 expect_pending_htlcs_forwardable!(nodes[1]);
621 check_added_monitors!(&nodes[1], 1);
623 let mut payment_event_1_2 = {
624 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
625 assert_eq!(events.len(), 1);
626 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
627 SendEvent::from_event(ev)
631 ReceiveCheckFail::RecipientFail => {
632 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
633 check_added_monitors!(nodes[2], 0);
634 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
635 expect_pending_htlcs_forwardable!(nodes[2]);
636 check_payment_claimable(
637 &nodes[2].node.get_and_clear_pending_events()[0], payment_hash, payment_secret, amt_msat,
638 None, nodes[2].node.get_our_node_id()
640 nodes[2].node.fail_htlc_backwards(&payment_hash);
641 expect_pending_htlcs_forwardable_conditions(
642 nodes[2].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash }]
644 nodes[2].node.process_pending_htlc_forwards();
645 check_added_monitors!(nodes[2], 1);
647 ReceiveCheckFail::OnionDecodeFail => {
648 let session_priv = SecretKey::from_slice(&session_priv).unwrap();
649 let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
650 let cur_height = nodes[0].best_block_info().1;
651 let (mut onion_payloads, ..) = onion_utils::build_onion_payloads(
652 &route.paths[0], amt_msat, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
654 let update_add = &mut payment_event_1_2.msgs[0];
655 onion_payloads.last_mut().map(|p| {
656 if let msgs::OutboundOnionPayload::BlindedReceive { ref mut intro_node_blinding_point, .. } = p {
657 // The receiver should error if both the update_add blinding_point and the
658 // intro_node_blinding_point are set.
659 assert!(intro_node_blinding_point.is_none() && update_add.blinding_point.is_some());
660 *intro_node_blinding_point = Some(PublicKey::from_slice(&[2; 33]).unwrap());
663 update_add.onion_routing_packet = onion_utils::construct_onion_packet(
664 vec![onion_payloads.pop().unwrap()], vec![onion_keys.pop().unwrap()], [0; 32],
667 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update_add);
668 check_added_monitors!(nodes[2], 0);
669 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
671 ReceiveCheckFail::ReceiveRequirements => {
672 let update_add = &mut payment_event_1_2.msgs[0];
673 update_add.amount_msat -= 1;
674 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update_add);
675 check_added_monitors!(nodes[2], 0);
676 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
678 ReceiveCheckFail::ChannelCheck => {
679 nodes[2].node.close_channel(&chan_id_1_2, &nodes[1].node.get_our_node_id()).unwrap();
680 let node_2_shutdown = get_event_msg!(nodes[2], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
681 nodes[1].node.handle_shutdown(&nodes[2].node.get_our_node_id(), &node_2_shutdown);
682 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
684 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
685 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event_1_2.commitment_msg);
686 check_added_monitors!(nodes[2], 1);
688 nodes[2].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
689 commitment_signed_dance!(nodes[2], nodes[1], (), false, true, false, false);
691 ReceiveCheckFail::ProcessPendingHTLCsCheck => {
692 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);
693 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
694 check_added_monitors!(nodes[2], 0);
695 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
696 expect_pending_htlcs_forwardable!(nodes[2]);
697 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[2],
698 vec![HTLCDestination::FailedPayment { payment_hash }]);
699 check_added_monitors!(nodes[2], 1);
701 ReceiveCheckFail::PaymentConstraints => {
702 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_1_2.msgs[0]);
703 check_added_monitors!(nodes[2], 0);
704 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event_1_2.commitment_msg, true, true);
708 let updates_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
709 assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
710 let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
711 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
712 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
713 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), update_malformed);
714 do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, true, false);
716 let updates_1_0 = if check == ReceiveCheckFail::ChannelCheck {
717 let events = nodes[1].node.get_and_clear_pending_msg_events();
718 assert_eq!(events.len(), 2);
719 events.into_iter().find_map(|ev| {
721 MessageSendEvent:: UpdateHTLCs { node_id, updates } => {
722 assert_eq!(node_id, nodes[0].node.get_our_node_id());
725 MessageSendEvent::SendClosingSigned { .. } => None,
729 } else { get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()) };
730 assert_eq!(updates_1_0.update_fail_htlcs.len(), 1);
731 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_1_0.update_fail_htlcs[0]);
732 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
733 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
734 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
738 fn blinded_path_retries() {
739 let chanmon_cfgs = create_chanmon_cfgs(4);
740 // Make one blinded path's fees slightly higher so they are tried in a deterministic order.
741 let mut higher_fee_chan_cfg = test_default_channel_config();
742 higher_fee_chan_cfg.channel_config.forwarding_fee_base_msat += 1;
743 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
744 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, Some(higher_fee_chan_cfg), None]);
745 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
747 // Create this network topology so nodes[0] has a blinded route hint to retry over.
753 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
754 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
755 let chan_1_3 = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0);
756 let chan_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
759 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
761 let pay_params = PaymentParameters::blinded(
763 blinded_payment_path(payment_secret,
764 vec![nodes[1].node.get_our_node_id(), nodes[3].node.get_our_node_id()], &[&chan_1_3.0.contents],
765 &chanmon_cfgs[3].keys_manager
767 blinded_payment_path(payment_secret,
768 vec![nodes[2].node.get_our_node_id(), nodes[3].node.get_our_node_id()], &[&chan_2_3.0.contents],
769 &chanmon_cfgs[3].keys_manager
773 .with_bolt12_features(channelmanager::provided_bolt12_invoice_features(&UserConfig::default()))
775 RouteParameters::from_payment_params_and_value(pay_params, amt_msat)
778 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(2)).unwrap();
779 check_added_monitors(&nodes[0], 1);
780 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]]], amt_msat, payment_hash, payment_secret);
782 macro_rules! fail_payment_back {
783 ($intro_node: expr) => {
784 nodes[3].node.fail_htlc_backwards(&payment_hash);
785 expect_pending_htlcs_forwardable_conditions(
786 nodes[3].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash }]
788 nodes[3].node.process_pending_htlc_forwards();
789 check_added_monitors!(nodes[3], 1);
791 let updates = get_htlc_update_msgs!(nodes[3], $intro_node.node.get_our_node_id());
792 assert_eq!(updates.update_fail_malformed_htlcs.len(), 1);
793 let update_malformed = &updates.update_fail_malformed_htlcs[0];
794 assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
795 assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
796 $intro_node.node.handle_update_fail_malformed_htlc(&nodes[3].node.get_our_node_id(), update_malformed);
797 do_commitment_signed_dance(&$intro_node, &nodes[3], &updates.commitment_signed, true, false);
799 let updates = get_htlc_update_msgs!($intro_node, nodes[0].node.get_our_node_id());
800 assert_eq!(updates.update_fail_htlcs.len(), 1);
801 nodes[0].node.handle_update_fail_htlc(&$intro_node.node.get_our_node_id(), &updates.update_fail_htlcs[0]);
802 do_commitment_signed_dance(&nodes[0], &$intro_node, &updates.commitment_signed, false, false);
804 let mut events = nodes[0].node.get_and_clear_pending_events();
805 assert_eq!(events.len(), 2);
807 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
808 assert_eq!(payment_hash, ev_payment_hash);
809 assert_eq!(payment_failed_permanently, false);
811 _ => panic!("Unexpected event"),
814 Event::PendingHTLCsForwardable { .. } => {},
815 _ => panic!("Unexpected event"),
817 nodes[0].node.process_pending_htlc_forwards();
821 fail_payment_back!(nodes[1]);
823 // Pass the retry along.
824 check_added_monitors!(nodes[0], 1);
825 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
826 assert_eq!(msg_events.len(), 1);
827 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], amt_msat, payment_hash, Some(payment_secret), msg_events.pop().unwrap(), true, None);
829 fail_payment_back!(nodes[2]);
830 let evs = nodes[0].node.get_and_clear_pending_events();
831 assert_eq!(evs.len(), 1);
833 Event::PaymentFailed { payment_hash: ev_payment_hash, reason, .. } => {
834 assert_eq!(ev_payment_hash, payment_hash);
835 // We have 1 retry attempt remaining, but we're out of blinded paths to try.
836 assert_eq!(reason, Some(PaymentFailureReason::RouteNotFound));