1 // This file is Copyright its original authors, visible in version control
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
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.
14 use ln::{PaymentPreimage, PaymentHash};
15 use ln::channelmanager::{PaymentId, PaymentSendFailure};
16 use routing::router::get_route;
17 use ln::features::{InitFeatures, InvoiceFeatures};
19 use ln::msgs::ChannelMessageHandler;
21 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
22 use util::errors::APIError;
24 use bitcoin::hashes::sha256::Hash as Sha256;
25 use bitcoin::hashes::Hash;
29 use ln::functional_test_utils::*;
32 fn retry_single_path_payment() {
33 let chanmon_cfgs = create_chanmon_cfgs(3);
34 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
35 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
36 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
38 let _chan_0 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
39 let _chan_1 = create_announced_chan_between_nodes(&nodes, 2, 1, InitFeatures::known(), InitFeatures::known());
40 // Rebalance to find a route
41 send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
43 let logger = test_utils::TestLogger::new();
44 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
45 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
46 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
48 // Rebalance so that the first hop fails.
49 send_payment(&nodes[1], &vec!(&nodes[2])[..], 2_000_000);
51 // Make sure the payment fails on the first hop.
52 let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
53 check_added_monitors!(nodes[0], 1);
54 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
55 assert_eq!(events.len(), 1);
56 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
57 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
58 check_added_monitors!(nodes[1], 0);
59 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
60 expect_pending_htlcs_forwardable!(nodes[1]);
61 expect_pending_htlcs_forwardable!(&nodes[1]);
62 let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
63 assert!(htlc_updates.update_add_htlcs.is_empty());
64 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
65 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
66 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
67 check_added_monitors!(nodes[1], 1);
68 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
69 commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
70 expect_payment_failed!(nodes[0], payment_hash, false);
72 // Rebalance the channel so the retry succeeds.
73 send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
75 // Mine two blocks (we expire retries after 3, so this will check that we don't expire early)
76 connect_blocks(&nodes[0], 2);
78 // Retry the payment and make sure it succeeds.
79 nodes[0].node.retry_payment(&route, payment_id).unwrap();
80 check_added_monitors!(nodes[0], 1);
81 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
82 assert_eq!(events.len(), 1);
83 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 100_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
84 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
89 let chanmon_cfgs = create_chanmon_cfgs(4);
90 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
91 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
92 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
94 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
95 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
96 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
97 let chan_4_id = create_announced_chan_between_nodes(&nodes, 3, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
98 let logger = test_utils::TestLogger::new();
100 send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
102 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[3]);
103 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
104 let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1_000_000, TEST_FINAL_CLTV, &logger).unwrap();
105 let path = route.paths[0].clone();
106 route.paths.push(path);
107 route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
108 route.paths[0][0].short_channel_id = chan_1_id;
109 route.paths[0][1].short_channel_id = chan_3_id;
110 route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
111 route.paths[1][0].short_channel_id = chan_2_id;
112 route.paths[1][1].short_channel_id = chan_4_id;
114 // Initiate the MPP payment.
115 let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
116 check_added_monitors!(nodes[0], 2); // one monitor per path
117 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
118 assert_eq!(events.len(), 2);
120 // Pass half of the payment along the success path.
121 let success_path_msgs = events.remove(0);
122 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
124 // Add the HTLC along the first hop.
125 let fail_path_msgs_1 = events.remove(0);
126 let (update_add, commitment_signed) = match fail_path_msgs_1 {
127 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 } } => {
128 assert_eq!(update_add_htlcs.len(), 1);
129 assert!(update_fail_htlcs.is_empty());
130 assert!(update_fulfill_htlcs.is_empty());
131 assert!(update_fail_malformed_htlcs.is_empty());
132 assert!(update_fee.is_none());
133 (update_add_htlcs[0].clone(), commitment_signed.clone())
135 _ => panic!("Unexpected event"),
137 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
138 commitment_signed_dance!(nodes[2], nodes[0], commitment_signed, false);
140 // Attempt to forward the payment and complete the 2nd path's failure.
141 expect_pending_htlcs_forwardable!(&nodes[2]);
142 expect_pending_htlcs_forwardable!(&nodes[2]);
143 let htlc_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
144 assert!(htlc_updates.update_add_htlcs.is_empty());
145 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
146 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
147 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
148 check_added_monitors!(nodes[2], 1);
149 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
150 commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
151 expect_payment_failed!(nodes[0], payment_hash, false);
153 // Rebalance the channel so the second half of the payment can succeed.
154 send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
156 // Make sure it errors as expected given a too-large amount.
157 if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
158 assert!(err.contains("over total_payment_amt_msat"));
159 } else { panic!("Unexpected error"); }
161 // Make sure it errors as expected given the wrong payment_id.
162 if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, PaymentId([0; 32])) {
163 assert!(err.contains("not found"));
164 } else { panic!("Unexpected error"); }
166 // Retry the second half of the payment and make sure it succeeds.
167 let mut path = route.clone();
168 path.paths.remove(0);
169 nodes[0].node.retry_payment(&path, payment_id).unwrap();
170 check_added_monitors!(nodes[0], 1);
171 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
172 assert_eq!(events.len(), 1);
173 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
174 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
178 fn retry_expired_payment() {
179 let chanmon_cfgs = create_chanmon_cfgs(3);
180 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
181 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
182 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
184 let _chan_0 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
185 let _chan_1 = create_announced_chan_between_nodes(&nodes, 2, 1, InitFeatures::known(), InitFeatures::known());
186 // Rebalance to find a route
187 send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
189 let logger = test_utils::TestLogger::new();
190 let (_payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
191 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
192 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
194 // Rebalance so that the first hop fails.
195 send_payment(&nodes[1], &vec!(&nodes[2])[..], 2_000_000);
197 // Make sure the payment fails on the first hop.
198 let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
199 check_added_monitors!(nodes[0], 1);
200 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
201 assert_eq!(events.len(), 1);
202 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
203 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
204 check_added_monitors!(nodes[1], 0);
205 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
206 expect_pending_htlcs_forwardable!(nodes[1]);
207 expect_pending_htlcs_forwardable!(&nodes[1]);
208 let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
209 assert!(htlc_updates.update_add_htlcs.is_empty());
210 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
211 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
212 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
213 check_added_monitors!(nodes[1], 1);
214 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
215 commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
216 expect_payment_failed!(nodes[0], payment_hash, false);
218 // Mine blocks so the payment will have expired.
219 connect_blocks(&nodes[0], 3);
221 // Retry the payment and make sure it errors as expected.
222 if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
223 assert!(err.contains("not found"));
225 panic!("Unexpected error");
230 fn no_pending_leak_on_initial_send_failure() {
231 // In an earlier version of our payment tracking, we'd have a retry entry even when the initial
232 // HTLC for payment failed to send due to local channel errors (e.g. peer disconnected). In this
233 // case, the user wouldn't have a PaymentId to retry the payment with, but we'd think we have a
234 // pending payment forever and never time it out.
235 // Here we test exactly that - retrying a payment when a peer was disconnected on the first
236 // try, and then check that no pending payment is being tracked.
237 let chanmon_cfgs = create_chanmon_cfgs(2);
238 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
239 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
240 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
242 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
244 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
246 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
247 nodes[1].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
249 unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)),
250 true, APIError::ChannelUnavailable { ref err },
251 assert_eq!(err, "Peer for first hop currently disconnected/pending monitor update!"));
253 assert!(!nodes[0].node.has_pending_payments());