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