@@ -9603,6 +9603,57 @@ fn test_keysend_payments_to_private_node() {
9603
9603
claim_payment(&nodes[0], &path, test_preimage);
9604
9604
}
9605
9605
9606
+ #[test]
9607
+ fn test_double_partial_claim() {
9608
+ // Test what happens if a node receives a payment, generates a PaymentReceived event, some of
9609
+ // the HTLCs time out, the sender resends only some of the MPP parts, then the user processes
9610
+ // the PaymentReceived event, ensuring they don't inadvertently claim only part of the full
9611
+ // payment amount.
9612
+ let chanmon_cfgs = create_chanmon_cfgs(4);
9613
+ let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9614
+ let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9615
+ let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9616
+
9617
+ create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, InitFeatures::known(), InitFeatures::known());
9618
+ create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0, InitFeatures::known(), InitFeatures::known());
9619
+ create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0, InitFeatures::known(), InitFeatures::known());
9620
+ create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0, InitFeatures::known(), InitFeatures::known());
9621
+
9622
+ let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
9623
+ assert_eq!(route.paths.len(), 2);
9624
+ route.paths.sort_by(|path_a, _| {
9625
+ // Sort the path so that the path through nodes[1] comes first
9626
+ if path_a[0].pubkey == nodes[1].node.get_our_node_id() {
9627
+ core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9628
+ });
9629
+
9630
+ send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 15_000_000, payment_hash, payment_secret);
9631
+ // nodes[3] has now received a PaymentReceived event...which it will take some (exorbitant)
9632
+ // amount of time to respond to.
9633
+
9634
+ // Connect some blocks to time out the payment
9635
+ connect_blocks(&nodes[3], TEST_FINAL_CLTV);
9636
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // To get the same height for sending later
9637
+
9638
+ expect_pending_htlcs_forwardable!(nodes[3]);
9639
+
9640
+ pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
9641
+
9642
+ // nodes[1] now retries one of the two paths...
9643
+ nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
9644
+ check_added_monitors!(nodes[0], 2);
9645
+
9646
+ let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9647
+ assert_eq!(events.len(), 2);
9648
+ pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
9649
+
9650
+ // At this point nodes[3] has received one half of the payment, and the user goes to handle
9651
+ // that PaymentReceived event they got hours ago and never handled...we should refuse to claim.
9652
+ nodes[3].node.claim_funds(payment_preimage);
9653
+ check_added_monitors!(nodes[3], 0);
9654
+ assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
9655
+ }
9656
+
9606
9657
fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
9607
9658
// Test what happens if a node receives an MPP payment, claims it, but crashes before
9608
9659
// persisting the ChannelManager. If `persist_both_monitors` is false, also crash after only
0 commit comments