lightning 0.0.116

A Bitcoin Lightning library in Rust. Does most of the hard work, without implying a specific runtime, requiring clients implement basic network logic, chain interactions and disk storage. Still missing tons of error-handling. See GitHub issues for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to build a client around it to even try.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Functional tests which test for correct behavior across node restarts.

use crate::chain::{ChannelMonitorUpdateStatus, Watch};
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
use crate::chain::channelmonitor::ChannelMonitor;
use crate::sign::EntropySource;
use crate::chain::transaction::OutPoint;
use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
use crate::ln::msgs;
use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
use crate::util::enforcing_trait_impls::EnforcingSigner;
use crate::util::test_utils;
use crate::util::errors::APIError;
use crate::util::ser::{Writeable, ReadableArgs};
use crate::util::config::UserConfig;
use crate::util::string::UntrustedString;

use bitcoin::hash_types::BlockHash;

use crate::prelude::*;
use core::default::Default;
use crate::sync::Mutex;

use crate::ln::functional_test_utils::*;

#[test]
fn test_funding_peer_disconnect() {
	// Test that we can lock in our funding tx while disconnected
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_0_deserialized: 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>;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001);

	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	confirm_transaction(&nodes[0], &tx);
	let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
	assert!(events_1.is_empty());

	reconnect_nodes(&nodes[0], &nodes[1], (false, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));

	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	confirm_transaction(&nodes[1], &tx);
	let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
	assert!(events_2.is_empty());

	nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
		features: nodes[1].node.init_features(), networks: None, remote_network_address: None
	}, true).unwrap();
	let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap();
	nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None
	}, false).unwrap();
	let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap();

	// nodes[0] hasn't yet received a channel_ready, so it only sends that on reconnect.
	nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
	let events_3 = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events_3.len(), 1);
	let as_channel_ready = match events_3[0] {
		MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
			assert_eq!(*node_id, nodes[1].node.get_our_node_id());
			msg.clone()
		},
		_ => panic!("Unexpected event {:?}", events_3[0]),
	};

	// nodes[1] received nodes[0]'s channel_ready on the first reconnect above, so it should send
	// announcement_signatures as well as channel_update.
	nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
	let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events_4.len(), 3);
	let chan_id;
	let bs_channel_ready = match events_4[0] {
		MessageSendEvent::SendChannelReady { ref node_id, ref msg } => {
			assert_eq!(*node_id, nodes[0].node.get_our_node_id());
			chan_id = msg.channel_id;
			msg.clone()
		},
		_ => panic!("Unexpected event {:?}", events_4[0]),
	};
	let bs_announcement_sigs = match events_4[1] {
		MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
			assert_eq!(*node_id, nodes[0].node.get_our_node_id());
			msg.clone()
		},
		_ => panic!("Unexpected event {:?}", events_4[1]),
	};
	match events_4[2] {
		MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } => {
			assert_eq!(*node_id, nodes[0].node.get_our_node_id());
		},
		_ => panic!("Unexpected event {:?}", events_4[2]),
	}

	// Re-deliver nodes[0]'s channel_ready, which nodes[1] can safely ignore. It currently
	// generates a duplicative private channel_update
	nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
	let events_5 = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events_5.len(), 1);
	match events_5[0] {
		MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } => {
			assert_eq!(*node_id, nodes[0].node.get_our_node_id());
		},
		_ => panic!("Unexpected event {:?}", events_5[0]),
	};

	// When we deliver nodes[1]'s channel_ready, however, nodes[0] will generate its
	// announcement_signatures.
	nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready);
	let events_6 = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events_6.len(), 1);
	let as_announcement_sigs = match events_6[0] {
		MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
			assert_eq!(*node_id, nodes[1].node.get_our_node_id());
			msg.clone()
		},
		_ => panic!("Unexpected event {:?}", events_6[0]),
	};
	expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
	expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());

	// When we deliver nodes[1]'s announcement_signatures to nodes[0], nodes[0] should immediately
	// broadcast the channel announcement globally, as well as re-send its (now-public)
	// channel_update.
	nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
	let events_7 = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events_7.len(), 1);
	let (chan_announcement, as_update) = match events_7[0] {
		MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
			(msg.clone(), update_msg.clone().unwrap())
		},
		_ => panic!("Unexpected event {:?}", events_7[0]),
	};

	// Finally, deliver nodes[0]'s announcement_signatures to nodes[1] and make sure it creates the
	// same channel_announcement.
	nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs);
	let events_8 = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events_8.len(), 1);
	let bs_update = match events_8[0] {
		MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
			assert_eq!(*msg, chan_announcement);
			update_msg.clone().unwrap()
		},
		_ => panic!("Unexpected event {:?}", events_8[0]),
	};

	// Provide the channel announcement and public updates to the network graph
	nodes[0].gossip_sync.handle_channel_announcement(&chan_announcement).unwrap();
	nodes[0].gossip_sync.handle_channel_update(&bs_update).unwrap();
	nodes[0].gossip_sync.handle_channel_update(&as_update).unwrap();

	let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
	let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
	claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);

	// Check that after deserialization and reconnection we can still generate an identical
	// channel_announcement from the cached signatures.
	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();

	reload_node!(nodes[0], &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);

	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
}

