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