Unblock channels awaiting monitor update based on `ChanMan` queue
[rust-lightning] / lightning / src / ln / reload_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Functional tests which test for correct behavior across node restarts.
11
12 use crate::chain::{ChannelMonitorUpdateStatus, Watch};
13 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
14 use crate::chain::channelmonitor::{CLOSED_CHANNEL_UPDATE_ID, ChannelMonitor};
15 use crate::sign::EntropySource;
16 use crate::chain::transaction::OutPoint;
17 use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
18 use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
19 use crate::ln::msgs;
20 use crate::ln::types::ChannelId;
21 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
22 use crate::util::test_channel_signer::TestChannelSigner;
23 use crate::util::test_utils;
24 use crate::util::errors::APIError;
25 use crate::util::ser::{Writeable, ReadableArgs};
26 use crate::util::config::UserConfig;
27
28 use bitcoin::hash_types::BlockHash;
29
30 use crate::prelude::*;
31 use crate::sync::Mutex;
32
33 use crate::ln::functional_test_utils::*;
34
35 #[test]
36 fn test_funding_peer_disconnect() {
37         // Test that we can lock in our funding tx while disconnected
38         let chanmon_cfgs = create_chanmon_cfgs(2);
39         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
40         let persister;
41         let new_chain_monitor;
42
43         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
44         let nodes_0_deserialized;
45         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
46         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001);
47
48         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
49         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
50
51         confirm_transaction(&nodes[0], &tx);
52         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
53         assert!(events_1.is_empty());
54
55         let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
56         reconnect_args.send_channel_ready.1 = true;
57         reconnect_nodes(reconnect_args);
58
59         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
60         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
61
62         confirm_transaction(&nodes[1], &tx);
63         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
64         assert!(events_2.is_empty());
65
66         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
67                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
68         }, true).unwrap();
69         let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
70         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
71                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
72         }, false).unwrap();
73         let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();
74
75         // nodes[0] hasn't yet received a channel_ready, so it only sends that on reconnect.
76         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
77         let events_3 = nodes[0].node.get_and_clear_pending_msg_events();
78         assert_eq!(events_3.len(), 1);
79         let as_channel_ready = match events_3[0] {
80                 MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
81                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
82                         msg.clone()
83                 },
84                 _ => panic!("Unexpected event {:?}", events_3[0]),
85         };
86
87         // nodes[1] received nodes[0]'s channel_ready on the first reconnect above, so it should send
88         // announcement_signatures as well as channel_update.
89         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
90         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
91         assert_eq!(events_4.len(), 3);
92         let chan_id;
93         let bs_channel_ready = match events_4[0] {
94                 MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
95                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
96                         chan_id = msg.channel_id;
97                         msg.clone()
98                 },
99                 _ => panic!("Unexpected event {:?}", events_4[0]),
100         };
101         let bs_announcement_sigs = match events_4[1] {
102                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
103                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
104                         msg.clone()
105                 },
106                 _ => panic!("Unexpected event {:?}", events_4[1]),
107         };
108         match events_4[2] {
109                 MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } => {
110                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
111                 },
112                 _ => panic!("Unexpected event {:?}", events_4[2]),
113         }
114
115         // Re-deliver nodes[0]'s channel_ready, which nodes[1] can safely ignore. It currently
116         // generates a duplicative private channel_update
117         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
118         let events_5 = nodes[1].node.get_and_clear_pending_msg_events();
119         assert_eq!(events_5.len(), 1);
120         match events_5[0] {
121                 MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } => {
122                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
123                 },
124                 _ => panic!("Unexpected event {:?}", events_5[0]),
125         };
126
127         // When we deliver nodes[1]'s channel_ready, however, nodes[0] will generate its
128         // announcement_signatures.
129         nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready);
130         let events_6 = nodes[0].node.get_and_clear_pending_msg_events();
131         assert_eq!(events_6.len(), 1);
132         let as_announcement_sigs = match events_6[0] {
133                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
134                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
135                         msg.clone()
136                 },
137                 _ => panic!("Unexpected event {:?}", events_6[0]),
138         };
139         expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
140         expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
141
142         // When we deliver nodes[1]'s announcement_signatures to nodes[0], nodes[0] should immediately
143         // broadcast the channel announcement globally, as well as re-send its (now-public)
144         // channel_update.
145         nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
146         let events_7 = nodes[0].node.get_and_clear_pending_msg_events();
147         assert_eq!(events_7.len(), 1);
148         let (chan_announcement, as_update) = match events_7[0] {
149                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
150                         (msg.clone(), update_msg.clone().unwrap())
151                 },
152                 _ => panic!("Unexpected event {:?}", events_7[0]),
153         };
154
155         // Finally, deliver nodes[0]'s announcement_signatures to nodes[1] and make sure it creates the
156         // same channel_announcement.
157         nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs);
158         let events_8 = nodes[1].node.get_and_clear_pending_msg_events();
159         assert_eq!(events_8.len(), 1);
160         let bs_update = match events_8[0] {
161                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
162                         assert_eq!(*msg, chan_announcement);
163                         update_msg.clone().unwrap()
164                 },
165                 _ => panic!("Unexpected event {:?}", events_8[0]),
166         };
167
168         // Provide the channel announcement and public updates to the network graph
169         nodes[0].gossip_sync.handle_channel_announcement(&chan_announcement).unwrap();
170         nodes[0].gossip_sync.handle_channel_update(&bs_update).unwrap();
171         nodes[0].gossip_sync.handle_channel_update(&as_update).unwrap();
172
173         let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
174         let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
175         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
176
177         // Check that after deserialization and reconnection we can still generate an identical
178         // channel_announcement from the cached signatures.
179         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
180
181         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
182
183         reload_node!(nodes[0], &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
184
185         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
186 }
187
188 #[test]
189 fn test_no_txn_manager_serialize_deserialize() {
190         let chanmon_cfgs = create_chanmon_cfgs(2);
191         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
192         let persister;
193         let new_chain_monitor;
194
195         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
196         let nodes_0_deserialized;
197         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
198
199         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001);
200
201         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
202
203         let chan_0_monitor_serialized =
204                 get_monitor!(nodes[0], ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.txid(), index: 0 })).encode();
205         reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
206
207         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
208                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
209         }, true).unwrap();
210         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
211         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
212                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
213         }, false).unwrap();
214         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
215
216         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
217         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
218         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
219         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
220
221         let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
222         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
223         for node in nodes.iter() {
224                 assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap());
225                 node.gossip_sync.handle_channel_update(&as_update).unwrap();
226                 node.gossip_sync.handle_channel_update(&bs_update).unwrap();
227         }
228
229         send_payment(&nodes[0], &[&nodes[1]], 1000000);
230 }
231
232 #[test]
233 fn test_manager_serialize_deserialize_events() {
234         // This test makes sure the events field in ChannelManager survives de/serialization
235         let chanmon_cfgs = create_chanmon_cfgs(2);
236         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
237         let persister;
238         let new_chain_monitor;
239
240         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
241         let nodes_0_deserialized;
242         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
243
244         // Start creating a channel, but stop right before broadcasting the funding transaction
245         let channel_value = 100000;
246         let push_msat = 10001;
247         let node_a = nodes.remove(0);
248         let node_b = nodes.remove(0);
249         node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None, None).unwrap();
250         node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id()));
251         node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id()));
252
253         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&node_a, &node_b.node.get_our_node_id(), channel_value, 42);
254
255         node_a.node.funding_transaction_generated(&temporary_channel_id, &node_b.node.get_our_node_id(), tx.clone()).unwrap();
256         check_added_monitors!(node_a, 0);
257
258         node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id()));
259         {
260                 let mut added_monitors = node_b.chain_monitor.added_monitors.lock().unwrap();
261                 assert_eq!(added_monitors.len(), 1);
262                 assert_eq!(added_monitors[0].0, funding_output);
263                 added_monitors.clear();
264         }
265
266         let bs_funding_signed = get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id());
267         node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &bs_funding_signed);
268         {
269                 let mut added_monitors = node_a.chain_monitor.added_monitors.lock().unwrap();
270                 assert_eq!(added_monitors.len(), 1);
271                 assert_eq!(added_monitors[0].0, funding_output);
272                 added_monitors.clear();
273         }
274         // Normally, this is where node_a would broadcast the funding transaction, but the test de/serializes first instead
275
276         expect_channel_pending_event(&node_a, &node_b.node.get_our_node_id());
277         expect_channel_pending_event(&node_b, &node_a.node.get_our_node_id());
278
279         nodes.push(node_a);
280         nodes.push(node_b);
281
282         // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
283         let chan_0_monitor_serialized = get_monitor!(nodes[0], bs_funding_signed.channel_id).encode();
284         reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
285
286         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
287
288         // After deserializing, make sure the funding_transaction is still held by the channel manager
289         let events_4 = nodes[0].node.get_and_clear_pending_events();
290         assert_eq!(events_4.len(), 0);
291         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
292         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].txid(), funding_output.txid);
293
294         // Make sure the channel is functioning as though the de/serialization never happened
295         assert_eq!(nodes[0].node.list_channels().len(), 1);
296
297         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
298                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
299         }, true).unwrap();
300         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
301         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
302                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
303         }, false).unwrap();
304         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
305
306         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
307         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
308         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
309         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
310
311         let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
312         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
313         for node in nodes.iter() {
314                 assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap());
315                 node.gossip_sync.handle_channel_update(&as_update).unwrap();
316                 node.gossip_sync.handle_channel_update(&bs_update).unwrap();
317         }
318
319         send_payment(&nodes[0], &[&nodes[1]], 1000000);
320 }
321
322 #[test]
323 fn test_simple_manager_serialize_deserialize() {
324         let chanmon_cfgs = create_chanmon_cfgs(2);
325         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
326         let persister;
327         let new_chain_monitor;
328
329         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
330         let nodes_0_deserialized;
331         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
332         let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
333
334         let (our_payment_preimage, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
335         let (_, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
336
337         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
338
339         let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
340         reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
341
342         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
343
344         fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
345         claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
346 }
347
348 #[test]
349 fn test_manager_serialize_deserialize_inconsistent_monitor() {
350         // Test deserializing a ChannelManager with an out-of-date ChannelMonitor
351         let chanmon_cfgs = create_chanmon_cfgs(4);
352         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
353         let logger;
354         let fee_estimator;
355         let persister;
356         let new_chain_monitor;
357
358         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
359         let nodes_0_deserialized;
360         let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
361
362         let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
363         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 2, 0).2;
364         let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3);
365
366         let mut node_0_stale_monitors_serialized = Vec::new();
367         for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
368                 let mut writer = test_utils::TestVecWriter(Vec::new());
369                 get_monitor!(nodes[0], chan_id_iter).write(&mut writer).unwrap();
370                 node_0_stale_monitors_serialized.push(writer.0);
371         }
372
373         let (our_payment_preimage, ..) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);
374
375         // Serialize the ChannelManager here, but the monitor we keep up-to-date
376         let nodes_0_serialized = nodes[0].node.encode();
377
378         route_payment(&nodes[0], &[&nodes[3]], 1000000);
379         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
380         nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id());
381         nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id());
382
383         // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
384         // nodes[3])
385         let mut node_0_monitors_serialized = Vec::new();
386         for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
387                 node_0_monitors_serialized.push(get_monitor!(nodes[0], chan_id_iter).encode());
388         }
389
390         logger = test_utils::TestLogger::new();
391         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
392         persister = test_utils::TestPersister::new();
393         let keys_manager = &chanmon_cfgs[0].keys_manager;
394         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
395         nodes[0].chain_monitor = &new_chain_monitor;
396
397
398         let mut node_0_stale_monitors = Vec::new();
399         for serialized in node_0_stale_monitors_serialized.iter() {
400                 let mut read = &serialized[..];
401                 let (_, monitor) = <(BlockHash, ChannelMonitor<TestChannelSigner>)>::read(&mut read, (keys_manager, keys_manager)).unwrap();
402                 assert!(read.is_empty());
403                 node_0_stale_monitors.push(monitor);
404         }
405
406         let mut node_0_monitors = Vec::new();
407         for serialized in node_0_monitors_serialized.iter() {
408                 let mut read = &serialized[..];
409                 let (_, monitor) = <(BlockHash, ChannelMonitor<TestChannelSigner>)>::read(&mut read, (keys_manager, keys_manager)).unwrap();
410                 assert!(read.is_empty());
411                 node_0_monitors.push(monitor);
412         }
413
414         let mut nodes_0_read = &nodes_0_serialized[..];
415         if let Err(msgs::DecodeError::DangerousValue) =
416                 <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
417                 default_config: UserConfig::default(),
418                 entropy_source: keys_manager,
419                 node_signer: keys_manager,
420                 signer_provider: keys_manager,
421                 fee_estimator: &fee_estimator,
422                 router: &nodes[0].router,
423                 chain_monitor: nodes[0].chain_monitor,
424                 tx_broadcaster: nodes[0].tx_broadcaster,
425                 logger: &logger,
426                 channel_monitors: node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
427         }) { } else {
428                 panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
429         };
430
431         let mut nodes_0_read = &nodes_0_serialized[..];
432         let (_, nodes_0_deserialized_tmp) =
433                 <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
434                 default_config: UserConfig::default(),
435                 entropy_source: keys_manager,
436                 node_signer: keys_manager,
437                 signer_provider: keys_manager,
438                 fee_estimator: &fee_estimator,
439                 router: nodes[0].router,
440                 chain_monitor: nodes[0].chain_monitor,
441                 tx_broadcaster: nodes[0].tx_broadcaster,
442                 logger: &logger,
443                 channel_monitors: node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
444         }).unwrap();
445         nodes_0_deserialized = nodes_0_deserialized_tmp;
446         assert!(nodes_0_read.is_empty());
447
448         for monitor in node_0_monitors.drain(..) {
449                 let funding_outpoint = monitor.get_funding_txo().0;
450                 assert_eq!(nodes[0].chain_monitor.watch_channel(funding_outpoint, monitor),
451                         Ok(ChannelMonitorUpdateStatus::Completed));
452                 check_added_monitors!(nodes[0], 1);
453         }
454         nodes[0].node = &nodes_0_deserialized;
455
456         check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager, [nodes[3].node.get_our_node_id()], 100000);
457         { // Channel close should result in a commitment tx
458                 nodes[0].node.timer_tick_occurred();
459                 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
460                 assert_eq!(txn.len(), 1);
461                 check_spends!(txn[0], funding_tx);
462                 assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
463         }
464         check_added_monitors!(nodes[0], 1);
465
466         // nodes[1] and nodes[2] have no lost state with nodes[0]...
467         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
468         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[2]));
469         //... and we can even still claim the payment!
470         claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);
471
472         nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
473                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
474         }, true).unwrap();
475         let reestablish = get_chan_reestablish_msgs!(nodes[3], nodes[0]).pop().unwrap();
476         nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
477                 features: nodes[3].node.init_features(), networks: None, remote_network_address: None
478         }, false).unwrap();
479         nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish);
480         let mut found_err = false;
481         for msg_event in nodes[0].node.get_and_clear_pending_msg_events() {
482                 if let MessageSendEvent::HandleError { ref action, .. } = msg_event {
483                         match action {
484                                 &ErrorAction::SendErrorMessage { ref msg } => {
485                                         assert_eq!(msg.channel_id, channel_id);
486                                         assert!(!found_err);
487                                         found_err = true;
488                                 },
489                                 _ => panic!("Unexpected event!"),
490                         }
491                 }
492         }
493         assert!(found_err);
494 }
495
496 #[cfg(feature = "std")]
497 fn do_test_data_loss_protect(reconnect_panicing: bool, substantially_old: bool, not_stale: bool) {
498         use crate::routing::router::{RouteParameters, PaymentParameters};
499         use crate::ln::channelmanager::Retry;
500         use crate::util::string::UntrustedString;
501         // When we get a data_loss_protect proving we're behind, we immediately panic as the
502         // chain::Watch API requirements have been violated (e.g. the user restored from a backup). The
503         // panic message informs the user they should force-close without broadcasting, which is tested
504         // if `reconnect_panicing` is not set.
505         let mut chanmon_cfgs = create_chanmon_cfgs(2);
506         // We broadcast during Drop because chanmon is out of sync with chanmgr, which would cause a panic
507         // during signing due to revoked tx
508         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
509         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
510         let persister;
511         let new_chain_monitor;
512
513         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
514         let nodes_0_deserialized;
515
516         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
517
518         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
519
520         // Cache node A state before any channel update
521         let previous_node_state = nodes[0].node.encode();
522         let previous_chain_monitor_state = get_monitor!(nodes[0], chan.2).encode();
523
524         assert!(!substantially_old || !not_stale, "substantially_old and not_stale doesn't make sense");
525         if not_stale || !substantially_old {
526                 // Previously, we'd only hit the data_loss_protect assertion if we had a state which
527                 // revoked at least two revocations ago, not the latest revocation. Here, we use
528                 // `not_stale` to test the boundary condition.
529                 let pay_params = PaymentParameters::for_keysend(nodes[1].node.get_our_node_id(), 100, false);
530                 let route_params = RouteParameters::from_payment_params_and_value(pay_params, 40000);
531                 nodes[0].node.send_spontaneous_payment_with_retry(None, RecipientOnionFields::spontaneous_empty(), PaymentId([0; 32]), route_params, Retry::Attempts(0)).unwrap();
532                 check_added_monitors(&nodes[0], 1);
533                 let update_add_commit = SendEvent::from_node(&nodes[0]);
534
535                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_commit.msgs[0]);
536                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update_add_commit.commitment_msg);
537                 check_added_monitors(&nodes[1], 1);
538                 let (raa, cs) = get_revoke_commit_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
539
540                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
541                 check_added_monitors(&nodes[0], 1);
542                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
543                 if !not_stale {
544                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &cs);
545                         check_added_monitors(&nodes[0], 1);
546                         // A now revokes their original state, at which point reconnect should panic
547                         let raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
548                         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
549                         check_added_monitors(&nodes[1], 1);
550                         expect_pending_htlcs_forwardable_ignore!(nodes[1]);
551                 }
552         } else {
553                 send_payment(&nodes[0], &[&nodes[1]], 8000000);
554                 send_payment(&nodes[0], &[&nodes[1]], 8000000);
555         }
556
557         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
558         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
559
560         reload_node!(nodes[0], previous_node_state, &[&previous_chain_monitor_state], persister, new_chain_monitor, nodes_0_deserialized);
561
562         if reconnect_panicing {
563                 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
564                         features: nodes[1].node.init_features(), networks: None, remote_network_address: None
565                 }, true).unwrap();
566                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
567                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
568                 }, false).unwrap();
569
570                 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
571
572                 // If A has fallen behind substantially, B should send it a message letting it know
573                 // that.
574                 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
575                 let reestablish_msg;
576                 if substantially_old {
577                         let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction: 0 (received) vs 4 (expected)".to_owned();
578
579                         let warn_reestablish = nodes[1].node.get_and_clear_pending_msg_events();
580                         assert_eq!(warn_reestablish.len(), 2);
581                         match warn_reestablish[1] {
582                                 MessageSendEvent::HandleError { action: ErrorAction::SendWarningMessage { ref msg, .. }, .. } => {
583                                         assert_eq!(msg.data, warn_msg);
584                                 },
585                                 _ => panic!("Unexpected events: {:?}", warn_reestablish),
586                         }
587                         reestablish_msg = match &warn_reestablish[0] {
588                                 MessageSendEvent::SendChannelReestablish { msg, .. } => msg.clone(),
589                                 _ => panic!("Unexpected events: {:?}", warn_reestablish),
590                         };
591                 } else {
592                         let msgs = nodes[1].node.get_and_clear_pending_msg_events();
593                         assert!(msgs.len() >= 4);
594                         match msgs.last() {
595                                 Some(MessageSendEvent::SendChannelUpdate { .. }) => {},
596                                 _ => panic!("Unexpected events: {:?}", msgs),
597                         }
598                         assert!(msgs.iter().any(|msg| matches!(msg, MessageSendEvent::SendRevokeAndACK { .. })));
599                         assert!(msgs.iter().any(|msg| matches!(msg, MessageSendEvent::UpdateHTLCs { .. })));
600                         reestablish_msg = match &msgs[0] {
601                                 MessageSendEvent::SendChannelReestablish { msg, .. } => msg.clone(),
602                                 _ => panic!("Unexpected events: {:?}", msgs),
603                         };
604                 }
605
606                 {
607                         let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
608                         // The node B should never force-close the channel.
609                         assert!(node_txn.is_empty());
610                 }
611
612                 // Check A panics upon seeing proof it has fallen behind.
613                 let reconnect_res = std::panic::catch_unwind(|| {
614                         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_msg);
615                 });
616                 if not_stale {
617                         assert!(reconnect_res.is_ok());
618                         // At this point A gets confused because B expects a commitment state newer than A
619                         // has sent, but not a newer revocation secret, so A just (correctly) closes.
620                         check_closed_broadcast(&nodes[0], 1, true);
621                         check_added_monitors(&nodes[0], 1);
622                         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError {
623                                 err: "Peer attempted to reestablish channel with a future remote commitment transaction: 2 (received) vs 1 (expected)".to_owned()
624                         }, [nodes[1].node.get_our_node_id()], 1000000);
625                 } else {
626                         assert!(reconnect_res.is_err());
627                         // Skip the `Drop` handler for `Node`s as some may be in an invalid (panicked) state.
628                         std::mem::forget(nodes);
629                 }
630         } else {
631                 let error_message = "Channel force-closed";
632                 assert!(!not_stale, "We only care about the stale case when not testing panicking");
633
634                 nodes[0].node.force_close_without_broadcasting_txn(&chan.2, &nodes[1].node.get_our_node_id(), error_message.to_string()).unwrap();
635                 check_added_monitors!(nodes[0], 1);
636                 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 1000000);
637                 {
638                         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
639                         assert_eq!(node_txn.len(), 0);
640                 }
641
642                 for msg in nodes[0].node.get_and_clear_pending_msg_events() {
643                         if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg {
644                         } else if let MessageSendEvent::HandleError { ref action, .. } = msg {
645                                 match action {
646                                         &ErrorAction::SendErrorMessage { ref msg } => {
647                                                 assert_eq!(&msg.data, "Channel force-closed");
648                                         },
649                                         _ => panic!("Unexpected event!"),
650                                 }
651                         } else {
652                                 panic!("Unexpected event {:?}", msg)
653                         }
654                 }
655
656                 // after the warning message sent by B, we should not able to
657                 // use the channel, or reconnect with success to the channel.
658                 assert!(nodes[0].node.list_usable_channels().is_empty());
659                 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
660                         features: nodes[1].node.init_features(), networks: None, remote_network_address: None
661                 }, true).unwrap();
662                 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
663                         features: nodes[0].node.init_features(), networks: None, remote_network_address: None
664                 }, false).unwrap();
665                 let retry_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
666
667                 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &retry_reestablish[0]);
668                 let mut err_msgs_0 = Vec::with_capacity(1);
669                 if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[1] {
670                         match action {
671                                 &ErrorAction::SendErrorMessage { ref msg } => {
672                                         assert_eq!(msg.data, format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id()));
673                                         err_msgs_0.push(msg.clone());
674                                 },
675                                 _ => panic!("Unexpected event!"),
676                         }
677                 } else {
678                         panic!("Unexpected event!");
679                 }
680                 assert_eq!(err_msgs_0.len(), 1);
681                 nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &err_msgs_0[0]);
682                 assert!(nodes[1].node.list_usable_channels().is_empty());
683                 check_added_monitors!(nodes[1], 1);
684                 check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id())) }
685                         , [nodes[0].node.get_our_node_id()], 1000000);
686                 check_closed_broadcast!(nodes[1], false);
687         }
688 }
689
690 #[test]
691 #[cfg(feature = "std")]
692 fn test_data_loss_protect() {
693         do_test_data_loss_protect(true, false, true);
694         do_test_data_loss_protect(true, true, false);
695         do_test_data_loss_protect(true, false, false);
696         do_test_data_loss_protect(false, true, false);
697         do_test_data_loss_protect(false, false, false);
698 }
699
700 #[test]
701 fn test_forwardable_regen() {
702         // Tests that if we reload a ChannelManager while forwards are pending we will regenerate the
703         // PendingHTLCsForwardable event automatically, ensuring we don't forget to forward/receive
704         // HTLCs.
705         // We test it for both payment receipt and payment forwarding.
706
707         let chanmon_cfgs = create_chanmon_cfgs(3);
708         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
709         let persister;
710         let new_chain_monitor;
711         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
712         let nodes_1_deserialized;
713         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
714         let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
715         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
716
717         // First send a payment to nodes[1]
718         let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
719         nodes[0].node.send_payment_with_route(&route, payment_hash,
720                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
721         check_added_monitors!(nodes[0], 1);
722
723         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
724         assert_eq!(events.len(), 1);
725         let payment_event = SendEvent::from_event(events.pop().unwrap());
726         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
727         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
728
729         expect_pending_htlcs_forwardable_ignore!(nodes[1]);
730
731         // Next send a payment which is forwarded by nodes[1]
732         let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 200_000);
733         nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
734                 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
735         check_added_monitors!(nodes[0], 1);
736
737         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
738         assert_eq!(events.len(), 1);
739         let payment_event = SendEvent::from_event(events.pop().unwrap());
740         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
741         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
742
743         // There is already a PendingHTLCsForwardable event "pending" so another one will not be
744         // generated
745         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
746
747         // Now restart nodes[1] and make sure it regenerates a single PendingHTLCsForwardable
748         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
749         nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
750
751         let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
752         let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
753         reload_node!(nodes[1], nodes[1].node.encode(), &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
754
755         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
756         // Note that nodes[1] and nodes[2] resend their channel_ready here since they haven't updated
757         // the commitment state.
758         let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[2]);
759         reconnect_args.send_channel_ready = (true, true);
760         reconnect_nodes(reconnect_args);
761
762         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
763
764         expect_pending_htlcs_forwardable!(nodes[1]);
765         expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 100_000);
766         check_added_monitors!(nodes[1], 1);
767
768         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
769         assert_eq!(events.len(), 1);
770         let payment_event = SendEvent::from_event(events.pop().unwrap());
771         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
772         commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false);
773         expect_pending_htlcs_forwardable!(nodes[2]);
774         expect_payment_claimable!(nodes[2], payment_hash_2, payment_secret_2, 200_000);
775
776         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
777         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
778 }
779
780 fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
781         // Test what happens if a node receives an MPP payment, claims it, but crashes before
782         // persisting the ChannelManager. If `persist_both_monitors` is false, also crash after only
783         // updating one of the two channels' ChannelMonitors. As a result, on startup, we'll (a) still
784         // have the PaymentClaimable event, (b) have one (or two) channel(s) that goes on chain with the
785         // HTLC preimage in them, and (c) optionally have one channel that is live off-chain but does
786         // not have the preimage tied to the still-pending HTLC.
787         //
788         // To get to the correct state, on startup we should propagate the preimage to the
789         // still-off-chain channel, claiming the HTLC as soon as the peer connects, with the monitor
790         // receiving the preimage without a state update.
791         //
792         // Further, we should generate a `PaymentClaimed` event to inform the user that the payment was
793         // definitely claimed.
794         let chanmon_cfgs = create_chanmon_cfgs(4);
795         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
796         let persister;
797         let new_chain_monitor;
798
799         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
800         let nodes_3_deserialized;
801
802         let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
803
804         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
805         create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
806         let chan_id_persisted = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0).2;
807         let chan_id_not_persisted = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0).2;
808
809         // Create an MPP route for 15k sats, more than the default htlc-max of 10%
810         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
811         assert_eq!(route.paths.len(), 2);
812         route.paths.sort_by(|path_a, _| {
813                 // Sort the path so that the path through nodes[1] comes first
814                 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
815                         core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
816         });
817
818         nodes[0].node.send_payment_with_route(&route, payment_hash,
819                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
820         check_added_monitors!(nodes[0], 2);
821
822         // Send the payment through to nodes[3] *without* clearing the PaymentClaimable event
823         let mut send_events = nodes[0].node.get_and_clear_pending_msg_events();
824         assert_eq!(send_events.len(), 2);
825         let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut send_events);
826         let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut send_events);
827         do_pass_along_path(PassAlongPathArgs::new(&nodes[0],&[&nodes[1], &nodes[3]], 15_000_000, payment_hash, node_1_msgs)
828                 .with_payment_secret(payment_secret)
829                 .without_clearing_recipient_events());
830         do_pass_along_path(PassAlongPathArgs::new(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, node_2_msgs)
831                 .with_payment_secret(payment_secret)
832                 .without_clearing_recipient_events());
833
834         // Now that we have an MPP payment pending, get the latest encoded copies of nodes[3]'s
835         // monitors and ChannelManager, for use later, if we don't want to persist both monitors.
836         let mut original_monitor = test_utils::TestVecWriter(Vec::new());
837         if !persist_both_monitors {
838                 for (outpoint, channel_id) in nodes[3].chain_monitor.chain_monitor.list_monitors() {
839                         if channel_id == chan_id_not_persisted {
840                                 assert!(original_monitor.0.is_empty());
841                                 nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut original_monitor).unwrap();
842                         }
843                 }
844         }
845
846         let original_manager = nodes[3].node.encode();
847
848         expect_payment_claimable!(nodes[3], payment_hash, payment_secret, 15_000_000);
849
850         nodes[3].node.claim_funds(payment_preimage);
851         check_added_monitors!(nodes[3], 2);
852         expect_payment_claimed!(nodes[3], payment_hash, 15_000_000);
853
854         // Now fetch one of the two updated ChannelMonitors from nodes[3], and restart pretending we
855         // crashed in between the two persistence calls - using one old ChannelMonitor and one new one,
856         // with the old ChannelManager.
857         let mut updated_monitor = test_utils::TestVecWriter(Vec::new());
858         for (outpoint, channel_id) in nodes[3].chain_monitor.chain_monitor.list_monitors() {
859                 if channel_id == chan_id_persisted {
860                         assert!(updated_monitor.0.is_empty());
861                         nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut updated_monitor).unwrap();
862                 }
863         }
864         // If `persist_both_monitors` is set, get the second monitor here as well
865         if persist_both_monitors {
866                 for (outpoint, channel_id) in nodes[3].chain_monitor.chain_monitor.list_monitors() {
867                         if channel_id == chan_id_not_persisted {
868                                 assert!(original_monitor.0.is_empty());
869                                 nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut original_monitor).unwrap();
870                         }
871                 }
872         }
873
874         // Now restart nodes[3].
875         reload_node!(nodes[3], original_manager, &[&updated_monitor.0, &original_monitor.0], persister, new_chain_monitor, nodes_3_deserialized);
876
877         // On startup the preimage should have been copied into the non-persisted monitor:
878         assert!(get_monitor!(nodes[3], chan_id_persisted).get_stored_preimages().contains_key(&payment_hash));
879         assert!(get_monitor!(nodes[3], chan_id_not_persisted).get_stored_preimages().contains_key(&payment_hash));
880
881         nodes[1].node.peer_disconnected(&nodes[3].node.get_our_node_id());
882         nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id());
883
884         // During deserialization, we should have closed one channel and broadcast its latest
885         // commitment transaction. We should also still have the original PaymentClaimable event we
886         // never finished processing.
887         let events = nodes[3].node.get_and_clear_pending_events();
888         assert_eq!(events.len(), if persist_both_monitors { 4 } else { 3 });
889         if let Event::PaymentClaimable { amount_msat: 15_000_000, .. } = events[0] { } else { panic!(); }
890         if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[1] { } else { panic!(); }
891         if persist_both_monitors {
892                 if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[2] { } else { panic!(); }
893                 check_added_monitors(&nodes[3], 2);
894         } else {
895                 check_added_monitors(&nodes[3], 1);
896         }
897
898         // On restart, we should also get a duplicate PaymentClaimed event as we persisted the
899         // ChannelManager prior to handling the original one.
900         if let Event::PaymentClaimed { payment_hash: our_payment_hash, amount_msat: 15_000_000, .. } =
901                 events[if persist_both_monitors { 3 } else { 2 }]
902         {
903                 assert_eq!(payment_hash, our_payment_hash);
904         } else { panic!(); }
905
906         assert_eq!(nodes[3].node.list_channels().len(), if persist_both_monitors { 0 } else { 1 });
907         if !persist_both_monitors {
908                 // If one of the two channels is still live, reveal the payment preimage over it.
909
910                 nodes[3].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init {
911                         features: nodes[2].node.init_features(), networks: None, remote_network_address: None
912                 }, true).unwrap();
913                 let reestablish_1 = get_chan_reestablish_msgs!(nodes[3], nodes[2]);
914                 nodes[2].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
915                         features: nodes[3].node.init_features(), networks: None, remote_network_address: None
916                 }, false).unwrap();
917                 let reestablish_2 = get_chan_reestablish_msgs!(nodes[2], nodes[3]);
918
919                 nodes[2].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish_1[0]);
920                 get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
921                 assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
922
923                 nodes[3].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &reestablish_2[0]);
924
925                 // Once we call `get_and_clear_pending_msg_events` the holding cell is cleared and the HTLC
926                 // claim should fly.
927                 let ds_msgs = nodes[3].node.get_and_clear_pending_msg_events();
928                 check_added_monitors!(nodes[3], 1);
929                 assert_eq!(ds_msgs.len(), 2);
930                 if let MessageSendEvent::SendChannelUpdate { .. } = ds_msgs[0] {} else { panic!(); }
931
932                 let cs_updates = match ds_msgs[1] {
933                         MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
934                                 nodes[2].node.handle_update_fulfill_htlc(&nodes[3].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
935                                 check_added_monitors!(nodes[2], 1);
936                                 let cs_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
937                                 expect_payment_forwarded!(nodes[2], nodes[0], nodes[3], Some(1000), false, false);
938                                 commitment_signed_dance!(nodes[2], nodes[3], updates.commitment_signed, false, true);
939                                 cs_updates
940                         }
941                         _ => panic!(),
942                 };
943
944                 nodes[0].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
945                 commitment_signed_dance!(nodes[0], nodes[2], cs_updates.commitment_signed, false, true);
946                 expect_payment_sent!(nodes[0], payment_preimage);
947         }
948 }
949
950 #[test]
951 fn test_partial_claim_before_restart() {
952         do_test_partial_claim_before_restart(false);
953         do_test_partial_claim_before_restart(true);
954 }
955
956 fn do_forwarded_payment_no_manager_persistence(use_cs_commitment: bool, claim_htlc: bool, use_intercept: bool) {
957         if !use_cs_commitment { assert!(!claim_htlc); }
958         // If we go to forward a payment, and the ChannelMonitor persistence completes, but the
959         // ChannelManager does not, we shouldn't try to forward the payment again, nor should we fail
960         // it back until the ChannelMonitor decides the fate of the HTLC.
961         // This was never an issue, but it may be easy to regress here going forward.
962         let chanmon_cfgs = create_chanmon_cfgs(3);
963         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
964         let persister;
965         let new_chain_monitor;
966
967         let mut intercept_forwards_config = test_default_channel_config();
968         intercept_forwards_config.accept_intercept_htlcs = true;
969         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);
970         let nodes_1_deserialized;
971
972         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
973
974         let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
975         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
976
977         let intercept_scid = nodes[1].node.get_intercept_scid();
978
979         let (mut route, payment_hash, payment_preimage, payment_secret) =
980                 get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
981         if use_intercept {
982                 route.paths[0].hops[1].short_channel_id = intercept_scid;
983         }
984         let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
985         let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV;
986         nodes[0].node.send_payment_with_route(&route, payment_hash,
987                 RecipientOnionFields::secret_only(payment_secret), payment_id).unwrap();
988         check_added_monitors!(nodes[0], 1);
989
990         let payment_event = SendEvent::from_node(&nodes[0]);
991         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
992         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
993
994         // Store the `ChannelManager` before handling the `PendingHTLCsForwardable`/`HTLCIntercepted`
995         // events, expecting either event (and the HTLC itself) to be missing on reload even though its
996         // present when we serialized.
997         let node_encoded = nodes[1].node.encode();
998
999         let mut intercept_id = None;
1000         let mut expected_outbound_amount_msat = None;
1001         if use_intercept {
1002                 let events = nodes[1].node.get_and_clear_pending_events();
1003                 assert_eq!(events.len(), 1);
1004                 match events[0] {
1005                         Event::HTLCIntercepted { intercept_id: ev_id, expected_outbound_amount_msat: ev_amt, .. } => {
1006                                 intercept_id = Some(ev_id);
1007                                 expected_outbound_amount_msat = Some(ev_amt);
1008                         },
1009                         _ => panic!()
1010                 }
1011                 nodes[1].node.forward_intercepted_htlc(intercept_id.unwrap(), &chan_id_2,
1012                         nodes[2].node.get_our_node_id(), expected_outbound_amount_msat.unwrap()).unwrap();
1013         }
1014
1015         expect_pending_htlcs_forwardable!(nodes[1]);
1016
1017         let payment_event = SendEvent::from_node(&nodes[1]);
1018         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1019         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
1020         check_added_monitors!(nodes[2], 1);
1021
1022         if claim_htlc {
1023                 get_monitor!(nodes[2], chan_id_2).provide_payment_preimage(&payment_hash, &payment_preimage,
1024                         &nodes[2].tx_broadcaster, &LowerBoundedFeeEstimator(nodes[2].fee_estimator), &nodes[2].logger);
1025         }
1026         assert!(nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1027
1028         let _ = nodes[2].node.get_and_clear_pending_msg_events();
1029         let error_message = "Channel force-closed";
1030
1031         nodes[2].node.force_close_broadcasting_latest_txn(&chan_id_2, &nodes[1].node.get_our_node_id(), error_message.to_string()).unwrap();
1032         let cs_commitment_tx = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1033         assert_eq!(cs_commitment_tx.len(), if claim_htlc { 2 } else { 1 });
1034
1035         check_added_monitors!(nodes[2], 1);
1036         check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
1037         check_closed_broadcast!(nodes[2], true);
1038
1039         let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
1040         let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
1041         reload_node!(nodes[1], node_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
1042
1043         // Note that this checks that this is the only event on nodes[1], implying the
1044         // `HTLCIntercepted` event has been removed in the `use_intercept` case.
1045         check_closed_event!(nodes[1], 1, ClosureReason::OutdatedChannelManager, [nodes[2].node.get_our_node_id()], 100000);
1046
1047         if use_intercept {
1048                 // Attempt to forward the HTLC back out over nodes[1]' still-open channel, ensuring we get
1049                 // a intercept-doesn't-exist error.
1050                 let forward_err = nodes[1].node.forward_intercepted_htlc(intercept_id.unwrap(), &chan_id_1,
1051                         nodes[0].node.get_our_node_id(), expected_outbound_amount_msat.unwrap()).unwrap_err();
1052                 assert_eq!(forward_err, APIError::APIMisuseError {
1053                         err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.unwrap().0))
1054                 });
1055         }
1056
1057         nodes[1].node.timer_tick_occurred();
1058         let bs_commitment_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1059         assert_eq!(bs_commitment_tx.len(), 1);
1060         check_added_monitors!(nodes[1], 1);
1061
1062         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
1063         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
1064
1065         if use_cs_commitment {
1066                 // If we confirm a commitment transaction that has the HTLC on-chain, nodes[1] should wait
1067                 // for an HTLC-spending transaction before it does anything with the HTLC upstream.
1068                 confirm_transaction(&nodes[1], &cs_commitment_tx[0]);
1069                 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1070                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1071
1072                 if claim_htlc {
1073                         confirm_transaction(&nodes[1], &cs_commitment_tx[1]);
1074                 } else {
1075                         connect_blocks(&nodes[1], htlc_expiry - nodes[1].best_block_info().1 + 1);
1076                         let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
1077                         assert_eq!(txn.len(), if nodes[1].connect_style.borrow().updates_best_block_first() { 2 } else { 1 });
1078                         let bs_htlc_timeout_tx = txn.pop().unwrap();
1079                         confirm_transaction(&nodes[1], &bs_htlc_timeout_tx);
1080                 }
1081         } else {
1082                 confirm_transaction(&nodes[1], &bs_commitment_tx[0]);
1083         }
1084
1085         if !claim_htlc {
1086                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
1087         } else {
1088                 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, true);
1089         }
1090         check_added_monitors!(nodes[1], 1);
1091
1092         let events = nodes[1].node.get_and_clear_pending_msg_events();
1093         assert_eq!(events.len(), 1);
1094         match &events[0] {
1095                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fulfill_htlcs, update_fail_htlcs, commitment_signed, .. }, .. } => {
1096                         if claim_htlc {
1097                                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
1098                         } else {
1099                                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
1100                         }
1101                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
1102                 },
1103                 _ => panic!("Unexpected event"),
1104         }
1105
1106         if claim_htlc {
1107                 expect_payment_sent!(nodes[0], payment_preimage);
1108         } else {
1109                 expect_payment_failed!(nodes[0], payment_hash, false);
1110         }
1111 }
1112
1113 #[test]
1114 fn forwarded_payment_no_manager_persistence() {
1115         do_forwarded_payment_no_manager_persistence(true, true, false);
1116         do_forwarded_payment_no_manager_persistence(true, false, false);
1117         do_forwarded_payment_no_manager_persistence(false, false, false);
1118 }
1119
1120 #[test]
1121 fn intercepted_payment_no_manager_persistence() {
1122         do_forwarded_payment_no_manager_persistence(true, true, true);
1123         do_forwarded_payment_no_manager_persistence(true, false, true);
1124         do_forwarded_payment_no_manager_persistence(false, false, true);
1125 }
1126
1127 #[test]
1128 fn removed_payment_no_manager_persistence() {
1129         // If an HTLC is failed to us on a channel, and the ChannelMonitor persistence completes, but
1130         // the corresponding ChannelManager persistence does not, we need to ensure that the HTLC is
1131         // still failed back to the previous hop even though the ChannelMonitor now no longer is aware
1132         // of the HTLC. This was previously broken as no attempt was made to figure out which HTLCs
1133         // were left dangling when a channel was force-closed due to a stale ChannelManager.
1134         let chanmon_cfgs = create_chanmon_cfgs(3);
1135         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1136         let persister;
1137         let new_chain_monitor;
1138
1139         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1140         let nodes_1_deserialized;
1141
1142         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1143
1144         let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
1145         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;
1146
1147         let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
1148
1149         let node_encoded = nodes[1].node.encode();
1150
1151         nodes[2].node.fail_htlc_backwards(&payment_hash);
1152         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], [HTLCDestination::FailedPayment { payment_hash }]);
1153         check_added_monitors!(nodes[2], 1);
1154         let events = nodes[2].node.get_and_clear_pending_msg_events();
1155         assert_eq!(events.len(), 1);
1156         match &events[0] {
1157                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. }, .. } => {
1158                         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &update_fail_htlcs[0]);
1159                         commitment_signed_dance!(nodes[1], nodes[2], commitment_signed, false);
1160                 },
1161                 _ => panic!("Unexpected event"),
1162         }
1163
1164         let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
1165         let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
1166         reload_node!(nodes[1], node_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
1167
1168         match nodes[1].node.pop_pending_event().unwrap() {
1169                 Event::ChannelClosed { ref reason, .. } => {
1170                         assert_eq!(*reason, ClosureReason::OutdatedChannelManager);
1171                 },
1172                 _ => panic!("Unexpected event"),
1173         }
1174
1175         nodes[1].node.test_process_background_events();
1176         check_added_monitors(&nodes[1], 1);
1177
1178         // Now that the ChannelManager has force-closed the channel which had the HTLC removed, it is
1179         // now forgotten everywhere. The ChannelManager should have, as a side-effect of reload,
1180         // learned that the HTLC is gone from the ChannelMonitor and added it to the to-fail-back set.
1181         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
1182         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
1183
1184         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], [HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
1185         check_added_monitors!(nodes[1], 1);
1186         let events = nodes[1].node.get_and_clear_pending_msg_events();
1187         assert_eq!(events.len(), 1);
1188         match &events[0] {
1189                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. }, .. } => {
1190                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
1191                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
1192                 },
1193                 _ => panic!("Unexpected event"),
1194         }
1195
1196         expect_payment_failed!(nodes[0], payment_hash, false);
1197 }
1198
1199 #[test]
1200 fn test_reload_partial_funding_batch() {
1201         let chanmon_cfgs = create_chanmon_cfgs(3);
1202         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1203         let new_persister;
1204         let new_chain_monitor;
1205
1206         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1207         let new_channel_manager;
1208         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1209
1210         // Initiate channel opening and create the batch channel funding transaction.
1211         let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[
1212                 (&nodes[1], 100_000, 0, 42, None),
1213                 (&nodes[2], 200_000, 0, 43, None),
1214         ]);
1215
1216         // Go through the funding_created and funding_signed flow with node 1.
1217         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[0]);
1218         check_added_monitors(&nodes[1], 1);
1219         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
1220
1221         // The monitor is persisted when receiving funding_signed.
1222         let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
1223         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
1224         check_added_monitors(&nodes[0], 1);
1225
1226         // The transaction should not have been broadcast before all channels are ready.
1227         assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
1228
1229         // Reload the node while a subset of the channels in the funding batch have persisted monitors.
1230         let channel_id_1 = ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.txid(), index: 0 });
1231         let node_encoded = nodes[0].node.encode();
1232         let channel_monitor_1_serialized = get_monitor!(nodes[0], channel_id_1).encode();
1233         reload_node!(nodes[0], node_encoded, &[&channel_monitor_1_serialized], new_persister, new_chain_monitor, new_channel_manager);
1234
1235         // Process monitor events.
1236         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1237
1238         // The monitor should become closed.
1239         check_added_monitors(&nodes[0], 1);
1240         {
1241                 let mut monitor_updates = nodes[0].chain_monitor.monitor_updates.lock().unwrap();
1242                 let monitor_updates_1 = monitor_updates.get(&channel_id_1).unwrap();
1243                 assert_eq!(monitor_updates_1.len(), 1);
1244                 assert_eq!(monitor_updates_1[0].update_id, CLOSED_CHANNEL_UPDATE_ID);
1245         }
1246
1247         // The funding transaction should not have been broadcast, but we broadcast the force-close
1248         // transaction as part of closing the monitor.
1249         {
1250                 let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
1251                 assert_eq!(broadcasted_txs.len(), 1);
1252                 assert!(broadcasted_txs[0].txid() != tx.txid());
1253                 assert_eq!(broadcasted_txs[0].input.len(), 1);
1254                 assert_eq!(broadcasted_txs[0].input[0].previous_output.txid, tx.txid());
1255         }
1256
1257         // Ensure the channels don't exist anymore.
1258         assert!(nodes[0].node.list_channels().is_empty());
1259 }