#[test]
fn test_no_txn_manager_serialize_deserialize() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_0_deserialized: 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>;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001);

	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	let chan_0_monitor_serialized =
		get_monitor!(nodes[0], OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).encode();
	reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);

	nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
		features: nodes[1].node.init_features(), networks: None, remote_network_address: None
	}, true).unwrap();
	let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
	nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None
	}, false).unwrap();
	let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);

	nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());

	let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
	let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
	for node in nodes.iter() {
		assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap());
		node.gossip_sync.handle_channel_update(&as_update).unwrap();
		node.gossip_sync.handle_channel_update(&bs_update).unwrap();
	}

	send_payment(&nodes[0], &[&nodes[1]], 1000000);
}

#[test]
fn test_manager_serialize_deserialize_events() {
	// This test makes sure the events field in ChannelManager survives de/serialization
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_0_deserialized: 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>;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	// Start creating a channel, but stop right before broadcasting the funding transaction
	let channel_value = 100000;
	let push_msat = 10001;
	let node_a = nodes.remove(0);
	let node_b = nodes.remove(0);
	node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
	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()));
	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()));

	let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&node_a, &node_b.node.get_our_node_id(), channel_value, 42);

	node_a.node.funding_transaction_generated(&temporary_channel_id, &node_b.node.get_our_node_id(), tx.clone()).unwrap();
	check_added_monitors!(node_a, 0);

	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()));
	{
		let mut added_monitors = node_b.chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, funding_output);
		added_monitors.clear();
	}

	let bs_funding_signed = get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id());
	node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &bs_funding_signed);
	{
		let mut added_monitors = node_a.chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, funding_output);
		added_monitors.clear();
	}
	// Normally, this is where node_a would broadcast the funding transaction, but the test de/serializes first instead

	expect_channel_pending_event(&node_a, &node_b.node.get_our_node_id());
	expect_channel_pending_event(&node_b, &node_a.node.get_our_node_id());

	nodes.push(node_a);
	nodes.push(node_b);

	// Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
	let chan_0_monitor_serialized = get_monitor!(nodes[0], bs_funding_signed.channel_id).encode();
	reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);

	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	// After deserializing, make sure the funding_transaction is still held by the channel manager
	let events_4 = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events_4.len(), 0);
	assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
	assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].txid(), funding_output.txid);

	// Make sure the channel is functioning as though the de/serialization never happened
	assert_eq!(nodes[0].node.list_channels().len(), 1);

	nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
		features: nodes[1].node.init_features(), networks: None, remote_network_address: None
	}, true).unwrap();
	let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
	nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None
	}, false).unwrap();
	let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);

	nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());

	let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
	let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
	for node in nodes.iter() {
		assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap());
		node.gossip_sync.handle_channel_update(&as_update).unwrap();
		node.gossip_sync.handle_channel_update(&bs_update).unwrap();
	}

	send_payment(&nodes[0], &[&nodes[1]], 1000000);
}

#[test]
fn test_simple_manager_serialize_deserialize() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_0_deserialized: 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>;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
	let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);

	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
	reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);

	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));

	fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
	claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
}

