+
+#[test]
+fn onchain_failed_probe_yields_event() {
+ // Tests that an attempt to probe over a channel that is eventaully closed results in a failure
+ // event.
+ let chanmon_cfgs = create_chanmon_cfgs(3);
+ let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
+ let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
+
+ let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
+ create_announced_chan_between_nodes(&nodes, 1, 2, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+
+ let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id());
+
+ // Send a dust HTLC, which will be treated as if it timed out once the channel hits the chain.
+ let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 1_000, 42);
+ let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
+
+ // node[0] -- update_add_htlcs -> node[1]
+ check_added_monitors!(nodes[0], 1);
+ let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+ let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
+ nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
+ check_added_monitors!(nodes[1], 0);
+ commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
+ expect_pending_htlcs_forwardable!(nodes[1]);
+
+ check_added_monitors!(nodes[1], 1);
+ let _ = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+
+ // Don't bother forwarding the HTLC onwards and just confirm the force-close transaction on
+ // Node A, which after 6 confirmations should result in a probe failure event.
+ let bs_txn = get_local_commitment_txn!(nodes[1], chan_id);
+ confirm_transaction(&nodes[0], &bs_txn[0]);
+ check_closed_broadcast!(&nodes[0], true);
+ check_added_monitors!(nodes[0], 1);
+
+ let mut events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 2);
+ let mut found_probe_failed = false;
+ for event in events.drain(..) {
+ match event {
+ Event::ProbeFailed { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
+ assert_eq!(payment_id, ev_pid);
+ assert_eq!(payment_hash, ev_ph);
+ found_probe_failed = true;
+ },
+ Event::ChannelClosed { .. } => {},
+ _ => panic!(),
+ }
+ }
+ assert!(found_probe_failed);
+}
+
+#[test]
+fn claimed_send_payment_idempotent() {
+ // Tests that `send_payment` (and friends) are (reasonably) idempotent.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
+
+ let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
+ let (first_payment_preimage, _, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
+
+ macro_rules! check_send_rejected {
+ () => {
+ // If we try to resend a new payment with a different payment_hash but with the same
+ // payment_id, it should be rejected.
+ let send_result = nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id);
+ match send_result {
+ Err(PaymentSendFailure::DuplicatePayment) => {},
+ _ => panic!("Unexpected send result: {:?}", send_result),
+ }
+
+ // Further, if we try to send a spontaneous payment with the same payment_id it should
+ // also be rejected.
+ let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
+ match send_result {
+ Err(PaymentSendFailure::DuplicatePayment) => {},
+ _ => panic!("Unexpected send result: {:?}", send_result),
+ }
+ }
+ }
+
+ check_send_rejected!();
+
+ // Claim the payment backwards, but note that the PaymentSent event is still pending and has
+ // not been seen by the user. At this point, from the user perspective nothing has changed, so
+ // we must remain just as idempotent as we were before.
+ do_claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, first_payment_preimage);
+
+ for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
+ nodes[0].node.timer_tick_occurred();
+ }
+
+ check_send_rejected!();
+
+ // Once the user sees and handles the `PaymentSent` event, we expect them to no longer call
+ // `send_payment`, and our idempotency guarantees are off - they should have atomically marked
+ // the payment complete. However, they could have called `send_payment` while the event was
+ // being processed, leading to a race in our idempotency guarantees. Thus, even immediately
+ // after the event is handled a duplicate payment should sitll be rejected.
+ expect_payment_sent!(&nodes[0], first_payment_preimage, Some(0));
+ check_send_rejected!();
+
+ // If relatively little time has passed, a duplicate payment should still fail.
+ nodes[0].node.timer_tick_occurred();
+ check_send_rejected!();
+
+ // However, after some time has passed (at least more than the one timer tick above), a
+ // duplicate payment should go through, as ChannelManager should no longer have any remaining
+ // references to the old payment data.
+ for _ in 0..IDEMPOTENCY_TIMEOUT_TICKS {
+ nodes[0].node.timer_tick_occurred();
+ }
+
+ nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
+ claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
+}
+
+#[test]
+fn abandoned_send_payment_idempotent() {
+ // Tests that `send_payment` (and friends) allow duplicate PaymentIds immediately after
+ // abandon_payment.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
+
+ let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
+ let (_, first_payment_hash, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
+
+ macro_rules! check_send_rejected {
+ () => {
+ // If we try to resend a new payment with a different payment_hash but with the same
+ // payment_id, it should be rejected.
+ let send_result = nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id);
+ match send_result {
+ Err(PaymentSendFailure::DuplicatePayment) => {},
+ _ => panic!("Unexpected send result: {:?}", send_result),
+ }
+
+ // Further, if we try to send a spontaneous payment with the same payment_id it should
+ // also be rejected.
+ let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
+ match send_result {
+ Err(PaymentSendFailure::DuplicatePayment) => {},
+ _ => panic!("Unexpected send result: {:?}", send_result),
+ }
+ }
+ }
+
+ check_send_rejected!();
+
+ nodes[1].node.fail_htlc_backwards(&first_payment_hash);
+ expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
+
+ pass_failed_payment_back_no_abandon(&nodes[0], &[&[&nodes[1]]], false, first_payment_hash);
+ check_send_rejected!();
+
+ // Until we abandon the payment, no matter how many timer ticks pass, we still cannot reuse the
+ // PaymentId.
+ for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
+ nodes[0].node.timer_tick_occurred();
+ }
+ check_send_rejected!();
+
+ nodes[0].node.abandon_payment(payment_id);
+ get_event!(nodes[0], Event::PaymentFailed);
+
+ // However, we can reuse the PaymentId immediately after we `abandon_payment`.
+ nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
+ claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
+}