|
10 | 10 | use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
|
11 | 11 | use crate::blinded_path::BlindedPath;
|
12 | 12 | use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs};
|
13 |
| -use crate::events::MessageSendEventsProvider; |
| 13 | +use crate::events::{HTLCDestination, MessageSendEventsProvider}; |
14 | 14 | use crate::ln::PaymentSecret;
|
15 | 15 | use crate::ln::channelmanager;
|
16 | 16 | use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
|
@@ -292,3 +292,87 @@ fn failed_backwards_to_intro_node() {
|
292 | 292 | expect_payment_failed_conditions(&nodes[0], payment_hash, false,
|
293 | 293 | PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
|
294 | 294 | }
|
| 295 | + |
| 296 | +enum ProcessPendingHTLCsCheck { |
| 297 | + FwdPeerDisconnected, |
| 298 | + FwdChannelClosed, |
| 299 | +} |
| 300 | + |
| 301 | +#[test] |
| 302 | +fn forward_fail_in_process_pending_htlc_fwds() { |
| 303 | + do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdPeerDisconnected); |
| 304 | + do_forward_fail_in_process_pending_htlc_fwds(ProcessPendingHTLCsCheck::FwdChannelClosed); |
| 305 | +} |
| 306 | +fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck) { |
| 307 | + // Ensure the intro node will error backwards properly if the HTLC fails in |
| 308 | + // process_pending_htlc_forwards. |
| 309 | + let chanmon_cfgs = create_chanmon_cfgs(3); |
| 310 | + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); |
| 311 | + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); |
| 312 | + let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); |
| 313 | + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); |
| 314 | + let (chan_upd_1_2, channel_id) = { |
| 315 | + let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0); |
| 316 | + (chan.0.contents, chan.2) |
| 317 | + }; |
| 318 | + |
| 319 | + let amt_msat = 5000; |
| 320 | + let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None); |
| 321 | + let route_params = get_blinded_route_parameters(amt_msat, payment_secret, |
| 322 | + nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2], |
| 323 | + &chanmon_cfgs[2].keys_manager); |
| 324 | + |
| 325 | + nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap(); |
| 326 | + check_added_monitors(&nodes[0], 1); |
| 327 | + |
| 328 | + let mut events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 329 | + assert_eq!(events.len(), 1); |
| 330 | + let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events); |
| 331 | + let mut payment_event = SendEvent::from_event(ev); |
| 332 | + |
| 333 | + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]); |
| 334 | + check_added_monitors!(nodes[1], 0); |
| 335 | + do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false); |
| 336 | + |
| 337 | + let (update_fail, cs) = match check { |
| 338 | + ProcessPendingHTLCsCheck::FwdPeerDisconnected => { |
| 339 | + // Disconnect the next-hop peer so when we go to forward in process_pending_htlc_forwards, the |
| 340 | + // intro node will error backwards. |
| 341 | + nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id()); |
| 342 | + expect_pending_htlcs_forwardable!(nodes[1]); |
| 343 | + expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], |
| 344 | + vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id }]); |
| 345 | + let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); |
| 346 | + (updates.update_fail_htlcs.pop().unwrap(), updates.commitment_signed) |
| 347 | + }, |
| 348 | + ProcessPendingHTLCsCheck::FwdChannelClosed => { |
| 349 | + // Force close the next-hop channel so when we go to forward in process_pending_htlc_forwards, |
| 350 | + // the intro node will error backwards. |
| 351 | + nodes[1].node.force_close_broadcasting_latest_txn(&channel_id, &nodes[2].node.get_our_node_id()).unwrap(); |
| 352 | + let events = nodes[1].node.get_and_clear_pending_events(); |
| 353 | + match events[0] { |
| 354 | + crate::events::Event::PendingHTLCsForwardable { .. } => {}, |
| 355 | + _ => panic!("Unexpected event {:?}", events), |
| 356 | + }; |
| 357 | + match events[1] { |
| 358 | + crate::events::Event::ChannelClosed { .. } => {}, |
| 359 | + _ => panic!("Unexpected event {:?}", events), |
| 360 | + } |
| 361 | + |
| 362 | + nodes[1].node.process_pending_htlc_forwards(); |
| 363 | + expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], |
| 364 | + vec![HTLCDestination::UnknownNextHop { requested_forward_scid: chan_upd_1_2.short_channel_id }]); |
| 365 | + check_closed_broadcast(&nodes[1], 1, true); |
| 366 | + check_added_monitors!(nodes[1], 1); |
| 367 | + nodes[1].node.process_pending_htlc_forwards(); |
| 368 | + let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); |
| 369 | + (updates.update_fail_htlcs.pop().unwrap(), updates.commitment_signed) |
| 370 | + }, |
| 371 | + }; |
| 372 | + |
| 373 | + nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail); |
| 374 | + check_added_monitors!(nodes[1], 1); |
| 375 | + do_commitment_signed_dance(&nodes[0], &nodes[1], &cs, false, false); |
| 376 | + expect_payment_failed_conditions(&nodes[0], payment_hash, false, |
| 377 | + PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32])); |
| 378 | +} |
0 commit comments