#[test]
fn test_manager_serialize_deserialize_inconsistent_monitor() {
	// Test deserializing a ChannelManager with an out-of-date ChannelMonitor
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
	let logger: test_utils::TestLogger;
	let fee_estimator: test_utils::TestFeeEstimator;
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_0_deserialized: 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>;
	let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
	let chan_id_2 = create_announced_chan_between_nodes(&nodes, 2, 0).2;
	let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3);

	let mut node_0_stale_monitors_serialized = Vec::new();
	for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
		let mut writer = test_utils::TestVecWriter(Vec::new());
		get_monitor!(nodes[0], chan_id_iter).write(&mut writer).unwrap();
		node_0_stale_monitors_serialized.push(writer.0);
	}

	let (our_payment_preimage, _, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);

	// Serialize the ChannelManager here, but the monitor we keep up-to-date
	let nodes_0_serialized = nodes[0].node.encode();

	route_payment(&nodes[0], &[&nodes[3]], 1000000);
	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
	nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id());
	nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	// Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
	// nodes[3])
	let mut node_0_monitors_serialized = Vec::new();
	for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] {
		node_0_monitors_serialized.push(get_monitor!(nodes[0], chan_id_iter).encode());
	}

	logger = test_utils::TestLogger::new();
	fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
	persister = test_utils::TestPersister::new();
	let keys_manager = &chanmon_cfgs[0].keys_manager;
	new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
	nodes[0].chain_monitor = &new_chain_monitor;


	let mut node_0_stale_monitors = Vec::new();
	for serialized in node_0_stale_monitors_serialized.iter() {
		let mut read = &serialized[..];
		let (_, monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut read, (keys_manager, keys_manager)).unwrap();
		assert!(read.is_empty());
		node_0_stale_monitors.push(monitor);
	}

	let mut node_0_monitors = Vec::new();
	for serialized in node_0_monitors_serialized.iter() {
		let mut read = &serialized[..];
		let (_, monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut read, (keys_manager, keys_manager)).unwrap();
		assert!(read.is_empty());
		node_0_monitors.push(monitor);
	}

	let mut nodes_0_read = &nodes_0_serialized[..];
	if let Err(msgs::DecodeError::InvalidValue) =
		<(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 {
		default_config: UserConfig::default(),
		entropy_source: keys_manager,
		node_signer: keys_manager,
		signer_provider: keys_manager,
		fee_estimator: &fee_estimator,
		router: &nodes[0].router,
		chain_monitor: nodes[0].chain_monitor,
		tx_broadcaster: nodes[0].tx_broadcaster.clone(),
		logger: &logger,
		channel_monitors: node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
	}) { } else {
		panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
	};

	let mut nodes_0_read = &nodes_0_serialized[..];
	let (_, nodes_0_deserialized_tmp) =
		<(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 {
		default_config: UserConfig::default(),
		entropy_source: keys_manager,
		node_signer: keys_manager,
		signer_provider: keys_manager,
		fee_estimator: &fee_estimator,
		router: nodes[0].router,
		chain_monitor: nodes[0].chain_monitor,
		tx_broadcaster: nodes[0].tx_broadcaster.clone(),
		logger: &logger,
		channel_monitors: node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
	}).unwrap();
	nodes_0_deserialized = nodes_0_deserialized_tmp;
	assert!(nodes_0_read.is_empty());

	for monitor in node_0_monitors.drain(..) {
		assert_eq!(nodes[0].chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor),
			ChannelMonitorUpdateStatus::Completed);
		check_added_monitors!(nodes[0], 1);
	}
	nodes[0].node = &nodes_0_deserialized;

	check_closed_event!(nodes[0], 1, ClosureReason::OutdatedChannelManager);
	{ // Channel close should result in a commitment tx
		nodes[0].node.timer_tick_occurred();
		let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
		assert_eq!(txn.len(), 1);
		check_spends!(txn[0], funding_tx);
		assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
	}
	check_added_monitors!(nodes[0], 1);

	// nodes[1] and nodes[2] have no lost state with nodes[0]...
	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
	reconnect_nodes(&nodes[0], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
	//... and we can even still claim the payment!
	claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);

	nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None
	}, true).unwrap();
	let reestablish = get_chan_reestablish_msgs!(nodes[3], nodes[0]).pop().unwrap();
	nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
		features: nodes[3].node.init_features(), networks: None, remote_network_address: None
	}, false).unwrap();
	nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish);
	let mut found_err = false;
	for msg_event in nodes[0].node.get_and_clear_pending_msg_events() {
		if let MessageSendEvent::HandleError { ref action, .. } = msg_event {
			match action {
				&ErrorAction::SendErrorMessage { ref msg } => {
					assert_eq!(msg.channel_id, channel_id);
					assert!(!found_err);
					found_err = true;
				},
				_ => panic!("Unexpected event!"),
			}
		}
	}
	assert!(found_err);
}

