On initial send retries, avoid previously failed scids
[rust-lightning] / lightning / src / ln / payment_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Tests that test the payment retry logic in ChannelManager, including various edge-cases around
11 //! serialization ordering between ChannelManager/ChannelMonitors and ensuring we can still retry
12 //! payments thereafter.
13
14 use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
15 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS};
16 use crate::chain::keysinterface::EntropySource;
17 use crate::chain::transaction::OutPoint;
18 use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
19 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails};
20 use crate::ln::features::InvoiceFeatures;
21 use crate::ln::msgs;
22 use crate::ln::msgs::ChannelMessageHandler;
23 use crate::ln::outbound_payment::Retry;
24 use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
25 use crate::routing::router::{get_route, PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RouteParameters};
26 use crate::routing::scoring::ChannelUsage;
27 use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
28 use crate::util::test_utils;
29 use crate::util::errors::APIError;
30 use crate::util::ser::Writeable;
31
32 use bitcoin::{Block, BlockHeader, TxMerkleNode};
33 use bitcoin::hashes::Hash;
34 use bitcoin::network::constants::Network;
35
36 use crate::prelude::*;
37
38 use crate::ln::functional_test_utils::*;
39 use crate::routing::gossip::NodeId;
40 #[cfg(feature = "std")]
41 use {
42         crate::util::time::tests::SinceEpoch,
43         std::time::{SystemTime, Instant, Duration}
44 };
45
46 #[test]
47 fn mpp_failure() {
48         let chanmon_cfgs = create_chanmon_cfgs(4);
49         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
50         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
51         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
52
53         let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
54         let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
55         let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
56         let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
57
58         let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
59         let path = route.paths[0].clone();
60         route.paths.push(path);
61         route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
62         route.paths[0][0].short_channel_id = chan_1_id;
63         route.paths[0][1].short_channel_id = chan_3_id;
64         route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
65         route.paths[1][0].short_channel_id = chan_2_id;
66         route.paths[1][1].short_channel_id = chan_4_id;
67         send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
68         fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
69 }
70
71 #[test]
72 fn mpp_retry() {
73         let chanmon_cfgs = create_chanmon_cfgs(4);
74         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
75         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
76         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
77
78         let (chan_1_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
79         let (chan_2_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 2);
80         let (chan_3_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 1, 3);
81         let (chan_4_update, _, chan_4_id, _) = create_announced_chan_between_nodes(&nodes, 3, 2);
82         // Rebalance
83         send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
84
85         let amt_msat = 1_000_000;
86         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], amt_msat);
87         let path = route.paths[0].clone();
88         route.paths.push(path);
89         route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
90         route.paths[0][0].short_channel_id = chan_1_update.contents.short_channel_id;
91         route.paths[0][1].short_channel_id = chan_3_update.contents.short_channel_id;
92         route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
93         route.paths[1][0].short_channel_id = chan_2_update.contents.short_channel_id;
94         route.paths[1][1].short_channel_id = chan_4_update.contents.short_channel_id;
95
96         // Initiate the MPP payment.
97         let payment_id = PaymentId(payment_hash.0);
98         let mut route_params = RouteParameters {
99                 payment_params: route.payment_params.clone().unwrap(),
100                 final_value_msat: amt_msat,
101                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
102         };
103
104         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
105         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), payment_id, route_params.clone(), Retry::Attempts(1)).unwrap();
106         check_added_monitors!(nodes[0], 2); // one monitor per path
107         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
108         assert_eq!(events.len(), 2);
109
110         // Pass half of the payment along the success path.
111         let success_path_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
112         pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
113
114         // Add the HTLC along the first hop.
115         let fail_path_msgs_1 = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
116         let (update_add, commitment_signed) = match fail_path_msgs_1 {
117                 MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
118                         assert_eq!(update_add_htlcs.len(), 1);
119                         assert!(update_fail_htlcs.is_empty());
120                         assert!(update_fulfill_htlcs.is_empty());
121                         assert!(update_fail_malformed_htlcs.is_empty());
122                         assert!(update_fee.is_none());
123                         (update_add_htlcs[0].clone(), commitment_signed.clone())
124                 },
125                 _ => panic!("Unexpected event"),
126         };
127         nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
128         commitment_signed_dance!(nodes[2], nodes[0], commitment_signed, false);
129
130         // Attempt to forward the payment and complete the 2nd path's failure.
131         expect_pending_htlcs_forwardable!(&nodes[2]);
132         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[2], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_4_id }]);
133         let htlc_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
134         assert!(htlc_updates.update_add_htlcs.is_empty());
135         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
136         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
137         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
138         check_added_monitors!(nodes[2], 1);
139         nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
140         commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
141         let mut events = nodes[0].node.get_and_clear_pending_events();
142         match events[1] {
143                 Event::PendingHTLCsForwardable { .. } => {},
144                 _ => panic!("Unexpected event")
145         }
146         events.remove(1);
147         expect_payment_failed_conditions_event(events, payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
148
149         // Rebalance the channel so the second half of the payment can succeed.
150         send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
151
152         // Retry the second half of the payment and make sure it succeeds.
153         route.paths.remove(0);
154         route_params.final_value_msat = 1_000_000;
155         route_params.payment_params.previously_failed_channels.push(chan_4_update.contents.short_channel_id);
156         nodes[0].router.expect_find_route(route_params, Ok(route));
157         nodes[0].node.process_pending_htlc_forwards();
158         check_added_monitors!(nodes[0], 1);
159         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
160         assert_eq!(events.len(), 1);
161         pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
162         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
163 }
164
165 fn do_mpp_receive_timeout(send_partial_mpp: bool) {
166         let chanmon_cfgs = create_chanmon_cfgs(4);
167         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
168         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
169         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
170
171         let (chan_1_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
172         let (chan_2_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 2);
173         let (chan_3_update, _, chan_3_id, _) = create_announced_chan_between_nodes(&nodes, 1, 3);
174         let (chan_4_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 2, 3);
175
176         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 100_000);
177         let path = route.paths[0].clone();
178         route.paths.push(path);
179         route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
180         route.paths[0][0].short_channel_id = chan_1_update.contents.short_channel_id;
181         route.paths[0][1].short_channel_id = chan_3_update.contents.short_channel_id;
182         route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
183         route.paths[1][0].short_channel_id = chan_2_update.contents.short_channel_id;
184         route.paths[1][1].short_channel_id = chan_4_update.contents.short_channel_id;
185
186         // Initiate the MPP payment.
187         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
188         check_added_monitors!(nodes[0], 2); // one monitor per path
189         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
190         assert_eq!(events.len(), 2);
191
192         // Pass half of the payment along the first path.
193         let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
194         pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
195
196         if send_partial_mpp {
197                 // Time out the partial MPP
198                 for _ in 0..MPP_TIMEOUT_TICKS {
199                         nodes[3].node.timer_tick_occurred();
200                 }
201
202                 // Failed HTLC from node 3 -> 1
203                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash }]);
204                 let htlc_fail_updates_3_1 = get_htlc_update_msgs!(nodes[3], nodes[1].node.get_our_node_id());
205                 assert_eq!(htlc_fail_updates_3_1.update_fail_htlcs.len(), 1);
206                 nodes[1].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &htlc_fail_updates_3_1.update_fail_htlcs[0]);
207                 check_added_monitors!(nodes[3], 1);
208                 commitment_signed_dance!(nodes[1], nodes[3], htlc_fail_updates_3_1.commitment_signed, false);
209
210                 // Failed HTLC from node 1 -> 0
211                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_3_id }]);
212                 let htlc_fail_updates_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
213                 assert_eq!(htlc_fail_updates_1_0.update_fail_htlcs.len(), 1);
214                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates_1_0.update_fail_htlcs[0]);
215                 check_added_monitors!(nodes[1], 1);
216                 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates_1_0.commitment_signed, false);
217
218                 expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain().expected_htlc_error_data(23, &[][..]));
219         } else {
220                 // Pass half of the payment along the second path.
221                 let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
222                 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_2_msgs, true, None);
223
224                 // Even after MPP_TIMEOUT_TICKS we should not timeout the MPP if we have all the parts
225                 for _ in 0..MPP_TIMEOUT_TICKS {
226                         nodes[3].node.timer_tick_occurred();
227                 }
228
229                 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
230         }
231 }
232
233 #[test]
234 fn mpp_receive_timeout() {
235         do_mpp_receive_timeout(true);
236         do_mpp_receive_timeout(false);
237 }
238
239 #[test]
240 fn no_pending_leak_on_initial_send_failure() {
241         // In an earlier version of our payment tracking, we'd have a retry entry even when the initial
242         // HTLC for payment failed to send due to local channel errors (e.g. peer disconnected). In this
243         // case, the user wouldn't have a PaymentId to retry the payment with, but we'd think we have a
244         // pending payment forever and never time it out.
245         // Here we test exactly that - retrying a payment when a peer was disconnected on the first
246         // try, and then check that no pending payment is being tracked.
247         let chanmon_cfgs = create_chanmon_cfgs(2);
248         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
249         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
250         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
251
252         create_announced_chan_between_nodes(&nodes, 0, 1);
253
254         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
255
256         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
257         nodes[1].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
258
259         unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)),
260                 true, APIError::ChannelUnavailable { ref err },
261                 assert_eq!(err, "Peer for first hop currently disconnected/pending monitor update!"));
262
263         assert!(!nodes[0].node.has_pending_payments());
264 }
265
266 fn do_retry_with_no_persist(confirm_before_reload: bool) {
267         // If we send a pending payment and `send_payment` returns success, we should always either
268         // return a payment failure event or a payment success event, and on failure the payment should
269         // be retryable.
270         //
271         // In order to do so when the ChannelManager isn't immediately persisted (which is normal - its
272         // always persisted asynchronously), the ChannelManager has to reload some payment data from
273         // ChannelMonitor(s) in some cases. This tests that reloading.
274         //
275         // `confirm_before_reload` confirms the channel-closing commitment transaction on-chain prior
276         // to reloading the ChannelManager, increasing test coverage in ChannelMonitor HTLC tracking
277         // which has separate codepaths for "commitment transaction already confirmed" and not.
278         let chanmon_cfgs = create_chanmon_cfgs(3);
279         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
280         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
281         let persister: test_utils::TestPersister;
282         let new_chain_monitor: test_utils::TestChainMonitor;
283         let nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
284         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
285
286         let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
287         let (_, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
288
289         // Serialize the ChannelManager prior to sending payments
290         let nodes_0_serialized = nodes[0].node.encode();
291
292         // Send two payments - one which will get to nodes[2] and will be claimed, one which we'll time
293         // out and retry.
294         let amt_msat = 1_000_000;
295         let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
296         let (payment_preimage_1, payment_hash_1, _, payment_id_1) = send_along_route(&nodes[0], route.clone(), &[&nodes[1], &nodes[2]], 1_000_000);
297         let route_params = RouteParameters {
298                 payment_params: route.payment_params.clone().unwrap(),
299                 final_value_msat: amt_msat,
300                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
301         };
302         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
303         check_added_monitors!(nodes[0], 1);
304
305         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
306         assert_eq!(events.len(), 1);
307         let payment_event = SendEvent::from_event(events.pop().unwrap());
308         assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
309
310         // We relay the payment to nodes[1] while its disconnected from nodes[2], causing the payment
311         // to be returned immediately to nodes[0], without having nodes[2] fail the inbound payment
312         // which would prevent retry.
313         nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
314         nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
315
316         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
317         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
318         // nodes[1] now immediately fails the HTLC as the next-hop channel is disconnected
319         let _ = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
320
321         reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
322
323         let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan_id)[0].clone();
324         if confirm_before_reload {
325                 mine_transaction(&nodes[0], &as_commitment_tx);
326                 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
327         }
328
329         // The ChannelMonitor should always be the latest version, as we're required to persist it
330         // during the `commitment_signed_dance!()`.
331         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
332         reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
333
334         // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and
335         // force-close the channel.
336         check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager);
337         assert!(nodes[0].node.list_channels().is_empty());
338         assert!(nodes[0].node.has_pending_payments());
339         let as_broadcasted_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
340         assert_eq!(as_broadcasted_txn.len(), 1);
341         assert_eq!(as_broadcasted_txn[0], as_commitment_tx);
342
343         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
344         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }).unwrap();
345         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
346
347         // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
348         // error, as the channel has hit the chain.
349         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }).unwrap();
350         let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
351         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
352         let as_err = nodes[0].node.get_and_clear_pending_msg_events();
353         assert_eq!(as_err.len(), 1);
354         match as_err[0] {
355                 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
356                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
357                         nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), msg);
358                         check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id()) });
359                         check_added_monitors!(nodes[1], 1);
360                         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
361                 },
362                 _ => panic!("Unexpected event"),
363         }
364         check_closed_broadcast!(nodes[1], false);
365
366         // Now claim the first payment, which should allow nodes[1] to claim the payment on-chain when
367         // we close in a moment.
368         nodes[2].node.claim_funds(payment_preimage_1);
369         check_added_monitors!(nodes[2], 1);
370         expect_payment_claimed!(nodes[2], payment_hash_1, 1_000_000);
371
372         let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
373         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
374         check_added_monitors!(nodes[1], 1);
375         commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false);
376         expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, false, false);
377
378         if confirm_before_reload {
379                 let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone();
380                 nodes[0].node.best_block_updated(&best_block.0.header, best_block.1);
381         }
382
383         // Create a new channel on which to retry the payment before we fail the payment via the
384         // HTLC-Timeout transaction. This avoids ChannelManager timing out the payment due to us
385         // connecting several blocks while creating the channel (implying time has passed).
386         create_announced_chan_between_nodes(&nodes, 0, 1);
387         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
388
389         mine_transaction(&nodes[1], &as_commitment_tx);
390         let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
391         assert_eq!(bs_htlc_claim_txn.len(), 1);
392         check_spends!(bs_htlc_claim_txn[0], as_commitment_tx);
393
394         if !confirm_before_reload {
395                 mine_transaction(&nodes[0], &as_commitment_tx);
396         }
397         mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
398         expect_payment_sent!(nodes[0], payment_preimage_1);
399         connect_blocks(&nodes[0], TEST_FINAL_CLTV*4 + 20);
400         let as_htlc_timeout_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
401         assert_eq!(as_htlc_timeout_txn.len(), 2);
402         let (first_htlc_timeout_tx, second_htlc_timeout_tx) = (&as_htlc_timeout_txn[0], &as_htlc_timeout_txn[1]);
403         check_spends!(first_htlc_timeout_tx, as_commitment_tx);
404         check_spends!(second_htlc_timeout_tx, as_commitment_tx);
405         if first_htlc_timeout_tx.input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
406                 confirm_transaction(&nodes[0], &second_htlc_timeout_tx);
407         } else {
408                 confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
409         }
410         nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
411         expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new());
412
413         // Finally, retry the payment (which was reloaded from the ChannelMonitor when nodes[0] was
414         // reloaded) via a route over the new channel, which work without issue and eventually be
415         // received and claimed at the recipient just like any other payment.
416         let (mut new_route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
417
418         // Update the fee on the middle hop to ensure PaymentSent events have the correct (retried) fee
419         // and not the original fee. We also update node[1]'s relevant config as
420         // do_claim_payment_along_route expects us to never overpay.
421         {
422                 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
423                 let mut peer_state = per_peer_state.get(&nodes[2].node.get_our_node_id())
424                         .unwrap().lock().unwrap();
425                 let mut channel = peer_state.channel_by_id.get_mut(&chan_id_2).unwrap();
426                 let mut new_config = channel.config();
427                 new_config.forwarding_fee_base_msat += 100_000;
428                 channel.update_config(&new_config);
429                 new_route.paths[0][0].fee_msat += 100_000;
430         }
431
432         // Force expiration of the channel's previous config.
433         for _ in 0..EXPIRE_PREV_CONFIG_TICKS {
434                 nodes[1].node.timer_tick_occurred();
435         }
436
437         assert!(nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id_1).is_err()); // Shouldn't be allowed to retry a fulfilled payment
438         nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
439         check_added_monitors!(nodes[0], 1);
440         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
441         assert_eq!(events.len(), 1);
442         pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
443         do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
444         expect_payment_sent!(nodes[0], payment_preimage, Some(new_route.paths[0][0].fee_msat));
445 }
446
447 #[test]
448 fn retry_with_no_persist() {
449         do_retry_with_no_persist(true);
450         do_retry_with_no_persist(false);
451 }
452
453 fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) {
454         // Test that an off-chain completed payment is not retryable on restart. This was previously
455         // broken for dust payments, but we test for both dust and non-dust payments.
456         //
457         // `use_dust` switches to using a dust HTLC, which results in the HTLC not having an on-chain
458         // output at all.
459         let chanmon_cfgs = create_chanmon_cfgs(3);
460         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
461
462         let mut manually_accept_config = test_default_channel_config();
463         manually_accept_config.manually_accept_inbound_channels = true;
464
465         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(manually_accept_config), None]);
466
467         let first_persister: test_utils::TestPersister;
468         let first_new_chain_monitor: test_utils::TestChainMonitor;
469         let first_nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
470         let second_persister: test_utils::TestPersister;
471         let second_new_chain_monitor: test_utils::TestChainMonitor;
472         let second_nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
473         let third_persister: test_utils::TestPersister;
474         let third_new_chain_monitor: test_utils::TestChainMonitor;
475         let third_nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
476
477         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
478
479         // Because we set nodes[1] to manually accept channels, just open a 0-conf channel.
480         let (funding_tx, chan_id) = open_zero_conf_channel(&nodes[0], &nodes[1], None);
481         confirm_transaction(&nodes[0], &funding_tx);
482         confirm_transaction(&nodes[1], &funding_tx);
483         // Ignore the announcement_signatures messages
484         nodes[0].node.get_and_clear_pending_msg_events();
485         nodes[1].node.get_and_clear_pending_msg_events();
486         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
487
488         // Serialize the ChannelManager prior to sending payments
489         let mut nodes_0_serialized = nodes[0].node.encode();
490
491         let route = get_route_and_payment_hash!(nodes[0], nodes[2], if use_dust { 1_000 } else { 1_000_000 }).0;
492         let (payment_preimage, payment_hash, payment_secret, payment_id) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], if use_dust { 1_000 } else { 1_000_000 });
493
494         // The ChannelMonitor should always be the latest version, as we're required to persist it
495         // during the `commitment_signed_dance!()`.
496         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
497
498         reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized], first_persister, first_new_chain_monitor, first_nodes_0_deserialized);
499         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
500
501         // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and
502         // force-close the channel.
503         check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager);
504         assert!(nodes[0].node.list_channels().is_empty());
505         assert!(nodes[0].node.has_pending_payments());
506         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
507
508         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: nodes[1].node.init_features(), remote_network_address: None }).unwrap();
509         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
510
511         // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
512         // error, as the channel has hit the chain.
513         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: nodes[0].node.init_features(), remote_network_address: None }).unwrap();
514         let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
515         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
516         let as_err = nodes[0].node.get_and_clear_pending_msg_events();
517         assert_eq!(as_err.len(), 1);
518         let bs_commitment_tx;
519         match as_err[0] {
520                 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
521                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
522                         nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), msg);
523                         check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id()) });
524                         check_added_monitors!(nodes[1], 1);
525                         bs_commitment_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
526                 },
527                 _ => panic!("Unexpected event"),
528         }
529         check_closed_broadcast!(nodes[1], false);
530
531         // Now fail back the payment from nodes[2] to nodes[1]. This doesn't really matter as the
532         // previous hop channel is already on-chain, but it makes nodes[2] willing to see additional
533         // incoming HTLCs with the same payment hash later.
534         nodes[2].node.fail_htlc_backwards(&payment_hash);
535         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], [HTLCDestination::FailedPayment { payment_hash }]);
536         check_added_monitors!(nodes[2], 1);
537
538         let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
539         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fail_htlcs[0]);
540         commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false);
541         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
542                 [HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
543
544         // Connect the HTLC-Timeout transaction, timing out the HTLC on both nodes (but not confirming
545         // the HTLC-Timeout transaction beyond 1 conf). For dust HTLCs, the HTLC is considered resolved
546         // after the commitment transaction, so always connect the commitment transaction.
547         mine_transaction(&nodes[0], &bs_commitment_tx[0]);
548         mine_transaction(&nodes[1], &bs_commitment_tx[0]);
549         if !use_dust {
550                 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1 + (MIN_CLTV_EXPIRY_DELTA as u32));
551                 connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1 + (MIN_CLTV_EXPIRY_DELTA as u32));
552                 let as_htlc_timeout = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
553                 check_spends!(as_htlc_timeout[0], bs_commitment_tx[0]);
554                 assert_eq!(as_htlc_timeout.len(), 1);
555
556                 mine_transaction(&nodes[0], &as_htlc_timeout[0]);
557                 // nodes[0] may rebroadcast (or RBF-bump) its HTLC-Timeout, so wipe the announced set.
558                 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
559                 mine_transaction(&nodes[1], &as_htlc_timeout[0]);
560         }
561
562         // Create a new channel on which to retry the payment before we fail the payment via the
563         // HTLC-Timeout transaction. This avoids ChannelManager timing out the payment due to us
564         // connecting several blocks while creating the channel (implying time has passed).
565         // We do this with a zero-conf channel to avoid connecting blocks as a side-effect.
566         let (_, chan_id_3) = open_zero_conf_channel(&nodes[0], &nodes[1], None);
567         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
568
569         // If we attempt to retry prior to the HTLC-Timeout (or commitment transaction, for dust HTLCs)
570         // confirming, we will fail as it's considered still-pending...
571         let (new_route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], if use_dust { 1_000 } else { 1_000_000 });
572         match nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id) {
573                 Err(PaymentSendFailure::DuplicatePayment) => {},
574                 _ => panic!("Unexpected error")
575         }
576         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
577
578         // After ANTI_REORG_DELAY confirmations, the HTLC should be failed and we can try the payment
579         // again. We serialize the node first as we'll then test retrying the HTLC after a restart
580         // (which should also still work).
581         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
582         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
583         expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new());
584
585         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
586         let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode();
587         nodes_0_serialized = nodes[0].node.encode();
588
589         // After the payment failed, we're free to send it again.
590         assert!(nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id).is_ok());
591         assert!(!nodes[0].node.get_and_clear_pending_msg_events().is_empty());
592
593         reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], second_persister, second_new_chain_monitor, second_nodes_0_deserialized);
594         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
595
596         reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
597
598         // Now resend the payment, delivering the HTLC and actually claiming it this time. This ensures
599         // the payment is not (spuriously) listed as still pending.
600         assert!(nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id).is_ok());
601         check_added_monitors!(nodes[0], 1);
602         pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], if use_dust { 1_000 } else { 1_000_000 }, payment_hash, payment_secret);
603         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
604
605         match nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id) {
606                 Err(PaymentSendFailure::DuplicatePayment) => {},
607                 _ => panic!("Unexpected error")
608         }
609         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
610
611         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
612         let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode();
613         nodes_0_serialized = nodes[0].node.encode();
614
615         // Check that after reload we can send the payment again (though we shouldn't, since it was
616         // claimed previously).
617         reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], third_persister, third_new_chain_monitor, third_nodes_0_deserialized);
618         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
619
620         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
621
622         match nodes[0].node.send_payment(&new_route, payment_hash, &Some(payment_secret), payment_id) {
623                 Err(PaymentSendFailure::DuplicatePayment) => {},
624                 _ => panic!("Unexpected error")
625         }
626         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
627 }
628
629 #[test]
630 fn test_completed_payment_not_retryable_on_reload() {
631         do_test_completed_payment_not_retryable_on_reload(true);
632         do_test_completed_payment_not_retryable_on_reload(false);
633 }
634
635
636 fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, confirm_commitment_tx: bool, payment_timeout: bool) {
637         // When a Channel is closed, any outbound HTLCs which were relayed through it are simply
638         // dropped when the Channel is. From there, the ChannelManager relies on the ChannelMonitor
639         // having a copy of the relevant fail-/claim-back data and processes the HTLC fail/claim when
640         // the ChannelMonitor tells it to.
641         //
642         // If, due to an on-chain event, an HTLC is failed/claimed, we should avoid providing the
643         // ChannelManager the HTLC event until after the monitor is re-persisted. This should prevent a
644         // duplicate HTLC fail/claim (e.g. via a PaymentPathFailed event).
645         let chanmon_cfgs = create_chanmon_cfgs(2);
646         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
647         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
648         let persister: test_utils::TestPersister;
649         let new_chain_monitor: test_utils::TestChainMonitor;
650         let nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
651         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
652
653         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
654
655         // Route a payment, but force-close the channel before the HTLC fulfill message arrives at
656         // nodes[0].
657         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 10_000_000);
658         nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
659         check_closed_broadcast!(nodes[0], true);
660         check_added_monitors!(nodes[0], 1);
661         check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
662
663         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
664         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
665
666         // Connect blocks until the CLTV timeout is up so that we get an HTLC-Timeout transaction
667         connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
668         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
669         assert_eq!(node_txn.len(), 3);
670         assert_eq!(node_txn[0], node_txn[1]);
671         check_spends!(node_txn[1], funding_tx);
672         check_spends!(node_txn[2], node_txn[1]);
673         let timeout_txn = vec![node_txn[2].clone()];
674
675         nodes[1].node.claim_funds(payment_preimage);
676         check_added_monitors!(nodes[1], 1);
677         expect_payment_claimed!(nodes[1], payment_hash, 10_000_000);
678
679         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
680         connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[1].clone()]});
681         check_closed_broadcast!(nodes[1], true);
682         check_added_monitors!(nodes[1], 1);
683         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
684         let claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
685         assert_eq!(claim_txn.len(), 1);
686         check_spends!(claim_txn[0], node_txn[1]);
687
688         header.prev_blockhash = nodes[0].best_block_hash();
689         connect_block(&nodes[0], &Block { header, txdata: vec![node_txn[1].clone()]});
690
691         if confirm_commitment_tx {
692                 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
693         }
694
695         header.prev_blockhash = nodes[0].best_block_hash();
696         let claim_block = Block { header, txdata: if payment_timeout { timeout_txn } else { vec![claim_txn[0].clone()] } };
697
698         if payment_timeout {
699                 assert!(confirm_commitment_tx); // Otherwise we're spending below our CSV!
700                 connect_block(&nodes[0], &claim_block);
701                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
702         }
703
704         // Now connect the HTLC claim transaction with the ChainMonitor-generated ChannelMonitor update
705         // returning InProgress. This should cause the claim event to never make its way to the
706         // ChannelManager.
707         chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap().clear();
708         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
709
710         if payment_timeout {
711                 connect_blocks(&nodes[0], 1);
712         } else {
713                 connect_block(&nodes[0], &claim_block);
714         }
715
716         let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
717         let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
718                 .get_mut(&funding_txo).unwrap().drain().collect();
719         // If we are using chain::Confirm instead of chain::Listen, we will get the same update twice.
720         // If we're testing connection idempotency we may get substantially more.
721         assert!(mon_updates.len() >= 1);
722         assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
723         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
724
725         // If we persist the ChannelManager here, we should get the PaymentSent event after
726         // deserialization.
727         let mut chan_manager_serialized = Vec::new();
728         if !persist_manager_post_event {
729                 chan_manager_serialized = nodes[0].node.encode();
730         }
731
732         // Now persist the ChannelMonitor and inform the ChainMonitor that we're done, generating the
733         // payment sent event.
734         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
735         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
736         for update in mon_updates {
737                 nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap();
738         }
739         if payment_timeout {
740                 expect_payment_failed!(nodes[0], payment_hash, false);
741         } else {
742                 expect_payment_sent!(nodes[0], payment_preimage);
743         }
744
745         // If we persist the ChannelManager after we get the PaymentSent event, we shouldn't get it
746         // twice.
747         if persist_manager_post_event {
748                 chan_manager_serialized = nodes[0].node.encode();
749         }
750
751         // Now reload nodes[0]...
752         reload_node!(nodes[0], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
753
754         if persist_manager_post_event {
755                 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
756         } else if payment_timeout {
757                 expect_payment_failed!(nodes[0], payment_hash, false);
758         } else {
759                 expect_payment_sent!(nodes[0], payment_preimage);
760         }
761
762         // Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
763         // which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
764         // payment events should kick in, leaving us with no pending events here.
765         let height = nodes[0].blocks.lock().unwrap().len() as u32 - 1;
766         nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, height);
767         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
768 }
769
770 #[test]
771 fn test_dup_htlc_onchain_fails_on_reload() {
772         do_test_dup_htlc_onchain_fails_on_reload(true, true, true);
773         do_test_dup_htlc_onchain_fails_on_reload(true, true, false);
774         do_test_dup_htlc_onchain_fails_on_reload(true, false, false);
775         do_test_dup_htlc_onchain_fails_on_reload(false, true, true);
776         do_test_dup_htlc_onchain_fails_on_reload(false, true, false);
777         do_test_dup_htlc_onchain_fails_on_reload(false, false, false);
778 }
779
780 #[test]
781 fn test_fulfill_restart_failure() {
782         // When we receive an update_fulfill_htlc message, we immediately consider the HTLC fully
783         // fulfilled. At this point, the peer can reconnect and decide to either fulfill the HTLC
784         // again, or fail it, giving us free money.
785         //
786         // Of course probably they won't fail it and give us free money, but because we have code to
787         // handle it, we should test the logic for it anyway. We do that here.
788         let chanmon_cfgs = create_chanmon_cfgs(2);
789         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
790         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
791         let persister: test_utils::TestPersister;
792         let new_chain_monitor: test_utils::TestChainMonitor;
793         let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
794         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
795
796         let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
797         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
798
799         // The simplest way to get a failure after a fulfill is to reload nodes[1] from a state
800         // pre-fulfill, which we do by serializing it here.
801         let chan_manager_serialized = nodes[1].node.encode();
802         let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id).encode();
803
804         nodes[1].node.claim_funds(payment_preimage);
805         check_added_monitors!(nodes[1], 1);
806         expect_payment_claimed!(nodes[1], payment_hash, 100_000);
807
808         let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
809         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
810         expect_payment_sent_without_paths!(nodes[0], payment_preimage);
811
812         // Now reload nodes[1]...
813         reload_node!(nodes[1], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
814
815         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
816         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
817
818         nodes[1].node.fail_htlc_backwards(&payment_hash);
819         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
820         check_added_monitors!(nodes[1], 1);
821         let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
822         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
823         commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, false);
824         // nodes[0] shouldn't generate any events here, while it just got a payment failure completion
825         // it had already considered the payment fulfilled, and now they just got free money.
826         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
827 }
828
829 #[test]
830 fn get_ldk_payment_preimage() {
831         // Ensure that `ChannelManager::get_payment_preimage` can successfully be used to claim a payment.
832         let chanmon_cfgs = create_chanmon_cfgs(2);
833         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
834         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
835         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
836         create_announced_chan_between_nodes(&nodes, 0, 1);
837
838         let amt_msat = 60_000;
839         let expiry_secs = 60 * 60;
840         let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(amt_msat), expiry_secs, None).unwrap();
841
842         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
843                 .with_features(nodes[1].node.invoice_features());
844         let scorer = test_utils::TestScorer::new();
845         let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
846         let random_seed_bytes = keys_manager.get_secure_random_bytes();
847         let route = get_route(
848                 &nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(),
849                 Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
850                 amt_msat, TEST_FINAL_CLTV, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
851         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
852         check_added_monitors!(nodes[0], 1);
853
854         // Make sure to use `get_payment_preimage`
855         let payment_preimage = nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
856         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
857         assert_eq!(events.len(), 1);
858         pass_along_path(&nodes[0], &[&nodes[1]], amt_msat, payment_hash, Some(payment_secret), events.pop().unwrap(), true, Some(payment_preimage));
859         claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, payment_preimage);
860 }
861
862 #[test]
863 fn sent_probe_is_probe_of_sending_node() {
864         let chanmon_cfgs = create_chanmon_cfgs(3);
865         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
866         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
867         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
868
869         create_announced_chan_between_nodes(&nodes, 0, 1);
870         create_announced_chan_between_nodes(&nodes, 1, 2);
871
872         // First check we refuse to build a single-hop probe
873         let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
874         assert!(nodes[0].node.send_probe(route.paths[0].clone()).is_err());
875
876         // Then build an actual two-hop probing path
877         let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
878
879         match nodes[0].node.send_probe(route.paths[0].clone()) {
880                 Ok((payment_hash, payment_id)) => {
881                         assert!(nodes[0].node.payment_is_probe(&payment_hash, &payment_id));
882                         assert!(!nodes[1].node.payment_is_probe(&payment_hash, &payment_id));
883                         assert!(!nodes[2].node.payment_is_probe(&payment_hash, &payment_id));
884                 },
885                 _ => panic!(),
886         }
887
888         get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
889         check_added_monitors!(nodes[0], 1);
890 }
891
892 #[test]
893 fn successful_probe_yields_event() {
894         let chanmon_cfgs = create_chanmon_cfgs(3);
895         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
896         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
897         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
898
899         create_announced_chan_between_nodes(&nodes, 0, 1);
900         create_announced_chan_between_nodes(&nodes, 1, 2);
901
902         let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
903
904         let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
905
906         // node[0] -- update_add_htlcs -> node[1]
907         check_added_monitors!(nodes[0], 1);
908         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
909         let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
910         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
911         check_added_monitors!(nodes[1], 0);
912         commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
913         expect_pending_htlcs_forwardable!(nodes[1]);
914
915         // node[1] -- update_add_htlcs -> node[2]
916         check_added_monitors!(nodes[1], 1);
917         let updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
918         let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
919         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &probe_event.msgs[0]);
920         check_added_monitors!(nodes[2], 0);
921         commitment_signed_dance!(nodes[2], nodes[1], probe_event.commitment_msg, true, true);
922
923         // node[1] <- update_fail_htlcs -- node[2]
924         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
925         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
926         check_added_monitors!(nodes[1], 0);
927         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, true);
928
929         // node[0] <- update_fail_htlcs -- node[1]
930         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
931         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
932         check_added_monitors!(nodes[0], 0);
933         commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
934
935         let mut events = nodes[0].node.get_and_clear_pending_events();
936         assert_eq!(events.len(), 1);
937         match events.drain(..).next().unwrap() {
938                 crate::util::events::Event::ProbeSuccessful { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
939                         assert_eq!(payment_id, ev_pid);
940                         assert_eq!(payment_hash, ev_ph);
941                 },
942                 _ => panic!(),
943         };
944         assert!(!nodes[0].node.has_pending_payments());
945 }
946
947 #[test]
948 fn failed_probe_yields_event() {
949         let chanmon_cfgs = create_chanmon_cfgs(3);
950         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
951         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
952         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
953
954         create_announced_chan_between_nodes(&nodes, 0, 1);
955         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 90000000);
956
957         let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
958
959         let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 9_998_000, 42);
960
961         let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
962
963         // node[0] -- update_add_htlcs -> node[1]
964         check_added_monitors!(nodes[0], 1);
965         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
966         let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
967         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
968         check_added_monitors!(nodes[1], 0);
969         commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
970         expect_pending_htlcs_forwardable!(nodes[1]);
971
972         // node[0] <- update_fail_htlcs -- node[1]
973         check_added_monitors!(nodes[1], 1);
974         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
975         // Skip the PendingHTLCsForwardable event
976         let _events = nodes[1].node.get_and_clear_pending_events();
977         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
978         check_added_monitors!(nodes[0], 0);
979         commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
980
981         let mut events = nodes[0].node.get_and_clear_pending_events();
982         assert_eq!(events.len(), 1);
983         match events.drain(..).next().unwrap() {
984                 crate::util::events::Event::ProbeFailed { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
985                         assert_eq!(payment_id, ev_pid);
986                         assert_eq!(payment_hash, ev_ph);
987                 },
988                 _ => panic!(),
989         };
990         assert!(!nodes[0].node.has_pending_payments());
991 }
992
993 #[test]
994 fn onchain_failed_probe_yields_event() {
995         // Tests that an attempt to probe over a channel that is eventaully closed results in a failure
996         // event.
997         let chanmon_cfgs = create_chanmon_cfgs(3);
998         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
999         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1000         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1001
1002         let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1003         create_announced_chan_between_nodes(&nodes, 1, 2);
1004
1005         let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
1006
1007         // Send a dust HTLC, which will be treated as if it timed out once the channel hits the chain.
1008         let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 1_000, 42);
1009         let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
1010
1011         // node[0] -- update_add_htlcs -> node[1]
1012         check_added_monitors!(nodes[0], 1);
1013         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1014         let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
1015         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
1016         check_added_monitors!(nodes[1], 0);
1017         commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
1018         expect_pending_htlcs_forwardable!(nodes[1]);
1019
1020         check_added_monitors!(nodes[1], 1);
1021         let _ = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1022
1023         // Don't bother forwarding the HTLC onwards and just confirm the force-close transaction on
1024         // Node A, which after 6 confirmations should result in a probe failure event.
1025         let bs_txn = get_local_commitment_txn!(nodes[1], chan_id);
1026         confirm_transaction(&nodes[0], &bs_txn[0]);
1027         check_closed_broadcast!(&nodes[0], true);
1028         check_added_monitors!(nodes[0], 1);
1029
1030         let mut events = nodes[0].node.get_and_clear_pending_events();
1031         assert_eq!(events.len(), 2);
1032         let mut found_probe_failed = false;
1033         for event in events.drain(..) {
1034                 match event {
1035                         Event::ProbeFailed { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
1036                                 assert_eq!(payment_id, ev_pid);
1037                                 assert_eq!(payment_hash, ev_ph);
1038                                 found_probe_failed = true;
1039                         },
1040                         Event::ChannelClosed { .. } => {},
1041                         _ => panic!(),
1042                 }
1043         }
1044         assert!(found_probe_failed);
1045         assert!(!nodes[0].node.has_pending_payments());
1046 }
1047
1048 #[test]
1049 fn claimed_send_payment_idempotent() {
1050         // Tests that `send_payment` (and friends) are (reasonably) idempotent.
1051         let chanmon_cfgs = create_chanmon_cfgs(2);
1052         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1053         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1054         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1055
1056         create_announced_chan_between_nodes(&nodes, 0, 1).2;
1057
1058         let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
1059         let (first_payment_preimage, _, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
1060
1061         macro_rules! check_send_rejected {
1062                 () => {
1063                         // If we try to resend a new payment with a different payment_hash but with the same
1064                         // payment_id, it should be rejected.
1065                         let send_result = nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id);
1066                         match send_result {
1067                                 Err(PaymentSendFailure::DuplicatePayment) => {},
1068                                 _ => panic!("Unexpected send result: {:?}", send_result),
1069                         }
1070
1071                         // Further, if we try to send a spontaneous payment with the same payment_id it should
1072                         // also be rejected.
1073                         let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
1074                         match send_result {
1075                                 Err(PaymentSendFailure::DuplicatePayment) => {},
1076                                 _ => panic!("Unexpected send result: {:?}", send_result),
1077                         }
1078                 }
1079         }
1080
1081         check_send_rejected!();
1082
1083         // Claim the payment backwards, but note that the PaymentSent event is still pending and has
1084         // not been seen by the user. At this point, from the user perspective nothing has changed, so
1085         // we must remain just as idempotent as we were before.
1086         do_claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, first_payment_preimage);
1087
1088         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1089                 nodes[0].node.timer_tick_occurred();
1090         }
1091
1092         check_send_rejected!();
1093
1094         // Once the user sees and handles the `PaymentSent` event, we expect them to no longer call
1095         // `send_payment`, and our idempotency guarantees are off - they should have atomically marked
1096         // the payment complete. However, they could have called `send_payment` while the event was
1097         // being processed, leading to a race in our idempotency guarantees. Thus, even immediately
1098         // after the event is handled a duplicate payment should sitll be rejected.
1099         expect_payment_sent!(&nodes[0], first_payment_preimage, Some(0));
1100         check_send_rejected!();
1101
1102         // If relatively little time has passed, a duplicate payment should still fail.
1103         nodes[0].node.timer_tick_occurred();
1104         check_send_rejected!();
1105
1106         // However, after some time has passed (at least more than the one timer tick above), a
1107         // duplicate payment should go through, as ChannelManager should no longer have any remaining
1108         // references to the old payment data.
1109         for _ in 0..IDEMPOTENCY_TIMEOUT_TICKS {
1110                 nodes[0].node.timer_tick_occurred();
1111         }
1112
1113         nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id).unwrap();
1114         check_added_monitors!(nodes[0], 1);
1115         pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
1116         claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
1117 }
1118
1119 #[test]
1120 fn abandoned_send_payment_idempotent() {
1121         // Tests that `send_payment` (and friends) allow duplicate PaymentIds immediately after
1122         // abandon_payment.
1123         let chanmon_cfgs = create_chanmon_cfgs(2);
1124         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1125         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1126         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1127
1128         create_announced_chan_between_nodes(&nodes, 0, 1).2;
1129
1130         let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
1131         let (_, first_payment_hash, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
1132
1133         macro_rules! check_send_rejected {
1134                 () => {
1135                         // If we try to resend a new payment with a different payment_hash but with the same
1136                         // payment_id, it should be rejected.
1137                         let send_result = nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id);
1138                         match send_result {
1139                                 Err(PaymentSendFailure::DuplicatePayment) => {},
1140                                 _ => panic!("Unexpected send result: {:?}", send_result),
1141                         }
1142
1143                         // Further, if we try to send a spontaneous payment with the same payment_id it should
1144                         // also be rejected.
1145                         let send_result = nodes[0].node.send_spontaneous_payment(&route, None, payment_id);
1146                         match send_result {
1147                                 Err(PaymentSendFailure::DuplicatePayment) => {},
1148                                 _ => panic!("Unexpected send result: {:?}", send_result),
1149                         }
1150                 }
1151         }
1152
1153         check_send_rejected!();
1154
1155         nodes[1].node.fail_htlc_backwards(&first_payment_hash);
1156         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
1157
1158         // Until we abandon the payment upon path failure, no matter how many timer ticks pass, we still cannot reuse the
1159         // PaymentId.
1160         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1161                 nodes[0].node.timer_tick_occurred();
1162         }
1163         check_send_rejected!();
1164
1165         pass_failed_payment_back(&nodes[0], &[&[&nodes[1]]], false, first_payment_hash);
1166
1167         // However, we can reuse the PaymentId immediately after we `abandon_payment` upon passing the
1168         // failed payment back.
1169         nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), payment_id).unwrap();
1170         check_added_monitors!(nodes[0], 1);
1171         pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
1172         claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
1173 }
1174
1175 #[derive(PartialEq)]
1176 enum InterceptTest {
1177         Forward,
1178         Fail,
1179         Timeout,
1180 }
1181
1182 #[test]
1183 fn test_trivial_inflight_htlc_tracking(){
1184         // In this test, we test three scenarios:
1185         // (1) Sending + claiming a payment successfully should return `None` when querying InFlightHtlcs
1186         // (2) Sending a payment without claiming it should return the payment's value (500000) when querying InFlightHtlcs
1187         // (3) After we claim the payment sent in (2), InFlightHtlcs should return `None` for the query.
1188         let chanmon_cfgs = create_chanmon_cfgs(3);
1189         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1190         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1191         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1192
1193         let (_, _, chan_1_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
1194         let (_, _, chan_2_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
1195
1196         // Send and claim the payment. Inflight HTLCs should be empty.
1197         let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 500000);
1198         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
1199         check_added_monitors!(nodes[0], 1);
1200         pass_along_route(&nodes[0], &[&vec!(&nodes[1], &nodes[2])[..]], 500000, payment_hash, payment_secret);
1201         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], payment_preimage);
1202         {
1203                 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1204
1205                 let mut node_0_per_peer_lock;
1206                 let mut node_0_peer_state_lock;
1207                 let mut node_1_per_peer_lock;
1208                 let mut node_1_peer_state_lock;
1209                 let channel_1 =  get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1210                 let channel_2 =  get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1211
1212                 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1213                         &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1214                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1215                         channel_1.get_short_channel_id().unwrap()
1216                 );
1217                 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1218                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1219                         &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1220                         channel_2.get_short_channel_id().unwrap()
1221                 );
1222
1223                 assert_eq!(chan_1_used_liquidity, None);
1224                 assert_eq!(chan_2_used_liquidity, None);
1225         }
1226         let pending_payments = nodes[0].node.list_recent_payments();
1227         assert_eq!(pending_payments.len(), 1);
1228         assert_eq!(pending_payments[0], RecentPaymentDetails::Fulfilled { payment_hash: Some(payment_hash) });
1229
1230         // Remove fulfilled payment
1231         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1232                 nodes[0].node.timer_tick_occurred();
1233         }
1234
1235         // Send the payment, but do not claim it. Our inflight HTLCs should contain the pending payment.
1236         let (payment_preimage, payment_hash,  _) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 500000);
1237         {
1238                 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1239
1240                 let mut node_0_per_peer_lock;
1241                 let mut node_0_peer_state_lock;
1242                 let mut node_1_per_peer_lock;
1243                 let mut node_1_peer_state_lock;
1244                 let channel_1 =  get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1245                 let channel_2 =  get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1246
1247                 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1248                         &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1249                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1250                         channel_1.get_short_channel_id().unwrap()
1251                 );
1252                 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1253                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1254                         &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1255                         channel_2.get_short_channel_id().unwrap()
1256                 );
1257
1258                 // First hop accounts for expected 1000 msat fee
1259                 assert_eq!(chan_1_used_liquidity, Some(501000));
1260                 assert_eq!(chan_2_used_liquidity, Some(500000));
1261         }
1262         let pending_payments = nodes[0].node.list_recent_payments();
1263         assert_eq!(pending_payments.len(), 1);
1264         assert_eq!(pending_payments[0], RecentPaymentDetails::Pending { payment_hash, total_msat: 500000 });
1265
1266         // Now, let's claim the payment. This should result in the used liquidity to return `None`.
1267         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
1268
1269         // Remove fulfilled payment
1270         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1271                 nodes[0].node.timer_tick_occurred();
1272         }
1273
1274         {
1275                 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1276
1277                 let mut node_0_per_peer_lock;
1278                 let mut node_0_peer_state_lock;
1279                 let mut node_1_per_peer_lock;
1280                 let mut node_1_peer_state_lock;
1281                 let channel_1 =  get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1282                 let channel_2 =  get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1283
1284                 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1285                         &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1286                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1287                         channel_1.get_short_channel_id().unwrap()
1288                 );
1289                 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1290                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1291                         &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1292                         channel_2.get_short_channel_id().unwrap()
1293                 );
1294
1295                 assert_eq!(chan_1_used_liquidity, None);
1296                 assert_eq!(chan_2_used_liquidity, None);
1297         }
1298
1299         let pending_payments = nodes[0].node.list_recent_payments();
1300         assert_eq!(pending_payments.len(), 0);
1301 }
1302
1303 #[test]
1304 fn test_holding_cell_inflight_htlcs() {
1305         let chanmon_cfgs = create_chanmon_cfgs(2);
1306         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1307         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1308         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1309         let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1310
1311         let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1312         let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
1313
1314         // Queue up two payments - one will be delivered right away, one immediately goes into the
1315         // holding cell as nodes[0] is AwaitingRAA.
1316         {
1317                 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
1318                 check_added_monitors!(nodes[0], 1);
1319                 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1320                 check_added_monitors!(nodes[0], 0);
1321         }
1322
1323         let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1324
1325         {
1326                 let mut node_0_per_peer_lock;
1327                 let mut node_0_peer_state_lock;
1328                 let channel =  get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id);
1329
1330                 let used_liquidity = inflight_htlcs.used_liquidity_msat(
1331                         &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1332                         &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1333                         channel.get_short_channel_id().unwrap()
1334                 );
1335
1336                 assert_eq!(used_liquidity, Some(2000000));
1337         }
1338
1339         // Clear pending events so test doesn't throw a "Had excess message on node..." error
1340         nodes[0].node.get_and_clear_pending_msg_events();
1341 }
1342
1343 #[test]
1344 fn intercepted_payment() {
1345         // Test that detecting an intercept scid on payment forward will signal LDK to generate an
1346         // intercept event, which the LSP can then use to either (a) open a JIT channel to forward the
1347         // payment or (b) fail the payment.
1348         do_test_intercepted_payment(InterceptTest::Forward);
1349         do_test_intercepted_payment(InterceptTest::Fail);
1350         // Make sure that intercepted payments will be automatically failed back if too many blocks pass.
1351         do_test_intercepted_payment(InterceptTest::Timeout);
1352 }
1353
1354 fn do_test_intercepted_payment(test: InterceptTest) {
1355         let chanmon_cfgs = create_chanmon_cfgs(3);
1356         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1357
1358         let mut zero_conf_chan_config = test_default_channel_config();
1359         zero_conf_chan_config.manually_accept_inbound_channels = true;
1360         let mut intercept_forwards_config = test_default_channel_config();
1361         intercept_forwards_config.accept_intercept_htlcs = true;
1362         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), Some(zero_conf_chan_config)]);
1363
1364         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1365         let scorer = test_utils::TestScorer::new();
1366         let random_seed_bytes = chanmon_cfgs[0].keys_manager.get_secure_random_bytes();
1367
1368         let _ = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1369
1370         let amt_msat = 100_000;
1371         let intercept_scid = nodes[1].node.get_intercept_scid();
1372         let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1373                 .with_route_hints(vec![
1374                         RouteHint(vec![RouteHintHop {
1375                                 src_node_id: nodes[1].node.get_our_node_id(),
1376                                 short_channel_id: intercept_scid,
1377                                 fees: RoutingFees {
1378                                         base_msat: 1000,
1379                                         proportional_millionths: 0,
1380                                 },
1381                                 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
1382                                 htlc_minimum_msat: None,
1383                                 htlc_maximum_msat: None,
1384                         }])
1385                 ])
1386                 .with_features(nodes[2].node.invoice_features());
1387         let route_params = RouteParameters {
1388                 payment_params,
1389                 final_value_msat: amt_msat,
1390                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
1391         };
1392         let route = get_route(
1393                 &nodes[0].node.get_our_node_id(), &route_params.payment_params,
1394                 &nodes[0].network_graph.read_only(), None, route_params.final_value_msat,
1395                 route_params.final_cltv_expiry_delta, nodes[0].logger, &scorer, &random_seed_bytes
1396         ).unwrap();
1397
1398         let (payment_hash, payment_secret) = nodes[2].node.create_inbound_payment(Some(amt_msat), 60 * 60, None).unwrap();
1399         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
1400         let payment_event = {
1401                 {
1402                         let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
1403                         assert_eq!(added_monitors.len(), 1);
1404                         added_monitors.clear();
1405                 }
1406                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1407                 assert_eq!(events.len(), 1);
1408                 SendEvent::from_event(events.remove(0))
1409         };
1410         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1411         commitment_signed_dance!(nodes[1], nodes[0], &payment_event.commitment_msg, false, true);
1412
1413         // Check that we generate the PaymentIntercepted event when an intercept forward is detected.
1414         let events = nodes[1].node.get_and_clear_pending_events();
1415         assert_eq!(events.len(), 1);
1416         let (intercept_id, expected_outbound_amount_msat) = match events[0] {
1417                 crate::util::events::Event::HTLCIntercepted {
1418                         intercept_id, expected_outbound_amount_msat, payment_hash: pmt_hash, inbound_amount_msat, requested_next_hop_scid: short_channel_id
1419                 } => {
1420                         assert_eq!(pmt_hash, payment_hash);
1421                         assert_eq!(inbound_amount_msat, route.get_total_amount() + route.get_total_fees());
1422                         assert_eq!(short_channel_id, intercept_scid);
1423                         (intercept_id, expected_outbound_amount_msat)
1424                 },
1425                 _ => panic!()
1426         };
1427
1428         // Check for unknown channel id error.
1429         let unknown_chan_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &[42; 32], nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1430         assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable  { err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });
1431
1432         if test == InterceptTest::Fail {
1433                 // Ensure we can fail the intercepted payment back.
1434                 nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
1435                 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
1436                 nodes[1].node.process_pending_htlc_forwards();
1437                 let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1438                 check_added_monitors!(&nodes[1], 1);
1439                 assert!(update_fail.update_fail_htlcs.len() == 1);
1440                 let fail_msg = update_fail.update_fail_htlcs[0].clone();
1441                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
1442                 commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
1443
1444                 // Ensure the payment fails with the expected error.
1445                 let fail_conditions = PaymentFailedConditions::new()
1446                         .blamed_scid(intercept_scid)
1447                         .blamed_chan_closed(true)
1448                         .expected_htlc_error_data(0x4000 | 10, &[]);
1449                 expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
1450         } else if test == InterceptTest::Forward {
1451                 // Check that we'll fail as expected when sending to a channel that isn't in `ChannelReady` yet.
1452                 let temp_chan_id = nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
1453                 let unusable_chan_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &temp_chan_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1454                 assert_eq!(unusable_chan_err , APIError::ChannelUnavailable { err: format!("Channel with id {} not fully established", log_bytes!(temp_chan_id)) });
1455                 assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
1456
1457                 // Open the just-in-time channel so the payment can then be forwarded.
1458                 let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1459
1460                 // Finally, forward the intercepted payment through and claim it.
1461                 nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
1462                 expect_pending_htlcs_forwardable!(nodes[1]);
1463
1464                 let payment_event = {
1465                         {
1466                                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1467                                 assert_eq!(added_monitors.len(), 1);
1468                                 added_monitors.clear();
1469                         }
1470                         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1471                         assert_eq!(events.len(), 1);
1472                         SendEvent::from_event(events.remove(0))
1473                 };
1474                 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1475                 commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1476                 expect_pending_htlcs_forwardable!(nodes[2]);
1477
1478                 let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1479                 expect_payment_claimable!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage), nodes[2].node.get_our_node_id());
1480                 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1481                 let events = nodes[0].node.get_and_clear_pending_events();
1482                 assert_eq!(events.len(), 2);
1483                 match events[0] {
1484                         Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1485                                 assert_eq!(payment_preimage, *ev_preimage);
1486                                 assert_eq!(payment_hash, *ev_hash);
1487                                 assert_eq!(fee_paid_msat, &Some(1000));
1488                         },
1489                         _ => panic!("Unexpected event")
1490                 }
1491                 match events[1] {
1492                         Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1493                                 assert_eq!(hash, Some(payment_hash));
1494                         },
1495                         _ => panic!("Unexpected event")
1496                 }
1497         } else if test == InterceptTest::Timeout {
1498                 let mut block = Block {
1499                         header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 },
1500                         txdata: vec![],
1501                 };
1502                 connect_block(&nodes[0], &block);
1503                 connect_block(&nodes[1], &block);
1504                 for _ in 0..TEST_FINAL_CLTV {
1505                         block.header.prev_blockhash = block.block_hash();
1506                         connect_block(&nodes[0], &block);
1507                         connect_block(&nodes[1], &block);
1508                 }
1509                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::InvalidForward { requested_forward_scid: intercept_scid }]);
1510                 check_added_monitors!(nodes[1], 1);
1511                 let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1512                 assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
1513                 assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
1514                 assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
1515                 assert!(htlc_timeout_updates.update_fee.is_none());
1516
1517                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
1518                 commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
1519                 expect_payment_failed!(nodes[0], payment_hash, false, 0x2000 | 2, []);
1520
1521                 // Check for unknown intercept id error.
1522                 let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1523                 let unknown_intercept_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1524                 assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0)) });
1525                 let unknown_intercept_id_err = nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap_err();
1526                 assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0)) });
1527         }
1528 }
1529
1530 #[derive(PartialEq)]
1531 enum AutoRetry {
1532         Success,
1533         Spontaneous,
1534         FailAttempts,
1535         FailTimeout,
1536         FailOnRestart,
1537         FailOnRetry,
1538 }
1539
1540 #[test]
1541 fn automatic_retries() {
1542         do_automatic_retries(AutoRetry::Success);
1543         do_automatic_retries(AutoRetry::Spontaneous);
1544         do_automatic_retries(AutoRetry::FailAttempts);
1545         do_automatic_retries(AutoRetry::FailTimeout);
1546         do_automatic_retries(AutoRetry::FailOnRestart);
1547         do_automatic_retries(AutoRetry::FailOnRetry);
1548 }
1549 fn do_automatic_retries(test: AutoRetry) {
1550         // Test basic automatic payment retries in ChannelManager. See individual `test` variant comments
1551         // below.
1552         let chanmon_cfgs = create_chanmon_cfgs(3);
1553         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1554         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1555
1556         let persister;
1557         let new_chain_monitor;
1558         let node_0_deserialized;
1559
1560         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1561         let channel_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1562         let channel_id_2 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
1563
1564         // Marshall data to send the payment
1565         #[cfg(feature = "std")]
1566         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
1567         #[cfg(not(feature = "std"))]
1568         let payment_expiry_secs = 60 * 60;
1569         let amt_msat = 1000;
1570         let mut invoice_features = InvoiceFeatures::empty();
1571         invoice_features.set_variable_length_onion_required();
1572         invoice_features.set_payment_secret_required();
1573         invoice_features.set_basic_mpp_optional();
1574         let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1575                 .with_expiry_time(payment_expiry_secs as u64)
1576                 .with_features(invoice_features);
1577         let route_params = RouteParameters {
1578                 payment_params,
1579                 final_value_msat: amt_msat,
1580                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
1581         };
1582         let (_, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
1583
1584         macro_rules! pass_failed_attempt_with_retry_along_path {
1585                 ($failing_channel_id: expr, $expect_pending_htlcs_forwardable: expr) => {
1586                         // Send a payment attempt that fails due to lack of liquidity on the second hop
1587                         check_added_monitors!(nodes[0], 1);
1588                         let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1589                         let mut update_add = update_0.update_add_htlcs[0].clone();
1590                         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
1591                         commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
1592                         expect_pending_htlcs_forwardable_ignore!(nodes[1]);
1593                         nodes[1].node.process_pending_htlc_forwards();
1594                         expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1],
1595                                 vec![HTLCDestination::NextHopChannel {
1596                                         node_id: Some(nodes[2].node.get_our_node_id()),
1597                                         channel_id: $failing_channel_id,
1598                                 }]);
1599                         nodes[1].node.process_pending_htlc_forwards();
1600                         let update_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1601                         check_added_monitors!(&nodes[1], 1);
1602                         assert!(update_1.update_fail_htlcs.len() == 1);
1603                         let fail_msg = update_1.update_fail_htlcs[0].clone();
1604                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
1605                         commitment_signed_dance!(nodes[0], nodes[1], update_1.commitment_signed, false);
1606
1607                         // Ensure the attempt fails and a new PendingHTLCsForwardable event is generated for the retry
1608                         let mut events = nodes[0].node.get_and_clear_pending_events();
1609                         assert_eq!(events.len(), 2);
1610                         match events[0] {
1611                                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, ..  } => {
1612                                         assert_eq!(payment_hash, ev_payment_hash);
1613                                         assert_eq!(payment_failed_permanently, false);
1614                                 },
1615                                 _ => panic!("Unexpected event"),
1616                         }
1617                         if $expect_pending_htlcs_forwardable {
1618                                 match events[1] {
1619                                         Event::PendingHTLCsForwardable { .. } => {},
1620                                         _ => panic!("Unexpected event"),
1621                                 }
1622                         } else {
1623                                 match events[1] {
1624                                         Event::PaymentFailed { payment_hash: ev_payment_hash, .. } => {
1625                                                 assert_eq!(payment_hash, ev_payment_hash);
1626                                         },
1627                                         _ => panic!("Unexpected event"),
1628                                 }
1629                         }
1630                 }
1631         }
1632
1633         if test == AutoRetry::Success {
1634                 // Test that we can succeed on the first retry.
1635                 nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
1636                 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1637
1638                 // Open a new channel with liquidity on the second hop so we can find a route for the retry
1639                 // attempt, since the initial second hop channel will be excluded from pathfinding
1640                 create_announced_chan_between_nodes(&nodes, 1, 2);
1641
1642                 // We retry payments in `process_pending_htlc_forwards`
1643                 nodes[0].node.process_pending_htlc_forwards();
1644                 check_added_monitors!(nodes[0], 1);
1645                 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1646                 assert_eq!(msg_events.len(), 1);
1647                 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, Some(payment_secret), msg_events.pop().unwrap(), true, None);
1648                 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
1649         } else if test == AutoRetry::Spontaneous {
1650                 nodes[0].node.send_spontaneous_payment_with_retry(Some(payment_preimage), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
1651                 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1652
1653                 // Open a new channel with liquidity on the second hop so we can find a route for the retry
1654                 // attempt, since the initial second hop channel will be excluded from pathfinding
1655                 create_announced_chan_between_nodes(&nodes, 1, 2);
1656
1657                 // We retry payments in `process_pending_htlc_forwards`
1658                 nodes[0].node.process_pending_htlc_forwards();
1659                 check_added_monitors!(nodes[0], 1);
1660                 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1661                 assert_eq!(msg_events.len(), 1);
1662                 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, None, msg_events.pop().unwrap(), true, Some(payment_preimage));
1663                 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
1664         } else if test == AutoRetry::FailAttempts {
1665                 // Ensure ChannelManager will not retry a payment if it has run out of payment attempts.
1666                 nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
1667                 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1668
1669                 // Open a new channel with no liquidity on the second hop so we can find a (bad) route for
1670                 // the retry attempt, since the initial second hop channel will be excluded from pathfinding
1671                 let channel_id_3 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
1672
1673                 // We retry payments in `process_pending_htlc_forwards`
1674                 nodes[0].node.process_pending_htlc_forwards();
1675                 pass_failed_attempt_with_retry_along_path!(channel_id_3, false);
1676
1677                 // Ensure we won't retry a second time.
1678                 nodes[0].node.process_pending_htlc_forwards();
1679                 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1680                 assert_eq!(msg_events.len(), 0);
1681         } else if test == AutoRetry::FailTimeout {
1682                 #[cfg(not(feature = "no-std"))] {
1683                         // Ensure ChannelManager will not retry a payment if it times out due to Retry::Timeout.
1684                         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Timeout(Duration::from_secs(60))).unwrap();
1685                         pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1686
1687                         // Advance the time so the second attempt fails due to timeout.
1688                         SinceEpoch::advance(Duration::from_secs(61));
1689
1690                         // Make sure we don't retry again.
1691                         nodes[0].node.process_pending_htlc_forwards();
1692                         let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1693                         assert_eq!(msg_events.len(), 0);
1694
1695                         let mut events = nodes[0].node.get_and_clear_pending_events();
1696                         assert_eq!(events.len(), 1);
1697                         match events[0] {
1698                                 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id } => {
1699                                         assert_eq!(payment_hash, *ev_payment_hash);
1700                                         assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
1701                                 },
1702                                 _ => panic!("Unexpected event"),
1703                         }
1704                 }
1705         } else if test == AutoRetry::FailOnRestart {
1706                 // Ensure ChannelManager will not retry a payment after restart, even if there were retry
1707                 // attempts remaining prior to restart.
1708                 nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(2)).unwrap();
1709                 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1710
1711                 // Open a new channel with no liquidity on the second hop so we can find a (bad) route for
1712                 // the retry attempt, since the initial second hop channel will be excluded from pathfinding
1713                 let channel_id_3 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
1714
1715                 // Ensure the first retry attempt fails, with 1 retry attempt remaining
1716                 nodes[0].node.process_pending_htlc_forwards();
1717                 pass_failed_attempt_with_retry_along_path!(channel_id_3, true);
1718
1719                 // Restart the node and ensure that ChannelManager does not use its remaining retry attempt
1720                 let node_encoded = nodes[0].node.encode();
1721                 let chan_1_monitor_serialized = get_monitor!(nodes[0], channel_id_1).encode();
1722                 reload_node!(nodes[0], node_encoded, &[&chan_1_monitor_serialized], persister, new_chain_monitor, node_0_deserialized);
1723
1724                 let mut events = nodes[0].node.get_and_clear_pending_events();
1725                 expect_pending_htlcs_forwardable_from_events!(nodes[0], events, true);
1726                 // Make sure we don't retry again.
1727                 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1728                 assert_eq!(msg_events.len(), 0);
1729
1730                 let mut events = nodes[0].node.get_and_clear_pending_events();
1731                 assert_eq!(events.len(), 1);
1732                 match events[0] {
1733                         Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id } => {
1734                                 assert_eq!(payment_hash, *ev_payment_hash);
1735                                 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
1736                         },
1737                         _ => panic!("Unexpected event"),
1738                 }
1739         } else if test == AutoRetry::FailOnRetry {
1740                 nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
1741                 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
1742
1743                 // We retry payments in `process_pending_htlc_forwards`. Since our channel closed, we should
1744                 // fail to find a route.
1745                 nodes[0].node.process_pending_htlc_forwards();
1746                 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1747                 assert_eq!(msg_events.len(), 0);
1748
1749                 let mut events = nodes[0].node.get_and_clear_pending_events();
1750                 assert_eq!(events.len(), 1);
1751                 match events[0] {
1752                         Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id } => {
1753                                 assert_eq!(payment_hash, *ev_payment_hash);
1754                                 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
1755                         },
1756                         _ => panic!("Unexpected event"),
1757                 }
1758         }
1759 }
1760
1761 #[test]
1762 fn auto_retry_partial_failure() {
1763         // Test that we'll retry appropriately on send partial failure and retry partial failure.
1764         let chanmon_cfgs = create_chanmon_cfgs(2);
1765         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1766         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1767         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1768
1769         let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
1770         let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
1771         let chan_3_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
1772
1773         // Marshall data to send the payment
1774         let amt_msat = 20_000;
1775         let (_, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
1776         #[cfg(feature = "std")]
1777         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
1778         #[cfg(not(feature = "std"))]
1779         let payment_expiry_secs = 60 * 60;
1780         let mut invoice_features = InvoiceFeatures::empty();
1781         invoice_features.set_variable_length_onion_required();
1782         invoice_features.set_payment_secret_required();
1783         invoice_features.set_basic_mpp_optional();
1784         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
1785                 .with_expiry_time(payment_expiry_secs as u64)
1786                 .with_features(invoice_features);
1787         let route_params = RouteParameters {
1788                 payment_params,
1789                 final_value_msat: amt_msat,
1790                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
1791         };
1792
1793         // Ensure the first monitor update (for the initial send path1 over chan_1) succeeds, but the
1794         // second (for the initial send path2 over chan_2) fails.
1795         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
1796         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
1797         // Ensure third monitor update (for the retry1's path1 over chan_1) succeeds, but the fourth (for
1798         // the retry1's path2 over chan_3) fails, and monitor updates succeed after that.
1799         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
1800         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
1801         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
1802
1803         // Configure the initial send, retry1 and retry2's paths.
1804         let send_route = Route {
1805                 paths: vec![
1806                         vec![RouteHop {
1807                                 pubkey: nodes[1].node.get_our_node_id(),
1808                                 node_features: nodes[1].node.node_features(),
1809                                 short_channel_id: chan_1_id,
1810                                 channel_features: nodes[1].node.channel_features(),
1811                                 fee_msat: amt_msat / 2,
1812                                 cltv_expiry_delta: 100,
1813                         }],
1814                         vec![RouteHop {
1815                                 pubkey: nodes[1].node.get_our_node_id(),
1816                                 node_features: nodes[1].node.node_features(),
1817                                 short_channel_id: chan_2_id,
1818                                 channel_features: nodes[1].node.channel_features(),
1819                                 fee_msat: amt_msat / 2,
1820                                 cltv_expiry_delta: 100,
1821                         }],
1822                 ],
1823                 payment_params: Some(route_params.payment_params.clone()),
1824         };
1825         let retry_1_route = Route {
1826                 paths: vec![
1827                         vec![RouteHop {
1828                                 pubkey: nodes[1].node.get_our_node_id(),
1829                                 node_features: nodes[1].node.node_features(),
1830                                 short_channel_id: chan_1_id,
1831                                 channel_features: nodes[1].node.channel_features(),
1832                                 fee_msat: amt_msat / 4,
1833                                 cltv_expiry_delta: 100,
1834                         }],
1835                         vec![RouteHop {
1836                                 pubkey: nodes[1].node.get_our_node_id(),
1837                                 node_features: nodes[1].node.node_features(),
1838                                 short_channel_id: chan_3_id,
1839                                 channel_features: nodes[1].node.channel_features(),
1840                                 fee_msat: amt_msat / 4,
1841                                 cltv_expiry_delta: 100,
1842                         }],
1843                 ],
1844                 payment_params: Some(route_params.payment_params.clone()),
1845         };
1846         let retry_2_route = Route {
1847                 paths: vec![
1848                         vec![RouteHop {
1849                                 pubkey: nodes[1].node.get_our_node_id(),
1850                                 node_features: nodes[1].node.node_features(),
1851                                 short_channel_id: chan_1_id,
1852                                 channel_features: nodes[1].node.channel_features(),
1853                                 fee_msat: amt_msat / 4,
1854                                 cltv_expiry_delta: 100,
1855                         }],
1856                 ],
1857                 payment_params: Some(route_params.payment_params.clone()),
1858         };
1859         nodes[0].router.expect_find_route(route_params.clone(), Ok(send_route));
1860         let mut payment_params = route_params.payment_params.clone();
1861         payment_params.previously_failed_channels.push(chan_2_id);
1862         nodes[0].router.expect_find_route(RouteParameters {
1863                         payment_params, final_value_msat: amt_msat / 2, final_cltv_expiry_delta: TEST_FINAL_CLTV
1864                 }, Ok(retry_1_route));
1865         let mut payment_params = route_params.payment_params.clone();
1866         payment_params.previously_failed_channels.push(chan_3_id);
1867         nodes[0].router.expect_find_route(RouteParameters {
1868                         payment_params, final_value_msat: amt_msat / 4, final_cltv_expiry_delta: TEST_FINAL_CLTV
1869                 }, Ok(retry_2_route));
1870
1871         // Send a payment that will partially fail on send, then partially fail on retry, then succeed.
1872         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(3)).unwrap();
1873         let closed_chan_events = nodes[0].node.get_and_clear_pending_events();
1874         assert_eq!(closed_chan_events.len(), 4);
1875         match closed_chan_events[0] {
1876                 Event::ChannelClosed { .. } => {},
1877                 _ => panic!("Unexpected event"),
1878         }
1879         match closed_chan_events[1] {
1880                 Event::PaymentPathFailed { .. } => {},
1881                 _ => panic!("Unexpected event"),
1882         }
1883         match closed_chan_events[2] {
1884                 Event::ChannelClosed { .. } => {},
1885                 _ => panic!("Unexpected event"),
1886         }
1887         match closed_chan_events[3] {
1888                 Event::PaymentPathFailed { .. } => {},
1889                 _ => panic!("Unexpected event"),
1890         }
1891
1892         // Pass the first part of the payment along the path.
1893         check_added_monitors!(nodes[0], 5); // three outbound channel updates succeeded, two permanently failed
1894         let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
1895
1896         // First message is the first update_add, remaining messages are broadcasting channel updates and
1897         // errors for the permfailed channels
1898         assert_eq!(msg_events.len(), 5);
1899         let mut payment_event = SendEvent::from_event(msg_events.remove(0));
1900
1901         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1902         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1903         check_added_monitors!(nodes[1], 1);
1904         let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1905
1906         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
1907         check_added_monitors!(nodes[0], 1);
1908         let as_second_htlc_updates = SendEvent::from_node(&nodes[0]);
1909
1910         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
1911         check_added_monitors!(nodes[0], 1);
1912         let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1913
1914         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
1915         check_added_monitors!(nodes[1], 1);
1916
1917         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.msgs[0]);
1918         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.msgs[1]);
1919         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.commitment_msg);
1920         check_added_monitors!(nodes[1], 1);
1921         let (bs_second_raa, bs_second_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1922
1923         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
1924         check_added_monitors!(nodes[0], 1);
1925
1926         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_cs);
1927         check_added_monitors!(nodes[0], 1);
1928         let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1929
1930         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
1931         check_added_monitors!(nodes[1], 1);
1932
1933         expect_pending_htlcs_forwardable_ignore!(nodes[1]);
1934         nodes[1].node.process_pending_htlc_forwards();
1935         expect_payment_claimable!(nodes[1], payment_hash, payment_secret, amt_msat);
1936         nodes[1].node.claim_funds(payment_preimage);
1937         expect_payment_claimed!(nodes[1], payment_hash, amt_msat);
1938         let bs_claim_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1939         assert_eq!(bs_claim_update.update_fulfill_htlcs.len(), 1);
1940
1941         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_claim_update.update_fulfill_htlcs[0]);
1942         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_claim_update.commitment_signed);
1943         check_added_monitors!(nodes[0], 1);
1944         let (as_third_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1945
1946         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
1947         check_added_monitors!(nodes[1], 4);
1948         let bs_second_claim_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1949
1950         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
1951         check_added_monitors!(nodes[1], 1);
1952         let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1953
1954         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
1955         check_added_monitors!(nodes[0], 1);
1956
1957         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[0]);
1958         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[1]);
1959         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.commitment_signed);
1960         check_added_monitors!(nodes[0], 1);
1961         let (as_fourth_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1962
1963         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_fourth_raa);
1964         check_added_monitors!(nodes[1], 1);
1965
1966         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
1967         check_added_monitors!(nodes[1], 1);
1968         let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1969
1970         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
1971         check_added_monitors!(nodes[0], 1);
1972         expect_payment_sent!(nodes[0], payment_preimage);
1973 }
1974
1975 #[test]
1976 fn auto_retry_zero_attempts_send_error() {
1977         let chanmon_cfgs = create_chanmon_cfgs(2);
1978         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1979         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1980         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1981
1982         create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
1983         create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
1984
1985         // Marshall data to send the payment
1986         let amt_msat = 20_000;
1987         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
1988         #[cfg(feature = "std")]
1989         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
1990         #[cfg(not(feature = "std"))]
1991         let payment_expiry_secs = 60 * 60;
1992         let mut invoice_features = InvoiceFeatures::empty();
1993         invoice_features.set_variable_length_onion_required();
1994         invoice_features.set_payment_secret_required();
1995         invoice_features.set_basic_mpp_optional();
1996         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
1997                 .with_expiry_time(payment_expiry_secs as u64)
1998                 .with_features(invoice_features);
1999         let route_params = RouteParameters {
2000                 payment_params,
2001                 final_value_msat: amt_msat,
2002                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2003         };
2004
2005         chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
2006         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
2007         assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 2); // channel close messages
2008         let events = nodes[0].node.get_and_clear_pending_events();
2009         assert_eq!(events.len(), 3);
2010         if let Event::ChannelClosed { .. } = events[0] { } else { panic!(); }
2011         if let Event::PaymentPathFailed { .. } = events[1] { } else { panic!(); }
2012         if let Event::PaymentFailed { .. } = events[2] { } else { panic!(); }
2013         check_added_monitors!(nodes[0], 2);
2014 }
2015
2016 #[test]
2017 fn fails_paying_after_rejected_by_payee() {
2018         let chanmon_cfgs = create_chanmon_cfgs(2);
2019         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2020         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2021         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2022
2023         create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2024
2025         // Marshall data to send the payment
2026         let amt_msat = 20_000;
2027         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2028         #[cfg(feature = "std")]
2029         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2030         #[cfg(not(feature = "std"))]
2031         let payment_expiry_secs = 60 * 60;
2032         let mut invoice_features = InvoiceFeatures::empty();
2033         invoice_features.set_variable_length_onion_required();
2034         invoice_features.set_payment_secret_required();
2035         invoice_features.set_basic_mpp_optional();
2036         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2037                 .with_expiry_time(payment_expiry_secs as u64)
2038                 .with_features(invoice_features);
2039         let route_params = RouteParameters {
2040                 payment_params,
2041                 final_value_msat: amt_msat,
2042                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2043         };
2044
2045         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2046         check_added_monitors!(nodes[0], 1);
2047         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2048         assert_eq!(events.len(), 1);
2049         let mut payment_event = SendEvent::from_event(events.pop().unwrap());
2050         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
2051         check_added_monitors!(nodes[1], 0);
2052         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
2053         expect_pending_htlcs_forwardable!(nodes[1]);
2054         expect_payment_claimable!(&nodes[1], payment_hash, payment_secret, amt_msat);
2055
2056         nodes[1].node.fail_htlc_backwards(&payment_hash);
2057         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::FailedPayment { payment_hash }]);
2058         pass_failed_payment_back(&nodes[0], &[&[&nodes[1]]], false, payment_hash);
2059 }
2060
2061 #[test]
2062 fn retry_multi_path_single_failed_payment() {
2063         // Tests that we can/will retry after a single path of an MPP payment failed immediately
2064         let chanmon_cfgs = create_chanmon_cfgs(2);
2065         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2066         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
2067         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2068
2069         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2070         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2071
2072         let amt_msat = 100_010_000;
2073
2074         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2075         #[cfg(feature = "std")]
2076         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2077         #[cfg(not(feature = "std"))]
2078         let payment_expiry_secs = 60 * 60;
2079         let mut invoice_features = InvoiceFeatures::empty();
2080         invoice_features.set_variable_length_onion_required();
2081         invoice_features.set_payment_secret_required();
2082         invoice_features.set_basic_mpp_optional();
2083         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2084                 .with_expiry_time(payment_expiry_secs as u64)
2085                 .with_features(invoice_features);
2086         let route_params = RouteParameters {
2087                 payment_params: payment_params.clone(),
2088                 final_value_msat: amt_msat,
2089                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2090         };
2091
2092         let chans = nodes[0].node.list_usable_channels();
2093         let mut route = Route {
2094                 paths: vec![
2095                         vec![RouteHop {
2096                                 pubkey: nodes[1].node.get_our_node_id(),
2097                                 node_features: nodes[1].node.node_features(),
2098                                 short_channel_id: chans[0].short_channel_id.unwrap(),
2099                                 channel_features: nodes[1].node.channel_features(),
2100                                 fee_msat: 10_000,
2101                                 cltv_expiry_delta: 100,
2102                         }],
2103                         vec![RouteHop {
2104                                 pubkey: nodes[1].node.get_our_node_id(),
2105                                 node_features: nodes[1].node.node_features(),
2106                                 short_channel_id: chans[1].short_channel_id.unwrap(),
2107                                 channel_features: nodes[1].node.channel_features(),
2108                                 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
2109                                 cltv_expiry_delta: 100,
2110                         }],
2111                 ],
2112                 payment_params: Some(payment_params),
2113         };
2114         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2115         // On retry, split the payment across both channels.
2116         route.paths[0][0].fee_msat = 50_000_001;
2117         route.paths[1][0].fee_msat = 50_000_000;
2118         let mut pay_params = route.payment_params.clone().unwrap();
2119         pay_params.previously_failed_channels.push(chans[1].short_channel_id.unwrap());
2120         nodes[0].router.expect_find_route(RouteParameters {
2121                         payment_params: pay_params,
2122                         // Note that the second request here requests the amount we originally failed to send,
2123                         // not the amount remaining on the full payment, which should be changed.
2124                         final_value_msat: 100_000_001, final_cltv_expiry_delta: TEST_FINAL_CLTV
2125                 }, Ok(route.clone()));
2126
2127         {
2128                 let scorer = chanmon_cfgs[0].scorer.lock().unwrap();
2129                 // The initial send attempt, 2 paths
2130                 scorer.expect_usage(chans[0].short_channel_id.unwrap(), ChannelUsage { amount_msat: 10_000, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2131                 scorer.expect_usage(chans[1].short_channel_id.unwrap(), ChannelUsage { amount_msat: 100_000_001, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2132                 // The retry, 2 paths. Ensure that the in-flight HTLC amount is factored in.
2133                 scorer.expect_usage(chans[0].short_channel_id.unwrap(), ChannelUsage { amount_msat: 50_000_001, inflight_htlc_msat: 10_000, effective_capacity: EffectiveCapacity::Unknown });
2134                 scorer.expect_usage(chans[1].short_channel_id.unwrap(), ChannelUsage { amount_msat: 50_000_000, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2135         }
2136
2137         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2138         let events = nodes[0].node.get_and_clear_pending_events();
2139         assert_eq!(events.len(), 1);
2140         match events[0] {
2141                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
2142                         network_update: None, all_paths_failed: false, short_channel_id: Some(expected_scid), .. } => {
2143                         assert_eq!(payment_hash, ev_payment_hash);
2144                         assert_eq!(expected_scid, route.paths[1][0].short_channel_id);
2145                 },
2146                 _ => panic!("Unexpected event"),
2147         }
2148         let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
2149         assert_eq!(htlc_msgs.len(), 2);
2150         check_added_monitors!(nodes[0], 2);
2151 }
2152
2153 #[test]
2154 fn immediate_retry_on_failure() {
2155         // Tests that we can/will retry immediately after a failure
2156         let chanmon_cfgs = create_chanmon_cfgs(2);
2157         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2158         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
2159         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2160
2161         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2162         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2163
2164         let amt_msat = 100_000_001;
2165         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2166         #[cfg(feature = "std")]
2167         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2168         #[cfg(not(feature = "std"))]
2169         let payment_expiry_secs = 60 * 60;
2170         let mut invoice_features = InvoiceFeatures::empty();
2171         invoice_features.set_variable_length_onion_required();
2172         invoice_features.set_payment_secret_required();
2173         invoice_features.set_basic_mpp_optional();
2174         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2175                 .with_expiry_time(payment_expiry_secs as u64)
2176                 .with_features(invoice_features);
2177         let route_params = RouteParameters {
2178                 payment_params,
2179                 final_value_msat: amt_msat,
2180                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2181         };
2182
2183         let chans = nodes[0].node.list_usable_channels();
2184         let mut route = Route {
2185                 paths: vec![
2186                         vec![RouteHop {
2187                                 pubkey: nodes[1].node.get_our_node_id(),
2188                                 node_features: nodes[1].node.node_features(),
2189                                 short_channel_id: chans[0].short_channel_id.unwrap(),
2190                                 channel_features: nodes[1].node.channel_features(),
2191                                 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
2192                                 cltv_expiry_delta: 100,
2193                         }],
2194                 ],
2195                 payment_params: Some(PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)),
2196         };
2197         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2198         // On retry, split the payment across both channels.
2199         route.paths.push(route.paths[0].clone());
2200         route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
2201         route.paths[0][0].fee_msat = 50_000_000;
2202         route.paths[1][0].fee_msat = 50_000_001;
2203         let mut pay_params = route_params.payment_params.clone();
2204         pay_params.previously_failed_channels.push(chans[0].short_channel_id.unwrap());
2205         nodes[0].router.expect_find_route(RouteParameters {
2206                         payment_params: pay_params, final_value_msat: amt_msat,
2207                         final_cltv_expiry_delta: TEST_FINAL_CLTV
2208                 }, Ok(route.clone()));
2209
2210         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2211         let events = nodes[0].node.get_and_clear_pending_events();
2212         assert_eq!(events.len(), 1);
2213         match events[0] {
2214                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
2215                 network_update: None, all_paths_failed: false, short_channel_id: Some(expected_scid), .. } => {
2216                         assert_eq!(payment_hash, ev_payment_hash);
2217                         assert_eq!(expected_scid, route.paths[1][0].short_channel_id);
2218                 },
2219                 _ => panic!("Unexpected event"),
2220         }
2221         let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
2222         assert_eq!(htlc_msgs.len(), 2);
2223         check_added_monitors!(nodes[0], 2);
2224 }
2225
2226 #[test]
2227 fn no_extra_retries_on_back_to_back_fail() {
2228         // In a previous release, we had a race where we may exceed the payment retry count if we
2229         // get two failures in a row with the second having `all_paths_failed` set.
2230         // Generally, when we give up trying to retry a payment, we don't know for sure what the
2231         // current state of the ChannelManager event queue is. Specifically, we cannot be sure that
2232         // there are not multiple additional `PaymentPathFailed` or even `PaymentSent` events
2233         // pending which we will see later. Thus, when we previously removed the retry tracking map
2234         // entry after a `all_paths_failed` `PaymentPathFailed` event, we may have dropped the
2235         // retry entry even though more events for the same payment were still pending. This led to
2236         // us retrying a payment again even though we'd already given up on it.
2237         //
2238         // We now have a separate event - `PaymentFailed` which indicates no HTLCs remain and which
2239         // is used to remove the payment retry counter entries instead. This tests for the specific
2240         // excess-retry case while also testing `PaymentFailed` generation.
2241
2242         let chanmon_cfgs = create_chanmon_cfgs(3);
2243         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2244         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2245         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2246
2247         let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
2248         let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0).0.contents.short_channel_id;
2249
2250         let amt_msat = 200_000_000;
2251         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2252         #[cfg(feature = "std")]
2253         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2254         #[cfg(not(feature = "std"))]
2255         let payment_expiry_secs = 60 * 60;
2256         let mut invoice_features = InvoiceFeatures::empty();
2257         invoice_features.set_variable_length_onion_required();
2258         invoice_features.set_payment_secret_required();
2259         invoice_features.set_basic_mpp_optional();
2260         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2261                 .with_expiry_time(payment_expiry_secs as u64)
2262                 .with_features(invoice_features);
2263         let route_params = RouteParameters {
2264                 payment_params,
2265                 final_value_msat: amt_msat,
2266                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2267         };
2268
2269         let mut route = Route {
2270                 paths: vec![
2271                         vec![RouteHop {
2272                                 pubkey: nodes[1].node.get_our_node_id(),
2273                                 node_features: nodes[1].node.node_features(),
2274                                 short_channel_id: chan_1_scid,
2275                                 channel_features: nodes[1].node.channel_features(),
2276                                 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2277                                 cltv_expiry_delta: 100,
2278                         }, RouteHop {
2279                                 pubkey: nodes[2].node.get_our_node_id(),
2280                                 node_features: nodes[2].node.node_features(),
2281                                 short_channel_id: chan_2_scid,
2282                                 channel_features: nodes[2].node.channel_features(),
2283                                 fee_msat: 100_000_000,
2284                                 cltv_expiry_delta: 100,
2285                         }],
2286                         vec![RouteHop {
2287                                 pubkey: nodes[1].node.get_our_node_id(),
2288                                 node_features: nodes[1].node.node_features(),
2289                                 short_channel_id: chan_1_scid,
2290                                 channel_features: nodes[1].node.channel_features(),
2291                                 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2292                                 cltv_expiry_delta: 100,
2293                         }, RouteHop {
2294                                 pubkey: nodes[2].node.get_our_node_id(),
2295                                 node_features: nodes[2].node.node_features(),
2296                                 short_channel_id: chan_2_scid,
2297                                 channel_features: nodes[2].node.channel_features(),
2298                                 fee_msat: 100_000_000,
2299                                 cltv_expiry_delta: 100,
2300                         }]
2301                 ],
2302                 payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
2303         };
2304         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2305         let mut second_payment_params = route_params.payment_params.clone();
2306         second_payment_params.previously_failed_channels = vec![chan_2_scid, chan_2_scid];
2307         // On retry, we'll only return one path
2308         route.paths.remove(1);
2309         route.paths[0][1].fee_msat = amt_msat;
2310         nodes[0].router.expect_find_route(RouteParameters {
2311                         payment_params: second_payment_params,
2312                         final_value_msat: amt_msat, final_cltv_expiry_delta: TEST_FINAL_CLTV,
2313                 }, Ok(route.clone()));
2314
2315         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2316         let htlc_updates = SendEvent::from_node(&nodes[0]);
2317         check_added_monitors!(nodes[0], 1);
2318         assert_eq!(htlc_updates.msgs.len(), 1);
2319
2320         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
2321         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
2322         check_added_monitors!(nodes[1], 1);
2323         let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2324
2325         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2326         check_added_monitors!(nodes[0], 1);
2327         let second_htlc_updates = SendEvent::from_node(&nodes[0]);
2328
2329         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
2330         check_added_monitors!(nodes[0], 1);
2331         let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2332
2333         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
2334         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
2335         check_added_monitors!(nodes[1], 1);
2336         let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2337
2338         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
2339         check_added_monitors!(nodes[1], 1);
2340         let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2341
2342         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2343         check_added_monitors!(nodes[0], 1);
2344
2345         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
2346         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
2347         check_added_monitors!(nodes[0], 1);
2348         let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2349
2350         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
2351         check_added_monitors!(nodes[1], 1);
2352         let bs_second_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2353
2354         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
2355         check_added_monitors!(nodes[1], 1);
2356         let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2357
2358         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.update_fail_htlcs[0]);
2359         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.commitment_signed);
2360         check_added_monitors!(nodes[0], 1);
2361
2362         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
2363         check_added_monitors!(nodes[0], 1);
2364         let (as_third_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2365
2366         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
2367         check_added_monitors!(nodes[1], 1);
2368         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
2369         check_added_monitors!(nodes[1], 1);
2370         let bs_fourth_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2371
2372         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_fourth_raa);
2373         check_added_monitors!(nodes[0], 1);
2374
2375         // At this point A has sent two HTLCs which both failed due to lack of fee. It now has two
2376         // pending `PaymentPathFailed` events, one with `all_paths_failed` unset, and the second
2377         // with it set.
2378         //
2379         // Previously, we retried payments in an event consumer, which would retry each
2380         // `PaymentPathFailed` individually. In that setup, we had retried the payment in response to
2381         // the first `PaymentPathFailed`, then seen the second `PaymentPathFailed` with
2382         // `all_paths_failed` set and assumed the payment was completely failed. We ultimately fixed it
2383         // by adding the `PaymentFailed` event.
2384         //
2385         // Because we now retry payments as a batch, we simply return a single-path route in the
2386         // second, batched, request, have that fail, ensure the payment was abandoned.
2387         let mut events = nodes[0].node.get_and_clear_pending_events();
2388         assert_eq!(events.len(), 3);
2389         match events[0] {
2390                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, ..  } => {
2391                         assert_eq!(payment_hash, ev_payment_hash);
2392                         assert_eq!(payment_failed_permanently, false);
2393                 },
2394                 _ => panic!("Unexpected event"),
2395         }
2396         match events[1] {
2397                 Event::PendingHTLCsForwardable { .. } => {},
2398                 _ => panic!("Unexpected event"),
2399         }
2400         match events[2] {
2401                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, ..  } => {
2402                         assert_eq!(payment_hash, ev_payment_hash);
2403                         assert_eq!(payment_failed_permanently, false);
2404                 },
2405                 _ => panic!("Unexpected event"),
2406         }
2407
2408         nodes[0].node.process_pending_htlc_forwards();
2409         let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
2410         check_added_monitors!(nodes[0], 1);
2411
2412         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
2413         commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
2414         let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2415         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
2416         commitment_signed_dance!(nodes[0], nodes[1], &bs_fail_update.commitment_signed, false, true);
2417
2418         let mut events = nodes[0].node.get_and_clear_pending_events();
2419         assert_eq!(events.len(), 2);
2420         match events[0] {
2421                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, ..  } => {
2422                         assert_eq!(payment_hash, ev_payment_hash);
2423                         assert_eq!(payment_failed_permanently, false);
2424                 },
2425                 _ => panic!("Unexpected event"),
2426         }
2427         match events[1] {
2428                 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id } => {
2429                         assert_eq!(payment_hash, *ev_payment_hash);
2430                         assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
2431                 },
2432                 _ => panic!("Unexpected event"),
2433         }
2434 }
2435
2436 #[test]
2437 fn test_simple_partial_retry() {
2438         // In the first version of the in-`ChannelManager` payment retries, retries were sent for the
2439         // full amount of the payment, rather than only the missing amount. Here we simply test for
2440         // this by sending a payment with two parts, failing one, and retrying the second. Note that
2441         // `TestRouter` will check that the `RouteParameters` (which contain the amount) matches the
2442         // request.
2443         let chanmon_cfgs = create_chanmon_cfgs(3);
2444         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2445         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2446         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2447
2448         let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
2449         let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0).0.contents.short_channel_id;
2450
2451         let amt_msat = 200_000_000;
2452         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[2], amt_msat);
2453         #[cfg(feature = "std")]
2454         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2455         #[cfg(not(feature = "std"))]
2456         let payment_expiry_secs = 60 * 60;
2457         let mut invoice_features = InvoiceFeatures::empty();
2458         invoice_features.set_variable_length_onion_required();
2459         invoice_features.set_payment_secret_required();
2460         invoice_features.set_basic_mpp_optional();
2461         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2462                 .with_expiry_time(payment_expiry_secs as u64)
2463                 .with_features(invoice_features);
2464         let route_params = RouteParameters {
2465                 payment_params,
2466                 final_value_msat: amt_msat,
2467                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2468         };
2469
2470         let mut route = Route {
2471                 paths: vec![
2472                         vec![RouteHop {
2473                                 pubkey: nodes[1].node.get_our_node_id(),
2474                                 node_features: nodes[1].node.node_features(),
2475                                 short_channel_id: chan_1_scid,
2476                                 channel_features: nodes[1].node.channel_features(),
2477                                 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2478                                 cltv_expiry_delta: 100,
2479                         }, RouteHop {
2480                                 pubkey: nodes[2].node.get_our_node_id(),
2481                                 node_features: nodes[2].node.node_features(),
2482                                 short_channel_id: chan_2_scid,
2483                                 channel_features: nodes[2].node.channel_features(),
2484                                 fee_msat: 100_000_000,
2485                                 cltv_expiry_delta: 100,
2486                         }],
2487                         vec![RouteHop {
2488                                 pubkey: nodes[1].node.get_our_node_id(),
2489                                 node_features: nodes[1].node.node_features(),
2490                                 short_channel_id: chan_1_scid,
2491                                 channel_features: nodes[1].node.channel_features(),
2492                                 fee_msat: 100_000,
2493                                 cltv_expiry_delta: 100,
2494                         }, RouteHop {
2495                                 pubkey: nodes[2].node.get_our_node_id(),
2496                                 node_features: nodes[2].node.node_features(),
2497                                 short_channel_id: chan_2_scid,
2498                                 channel_features: nodes[2].node.channel_features(),
2499                                 fee_msat: 100_000_000,
2500                                 cltv_expiry_delta: 100,
2501                         }]
2502                 ],
2503                 payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
2504         };
2505         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2506         let mut second_payment_params = route_params.payment_params.clone();
2507         second_payment_params.previously_failed_channels = vec![chan_2_scid];
2508         // On retry, we'll only be asked for one path (or 100k sats)
2509         route.paths.remove(0);
2510         nodes[0].router.expect_find_route(RouteParameters {
2511                         payment_params: second_payment_params,
2512                         final_value_msat: amt_msat / 2, final_cltv_expiry_delta: TEST_FINAL_CLTV,
2513                 }, Ok(route.clone()));
2514
2515         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2516         let htlc_updates = SendEvent::from_node(&nodes[0]);
2517         check_added_monitors!(nodes[0], 1);
2518         assert_eq!(htlc_updates.msgs.len(), 1);
2519
2520         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
2521         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
2522         check_added_monitors!(nodes[1], 1);
2523         let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2524
2525         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2526         check_added_monitors!(nodes[0], 1);
2527         let second_htlc_updates = SendEvent::from_node(&nodes[0]);
2528
2529         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
2530         check_added_monitors!(nodes[0], 1);
2531         let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2532
2533         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
2534         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
2535         check_added_monitors!(nodes[1], 1);
2536         let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2537
2538         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
2539         check_added_monitors!(nodes[1], 1);
2540         let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2541
2542         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2543         check_added_monitors!(nodes[0], 1);
2544
2545         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
2546         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
2547         check_added_monitors!(nodes[0], 1);
2548         let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2549
2550         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
2551         check_added_monitors!(nodes[1], 1);
2552
2553         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
2554         check_added_monitors!(nodes[1], 1);
2555
2556         let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2557
2558         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
2559         check_added_monitors!(nodes[0], 1);
2560
2561         let mut events = nodes[0].node.get_and_clear_pending_events();
2562         assert_eq!(events.len(), 2);
2563         match events[0] {
2564                 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, ..  } => {
2565                         assert_eq!(payment_hash, ev_payment_hash);
2566                         assert_eq!(payment_failed_permanently, false);
2567                 },
2568                 _ => panic!("Unexpected event"),
2569         }
2570         match events[1] {
2571                 Event::PendingHTLCsForwardable { .. } => {},
2572                 _ => panic!("Unexpected event"),
2573         }
2574
2575         nodes[0].node.process_pending_htlc_forwards();
2576         let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
2577         check_added_monitors!(nodes[0], 1);
2578
2579         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
2580         commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
2581
2582         expect_pending_htlcs_forwardable!(nodes[1]);
2583         check_added_monitors!(nodes[1], 1);
2584
2585         let bs_forward_update = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
2586         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[0]);
2587         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[1]);
2588         commitment_signed_dance!(nodes[2], nodes[1], &bs_forward_update.commitment_signed, false);
2589
2590         expect_pending_htlcs_forwardable!(nodes[2]);
2591         expect_payment_claimable!(nodes[2], payment_hash, payment_secret, amt_msat);
2592 }
2593
2594 #[test]
2595 #[cfg(feature = "std")]
2596 fn test_threaded_payment_retries() {
2597         // In the first version of the in-`ChannelManager` payment retries, retries weren't limited to
2598         // a single thread and would happily let multiple threads run retries at the same time. Because
2599         // retries are done by first calculating the amount we need to retry, then dropping the
2600         // relevant lock, then actually sending, we would happily let multiple threads retry the same
2601         // amount at the same time, overpaying our original HTLC!
2602         let chanmon_cfgs = create_chanmon_cfgs(4);
2603         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
2604         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
2605         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
2606
2607         // There is one mitigating guardrail when retrying payments - we can never over-pay by more
2608         // than 10% of the original value. Thus, we want all our retries to be below that. In order to
2609         // keep things simple, we route one HTLC for 0.1% of the payment over channel 1 and the rest
2610         // out over channel 3+4. This will let us ignore 99% of the payment value and deal with only
2611         // our channel.
2612         let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
2613         create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 10_000_000, 0);
2614         let chan_3_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 10_000_000, 0).0.contents.short_channel_id;
2615         let chan_4_scid = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 0).0.contents.short_channel_id;
2616
2617         let amt_msat = 100_000_000;
2618         let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[2], amt_msat);
2619         #[cfg(feature = "std")]
2620         let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2621         #[cfg(not(feature = "std"))]
2622         let payment_expiry_secs = 60 * 60;
2623         let mut invoice_features = InvoiceFeatures::empty();
2624         invoice_features.set_variable_length_onion_required();
2625         invoice_features.set_payment_secret_required();
2626         invoice_features.set_basic_mpp_optional();
2627         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2628                 .with_expiry_time(payment_expiry_secs as u64)
2629                 .with_features(invoice_features);
2630         let mut route_params = RouteParameters {
2631                 payment_params,
2632                 final_value_msat: amt_msat,
2633                 final_cltv_expiry_delta: TEST_FINAL_CLTV,
2634         };
2635
2636         let mut route = Route {
2637                 paths: vec![
2638                         vec![RouteHop {
2639                                 pubkey: nodes[1].node.get_our_node_id(),
2640                                 node_features: nodes[1].node.node_features(),
2641                                 short_channel_id: chan_1_scid,
2642                                 channel_features: nodes[1].node.channel_features(),
2643                                 fee_msat: 0,
2644                                 cltv_expiry_delta: 100,
2645                         }, RouteHop {
2646                                 pubkey: nodes[3].node.get_our_node_id(),
2647                                 node_features: nodes[2].node.node_features(),
2648                                 short_channel_id: 42, // Set a random SCID which nodes[1] will fail as unknown
2649                                 channel_features: nodes[2].node.channel_features(),
2650                                 fee_msat: amt_msat / 1000,
2651                                 cltv_expiry_delta: 100,
2652                         }],
2653                         vec![RouteHop {
2654                                 pubkey: nodes[2].node.get_our_node_id(),
2655                                 node_features: nodes[2].node.node_features(),
2656                                 short_channel_id: chan_3_scid,
2657                                 channel_features: nodes[2].node.channel_features(),
2658                                 fee_msat: 100_000,
2659                                 cltv_expiry_delta: 100,
2660                         }, RouteHop {
2661                                 pubkey: nodes[3].node.get_our_node_id(),
2662                                 node_features: nodes[3].node.node_features(),
2663                                 short_channel_id: chan_4_scid,
2664                                 channel_features: nodes[3].node.channel_features(),
2665                                 fee_msat: amt_msat - amt_msat / 1000,
2666                                 cltv_expiry_delta: 100,
2667                         }]
2668                 ],
2669                 payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
2670         };
2671         nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2672
2673         nodes[0].node.send_payment_with_retry(payment_hash, &Some(payment_secret), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0xdeadbeef)).unwrap();
2674         check_added_monitors!(nodes[0], 2);
2675         let mut send_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2676         assert_eq!(send_msg_events.len(), 2);
2677         send_msg_events.retain(|msg|
2678                 if let MessageSendEvent::UpdateHTLCs { node_id, .. } = msg {
2679                         // Drop the commitment update for nodes[2], we can just let that one sit pending
2680                         // forever.
2681                         *node_id == nodes[1].node.get_our_node_id()
2682                 } else { panic!(); }
2683         );
2684
2685         // from here on out, the retry `RouteParameters` amount will be amt/1000
2686         route_params.final_value_msat /= 1000;
2687         route.paths.pop();
2688
2689         let end_time = Instant::now() + Duration::from_secs(1);
2690         macro_rules! thread_body { () => { {
2691                 // We really want std::thread::scope, but its not stable until 1.63. Until then, we get unsafe.
2692                 let node_ref = NodePtr::from_node(&nodes[0]);
2693                 move || {
2694                         let node_a = unsafe { &*node_ref.0 };
2695                         while Instant::now() < end_time {
2696                                 node_a.node.get_and_clear_pending_events(); // wipe the PendingHTLCsForwardable
2697                                 // Ignore if we have any pending events, just always pretend we just got a
2698                                 // PendingHTLCsForwardable
2699                                 node_a.node.process_pending_htlc_forwards();
2700                         }
2701                 }
2702         } } }
2703         let mut threads = Vec::new();
2704         for _ in 0..16 { threads.push(std::thread::spawn(thread_body!())); }
2705
2706         // Back in the main thread, poll pending messages and make sure that we never have more than
2707         // one HTLC pending at a time. Note that the commitment_signed_dance will fail horribly if
2708         // there are HTLC messages shoved in while its running. This allows us to test that we never
2709         // generate an additional update_add_htlc until we've fully failed the first.
2710         let mut previously_failed_channels = Vec::new();
2711         loop {
2712                 assert_eq!(send_msg_events.len(), 1);
2713                 let send_event = SendEvent::from_event(send_msg_events.pop().unwrap());
2714                 assert_eq!(send_event.msgs.len(), 1);
2715
2716                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
2717                 commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true);
2718
2719                 // Note that we only push one route into `expect_find_route` at a time, because that's all
2720                 // the retries (should) need. If the bug is reintroduced "real" routes may be selected, but
2721                 // we should still ultimately fail for the same reason - because we're trying to send too
2722                 // many HTLCs at once.
2723                 let mut new_route_params = route_params.clone();
2724                 previously_failed_channels.push(route.paths[0][1].short_channel_id);
2725                 new_route_params.payment_params.previously_failed_channels = previously_failed_channels.clone();
2726                 route.paths[0][1].short_channel_id += 1;
2727                 nodes[0].router.expect_find_route(new_route_params, Ok(route.clone()));
2728
2729                 let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2730                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
2731                 // The "normal" commitment_signed_dance delivers the final RAA and then calls
2732                 // `check_added_monitors` to ensure only the one RAA-generated monitor update was created.
2733                 // This races with our other threads which may generate an add-HTLCs commitment update via
2734                 // `process_pending_htlc_forwards`. Instead, we defer the monitor update check until after
2735                 // *we've* called `process_pending_htlc_forwards` when its guaranteed to have two updates.
2736                 let last_raa = commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true, false, true);
2737                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &last_raa);
2738
2739                 let cur_time = Instant::now();
2740                 if cur_time > end_time {
2741                         for thread in threads.drain(..) { thread.join().unwrap(); }
2742                 }
2743
2744                 // Make sure we have some events to handle when we go around...
2745                 nodes[0].node.get_and_clear_pending_events(); // wipe the PendingHTLCsForwardable
2746                 nodes[0].node.process_pending_htlc_forwards();
2747                 send_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2748                 check_added_monitors!(nodes[0], 2);
2749
2750                 if cur_time > end_time {
2751                         break;
2752                 }
2753         }
2754 }