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 crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
15 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, HTLC_FAIL_BACK_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
16 use crate::sign::EntropySource;
17 use crate::chain::transaction::OutPoint;
18 use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason, PaymentPurpose};
19 use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
20 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, RecentPaymentDetails, RecipientOnionFields, HTLCForwardInfo, PendingHTLCRouting, PendingAddHTLCInfo};
21 use crate::ln::features::Bolt11InvoiceFeatures;
22 use crate::ln::{msgs, ChannelId, PaymentSecret, PaymentPreimage};
23 use crate::ln::msgs::ChannelMessageHandler;
24 use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, Retry};
25 use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
26 use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters, find_route};
27 use crate::routing::scoring::ChannelUsage;
28 use crate::util::config::UserConfig;
29 use crate::util::test_utils;
30 use crate::util::errors::APIError;
31 use crate::util::ser::Writeable;
32 use crate::util::string::UntrustedString;
34 use bitcoin::network::constants::Network;
36 use crate::prelude::*;
38 use crate::ln::functional_test_utils::*;
39 use crate::routing::gossip::NodeId;
40 #[cfg(feature = "std")]
42 crate::util::time::tests::SinceEpoch,
43 std::time::{SystemTime, Instant, Duration}
48 let chanmon_cfgs = create_chanmon_cfgs(4);
49 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
50 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
51 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
53 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
54 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
55 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
56 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
58 let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
59 let path = route.paths[0].clone();
60 route.paths.push(path);
61 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
62 route.paths[0].hops[0].short_channel_id = chan_1_id;
63 route.paths[0].hops[1].short_channel_id = chan_3_id;
64 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
65 route.paths[1].hops[0].short_channel_id = chan_2_id;
66 route.paths[1].hops[1].short_channel_id = chan_4_id;
67 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
68 fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
73 let chanmon_cfgs = create_chanmon_cfgs(4);
74 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
75 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
76 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
78 let (chan_1_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
79 let (chan_2_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 2);
80 let (chan_3_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 1, 3);
81 let (chan_4_update, _, chan_4_id, _) = create_announced_chan_between_nodes(&nodes, 3, 2);
83 send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
85 let amt_msat = 1_000_000;
86 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], amt_msat);
87 let path = route.paths[0].clone();
88 route.paths.push(path);
89 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
90 route.paths[0].hops[0].short_channel_id = chan_1_update.contents.short_channel_id;
91 route.paths[0].hops[1].short_channel_id = chan_3_update.contents.short_channel_id;
92 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
93 route.paths[1].hops[0].short_channel_id = chan_2_update.contents.short_channel_id;
94 route.paths[1].hops[1].short_channel_id = chan_4_update.contents.short_channel_id;
96 // Initiate the MPP payment.
97 let payment_id = PaymentId(payment_hash.0);
98 let mut route_params = route.route_params.clone().unwrap();
100 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
101 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
102 payment_id, route_params.clone(), Retry::Attempts(1)).unwrap();
103 check_added_monitors!(nodes[0], 2); // one monitor per path
104 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
105 assert_eq!(events.len(), 2);
107 // Pass half of the payment along the success path.
108 let success_path_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
109 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
111 // Add the HTLC along the first hop.
112 let fail_path_msgs_1 = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
113 let (update_add, commitment_signed) = match fail_path_msgs_1 {
114 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 } } => {
115 assert_eq!(update_add_htlcs.len(), 1);
116 assert!(update_fail_htlcs.is_empty());
117 assert!(update_fulfill_htlcs.is_empty());
118 assert!(update_fail_malformed_htlcs.is_empty());
119 assert!(update_fee.is_none());
120 (update_add_htlcs[0].clone(), commitment_signed.clone())
122 _ => panic!("Unexpected event"),
124 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
125 commitment_signed_dance!(nodes[2], nodes[0], commitment_signed, false);
127 // Attempt to forward the payment and complete the 2nd path's failure.
128 expect_pending_htlcs_forwardable!(&nodes[2]);
129 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 }]);
130 let htlc_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
131 assert!(htlc_updates.update_add_htlcs.is_empty());
132 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
133 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
134 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
135 check_added_monitors!(nodes[2], 1);
136 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
137 commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
138 let mut events = nodes[0].node.get_and_clear_pending_events();
140 Event::PendingHTLCsForwardable { .. } => {},
141 _ => panic!("Unexpected event")
144 expect_payment_failed_conditions_event(events, payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
146 // Rebalance the channel so the second half of the payment can succeed.
147 send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
149 // Retry the second half of the payment and make sure it succeeds.
150 route.paths.remove(0);
151 route_params.final_value_msat = 1_000_000;
152 route_params.payment_params.previously_failed_channels.push(chan_4_update.contents.short_channel_id);
153 nodes[0].router.expect_find_route(route_params, Ok(route));
154 nodes[0].node.process_pending_htlc_forwards();
155 check_added_monitors!(nodes[0], 1);
156 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
157 assert_eq!(events.len(), 1);
158 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
159 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
162 fn do_mpp_receive_timeout(send_partial_mpp: bool) {
163 let chanmon_cfgs = create_chanmon_cfgs(4);
164 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
165 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
166 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
168 let (chan_1_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
169 let (chan_2_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 0, 2);
170 let (chan_3_update, _, chan_3_id, _) = create_announced_chan_between_nodes(&nodes, 1, 3);
171 let (chan_4_update, _, _, _) = create_announced_chan_between_nodes(&nodes, 2, 3);
173 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 100_000);
174 let path = route.paths[0].clone();
175 route.paths.push(path);
176 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
177 route.paths[0].hops[0].short_channel_id = chan_1_update.contents.short_channel_id;
178 route.paths[0].hops[1].short_channel_id = chan_3_update.contents.short_channel_id;
179 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
180 route.paths[1].hops[0].short_channel_id = chan_2_update.contents.short_channel_id;
181 route.paths[1].hops[1].short_channel_id = chan_4_update.contents.short_channel_id;
183 // Initiate the MPP payment.
184 nodes[0].node.send_payment_with_route(&route, payment_hash,
185 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
186 check_added_monitors!(nodes[0], 2); // one monitor per path
187 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
188 assert_eq!(events.len(), 2);
190 // Pass half of the payment along the first path.
191 let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
192 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
194 if send_partial_mpp {
195 // Time out the partial MPP
196 for _ in 0..MPP_TIMEOUT_TICKS {
197 nodes[3].node.timer_tick_occurred();
200 // Failed HTLC from node 3 -> 1
201 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash }]);
202 let htlc_fail_updates_3_1 = get_htlc_update_msgs!(nodes[3], nodes[1].node.get_our_node_id());
203 assert_eq!(htlc_fail_updates_3_1.update_fail_htlcs.len(), 1);
204 nodes[1].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &htlc_fail_updates_3_1.update_fail_htlcs[0]);
205 check_added_monitors!(nodes[3], 1);
206 commitment_signed_dance!(nodes[1], nodes[3], htlc_fail_updates_3_1.commitment_signed, false);
208 // Failed HTLC from node 1 -> 0
209 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 }]);
210 let htlc_fail_updates_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
211 assert_eq!(htlc_fail_updates_1_0.update_fail_htlcs.len(), 1);
212 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates_1_0.update_fail_htlcs[0]);
213 check_added_monitors!(nodes[1], 1);
214 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates_1_0.commitment_signed, false);
216 expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain().expected_htlc_error_data(23, &[][..]));
218 // Pass half of the payment along the second path.
219 let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
220 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_2_msgs, true, None);
222 // Even after MPP_TIMEOUT_TICKS we should not timeout the MPP if we have all the parts
223 for _ in 0..MPP_TIMEOUT_TICKS {
224 nodes[3].node.timer_tick_occurred();
227 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
232 fn mpp_receive_timeout() {
233 do_mpp_receive_timeout(true);
234 do_mpp_receive_timeout(false);
238 fn test_keysend_payments() {
239 do_test_keysend_payments(false, false);
240 do_test_keysend_payments(false, true);
241 do_test_keysend_payments(true, false);
242 do_test_keysend_payments(true, true);
245 fn do_test_keysend_payments(public_node: bool, with_retry: bool) {
246 let chanmon_cfgs = create_chanmon_cfgs(2);
247 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
248 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
249 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
252 create_announced_chan_between_nodes(&nodes, 0, 1);
254 create_chan_between_nodes(&nodes[0], &nodes[1]);
256 let payer_pubkey = nodes[0].node.get_our_node_id();
257 let payee_pubkey = nodes[1].node.get_our_node_id();
258 let route_params = RouteParameters::from_payment_params_and_value(
259 PaymentParameters::for_keysend(payee_pubkey, 40, false), 10000);
261 let network_graph = nodes[0].network_graph.clone();
262 let channels = nodes[0].node.list_usable_channels();
263 let first_hops = channels.iter().collect::<Vec<_>>();
264 let first_hops = if public_node { None } else { Some(first_hops.as_slice()) };
266 let scorer = test_utils::TestScorer::new();
267 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
268 let route = find_route(
269 &payer_pubkey, &route_params, &network_graph, first_hops,
270 nodes[0].logger, &scorer, &(), &random_seed_bytes
274 let test_preimage = PaymentPreimage([42; 32]);
276 nodes[0].node.send_spontaneous_payment_with_retry(Some(test_preimage),
277 RecipientOnionFields::spontaneous_empty(), PaymentId(test_preimage.0),
278 route_params, Retry::Attempts(1)).unwrap()
280 nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage),
281 RecipientOnionFields::spontaneous_empty(), PaymentId(test_preimage.0)).unwrap()
284 check_added_monitors!(nodes[0], 1);
285 let send_event = SendEvent::from_node(&nodes[0]);
286 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
287 do_commitment_signed_dance(&nodes[1], &nodes[0], &send_event.commitment_msg, false, false);
288 expect_pending_htlcs_forwardable!(nodes[1]);
289 // Previously, a refactor caused us to stop including the payment preimage in the onion which
290 // is sent as a part of keysend payments. Thus, to be extra careful here, we scope the preimage
291 // above to demonstrate that we have no way to get the preimage at this point except by
292 // extracting it from the onion nodes[1] received.
293 let event = nodes[1].node.get_and_clear_pending_events();
294 assert_eq!(event.len(), 1);
295 if let Event::PaymentClaimable { purpose: PaymentPurpose::SpontaneousPayment(preimage), .. } = event[0] {
296 claim_payment(&nodes[0], &[&nodes[1]], preimage);
301 fn test_mpp_keysend() {
302 let mut mpp_keysend_config = test_default_channel_config();
303 mpp_keysend_config.accept_mpp_keysend = true;
304 let chanmon_cfgs = create_chanmon_cfgs(4);
305 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
306 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, Some(mpp_keysend_config)]);
307 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
309 create_announced_chan_between_nodes(&nodes, 0, 1);
310 create_announced_chan_between_nodes(&nodes, 0, 2);
311 create_announced_chan_between_nodes(&nodes, 1, 3);
312 create_announced_chan_between_nodes(&nodes, 2, 3);
313 let network_graph = nodes[0].network_graph.clone();
315 let payer_pubkey = nodes[0].node.get_our_node_id();
316 let payee_pubkey = nodes[3].node.get_our_node_id();
317 let recv_value = 15_000_000;
318 let route_params = RouteParameters::from_payment_params_and_value(
319 PaymentParameters::for_keysend(payee_pubkey, 40, true), recv_value);
320 let scorer = test_utils::TestScorer::new();
321 let random_seed_bytes = chanmon_cfgs[0].keys_manager.get_secure_random_bytes();
322 let route = find_route(&payer_pubkey, &route_params, &network_graph, None, nodes[0].logger,
323 &scorer, &(), &random_seed_bytes).unwrap();
325 let payment_preimage = PaymentPreimage([42; 32]);
326 let payment_secret = PaymentSecret(payment_preimage.0);
327 let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
328 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_preimage.0)).unwrap();
329 check_added_monitors!(nodes[0], 2);
331 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
332 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
333 assert_eq!(events.len(), 2);
335 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
336 pass_along_path(&nodes[0], expected_route[0], recv_value, payment_hash.clone(),
337 Some(payment_secret), ev.clone(), false, Some(payment_preimage));
339 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
340 pass_along_path(&nodes[0], expected_route[1], recv_value, payment_hash.clone(),
341 Some(payment_secret), ev.clone(), true, Some(payment_preimage));
342 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
346 fn test_reject_mpp_keysend_htlc() {
347 // This test enforces that we reject MPP keysend HTLCs if our config states we don't support
348 // MPP keysend. When receiving a payment, if we don't support MPP keysend we'll reject the
349 // payment if it's keysend and has a payment secret, never reaching our payment validation
350 // logic. To check that we enforce rejecting MPP keysends in our payment logic, here we send
351 // keysend payments without payment secrets, then modify them by adding payment secrets in the
352 // final node in between receiving the HTLCs and actually processing them.
353 let mut reject_mpp_keysend_cfg = test_default_channel_config();
354 reject_mpp_keysend_cfg.accept_mpp_keysend = false;
356 let chanmon_cfgs = create_chanmon_cfgs(4);
357 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
358 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, Some(reject_mpp_keysend_cfg)]);
359 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
360 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
361 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
362 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
363 let (update_a, _, chan_4_channel_id, _) = create_announced_chan_between_nodes(&nodes, 2, 3);
364 let chan_4_id = update_a.contents.short_channel_id;
366 let (mut route, payment_hash, payment_preimage, _) = get_route_and_payment_hash!(nodes[0], nodes[3], amount);
368 // Pay along nodes[1]
369 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
370 route.paths[0].hops[0].short_channel_id = chan_1_id;
371 route.paths[0].hops[1].short_channel_id = chan_3_id;
373 let payment_id_0 = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
374 nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_0).unwrap();
375 check_added_monitors!(nodes[0], 1);
377 let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
378 let update_add_0 = update_0.update_add_htlcs[0].clone();
379 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0);
380 commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
381 expect_pending_htlcs_forwardable!(nodes[1]);
383 check_added_monitors!(&nodes[1], 1);
384 let update_1 = get_htlc_update_msgs!(nodes[1], nodes[3].node.get_our_node_id());
385 let update_add_1 = update_1.update_add_htlcs[0].clone();
386 nodes[3].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1);
387 commitment_signed_dance!(nodes[3], nodes[1], update_1.commitment_signed, false, true);
389 assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
390 for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
391 for f in pending_forwards.iter_mut() {
393 &mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
394 match forward_info.routing {
395 PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
396 *payment_data = Some(msgs::FinalOnionHopData {
397 payment_secret: PaymentSecret([42; 32]),
398 total_msat: amount * 2,
401 _ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
408 expect_pending_htlcs_forwardable!(nodes[3]);
410 // Pay along nodes[2]
411 route.paths[0].hops[0].pubkey = nodes[2].node.get_our_node_id();
412 route.paths[0].hops[0].short_channel_id = chan_2_id;
413 route.paths[0].hops[1].short_channel_id = chan_4_id;
415 let payment_id_1 = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
416 nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id_1).unwrap();
417 check_added_monitors!(nodes[0], 1);
419 let update_2 = get_htlc_update_msgs!(nodes[0], nodes[2].node.get_our_node_id());
420 let update_add_2 = update_2.update_add_htlcs[0].clone();
421 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_2);
422 commitment_signed_dance!(nodes[2], nodes[0], &update_2.commitment_signed, false, true);
423 expect_pending_htlcs_forwardable!(nodes[2]);
425 check_added_monitors!(&nodes[2], 1);
426 let update_3 = get_htlc_update_msgs!(nodes[2], nodes[3].node.get_our_node_id());
427 let update_add_3 = update_3.update_add_htlcs[0].clone();
428 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &update_add_3);
429 commitment_signed_dance!(nodes[3], nodes[2], update_3.commitment_signed, false, true);
431 assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
432 for (_, pending_forwards) in nodes[3].node.forward_htlcs.lock().unwrap().iter_mut() {
433 for f in pending_forwards.iter_mut() {
435 &mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) => {
436 match forward_info.routing {
437 PendingHTLCRouting::ReceiveKeysend { ref mut payment_data, .. } => {
438 *payment_data = Some(msgs::FinalOnionHopData {
439 payment_secret: PaymentSecret([42; 32]),
440 total_msat: amount * 2,
443 _ => panic!("Expected PendingHTLCRouting::ReceiveKeysend"),
450 expect_pending_htlcs_forwardable!(nodes[3]);
451 check_added_monitors!(nodes[3], 1);
453 // Fail back along nodes[2]
454 let update_fail_0 = get_htlc_update_msgs!(&nodes[3], &nodes[2].node.get_our_node_id());
455 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &update_fail_0.update_fail_htlcs[0]);
456 commitment_signed_dance!(nodes[2], nodes[3], update_fail_0.commitment_signed, false);
457 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_channel_id }]);
458 check_added_monitors!(nodes[2], 1);
460 let update_fail_1 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
461 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &update_fail_1.update_fail_htlcs[0]);
462 commitment_signed_dance!(nodes[0], nodes[2], update_fail_1.commitment_signed, false);
464 expect_payment_failed_conditions(&nodes[0], payment_hash, true, PaymentFailedConditions::new());
465 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash }]);
470 fn no_pending_leak_on_initial_send_failure() {
471 // In an earlier version of our payment tracking, we'd have a retry entry even when the initial
472 // HTLC for payment failed to send due to local channel errors (e.g. peer disconnected). In this
473 // case, the user wouldn't have a PaymentId to retry the payment with, but we'd think we have a
474 // pending payment forever and never time it out.
475 // Here we test exactly that - retrying a payment when a peer was disconnected on the first
476 // try, and then check that no pending payment is being tracked.
477 let chanmon_cfgs = create_chanmon_cfgs(2);
478 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
479 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
480 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
482 create_announced_chan_between_nodes(&nodes, 0, 1);
484 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
486 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
487 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
489 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
490 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
491 ), true, APIError::ChannelUnavailable { ref err },
492 assert_eq!(err, "Peer for first hop currently disconnected"));
494 assert!(!nodes[0].node.has_pending_payments());
497 fn do_retry_with_no_persist(confirm_before_reload: bool) {
498 // If we send a pending payment and `send_payment` returns success, we should always either
499 // return a payment failure event or a payment success event, and on failure the payment should
502 // In order to do so when the ChannelManager isn't immediately persisted (which is normal - its
503 // always persisted asynchronously), the ChannelManager has to reload some payment data from
504 // ChannelMonitor(s) in some cases. This tests that reloading.
506 // `confirm_before_reload` confirms the channel-closing commitment transaction on-chain prior
507 // to reloading the ChannelManager, increasing test coverage in ChannelMonitor HTLC tracking
508 // which has separate codepaths for "commitment transaction already confirmed" and not.
509 let chanmon_cfgs = create_chanmon_cfgs(3);
510 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
512 let new_chain_monitor;
513 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
514 let nodes_0_deserialized;
515 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
517 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
518 let (_, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
520 // Serialize the ChannelManager prior to sending payments
521 let nodes_0_serialized = nodes[0].node.encode();
523 // Send two payments - one which will get to nodes[2] and will be claimed, one which we'll time
525 let amt_msat = 1_000_000;
526 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
527 let (payment_preimage_1, payment_hash_1, _, payment_id_1) = send_along_route(&nodes[0], route.clone(), &[&nodes[1], &nodes[2]], 1_000_000);
528 let route_params = route.route_params.unwrap().clone();
529 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
530 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
531 check_added_monitors!(nodes[0], 1);
533 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
534 assert_eq!(events.len(), 1);
535 let payment_event = SendEvent::from_event(events.pop().unwrap());
536 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
538 // We relay the payment to nodes[1] while its disconnected from nodes[2], causing the payment
539 // to be returned immediately to nodes[0], without having nodes[2] fail the inbound payment
540 // which would prevent retry.
541 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
542 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
544 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
545 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
546 // nodes[1] now immediately fails the HTLC as the next-hop channel is disconnected
547 let _ = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
549 reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[2]));
551 let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan_id)[0].clone();
552 if confirm_before_reload {
553 mine_transaction(&nodes[0], &as_commitment_tx);
554 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
557 // The ChannelMonitor should always be the latest version, as we're required to persist it
558 // during the `commitment_signed_dance!()`.
559 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
560 reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
562 // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and
563 // force-close the channel.
564 check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager, [nodes[1].node.get_our_node_id()], 100000);
565 assert!(nodes[0].node.list_channels().is_empty());
566 assert!(nodes[0].node.has_pending_payments());
567 nodes[0].node.timer_tick_occurred();
568 if !confirm_before_reload {
569 let as_broadcasted_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
570 assert_eq!(as_broadcasted_txn.len(), 1);
571 assert_eq!(as_broadcasted_txn[0].txid(), as_commitment_tx.txid());
573 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
575 check_added_monitors!(nodes[0], 1);
577 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
578 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
579 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
581 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
583 // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
584 // error, as the channel has hit the chain.
585 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
586 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
588 let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
589 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
590 let as_err = nodes[0].node.get_and_clear_pending_msg_events();
591 assert_eq!(as_err.len(), 1);
593 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
594 assert_eq!(node_id, nodes[1].node.get_our_node_id());
595 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), msg);
596 check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
597 &nodes[1].node.get_our_node_id())) }, [nodes[0].node.get_our_node_id()], 100000);
598 check_added_monitors!(nodes[1], 1);
599 assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
601 _ => panic!("Unexpected event"),
603 check_closed_broadcast!(nodes[1], false);
605 // Now claim the first payment, which should allow nodes[1] to claim the payment on-chain when
606 // we close in a moment.
607 nodes[2].node.claim_funds(payment_preimage_1);
608 check_added_monitors!(nodes[2], 1);
609 expect_payment_claimed!(nodes[2], payment_hash_1, 1_000_000);
611 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
612 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
613 check_added_monitors!(nodes[1], 1);
614 commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false);
615 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, true, false);
617 if confirm_before_reload {
618 let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone();
619 nodes[0].node.best_block_updated(&best_block.0.header, best_block.1);
622 // Create a new channel on which to retry the payment before we fail the payment via the
623 // HTLC-Timeout transaction. This avoids ChannelManager timing out the payment due to us
624 // connecting several blocks while creating the channel (implying time has passed).
625 create_announced_chan_between_nodes(&nodes, 0, 1);
626 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
628 mine_transaction(&nodes[1], &as_commitment_tx);
629 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
630 assert_eq!(bs_htlc_claim_txn.len(), 1);
631 check_spends!(bs_htlc_claim_txn[0], as_commitment_tx);
633 if !confirm_before_reload {
634 mine_transaction(&nodes[0], &as_commitment_tx);
636 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
637 expect_payment_sent(&nodes[0], payment_preimage_1, None, true, false);
638 connect_blocks(&nodes[0], TEST_FINAL_CLTV*4 + 20);
639 let (first_htlc_timeout_tx, second_htlc_timeout_tx) = {
640 let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
641 assert_eq!(txn.len(), 2);
642 (txn.remove(0), txn.remove(0))
644 check_spends!(first_htlc_timeout_tx, as_commitment_tx);
645 check_spends!(second_htlc_timeout_tx, as_commitment_tx);
646 if first_htlc_timeout_tx.input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
647 confirm_transaction(&nodes[0], &second_htlc_timeout_tx);
649 confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
651 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
652 expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new());
654 // Finally, retry the payment (which was reloaded from the ChannelMonitor when nodes[0] was
655 // reloaded) via a route over the new channel, which work without issue and eventually be
656 // received and claimed at the recipient just like any other payment.
657 let (mut new_route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
659 // Update the fee on the middle hop to ensure PaymentSent events have the correct (retried) fee
660 // and not the original fee. We also update node[1]'s relevant config as
661 // do_claim_payment_along_route expects us to never overpay.
663 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
664 let mut peer_state = per_peer_state.get(&nodes[2].node.get_our_node_id())
665 .unwrap().lock().unwrap();
666 let mut channel = peer_state.channel_by_id.get_mut(&chan_id_2).unwrap();
667 let mut new_config = channel.context().config();
668 new_config.forwarding_fee_base_msat += 100_000;
669 channel.context_mut().update_config(&new_config);
670 new_route.paths[0].hops[0].fee_msat += 100_000;
673 // Force expiration of the channel's previous config.
674 for _ in 0..EXPIRE_PREV_CONFIG_TICKS {
675 nodes[1].node.timer_tick_occurred();
678 assert!(nodes[0].node.send_payment_with_route(&new_route, payment_hash, // Shouldn't be allowed to retry a fulfilled payment
679 RecipientOnionFields::secret_only(payment_secret), payment_id_1).is_err());
680 nodes[0].node.send_payment_with_route(&new_route, payment_hash,
681 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
682 check_added_monitors!(nodes[0], 1);
683 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
684 assert_eq!(events.len(), 1);
685 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
686 do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
687 expect_payment_sent!(nodes[0], payment_preimage, Some(new_route.paths[0].hops[0].fee_msat));
691 fn retry_with_no_persist() {
692 do_retry_with_no_persist(true);
693 do_retry_with_no_persist(false);
696 fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) {
697 // Test that an off-chain completed payment is not retryable on restart. This was previously
698 // broken for dust payments, but we test for both dust and non-dust payments.
700 // `use_dust` switches to using a dust HTLC, which results in the HTLC not having an on-chain
702 let chanmon_cfgs = create_chanmon_cfgs(3);
703 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
705 let mut manually_accept_config = test_default_channel_config();
706 manually_accept_config.manually_accept_inbound_channels = true;
709 let first_new_chain_monitor;
710 let second_persister;
711 let second_new_chain_monitor;
713 let third_new_chain_monitor;
715 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(manually_accept_config), None]);
716 let first_nodes_0_deserialized;
717 let second_nodes_0_deserialized;
718 let third_nodes_0_deserialized;
720 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
722 // Because we set nodes[1] to manually accept channels, just open a 0-conf channel.
723 let (funding_tx, chan_id) = open_zero_conf_channel(&nodes[0], &nodes[1], None);
724 confirm_transaction(&nodes[0], &funding_tx);
725 confirm_transaction(&nodes[1], &funding_tx);
726 // Ignore the announcement_signatures messages
727 nodes[0].node.get_and_clear_pending_msg_events();
728 nodes[1].node.get_and_clear_pending_msg_events();
729 let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
731 // Serialize the ChannelManager prior to sending payments
732 let mut nodes_0_serialized = nodes[0].node.encode();
734 let route = get_route_and_payment_hash!(nodes[0], nodes[2], if use_dust { 1_000 } else { 1_000_000 }).0;
735 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 });
737 // The ChannelMonitor should always be the latest version, as we're required to persist it
738 // during the `commitment_signed_dance!()`.
739 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
741 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);
742 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
744 // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and
745 // force-close the channel.
746 check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager, [nodes[1].node.get_our_node_id()], 100000);
747 nodes[0].node.timer_tick_occurred();
748 assert!(nodes[0].node.list_channels().is_empty());
749 assert!(nodes[0].node.has_pending_payments());
750 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
751 check_added_monitors!(nodes[0], 1);
753 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
754 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
756 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
758 // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an
759 // error, as the channel has hit the chain.
760 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
761 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
763 let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
764 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
765 let as_err = nodes[0].node.get_and_clear_pending_msg_events();
766 assert_eq!(as_err.len(), 1);
767 let bs_commitment_tx;
769 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
770 assert_eq!(node_id, nodes[1].node.get_our_node_id());
771 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), msg);
772 check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(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())) }
773 , [nodes[0].node.get_our_node_id()], 100000);
774 check_added_monitors!(nodes[1], 1);
775 bs_commitment_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
777 _ => panic!("Unexpected event"),
779 check_closed_broadcast!(nodes[1], false);
781 // Now fail back the payment from nodes[2] to nodes[1]. This doesn't really matter as the
782 // previous hop channel is already on-chain, but it makes nodes[2] willing to see additional
783 // incoming HTLCs with the same payment hash later.
784 nodes[2].node.fail_htlc_backwards(&payment_hash);
785 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], [HTLCDestination::FailedPayment { payment_hash }]);
786 check_added_monitors!(nodes[2], 1);
788 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
789 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fail_htlcs[0]);
790 commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false);
791 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
792 [HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
794 // Connect the HTLC-Timeout transaction, timing out the HTLC on both nodes (but not confirming
795 // the HTLC-Timeout transaction beyond 1 conf). For dust HTLCs, the HTLC is considered resolved
796 // after the commitment transaction, so always connect the commitment transaction.
797 mine_transaction(&nodes[0], &bs_commitment_tx[0]);
798 mine_transaction(&nodes[1], &bs_commitment_tx[0]);
800 connect_blocks(&nodes[0], TEST_FINAL_CLTV + (MIN_CLTV_EXPIRY_DELTA as u32));
801 connect_blocks(&nodes[1], TEST_FINAL_CLTV + (MIN_CLTV_EXPIRY_DELTA as u32));
802 let as_htlc_timeout = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
803 check_spends!(as_htlc_timeout[0], bs_commitment_tx[0]);
804 assert_eq!(as_htlc_timeout.len(), 1);
806 mine_transaction(&nodes[0], &as_htlc_timeout[0]);
807 // nodes[0] may rebroadcast (or RBF-bump) its HTLC-Timeout, so wipe the announced set.
808 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
809 mine_transaction(&nodes[1], &as_htlc_timeout[0]);
812 // Create a new channel on which to retry the payment before we fail the payment via the
813 // HTLC-Timeout transaction. This avoids ChannelManager timing out the payment due to us
814 // connecting several blocks while creating the channel (implying time has passed).
815 // We do this with a zero-conf channel to avoid connecting blocks as a side-effect.
816 let (_, chan_id_3) = open_zero_conf_channel(&nodes[0], &nodes[1], None);
817 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
819 // If we attempt to retry prior to the HTLC-Timeout (or commitment transaction, for dust HTLCs)
820 // confirming, we will fail as it's considered still-pending...
821 let (new_route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], if use_dust { 1_000 } else { 1_000_000 });
822 match nodes[0].node.send_payment_with_route(&new_route, payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id) {
823 Err(PaymentSendFailure::DuplicatePayment) => {},
824 _ => panic!("Unexpected error")
826 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
828 // After ANTI_REORG_DELAY confirmations, the HTLC should be failed and we can try the payment
829 // again. We serialize the node first as we'll then test retrying the HTLC after a restart
830 // (which should also still work).
831 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
832 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
833 expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new());
835 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
836 let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode();
837 nodes_0_serialized = nodes[0].node.encode();
839 // After the payment failed, we're free to send it again.
840 assert!(nodes[0].node.send_payment_with_route(&new_route, payment_hash,
841 RecipientOnionFields::secret_only(payment_secret), payment_id).is_ok());
842 assert!(!nodes[0].node.get_and_clear_pending_msg_events().is_empty());
844 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);
845 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
847 nodes[0].node.test_process_background_events();
848 check_added_monitors(&nodes[0], 1);
850 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
851 reconnect_args.send_channel_ready = (true, true);
852 reconnect_nodes(reconnect_args);
854 // Now resend the payment, delivering the HTLC and actually claiming it this time. This ensures
855 // the payment is not (spuriously) listed as still pending.
856 assert!(nodes[0].node.send_payment_with_route(&new_route, payment_hash,
857 RecipientOnionFields::secret_only(payment_secret), payment_id).is_ok());
858 check_added_monitors!(nodes[0], 1);
859 pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], if use_dust { 1_000 } else { 1_000_000 }, payment_hash, payment_secret);
860 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
862 match nodes[0].node.send_payment_with_route(&new_route, payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id) {
863 Err(PaymentSendFailure::DuplicatePayment) => {},
864 _ => panic!("Unexpected error")
866 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
868 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
869 let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode();
870 nodes_0_serialized = nodes[0].node.encode();
872 // Check that after reload we can send the payment again (though we shouldn't, since it was
873 // claimed previously).
874 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);
875 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
877 nodes[0].node.test_process_background_events();
878 check_added_monitors(&nodes[0], 1);
880 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
882 match nodes[0].node.send_payment_with_route(&new_route, payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id) {
883 Err(PaymentSendFailure::DuplicatePayment) => {},
884 _ => panic!("Unexpected error")
886 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
890 fn test_completed_payment_not_retryable_on_reload() {
891 do_test_completed_payment_not_retryable_on_reload(true);
892 do_test_completed_payment_not_retryable_on_reload(false);
896 fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, confirm_commitment_tx: bool, payment_timeout: bool) {
897 // When a Channel is closed, any outbound HTLCs which were relayed through it are simply
898 // dropped when the Channel is. From there, the ChannelManager relies on the ChannelMonitor
899 // having a copy of the relevant fail-/claim-back data and processes the HTLC fail/claim when
900 // the ChannelMonitor tells it to.
902 // If, due to an on-chain event, an HTLC is failed/claimed, we should avoid providing the
903 // ChannelManager the HTLC event until after the monitor is re-persisted. This should prevent a
904 // duplicate HTLC fail/claim (e.g. via a PaymentPathFailed event).
905 let chanmon_cfgs = create_chanmon_cfgs(2);
906 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
908 let new_chain_monitor;
909 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
910 let nodes_0_deserialized;
911 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
913 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
915 // Route a payment, but force-close the channel before the HTLC fulfill message arrives at
917 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 10_000_000);
918 nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
919 check_closed_broadcast!(nodes[0], true);
920 check_added_monitors!(nodes[0], 1);
921 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
923 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
924 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
926 // Connect blocks until the CLTV timeout is up so that we get an HTLC-Timeout transaction
927 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
928 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
929 assert_eq!(node_txn.len(), 3);
930 assert_eq!(node_txn[0].txid(), node_txn[1].txid());
931 check_spends!(node_txn[1], funding_tx);
932 check_spends!(node_txn[2], node_txn[1]);
933 let timeout_txn = vec![node_txn[2].clone()];
935 nodes[1].node.claim_funds(payment_preimage);
936 check_added_monitors!(nodes[1], 1);
937 expect_payment_claimed!(nodes[1], payment_hash, 10_000_000);
939 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![node_txn[1].clone()]));
940 check_closed_broadcast!(nodes[1], true);
941 check_added_monitors!(nodes[1], 1);
942 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
943 let claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
944 assert_eq!(claim_txn.len(), 1);
945 check_spends!(claim_txn[0], node_txn[1]);
947 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![node_txn[1].clone()]));
949 if confirm_commitment_tx {
950 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
953 let claim_block = create_dummy_block(nodes[0].best_block_hash(), 42, if payment_timeout { timeout_txn } else { vec![claim_txn[0].clone()] });
956 assert!(confirm_commitment_tx); // Otherwise we're spending below our CSV!
957 connect_block(&nodes[0], &claim_block);
958 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
961 // Now connect the HTLC claim transaction with the ChainMonitor-generated ChannelMonitor update
962 // returning InProgress. This should cause the claim event to never make its way to the
964 chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap().clear();
965 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
968 connect_blocks(&nodes[0], 1);
970 connect_block(&nodes[0], &claim_block);
973 let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
974 let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
975 .get_mut(&funding_txo).unwrap().drain().collect();
976 // If we are using chain::Confirm instead of chain::Listen, we will get the same update twice.
977 // If we're testing connection idempotency we may get substantially more.
978 assert!(mon_updates.len() >= 1);
979 assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
980 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
982 // If we persist the ChannelManager here, we should get the PaymentSent event after
984 let mut chan_manager_serialized = Vec::new();
985 if !persist_manager_post_event {
986 chan_manager_serialized = nodes[0].node.encode();
989 // Now persist the ChannelMonitor and inform the ChainMonitor that we're done, generating the
990 // payment sent event.
991 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
992 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
993 for update in mon_updates {
994 nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap();
997 expect_payment_failed!(nodes[0], payment_hash, false);
999 expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
1002 // If we persist the ChannelManager after we get the PaymentSent event, we shouldn't get it
1004 if persist_manager_post_event {
1005 chan_manager_serialized = nodes[0].node.encode();
1008 // Now reload nodes[0]...
1009 reload_node!(nodes[0], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
1011 if persist_manager_post_event {
1012 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1013 } else if payment_timeout {
1014 expect_payment_failed!(nodes[0], payment_hash, false);
1016 expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
1019 // Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
1020 // which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
1021 // payment events should kick in, leaving us with no pending events here.
1022 let height = nodes[0].blocks.lock().unwrap().len() as u32 - 1;
1023 nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, height);
1024 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1025 check_added_monitors(&nodes[0], 1);
1029 fn test_dup_htlc_onchain_fails_on_reload() {
1030 do_test_dup_htlc_onchain_fails_on_reload(true, true, true);
1031 do_test_dup_htlc_onchain_fails_on_reload(true, true, false);
1032 do_test_dup_htlc_onchain_fails_on_reload(true, false, false);
1033 do_test_dup_htlc_onchain_fails_on_reload(false, true, true);
1034 do_test_dup_htlc_onchain_fails_on_reload(false, true, false);
1035 do_test_dup_htlc_onchain_fails_on_reload(false, false, false);
1039 fn test_fulfill_restart_failure() {
1040 // When we receive an update_fulfill_htlc message, we immediately consider the HTLC fully
1041 // fulfilled. At this point, the peer can reconnect and decide to either fulfill the HTLC
1042 // again, or fail it, giving us free money.
1044 // Of course probably they won't fail it and give us free money, but because we have code to
1045 // handle it, we should test the logic for it anyway. We do that here.
1046 let chanmon_cfgs = create_chanmon_cfgs(2);
1047 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1049 let new_chain_monitor;
1050 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1051 let nodes_1_deserialized;
1052 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1054 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1055 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
1057 // The simplest way to get a failure after a fulfill is to reload nodes[1] from a state
1058 // pre-fulfill, which we do by serializing it here.
1059 let chan_manager_serialized = nodes[1].node.encode();
1060 let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id).encode();
1062 nodes[1].node.claim_funds(payment_preimage);
1063 check_added_monitors!(nodes[1], 1);
1064 expect_payment_claimed!(nodes[1], payment_hash, 100_000);
1066 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1067 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
1068 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
1070 // Now reload nodes[1]...
1071 reload_node!(nodes[1], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
1073 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
1074 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
1076 nodes[1].node.fail_htlc_backwards(&payment_hash);
1077 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
1078 check_added_monitors!(nodes[1], 1);
1079 let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1080 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
1081 commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, false);
1082 // nodes[0] shouldn't generate any events here, while it just got a payment failure completion
1083 // it had already considered the payment fulfilled, and now they just got free money.
1084 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1088 fn get_ldk_payment_preimage() {
1089 // Ensure that `ChannelManager::get_payment_preimage` can successfully be used to claim a payment.
1090 let chanmon_cfgs = create_chanmon_cfgs(2);
1091 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1092 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1093 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1094 create_announced_chan_between_nodes(&nodes, 0, 1);
1096 let amt_msat = 60_000;
1097 let expiry_secs = 60 * 60;
1098 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(amt_msat), expiry_secs, None).unwrap();
1100 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
1101 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
1102 let scorer = test_utils::TestScorer::new();
1103 let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
1104 let random_seed_bytes = keys_manager.get_secure_random_bytes();
1105 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
1106 let route = get_route( &nodes[0].node.get_our_node_id(), &route_params,
1107 &nodes[0].network_graph.read_only(),
1108 Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()), nodes[0].logger,
1109 &scorer, &(), &random_seed_bytes).unwrap();
1110 nodes[0].node.send_payment_with_route(&route, payment_hash,
1111 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1112 check_added_monitors!(nodes[0], 1);
1114 // Make sure to use `get_payment_preimage`
1115 let payment_preimage = nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1116 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1117 assert_eq!(events.len(), 1);
1118 pass_along_path(&nodes[0], &[&nodes[1]], amt_msat, payment_hash, Some(payment_secret), events.pop().unwrap(), true, Some(payment_preimage));
1119 claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, payment_preimage);
1123 fn sent_probe_is_probe_of_sending_node() {
1124 let chanmon_cfgs = create_chanmon_cfgs(3);
1125 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1126 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
1127 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1129 create_announced_chan_between_nodes(&nodes, 0, 1);
1130 create_announced_chan_between_nodes(&nodes, 1, 2);
1132 // First check we refuse to build a single-hop probe
1133 let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
1134 assert!(nodes[0].node.send_probe(route.paths[0].clone()).is_err());
1136 // Then build an actual two-hop probing path
1137 let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
1139 match nodes[0].node.send_probe(route.paths[0].clone()) {
1140 Ok((payment_hash, payment_id)) => {
1141 assert!(nodes[0].node.payment_is_probe(&payment_hash, &payment_id));
1142 assert!(!nodes[1].node.payment_is_probe(&payment_hash, &payment_id));
1143 assert!(!nodes[2].node.payment_is_probe(&payment_hash, &payment_id));
1148 get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1149 check_added_monitors!(nodes[0], 1);
1153 fn successful_probe_yields_event() {
1154 let chanmon_cfgs = create_chanmon_cfgs(3);
1155 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1156 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
1157 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1159 create_announced_chan_between_nodes(&nodes, 0, 1);
1160 create_announced_chan_between_nodes(&nodes, 1, 2);
1162 let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
1164 let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
1166 // node[0] -- update_add_htlcs -> node[1]
1167 check_added_monitors!(nodes[0], 1);
1168 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1169 let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
1170 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
1171 check_added_monitors!(nodes[1], 0);
1172 commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
1173 expect_pending_htlcs_forwardable!(nodes[1]);
1175 // node[1] -- update_add_htlcs -> node[2]
1176 check_added_monitors!(nodes[1], 1);
1177 let updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1178 let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
1179 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &probe_event.msgs[0]);
1180 check_added_monitors!(nodes[2], 0);
1181 commitment_signed_dance!(nodes[2], nodes[1], probe_event.commitment_msg, true, true);
1183 // node[1] <- update_fail_htlcs -- node[2]
1184 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1185 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1186 check_added_monitors!(nodes[1], 0);
1187 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, true);
1189 // node[0] <- update_fail_htlcs -- node[1]
1190 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1191 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1192 check_added_monitors!(nodes[0], 0);
1193 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
1195 let mut events = nodes[0].node.get_and_clear_pending_events();
1196 assert_eq!(events.len(), 1);
1197 match events.drain(..).next().unwrap() {
1198 crate::events::Event::ProbeSuccessful { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
1199 assert_eq!(payment_id, ev_pid);
1200 assert_eq!(payment_hash, ev_ph);
1204 assert!(!nodes[0].node.has_pending_payments());
1208 fn failed_probe_yields_event() {
1209 let chanmon_cfgs = create_chanmon_cfgs(3);
1210 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1211 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None, None]);
1212 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1214 create_announced_chan_between_nodes(&nodes, 0, 1);
1215 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 90000000);
1217 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
1219 let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], payment_params, 9_998_000);
1221 let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
1223 // node[0] -- update_add_htlcs -> node[1]
1224 check_added_monitors!(nodes[0], 1);
1225 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1226 let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
1227 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
1228 check_added_monitors!(nodes[1], 0);
1229 commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
1230 expect_pending_htlcs_forwardable!(nodes[1]);
1232 // node[0] <- update_fail_htlcs -- node[1]
1233 check_added_monitors!(nodes[1], 1);
1234 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1235 // Skip the PendingHTLCsForwardable event
1236 let _events = nodes[1].node.get_and_clear_pending_events();
1237 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1238 check_added_monitors!(nodes[0], 0);
1239 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
1241 let mut events = nodes[0].node.get_and_clear_pending_events();
1242 assert_eq!(events.len(), 1);
1243 match events.drain(..).next().unwrap() {
1244 crate::events::Event::ProbeFailed { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
1245 assert_eq!(payment_id, ev_pid);
1246 assert_eq!(payment_hash, ev_ph);
1250 assert!(!nodes[0].node.has_pending_payments());
1254 fn onchain_failed_probe_yields_event() {
1255 // Tests that an attempt to probe over a channel that is eventaully closed results in a failure
1257 let chanmon_cfgs = create_chanmon_cfgs(3);
1258 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1259 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1260 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1262 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1263 create_announced_chan_between_nodes(&nodes, 1, 2);
1265 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
1267 // Send a dust HTLC, which will be treated as if it timed out once the channel hits the chain.
1268 let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], payment_params, 1_000);
1269 let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
1271 // node[0] -- update_add_htlcs -> node[1]
1272 check_added_monitors!(nodes[0], 1);
1273 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1274 let probe_event = SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), updates);
1275 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
1276 check_added_monitors!(nodes[1], 0);
1277 commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
1278 expect_pending_htlcs_forwardable!(nodes[1]);
1280 check_added_monitors!(nodes[1], 1);
1281 let _ = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1283 // Don't bother forwarding the HTLC onwards and just confirm the force-close transaction on
1284 // Node A, which after 6 confirmations should result in a probe failure event.
1285 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id);
1286 confirm_transaction(&nodes[0], &bs_txn[0]);
1287 check_closed_broadcast!(&nodes[0], true);
1288 check_added_monitors!(nodes[0], 1);
1290 let mut events = nodes[0].node.get_and_clear_pending_events();
1291 assert_eq!(events.len(), 2);
1292 let mut found_probe_failed = false;
1293 for event in events.drain(..) {
1295 Event::ProbeFailed { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
1296 assert_eq!(payment_id, ev_pid);
1297 assert_eq!(payment_hash, ev_ph);
1298 found_probe_failed = true;
1300 Event::ChannelClosed { .. } => {},
1304 assert!(found_probe_failed);
1305 assert!(!nodes[0].node.has_pending_payments());
1309 fn preflight_probes_yield_event_and_skip() {
1310 let chanmon_cfgs = create_chanmon_cfgs(5);
1311 let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
1313 // We alleviate the HTLC max-in-flight limit, as otherwise we'd always be limited through that.
1314 let mut no_htlc_limit_config = test_default_channel_config();
1315 no_htlc_limit_config.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 100;
1317 let user_configs = std::iter::repeat(no_htlc_limit_config).take(5).map(|c| Some(c)).collect::<Vec<Option<UserConfig>>>();
1318 let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &user_configs);
1319 let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
1321 // Setup channel topology:
1322 // (30k:0)- N2 -(1M:0)
1324 // N0 -(100k:0)-> N1 N4
1326 // (70k:0)- N3 -(1M:0)
1328 let first_chan_update = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0).0;
1329 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 30_000, 0);
1330 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 70_000, 0);
1331 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 1_000_000, 0);
1332 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 1_000_000, 0);
1334 let mut invoice_features = Bolt11InvoiceFeatures::empty();
1335 invoice_features.set_basic_mpp_optional();
1337 let mut payment_params = PaymentParameters::from_node_id(nodes[4].node.get_our_node_id(), TEST_FINAL_CLTV)
1338 .with_bolt11_features(invoice_features).unwrap();
1340 let route_params = RouteParameters { payment_params, final_value_msat: 80_000_000 };
1341 let res = nodes[0].node.send_preflight_probes(route_params, None).unwrap();
1343 // We check that only one probe was sent, the other one was skipped due to limited liquidity.
1344 assert_eq!(res.len(), 1);
1345 let log_msg = format!("Skipped sending payment probe to avoid putting channel {} under the liquidity limit.",
1346 first_chan_update.contents.short_channel_id);
1347 node_cfgs[0].logger.assert_log_contains("lightning::ln::channelmanager", &log_msg, 1);
1349 let (payment_hash, payment_id) = res.first().unwrap();
1351 // node[0] -- update_add_htlcs -> node[1]
1352 check_added_monitors!(nodes[0], 1);
1353 let probe_event = SendEvent::from_node(&nodes[0]);
1354 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &probe_event.msgs[0]);
1355 check_added_monitors!(nodes[1], 0);
1356 commitment_signed_dance!(nodes[1], nodes[0], probe_event.commitment_msg, false);
1357 expect_pending_htlcs_forwardable!(nodes[1]);
1359 // node[1] -- update_add_htlcs -> node[2]
1360 check_added_monitors!(nodes[1], 1);
1361 let probe_event = SendEvent::from_node(&nodes[1]);
1362 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &probe_event.msgs[0]);
1363 check_added_monitors!(nodes[2], 0);
1364 commitment_signed_dance!(nodes[2], nodes[1], probe_event.commitment_msg, false);
1365 expect_pending_htlcs_forwardable!(nodes[2]);
1367 // node[2] -- update_add_htlcs -> node[4]
1368 check_added_monitors!(nodes[2], 1);
1369 let probe_event = SendEvent::from_node(&nodes[2]);
1370 nodes[4].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &probe_event.msgs[0]);
1371 check_added_monitors!(nodes[4], 0);
1372 commitment_signed_dance!(nodes[4], nodes[2], probe_event.commitment_msg, true, true);
1374 // node[2] <- update_fail_htlcs -- node[4]
1375 let updates = get_htlc_update_msgs!(nodes[4], nodes[2].node.get_our_node_id());
1376 nodes[2].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1377 check_added_monitors!(nodes[2], 0);
1378 commitment_signed_dance!(nodes[2], nodes[4], updates.commitment_signed, true);
1380 // node[1] <- update_fail_htlcs -- node[2]
1381 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1382 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1383 check_added_monitors!(nodes[1], 0);
1384 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, true);
1386 // node[0] <- update_fail_htlcs -- node[1]
1387 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1388 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
1389 check_added_monitors!(nodes[0], 0);
1390 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
1392 let mut events = nodes[0].node.get_and_clear_pending_events();
1393 assert_eq!(events.len(), 1);
1394 match events.drain(..).next().unwrap() {
1395 crate::events::Event::ProbeSuccessful { payment_id: ev_pid, payment_hash: ev_ph, .. } => {
1396 assert_eq!(*payment_id, ev_pid);
1397 assert_eq!(*payment_hash, ev_ph);
1401 assert!(!nodes[0].node.has_pending_payments());
1405 fn claimed_send_payment_idempotent() {
1406 // Tests that `send_payment` (and friends) are (reasonably) idempotent.
1407 let chanmon_cfgs = create_chanmon_cfgs(2);
1408 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1409 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1410 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1412 create_announced_chan_between_nodes(&nodes, 0, 1).2;
1414 let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
1415 let (first_payment_preimage, _, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
1417 macro_rules! check_send_rejected {
1419 // If we try to resend a new payment with a different payment_hash but with the same
1420 // payment_id, it should be rejected.
1421 let send_result = nodes[0].node.send_payment_with_route(&route, second_payment_hash,
1422 RecipientOnionFields::secret_only(second_payment_secret), payment_id);
1424 Err(PaymentSendFailure::DuplicatePayment) => {},
1425 _ => panic!("Unexpected send result: {:?}", send_result),
1428 // Further, if we try to send a spontaneous payment with the same payment_id it should
1429 // also be rejected.
1430 let send_result = nodes[0].node.send_spontaneous_payment(
1431 &route, None, RecipientOnionFields::spontaneous_empty(), payment_id);
1433 Err(PaymentSendFailure::DuplicatePayment) => {},
1434 _ => panic!("Unexpected send result: {:?}", send_result),
1439 check_send_rejected!();
1441 // Claim the payment backwards, but note that the PaymentSent event is still pending and has
1442 // not been seen by the user. At this point, from the user perspective nothing has changed, so
1443 // we must remain just as idempotent as we were before.
1444 do_claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, first_payment_preimage);
1446 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1447 nodes[0].node.timer_tick_occurred();
1450 check_send_rejected!();
1452 // Once the user sees and handles the `PaymentSent` event, we expect them to no longer call
1453 // `send_payment`, and our idempotency guarantees are off - they should have atomically marked
1454 // the payment complete. However, they could have called `send_payment` while the event was
1455 // being processed, leading to a race in our idempotency guarantees. Thus, even immediately
1456 // after the event is handled a duplicate payment should sitll be rejected.
1457 expect_payment_sent!(&nodes[0], first_payment_preimage, Some(0));
1458 check_send_rejected!();
1460 // If relatively little time has passed, a duplicate payment should still fail.
1461 nodes[0].node.timer_tick_occurred();
1462 check_send_rejected!();
1464 // However, after some time has passed (at least more than the one timer tick above), a
1465 // duplicate payment should go through, as ChannelManager should no longer have any remaining
1466 // references to the old payment data.
1467 for _ in 0..IDEMPOTENCY_TIMEOUT_TICKS {
1468 nodes[0].node.timer_tick_occurred();
1471 nodes[0].node.send_payment_with_route(&route, second_payment_hash,
1472 RecipientOnionFields::secret_only(second_payment_secret), payment_id).unwrap();
1473 check_added_monitors!(nodes[0], 1);
1474 pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
1475 claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
1479 fn abandoned_send_payment_idempotent() {
1480 // Tests that `send_payment` (and friends) allow duplicate PaymentIds immediately after
1482 let chanmon_cfgs = create_chanmon_cfgs(2);
1483 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1484 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1485 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1487 create_announced_chan_between_nodes(&nodes, 0, 1).2;
1489 let (route, second_payment_hash, second_payment_preimage, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
1490 let (_, first_payment_hash, _, payment_id) = send_along_route(&nodes[0], route.clone(), &[&nodes[1]], 100_000);
1492 macro_rules! check_send_rejected {
1494 // If we try to resend a new payment with a different payment_hash but with the same
1495 // payment_id, it should be rejected.
1496 let send_result = nodes[0].node.send_payment_with_route(&route, second_payment_hash,
1497 RecipientOnionFields::secret_only(second_payment_secret), payment_id);
1499 Err(PaymentSendFailure::DuplicatePayment) => {},
1500 _ => panic!("Unexpected send result: {:?}", send_result),
1503 // Further, if we try to send a spontaneous payment with the same payment_id it should
1504 // also be rejected.
1505 let send_result = nodes[0].node.send_spontaneous_payment(
1506 &route, None, RecipientOnionFields::spontaneous_empty(), payment_id);
1508 Err(PaymentSendFailure::DuplicatePayment) => {},
1509 _ => panic!("Unexpected send result: {:?}", send_result),
1514 check_send_rejected!();
1516 nodes[1].node.fail_htlc_backwards(&first_payment_hash);
1517 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
1519 // Until we abandon the payment upon path failure, no matter how many timer ticks pass, we still cannot reuse the
1521 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1522 nodes[0].node.timer_tick_occurred();
1524 check_send_rejected!();
1526 pass_failed_payment_back(&nodes[0], &[&[&nodes[1]]], false, first_payment_hash, PaymentFailureReason::RecipientRejected);
1528 // However, we can reuse the PaymentId immediately after we `abandon_payment` upon passing the
1529 // failed payment back.
1530 nodes[0].node.send_payment_with_route(&route, second_payment_hash,
1531 RecipientOnionFields::secret_only(second_payment_secret), payment_id).unwrap();
1532 check_added_monitors!(nodes[0], 1);
1533 pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
1534 claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
1537 #[derive(PartialEq)]
1538 enum InterceptTest {
1545 fn test_trivial_inflight_htlc_tracking(){
1546 // In this test, we test three scenarios:
1547 // (1) Sending + claiming a payment successfully should return `None` when querying InFlightHtlcs
1548 // (2) Sending a payment without claiming it should return the payment's value (500000) when querying InFlightHtlcs
1549 // (3) After we claim the payment sent in (2), InFlightHtlcs should return `None` for the query.
1550 let chanmon_cfgs = create_chanmon_cfgs(3);
1551 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1552 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1553 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1555 let (_, _, chan_1_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
1556 let (_, _, chan_2_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
1558 // Send and claim the payment. Inflight HTLCs should be empty.
1559 let (_, payment_hash, _, payment_id) = send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 500000);
1560 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1562 let mut node_0_per_peer_lock;
1563 let mut node_0_peer_state_lock;
1564 let channel_1 = get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1566 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1567 &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1568 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1569 channel_1.context().get_short_channel_id().unwrap()
1571 assert_eq!(chan_1_used_liquidity, None);
1574 let mut node_1_per_peer_lock;
1575 let mut node_1_peer_state_lock;
1576 let channel_2 = get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1578 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1579 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1580 &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1581 channel_2.context().get_short_channel_id().unwrap()
1584 assert_eq!(chan_2_used_liquidity, None);
1586 let pending_payments = nodes[0].node.list_recent_payments();
1587 assert_eq!(pending_payments.len(), 1);
1588 assert_eq!(pending_payments[0], RecentPaymentDetails::Fulfilled { payment_hash: Some(payment_hash), payment_id });
1590 // Remove fulfilled payment
1591 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1592 nodes[0].node.timer_tick_occurred();
1595 // Send the payment, but do not claim it. Our inflight HTLCs should contain the pending payment.
1596 let (payment_preimage, payment_hash, _, payment_id) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 500000);
1597 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1599 let mut node_0_per_peer_lock;
1600 let mut node_0_peer_state_lock;
1601 let channel_1 = get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1603 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1604 &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1605 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1606 channel_1.context().get_short_channel_id().unwrap()
1608 // First hop accounts for expected 1000 msat fee
1609 assert_eq!(chan_1_used_liquidity, Some(501000));
1612 let mut node_1_per_peer_lock;
1613 let mut node_1_peer_state_lock;
1614 let channel_2 = get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1616 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1617 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1618 &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1619 channel_2.context().get_short_channel_id().unwrap()
1622 assert_eq!(chan_2_used_liquidity, Some(500000));
1624 let pending_payments = nodes[0].node.list_recent_payments();
1625 assert_eq!(pending_payments.len(), 1);
1626 assert_eq!(pending_payments[0], RecentPaymentDetails::Pending { payment_id, payment_hash, total_msat: 500000 });
1628 // Now, let's claim the payment. This should result in the used liquidity to return `None`.
1629 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
1631 // Remove fulfilled payment
1632 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
1633 nodes[0].node.timer_tick_occurred();
1636 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1638 let mut node_0_per_peer_lock;
1639 let mut node_0_peer_state_lock;
1640 let channel_1 = get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1_id);
1642 let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
1643 &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1644 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1645 channel_1.context().get_short_channel_id().unwrap()
1647 assert_eq!(chan_1_used_liquidity, None);
1650 let mut node_1_per_peer_lock;
1651 let mut node_1_peer_state_lock;
1652 let channel_2 = get_channel_ref!(&nodes[1], nodes[2], node_1_per_peer_lock, node_1_peer_state_lock, chan_2_id);
1654 let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
1655 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
1656 &NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1657 channel_2.context().get_short_channel_id().unwrap()
1659 assert_eq!(chan_2_used_liquidity, None);
1662 let pending_payments = nodes[0].node.list_recent_payments();
1663 assert_eq!(pending_payments.len(), 0);
1667 fn test_holding_cell_inflight_htlcs() {
1668 let chanmon_cfgs = create_chanmon_cfgs(2);
1669 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1670 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1671 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1672 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1674 let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1675 let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
1677 // Queue up two payments - one will be delivered right away, one immediately goes into the
1678 // holding cell as nodes[0] is AwaitingRAA.
1680 nodes[0].node.send_payment_with_route(&route, payment_hash_1,
1681 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
1682 check_added_monitors!(nodes[0], 1);
1683 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
1684 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1685 check_added_monitors!(nodes[0], 0);
1688 let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1691 let mut node_0_per_peer_lock;
1692 let mut node_0_peer_state_lock;
1693 let channel = get_channel_ref!(&nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id);
1695 let used_liquidity = inflight_htlcs.used_liquidity_msat(
1696 &NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1697 &NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1698 channel.context().get_short_channel_id().unwrap()
1701 assert_eq!(used_liquidity, Some(2000000));
1704 // Clear pending events so test doesn't throw a "Had excess message on node..." error
1705 nodes[0].node.get_and_clear_pending_msg_events();
1709 fn intercepted_payment() {
1710 // Test that detecting an intercept scid on payment forward will signal LDK to generate an
1711 // intercept event, which the LSP can then use to either (a) open a JIT channel to forward the
1712 // payment or (b) fail the payment.
1713 do_test_intercepted_payment(InterceptTest::Forward);
1714 do_test_intercepted_payment(InterceptTest::Fail);
1715 // Make sure that intercepted payments will be automatically failed back if too many blocks pass.
1716 do_test_intercepted_payment(InterceptTest::Timeout);
1719 fn do_test_intercepted_payment(test: InterceptTest) {
1720 let chanmon_cfgs = create_chanmon_cfgs(3);
1721 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1723 let mut zero_conf_chan_config = test_default_channel_config();
1724 zero_conf_chan_config.manually_accept_inbound_channels = true;
1725 let mut intercept_forwards_config = test_default_channel_config();
1726 intercept_forwards_config.accept_intercept_htlcs = true;
1727 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), Some(zero_conf_chan_config)]);
1729 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1730 let scorer = test_utils::TestScorer::new();
1731 let random_seed_bytes = chanmon_cfgs[0].keys_manager.get_secure_random_bytes();
1733 let _ = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1735 let amt_msat = 100_000;
1736 let intercept_scid = nodes[1].node.get_intercept_scid();
1737 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1738 .with_route_hints(vec![
1739 RouteHint(vec![RouteHintHop {
1740 src_node_id: nodes[1].node.get_our_node_id(),
1741 short_channel_id: intercept_scid,
1744 proportional_millionths: 0,
1746 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
1747 htlc_minimum_msat: None,
1748 htlc_maximum_msat: None,
1751 .with_bolt11_features(nodes[2].node.invoice_features()).unwrap();
1752 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat,);
1753 let route = get_route( &nodes[0].node.get_our_node_id(), &route_params,
1754 &nodes[0].network_graph.read_only(), None, nodes[0].logger, &scorer, &(),
1755 &random_seed_bytes,).unwrap();
1757 let (payment_hash, payment_secret) = nodes[2].node.create_inbound_payment(Some(amt_msat), 60 * 60, None).unwrap();
1758 nodes[0].node.send_payment_with_route(&route, payment_hash,
1759 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1760 let payment_event = {
1762 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
1763 assert_eq!(added_monitors.len(), 1);
1764 added_monitors.clear();
1766 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1767 assert_eq!(events.len(), 1);
1768 SendEvent::from_event(events.remove(0))
1770 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1771 commitment_signed_dance!(nodes[1], nodes[0], &payment_event.commitment_msg, false, true);
1773 // Check that we generate the PaymentIntercepted event when an intercept forward is detected.
1774 let events = nodes[1].node.get_and_clear_pending_events();
1775 assert_eq!(events.len(), 1);
1776 let (intercept_id, expected_outbound_amount_msat) = match events[0] {
1777 crate::events::Event::HTLCIntercepted {
1778 intercept_id, expected_outbound_amount_msat, payment_hash: pmt_hash, inbound_amount_msat, requested_next_hop_scid: short_channel_id
1780 assert_eq!(pmt_hash, payment_hash);
1781 assert_eq!(inbound_amount_msat, route.get_total_amount() + route.get_total_fees());
1782 assert_eq!(short_channel_id, intercept_scid);
1783 (intercept_id, expected_outbound_amount_msat)
1788 // Check for unknown channel id error.
1789 let unknown_chan_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &ChannelId::from_bytes([42; 32]), nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1790 assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable {
1791 err: format!("Channel with id {} not found for the passed counterparty node_id {}.",
1792 log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });
1794 if test == InterceptTest::Fail {
1795 // Ensure we can fail the intercepted payment back.
1796 nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
1797 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
1798 nodes[1].node.process_pending_htlc_forwards();
1799 let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1800 check_added_monitors!(&nodes[1], 1);
1801 assert!(update_fail.update_fail_htlcs.len() == 1);
1802 let fail_msg = update_fail.update_fail_htlcs[0].clone();
1803 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
1804 commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
1806 // Ensure the payment fails with the expected error.
1807 let fail_conditions = PaymentFailedConditions::new()
1808 .blamed_scid(intercept_scid)
1809 .blamed_chan_closed(true)
1810 .expected_htlc_error_data(0x4000 | 10, &[]);
1811 expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
1812 } else if test == InterceptTest::Forward {
1813 // Check that we'll fail as expected when sending to a channel that isn't in `ChannelReady` yet.
1814 let temp_chan_id = nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
1815 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();
1816 assert_eq!(unusable_chan_err , APIError::ChannelUnavailable {
1817 err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
1818 temp_chan_id, nodes[2].node.get_our_node_id()) });
1819 assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
1821 // Open the just-in-time channel so the payment can then be forwarded.
1822 let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1824 // Finally, forward the intercepted payment through and claim it.
1825 nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
1826 expect_pending_htlcs_forwardable!(nodes[1]);
1828 let payment_event = {
1830 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1831 assert_eq!(added_monitors.len(), 1);
1832 added_monitors.clear();
1834 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1835 assert_eq!(events.len(), 1);
1836 SendEvent::from_event(events.remove(0))
1838 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1839 commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1840 expect_pending_htlcs_forwardable!(nodes[2]);
1842 let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1843 expect_payment_claimable!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage), nodes[2].node.get_our_node_id());
1844 do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1845 let events = nodes[0].node.get_and_clear_pending_events();
1846 assert_eq!(events.len(), 2);
1848 Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1849 assert_eq!(payment_preimage, *ev_preimage);
1850 assert_eq!(payment_hash, *ev_hash);
1851 assert_eq!(fee_paid_msat, &Some(1000));
1853 _ => panic!("Unexpected event")
1856 Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1857 assert_eq!(hash, Some(payment_hash));
1859 _ => panic!("Unexpected event")
1861 check_added_monitors(&nodes[0], 1);
1862 } else if test == InterceptTest::Timeout {
1863 let mut block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
1864 connect_block(&nodes[0], &block);
1865 connect_block(&nodes[1], &block);
1866 for _ in 0..TEST_FINAL_CLTV {
1867 block.header.prev_blockhash = block.block_hash();
1868 connect_block(&nodes[0], &block);
1869 connect_block(&nodes[1], &block);
1871 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::InvalidForward { requested_forward_scid: intercept_scid }]);
1872 check_added_monitors!(nodes[1], 1);
1873 let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1874 assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
1875 assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
1876 assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
1877 assert!(htlc_timeout_updates.update_fee.is_none());
1879 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
1880 commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
1881 expect_payment_failed!(nodes[0], payment_hash, false, 0x2000 | 2, []);
1883 // Check for unknown intercept id error.
1884 let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1885 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();
1886 assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0)) });
1887 let unknown_intercept_id_err = nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap_err();
1888 assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.0)) });
1893 fn accept_underpaying_htlcs_config() {
1894 do_accept_underpaying_htlcs_config(1);
1895 do_accept_underpaying_htlcs_config(2);
1896 do_accept_underpaying_htlcs_config(3);
1899 fn do_accept_underpaying_htlcs_config(num_mpp_parts: usize) {
1900 let chanmon_cfgs = create_chanmon_cfgs(3);
1901 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1902 let mut intercept_forwards_config = test_default_channel_config();
1903 intercept_forwards_config.accept_intercept_htlcs = true;
1904 let mut underpay_config = test_default_channel_config();
1905 underpay_config.channel_config.accept_underpaying_htlcs = true;
1906 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), Some(underpay_config)]);
1907 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1909 let mut chan_ids = Vec::new();
1910 for _ in 0..num_mpp_parts {
1911 let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000, 0);
1912 let channel_id = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 2_000_000, 0).0.channel_id;
1913 chan_ids.push(channel_id);
1916 // Send the initial payment.
1917 let amt_msat = 900_000;
1918 let skimmed_fee_msat = 20;
1919 let mut route_hints = Vec::new();
1920 for _ in 0..num_mpp_parts {
1921 route_hints.push(RouteHint(vec![RouteHintHop {
1922 src_node_id: nodes[1].node.get_our_node_id(),
1923 short_channel_id: nodes[1].node.get_intercept_scid(),
1926 proportional_millionths: 0,
1928 cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
1929 htlc_minimum_msat: None,
1930 htlc_maximum_msat: Some(amt_msat / num_mpp_parts as u64 + 5),
1933 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1934 .with_route_hints(route_hints).unwrap()
1935 .with_bolt11_features(nodes[2].node.invoice_features()).unwrap();
1936 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
1937 let (payment_hash, payment_secret) = nodes[2].node.create_inbound_payment(Some(amt_msat), 60 * 60, None).unwrap();
1938 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
1939 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
1940 check_added_monitors!(nodes[0], num_mpp_parts); // one monitor per path
1941 let mut events: Vec<SendEvent> = nodes[0].node.get_and_clear_pending_msg_events().into_iter().map(|e| SendEvent::from_event(e)).collect();
1942 assert_eq!(events.len(), num_mpp_parts);
1944 // Forward the intercepted payments.
1945 for (idx, ev) in events.into_iter().enumerate() {
1946 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &ev.msgs[0]);
1947 do_commitment_signed_dance(&nodes[1], &nodes[0], &ev.commitment_msg, false, true);
1949 let events = nodes[1].node.get_and_clear_pending_events();
1950 assert_eq!(events.len(), 1);
1951 let (intercept_id, expected_outbound_amt_msat) = match events[0] {
1952 crate::events::Event::HTLCIntercepted {
1953 intercept_id, expected_outbound_amount_msat, payment_hash: pmt_hash, ..
1955 assert_eq!(pmt_hash, payment_hash);
1956 (intercept_id, expected_outbound_amount_msat)
1960 nodes[1].node.forward_intercepted_htlc(intercept_id, &chan_ids[idx],
1961 nodes[2].node.get_our_node_id(), expected_outbound_amt_msat - skimmed_fee_msat).unwrap();
1962 expect_pending_htlcs_forwardable!(nodes[1]);
1963 let payment_event = {
1965 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1966 assert_eq!(added_monitors.len(), 1);
1967 added_monitors.clear();
1969 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1970 assert_eq!(events.len(), 1);
1971 SendEvent::from_event(events.remove(0))
1973 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1974 do_commitment_signed_dance(&nodes[2], &nodes[1], &payment_event.commitment_msg, false, true);
1975 if idx == num_mpp_parts - 1 {
1976 expect_pending_htlcs_forwardable!(nodes[2]);
1980 // Claim the payment and check that the skimmed fee is as expected.
1981 let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1982 let events = nodes[2].node.get_and_clear_pending_events();
1983 assert_eq!(events.len(), 1);
1985 crate::events::Event::PaymentClaimable {
1986 ref payment_hash, ref purpose, amount_msat, counterparty_skimmed_fee_msat, receiver_node_id, ..
1988 assert_eq!(payment_hash, payment_hash);
1989 assert_eq!(amt_msat - skimmed_fee_msat * num_mpp_parts as u64, amount_msat);
1990 assert_eq!(skimmed_fee_msat * num_mpp_parts as u64, counterparty_skimmed_fee_msat);
1991 assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
1993 crate::events::PaymentPurpose::InvoicePayment { payment_preimage: ev_payment_preimage,
1994 payment_secret: ev_payment_secret, .. } =>
1996 assert_eq!(payment_preimage, ev_payment_preimage.unwrap());
1997 assert_eq!(payment_secret, *ev_payment_secret);
2002 _ => panic!("Unexpected event"),
2004 let mut expected_paths_vecs = Vec::new();
2005 let mut expected_paths = Vec::new();
2006 for _ in 0..num_mpp_parts { expected_paths_vecs.push(vec!(&nodes[1], &nodes[2])); }
2007 for i in 0..num_mpp_parts { expected_paths.push(&expected_paths_vecs[i][..]); }
2008 let total_fee_msat = do_claim_payment_along_route_with_extra_penultimate_hop_fees(
2009 &nodes[0], &expected_paths[..], &vec![skimmed_fee_msat as u32; num_mpp_parts][..], false,
2011 // The sender doesn't know that the penultimate hop took an extra fee.
2012 expect_payment_sent(&nodes[0], payment_preimage,
2013 Some(Some(total_fee_msat - skimmed_fee_msat * num_mpp_parts as u64)), true, true);
2016 #[derive(PartialEq)]
2027 fn automatic_retries() {
2028 do_automatic_retries(AutoRetry::Success);
2029 do_automatic_retries(AutoRetry::Spontaneous);
2030 do_automatic_retries(AutoRetry::FailAttempts);
2031 do_automatic_retries(AutoRetry::FailTimeout);
2032 do_automatic_retries(AutoRetry::FailOnRestart);
2033 do_automatic_retries(AutoRetry::FailOnRetry);
2035 fn do_automatic_retries(test: AutoRetry) {
2036 // Test basic automatic payment retries in ChannelManager. See individual `test` variant comments
2038 let chanmon_cfgs = create_chanmon_cfgs(3);
2039 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2041 let new_chain_monitor;
2043 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2044 let node_0_deserialized;
2046 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2047 let channel_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
2048 let channel_id_2 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
2050 // Marshall data to send the payment
2051 #[cfg(feature = "std")]
2052 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2053 #[cfg(not(feature = "std"))]
2054 let payment_expiry_secs = 60 * 60;
2055 let amt_msat = 1000;
2056 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2057 invoice_features.set_variable_length_onion_required();
2058 invoice_features.set_payment_secret_required();
2059 invoice_features.set_basic_mpp_optional();
2060 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
2061 .with_expiry_time(payment_expiry_secs as u64)
2062 .with_bolt11_features(invoice_features).unwrap();
2063 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2064 let (_, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
2066 macro_rules! pass_failed_attempt_with_retry_along_path {
2067 ($failing_channel_id: expr, $expect_pending_htlcs_forwardable: expr) => {
2068 // Send a payment attempt that fails due to lack of liquidity on the second hop
2069 check_added_monitors!(nodes[0], 1);
2070 let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2071 let mut update_add = update_0.update_add_htlcs[0].clone();
2072 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
2073 commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
2074 expect_pending_htlcs_forwardable_ignore!(nodes[1]);
2075 nodes[1].node.process_pending_htlc_forwards();
2076 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1],
2077 vec![HTLCDestination::NextHopChannel {
2078 node_id: Some(nodes[2].node.get_our_node_id()),
2079 channel_id: $failing_channel_id,
2081 nodes[1].node.process_pending_htlc_forwards();
2082 let update_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2083 check_added_monitors!(&nodes[1], 1);
2084 assert!(update_1.update_fail_htlcs.len() == 1);
2085 let fail_msg = update_1.update_fail_htlcs[0].clone();
2086 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
2087 commitment_signed_dance!(nodes[0], nodes[1], update_1.commitment_signed, false);
2089 // Ensure the attempt fails and a new PendingHTLCsForwardable event is generated for the retry
2090 let mut events = nodes[0].node.get_and_clear_pending_events();
2091 assert_eq!(events.len(), 2);
2093 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
2094 assert_eq!(payment_hash, ev_payment_hash);
2095 assert_eq!(payment_failed_permanently, false);
2097 _ => panic!("Unexpected event"),
2099 if $expect_pending_htlcs_forwardable {
2101 Event::PendingHTLCsForwardable { .. } => {},
2102 _ => panic!("Unexpected event"),
2106 Event::PaymentFailed { payment_hash: ev_payment_hash, .. } => {
2107 assert_eq!(payment_hash, ev_payment_hash);
2109 _ => panic!("Unexpected event"),
2115 if test == AutoRetry::Success {
2116 // Test that we can succeed on the first retry.
2117 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2118 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2119 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2121 // Open a new channel with liquidity on the second hop so we can find a route for the retry
2122 // attempt, since the initial second hop channel will be excluded from pathfinding
2123 create_announced_chan_between_nodes(&nodes, 1, 2);
2125 // We retry payments in `process_pending_htlc_forwards`
2126 nodes[0].node.process_pending_htlc_forwards();
2127 check_added_monitors!(nodes[0], 1);
2128 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2129 assert_eq!(msg_events.len(), 1);
2130 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, Some(payment_secret), msg_events.pop().unwrap(), true, None);
2131 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
2132 } else if test == AutoRetry::Spontaneous {
2133 nodes[0].node.send_spontaneous_payment_with_retry(Some(payment_preimage),
2134 RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params,
2135 Retry::Attempts(1)).unwrap();
2136 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2138 // Open a new channel with liquidity on the second hop so we can find a route for the retry
2139 // attempt, since the initial second hop channel will be excluded from pathfinding
2140 create_announced_chan_between_nodes(&nodes, 1, 2);
2142 // We retry payments in `process_pending_htlc_forwards`
2143 nodes[0].node.process_pending_htlc_forwards();
2144 check_added_monitors!(nodes[0], 1);
2145 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2146 assert_eq!(msg_events.len(), 1);
2147 pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, None, msg_events.pop().unwrap(), true, Some(payment_preimage));
2148 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
2149 } else if test == AutoRetry::FailAttempts {
2150 // Ensure ChannelManager will not retry a payment if it has run out of payment attempts.
2151 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2152 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2153 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2155 // Open a new channel with no liquidity on the second hop so we can find a (bad) route for
2156 // the retry attempt, since the initial second hop channel will be excluded from pathfinding
2157 let channel_id_3 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
2159 // We retry payments in `process_pending_htlc_forwards`
2160 nodes[0].node.process_pending_htlc_forwards();
2161 pass_failed_attempt_with_retry_along_path!(channel_id_3, false);
2163 // Ensure we won't retry a second time.
2164 nodes[0].node.process_pending_htlc_forwards();
2165 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2166 assert_eq!(msg_events.len(), 0);
2167 } else if test == AutoRetry::FailTimeout {
2168 #[cfg(not(feature = "no-std"))] {
2169 // Ensure ChannelManager will not retry a payment if it times out due to Retry::Timeout.
2170 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2171 PaymentId(payment_hash.0), route_params, Retry::Timeout(Duration::from_secs(60))).unwrap();
2172 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2174 // Advance the time so the second attempt fails due to timeout.
2175 SinceEpoch::advance(Duration::from_secs(61));
2177 // Make sure we don't retry again.
2178 nodes[0].node.process_pending_htlc_forwards();
2179 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2180 assert_eq!(msg_events.len(), 0);
2182 let mut events = nodes[0].node.get_and_clear_pending_events();
2183 assert_eq!(events.len(), 1);
2185 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id, reason: ref ev_reason } => {
2186 assert_eq!(payment_hash, *ev_payment_hash);
2187 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
2188 assert_eq!(PaymentFailureReason::RetriesExhausted, ev_reason.unwrap());
2190 _ => panic!("Unexpected event"),
2193 } else if test == AutoRetry::FailOnRestart {
2194 // Ensure ChannelManager will not retry a payment after restart, even if there were retry
2195 // attempts remaining prior to restart.
2196 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2197 PaymentId(payment_hash.0), route_params, Retry::Attempts(2)).unwrap();
2198 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2200 // Open a new channel with no liquidity on the second hop so we can find a (bad) route for
2201 // the retry attempt, since the initial second hop channel will be excluded from pathfinding
2202 let channel_id_3 = create_announced_chan_between_nodes(&nodes, 2, 1).2;
2204 // Ensure the first retry attempt fails, with 1 retry attempt remaining
2205 nodes[0].node.process_pending_htlc_forwards();
2206 pass_failed_attempt_with_retry_along_path!(channel_id_3, true);
2208 // Restart the node and ensure that ChannelManager does not use its remaining retry attempt
2209 let node_encoded = nodes[0].node.encode();
2210 let chan_1_monitor_serialized = get_monitor!(nodes[0], channel_id_1).encode();
2211 reload_node!(nodes[0], node_encoded, &[&chan_1_monitor_serialized], persister, new_chain_monitor, node_0_deserialized);
2213 let mut events = nodes[0].node.get_and_clear_pending_events();
2214 expect_pending_htlcs_forwardable_from_events!(nodes[0], events, true);
2215 // Make sure we don't retry again.
2216 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2217 assert_eq!(msg_events.len(), 0);
2219 let mut events = nodes[0].node.get_and_clear_pending_events();
2220 assert_eq!(events.len(), 1);
2222 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id, reason: ref ev_reason } => {
2223 assert_eq!(payment_hash, *ev_payment_hash);
2224 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
2225 assert_eq!(PaymentFailureReason::RetriesExhausted, ev_reason.unwrap());
2227 _ => panic!("Unexpected event"),
2229 } else if test == AutoRetry::FailOnRetry {
2230 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2231 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2232 pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
2234 // We retry payments in `process_pending_htlc_forwards`. Since our channel closed, we should
2235 // fail to find a route.
2236 nodes[0].node.process_pending_htlc_forwards();
2237 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2238 assert_eq!(msg_events.len(), 0);
2240 let mut events = nodes[0].node.get_and_clear_pending_events();
2241 assert_eq!(events.len(), 1);
2243 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id, reason: ref ev_reason } => {
2244 assert_eq!(payment_hash, *ev_payment_hash);
2245 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
2246 assert_eq!(PaymentFailureReason::RouteNotFound, ev_reason.unwrap());
2248 _ => panic!("Unexpected event"),
2254 fn auto_retry_partial_failure() {
2255 // Test that we'll retry appropriately on send partial failure and retry partial failure.
2256 let chanmon_cfgs = create_chanmon_cfgs(2);
2257 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2258 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2259 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2261 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2262 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2263 let chan_3_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2265 // Marshall data to send the payment
2266 let amt_msat = 20_000;
2267 let (_, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2268 #[cfg(feature = "std")]
2269 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2270 #[cfg(not(feature = "std"))]
2271 let payment_expiry_secs = 60 * 60;
2272 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2273 invoice_features.set_variable_length_onion_required();
2274 invoice_features.set_payment_secret_required();
2275 invoice_features.set_basic_mpp_optional();
2276 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2277 .with_expiry_time(payment_expiry_secs as u64)
2278 .with_bolt11_features(invoice_features).unwrap();
2279 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2281 // Ensure the first monitor update (for the initial send path1 over chan_1) succeeds, but the
2282 // second (for the initial send path2 over chan_2) fails.
2283 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
2284 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
2285 // Ensure third monitor update (for the retry1's path1 over chan_1) succeeds, but the fourth (for
2286 // the retry1's path2 over chan_3) fails, and monitor updates succeed after that.
2287 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
2288 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
2289 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
2291 // Configure the initial send, retry1 and retry2's paths.
2292 let send_route = Route {
2294 Path { hops: vec![RouteHop {
2295 pubkey: nodes[1].node.get_our_node_id(),
2296 node_features: nodes[1].node.node_features(),
2297 short_channel_id: chan_1_id,
2298 channel_features: nodes[1].node.channel_features(),
2299 fee_msat: amt_msat / 2,
2300 cltv_expiry_delta: 100,
2301 maybe_announced_channel: true,
2302 }], blinded_tail: None },
2303 Path { hops: vec![RouteHop {
2304 pubkey: nodes[1].node.get_our_node_id(),
2305 node_features: nodes[1].node.node_features(),
2306 short_channel_id: chan_2_id,
2307 channel_features: nodes[1].node.channel_features(),
2308 fee_msat: amt_msat / 2,
2309 cltv_expiry_delta: 100,
2310 maybe_announced_channel: true,
2311 }], blinded_tail: None },
2313 route_params: Some(route_params.clone()),
2315 let retry_1_route = Route {
2317 Path { hops: vec![RouteHop {
2318 pubkey: nodes[1].node.get_our_node_id(),
2319 node_features: nodes[1].node.node_features(),
2320 short_channel_id: chan_1_id,
2321 channel_features: nodes[1].node.channel_features(),
2322 fee_msat: amt_msat / 4,
2323 cltv_expiry_delta: 100,
2324 maybe_announced_channel: true,
2325 }], blinded_tail: None },
2326 Path { hops: vec![RouteHop {
2327 pubkey: nodes[1].node.get_our_node_id(),
2328 node_features: nodes[1].node.node_features(),
2329 short_channel_id: chan_3_id,
2330 channel_features: nodes[1].node.channel_features(),
2331 fee_msat: amt_msat / 4,
2332 cltv_expiry_delta: 100,
2333 maybe_announced_channel: true,
2334 }], blinded_tail: None },
2336 route_params: Some(route_params.clone()),
2338 let retry_2_route = Route {
2340 Path { hops: vec![RouteHop {
2341 pubkey: nodes[1].node.get_our_node_id(),
2342 node_features: nodes[1].node.node_features(),
2343 short_channel_id: chan_1_id,
2344 channel_features: nodes[1].node.channel_features(),
2345 fee_msat: amt_msat / 4,
2346 cltv_expiry_delta: 100,
2347 maybe_announced_channel: true,
2348 }], blinded_tail: None },
2350 route_params: Some(route_params.clone()),
2352 nodes[0].router.expect_find_route(route_params.clone(), Ok(send_route));
2353 let mut payment_params = route_params.payment_params.clone();
2354 payment_params.previously_failed_channels.push(chan_2_id);
2355 nodes[0].router.expect_find_route(
2356 RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 2),
2358 let mut payment_params = route_params.payment_params.clone();
2359 payment_params.previously_failed_channels.push(chan_3_id);
2360 nodes[0].router.expect_find_route(
2361 RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 4),
2364 // Send a payment that will partially fail on send, then partially fail on retry, then succeed.
2365 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2366 PaymentId(payment_hash.0), route_params, Retry::Attempts(3)).unwrap();
2367 let closed_chan_events = nodes[0].node.get_and_clear_pending_events();
2368 assert_eq!(closed_chan_events.len(), 4);
2369 match closed_chan_events[0] {
2370 Event::ChannelClosed { .. } => {},
2371 _ => panic!("Unexpected event"),
2373 match closed_chan_events[1] {
2374 Event::PaymentPathFailed { .. } => {},
2375 _ => panic!("Unexpected event"),
2377 match closed_chan_events[2] {
2378 Event::ChannelClosed { .. } => {},
2379 _ => panic!("Unexpected event"),
2381 match closed_chan_events[3] {
2382 Event::PaymentPathFailed { .. } => {},
2383 _ => panic!("Unexpected event"),
2386 // Pass the first part of the payment along the path.
2387 check_added_monitors!(nodes[0], 5); // three outbound channel updates succeeded, two permanently failed
2388 let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events();
2390 // First message is the first update_add, remaining messages are broadcasting channel updates and
2391 // errors for the permfailed channels
2392 assert_eq!(msg_events.len(), 5);
2393 let mut payment_event = SendEvent::from_event(msg_events.remove(0));
2395 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
2396 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
2397 check_added_monitors!(nodes[1], 1);
2398 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2400 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2401 check_added_monitors!(nodes[0], 1);
2402 let as_second_htlc_updates = SendEvent::from_node(&nodes[0]);
2404 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
2405 check_added_monitors!(nodes[0], 1);
2406 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2408 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
2409 check_added_monitors!(nodes[1], 1);
2411 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.msgs[0]);
2412 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.msgs[1]);
2413 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_htlc_updates.commitment_msg);
2414 check_added_monitors!(nodes[1], 1);
2415 let (bs_second_raa, bs_second_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2417 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2418 check_added_monitors!(nodes[0], 1);
2420 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_cs);
2421 check_added_monitors!(nodes[0], 1);
2422 let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2424 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
2425 check_added_monitors!(nodes[1], 1);
2427 expect_pending_htlcs_forwardable_ignore!(nodes[1]);
2428 nodes[1].node.process_pending_htlc_forwards();
2429 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, amt_msat);
2430 nodes[1].node.claim_funds(payment_preimage);
2431 expect_payment_claimed!(nodes[1], payment_hash, amt_msat);
2432 let bs_claim_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2433 assert_eq!(bs_claim_update.update_fulfill_htlcs.len(), 1);
2435 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_claim_update.update_fulfill_htlcs[0]);
2436 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
2437 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_claim_update.commitment_signed);
2438 check_added_monitors!(nodes[0], 1);
2439 let (as_third_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2441 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
2442 check_added_monitors!(nodes[1], 4);
2443 let bs_second_claim_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2445 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
2446 check_added_monitors!(nodes[1], 1);
2447 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2449 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
2450 check_added_monitors!(nodes[0], 1);
2451 expect_payment_path_successful!(nodes[0]);
2453 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[0]);
2454 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[1]);
2455 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.commitment_signed);
2456 check_added_monitors!(nodes[0], 1);
2457 let (as_fourth_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2459 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_fourth_raa);
2460 check_added_monitors!(nodes[1], 1);
2462 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
2463 check_added_monitors!(nodes[1], 1);
2464 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2466 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2467 check_added_monitors!(nodes[0], 1);
2468 let events = nodes[0].node.get_and_clear_pending_events();
2469 assert_eq!(events.len(), 2);
2470 if let Event::PaymentPathSuccessful { .. } = events[0] {} else { panic!(); }
2471 if let Event::PaymentPathSuccessful { .. } = events[1] {} else { panic!(); }
2475 fn auto_retry_zero_attempts_send_error() {
2476 let chanmon_cfgs = create_chanmon_cfgs(2);
2477 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2478 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2479 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2481 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2482 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2484 // Marshall data to send the payment
2485 let amt_msat = 20_000;
2486 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2487 #[cfg(feature = "std")]
2488 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2489 #[cfg(not(feature = "std"))]
2490 let payment_expiry_secs = 60 * 60;
2491 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2492 invoice_features.set_variable_length_onion_required();
2493 invoice_features.set_payment_secret_required();
2494 invoice_features.set_basic_mpp_optional();
2495 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2496 .with_expiry_time(payment_expiry_secs as u64)
2497 .with_bolt11_features(invoice_features).unwrap();
2498 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2500 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
2501 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2502 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
2503 assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 2); // channel close messages
2504 let events = nodes[0].node.get_and_clear_pending_events();
2505 assert_eq!(events.len(), 3);
2506 if let Event::ChannelClosed { .. } = events[0] { } else { panic!(); }
2507 if let Event::PaymentPathFailed { .. } = events[1] { } else { panic!(); }
2508 if let Event::PaymentFailed { .. } = events[2] { } else { panic!(); }
2509 check_added_monitors!(nodes[0], 2);
2513 fn fails_paying_after_rejected_by_payee() {
2514 let chanmon_cfgs = create_chanmon_cfgs(2);
2515 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2516 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2517 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2519 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
2521 // Marshall data to send the payment
2522 let amt_msat = 20_000;
2523 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2524 #[cfg(feature = "std")]
2525 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2526 #[cfg(not(feature = "std"))]
2527 let payment_expiry_secs = 60 * 60;
2528 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2529 invoice_features.set_variable_length_onion_required();
2530 invoice_features.set_payment_secret_required();
2531 invoice_features.set_basic_mpp_optional();
2532 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2533 .with_expiry_time(payment_expiry_secs as u64)
2534 .with_bolt11_features(invoice_features).unwrap();
2535 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2537 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2538 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2539 check_added_monitors!(nodes[0], 1);
2540 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2541 assert_eq!(events.len(), 1);
2542 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
2543 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
2544 check_added_monitors!(nodes[1], 0);
2545 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
2546 expect_pending_htlcs_forwardable!(nodes[1]);
2547 expect_payment_claimable!(&nodes[1], payment_hash, payment_secret, amt_msat);
2549 nodes[1].node.fail_htlc_backwards(&payment_hash);
2550 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::FailedPayment { payment_hash }]);
2551 pass_failed_payment_back(&nodes[0], &[&[&nodes[1]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
2555 fn retry_multi_path_single_failed_payment() {
2556 // Tests that we can/will retry after a single path of an MPP payment failed immediately
2557 let chanmon_cfgs = create_chanmon_cfgs(2);
2558 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2559 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
2560 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2562 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2563 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2565 let amt_msat = 100_010_000;
2567 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2568 #[cfg(feature = "std")]
2569 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2570 #[cfg(not(feature = "std"))]
2571 let payment_expiry_secs = 60 * 60;
2572 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2573 invoice_features.set_variable_length_onion_required();
2574 invoice_features.set_payment_secret_required();
2575 invoice_features.set_basic_mpp_optional();
2576 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2577 .with_expiry_time(payment_expiry_secs as u64)
2578 .with_bolt11_features(invoice_features).unwrap();
2579 let route_params = RouteParameters::from_payment_params_and_value(
2580 payment_params.clone(), amt_msat);
2582 let chans = nodes[0].node.list_usable_channels();
2583 let mut route = Route {
2585 Path { hops: vec![RouteHop {
2586 pubkey: nodes[1].node.get_our_node_id(),
2587 node_features: nodes[1].node.node_features(),
2588 short_channel_id: chans[0].short_channel_id.unwrap(),
2589 channel_features: nodes[1].node.channel_features(),
2591 cltv_expiry_delta: 100,
2592 maybe_announced_channel: true,
2593 }], blinded_tail: None },
2594 Path { hops: vec![RouteHop {
2595 pubkey: nodes[1].node.get_our_node_id(),
2596 node_features: nodes[1].node.node_features(),
2597 short_channel_id: chans[1].short_channel_id.unwrap(),
2598 channel_features: nodes[1].node.channel_features(),
2599 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
2600 cltv_expiry_delta: 100,
2601 maybe_announced_channel: true,
2602 }], blinded_tail: None },
2604 route_params: Some(route_params.clone()),
2606 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2607 // On retry, split the payment across both channels.
2608 route.paths[0].hops[0].fee_msat = 50_000_001;
2609 route.paths[1].hops[0].fee_msat = 50_000_000;
2610 let mut pay_params = route.route_params.clone().unwrap().payment_params;
2611 pay_params.previously_failed_channels.push(chans[1].short_channel_id.unwrap());
2612 nodes[0].router.expect_find_route(
2613 // Note that the second request here requests the amount we originally failed to send,
2614 // not the amount remaining on the full payment, which should be changed.
2615 RouteParameters::from_payment_params_and_value(pay_params, 100_000_001),
2619 let scorer = chanmon_cfgs[0].scorer.read().unwrap();
2620 // The initial send attempt, 2 paths
2621 scorer.expect_usage(chans[0].short_channel_id.unwrap(), ChannelUsage { amount_msat: 10_000, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2622 scorer.expect_usage(chans[1].short_channel_id.unwrap(), ChannelUsage { amount_msat: 100_000_001, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2623 // The retry, 2 paths. Ensure that the in-flight HTLC amount is factored in.
2624 scorer.expect_usage(chans[0].short_channel_id.unwrap(), ChannelUsage { amount_msat: 50_000_001, inflight_htlc_msat: 10_000, effective_capacity: EffectiveCapacity::Unknown });
2625 scorer.expect_usage(chans[1].short_channel_id.unwrap(), ChannelUsage { amount_msat: 50_000_000, inflight_htlc_msat: 0, effective_capacity: EffectiveCapacity::Unknown });
2628 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2629 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2630 let events = nodes[0].node.get_and_clear_pending_events();
2631 assert_eq!(events.len(), 1);
2633 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
2634 failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { .. }},
2635 short_channel_id: Some(expected_scid), .. } =>
2637 assert_eq!(payment_hash, ev_payment_hash);
2638 assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
2640 _ => panic!("Unexpected event"),
2642 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
2643 assert_eq!(htlc_msgs.len(), 2);
2644 check_added_monitors!(nodes[0], 2);
2648 fn immediate_retry_on_failure() {
2649 // Tests that we can/will retry immediately after a failure
2650 let chanmon_cfgs = create_chanmon_cfgs(2);
2651 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2652 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
2653 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2655 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2656 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
2658 let amt_msat = 100_000_001;
2659 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2660 #[cfg(feature = "std")]
2661 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2662 #[cfg(not(feature = "std"))]
2663 let payment_expiry_secs = 60 * 60;
2664 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2665 invoice_features.set_variable_length_onion_required();
2666 invoice_features.set_payment_secret_required();
2667 invoice_features.set_basic_mpp_optional();
2668 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2669 .with_expiry_time(payment_expiry_secs as u64)
2670 .with_bolt11_features(invoice_features).unwrap();
2671 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2673 let chans = nodes[0].node.list_usable_channels();
2674 let mut route = Route {
2676 Path { hops: vec![RouteHop {
2677 pubkey: nodes[1].node.get_our_node_id(),
2678 node_features: nodes[1].node.node_features(),
2679 short_channel_id: chans[0].short_channel_id.unwrap(),
2680 channel_features: nodes[1].node.channel_features(),
2681 fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
2682 cltv_expiry_delta: 100,
2683 maybe_announced_channel: true,
2684 }], blinded_tail: None },
2686 route_params: Some(RouteParameters::from_payment_params_and_value(
2687 PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV),
2690 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2691 // On retry, split the payment across both channels.
2692 route.paths.push(route.paths[0].clone());
2693 route.paths[0].hops[0].short_channel_id = chans[1].short_channel_id.unwrap();
2694 route.paths[0].hops[0].fee_msat = 50_000_000;
2695 route.paths[1].hops[0].fee_msat = 50_000_001;
2696 let mut pay_params = route_params.payment_params.clone();
2697 pay_params.previously_failed_channels.push(chans[0].short_channel_id.unwrap());
2698 nodes[0].router.expect_find_route(
2699 RouteParameters::from_payment_params_and_value(pay_params, amt_msat),
2702 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2703 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2704 let events = nodes[0].node.get_and_clear_pending_events();
2705 assert_eq!(events.len(), 1);
2707 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently: false,
2708 failure: PathFailure::InitialSend { err: APIError::ChannelUnavailable { .. }},
2709 short_channel_id: Some(expected_scid), .. } =>
2711 assert_eq!(payment_hash, ev_payment_hash);
2712 assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
2714 _ => panic!("Unexpected event"),
2716 let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
2717 assert_eq!(htlc_msgs.len(), 2);
2718 check_added_monitors!(nodes[0], 2);
2722 fn no_extra_retries_on_back_to_back_fail() {
2723 // In a previous release, we had a race where we may exceed the payment retry count if we
2724 // get two failures in a row with the second indicating that all paths had failed (this field,
2725 // `all_paths_failed`, has since been removed).
2726 // Generally, when we give up trying to retry a payment, we don't know for sure what the
2727 // current state of the ChannelManager event queue is. Specifically, we cannot be sure that
2728 // there are not multiple additional `PaymentPathFailed` or even `PaymentSent` events
2729 // pending which we will see later. Thus, when we previously removed the retry tracking map
2730 // entry after a `all_paths_failed` `PaymentPathFailed` event, we may have dropped the
2731 // retry entry even though more events for the same payment were still pending. This led to
2732 // us retrying a payment again even though we'd already given up on it.
2734 // We now have a separate event - `PaymentFailed` which indicates no HTLCs remain and which
2735 // is used to remove the payment retry counter entries instead. This tests for the specific
2736 // excess-retry case while also testing `PaymentFailed` generation.
2738 let chanmon_cfgs = create_chanmon_cfgs(3);
2739 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2740 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2741 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2743 let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
2744 let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0).0.contents.short_channel_id;
2746 let amt_msat = 200_000_000;
2747 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], amt_msat);
2748 #[cfg(feature = "std")]
2749 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2750 #[cfg(not(feature = "std"))]
2751 let payment_expiry_secs = 60 * 60;
2752 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2753 invoice_features.set_variable_length_onion_required();
2754 invoice_features.set_payment_secret_required();
2755 invoice_features.set_basic_mpp_optional();
2756 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2757 .with_expiry_time(payment_expiry_secs as u64)
2758 .with_bolt11_features(invoice_features).unwrap();
2759 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2761 let mut route = Route {
2763 Path { hops: vec![RouteHop {
2764 pubkey: nodes[1].node.get_our_node_id(),
2765 node_features: nodes[1].node.node_features(),
2766 short_channel_id: chan_1_scid,
2767 channel_features: nodes[1].node.channel_features(),
2768 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2769 cltv_expiry_delta: 100,
2770 maybe_announced_channel: true,
2772 pubkey: nodes[2].node.get_our_node_id(),
2773 node_features: nodes[2].node.node_features(),
2774 short_channel_id: chan_2_scid,
2775 channel_features: nodes[2].node.channel_features(),
2776 fee_msat: 100_000_000,
2777 cltv_expiry_delta: 100,
2778 maybe_announced_channel: true,
2779 }], blinded_tail: None },
2780 Path { hops: vec![RouteHop {
2781 pubkey: nodes[1].node.get_our_node_id(),
2782 node_features: nodes[1].node.node_features(),
2783 short_channel_id: chan_1_scid,
2784 channel_features: nodes[1].node.channel_features(),
2785 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2786 cltv_expiry_delta: 100,
2787 maybe_announced_channel: true,
2789 pubkey: nodes[2].node.get_our_node_id(),
2790 node_features: nodes[2].node.node_features(),
2791 short_channel_id: chan_2_scid,
2792 channel_features: nodes[2].node.channel_features(),
2793 fee_msat: 100_000_000,
2794 cltv_expiry_delta: 100,
2795 maybe_announced_channel: true,
2796 }], blinded_tail: None }
2798 route_params: Some(RouteParameters::from_payment_params_and_value(
2799 PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
2802 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
2803 let mut second_payment_params = route_params.payment_params.clone();
2804 second_payment_params.previously_failed_channels = vec![chan_2_scid, chan_2_scid];
2805 // On retry, we'll only return one path
2806 route.paths.remove(1);
2807 route.paths[0].hops[1].fee_msat = amt_msat;
2808 nodes[0].router.expect_find_route(
2809 RouteParameters::from_payment_params_and_value(second_payment_params, amt_msat),
2812 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
2813 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
2814 let htlc_updates = SendEvent::from_node(&nodes[0]);
2815 check_added_monitors!(nodes[0], 1);
2816 assert_eq!(htlc_updates.msgs.len(), 1);
2818 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
2819 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
2820 check_added_monitors!(nodes[1], 1);
2821 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2823 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2824 check_added_monitors!(nodes[0], 1);
2825 let second_htlc_updates = SendEvent::from_node(&nodes[0]);
2827 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
2828 check_added_monitors!(nodes[0], 1);
2829 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2831 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
2832 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
2833 check_added_monitors!(nodes[1], 1);
2834 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2836 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
2837 check_added_monitors!(nodes[1], 1);
2838 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2840 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2841 check_added_monitors!(nodes[0], 1);
2843 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
2844 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
2845 check_added_monitors!(nodes[0], 1);
2846 let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2848 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
2849 check_added_monitors!(nodes[1], 1);
2850 let bs_second_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2852 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
2853 check_added_monitors!(nodes[1], 1);
2854 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2856 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.update_fail_htlcs[0]);
2857 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_fail_update.commitment_signed);
2858 check_added_monitors!(nodes[0], 1);
2860 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
2861 check_added_monitors!(nodes[0], 1);
2862 let (as_third_raa, as_fourth_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2864 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_third_raa);
2865 check_added_monitors!(nodes[1], 1);
2866 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_fourth_cs);
2867 check_added_monitors!(nodes[1], 1);
2868 let bs_fourth_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2870 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_fourth_raa);
2871 check_added_monitors!(nodes[0], 1);
2873 // At this point A has sent two HTLCs which both failed due to lack of fee. It now has two
2874 // pending `PaymentPathFailed` events, one with `all_paths_failed` unset, and the second
2877 // Previously, we retried payments in an event consumer, which would retry each
2878 // `PaymentPathFailed` individually. In that setup, we had retried the payment in response to
2879 // the first `PaymentPathFailed`, then seen the second `PaymentPathFailed` with
2880 // `all_paths_failed` set and assumed the payment was completely failed. We ultimately fixed it
2881 // by adding the `PaymentFailed` event.
2883 // Because we now retry payments as a batch, we simply return a single-path route in the
2884 // second, batched, request, have that fail, ensure the payment was abandoned.
2885 let mut events = nodes[0].node.get_and_clear_pending_events();
2886 assert_eq!(events.len(), 3);
2888 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
2889 assert_eq!(payment_hash, ev_payment_hash);
2890 assert_eq!(payment_failed_permanently, false);
2892 _ => panic!("Unexpected event"),
2895 Event::PendingHTLCsForwardable { .. } => {},
2896 _ => panic!("Unexpected event"),
2899 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
2900 assert_eq!(payment_hash, ev_payment_hash);
2901 assert_eq!(payment_failed_permanently, false);
2903 _ => panic!("Unexpected event"),
2906 nodes[0].node.process_pending_htlc_forwards();
2907 let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
2908 check_added_monitors!(nodes[0], 1);
2910 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
2911 commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
2912 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2913 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
2914 commitment_signed_dance!(nodes[0], nodes[1], &bs_fail_update.commitment_signed, false, true);
2916 let mut events = nodes[0].node.get_and_clear_pending_events();
2917 assert_eq!(events.len(), 2);
2919 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
2920 assert_eq!(payment_hash, ev_payment_hash);
2921 assert_eq!(payment_failed_permanently, false);
2923 _ => panic!("Unexpected event"),
2926 Event::PaymentFailed { payment_hash: ref ev_payment_hash, payment_id: ref ev_payment_id, reason: ref ev_reason } => {
2927 assert_eq!(payment_hash, *ev_payment_hash);
2928 assert_eq!(PaymentId(payment_hash.0), *ev_payment_id);
2929 assert_eq!(PaymentFailureReason::RetriesExhausted, ev_reason.unwrap());
2931 _ => panic!("Unexpected event"),
2936 fn test_simple_partial_retry() {
2937 // In the first version of the in-`ChannelManager` payment retries, retries were sent for the
2938 // full amount of the payment, rather than only the missing amount. Here we simply test for
2939 // this by sending a payment with two parts, failing one, and retrying the second. Note that
2940 // `TestRouter` will check that the `RouteParameters` (which contain the amount) matches the
2942 let chanmon_cfgs = create_chanmon_cfgs(3);
2943 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2944 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2945 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2947 let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
2948 let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0).0.contents.short_channel_id;
2950 let amt_msat = 200_000_000;
2951 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[2], amt_msat);
2952 #[cfg(feature = "std")]
2953 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
2954 #[cfg(not(feature = "std"))]
2955 let payment_expiry_secs = 60 * 60;
2956 let mut invoice_features = Bolt11InvoiceFeatures::empty();
2957 invoice_features.set_variable_length_onion_required();
2958 invoice_features.set_payment_secret_required();
2959 invoice_features.set_basic_mpp_optional();
2960 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
2961 .with_expiry_time(payment_expiry_secs as u64)
2962 .with_bolt11_features(invoice_features).unwrap();
2963 let route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
2965 let mut route = Route {
2967 Path { hops: vec![RouteHop {
2968 pubkey: nodes[1].node.get_our_node_id(),
2969 node_features: nodes[1].node.node_features(),
2970 short_channel_id: chan_1_scid,
2971 channel_features: nodes[1].node.channel_features(),
2972 fee_msat: 0, // nodes[1] will fail the payment as we don't pay its fee
2973 cltv_expiry_delta: 100,
2974 maybe_announced_channel: true,
2976 pubkey: nodes[2].node.get_our_node_id(),
2977 node_features: nodes[2].node.node_features(),
2978 short_channel_id: chan_2_scid,
2979 channel_features: nodes[2].node.channel_features(),
2980 fee_msat: 100_000_000,
2981 cltv_expiry_delta: 100,
2982 maybe_announced_channel: true,
2983 }], blinded_tail: None },
2984 Path { hops: vec![RouteHop {
2985 pubkey: nodes[1].node.get_our_node_id(),
2986 node_features: nodes[1].node.node_features(),
2987 short_channel_id: chan_1_scid,
2988 channel_features: nodes[1].node.channel_features(),
2990 cltv_expiry_delta: 100,
2991 maybe_announced_channel: true,
2993 pubkey: nodes[2].node.get_our_node_id(),
2994 node_features: nodes[2].node.node_features(),
2995 short_channel_id: chan_2_scid,
2996 channel_features: nodes[2].node.channel_features(),
2997 fee_msat: 100_000_000,
2998 cltv_expiry_delta: 100,
2999 maybe_announced_channel: true,
3000 }], blinded_tail: None }
3002 route_params: Some(RouteParameters::from_payment_params_and_value(
3003 PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
3006 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
3007 let mut second_payment_params = route_params.payment_params.clone();
3008 second_payment_params.previously_failed_channels = vec![chan_2_scid];
3009 // On retry, we'll only be asked for one path (or 100k sats)
3010 route.paths.remove(0);
3011 nodes[0].router.expect_find_route(
3012 RouteParameters::from_payment_params_and_value(second_payment_params, amt_msat / 2),
3015 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
3016 PaymentId(payment_hash.0), route_params, Retry::Attempts(1)).unwrap();
3017 let htlc_updates = SendEvent::from_node(&nodes[0]);
3018 check_added_monitors!(nodes[0], 1);
3019 assert_eq!(htlc_updates.msgs.len(), 1);
3021 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &htlc_updates.msgs[0]);
3022 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &htlc_updates.commitment_msg);
3023 check_added_monitors!(nodes[1], 1);
3024 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3026 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
3027 check_added_monitors!(nodes[0], 1);
3028 let second_htlc_updates = SendEvent::from_node(&nodes[0]);
3030 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
3031 check_added_monitors!(nodes[0], 1);
3032 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3034 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &second_htlc_updates.msgs[0]);
3035 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &second_htlc_updates.commitment_msg);
3036 check_added_monitors!(nodes[1], 1);
3037 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3039 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
3040 check_added_monitors!(nodes[1], 1);
3041 let bs_fail_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3043 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
3044 check_added_monitors!(nodes[0], 1);
3046 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]);
3047 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_fail_update.commitment_signed);
3048 check_added_monitors!(nodes[0], 1);
3049 let (as_second_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3051 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
3052 check_added_monitors!(nodes[1], 1);
3054 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_third_cs);
3055 check_added_monitors!(nodes[1], 1);
3057 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3059 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
3060 check_added_monitors!(nodes[0], 1);
3062 let mut events = nodes[0].node.get_and_clear_pending_events();
3063 assert_eq!(events.len(), 2);
3065 Event::PaymentPathFailed { payment_hash: ev_payment_hash, payment_failed_permanently, .. } => {
3066 assert_eq!(payment_hash, ev_payment_hash);
3067 assert_eq!(payment_failed_permanently, false);
3069 _ => panic!("Unexpected event"),
3072 Event::PendingHTLCsForwardable { .. } => {},
3073 _ => panic!("Unexpected event"),
3076 nodes[0].node.process_pending_htlc_forwards();
3077 let retry_htlc_updates = SendEvent::from_node(&nodes[0]);
3078 check_added_monitors!(nodes[0], 1);
3080 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &retry_htlc_updates.msgs[0]);
3081 commitment_signed_dance!(nodes[1], nodes[0], &retry_htlc_updates.commitment_msg, false, true);
3083 expect_pending_htlcs_forwardable!(nodes[1]);
3084 check_added_monitors!(nodes[1], 1);
3086 let bs_forward_update = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
3087 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[0]);
3088 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[1]);
3089 commitment_signed_dance!(nodes[2], nodes[1], &bs_forward_update.commitment_signed, false);
3091 expect_pending_htlcs_forwardable!(nodes[2]);
3092 expect_payment_claimable!(nodes[2], payment_hash, payment_secret, amt_msat);
3096 #[cfg(feature = "std")]
3097 fn test_threaded_payment_retries() {
3098 // In the first version of the in-`ChannelManager` payment retries, retries weren't limited to
3099 // a single thread and would happily let multiple threads run retries at the same time. Because
3100 // retries are done by first calculating the amount we need to retry, then dropping the
3101 // relevant lock, then actually sending, we would happily let multiple threads retry the same
3102 // amount at the same time, overpaying our original HTLC!
3103 let chanmon_cfgs = create_chanmon_cfgs(4);
3104 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
3105 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
3106 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
3108 // There is one mitigating guardrail when retrying payments - we can never over-pay by more
3109 // than 10% of the original value. Thus, we want all our retries to be below that. In order to
3110 // keep things simple, we route one HTLC for 0.1% of the payment over channel 1 and the rest
3111 // out over channel 3+4. This will let us ignore 99% of the payment value and deal with only
3113 let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id;
3114 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 10_000_000, 0);
3115 let chan_3_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 10_000_000, 0).0.contents.short_channel_id;
3116 let chan_4_scid = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 0).0.contents.short_channel_id;
3118 let amt_msat = 100_000_000;
3119 let (_, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[2], amt_msat);
3120 #[cfg(feature = "std")]
3121 let payment_expiry_secs = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs() + 60 * 60;
3122 #[cfg(not(feature = "std"))]
3123 let payment_expiry_secs = 60 * 60;
3124 let mut invoice_features = Bolt11InvoiceFeatures::empty();
3125 invoice_features.set_variable_length_onion_required();
3126 invoice_features.set_payment_secret_required();
3127 invoice_features.set_basic_mpp_optional();
3128 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
3129 .with_expiry_time(payment_expiry_secs as u64)
3130 .with_bolt11_features(invoice_features).unwrap();
3131 let mut route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
3133 let mut route = Route {
3135 Path { hops: vec![RouteHop {
3136 pubkey: nodes[1].node.get_our_node_id(),
3137 node_features: nodes[1].node.node_features(),
3138 short_channel_id: chan_1_scid,
3139 channel_features: nodes[1].node.channel_features(),
3141 cltv_expiry_delta: 100,
3142 maybe_announced_channel: true,
3144 pubkey: nodes[3].node.get_our_node_id(),
3145 node_features: nodes[2].node.node_features(),
3146 short_channel_id: 42, // Set a random SCID which nodes[1] will fail as unknown
3147 channel_features: nodes[2].node.channel_features(),
3148 fee_msat: amt_msat / 1000,
3149 cltv_expiry_delta: 100,
3150 maybe_announced_channel: true,
3151 }], blinded_tail: None },
3152 Path { hops: vec![RouteHop {
3153 pubkey: nodes[2].node.get_our_node_id(),
3154 node_features: nodes[2].node.node_features(),
3155 short_channel_id: chan_3_scid,
3156 channel_features: nodes[2].node.channel_features(),
3158 cltv_expiry_delta: 100,
3159 maybe_announced_channel: true,
3161 pubkey: nodes[3].node.get_our_node_id(),
3162 node_features: nodes[3].node.node_features(),
3163 short_channel_id: chan_4_scid,
3164 channel_features: nodes[3].node.channel_features(),
3165 fee_msat: amt_msat - amt_msat / 1000,
3166 cltv_expiry_delta: 100,
3167 maybe_announced_channel: true,
3168 }], blinded_tail: None }
3170 route_params: Some(RouteParameters::from_payment_params_and_value(
3171 PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
3172 amt_msat - amt_msat / 1000)),
3174 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
3176 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
3177 PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0xdeadbeef)).unwrap();
3178 check_added_monitors!(nodes[0], 2);
3179 let mut send_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
3180 assert_eq!(send_msg_events.len(), 2);
3181 send_msg_events.retain(|msg|
3182 if let MessageSendEvent::UpdateHTLCs { node_id, .. } = msg {
3183 // Drop the commitment update for nodes[2], we can just let that one sit pending
3185 *node_id == nodes[1].node.get_our_node_id()
3186 } else { panic!(); }
3189 // from here on out, the retry `RouteParameters` amount will be amt/1000
3190 route_params.final_value_msat /= 1000;
3193 let end_time = Instant::now() + Duration::from_secs(1);
3194 macro_rules! thread_body { () => { {
3195 // We really want std::thread::scope, but its not stable until 1.63. Until then, we get unsafe.
3196 let node_ref = NodePtr::from_node(&nodes[0]);
3198 let node_a = unsafe { &*node_ref.0 };
3199 while Instant::now() < end_time {
3200 node_a.node.get_and_clear_pending_events(); // wipe the PendingHTLCsForwardable
3201 // Ignore if we have any pending events, just always pretend we just got a
3202 // PendingHTLCsForwardable
3203 node_a.node.process_pending_htlc_forwards();
3207 let mut threads = Vec::new();
3208 for _ in 0..16 { threads.push(std::thread::spawn(thread_body!())); }
3210 // Back in the main thread, poll pending messages and make sure that we never have more than
3211 // one HTLC pending at a time. Note that the commitment_signed_dance will fail horribly if
3212 // there are HTLC messages shoved in while its running. This allows us to test that we never
3213 // generate an additional update_add_htlc until we've fully failed the first.
3214 let mut previously_failed_channels = Vec::new();
3216 assert_eq!(send_msg_events.len(), 1);
3217 let send_event = SendEvent::from_event(send_msg_events.pop().unwrap());
3218 assert_eq!(send_event.msgs.len(), 1);
3220 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
3221 commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true);
3223 // Note that we only push one route into `expect_find_route` at a time, because that's all
3224 // the retries (should) need. If the bug is reintroduced "real" routes may be selected, but
3225 // we should still ultimately fail for the same reason - because we're trying to send too
3226 // many HTLCs at once.
3227 let mut new_route_params = route_params.clone();
3228 previously_failed_channels.push(route.paths[0].hops[1].short_channel_id);
3229 new_route_params.payment_params.previously_failed_channels = previously_failed_channels.clone();
3230 route.paths[0].hops[1].short_channel_id += 1;
3231 nodes[0].router.expect_find_route(new_route_params, Ok(route.clone()));
3233 let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3234 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
3235 // The "normal" commitment_signed_dance delivers the final RAA and then calls
3236 // `check_added_monitors` to ensure only the one RAA-generated monitor update was created.
3237 // This races with our other threads which may generate an add-HTLCs commitment update via
3238 // `process_pending_htlc_forwards`. Instead, we defer the monitor update check until after
3239 // *we've* called `process_pending_htlc_forwards` when its guaranteed to have two updates.
3240 let last_raa = commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true, false, true);
3241 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &last_raa);
3243 let cur_time = Instant::now();
3244 if cur_time > end_time {
3245 for thread in threads.drain(..) { thread.join().unwrap(); }
3248 // Make sure we have some events to handle when we go around...
3249 nodes[0].node.get_and_clear_pending_events(); // wipe the PendingHTLCsForwardable
3250 nodes[0].node.process_pending_htlc_forwards();
3251 send_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
3252 check_added_monitors!(nodes[0], 2);
3254 if cur_time > end_time {
3260 fn do_no_missing_sent_on_reload(persist_manager_with_payment: bool, at_midpoint: bool) {
3261 // Test that if we reload in the middle of an HTLC claim commitment signed dance we'll still
3262 // receive the PaymentSent event even if the ChannelManager had no idea about the payment when
3263 // it was last persisted.
3264 let chanmon_cfgs = create_chanmon_cfgs(2);
3265 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3266 let (persister_a, persister_b, persister_c);
3267 let (chain_monitor_a, chain_monitor_b, chain_monitor_c);
3268 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3269 let (nodes_0_deserialized, nodes_0_deserialized_b, nodes_0_deserialized_c);
3270 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3272 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
3274 let mut nodes_0_serialized = Vec::new();
3275 if !persist_manager_with_payment {
3276 nodes_0_serialized = nodes[0].node.encode();
3279 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
3281 if persist_manager_with_payment {
3282 nodes_0_serialized = nodes[0].node.encode();
3285 nodes[1].node.claim_funds(our_payment_preimage);
3286 check_added_monitors!(nodes[1], 1);
3287 expect_payment_claimed!(nodes[1], our_payment_hash, 1_000_000);
3290 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3291 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
3292 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
3293 check_added_monitors!(nodes[0], 1);
3295 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3296 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
3297 commitment_signed_dance!(nodes[0], nodes[1], htlc_fulfill_updates.commitment_signed, false);
3298 // Ignore the PaymentSent event which is now pending on nodes[0] - if we were to handle it we'd
3299 // be expected to ignore the eventual conflicting PaymentFailed, but by not looking at it we
3300 // expect to get the PaymentSent again later.
3301 check_added_monitors(&nodes[0], 0);
3304 // The ChannelMonitor should always be the latest version, as we're required to persist it
3305 // during the commitment signed handling.
3306 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
3307 reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister_a, chain_monitor_a, nodes_0_deserialized);
3309 let events = nodes[0].node.get_and_clear_pending_events();
3310 assert_eq!(events.len(), 2);
3311 if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[0] {} else { panic!(); }
3312 if let Event::PaymentSent { payment_preimage, .. } = events[1] { assert_eq!(payment_preimage, our_payment_preimage); } else { panic!(); }
3313 // Note that we don't get a PaymentPathSuccessful here as we leave the HTLC pending to avoid
3314 // the double-claim that would otherwise appear at the end of this test.
3315 nodes[0].node.timer_tick_occurred();
3316 let as_broadcasted_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3317 assert_eq!(as_broadcasted_txn.len(), 1);
3319 // Ensure that, even after some time, if we restart we still include *something* in the current
3320 // `ChannelManager` which prevents a `PaymentFailed` when we restart even if pending resolved
3321 // payments have since been timed out thanks to `IDEMPOTENCY_TIMEOUT_TICKS`.
3322 // A naive implementation of the fix here would wipe the pending payments set, causing a
3323 // failure event when we restart.
3324 for _ in 0..(IDEMPOTENCY_TIMEOUT_TICKS * 2) { nodes[0].node.timer_tick_occurred(); }
3326 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
3327 reload_node!(nodes[0], test_default_channel_config(), &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister_b, chain_monitor_b, nodes_0_deserialized_b);
3328 let events = nodes[0].node.get_and_clear_pending_events();
3329 assert!(events.is_empty());
3331 // Ensure that we don't generate any further events even after the channel-closing commitment
3332 // transaction is confirmed on-chain.
3333 confirm_transaction(&nodes[0], &as_broadcasted_txn[0]);
3334 for _ in 0..(IDEMPOTENCY_TIMEOUT_TICKS * 2) { nodes[0].node.timer_tick_occurred(); }
3336 let events = nodes[0].node.get_and_clear_pending_events();
3337 assert!(events.is_empty());
3339 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
3340 reload_node!(nodes[0], test_default_channel_config(), &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister_c, chain_monitor_c, nodes_0_deserialized_c);
3341 let events = nodes[0].node.get_and_clear_pending_events();
3342 assert!(events.is_empty());
3343 check_added_monitors(&nodes[0], 1);
3347 fn no_missing_sent_on_midpoint_reload() {
3348 do_no_missing_sent_on_reload(false, true);
3349 do_no_missing_sent_on_reload(true, true);
3353 fn no_missing_sent_on_reload() {
3354 do_no_missing_sent_on_reload(false, false);
3355 do_no_missing_sent_on_reload(true, false);
3358 fn do_claim_from_closed_chan(fail_payment: bool) {
3359 // Previously, LDK would refuse to claim a payment if a channel on which the payment was
3360 // received had been closed between when the HTLC was received and when we went to claim it.
3361 // This makes sense in the payment case - why pay an on-chain fee to claim the HTLC when
3362 // presumably the sender may retry later. Long ago it also reduced total code in the claim
3365 // However, this doesn't make sense if you're trying to do an atomic swap or some other
3366 // protocol that requires atomicity with some other action - if your money got claimed
3367 // elsewhere you need to be able to claim the HTLC in lightning no matter what. Further, this
3368 // is an over-optimization - there should be a very, very low likelihood that a channel closes
3369 // between when we receive the last HTLC for a payment and the user goes to claim the payment.
3370 // Since we now have code to handle this anyway we should allow it.
3372 // Build 4 nodes and send an MPP payment across two paths. By building a route manually set the
3373 // CLTVs on the paths to different value resulting in a different claim deadline.
3374 let chanmon_cfgs = create_chanmon_cfgs(4);
3375 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
3376 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
3377 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
3379 create_announced_chan_between_nodes(&nodes, 0, 1);
3380 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
3381 let chan_bd = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0).2;
3382 create_announced_chan_between_nodes(&nodes, 2, 3);
3384 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[3]);
3385 let mut route_params = RouteParameters::from_payment_params_and_value(
3386 PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
3387 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap(),
3389 let mut route = nodes[0].router.find_route(&nodes[0].node.get_our_node_id(), &route_params,
3390 None, nodes[0].node.compute_inflight_htlcs()).unwrap();
3391 // Make sure the route is ordered as the B->D path before C->D
3392 route.paths.sort_by(|a, _| if a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
3393 std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater });
3395 // Note that we add an extra 1 in the send pipeline to compensate for any blocks found while
3396 // the HTLC is being relayed.
3397 route.paths[0].hops[1].cltv_expiry_delta = TEST_FINAL_CLTV + 8;
3398 route.paths[1].hops[1].cltv_expiry_delta = TEST_FINAL_CLTV + 12;
3399 let final_cltv = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 8 + 1;
3401 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
3402 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
3403 PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(1)).unwrap();
3404 check_added_monitors(&nodes[0], 2);
3405 let mut send_msgs = nodes[0].node.get_and_clear_pending_msg_events();
3406 send_msgs.sort_by(|a, _| {
3408 if let MessageSendEvent::UpdateHTLCs { node_id, .. } = a { node_id } else { panic!() };
3409 let node_b_id = nodes[1].node.get_our_node_id();
3410 if *a_node_id == node_b_id { std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater }
3413 assert_eq!(send_msgs.len(), 2);
3414 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 10_000_000,
3415 payment_hash, Some(payment_secret), send_msgs.remove(0), false, None);
3416 let receive_event = pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 10_000_000,
3417 payment_hash, Some(payment_secret), send_msgs.remove(0), true, None);
3419 match receive_event.unwrap() {
3420 Event::PaymentClaimable { claim_deadline, .. } => {
3421 assert_eq!(claim_deadline.unwrap(), final_cltv - HTLC_FAIL_BACK_BUFFER);
3426 // Ensure that the claim_deadline is correct, with the payment failing at exactly the given
3428 connect_blocks(&nodes[3], final_cltv - HTLC_FAIL_BACK_BUFFER - nodes[3].best_block_info().1
3429 - if fail_payment { 0 } else { 2 });
3431 // We fail the HTLC on the A->B->D path first as it expires 4 blocks earlier. We go ahead
3432 // and expire both immediately, though, by connecting another 4 blocks.
3433 let reason = HTLCDestination::FailedPayment { payment_hash };
3434 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[3], [reason.clone()]);
3435 connect_blocks(&nodes[3], 4);
3436 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[3], [reason]);
3437 pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
3439 nodes[1].node.force_close_broadcasting_latest_txn(&chan_bd, &nodes[3].node.get_our_node_id()).unwrap();
3440 check_closed_event!(&nodes[1], 1, ClosureReason::HolderForceClosed, false,
3441 [nodes[3].node.get_our_node_id()], 1000000);
3442 check_closed_broadcast(&nodes[1], 1, true);
3443 let bs_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3444 assert_eq!(bs_tx.len(), 1);
3446 mine_transaction(&nodes[3], &bs_tx[0]);
3447 check_added_monitors(&nodes[3], 1);
3448 check_closed_broadcast(&nodes[3], 1, true);
3449 check_closed_event!(&nodes[3], 1, ClosureReason::CommitmentTxConfirmed, false,
3450 [nodes[1].node.get_our_node_id()], 1000000);
3452 nodes[3].node.claim_funds(payment_preimage);
3453 check_added_monitors(&nodes[3], 2);
3454 expect_payment_claimed!(nodes[3], payment_hash, 10_000_000);
3456 let ds_tx = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3457 assert_eq!(ds_tx.len(), 1);
3458 check_spends!(&ds_tx[0], &bs_tx[0]);
3460 mine_transactions(&nodes[1], &[&bs_tx[0], &ds_tx[0]]);
3461 check_added_monitors(&nodes[1], 1);
3462 expect_payment_forwarded!(nodes[1], nodes[0], nodes[3], Some(1000), false, true);
3464 let bs_claims = nodes[1].node.get_and_clear_pending_msg_events();
3465 check_added_monitors(&nodes[1], 1);
3466 assert_eq!(bs_claims.len(), 1);
3467 if let MessageSendEvent::UpdateHTLCs { updates, .. } = &bs_claims[0] {
3468 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
3469 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
3470 } else { panic!(); }
3472 expect_payment_sent!(nodes[0], payment_preimage);
3474 let ds_claim_msgs = nodes[3].node.get_and_clear_pending_msg_events();
3475 assert_eq!(ds_claim_msgs.len(), 1);
3476 let cs_claim_msgs = if let MessageSendEvent::UpdateHTLCs { updates, .. } = &ds_claim_msgs[0] {
3477 nodes[2].node.handle_update_fulfill_htlc(&nodes[3].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
3478 let cs_claim_msgs = nodes[2].node.get_and_clear_pending_msg_events();
3479 check_added_monitors(&nodes[2], 1);
3480 commitment_signed_dance!(nodes[2], nodes[3], updates.commitment_signed, false, true);
3481 expect_payment_forwarded!(nodes[2], nodes[0], nodes[3], Some(1000), false, false);
3483 } else { panic!(); };
3485 assert_eq!(cs_claim_msgs.len(), 1);
3486 if let MessageSendEvent::UpdateHTLCs { updates, .. } = &cs_claim_msgs[0] {
3487 nodes[0].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
3488 commitment_signed_dance!(nodes[0], nodes[2], updates.commitment_signed, false, true);
3489 } else { panic!(); }
3491 expect_payment_path_successful!(nodes[0]);
3496 fn claim_from_closed_chan() {
3497 do_claim_from_closed_chan(true);
3498 do_claim_from_closed_chan(false);
3502 fn test_custom_tlvs_basic() {
3503 do_test_custom_tlvs(false, false, false);
3504 do_test_custom_tlvs(true, false, false);
3508 fn test_custom_tlvs_explicit_claim() {
3509 // Test that when receiving even custom TLVs the user must explicitly accept in case they
3511 do_test_custom_tlvs(false, true, false);
3512 do_test_custom_tlvs(false, true, true);
3515 fn do_test_custom_tlvs(spontaneous: bool, even_tlvs: bool, known_tlvs: bool) {
3516 let chanmon_cfgs = create_chanmon_cfgs(2);
3517 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3518 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None; 2]);
3519 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3521 create_announced_chan_between_nodes(&nodes, 0, 1);
3523 let amt_msat = 100_000;
3524 let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[0], &nodes[1], amt_msat);
3525 let payment_id = PaymentId(our_payment_hash.0);
3526 let custom_tlvs = vec![
3527 (if even_tlvs { 5482373482 } else { 5482373483 }, vec![1, 2, 3, 4]),
3528 (5482373487, vec![0x42u8; 16]),
3530 let onion_fields = RecipientOnionFields {
3531 payment_secret: if spontaneous { None } else { Some(our_payment_secret) },
3532 payment_metadata: None,
3533 custom_tlvs: custom_tlvs.clone()
3536 nodes[0].node.send_spontaneous_payment(&route, Some(our_payment_preimage), onion_fields, payment_id).unwrap();
3538 nodes[0].node.send_payment_with_route(&route, our_payment_hash, onion_fields, payment_id).unwrap();
3540 check_added_monitors(&nodes[0], 1);
3542 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3543 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
3544 let mut payment_event = SendEvent::from_event(ev);
3546 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3547 check_added_monitors!(&nodes[1], 0);
3548 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3549 expect_pending_htlcs_forwardable!(nodes[1]);
3551 let events = nodes[1].node.get_and_clear_pending_events();
3552 assert_eq!(events.len(), 1);
3554 Event::PaymentClaimable { ref onion_fields, .. } => {
3555 assert_eq!(onion_fields.clone().unwrap().custom_tlvs().clone(), custom_tlvs);
3557 _ => panic!("Unexpected event"),
3560 match (known_tlvs, even_tlvs) {
3562 nodes[1].node.claim_funds_with_known_custom_tlvs(our_payment_preimage);
3563 let expected_total_fee_msat = pass_claimed_payment_along_route(&nodes[0], &[&[&nodes[1]]], &[0; 1], false, our_payment_preimage);
3564 expect_payment_sent!(&nodes[0], our_payment_preimage, Some(expected_total_fee_msat));
3567 claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
3570 nodes[1].node.claim_funds(our_payment_preimage);
3571 let expected_destinations = vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }];
3572 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], expected_destinations);
3573 pass_failed_payment_back(&nodes[0], &[&[&nodes[1]]], false, our_payment_hash, PaymentFailureReason::RecipientRejected);
3579 fn test_retry_custom_tlvs() {
3580 // Test that custom TLVs are successfully sent on retries
3581 let chanmon_cfgs = create_chanmon_cfgs(3);
3582 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3583 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3584 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3586 create_announced_chan_between_nodes(&nodes, 0, 1);
3587 let (chan_2_update, _, chan_2_id, _) = create_announced_chan_between_nodes(&nodes, 2, 1);
3590 send_payment(&nodes[2], &vec!(&nodes[1])[..], 1_500_000);
3592 let amt_msat = 1_000_000;
3593 let (route, payment_hash, payment_preimage, payment_secret) =
3594 get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
3596 // Initiate the payment
3597 let payment_id = PaymentId(payment_hash.0);
3598 let mut route_params = route.route_params.clone().unwrap();
3600 let custom_tlvs = vec![((1 << 16) + 1, vec![0x42u8; 16])];
3601 let onion_fields = RecipientOnionFields::secret_only(payment_secret);
3602 let onion_fields = onion_fields.with_custom_tlvs(custom_tlvs.clone()).unwrap();
3604 nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
3605 nodes[0].node.send_payment(payment_hash, onion_fields,
3606 payment_id, route_params.clone(), Retry::Attempts(1)).unwrap();
3607 check_added_monitors!(nodes[0], 1); // one monitor per path
3609 // Add the HTLC along the first hop.
3610 let htlc_updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
3611 let msgs::CommitmentUpdate { update_add_htlcs, commitment_signed, .. } = htlc_updates;
3612 assert_eq!(update_add_htlcs.len(), 1);
3613 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_htlcs[0]);
3614 commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
3616 // Attempt to forward the payment and complete the path's failure.
3617 expect_pending_htlcs_forwardable!(&nodes[1]);
3618 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(&nodes[1],
3619 vec![HTLCDestination::NextHopChannel {
3620 node_id: Some(nodes[2].node.get_our_node_id()),
3621 channel_id: chan_2_id
3623 check_added_monitors!(nodes[1], 1);
3625 let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3626 let msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } = htlc_updates;
3627 assert_eq!(update_fail_htlcs.len(), 1);
3628 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3629 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
3631 let mut events = nodes[0].node.get_and_clear_pending_events();
3633 Event::PendingHTLCsForwardable { .. } => {},
3634 _ => panic!("Unexpected event")
3637 expect_payment_failed_conditions_event(events, payment_hash, false,
3638 PaymentFailedConditions::new().mpp_parts_remain());
3640 // Rebalance the channel so the retry of the payment can succeed.
3641 send_payment(&nodes[2], &vec!(&nodes[1])[..], 1_500_000);
3643 // Retry the payment and make sure it succeeds
3644 route_params.payment_params.previously_failed_channels.push(chan_2_update.contents.short_channel_id);
3645 nodes[0].router.expect_find_route(route_params, Ok(route));
3646 nodes[0].node.process_pending_htlc_forwards();
3647 check_added_monitors!(nodes[0], 1);
3648 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3649 assert_eq!(events.len(), 1);
3650 let payment_claimable = pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000,
3651 payment_hash, Some(payment_secret), events.pop().unwrap(), true, None).unwrap();
3652 match payment_claimable {
3653 Event::PaymentClaimable { onion_fields, .. } => {
3654 assert_eq!(onion_fields.unwrap().custom_tlvs(), &custom_tlvs);
3656 _ => panic!("Unexpected event"),
3658 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
3662 fn test_custom_tlvs_consistency() {
3663 let even_type_1 = 1 << 16;
3664 let odd_type_1 = (1 << 16)+ 1;
3665 let even_type_2 = (1 << 16) + 2;
3666 let odd_type_2 = (1 << 16) + 3;
3667 let value_1 = || vec![1, 2, 3, 4];
3668 let differing_value_1 = || vec![1, 2, 3, 5];
3669 let value_2 = || vec![42u8; 16];
3671 // Drop missing odd tlvs
3672 do_test_custom_tlvs_consistency(
3673 vec![(odd_type_1, value_1()), (odd_type_2, value_2())],
3674 vec![(odd_type_1, value_1())],
3675 Some(vec![(odd_type_1, value_1())]),
3677 // Drop non-matching odd tlvs
3678 do_test_custom_tlvs_consistency(
3679 vec![(odd_type_1, value_1()), (odd_type_2, value_2())],
3680 vec![(odd_type_1, differing_value_1()), (odd_type_2, value_2())],
3681 Some(vec![(odd_type_2, value_2())]),
3683 // Fail missing even tlvs
3684 do_test_custom_tlvs_consistency(
3685 vec![(odd_type_1, value_1()), (even_type_2, value_2())],
3686 vec![(odd_type_1, value_1())],
3689 // Fail non-matching even tlvs
3690 do_test_custom_tlvs_consistency(
3691 vec![(even_type_1, value_1()), (odd_type_2, value_2())],
3692 vec![(even_type_1, differing_value_1()), (odd_type_2, value_2())],
3697 fn do_test_custom_tlvs_consistency(first_tlvs: Vec<(u64, Vec<u8>)>, second_tlvs: Vec<(u64, Vec<u8>)>,
3698 expected_receive_tlvs: Option<Vec<(u64, Vec<u8>)>>) {
3700 let chanmon_cfgs = create_chanmon_cfgs(4);
3701 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
3702 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
3703 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
3705 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
3706 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
3707 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
3708 let chan_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
3710 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
3711 .with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
3712 let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
3713 assert_eq!(route.paths.len(), 2);
3714 route.paths.sort_by(|path_a, _| {
3715 // Sort the path so that the path through nodes[1] comes first
3716 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
3717 core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
3720 let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[3]);
3721 let payment_id = PaymentId([42; 32]);
3722 let amt_msat = 15_000_000;
3725 let onion_fields = RecipientOnionFields {
3726 payment_secret: Some(our_payment_secret),
3727 payment_metadata: None,
3728 custom_tlvs: first_tlvs
3730 let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
3731 onion_fields.clone(), payment_id, &route).unwrap();
3732 let cur_height = nodes[0].best_block_info().1;
3733 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
3734 onion_fields.clone(), amt_msat, cur_height, payment_id,
3735 &None, session_privs[0]).unwrap();
3736 check_added_monitors!(nodes[0], 1);
3739 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3740 assert_eq!(events.len(), 1);
3741 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], amt_msat, our_payment_hash,
3742 Some(our_payment_secret), events.pop().unwrap(), false, None);
3744 assert!(nodes[3].node.get_and_clear_pending_events().is_empty());
3747 let onion_fields = RecipientOnionFields {
3748 payment_secret: Some(our_payment_secret),
3749 payment_metadata: None,
3750 custom_tlvs: second_tlvs
3752 nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
3753 onion_fields.clone(), amt_msat, cur_height, payment_id, &None, session_privs[1]).unwrap();
3754 check_added_monitors!(nodes[0], 1);
3757 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3758 assert_eq!(events.len(), 1);
3759 let payment_event = SendEvent::from_event(events.pop().unwrap());
3761 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3762 commitment_signed_dance!(nodes[2], nodes[0], payment_event.commitment_msg, false);
3764 expect_pending_htlcs_forwardable!(nodes[2]);
3765 check_added_monitors!(nodes[2], 1);
3767 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
3768 assert_eq!(events.len(), 1);
3769 let payment_event = SendEvent::from_event(events.pop().unwrap());
3771 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
3772 check_added_monitors!(nodes[3], 0);
3773 commitment_signed_dance!(nodes[3], nodes[2], payment_event.commitment_msg, true, true);
3775 expect_pending_htlcs_forwardable_ignore!(nodes[3]);
3776 nodes[3].node.process_pending_htlc_forwards();
3778 if let Some(expected_tlvs) = expected_receive_tlvs {
3779 // Claim and match expected
3780 let events = nodes[3].node.get_and_clear_pending_events();
3781 assert_eq!(events.len(), 1);
3783 Event::PaymentClaimable { ref onion_fields, .. } => {
3784 assert_eq!(onion_fields.clone().unwrap().custom_tlvs, expected_tlvs);
3786 _ => panic!("Unexpected event"),
3789 do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]],
3790 false, our_payment_preimage);
3791 expect_payment_sent(&nodes[0], our_payment_preimage, Some(Some(2000)), true, true);
3794 let expected_destinations = vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }];
3795 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], expected_destinations);
3796 check_added_monitors!(nodes[3], 1);
3798 let fail_updates_1 = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
3799 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
3800 commitment_signed_dance!(nodes[2], nodes[3], fail_updates_1.commitment_signed, false);
3802 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![
3803 HTLCDestination::NextHopChannel {
3804 node_id: Some(nodes[3].node.get_our_node_id()),
3805 channel_id: chan_2_3.2
3807 check_added_monitors!(nodes[2], 1);
3809 let fail_updates_2 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
3810 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
3811 commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
3813 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true,
3814 PaymentFailedConditions::new().mpp_parts_remain());
3818 fn do_test_payment_metadata_consistency(do_reload: bool, do_modify: bool) {
3819 // Check that a payment metadata received on one HTLC that doesn't match the one received on
3820 // another results in the HTLC being rejected.
3822 // We first set up a diamond shaped network, allowing us to split a payment into two HTLCs, the
3823 // first of which we'll deliver and the second of which we'll fail and then re-send with
3824 // modified payment metadata, which will in turn result in it being failed by the recipient.
3825 let chanmon_cfgs = create_chanmon_cfgs(4);
3826 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
3828 let new_chain_monitor;
3830 let mut config = test_default_channel_config();
3831 config.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 50;
3832 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, Some(config), Some(config), Some(config)]);
3833 let nodes_0_deserialized;
3835 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
3837 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
3838 let chan_id_bd = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0).2;
3839 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
3840 let chan_id_cd = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).2;
3842 // Pay more than half of each channel's max, requiring MPP
3843 let amt_msat = 750_000_000;
3844 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[3], Some(amt_msat));
3845 let payment_id = PaymentId(payment_hash.0);
3846 let payment_metadata = vec![44, 49, 52, 142];
3848 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
3849 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
3850 let mut route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
3852 // Send the MPP payment, delivering the updated commitment state to nodes[1].
3853 nodes[0].node.send_payment(payment_hash, RecipientOnionFields {
3854 payment_secret: Some(payment_secret), payment_metadata: Some(payment_metadata), custom_tlvs: vec![],
3855 }, payment_id, route_params.clone(), Retry::Attempts(1)).unwrap();
3856 check_added_monitors!(nodes[0], 2);
3858 let mut send_events = nodes[0].node.get_and_clear_pending_msg_events();
3859 assert_eq!(send_events.len(), 2);
3860 let first_send = SendEvent::from_event(send_events.pop().unwrap());
3861 let second_send = SendEvent::from_event(send_events.pop().unwrap());
3863 let (b_recv_ev, c_recv_ev) = if first_send.node_id == nodes[1].node.get_our_node_id() {
3864 (&first_send, &second_send)
3866 (&second_send, &first_send)
3868 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &b_recv_ev.msgs[0]);
3869 commitment_signed_dance!(nodes[1], nodes[0], b_recv_ev.commitment_msg, false, true);
3871 expect_pending_htlcs_forwardable!(nodes[1]);
3872 check_added_monitors(&nodes[1], 1);
3873 let b_forward_ev = SendEvent::from_node(&nodes[1]);
3874 nodes[3].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &b_forward_ev.msgs[0]);
3875 commitment_signed_dance!(nodes[3], nodes[1], b_forward_ev.commitment_msg, false, true);
3877 expect_pending_htlcs_forwardable!(nodes[3]);
3879 // Before delivering the second MPP HTLC to nodes[2], disconnect nodes[2] and nodes[3], which
3880 // will result in nodes[2] failing the HTLC back.
3881 nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id());
3882 nodes[3].node.peer_disconnected(&nodes[2].node.get_our_node_id());
3884 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &c_recv_ev.msgs[0]);
3885 commitment_signed_dance!(nodes[2], nodes[0], c_recv_ev.commitment_msg, false, true);
3887 let cs_fail = get_htlc_update_msgs(&nodes[2], &nodes[0].node.get_our_node_id());
3888 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &cs_fail.update_fail_htlcs[0]);
3889 commitment_signed_dance!(nodes[0], nodes[2], cs_fail.commitment_signed, false, true);
3891 let payment_fail_retryable_evs = nodes[0].node.get_and_clear_pending_events();
3892 assert_eq!(payment_fail_retryable_evs.len(), 2);
3893 if let Event::PaymentPathFailed { .. } = payment_fail_retryable_evs[0] {} else { panic!(); }
3894 if let Event::PendingHTLCsForwardable { .. } = payment_fail_retryable_evs[1] {} else { panic!(); }
3896 // Before we allow the HTLC to be retried, optionally change the payment_metadata we have
3897 // stored for our payment.
3899 nodes[0].node.test_set_payment_metadata(payment_id, Some(Vec::new()));
3902 // Optionally reload nodes[3] to check that the payment_metadata is properly serialized with
3903 // the payment state.
3905 let mon_bd = get_monitor!(nodes[3], chan_id_bd).encode();
3906 let mon_cd = get_monitor!(nodes[3], chan_id_cd).encode();
3907 reload_node!(nodes[3], config, &nodes[3].node.encode(), &[&mon_bd, &mon_cd],
3908 persister, new_chain_monitor, nodes_0_deserialized);
3909 nodes[1].node.peer_disconnected(&nodes[3].node.get_our_node_id());
3910 reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[3]));
3912 let mut reconnect_args = ReconnectArgs::new(&nodes[2], &nodes[3]);
3913 reconnect_args.send_channel_ready = (true, true);
3914 reconnect_nodes(reconnect_args);
3916 // Create a new channel between C and D as A will refuse to retry on the existing one because
3918 let chan_id_cd_2 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).2;
3920 // Now retry the failed HTLC.
3921 nodes[0].node.process_pending_htlc_forwards();
3922 check_added_monitors(&nodes[0], 1);
3923 let as_resend = SendEvent::from_node(&nodes[0]);
3924 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resend.msgs[0]);
3925 commitment_signed_dance!(nodes[2], nodes[0], as_resend.commitment_msg, false, true);
3927 expect_pending_htlcs_forwardable!(nodes[2]);
3928 check_added_monitors(&nodes[2], 1);
3929 let cs_forward = SendEvent::from_node(&nodes[2]);
3930 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &cs_forward.msgs[0]);
3931 commitment_signed_dance!(nodes[3], nodes[2], cs_forward.commitment_msg, false, true);
3933 // Finally, check that nodes[3] does the correct thing - either accepting the payment or, if
3934 // the payment metadata was modified, failing only the one modified HTLC and retaining the
3937 expect_pending_htlcs_forwardable_ignore!(nodes[3]);
3938 nodes[3].node.process_pending_htlc_forwards();
3939 expect_pending_htlcs_forwardable_conditions(nodes[3].node.get_and_clear_pending_events(),
3940 &[HTLCDestination::FailedPayment {payment_hash}]);
3941 nodes[3].node.process_pending_htlc_forwards();
3943 check_added_monitors(&nodes[3], 1);
3944 let ds_fail = get_htlc_update_msgs(&nodes[3], &nodes[2].node.get_our_node_id());
3946 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &ds_fail.update_fail_htlcs[0]);
3947 commitment_signed_dance!(nodes[2], nodes[3], ds_fail.commitment_signed, false, true);
3948 expect_pending_htlcs_forwardable_conditions(nodes[2].node.get_and_clear_pending_events(),
3949 &[HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_id_cd_2 }]);
3951 expect_pending_htlcs_forwardable!(nodes[3]);
3952 expect_payment_claimable!(nodes[3], payment_hash, payment_secret, amt_msat);
3953 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
3958 fn test_payment_metadata_consistency() {
3959 do_test_payment_metadata_consistency(true, true);
3960 do_test_payment_metadata_consistency(true, false);
3961 do_test_payment_metadata_consistency(false, true);
3962 do_test_payment_metadata_consistency(false, false);