fn do_test_data_loss_protect(reconnect_panicing: bool) {
	// When we get a data_loss_protect proving we're behind, we immediately panic as the
	// chain::Watch API requirements have been violated (e.g. the user restored from a backup). The
	// panic message informs the user they should force-close without broadcasting, which is tested
	// if `reconnect_panicing` is not set.
	let mut chanmon_cfgs = create_chanmon_cfgs(2);
	// We broadcast during Drop because chanmon is out of sync with chanmgr, which would cause a panic
	// during signing due to revoked tx
	chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
	let persister;
	let new_chain_monitor;
	let nodes_0_deserialized;
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);

	// Cache node A state before any channel update
	let previous_node_state = nodes[0].node.encode();
	let previous_chain_monitor_state = get_monitor!(nodes[0], chan.2).encode();

	send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
	send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);

	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());

	reload_node!(nodes[0], previous_node_state, &[&previous_chain_monitor_state], persister, new_chain_monitor, nodes_0_deserialized);

	if reconnect_panicing {
		nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
			features: nodes[1].node.init_features(), networks: None, remote_network_address: None
		}, true).unwrap();
		nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
			features: nodes[0].node.init_features(), networks: None, remote_network_address: None
		}, false).unwrap();

		let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);

		// Check we close channel detecting A is fallen-behind
		// Check that we sent the warning message when we detected that A has fallen behind,
		// and give the possibility for A to recover from the warning.
		nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
		let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction".to_owned();
		assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan.2).contains(&warn_msg));

		{
			let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
			// The node B should not broadcast the transaction to force close the channel!
			assert!(node_txn.is_empty());
		}

		let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
		// Check A panics upon seeing proof it has fallen behind.
		nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_0[0]);
		return; // By this point we should have panic'ed!
	}

	nodes[0].node.force_close_without_broadcasting_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
	check_added_monitors!(nodes[0], 1);
	check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
	{
		let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
		assert_eq!(node_txn.len(), 0);
	}

	for msg in nodes[0].node.get_and_clear_pending_msg_events() {
		if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg {
		} else if let MessageSendEvent::HandleError { ref action, .. } = msg {
			match action {
				&ErrorAction::SendErrorMessage { ref msg } => {
					assert_eq!(msg.data, "Channel force-closed");
				},
				_ => panic!("Unexpected event!"),
			}
		} else {
			panic!("Unexpected event {:?}", msg)
		}
	}

	// after the warning message sent by B, we should not able to
	// use the channel, or reconnect with success to the channel.
	assert!(nodes[0].node.list_usable_channels().is_empty());
	nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
		features: nodes[1].node.init_features(), networks: None, remote_network_address: None
	}, true).unwrap();
	nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None
	}, false).unwrap();
	let retry_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]);

	nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &retry_reestablish[0]);
	let mut err_msgs_0 = Vec::with_capacity(1);
	for msg in nodes[0].node.get_and_clear_pending_msg_events() {
		if let MessageSendEvent::HandleError { ref action, .. } = msg {
			match action {
				&ErrorAction::SendErrorMessage { ref msg } => {
					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()));
					err_msgs_0.push(msg.clone());
				},
				_ => panic!("Unexpected event!"),
			}
		} else {
			panic!("Unexpected event!");
		}
	}
	assert_eq!(err_msgs_0.len(), 1);
	nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &err_msgs_0[0]);
	assert!(nodes[1].node.list_usable_channels().is_empty());
	check_added_monitors!(nodes[1], 1);
	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())) });
	check_closed_broadcast!(nodes[1], false);
}

#[test]
#[should_panic]
fn test_data_loss_protect_showing_stale_state_panics() {
	do_test_data_loss_protect(true);
}

#[test]
fn test_force_close_without_broadcast() {
	do_test_data_loss_protect(false);
}

