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