4cd7761834370103f639c003814e69f71857df0a
[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_retry() {
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, 3, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
93         // Rebalance
94         send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
95
96         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 1_000_000);
97         let path = route.paths[0].clone();
98         route.paths.push(path);
99         route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
100         route.paths[0][0].short_channel_id = chan_1_id;
101         route.paths[0][1].short_channel_id = chan_3_id;
102         route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
103         route.paths[1][0].short_channel_id = chan_2_id;
104         route.paths[1][1].short_channel_id = chan_4_id;
105
106         // Initiate the MPP payment.
107         let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
108         check_added_monitors!(nodes[0], 2); // one monitor per path
109         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
110         assert_eq!(events.len(), 2);
111
112         // Pass half of the payment along the success path.
113         let success_path_msgs = events.remove(0);
114         pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
115
116         // Add the HTLC along the first hop.
117         let fail_path_msgs_1 = events.remove(0);
118         let (update_add, commitment_signed) = match fail_path_msgs_1 {
119                 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 } } => {
120                         assert_eq!(update_add_htlcs.len(), 1);
121                         assert!(update_fail_htlcs.is_empty());
122                         assert!(update_fulfill_htlcs.is_empty());
123                         assert!(update_fail_malformed_htlcs.is_empty());
124                         assert!(update_fee.is_none());
125                         (update_add_htlcs[0].clone(), commitment_signed.clone())
126                 },
127                 _ => panic!("Unexpected event"),
128         };
129         nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
130         commitment_signed_dance!(nodes[2], nodes[0], commitment_signed, false);
131
132         // Attempt to forward the payment and complete the 2nd path's failure.
133         expect_pending_htlcs_forwardable!(&nodes[2]);
134         expect_pending_htlcs_forwardable!(&nodes[2]);
135         let htlc_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
136         assert!(htlc_updates.update_add_htlcs.is_empty());
137         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
138         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
139         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
140         check_added_monitors!(nodes[2], 1);
141         nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
142         commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
143         expect_payment_failed!(nodes[0], payment_hash, false);
144
145         // Rebalance the channel so the second half of the payment can succeed.
146         send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
147
148         // Make sure it errors as expected given a too-large amount.
149         if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
150                 assert!(err.contains("over total_payment_amt_msat"));
151         } else { panic!("Unexpected error"); }
152
153         // Make sure it errors as expected given the wrong payment_id.
154         if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, PaymentId([0; 32])) {
155                 assert!(err.contains("not found"));
156         } else { panic!("Unexpected error"); }
157
158         // Retry the second half of the payment and make sure it succeeds.
159         let mut path = route.clone();
160         path.paths.remove(0);
161         nodes[0].node.retry_payment(&path, payment_id).unwrap();
162         check_added_monitors!(nodes[0], 1);
163         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
164         assert_eq!(events.len(), 1);
165         pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
166         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
167 }
168
169 #[test]
170 fn retry_expired_payment() {
171         let chanmon_cfgs = create_chanmon_cfgs(3);
172         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
173         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
174         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
175
176         let _chan_0 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
177         let _chan_1 = create_announced_chan_between_nodes(&nodes, 2, 1, InitFeatures::known(), InitFeatures::known());
178         // Rebalance to find a route
179         send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
180
181         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
182
183         // Rebalance so that the first hop fails.
184         send_payment(&nodes[1], &vec!(&nodes[2])[..], 2_000_000);
185
186         // Make sure the payment fails on the first hop.
187         let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
188         check_added_monitors!(nodes[0], 1);
189         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
190         assert_eq!(events.len(), 1);
191         let mut payment_event = SendEvent::from_event(events.pop().unwrap());
192         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
193         check_added_monitors!(nodes[1], 0);
194         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
195         expect_pending_htlcs_forwardable!(nodes[1]);
196         expect_pending_htlcs_forwardable!(&nodes[1]);
197         let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
198         assert!(htlc_updates.update_add_htlcs.is_empty());
199         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
200         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
201         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
202         check_added_monitors!(nodes[1], 1);
203         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
204         commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
205         expect_payment_failed!(nodes[0], payment_hash, false);
206
207         // Mine blocks so the payment will have expired.
208         connect_blocks(&nodes[0], 3);
209
210         // Retry the payment and make sure it errors as expected.
211         if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
212                 assert!(err.contains("not found"));
213         } else {
214                 panic!("Unexpected error");
215         }
216 }
217
218 #[test]
219 fn no_pending_leak_on_initial_send_failure() {
220         // In an earlier version of our payment tracking, we'd have a retry entry even when the initial
221         // HTLC for payment failed to send due to local channel errors (e.g. peer disconnected). In this
222         // case, the user wouldn't have a PaymentId to retry the payment with, but we'd think we have a
223         // pending payment forever and never time it out.
224         // Here we test exactly that - retrying a payment when a peer was disconnected on the first
225         // try, and then check that no pending payment is being tracked.
226         let chanmon_cfgs = create_chanmon_cfgs(2);
227         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
228         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
229         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
230
231         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
232
233         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
234
235         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
236         nodes[1].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
237
238         unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)),
239                 true, APIError::ChannelUnavailable { ref err },
240                 assert_eq!(err, "Peer for first hop currently disconnected/pending monitor update!"));
241
242         assert!(!nodes[0].node.has_pending_payments());
243 }