#[test]
fn test_forwardable_regen() {
	// Tests that if we reload a ChannelManager while forwards are pending we will regenerate the
	// PendingHTLCsForwardable event automatically, ensuring we don't forget to forward/receive
	// HTLCs.
	// We test it for both payment receipt and payment forwarding.

	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_1_deserialized: 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>;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
	let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;

	// First send a payment to nodes[1]
	let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
	nodes[0].node.send_payment_with_route(&route, payment_hash,
		RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
	check_added_monitors!(nodes[0], 1);

	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let payment_event = SendEvent::from_event(events.pop().unwrap());
	nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
	commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);

	expect_pending_htlcs_forwardable_ignore!(nodes[1]);

	// Next send a payment which is forwarded by nodes[1]
	let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 200_000);
	nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
		RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
	check_added_monitors!(nodes[0], 1);

	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let payment_event = SendEvent::from_event(events.pop().unwrap());
	nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
	commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);

	// There is already a PendingHTLCsForwardable event "pending" so another one will not be
	// generated
	assert!(nodes[1].node.get_and_clear_pending_events().is_empty());

	// Now restart nodes[1] and make sure it regenerates a single PendingHTLCsForwardable
	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());

	let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
	let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
	reload_node!(nodes[1], nodes[1].node.encode(), &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
	// Note that nodes[1] and nodes[2] resend their channel_ready here since they haven't updated
	// the commitment state.
	reconnect_nodes(&nodes[1], &nodes[2], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));

	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	expect_pending_htlcs_forwardable!(nodes[1]);
	expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 100_000);
	check_added_monitors!(nodes[1], 1);

	let mut events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let payment_event = SendEvent::from_event(events.pop().unwrap());
	nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
	commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false);
	expect_pending_htlcs_forwardable!(nodes[2]);
	expect_payment_claimable!(nodes[2], payment_hash_2, payment_secret_2, 200_000);

	claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
	claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_2);
}

fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
	// Test what happens if a node receives an MPP payment, claims it, but crashes before
	// persisting the ChannelManager. If `persist_both_monitors` is false, also crash after only
	// updating one of the two channels' ChannelMonitors. As a result, on startup, we'll (a) still
	// have the PaymentClaimable event, (b) have one (or two) channel(s) that goes on chain with the
	// HTLC preimage in them, and (c) optionally have one channel that is live off-chain but does
	// not have the preimage tied to the still-pending HTLC.
	//
	// To get to the correct state, on startup we should propagate the preimage to the
	// still-off-chain channel, claiming the HTLC as soon as the peer connects, with the monitor
	// receiving the preimage without a state update.
	//
	// Further, we should generate a `PaymentClaimed` event to inform the user that the payment was
	// definitely claimed.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);

	let persister: test_utils::TestPersister;
	let new_chain_monitor: test_utils::TestChainMonitor;
	let nodes_3_deserialized: 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>;

	let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);

	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
	let chan_id_persisted = create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0).2;
	let chan_id_not_persisted = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0).2;

	// Create an MPP route for 15k sats, more than the default htlc-max of 10%
	let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
	assert_eq!(route.paths.len(), 2);
	route.paths.sort_by(|path_a, _| {
		// Sort the path so that the path through nodes[1] comes first
		if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
			core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
	});

	nodes[0].node.send_payment_with_route(&route, payment_hash,
		RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
	check_added_monitors!(nodes[0], 2);

	// Send the payment through to nodes[3] *without* clearing the PaymentClaimable event
	let mut send_events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(send_events.len(), 2);
	let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut send_events);
	let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut send_events);
	do_pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, true, false, None);
	do_pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_2_msgs, true, false, None);

	// Now that we have an MPP payment pending, get the latest encoded copies of nodes[3]'s
	// monitors and ChannelManager, for use later, if we don't want to persist both monitors.
	let mut original_monitor = test_utils::TestVecWriter(Vec::new());
	if !persist_both_monitors {
		for outpoint in nodes[3].chain_monitor.chain_monitor.list_monitors() {
			if outpoint.to_channel_id() == chan_id_not_persisted {
				assert!(original_monitor.0.is_empty());
				nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut original_monitor).unwrap();
			}
		}
	}

	let original_manager = nodes[3].node.encode();

	expect_payment_claimable!(nodes[3], payment_hash, payment_secret, 15_000_000);

	nodes[3].node.claim_funds(payment_preimage);
	check_added_monitors!(nodes[3], 2);
	expect_payment_claimed!(nodes[3], payment_hash, 15_000_000);

	// Now fetch one of the two updated ChannelMonitors from nodes[3], and restart pretending we
	// crashed in between the two persistence calls - using one old ChannelMonitor and one new one,
	// with the old ChannelManager.
	let mut updated_monitor = test_utils::TestVecWriter(Vec::new());
	for outpoint in nodes[3].chain_monitor.chain_monitor.list_monitors() {
		if outpoint.to_channel_id() == chan_id_persisted {
			assert!(updated_monitor.0.is_empty());
			nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut updated_monitor).unwrap();
		}
	}
	// If `persist_both_monitors` is set, get the second monitor here as well
	if persist_both_monitors {
		for outpoint in nodes[3].chain_monitor.chain_monitor.list_monitors() {
			if outpoint.to_channel_id() == chan_id_not_persisted {
				assert!(original_monitor.0.is_empty());
				nodes[3].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut original_monitor).unwrap();
			}
		}
	}

	// Now restart nodes[3].
	reload_node!(nodes[3], original_manager, &[&updated_monitor.0, &original_monitor.0], persister, new_chain_monitor, nodes_3_deserialized);

	// On startup the preimage should have been copied into the non-persisted monitor:
	assert!(get_monitor!(nodes[3], chan_id_persisted).get_stored_preimages().contains_key(&payment_hash));
	assert!(get_monitor!(nodes[3], chan_id_not_persisted).get_stored_preimages().contains_key(&payment_hash));

	nodes[1].node.peer_disconnected(&nodes[3].node.get_our_node_id());
	nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id());

	// During deserialization, we should have closed one channel and broadcast its latest
	// commitment transaction. We should also still have the original PaymentClaimable event we
	// never finished processing.
	let events = nodes[3].node.get_and_clear_pending_events();
	assert_eq!(events.len(), if persist_both_monitors { 4 } else { 3 });
	if let Event::PaymentClaimable { amount_msat: 15_000_000, .. } = events[0] { } else { panic!(); }
	if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[1] { } else { panic!(); }
	if persist_both_monitors {
		if let Event::ChannelClosed { reason: ClosureReason::OutdatedChannelManager, .. } = events[2] { } else { panic!(); }
		check_added_monitors(&nodes[3], 2);
	} else {
		check_added_monitors(&nodes[3], 1);
	}

	// On restart, we should also get a duplicate PaymentClaimed event as we persisted the
	// ChannelManager prior to handling the original one.
	if let Event::PaymentClaimed { payment_hash: our_payment_hash, amount_msat: 15_000_000, .. } =
		events[if persist_both_monitors { 3 } else { 2 }]
	{
		assert_eq!(payment_hash, our_payment_hash);
	} else { panic!(); }

	assert_eq!(nodes[3].node.list_channels().len(), if persist_both_monitors { 0 } else { 1 });
	if !persist_both_monitors {
		// If one of the two channels is still live, reveal the payment preimage over it.

		nodes[3].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init {
			features: nodes[2].node.init_features(), networks: None, remote_network_address: None
		}, true).unwrap();
		let reestablish_1 = get_chan_reestablish_msgs!(nodes[3], nodes[2]);
		nodes[2].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init {
			features: nodes[3].node.init_features(), networks: None, remote_network_address: None
		}, false).unwrap();
		let reestablish_2 = get_chan_reestablish_msgs!(nodes[2], nodes[3]);

		nodes[2].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish_1[0]);
		get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
		assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());

		nodes[3].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &reestablish_2[0]);

		// Once we call `get_and_clear_pending_msg_events` the holding cell is cleared and the HTLC
		// claim should fly.
		let ds_msgs = nodes[3].node.get_and_clear_pending_msg_events();
		check_added_monitors!(nodes[3], 1);
		assert_eq!(ds_msgs.len(), 2);
		if let MessageSendEvent::SendChannelUpdate { .. } = ds_msgs[0] {} else { panic!(); }

		let cs_updates = match ds_msgs[1] {
			MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
				nodes[2].node.handle_update_fulfill_htlc(&nodes[3].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
				check_added_monitors!(nodes[2], 1);
				let cs_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
				expect_payment_forwarded!(nodes[2], nodes[0], nodes[3], Some(1000), false, false);
				commitment_signed_dance!(nodes[2], nodes[3], updates.commitment_signed, false, true);
				cs_updates
			}
			_ => panic!(),
		};

		nodes[0].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
		commitment_signed_dance!(nodes[0], nodes[2], cs_updates.commitment_signed, false, true);
		expect_payment_sent!(nodes[0], payment_preimage);
	}
}

