@@ -9601,6 +9601,77 @@ fn test_forwardable_regen() {
9601
9601
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
9602
9602
}
9603
9603
9604
+ #[test]
9605
+ fn test_dup_htlc_second_fail_panic() {
9606
+ // Previously, if we received two HTLCs back-to-back, where the second overran the expected
9607
+ // value for the payment, we'd fail back both HTLCs after generating a `PaymentReceived` event.
9608
+ // Then, if the user failed the second payment, they'd hit a "tried to fail an already failed
9609
+ // HTLC" debug panic. This tests for this behavior, checking that only one HTLC is auto-failed.
9610
+ let chanmon_cfgs = create_chanmon_cfgs(2);
9611
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9612
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9613
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9614
+
9615
+ let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9616
+
9617
+ let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id())
9618
+ .with_features(InvoiceFeatures::known());
9619
+ let scorer = test_utils::TestScorer::with_penalty(0);
9620
+ let route = get_route(
9621
+ &nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph,
9622
+ Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
9623
+ 10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer).unwrap();
9624
+
9625
+ let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
9626
+
9627
+ {
9628
+ nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
9629
+ check_added_monitors!(nodes[0], 1);
9630
+ let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9631
+ assert_eq!(events.len(), 1);
9632
+ let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9633
+ nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9634
+ commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9635
+ }
9636
+ expect_pending_htlcs_forwardable!(nodes[1]);
9637
+ expect_payment_received!(nodes[1], our_payment_hash, our_payment_secret, 10_000);
9638
+
9639
+ {
9640
+ nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
9641
+ check_added_monitors!(nodes[0], 1);
9642
+ let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9643
+ assert_eq!(events.len(), 1);
9644
+ let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9645
+ nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9646
+ commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9647
+ // At this point, nodes[1] would notice it has too much value for the payment. It will
9648
+ // assume the second is a privacy attack (no longer particularly relevant
9649
+ // post-payment_secrets) and fail back the new HTLC. Previously, it'd also have failed back
9650
+ // the first HTLC delivered above.
9651
+ }
9652
+
9653
+ // Now we go fail back the first HTLC from the user end.
9654
+ expect_pending_htlcs_forwardable_ignore!(nodes[1]);
9655
+ nodes[1].node.process_pending_htlc_forwards();
9656
+ nodes[1].node.fail_htlc_backwards(&our_payment_hash);
9657
+
9658
+ expect_pending_htlcs_forwardable_ignore!(nodes[1]);
9659
+ nodes[1].node.process_pending_htlc_forwards();
9660
+
9661
+ check_added_monitors!(nodes[1], 1);
9662
+ let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9663
+ assert_eq!(fail_updates_1.update_fail_htlcs.len(), 2);
9664
+
9665
+ nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9666
+ nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[1]);
9667
+ commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9668
+
9669
+ let failure_events = nodes[0].node.get_and_clear_pending_events();
9670
+ assert_eq!(failure_events.len(), 2);
9671
+ if let Event::PaymentPathFailed { .. } = failure_events[0] {} else { panic!(); }
9672
+ if let Event::PaymentPathFailed { .. } = failure_events[1] {} else { panic!(); }
9673
+ }
9674
+
9604
9675
#[test]
9605
9676
fn test_keysend_payments_to_public_node() {
9606
9677
let chanmon_cfgs = create_chanmon_cfgs(2);
0 commit comments