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 //! Functional tests which test the correct handling of ChannelMonitorUpdateErr returns from
12 //! There are a bunch of these as their handling is relatively error-prone so they are split out
13 //! here. See also the chanmon_fail_consistency fuzz test.
15 use bitcoin::blockdata::block::{Block, BlockHeader};
16 use bitcoin::blockdata::constants::genesis_block;
17 use bitcoin::hash_types::BlockHash;
18 use bitcoin::network::constants::Network;
19 use chain::channelmonitor::ChannelMonitor;
20 use chain::transaction::OutPoint;
21 use chain::{ChannelMonitorUpdateErr, Listen, Watch};
22 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
23 use ln::channel::AnnouncementSigsState;
24 use ln::features::InitFeatures;
26 use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
27 use util::config::UserConfig;
28 use util::enforcing_trait_impls::EnforcingSigner;
29 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason};
30 use util::errors::APIError;
31 use util::ser::{ReadableArgs, Writeable};
32 use util::test_utils::TestBroadcaster;
34 use ln::functional_test_utils::*;
40 use sync::{Arc, Mutex};
43 fn test_simple_monitor_permanent_update_fail() {
44 // Test that we handle a simple permanent monitor update failure
45 let chanmon_cfgs = create_chanmon_cfgs(2);
46 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
47 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
48 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
49 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
51 let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
52 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::PermanentFailure));
53 unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), true, APIError::ChannelUnavailable {..}, {});
54 check_added_monitors!(nodes[0], 2);
56 let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
57 assert_eq!(events_1.len(), 2);
59 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
60 _ => panic!("Unexpected event"),
63 MessageSendEvent::HandleError { node_id, .. } => assert_eq!(node_id, nodes[1].node.get_our_node_id()),
64 _ => panic!("Unexpected event"),
67 // TODO: Once we hit the chain with the failure transaction we should check that we get a
68 // PaymentPathFailed event
70 assert_eq!(nodes[0].node.list_channels().len(), 0);
71 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "ChannelMonitor storage failure".to_string() });
75 fn test_monitor_and_persister_update_fail() {
76 // Test that if both updating the `ChannelMonitor` and persisting the updated
77 // `ChannelMonitor` fail, then the failure from updating the `ChannelMonitor`
78 // one that gets returned.
79 let chanmon_cfgs = create_chanmon_cfgs(2);
80 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
81 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
82 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
84 // Create some initial channel
85 let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
86 let outpoint = OutPoint { txid: chan.3.txid(), index: 0 };
88 // Rebalance the network to generate htlc in the two directions
89 send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
91 // Route an HTLC from node 0 to node 1 (but don't settle)
92 let preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
94 // Make a copy of the ChainMonitor so we can capture the error it returns on a
95 // bogus update. Note that if instead we updated the nodes[0]'s ChainMonitor
96 // directly, the node would fail to be `Drop`'d at the end because its
97 // ChannelManager and ChainMonitor would be out of sync.
98 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
99 let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
100 let persister = test_utils::TestPersister::new();
101 let tx_broadcaster = TestBroadcaster {
102 txn_broadcasted: Mutex::new(Vec::new()),
103 // Because we will connect a block at height 200 below, we need the TestBroadcaster to know
104 // that we are at height 200 so that it doesn't think we're violating the time lock
105 // requirements of transactions broadcasted at that point.
106 blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 200); 200])),
109 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
110 let mut w = test_utils::TestVecWriter(Vec::new());
111 monitor.write(&mut w).unwrap();
112 let new_monitor = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
113 &mut io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1;
114 assert!(new_monitor == *monitor);
115 let chain_mon = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
116 assert!(chain_mon.watch_channel(outpoint, new_monitor).is_ok());
119 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
120 chain_mon.chain_monitor.block_connected(&Block { header, txdata: vec![] }, 200);
122 // Set the persister's return value to be a TemporaryFailure.
123 persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
125 // Try to update ChannelMonitor
126 assert!(nodes[1].node.claim_funds(preimage));
127 check_added_monitors!(nodes[1], 1);
128 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
129 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
130 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
131 if let Some(ref mut channel) = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&chan.2) {
132 if let Ok((_, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
133 // Check that even though the persister is returning a TemporaryFailure,
134 // because the update is bogus, ultimately the error that's returned
135 // should be a PermanentFailure.
136 if let Err(ChannelMonitorUpdateErr::PermanentFailure) = chain_mon.chain_monitor.update_channel(outpoint, update.clone()) {} else { panic!("Expected monitor error to be permanent"); }
137 logger.assert_log_regex("lightning::chain::chainmonitor".to_string(), regex::Regex::new("Failed to persist ChannelMonitor update for channel [0-9a-f]*: TemporaryFailure").unwrap(), 1);
138 if let Ok(_) = nodes[0].chain_monitor.update_channel(outpoint, update) {} else { assert!(false); }
139 } else { assert!(false); }
140 } else { assert!(false); };
142 check_added_monitors!(nodes[0], 1);
143 let events = nodes[0].node.get_and_clear_pending_events();
144 assert_eq!(events.len(), 1);
147 fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
148 // Test that we can recover from a simple temporary monitor update failure optionally with
149 // a disconnect in between
150 let chanmon_cfgs = create_chanmon_cfgs(2);
151 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
152 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
153 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
154 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
156 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
158 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
161 unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), false, APIError::MonitorUpdateFailed, {});
162 check_added_monitors!(nodes[0], 1);
165 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
166 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
167 assert_eq!(nodes[0].node.list_channels().len(), 1);
170 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
171 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
172 reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
175 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
176 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
177 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
178 check_added_monitors!(nodes[0], 0);
180 let mut events_2 = nodes[0].node.get_and_clear_pending_msg_events();
181 assert_eq!(events_2.len(), 1);
182 let payment_event = SendEvent::from_event(events_2.pop().unwrap());
183 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
184 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
185 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
187 expect_pending_htlcs_forwardable!(nodes[1]);
189 let events_3 = nodes[1].node.get_and_clear_pending_events();
190 assert_eq!(events_3.len(), 1);
192 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
193 assert_eq!(payment_hash_1, *payment_hash);
194 assert_eq!(amt, 1000000);
196 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
197 assert!(payment_preimage.is_none());
198 assert_eq!(payment_secret_1, *payment_secret);
200 _ => panic!("expected PaymentPurpose::InvoicePayment")
203 _ => panic!("Unexpected event"),
206 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
208 // Now set it to failed again...
209 let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
211 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
212 unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateFailed, {});
213 check_added_monitors!(nodes[0], 1);
216 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
217 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
218 assert_eq!(nodes[0].node.list_channels().len(), 1);
221 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
222 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
223 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
226 // ...and make sure we can force-close a frozen channel
227 nodes[0].node.force_close_channel(&channel_id).unwrap();
228 check_added_monitors!(nodes[0], 1);
229 check_closed_broadcast!(nodes[0], true);
231 // TODO: Once we hit the chain with the failure transaction we should check that we get a
232 // PaymentPathFailed event
234 assert_eq!(nodes[0].node.list_channels().len(), 0);
235 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
239 fn test_simple_monitor_temporary_update_fail() {
240 do_test_simple_monitor_temporary_update_fail(false);
241 do_test_simple_monitor_temporary_update_fail(true);
244 fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
245 let disconnect_flags = 8 | 16;
247 // Test that we can recover from a temporary monitor update failure with some in-flight
248 // HTLCs going on at the same time potentially with some disconnection thrown in.
249 // * First we route a payment, then get a temporary monitor update failure when trying to
250 // route a second payment. We then claim the first payment.
251 // * If disconnect_count is set, we will disconnect at this point (which is likely as
252 // TemporaryFailure likely indicates net disconnect which resulted in failing to update
253 // the ChannelMonitor on a watchtower).
254 // * If !(disconnect_count & 16) we deliver a update_fulfill_htlc/CS for the first payment
255 // immediately, otherwise we wait disconnect and deliver them via the reconnect
256 // channel_reestablish processing (ie disconnect_count & 16 makes no sense if
257 // disconnect_count & !disconnect_flags is 0).
258 // * We then update the channel monitor, reconnecting if disconnect_count is set and walk
259 // through message sending, potentially disconnect/reconnecting multiple times based on
260 // disconnect_count, to get the update_fulfill_htlc through.
261 // * We then walk through more message exchanges to get the original update_add_htlc
262 // through, swapping message ordering based on disconnect_count & 8 and optionally
263 // disconnect/reconnecting based on disconnect_count.
264 let chanmon_cfgs = create_chanmon_cfgs(2);
265 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
266 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
267 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
268 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
270 let (payment_preimage_1, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
272 // Now try to send a second payment which will fail to send
273 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
275 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
276 unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateFailed, {});
277 check_added_monitors!(nodes[0], 1);
280 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
281 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
282 assert_eq!(nodes[0].node.list_channels().len(), 1);
284 // Claim the previous payment, which will result in a update_fulfill_htlc/CS from nodes[1]
285 // but nodes[0] won't respond since it is frozen.
286 assert!(nodes[1].node.claim_funds(payment_preimage_1));
287 check_added_monitors!(nodes[1], 1);
288 let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
289 assert_eq!(events_2.len(), 1);
290 let (bs_initial_fulfill, bs_initial_commitment_signed) = match events_2[0] {
291 MessageSendEvent::UpdateHTLCs { ref 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 } } => {
292 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
293 assert!(update_add_htlcs.is_empty());
294 assert_eq!(update_fulfill_htlcs.len(), 1);
295 assert!(update_fail_htlcs.is_empty());
296 assert!(update_fail_malformed_htlcs.is_empty());
297 assert!(update_fee.is_none());
299 if (disconnect_count & 16) == 0 {
300 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
301 let events_3 = nodes[0].node.get_and_clear_pending_events();
302 assert_eq!(events_3.len(), 1);
304 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
305 assert_eq!(*payment_preimage, payment_preimage_1);
306 assert_eq!(*payment_hash, payment_hash_1);
308 _ => panic!("Unexpected event"),
311 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
312 check_added_monitors!(nodes[0], 1);
313 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
314 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented generation of RAA".to_string(), 1);
317 (update_fulfill_htlcs[0].clone(), commitment_signed.clone())
319 _ => panic!("Unexpected event"),
322 if disconnect_count & !disconnect_flags > 0 {
323 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
324 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
327 // Now fix monitor updating...
328 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
329 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
330 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
331 check_added_monitors!(nodes[0], 0);
333 macro_rules! disconnect_reconnect_peers { () => { {
334 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
335 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
337 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
338 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
339 assert_eq!(reestablish_1.len(), 1);
340 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
341 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
342 assert_eq!(reestablish_2.len(), 1);
344 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
345 let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
346 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
347 let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
349 assert!(as_resp.0.is_none());
350 assert!(bs_resp.0.is_none());
352 (reestablish_1, reestablish_2, as_resp, bs_resp)
355 let (payment_event, initial_revoke_and_ack) = if disconnect_count & !disconnect_flags > 0 {
356 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
357 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
359 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
360 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
361 assert_eq!(reestablish_1.len(), 1);
362 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
363 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
364 assert_eq!(reestablish_2.len(), 1);
366 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
367 check_added_monitors!(nodes[0], 0);
368 let mut as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
369 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
370 check_added_monitors!(nodes[1], 0);
371 let mut bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
373 assert!(as_resp.0.is_none());
374 assert!(bs_resp.0.is_none());
376 assert!(bs_resp.1.is_none());
377 if (disconnect_count & 16) == 0 {
378 assert!(bs_resp.2.is_none());
380 assert!(as_resp.1.is_some());
381 assert!(as_resp.2.is_some());
382 assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
384 assert!(bs_resp.2.as_ref().unwrap().update_add_htlcs.is_empty());
385 assert!(bs_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
386 assert!(bs_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
387 assert!(bs_resp.2.as_ref().unwrap().update_fee.is_none());
388 assert!(bs_resp.2.as_ref().unwrap().update_fulfill_htlcs == vec![bs_initial_fulfill]);
389 assert!(bs_resp.2.as_ref().unwrap().commitment_signed == bs_initial_commitment_signed);
391 assert!(as_resp.1.is_none());
393 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().update_fulfill_htlcs[0]);
394 let events_3 = nodes[0].node.get_and_clear_pending_events();
395 assert_eq!(events_3.len(), 1);
397 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
398 assert_eq!(*payment_preimage, payment_preimage_1);
399 assert_eq!(*payment_hash, payment_hash_1);
401 _ => panic!("Unexpected event"),
404 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_resp.2.as_ref().unwrap().commitment_signed);
405 let as_resp_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
406 // No commitment_signed so get_event_msg's assert(len == 1) passes
407 check_added_monitors!(nodes[0], 1);
409 as_resp.1 = Some(as_resp_raa);
413 if disconnect_count & !disconnect_flags > 1 {
414 let (second_reestablish_1, second_reestablish_2, second_as_resp, second_bs_resp) = disconnect_reconnect_peers!();
416 if (disconnect_count & 16) == 0 {
417 assert!(reestablish_1 == second_reestablish_1);
418 assert!(reestablish_2 == second_reestablish_2);
420 assert!(as_resp == second_as_resp);
421 assert!(bs_resp == second_bs_resp);
424 (SendEvent::from_commitment_update(nodes[1].node.get_our_node_id(), as_resp.2.unwrap()), as_resp.1.unwrap())
426 let mut events_4 = nodes[0].node.get_and_clear_pending_msg_events();
427 assert_eq!(events_4.len(), 2);
428 (SendEvent::from_event(events_4.remove(0)), match events_4[0] {
429 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
430 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
433 _ => panic!("Unexpected event"),
437 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
439 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
440 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
441 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
442 // nodes[1] is awaiting an RAA from nodes[0] still so get_event_msg's assert(len == 1) passes
443 check_added_monitors!(nodes[1], 1);
445 if disconnect_count & !disconnect_flags > 2 {
446 let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
448 assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
449 assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
451 assert!(as_resp.2.is_none());
452 assert!(bs_resp.2.is_none());
455 let as_commitment_update;
456 let bs_second_commitment_update;
458 macro_rules! handle_bs_raa { () => {
459 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
460 as_commitment_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
461 assert!(as_commitment_update.update_add_htlcs.is_empty());
462 assert!(as_commitment_update.update_fulfill_htlcs.is_empty());
463 assert!(as_commitment_update.update_fail_htlcs.is_empty());
464 assert!(as_commitment_update.update_fail_malformed_htlcs.is_empty());
465 assert!(as_commitment_update.update_fee.is_none());
466 check_added_monitors!(nodes[0], 1);
469 macro_rules! handle_initial_raa { () => {
470 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &initial_revoke_and_ack);
471 bs_second_commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
472 assert!(bs_second_commitment_update.update_add_htlcs.is_empty());
473 assert!(bs_second_commitment_update.update_fulfill_htlcs.is_empty());
474 assert!(bs_second_commitment_update.update_fail_htlcs.is_empty());
475 assert!(bs_second_commitment_update.update_fail_malformed_htlcs.is_empty());
476 assert!(bs_second_commitment_update.update_fee.is_none());
477 check_added_monitors!(nodes[1], 1);
480 if (disconnect_count & 8) == 0 {
483 if disconnect_count & !disconnect_flags > 3 {
484 let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
486 assert!(as_resp.1.unwrap() == initial_revoke_and_ack);
487 assert!(bs_resp.1.is_none());
489 assert!(as_resp.2.unwrap() == as_commitment_update);
490 assert!(bs_resp.2.is_none());
492 assert!(as_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
495 handle_initial_raa!();
497 if disconnect_count & !disconnect_flags > 4 {
498 let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
500 assert!(as_resp.1.is_none());
501 assert!(bs_resp.1.is_none());
503 assert!(as_resp.2.unwrap() == as_commitment_update);
504 assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
507 handle_initial_raa!();
509 if disconnect_count & !disconnect_flags > 3 {
510 let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
512 assert!(as_resp.1.is_none());
513 assert!(bs_resp.1.unwrap() == bs_revoke_and_ack);
515 assert!(as_resp.2.is_none());
516 assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
518 assert!(bs_resp.3 == RAACommitmentOrder::RevokeAndACKFirst);
523 if disconnect_count & !disconnect_flags > 4 {
524 let (_, _, as_resp, bs_resp) = disconnect_reconnect_peers!();
526 assert!(as_resp.1.is_none());
527 assert!(bs_resp.1.is_none());
529 assert!(as_resp.2.unwrap() == as_commitment_update);
530 assert!(bs_resp.2.unwrap() == bs_second_commitment_update);
534 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_update.commitment_signed);
535 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
536 // No commitment_signed so get_event_msg's assert(len == 1) passes
537 check_added_monitors!(nodes[0], 1);
539 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_update.commitment_signed);
540 let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
541 // No commitment_signed so get_event_msg's assert(len == 1) passes
542 check_added_monitors!(nodes[1], 1);
544 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
545 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
546 check_added_monitors!(nodes[1], 1);
548 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack);
549 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
550 check_added_monitors!(nodes[0], 1);
551 expect_payment_path_successful!(nodes[0]);
553 expect_pending_htlcs_forwardable!(nodes[1]);
555 let events_5 = nodes[1].node.get_and_clear_pending_events();
556 assert_eq!(events_5.len(), 1);
558 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
559 assert_eq!(payment_hash_2, *payment_hash);
560 assert_eq!(amt, 1000000);
562 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
563 assert!(payment_preimage.is_none());
564 assert_eq!(payment_secret_2, *payment_secret);
566 _ => panic!("expected PaymentPurpose::InvoicePayment")
569 _ => panic!("Unexpected event"),
572 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
576 fn test_monitor_temporary_update_fail_a() {
577 do_test_monitor_temporary_update_fail(0);
578 do_test_monitor_temporary_update_fail(1);
579 do_test_monitor_temporary_update_fail(2);
580 do_test_monitor_temporary_update_fail(3);
581 do_test_monitor_temporary_update_fail(4);
582 do_test_monitor_temporary_update_fail(5);
586 fn test_monitor_temporary_update_fail_b() {
587 do_test_monitor_temporary_update_fail(2 | 8);
588 do_test_monitor_temporary_update_fail(3 | 8);
589 do_test_monitor_temporary_update_fail(4 | 8);
590 do_test_monitor_temporary_update_fail(5 | 8);
594 fn test_monitor_temporary_update_fail_c() {
595 do_test_monitor_temporary_update_fail(1 | 16);
596 do_test_monitor_temporary_update_fail(2 | 16);
597 do_test_monitor_temporary_update_fail(3 | 16);
598 do_test_monitor_temporary_update_fail(2 | 8 | 16);
599 do_test_monitor_temporary_update_fail(3 | 8 | 16);
603 fn test_monitor_update_fail_cs() {
604 // Tests handling of a monitor update failure when processing an incoming commitment_signed
605 let chanmon_cfgs = create_chanmon_cfgs(2);
606 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
607 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
608 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
609 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
611 let (route, our_payment_hash, payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
613 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
614 check_added_monitors!(nodes[0], 1);
617 let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
618 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
620 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
621 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event.commitment_msg);
622 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
623 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
624 check_added_monitors!(nodes[1], 1);
625 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
627 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
628 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
629 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
630 check_added_monitors!(nodes[1], 0);
631 let responses = nodes[1].node.get_and_clear_pending_msg_events();
632 assert_eq!(responses.len(), 2);
635 MessageSendEvent::SendRevokeAndACK { ref msg, ref node_id } => {
636 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
637 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &msg);
638 check_added_monitors!(nodes[0], 1);
640 _ => panic!("Unexpected event"),
643 MessageSendEvent::UpdateHTLCs { ref updates, ref node_id } => {
644 assert!(updates.update_add_htlcs.is_empty());
645 assert!(updates.update_fulfill_htlcs.is_empty());
646 assert!(updates.update_fail_htlcs.is_empty());
647 assert!(updates.update_fail_malformed_htlcs.is_empty());
648 assert!(updates.update_fee.is_none());
649 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
651 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
652 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
653 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
654 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
655 check_added_monitors!(nodes[0], 1);
656 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
658 _ => panic!("Unexpected event"),
661 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
662 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
663 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
664 check_added_monitors!(nodes[0], 0);
666 let final_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
667 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &final_raa);
668 check_added_monitors!(nodes[1], 1);
670 expect_pending_htlcs_forwardable!(nodes[1]);
672 let events = nodes[1].node.get_and_clear_pending_events();
673 assert_eq!(events.len(), 1);
675 Event::PaymentReceived { payment_hash, ref purpose, amt } => {
676 assert_eq!(payment_hash, our_payment_hash);
677 assert_eq!(amt, 1000000);
679 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
680 assert!(payment_preimage.is_none());
681 assert_eq!(our_payment_secret, *payment_secret);
683 _ => panic!("expected PaymentPurpose::InvoicePayment")
686 _ => panic!("Unexpected event"),
689 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
693 fn test_monitor_update_fail_no_rebroadcast() {
694 // Tests handling of a monitor update failure when no message rebroadcasting on
695 // channel_monitor_updated() is required. Backported from chanmon_fail_consistency
697 let chanmon_cfgs = create_chanmon_cfgs(2);
698 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
699 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
700 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
701 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
703 let (route, our_payment_hash, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
705 nodes[0].node.send_payment(&route, our_payment_hash, &Some(payment_secret_1)).unwrap();
706 check_added_monitors!(nodes[0], 1);
709 let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
710 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
711 let bs_raa = commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true, false, true);
713 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
714 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_raa);
715 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
716 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
717 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
718 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
719 check_added_monitors!(nodes[1], 1);
721 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
722 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
723 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
724 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
725 check_added_monitors!(nodes[1], 0);
726 expect_pending_htlcs_forwardable!(nodes[1]);
728 let events = nodes[1].node.get_and_clear_pending_events();
729 assert_eq!(events.len(), 1);
731 Event::PaymentReceived { payment_hash, .. } => {
732 assert_eq!(payment_hash, our_payment_hash);
734 _ => panic!("Unexpected event"),
737 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
741 fn test_monitor_update_raa_while_paused() {
742 // Tests handling of an RAA while monitor updating has already been marked failed.
743 // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
744 let chanmon_cfgs = create_chanmon_cfgs(2);
745 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
746 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
747 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
748 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
750 send_payment(&nodes[0], &[&nodes[1]], 5000000);
751 let (route, our_payment_hash_1, payment_preimage_1, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
753 nodes[0].node.send_payment(&route, our_payment_hash_1, &Some(our_payment_secret_1)).unwrap();
754 check_added_monitors!(nodes[0], 1);
756 let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
758 let (route, our_payment_hash_2, payment_preimage_2, our_payment_secret_2) = get_route_and_payment_hash!(nodes[1], nodes[0], 1000000);
760 nodes[1].node.send_payment(&route, our_payment_hash_2, &Some(our_payment_secret_2)).unwrap();
761 check_added_monitors!(nodes[1], 1);
763 let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0));
765 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event_1.msgs[0]);
766 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event_1.commitment_msg);
767 check_added_monitors!(nodes[1], 1);
768 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
770 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
771 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_2.msgs[0]);
772 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_2.commitment_msg);
773 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
774 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
775 check_added_monitors!(nodes[0], 1);
777 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
778 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
779 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented responses to RAA".to_string(), 1);
780 check_added_monitors!(nodes[0], 1);
782 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
783 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
784 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
785 check_added_monitors!(nodes[0], 0);
787 let as_update_raa = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
788 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_update_raa.0);
789 check_added_monitors!(nodes[1], 1);
790 let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
792 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update_raa.1);
793 check_added_monitors!(nodes[1], 1);
794 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
796 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed);
797 check_added_monitors!(nodes[0], 1);
798 let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
800 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
801 check_added_monitors!(nodes[0], 1);
802 expect_pending_htlcs_forwardable!(nodes[0]);
803 expect_payment_received!(nodes[0], our_payment_hash_2, our_payment_secret_2, 1000000);
805 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
806 check_added_monitors!(nodes[1], 1);
807 expect_pending_htlcs_forwardable!(nodes[1]);
808 expect_payment_received!(nodes[1], our_payment_hash_1, our_payment_secret_1, 1000000);
810 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
811 claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_2);
814 fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
815 // Tests handling of a monitor update failure when processing an incoming RAA
816 let chanmon_cfgs = create_chanmon_cfgs(3);
817 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
818 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
819 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
820 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
821 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
823 // Rebalance a bit so that we can send backwards from 2 to 1.
824 send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
826 // Route a first payment that we'll fail backwards
827 let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
829 // Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
830 assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
831 expect_pending_htlcs_forwardable!(nodes[2]);
832 check_added_monitors!(nodes[2], 1);
834 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
835 assert!(updates.update_add_htlcs.is_empty());
836 assert!(updates.update_fulfill_htlcs.is_empty());
837 assert_eq!(updates.update_fail_htlcs.len(), 1);
838 assert!(updates.update_fail_malformed_htlcs.is_empty());
839 assert!(updates.update_fee.is_none());
840 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
842 let bs_revoke_and_ack = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
843 check_added_monitors!(nodes[0], 0);
845 // While the second channel is AwaitingRAA, forward a second payment to get it into the
847 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
849 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
850 check_added_monitors!(nodes[0], 1);
853 let mut send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
854 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
855 commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false);
857 expect_pending_htlcs_forwardable!(nodes[1]);
858 check_added_monitors!(nodes[1], 0);
859 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
861 // Now fail monitor updating.
862 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
863 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
864 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
865 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
866 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
867 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
868 check_added_monitors!(nodes[1], 1);
870 // Forward a third payment which will also be added to the holding cell, despite the channel
871 // being paused waiting a monitor update.
872 let (route, payment_hash_3, _, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
874 nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
875 check_added_monitors!(nodes[0], 1);
878 chanmon_cfgs[1].persister.set_update_ret(Ok(())); // We succeed in updating the monitor for the first channel
879 send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
880 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
881 commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true);
882 check_added_monitors!(nodes[1], 0);
884 // Call forward_pending_htlcs and check that the new HTLC was simply added to the holding cell
885 // and not forwarded.
886 expect_pending_htlcs_forwardable!(nodes[1]);
887 check_added_monitors!(nodes[1], 0);
888 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
890 let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs {
891 // Try to route another payment backwards from 2 to make sure 1 holds off on responding
892 let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
893 nodes[2].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4)).unwrap();
894 check_added_monitors!(nodes[2], 1);
896 send_event = SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
897 nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &send_event.msgs[0]);
898 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &send_event.commitment_msg);
899 check_added_monitors!(nodes[1], 1);
900 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
901 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented generation of RAA".to_string(), 1);
902 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
903 (Some(payment_preimage_4), Some(payment_hash_4))
904 } else { (None, None) };
906 // Restore monitor updating, ensuring we immediately get a fail-back update and a
907 // update_add update.
908 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
909 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_2.2).unwrap().clone();
910 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
911 check_added_monitors!(nodes[1], 0);
912 expect_pending_htlcs_forwardable!(nodes[1]);
913 check_added_monitors!(nodes[1], 1);
915 let mut events_3 = nodes[1].node.get_and_clear_pending_msg_events();
916 if test_ignore_second_cs {
917 assert_eq!(events_3.len(), 3);
919 assert_eq!(events_3.len(), 2);
922 // Note that the ordering of the events for different nodes is non-prescriptive, though the
923 // ordering of the two events that both go to nodes[2] have to stay in the same order.
924 let messages_a = match events_3.pop().unwrap() {
925 MessageSendEvent::UpdateHTLCs { node_id, mut updates } => {
926 assert_eq!(node_id, nodes[0].node.get_our_node_id());
927 assert!(updates.update_fulfill_htlcs.is_empty());
928 assert_eq!(updates.update_fail_htlcs.len(), 1);
929 assert!(updates.update_fail_malformed_htlcs.is_empty());
930 assert!(updates.update_add_htlcs.is_empty());
931 assert!(updates.update_fee.is_none());
932 (updates.update_fail_htlcs.remove(0), updates.commitment_signed)
934 _ => panic!("Unexpected event type!"),
936 let raa = if test_ignore_second_cs {
937 match events_3.remove(1) {
938 MessageSendEvent::SendRevokeAndACK { node_id, msg } => {
939 assert_eq!(node_id, nodes[2].node.get_our_node_id());
942 _ => panic!("Unexpected event"),
945 let send_event_b = SendEvent::from_event(events_3.remove(0));
946 assert_eq!(send_event_b.node_id, nodes[2].node.get_our_node_id());
948 // Now deliver the new messages...
950 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &messages_a.0);
951 commitment_signed_dance!(nodes[0], nodes[1], messages_a.1, false);
952 expect_payment_failed!(nodes[0], payment_hash_1, true);
954 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_b.msgs[0]);
956 if test_ignore_second_cs {
957 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_b.commitment_msg);
958 check_added_monitors!(nodes[2], 1);
959 let bs_revoke_and_ack = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
960 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa.unwrap());
961 check_added_monitors!(nodes[2], 1);
962 let bs_cs = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
963 assert!(bs_cs.update_add_htlcs.is_empty());
964 assert!(bs_cs.update_fail_htlcs.is_empty());
965 assert!(bs_cs.update_fail_malformed_htlcs.is_empty());
966 assert!(bs_cs.update_fulfill_htlcs.is_empty());
967 assert!(bs_cs.update_fee.is_none());
969 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
970 check_added_monitors!(nodes[1], 1);
971 as_cs = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
973 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_cs.commitment_signed);
974 check_added_monitors!(nodes[1], 1);
976 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_b.commitment_msg);
977 check_added_monitors!(nodes[2], 1);
979 let bs_revoke_and_commit = nodes[2].node.get_and_clear_pending_msg_events();
980 assert_eq!(bs_revoke_and_commit.len(), 2);
981 match bs_revoke_and_commit[0] {
982 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
983 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
984 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &msg);
985 check_added_monitors!(nodes[1], 1);
987 _ => panic!("Unexpected event"),
990 as_cs = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
992 match bs_revoke_and_commit[1] {
993 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
994 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
995 assert!(updates.update_add_htlcs.is_empty());
996 assert!(updates.update_fail_htlcs.is_empty());
997 assert!(updates.update_fail_malformed_htlcs.is_empty());
998 assert!(updates.update_fulfill_htlcs.is_empty());
999 assert!(updates.update_fee.is_none());
1000 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
1001 check_added_monitors!(nodes[1], 1);
1003 _ => panic!("Unexpected event"),
1007 assert_eq!(as_cs.update_add_htlcs.len(), 1);
1008 assert!(as_cs.update_fail_htlcs.is_empty());
1009 assert!(as_cs.update_fail_malformed_htlcs.is_empty());
1010 assert!(as_cs.update_fulfill_htlcs.is_empty());
1011 assert!(as_cs.update_fee.is_none());
1012 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1015 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &as_cs.update_add_htlcs[0]);
1016 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_cs.commitment_signed);
1017 check_added_monitors!(nodes[2], 1);
1018 let bs_second_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1020 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
1021 check_added_monitors!(nodes[2], 1);
1022 let bs_second_cs = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1024 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_second_raa);
1025 check_added_monitors!(nodes[1], 1);
1026 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1028 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_second_cs.commitment_signed);
1029 check_added_monitors!(nodes[1], 1);
1030 let as_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1032 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_second_raa);
1033 check_added_monitors!(nodes[2], 1);
1034 assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
1036 expect_pending_htlcs_forwardable!(nodes[2]);
1038 let events_6 = nodes[2].node.get_and_clear_pending_events();
1039 assert_eq!(events_6.len(), 2);
1041 Event::PaymentReceived { payment_hash, .. } => { assert_eq!(payment_hash, payment_hash_2); },
1042 _ => panic!("Unexpected event"),
1045 Event::PaymentReceived { payment_hash, .. } => { assert_eq!(payment_hash, payment_hash_3); },
1046 _ => panic!("Unexpected event"),
1049 if test_ignore_second_cs {
1050 expect_pending_htlcs_forwardable!(nodes[1]);
1051 check_added_monitors!(nodes[1], 1);
1053 send_event = SendEvent::from_node(&nodes[1]);
1054 assert_eq!(send_event.node_id, nodes[0].node.get_our_node_id());
1055 assert_eq!(send_event.msgs.len(), 1);
1056 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]);
1057 commitment_signed_dance!(nodes[0], nodes[1], send_event.commitment_msg, false);
1059 expect_pending_htlcs_forwardable!(nodes[0]);
1061 let events_9 = nodes[0].node.get_and_clear_pending_events();
1062 assert_eq!(events_9.len(), 1);
1064 Event::PaymentReceived { payment_hash, .. } => assert_eq!(payment_hash, payment_hash_4.unwrap()),
1065 _ => panic!("Unexpected event"),
1067 claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_4.unwrap());
1070 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
1074 fn test_monitor_update_fail_raa() {
1075 do_test_monitor_update_fail_raa(false);
1076 do_test_monitor_update_fail_raa(true);
1080 fn test_monitor_update_fail_reestablish() {
1081 // Simple test for message retransmission after monitor update failure on
1082 // channel_reestablish generating a monitor update (which comes from freeing holding cell
1084 let chanmon_cfgs = create_chanmon_cfgs(3);
1085 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1086 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1087 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1088 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1089 create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1091 let (payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1093 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1094 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1096 assert!(nodes[2].node.claim_funds(payment_preimage));
1097 check_added_monitors!(nodes[2], 1);
1098 let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1099 assert!(updates.update_add_htlcs.is_empty());
1100 assert!(updates.update_fail_htlcs.is_empty());
1101 assert!(updates.update_fail_malformed_htlcs.is_empty());
1102 assert!(updates.update_fee.is_none());
1103 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
1104 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1105 expect_payment_forwarded!(nodes[1], Some(1000), false);
1106 check_added_monitors!(nodes[1], 1);
1107 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1108 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
1110 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1111 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1112 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1114 let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
1115 let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
1117 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
1119 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
1121 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id())
1122 .contents.flags & 2, 0); // The "disabled" bit should be unset as we just reconnected
1124 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1125 check_added_monitors!(nodes[1], 1);
1127 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1128 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1130 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1131 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1133 assert!(as_reestablish == get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()));
1134 assert!(bs_reestablish == get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()));
1136 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
1138 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id())
1139 .contents.flags & 2, 0); // The "disabled" bit should be unset as we just reconnected
1141 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
1142 check_added_monitors!(nodes[1], 0);
1144 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id())
1145 .contents.flags & 2, 0); // The "disabled" bit should be unset as we just reconnected
1147 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1148 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_1.2).unwrap().clone();
1149 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1150 check_added_monitors!(nodes[1], 0);
1152 updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1153 assert!(updates.update_add_htlcs.is_empty());
1154 assert!(updates.update_fail_htlcs.is_empty());
1155 assert!(updates.update_fail_malformed_htlcs.is_empty());
1156 assert!(updates.update_fee.is_none());
1157 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
1158 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1159 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
1160 expect_payment_sent!(nodes[0], payment_preimage);
1164 fn raa_no_response_awaiting_raa_state() {
1165 // This is a rather convoluted test which ensures that if handling of an RAA does not happen
1166 // due to a previous monitor update failure, we still set AwaitingRemoteRevoke on the channel
1167 // in question (assuming it intends to respond with a CS after monitor updating is restored).
1168 // Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
1169 let chanmon_cfgs = create_chanmon_cfgs(2);
1170 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1171 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1172 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1173 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1175 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1176 let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
1177 let (payment_preimage_3, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[1]);
1179 // Queue up two payments - one will be delivered right away, one immediately goes into the
1180 // holding cell as nodes[0] is AwaitingRAA. Ultimately this allows us to deliver an RAA
1181 // immediately after a CS. By setting failing the monitor update failure from the CS (which
1182 // requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
1183 // generation during RAA while in monitor-update-failed state.
1185 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1186 check_added_monitors!(nodes[0], 1);
1187 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1188 check_added_monitors!(nodes[0], 0);
1191 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1192 assert_eq!(events.len(), 1);
1193 let payment_event = SendEvent::from_event(events.pop().unwrap());
1194 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1195 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1196 check_added_monitors!(nodes[1], 1);
1198 let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1199 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0);
1200 check_added_monitors!(nodes[0], 1);
1201 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1202 assert_eq!(events.len(), 1);
1203 let payment_event = SendEvent::from_event(events.pop().unwrap());
1205 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1);
1206 check_added_monitors!(nodes[0], 1);
1207 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1209 // Now we have a CS queued up which adds a new HTLC (which will need a RAA/CS response from
1210 // nodes[1]) followed by an RAA. Fail the monitor updating prior to the CS, deliver the RAA,
1211 // then restore channel monitor updates.
1212 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1213 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1214 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1215 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1216 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1217 check_added_monitors!(nodes[1], 1);
1219 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1220 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1221 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented responses to RAA".to_string(), 1);
1222 check_added_monitors!(nodes[1], 1);
1224 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1225 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1226 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1227 // nodes[1] should be AwaitingRAA here!
1228 check_added_monitors!(nodes[1], 0);
1229 let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1230 expect_pending_htlcs_forwardable!(nodes[1]);
1231 expect_payment_received!(nodes[1], payment_hash_1, payment_secret_1, 1000000);
1233 // We send a third payment here, which is somewhat of a redundant test, but the
1234 // chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
1235 // commitment transaction states) whereas here we can explicitly check for it.
1237 nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
1238 check_added_monitors!(nodes[0], 0);
1239 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1241 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0);
1242 check_added_monitors!(nodes[0], 1);
1243 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1244 assert_eq!(events.len(), 1);
1245 let payment_event = SendEvent::from_event(events.pop().unwrap());
1247 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1);
1248 check_added_monitors!(nodes[0], 1);
1249 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1251 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1252 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1253 check_added_monitors!(nodes[1], 1);
1254 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1256 // Finally deliver the RAA to nodes[1] which results in a CS response to the last update
1257 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1258 check_added_monitors!(nodes[1], 1);
1259 expect_pending_htlcs_forwardable!(nodes[1]);
1260 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 1000000);
1261 let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1263 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
1264 check_added_monitors!(nodes[0], 1);
1266 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed);
1267 check_added_monitors!(nodes[0], 1);
1268 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1270 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1271 check_added_monitors!(nodes[1], 1);
1272 expect_pending_htlcs_forwardable!(nodes[1]);
1273 expect_payment_received!(nodes[1], payment_hash_3, payment_secret_3, 1000000);
1275 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
1276 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
1277 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
1281 fn claim_while_disconnected_monitor_update_fail() {
1282 // Test for claiming a payment while disconnected and then having the resulting
1283 // channel-update-generated monitor update fail. This kind of thing isn't a particularly
1284 // contrived case for nodes with network instability.
1285 // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
1286 // code introduced a regression in this test (specifically, this caught a removal of the
1287 // channel_reestablish handling ensuring the order was sensical given the messages used).
1288 let chanmon_cfgs = create_chanmon_cfgs(2);
1289 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1290 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1291 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1292 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1294 // Forward a payment for B to claim
1295 let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1297 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1298 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1300 assert!(nodes[1].node.claim_funds(payment_preimage_1));
1301 check_added_monitors!(nodes[1], 1);
1303 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1304 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1306 let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
1307 let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
1309 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect);
1310 let _as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
1312 // Now deliver a's reestablish, freeing the claim from the holding cell, but fail the monitor
1314 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1316 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect);
1317 let _bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
1318 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1319 check_added_monitors!(nodes[1], 1);
1320 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1322 // Send a second payment from A to B, resulting in a commitment update that gets swallowed with
1323 // the monitor still failed
1324 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1326 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1327 check_added_monitors!(nodes[0], 1);
1330 let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1331 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
1332 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.commitment_signed);
1333 check_added_monitors!(nodes[1], 1);
1334 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1335 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented generation of RAA".to_string(), 1);
1336 // Note that nodes[1] not updating monitor here is OK - it wont take action on the new HTLC
1337 // until we've channel_monitor_update'd and updated for the new commitment transaction.
1339 // Now un-fail the monitor, which will result in B sending its original commitment update,
1340 // receiving the commitment update from A, and the resulting commitment dances.
1341 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1342 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1343 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1344 check_added_monitors!(nodes[1], 0);
1346 let bs_msgs = nodes[1].node.get_and_clear_pending_msg_events();
1347 assert_eq!(bs_msgs.len(), 2);
1350 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1351 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
1352 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1353 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
1354 check_added_monitors!(nodes[0], 1);
1356 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1357 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1358 check_added_monitors!(nodes[1], 1);
1360 _ => panic!("Unexpected event"),
1364 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1365 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
1366 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), msg);
1367 check_added_monitors!(nodes[0], 1);
1369 _ => panic!("Unexpected event"),
1372 let as_commitment = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1374 let bs_commitment = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1375 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment.commitment_signed);
1376 check_added_monitors!(nodes[0], 1);
1377 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1379 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment.commitment_signed);
1380 check_added_monitors!(nodes[1], 1);
1381 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1382 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1383 check_added_monitors!(nodes[1], 1);
1385 expect_pending_htlcs_forwardable!(nodes[1]);
1386 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 1000000);
1388 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
1389 check_added_monitors!(nodes[0], 1);
1390 expect_payment_sent!(nodes[0], payment_preimage_1);
1392 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
1396 fn monitor_failed_no_reestablish_response() {
1397 // Test for receiving a channel_reestablish after a monitor update failure resulted in no
1398 // response to a commitment_signed.
1399 // Backported from chanmon_fail_consistency fuzz tests as it caught a long-standing
1400 // debug_assert!() failure in channel_reestablish handling.
1401 let chanmon_cfgs = create_chanmon_cfgs(2);
1402 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1403 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1404 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1405 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1408 get_channel_ref!(nodes[0], lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
1409 get_channel_ref!(nodes[1], lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
1412 // Route the payment and deliver the initial commitment_signed (with a monitor update failure
1414 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1416 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1417 check_added_monitors!(nodes[0], 1);
1420 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1421 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1422 assert_eq!(events.len(), 1);
1423 let payment_event = SendEvent::from_event(events.pop().unwrap());
1424 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1425 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1426 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1427 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1428 check_added_monitors!(nodes[1], 1);
1430 // Now disconnect and immediately reconnect, delivering the channel_reestablish while nodes[1]
1431 // is still failing to update monitors.
1432 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1433 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1435 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1436 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1438 let as_reconnect = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
1439 let bs_reconnect = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
1441 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reconnect);
1442 let _bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
1443 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reconnect);
1444 let _as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
1446 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1447 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1448 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1449 check_added_monitors!(nodes[1], 0);
1450 let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1452 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0);
1453 check_added_monitors!(nodes[0], 1);
1454 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1);
1455 check_added_monitors!(nodes[0], 1);
1457 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1458 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1459 check_added_monitors!(nodes[1], 1);
1461 expect_pending_htlcs_forwardable!(nodes[1]);
1462 expect_payment_received!(nodes[1], payment_hash_1, payment_secret_1, 1000000);
1464 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
1468 fn first_message_on_recv_ordering() {
1469 // Test that if the initial generator of a monitor-update-frozen state doesn't generate
1470 // messages, we're willing to flip the order of response messages if neccessary in resposne to
1471 // a commitment_signed which needs to send an RAA first.
1472 // At a high level, our goal is to fail monitor updating in response to an RAA which needs no
1473 // response and then handle a CS while in the failed state, requiring an RAA followed by a CS
1474 // response. To do this, we start routing two payments, with the final RAA for the first being
1475 // delivered while B is in AwaitingRAA, hence when we deliver the CS for the second B will
1476 // have no pending response but will want to send a RAA/CS (with the updates for the second
1477 // payment applied).
1478 // Backported from chanmon_fail_consistency fuzz tests as it caught a bug here.
1479 let chanmon_cfgs = create_chanmon_cfgs(2);
1480 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1481 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1482 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1483 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1485 // Route the first payment outbound, holding the last RAA for B until we are set up so that we
1486 // can deliver it and fail the monitor update.
1487 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1489 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
1490 check_added_monitors!(nodes[0], 1);
1493 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1494 assert_eq!(events.len(), 1);
1495 let payment_event = SendEvent::from_event(events.pop().unwrap());
1496 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1497 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1498 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1499 check_added_monitors!(nodes[1], 1);
1500 let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1502 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0);
1503 check_added_monitors!(nodes[0], 1);
1504 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1);
1505 check_added_monitors!(nodes[0], 1);
1507 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1509 // Route the second payment, generating an update_add_htlc/commitment_signed
1510 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1512 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1513 check_added_monitors!(nodes[0], 1);
1515 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1516 assert_eq!(events.len(), 1);
1517 let payment_event = SendEvent::from_event(events.pop().unwrap());
1518 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1520 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1522 // Deliver the final RAA for the first payment, which does not require a response. RAAs
1523 // generally require a commitment_signed, so the fact that we're expecting an opposite response
1524 // to the next message also tests resetting the delivery order.
1525 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1526 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1527 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1528 check_added_monitors!(nodes[1], 1);
1530 // Now deliver the update_add_htlc/commitment_signed for the second payment, which does need an
1531 // RAA/CS response, which should be generated when we call channel_monitor_update (with the
1532 // appropriate HTLC acceptance).
1533 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1534 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
1535 check_added_monitors!(nodes[1], 1);
1536 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1537 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Previous monitor update failure prevented generation of RAA".to_string(), 1);
1539 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1540 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1541 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1542 check_added_monitors!(nodes[1], 0);
1544 expect_pending_htlcs_forwardable!(nodes[1]);
1545 expect_payment_received!(nodes[1], payment_hash_1, payment_secret_1, 1000000);
1547 let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1548 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0);
1549 check_added_monitors!(nodes[0], 1);
1550 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1);
1551 check_added_monitors!(nodes[0], 1);
1553 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1554 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1555 check_added_monitors!(nodes[1], 1);
1557 expect_pending_htlcs_forwardable!(nodes[1]);
1558 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 1000000);
1560 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
1561 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
1565 fn test_monitor_update_fail_claim() {
1566 // Basic test for monitor update failures when processing claim_funds calls.
1567 // We set up a simple 3-node network, sending a payment from A to B and failing B's monitor
1568 // update to claim the payment. We then send two payments C->B->A, which are held at B.
1569 // Finally, we restore the channel monitor updating and claim the payment on B, forwarding
1570 // the payments from C onwards to A.
1571 let chanmon_cfgs = create_chanmon_cfgs(3);
1572 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1573 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1574 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1575 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1576 create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1578 // Rebalance a bit so that we can send backwards from 3 to 2.
1579 send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
1581 let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1583 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1584 assert!(nodes[1].node.claim_funds(payment_preimage_1));
1585 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Temporary failure claiming HTLC, treating as success: Failed to update ChannelMonitor".to_string(), 1);
1586 check_added_monitors!(nodes[1], 1);
1588 // Note that at this point there is a pending commitment transaction update for A being held by
1589 // B. Even when we go to send the payment from C through B to A, B will not update this
1590 // already-signed commitment transaction and will instead wait for it to resolve before
1591 // forwarding the payment onwards.
1593 let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1_000_000);
1595 nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1596 check_added_monitors!(nodes[2], 1);
1599 // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be
1600 // paused, so forward shouldn't succeed until we call channel_monitor_updated().
1601 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1603 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
1604 assert_eq!(events.len(), 1);
1605 let payment_event = SendEvent::from_event(events.pop().unwrap());
1606 nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
1607 let events = nodes[1].node.get_and_clear_pending_msg_events();
1608 assert_eq!(events.len(), 0);
1609 commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
1611 let (_, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[0]);
1612 nodes[2].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
1613 check_added_monitors!(nodes[2], 1);
1615 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
1616 assert_eq!(events.len(), 1);
1617 let payment_event = SendEvent::from_event(events.pop().unwrap());
1618 nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
1619 let events = nodes[1].node.get_and_clear_pending_msg_events();
1620 assert_eq!(events.len(), 0);
1621 commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
1623 // Now restore monitor updating on the 0<->1 channel and claim the funds on B.
1624 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_1.2).unwrap().clone();
1625 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1626 check_added_monitors!(nodes[1], 0);
1628 let bs_fulfill_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1629 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_fulfill_update.update_fulfill_htlcs[0]);
1630 commitment_signed_dance!(nodes[0], nodes[1], bs_fulfill_update.commitment_signed, false);
1631 expect_payment_sent!(nodes[0], payment_preimage_1);
1633 // Get the payment forwards, note that they were batched into one commitment update.
1634 expect_pending_htlcs_forwardable!(nodes[1]);
1635 check_added_monitors!(nodes[1], 1);
1636 let bs_forward_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1637 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[0]);
1638 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_forward_update.update_add_htlcs[1]);
1639 commitment_signed_dance!(nodes[0], nodes[1], bs_forward_update.commitment_signed, false);
1640 expect_pending_htlcs_forwardable!(nodes[0]);
1642 let events = nodes[0].node.get_and_clear_pending_events();
1643 assert_eq!(events.len(), 2);
1645 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
1646 assert_eq!(payment_hash_2, *payment_hash);
1647 assert_eq!(1_000_000, amt);
1649 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
1650 assert!(payment_preimage.is_none());
1651 assert_eq!(payment_secret_2, *payment_secret);
1653 _ => panic!("expected PaymentPurpose::InvoicePayment")
1656 _ => panic!("Unexpected event"),
1659 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
1660 assert_eq!(payment_hash_3, *payment_hash);
1661 assert_eq!(1_000_000, amt);
1663 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
1664 assert!(payment_preimage.is_none());
1665 assert_eq!(payment_secret_3, *payment_secret);
1667 _ => panic!("expected PaymentPurpose::InvoicePayment")
1670 _ => panic!("Unexpected event"),
1675 fn test_monitor_update_on_pending_forwards() {
1676 // Basic test for monitor update failures when processing pending HTLC fail/add forwards.
1677 // We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
1678 // The payment from A to C will be failed by C and pending a back-fail to A, while the payment
1679 // from C to A will be pending a forward to A.
1680 let chanmon_cfgs = create_chanmon_cfgs(3);
1681 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1682 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1683 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1684 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1685 create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1687 // Rebalance a bit so that we can send backwards from 3 to 1.
1688 send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
1690 let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1691 assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
1692 expect_pending_htlcs_forwardable!(nodes[2]);
1693 check_added_monitors!(nodes[2], 1);
1695 let cs_fail_update = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1696 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &cs_fail_update.update_fail_htlcs[0]);
1697 commitment_signed_dance!(nodes[1], nodes[2], cs_fail_update.commitment_signed, true, true);
1698 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1700 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
1702 nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1703 check_added_monitors!(nodes[2], 1);
1706 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
1707 assert_eq!(events.len(), 1);
1708 let payment_event = SendEvent::from_event(events.pop().unwrap());
1709 nodes[1].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
1710 commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false);
1712 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1713 expect_pending_htlcs_forwardable!(nodes[1]);
1714 check_added_monitors!(nodes[1], 1);
1715 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1716 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1718 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1719 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_1.2).unwrap().clone();
1720 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1721 check_added_monitors!(nodes[1], 0);
1723 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1724 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
1725 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_add_htlcs[0]);
1726 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false, true);
1728 let events = nodes[0].node.get_and_clear_pending_events();
1729 assert_eq!(events.len(), 2);
1730 if let Event::PaymentPathFailed { payment_hash, rejected_by_dest, .. } = events[0] {
1731 assert_eq!(payment_hash, payment_hash_1);
1732 assert!(rejected_by_dest);
1733 } else { panic!("Unexpected event!"); }
1735 Event::PendingHTLCsForwardable { .. } => { },
1736 _ => panic!("Unexpected event"),
1738 nodes[0].node.process_pending_htlc_forwards();
1739 expect_payment_received!(nodes[0], payment_hash_2, payment_secret_2, 1000000);
1741 claim_payment(&nodes[2], &[&nodes[1], &nodes[0]], payment_preimage_2);
1745 fn monitor_update_claim_fail_no_response() {
1746 // Test for claim_funds resulting in both a monitor update failure and no message response (due
1747 // to channel being AwaitingRAA).
1748 // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
1750 let chanmon_cfgs = create_chanmon_cfgs(2);
1751 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1752 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1753 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1754 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1756 // Forward a payment for B to claim
1757 let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
1759 // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
1760 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1762 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1763 check_added_monitors!(nodes[0], 1);
1766 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1767 assert_eq!(events.len(), 1);
1768 let payment_event = SendEvent::from_event(events.pop().unwrap());
1769 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1770 let as_raa = commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true, false, true);
1772 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1773 assert!(nodes[1].node.claim_funds(payment_preimage_1));
1774 check_added_monitors!(nodes[1], 1);
1775 let events = nodes[1].node.get_and_clear_pending_msg_events();
1776 assert_eq!(events.len(), 0);
1777 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Temporary failure claiming HTLC, treating as success: Failed to update ChannelMonitor".to_string(), 1);
1779 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1780 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1781 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1782 check_added_monitors!(nodes[1], 0);
1783 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1785 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
1786 check_added_monitors!(nodes[1], 1);
1787 expect_pending_htlcs_forwardable!(nodes[1]);
1788 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 1000000);
1790 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1791 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
1792 commitment_signed_dance!(nodes[0], nodes[1], bs_updates.commitment_signed, false);
1793 expect_payment_sent!(nodes[0], payment_preimage_1);
1795 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
1798 // restore_b_before_conf has no meaning if !confirm_a_first
1799 // restore_b_before_lock has no meaning if confirm_a_first
1800 fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: bool, restore_b_before_lock: bool) {
1801 // Test that if the monitor update generated by funding_transaction_generated fails we continue
1802 // the channel setup happily after the update is restored.
1803 let chanmon_cfgs = create_chanmon_cfgs(2);
1804 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1805 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1806 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1808 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43, None).unwrap();
1809 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
1810 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
1812 let (temporary_channel_id, funding_tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 43);
1814 nodes[0].node.funding_transaction_generated(&temporary_channel_id, funding_tx.clone()).unwrap();
1815 check_added_monitors!(nodes[0], 0);
1817 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1818 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
1819 let channel_id = OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
1820 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
1821 check_added_monitors!(nodes[1], 1);
1823 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1824 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
1825 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1826 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
1827 check_added_monitors!(nodes[0], 1);
1828 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1829 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
1830 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1831 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1832 check_added_monitors!(nodes[0], 0);
1834 let events = nodes[0].node.get_and_clear_pending_events();
1835 assert_eq!(events.len(), 0);
1836 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1837 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0].txid(), funding_output.txid);
1839 if confirm_a_first {
1840 confirm_transaction(&nodes[0], &funding_tx);
1841 nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
1842 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1843 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1845 assert!(!restore_b_before_conf);
1846 confirm_transaction(&nodes[1], &funding_tx);
1847 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1850 // Make sure nodes[1] isn't stupid enough to re-send the FundingLocked on reconnect
1851 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1852 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1853 reconnect_nodes(&nodes[0], &nodes[1], (false, confirm_a_first), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
1854 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1855 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1857 if !restore_b_before_conf {
1858 confirm_transaction(&nodes[1], &funding_tx);
1859 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1860 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1862 if !confirm_a_first && !restore_b_before_lock {
1863 confirm_transaction(&nodes[0], &funding_tx);
1864 nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
1865 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1866 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1869 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
1870 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
1871 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1872 check_added_monitors!(nodes[1], 0);
1874 let (channel_id, (announcement, as_update, bs_update)) = if !confirm_a_first {
1875 if !restore_b_before_lock {
1876 let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
1877 (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked))
1879 nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
1880 confirm_transaction(&nodes[0], &funding_tx);
1881 let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
1882 (channel_id, create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked))
1885 if restore_b_before_conf {
1886 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1887 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1888 confirm_transaction(&nodes[1], &funding_tx);
1890 let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
1891 (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked))
1893 for node in nodes.iter() {
1894 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
1895 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
1896 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
1899 send_payment(&nodes[0], &[&nodes[1]], 8000000);
1900 close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
1901 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
1902 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
1906 fn during_funding_monitor_fail() {
1907 do_during_funding_monitor_fail(true, true, false);
1908 do_during_funding_monitor_fail(true, false, false);
1909 do_during_funding_monitor_fail(false, false, false);
1910 do_during_funding_monitor_fail(false, false, true);
1914 fn test_path_paused_mpp() {
1915 // Simple test of sending a multi-part payment where one path is currently blocked awaiting
1917 let chanmon_cfgs = create_chanmon_cfgs(4);
1918 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1919 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1920 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1922 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1923 let (chan_2_ann, _, chan_2_id, _) = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known());
1924 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1925 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
1927 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
1929 // Set us up to take multiple routes, one 0 -> 1 -> 3 and one 0 -> 2 -> 3:
1930 let path = route.paths[0].clone();
1931 route.paths.push(path);
1932 route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
1933 route.paths[0][0].short_channel_id = chan_1_id;
1934 route.paths[0][1].short_channel_id = chan_3_id;
1935 route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
1936 route.paths[1][0].short_channel_id = chan_2_ann.contents.short_channel_id;
1937 route.paths[1][1].short_channel_id = chan_4_id;
1939 // Set it so that the first monitor update (for the path 0 -> 1 -> 3) succeeds, but the second
1940 // (for the path 0 -> 2 -> 3) fails.
1941 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
1942 chanmon_cfgs[0].persister.set_next_update_ret(Some(Err(ChannelMonitorUpdateErr::TemporaryFailure)));
1944 // Now check that we get the right return value, indicating that the first path succeeded but
1945 // the second got a MonitorUpdateFailed err. This implies PaymentSendFailure::PartialFailure as
1946 // some paths succeeded, preventing retry.
1947 if let Err(PaymentSendFailure::PartialFailure { results, ..}) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) {
1948 assert_eq!(results.len(), 2);
1949 if let Ok(()) = results[0] {} else { panic!(); }
1950 if let Err(APIError::MonitorUpdateFailed) = results[1] {} else { panic!(); }
1951 } else { panic!(); }
1952 check_added_monitors!(nodes[0], 2);
1953 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
1955 // Pass the first HTLC of the payment along to nodes[3].
1956 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1957 assert_eq!(events.len(), 1);
1958 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 0, payment_hash.clone(), Some(payment_secret), events.pop().unwrap(), false, None);
1960 // And check that, after we successfully update the monitor for chan_2 we can pass the second
1961 // HTLC along to nodes[3] and claim the whole payment back to nodes[0].
1962 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_2_id).unwrap().clone();
1963 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
1964 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1965 assert_eq!(events.len(), 1);
1966 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash.clone(), Some(payment_secret), events.pop().unwrap(), true, None);
1968 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
1972 fn test_pending_update_fee_ack_on_reconnect() {
1973 // In early versions of our automated fee update patch, nodes did not correctly use the
1974 // previous channel feerate after sending an undelivered revoke_and_ack when re-sending an
1975 // undelivered commitment_signed.
1977 // B sends A new HTLC + CS, not delivered
1978 // A sends B update_fee + CS
1979 // B receives the CS and sends RAA, previously causing B to lock in the new feerate
1981 // B resends initial CS, using the original fee
1983 let chanmon_cfgs = create_chanmon_cfgs(2);
1984 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1985 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1986 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1988 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1989 send_payment(&nodes[0], &[&nodes[1]], 100_000_00);
1991 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[1], nodes[0], 1_000_000);
1992 nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
1993 check_added_monitors!(nodes[1], 1);
1994 let bs_initial_send_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1995 // bs_initial_send_msgs are not delivered until they are re-generated after reconnect
1998 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
2001 nodes[0].node.timer_tick_occurred();
2002 check_added_monitors!(nodes[0], 1);
2003 let as_update_fee_msgs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2004 assert!(as_update_fee_msgs.update_fee.is_some());
2006 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), as_update_fee_msgs.update_fee.as_ref().unwrap());
2007 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update_fee_msgs.commitment_signed);
2008 check_added_monitors!(nodes[1], 1);
2009 let bs_first_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2010 // bs_first_raa is not delivered until it is re-generated after reconnect
2012 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2013 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2015 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
2016 let as_connect_msg = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
2017 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
2018 let bs_connect_msg = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
2020 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_connect_msg);
2021 let bs_resend_msgs = nodes[1].node.get_and_clear_pending_msg_events();
2022 assert_eq!(bs_resend_msgs.len(), 3);
2023 if let MessageSendEvent::UpdateHTLCs { ref updates, .. } = bs_resend_msgs[0] {
2024 assert_eq!(*updates, bs_initial_send_msgs);
2025 } else { panic!(); }
2026 if let MessageSendEvent::SendRevokeAndACK { ref msg, .. } = bs_resend_msgs[1] {
2027 assert_eq!(*msg, bs_first_raa);
2028 } else { panic!(); }
2029 if let MessageSendEvent::SendChannelUpdate { .. } = bs_resend_msgs[2] { } else { panic!(); }
2031 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_connect_msg);
2032 get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
2034 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_initial_send_msgs.update_add_htlcs[0]);
2035 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_initial_send_msgs.commitment_signed);
2036 check_added_monitors!(nodes[0], 1);
2037 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()));
2038 check_added_monitors!(nodes[1], 1);
2039 let bs_second_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()).commitment_signed;
2041 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2042 check_added_monitors!(nodes[0], 1);
2043 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()).commitment_signed);
2044 check_added_monitors!(nodes[1], 1);
2045 let bs_third_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2047 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_cs);
2048 check_added_monitors!(nodes[0], 1);
2049 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
2050 check_added_monitors!(nodes[0], 1);
2052 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()));
2053 check_added_monitors!(nodes[1], 1);
2055 expect_pending_htlcs_forwardable!(nodes[0]);
2056 expect_payment_received!(nodes[0], payment_hash, payment_secret, 1_000_000);
2058 claim_payment(&nodes[1], &[&nodes[0]], payment_preimage);
2061 fn do_update_fee_resend_test(deliver_update: bool, parallel_updates: bool) {
2062 // In early versions we did not handle resending of update_fee on reconnect correctly. The
2063 // chanmon_consistency fuzz target, of course, immediately found it, but we test a few cases
2065 let chanmon_cfgs = create_chanmon_cfgs(2);
2066 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2067 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2068 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2070 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2071 send_payment(&nodes[0], &[&nodes[1]], 1000);
2074 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
2075 *feerate_lock += 20;
2077 nodes[0].node.timer_tick_occurred();
2078 check_added_monitors!(nodes[0], 1);
2079 let update_msgs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2080 assert!(update_msgs.update_fee.is_some());
2082 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msgs.update_fee.as_ref().unwrap());
2085 if parallel_updates {
2087 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
2088 *feerate_lock += 20;
2090 nodes[0].node.timer_tick_occurred();
2091 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2094 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2095 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2097 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
2098 let as_connect_msg = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
2099 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
2100 let bs_connect_msg = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
2102 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_connect_msg);
2103 get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
2104 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2106 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_connect_msg);
2107 let mut as_reconnect_msgs = nodes[0].node.get_and_clear_pending_msg_events();
2108 assert_eq!(as_reconnect_msgs.len(), 2);
2109 if let MessageSendEvent::SendChannelUpdate { .. } = as_reconnect_msgs.pop().unwrap() {} else { panic!(); }
2110 let update_msgs = if let MessageSendEvent::UpdateHTLCs { updates, .. } = as_reconnect_msgs.pop().unwrap()
2111 { updates } else { panic!(); };
2112 assert!(update_msgs.update_fee.is_some());
2113 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msgs.update_fee.as_ref().unwrap());
2114 if parallel_updates {
2115 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update_msgs.commitment_signed);
2116 check_added_monitors!(nodes[1], 1);
2117 let (bs_first_raa, bs_first_cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2118 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_first_raa);
2119 check_added_monitors!(nodes[0], 1);
2120 let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2122 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_first_cs);
2123 check_added_monitors!(nodes[0], 1);
2124 let as_first_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2126 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), as_second_update.update_fee.as_ref().unwrap());
2127 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed);
2128 check_added_monitors!(nodes[1], 1);
2129 let bs_second_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2131 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_first_raa);
2132 let bs_second_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2133 check_added_monitors!(nodes[1], 1);
2135 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
2136 check_added_monitors!(nodes[0], 1);
2138 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_cs.commitment_signed);
2139 check_added_monitors!(nodes[0], 1);
2140 let as_second_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2142 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_raa);
2143 check_added_monitors!(nodes[1], 1);
2145 commitment_signed_dance!(nodes[1], nodes[0], update_msgs.commitment_signed, false);
2148 send_payment(&nodes[0], &[&nodes[1]], 1000);
2151 fn update_fee_resend_test() {
2152 do_update_fee_resend_test(false, false);
2153 do_update_fee_resend_test(true, false);
2154 do_update_fee_resend_test(false, true);
2155 do_update_fee_resend_test(true, true);
2158 fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
2159 // Tests that, when we serialize a channel with AddHTLC entries in the holding cell, we
2160 // properly free them on reconnect. We previously failed such HTLCs upon serialization, but
2161 // that behavior was both somewhat unexpected and also broken (there was a debug assertion
2162 // which failed in such a case).
2163 let chanmon_cfgs = create_chanmon_cfgs(2);
2164 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2165 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2166 let persister: test_utils::TestPersister;
2167 let new_chain_monitor: test_utils::TestChainMonitor;
2168 let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
2169 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2171 let chan_id = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 15_000_000, 7_000_000_000, InitFeatures::known(), InitFeatures::known()).2;
2172 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
2173 let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(&nodes[1]);
2175 // Do a really complicated dance to get an HTLC into the holding cell, with MonitorUpdateFailed
2176 // set but AwaitingRemoteRevoke unset. When this test was written, any attempts to send an HTLC
2177 // while MonitorUpdateFailed is set are immediately failed-backwards. Thus, the only way to get
2178 // an AddHTLC into the holding cell is to add it while AwaitingRemoteRevoke is set but
2179 // MonitorUpdateFailed is unset, and then swap the flags.
2182 // a) routing a payment from node B to node A,
2183 // b) sending a payment from node A to node B without delivering any of the generated messages,
2184 // putting node A in AwaitingRemoteRevoke,
2185 // c) sending a second payment from node A to node B, which is immediately placed in the
2187 // d) claiming the first payment from B, allowing us to fail the monitor update which occurs
2188 // when we try to persist the payment preimage,
2189 // e) delivering A's commitment_signed from (b) and the resulting B revoke_and_ack message,
2190 // clearing AwaitingRemoteRevoke on node A.
2192 // Note that because, at the end, MonitorUpdateFailed is still set, the HTLC generated in (c)
2193 // will not be freed from the holding cell.
2194 let (payment_preimage_0, _, _) = route_payment(&nodes[1], &[&nodes[0]], 100000);
2196 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
2197 check_added_monitors!(nodes[0], 1);
2198 let send = SendEvent::from_node(&nodes[0]);
2199 assert_eq!(send.msgs.len(), 1);
2201 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
2202 check_added_monitors!(nodes[0], 0);
2204 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
2205 assert!(nodes[0].node.claim_funds(payment_preimage_0));
2206 check_added_monitors!(nodes[0], 1);
2208 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send.msgs[0]);
2209 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send.commitment_msg);
2210 check_added_monitors!(nodes[1], 1);
2212 let (raa, cs) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2214 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
2215 check_added_monitors!(nodes[0], 1);
2218 // Optionally reload nodes[0] entirely through a serialization roundtrip, otherwise just
2219 // disconnect the peers. Note that the fuzzer originally found this issue because
2220 // deserializing a ChannelManager in this state causes an assertion failure.
2222 let nodes_0_serialized = nodes[0].node.encode();
2223 let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
2224 get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
2226 persister = test_utils::TestPersister::new();
2227 let keys_manager = &chanmon_cfgs[0].keys_manager;
2228 new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
2229 nodes[0].chain_monitor = &new_chain_monitor;
2230 let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
2231 let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
2232 &mut chan_0_monitor_read, keys_manager).unwrap();
2233 assert!(chan_0_monitor_read.is_empty());
2235 let mut nodes_0_read = &nodes_0_serialized[..];
2236 let config = UserConfig::default();
2237 nodes_0_deserialized = {
2238 let mut channel_monitors = HashMap::new();
2239 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
2240 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
2241 default_config: config,
2243 fee_estimator: node_cfgs[0].fee_estimator,
2244 chain_monitor: nodes[0].chain_monitor,
2245 tx_broadcaster: nodes[0].tx_broadcaster.clone(),
2246 logger: nodes[0].logger,
2250 nodes[0].node = &nodes_0_deserialized;
2251 assert!(nodes_0_read.is_empty());
2253 nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0.clone(), chan_0_monitor).unwrap();
2254 check_added_monitors!(nodes[0], 1);
2256 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2258 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2260 // Now reconnect the two
2261 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
2262 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
2263 assert_eq!(reestablish_1.len(), 1);
2264 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
2265 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
2266 assert_eq!(reestablish_2.len(), 1);
2268 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
2269 let resp_1 = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
2270 check_added_monitors!(nodes[1], 0);
2272 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
2273 let resp_0 = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
2275 assert!(resp_0.0.is_none());
2276 assert!(resp_0.1.is_none());
2277 assert!(resp_0.2.is_none());
2278 assert!(resp_1.0.is_none());
2279 assert!(resp_1.1.is_none());
2281 // Check that the freshly-generated cs is equal to the original (which we will deliver in a
2283 if let Some(pending_cs) = resp_1.2 {
2284 assert!(pending_cs.update_add_htlcs.is_empty());
2285 assert!(pending_cs.update_fail_htlcs.is_empty());
2286 assert!(pending_cs.update_fulfill_htlcs.is_empty());
2287 assert_eq!(pending_cs.commitment_signed, cs);
2288 } else { panic!(); }
2290 // There should be no monitor updates as we are still pending awaiting a failed one.
2291 check_added_monitors!(nodes[0], 0);
2292 check_added_monitors!(nodes[1], 0);
2295 // If we finish updating the monitor, we should free the holding cell right away (this did
2296 // not occur prior to #756).
2297 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
2298 let (funding_txo, mon_id, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone();
2299 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(funding_txo, mon_id);
2301 // New outbound messages should be generated immediately upon a call to
2302 // get_and_clear_pending_msg_events (but not before).
2303 check_added_monitors!(nodes[0], 0);
2304 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2305 check_added_monitors!(nodes[0], 1);
2306 assert_eq!(events.len(), 1);
2308 // Deliver the pending in-flight CS
2309 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &cs);
2310 check_added_monitors!(nodes[0], 1);
2312 let commitment_msg = match events.pop().unwrap() {
2313 MessageSendEvent::UpdateHTLCs { node_id, updates } => {
2314 assert_eq!(node_id, nodes[1].node.get_our_node_id());
2315 assert!(updates.update_fail_htlcs.is_empty());
2316 assert!(updates.update_fail_malformed_htlcs.is_empty());
2317 assert!(updates.update_fee.is_none());
2318 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2319 nodes[1].node.handle_update_fulfill_htlc(&nodes[0].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
2320 expect_payment_sent_without_paths!(nodes[1], payment_preimage_0);
2321 assert_eq!(updates.update_add_htlcs.len(), 1);
2322 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
2323 updates.commitment_signed
2325 _ => panic!("Unexpected event type!"),
2328 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_msg);
2329 check_added_monitors!(nodes[1], 1);
2331 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2332 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
2333 expect_pending_htlcs_forwardable!(nodes[1]);
2334 expect_payment_received!(nodes[1], payment_hash_1, payment_secret_1, 100000);
2335 check_added_monitors!(nodes[1], 1);
2337 commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
2339 let events = nodes[1].node.get_and_clear_pending_events();
2340 assert_eq!(events.len(), 2);
2342 Event::PendingHTLCsForwardable { .. } => { },
2343 _ => panic!("Unexpected event"),
2346 Event::PaymentPathSuccessful { .. } => { },
2347 _ => panic!("Unexpected event"),
2350 nodes[1].node.process_pending_htlc_forwards();
2351 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 100000);
2353 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
2354 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
2357 fn channel_holding_cell_serialize() {
2358 do_channel_holding_cell_serialize(true, true);
2359 do_channel_holding_cell_serialize(true, false);
2360 do_channel_holding_cell_serialize(false, true); // last arg doesn't matter
2363 #[derive(PartialEq)]
2364 enum HTLCStatusAtDupClaim {
2369 fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_fails: bool) {
2370 // When receiving an update_fulfill_htlc message, we immediately forward the claim backwards
2371 // along the payment path before waiting for a full commitment_signed dance. This is great, but
2372 // can cause duplicative claims if a node sends an update_fulfill_htlc message, disconnects,
2373 // reconnects, and then has to re-send its update_fulfill_htlc message again.
2374 // In previous code, we didn't handle the double-claim correctly, spuriously closing the
2375 // channel on which the inbound HTLC was received.
2376 let chanmon_cfgs = create_chanmon_cfgs(3);
2377 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2378 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2379 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2381 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2382 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()).2;
2384 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
2386 let mut as_raa = None;
2387 if htlc_status == HTLCStatusAtDupClaim::HoldingCell {
2388 // In order to get the HTLC claim into the holding cell at nodes[1], we need nodes[1] to be
2389 // awaiting a remote revoke_and_ack from nodes[0].
2390 let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
2391 nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
2392 check_added_monitors!(nodes[0], 1);
2394 let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
2395 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
2396 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_event.commitment_msg);
2397 check_added_monitors!(nodes[1], 1);
2399 let (bs_raa, bs_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_raa);
2401 check_added_monitors!(nodes[0], 1);
2402 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs);
2403 check_added_monitors!(nodes[0], 1);
2405 as_raa = Some(get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()));
2408 let fulfill_msg = msgs::UpdateFulfillHTLC {
2414 assert!(nodes[2].node.fail_htlc_backwards(&payment_hash));
2415 expect_pending_htlcs_forwardable!(nodes[2]);
2416 check_added_monitors!(nodes[2], 1);
2417 get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2419 assert!(nodes[2].node.claim_funds(payment_preimage));
2420 check_added_monitors!(nodes[2], 1);
2421 let cs_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2422 assert_eq!(cs_updates.update_fulfill_htlcs.len(), 1);
2423 // Check that the message we're about to deliver matches the one generated:
2424 assert_eq!(fulfill_msg, cs_updates.update_fulfill_htlcs[0]);
2426 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &fulfill_msg);
2427 expect_payment_forwarded!(nodes[1], Some(1000), false);
2428 check_added_monitors!(nodes[1], 1);
2430 let mut bs_updates = None;
2431 if htlc_status != HTLCStatusAtDupClaim::HoldingCell {
2432 bs_updates = Some(get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()));
2433 assert_eq!(bs_updates.as_ref().unwrap().update_fulfill_htlcs.len(), 1);
2434 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.as_ref().unwrap().update_fulfill_htlcs[0]);
2435 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
2436 if htlc_status == HTLCStatusAtDupClaim::Cleared {
2437 commitment_signed_dance!(nodes[0], nodes[1], &bs_updates.as_ref().unwrap().commitment_signed, false);
2438 expect_payment_path_successful!(nodes[0]);
2441 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2444 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
2445 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2448 reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (1, 0), (0, 0), (0, 0), (false, false));
2449 expect_pending_htlcs_forwardable!(nodes[1]);
2451 reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (0, 0), (false, false));
2454 if htlc_status == HTLCStatusAtDupClaim::HoldingCell {
2455 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa.unwrap());
2456 check_added_monitors!(nodes[1], 1);
2457 expect_pending_htlcs_forwardable_ignore!(nodes[1]); // We finally receive the second payment, but don't claim it
2459 bs_updates = Some(get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()));
2460 assert_eq!(bs_updates.as_ref().unwrap().update_fulfill_htlcs.len(), 1);
2461 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.as_ref().unwrap().update_fulfill_htlcs[0]);
2462 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
2464 if htlc_status != HTLCStatusAtDupClaim::Cleared {
2465 commitment_signed_dance!(nodes[0], nodes[1], &bs_updates.as_ref().unwrap().commitment_signed, false);
2466 expect_payment_path_successful!(nodes[0]);
2471 fn test_reconnect_dup_htlc_claims() {
2472 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::Received, false);
2473 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::HoldingCell, false);
2474 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::Cleared, false);
2475 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::Received, true);
2476 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::HoldingCell, true);
2477 do_test_reconnect_dup_htlc_claims(HTLCStatusAtDupClaim::Cleared, true);
2481 fn test_temporary_error_during_shutdown() {
2482 // Test that temporary failures when updating the monitor's shutdown script delay cooperative
2484 let mut config = test_default_channel_config();
2485 config.channel_options.commit_upfront_shutdown_pubkey = false;
2487 let chanmon_cfgs = create_chanmon_cfgs(2);
2488 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2489 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
2490 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2492 let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2494 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
2495 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
2497 nodes[0].node.close_channel(&channel_id).unwrap();
2498 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id()));
2499 check_added_monitors!(nodes[1], 1);
2501 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id()));
2502 check_added_monitors!(nodes[0], 1);
2504 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2506 chanmon_cfgs[0].persister.set_update_ret(Ok(()));
2507 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
2509 let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
2510 nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
2511 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id()));
2513 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2515 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
2516 let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
2517 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
2519 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id()));
2520 let (_, closing_signed_a) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
2521 let txn_a = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2523 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &closing_signed_a.unwrap());
2524 let (_, none_b) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
2525 assert!(none_b.is_none());
2526 let txn_b = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2528 assert_eq!(txn_a, txn_b);
2529 assert_eq!(txn_a.len(), 1);
2530 check_spends!(txn_a[0], funding_tx);
2531 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
2532 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
2536 fn test_permanent_error_during_sending_shutdown() {
2537 // Test that permanent failures when updating the monitor's shutdown script result in a force
2538 // close when initiating a cooperative close.
2539 let mut config = test_default_channel_config();
2540 config.channel_options.commit_upfront_shutdown_pubkey = false;
2542 let chanmon_cfgs = create_chanmon_cfgs(2);
2543 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2544 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
2545 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2547 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
2548 chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::PermanentFailure));
2550 assert!(nodes[0].node.close_channel(&channel_id).is_ok());
2551 check_closed_broadcast!(nodes[0], true);
2552 check_added_monitors!(nodes[0], 2);
2553 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "ChannelMonitor storage failure".to_string() });
2557 fn test_permanent_error_during_handling_shutdown() {
2558 // Test that permanent failures when updating the monitor's shutdown script result in a force
2559 // close when handling a cooperative close.
2560 let mut config = test_default_channel_config();
2561 config.channel_options.commit_upfront_shutdown_pubkey = false;
2563 let chanmon_cfgs = create_chanmon_cfgs(2);
2564 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2565 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(config)]);
2566 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2568 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
2569 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::PermanentFailure));
2571 assert!(nodes[0].node.close_channel(&channel_id).is_ok());
2572 let shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
2573 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &shutdown);
2574 check_closed_broadcast!(nodes[1], true);
2575 check_added_monitors!(nodes[1], 2);
2576 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "ChannelMonitor storage failure".to_string() });
2580 fn double_temp_error() {
2581 // Test that it's OK to have multiple `ChainMonitor::update_channel` calls fail in a row.
2582 let chanmon_cfgs = create_chanmon_cfgs(2);
2583 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2584 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2585 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2587 let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2589 let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
2590 let (payment_preimage_2, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
2592 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
2593 // `claim_funds` results in a ChannelMonitorUpdate.
2594 assert!(nodes[1].node.claim_funds(payment_preimage_1));
2595 check_added_monitors!(nodes[1], 1);
2596 let (funding_tx, latest_update_1, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
2598 chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
2599 // Previously, this would've panicked due to a double-call to `Channel::monitor_update_failed`,
2600 // which had some asserts that prevented it from being called twice.
2601 assert!(nodes[1].node.claim_funds(payment_preimage_2));
2602 check_added_monitors!(nodes[1], 1);
2603 chanmon_cfgs[1].persister.set_update_ret(Ok(()));
2605 let (_, latest_update_2, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
2606 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(funding_tx, latest_update_1);
2607 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2608 check_added_monitors!(nodes[1], 0);
2609 nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(funding_tx, latest_update_2);
2611 // Complete the first HTLC.
2612 let events = nodes[1].node.get_and_clear_pending_msg_events();
2613 assert_eq!(events.len(), 1);
2614 let (update_fulfill_1, commitment_signed_b1, node_id) = {
2616 &MessageSendEvent::UpdateHTLCs { ref 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 } } => {
2617 assert!(update_add_htlcs.is_empty());
2618 assert_eq!(update_fulfill_htlcs.len(), 1);
2619 assert!(update_fail_htlcs.is_empty());
2620 assert!(update_fail_malformed_htlcs.is_empty());
2621 assert!(update_fee.is_none());
2622 (update_fulfill_htlcs[0].clone(), commitment_signed.clone(), node_id.clone())
2624 _ => panic!("Unexpected event"),
2627 assert_eq!(node_id, nodes[0].node.get_our_node_id());
2628 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_1);
2629 check_added_monitors!(nodes[0], 0);
2630 expect_payment_sent_without_paths!(nodes[0], payment_preimage_1);
2631 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_b1);
2632 check_added_monitors!(nodes[0], 1);
2633 nodes[0].node.process_pending_htlc_forwards();
2634 let (raa_a1, commitment_signed_a1) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2635 check_added_monitors!(nodes[1], 0);
2636 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2637 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_a1);
2638 check_added_monitors!(nodes[1], 1);
2639 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed_a1);
2640 check_added_monitors!(nodes[1], 1);
2642 // Complete the second HTLC.
2643 let ((update_fulfill_2, commitment_signed_b2), raa_b2) = {
2644 let events = nodes[1].node.get_and_clear_pending_msg_events();
2645 assert_eq!(events.len(), 2);
2647 MessageSendEvent::UpdateHTLCs { node_id, updates } => {
2648 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
2649 assert!(updates.update_add_htlcs.is_empty());
2650 assert!(updates.update_fail_htlcs.is_empty());
2651 assert!(updates.update_fail_malformed_htlcs.is_empty());
2652 assert!(updates.update_fee.is_none());
2653 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2654 (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
2656 _ => panic!("Unexpected event"),
2659 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
2660 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
2663 _ => panic!("Unexpected event"),
2666 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa_b2);
2667 check_added_monitors!(nodes[0], 1);
2668 expect_payment_path_successful!(nodes[0]);
2670 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_2);
2671 check_added_monitors!(nodes[0], 0);
2672 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2673 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed_b2, false);
2674 expect_payment_sent!(nodes[0], payment_preimage_2);