#[test]
fn test_partial_claim_before_restart() {
	do_test_partial_claim_before_restart(false);
	do_test_partial_claim_before_restart(true);
}

fn do_forwarded_payment_no_manager_persistence(use_cs_commitment: bool, claim_htlc: bool, use_intercept: bool) {
	if !use_cs_commitment { assert!(!claim_htlc); }
	// If we go to forward a payment, and the ChannelMonitor persistence completes, but the
	// ChannelManager does not, we shouldn't try to forward the payment again, nor should we fail
	// it back until the ChannelMonitor decides the fate of the HTLC.
	// This was never an issue, but it may be easy to regress here going forward.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let mut intercept_forwards_config = test_default_channel_config();
	intercept_forwards_config.accept_intercept_htlcs = true;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);

	let persister;
	let new_chain_monitor;
	let nodes_1_deserialized;

	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
	let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;

	let intercept_scid = nodes[1].node.get_intercept_scid();

	let (mut route, payment_hash, payment_preimage, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
	if use_intercept {
		route.paths[0].hops[1].short_channel_id = intercept_scid;
	}
	let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
	let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV;
	nodes[0].node.send_payment_with_route(&route, payment_hash,
		RecipientOnionFields::secret_only(payment_secret), payment_id).unwrap();
	check_added_monitors!(nodes[0], 1);

	let payment_event = SendEvent::from_node(&nodes[0]);
	nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
	commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);

	// Store the `ChannelManager` before handling the `PendingHTLCsForwardable`/`HTLCIntercepted`
	// events, expecting either event (and the HTLC itself) to be missing on reload even though its
	// present when we serialized.
	let node_encoded = nodes[1].node.encode();

	let mut intercept_id = None;
	let mut expected_outbound_amount_msat = None;
	if use_intercept {
		let events = nodes[1].node.get_and_clear_pending_events();
		assert_eq!(events.len(), 1);
		match events[0] {
			Event::HTLCIntercepted { intercept_id: ev_id, expected_outbound_amount_msat: ev_amt, .. } => {
				intercept_id = Some(ev_id);
				expected_outbound_amount_msat = Some(ev_amt);
			},
			_ => panic!()
		}
		nodes[1].node.forward_intercepted_htlc(intercept_id.unwrap(), &chan_id_2,
			nodes[2].node.get_our_node_id(), expected_outbound_amount_msat.unwrap()).unwrap();
	}

	expect_pending_htlcs_forwardable!(nodes[1]);

	let payment_event = SendEvent::from_node(&nodes[1]);
	nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
	nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
	check_added_monitors!(nodes[2], 1);

	if claim_htlc {
		get_monitor!(nodes[2], chan_id_2).provide_payment_preimage(&payment_hash, &payment_preimage,
			&nodes[2].tx_broadcaster, &LowerBoundedFeeEstimator(nodes[2].fee_estimator), &nodes[2].logger);
	}
	assert!(nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());

	let _ = nodes[2].node.get_and_clear_pending_msg_events();

	nodes[2].node.force_close_broadcasting_latest_txn(&chan_id_2, &nodes[1].node.get_our_node_id()).unwrap();
	let cs_commitment_tx = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
	assert_eq!(cs_commitment_tx.len(), if claim_htlc { 2 } else { 1 });

	check_added_monitors!(nodes[2], 1);
	check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed);
	check_closed_broadcast!(nodes[2], true);

	let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
	let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
	reload_node!(nodes[1], node_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	// Note that this checks that this is the only event on nodes[1], implying the
	// `HTLCIntercepted` event has been removed in the `use_intercept` case.
	check_closed_event!(nodes[1], 1, ClosureReason::OutdatedChannelManager);

	if use_intercept {
		// Attempt to forward the HTLC back out over nodes[1]' still-open channel, ensuring we get
		// a intercept-doesn't-exist error.
		let forward_err = nodes[1].node.forward_intercepted_htlc(intercept_id.unwrap(), &chan_id_1,
			nodes[0].node.get_our_node_id(), expected_outbound_amount_msat.unwrap()).unwrap_err();
		assert_eq!(forward_err, APIError::APIMisuseError {
			err: format!("Payment with intercept id {} not found", log_bytes!(intercept_id.unwrap().0))
		});
	}

	nodes[1].node.timer_tick_occurred();
	let bs_commitment_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
	assert_eq!(bs_commitment_tx.len(), 1);
	check_added_monitors!(nodes[1], 1);

	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));

	if use_cs_commitment {
		// If we confirm a commitment transaction that has the HTLC on-chain, nodes[1] should wait
		// for an HTLC-spending transaction before it does anything with the HTLC upstream.
		confirm_transaction(&nodes[1], &cs_commitment_tx[0]);
		assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
		assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

		if claim_htlc {
			confirm_transaction(&nodes[1], &cs_commitment_tx[1]);
		} else {
			connect_blocks(&nodes[1], htlc_expiry - nodes[1].best_block_info().1 + 1);
			let bs_htlc_timeout_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
			assert_eq!(bs_htlc_timeout_tx.len(), 1);
			confirm_transaction(&nodes[1], &bs_htlc_timeout_tx[0]);
		}
	} else {
		confirm_transaction(&nodes[1], &bs_commitment_tx[0]);
	}

	if !claim_htlc {
		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 }]);
	} else {
		expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, true);
	}
	check_added_monitors!(nodes[1], 1);

	let events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	match &events[0] {
		MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fulfill_htlcs, update_fail_htlcs, commitment_signed, .. }, .. } => {
			if claim_htlc {
				nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
			} else {
				nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
			}
			commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
		},
		_ => panic!("Unexpected event"),
	}

	if claim_htlc {
		expect_payment_sent!(nodes[0], payment_preimage);
	} else {
		expect_payment_failed!(nodes[0], payment_hash, false);
	}
}

#[test]
fn forwarded_payment_no_manager_persistence() {
	do_forwarded_payment_no_manager_persistence(true, true, false);
	do_forwarded_payment_no_manager_persistence(true, false, false);
	do_forwarded_payment_no_manager_persistence(false, false, false);
}

#[test]
fn intercepted_payment_no_manager_persistence() {
	do_forwarded_payment_no_manager_persistence(true, true, true);
	do_forwarded_payment_no_manager_persistence(true, false, true);
	do_forwarded_payment_no_manager_persistence(false, false, true);
}

#[test]
fn removed_payment_no_manager_persistence() {
	// If an HTLC is failed to us on a channel, and the ChannelMonitor persistence completes, but
	// the corresponding ChannelManager persistence does not, we need to ensure that the HTLC is
	// still failed back to the previous hop even though the ChannelMonitor now no longer is aware
	// of the HTLC. This was previously broken as no attempt was made to figure out which HTLCs
	// were left dangling when a channel was force-closed due to a stale ChannelManager.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);

	let persister;
	let new_chain_monitor;
	let nodes_1_deserialized;

	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let chan_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1).2;
	let chan_id_2 = create_announced_chan_between_nodes(&nodes, 1, 2).2;

	let (_, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);

	let node_encoded = nodes[1].node.encode();

	nodes[2].node.fail_htlc_backwards(&payment_hash);
	expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], [HTLCDestination::FailedPayment { payment_hash }]);
	check_added_monitors!(nodes[2], 1);
	let events = nodes[2].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	match &events[0] {
		MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. }, .. } => {
			nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &update_fail_htlcs[0]);
			commitment_signed_dance!(nodes[1], nodes[2], commitment_signed, false);
		},
		_ => panic!("Unexpected event"),
	}

	let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode();
	let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode();
	reload_node!(nodes[1], node_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	match nodes[1].node.pop_pending_event().unwrap() {
		Event::ChannelClosed { ref reason, .. } => {
			assert_eq!(*reason, ClosureReason::OutdatedChannelManager);
		},
		_ => panic!("Unexpected event"),
	}

	nodes[1].node.test_process_background_events();
	check_added_monitors(&nodes[1], 1);

	// Now that the ChannelManager has force-closed the channel which had the HTLC removed, it is
	// now forgotten everywhere. The ChannelManager should have, as a side-effect of reload,
	// learned that the HTLC is gone from the ChannelMonitor and added it to the to-fail-back set.
	nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
	reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));

	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 }]);
	check_added_monitors!(nodes[1], 1);
	let events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	match &events[0] {
		MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. }, .. } => {
			nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
			commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
		},
		_ => panic!("Unexpected event"),
	}

	expect_payment_failed!(nodes[0], payment_hash, false);
}