lightning 0.3.0-beta1

A Complete Bitcoin Lightning Library in Rust. Handles the core functionality of the Lightning Network, allowing clients to implement custom wallet, chain interactions, storage and network logic without enforcing a specific runtime.
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
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
#![cfg_attr(rustfmt, rustfmt_skip)]

// 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::{BlockLocator, ChannelMonitorUpdateStatus, Watch};
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateStep};
use crate::routing::router::{PaymentParameters, RouteParameters};
use crate::sign::EntropySource;
use crate::chain::transaction::OutPoint;
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType};
use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAACommitmentOrder};
use crate::ln::outbound_payment::RecipientOnionFields;
use crate::ln::msgs;
use crate::ln::types::ChannelId;
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, RoutingMessageHandler, ErrorAction, MessageSendEvent};
use crate::util::test_channel_signer::TestChannelSigner;
use crate::util::test_utils;
use crate::util::errors::APIError;
use crate::util::ser::{Writeable, ReadableArgs};
use crate::util::config::{HTLCInterceptionFlags, UserConfig};

use bitcoin::hashes::Hash;
use types::payment::{PaymentHash, PaymentPreimage};

use crate::prelude::*;

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 persister;
	let new_chain_monitor;

	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_0_deserialized;
	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());

	let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
	reconnect_args.send_channel_ready.1 = true;
	reconnect_nodes(reconnect_args);

	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
	let node_1_pubkey = nodes[1].node.get_our_node_id();
	nodes[0].gossip_sync.handle_channel_announcement(Some(node_1_pubkey), &chan_announcement).unwrap();
	nodes[0].gossip_sync.handle_channel_update(Some(node_1_pubkey), &bs_update).unwrap();
	nodes[0].gossip_sync.handle_channel_update(Some(node_1_pubkey), &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(ReconnectArgs::new(&nodes[0], &nodes[1]));
}

#[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 persister;
	let new_chain_monitor;

	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_0_deserialized;
	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], ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.compute_txid(), index: 0 })).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 (i, node) in nodes.iter().enumerate() {
		let counterparty_node_id = nodes[(i + 1) % 2].node.get_our_node_id();
		assert!(node.gossip_sync.handle_channel_announcement(Some(counterparty_node_id), &announcement).unwrap());
		node.gossip_sync.handle_channel_update(Some(counterparty_node_id), &as_update).unwrap();
		node.gossip_sync.handle_channel_update(Some(counterparty_node_id), &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 persister;
	let new_chain_monitor;

	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_0_deserialized;
	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, None).unwrap();
	handle_and_accept_open_channel(&node_b, 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);

	let funding_created = get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id());
	let channel_id = ChannelId::v1_from_funding_txid(
		funding_created.funding_txid.as_byte_array(), funding_created.funding_output_index
	);

	node_b.node.handle_funding_created(node_a.node.get_our_node_id(), &funding_created);
	{
		let mut added_monitors = node_b.chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, channel_id);
		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, channel_id);
		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].compute_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 (i, node) in nodes.iter().enumerate() {
		let counterparty_node_id = nodes[(i + 1) % 2].node.get_our_node_id();
		assert!(node.gossip_sync.handle_channel_announcement(Some(counterparty_node_id), &announcement).unwrap());
		node.gossip_sync.handle_channel_update(Some(counterparty_node_id), &as_update).unwrap();
		node.gossip_sync.handle_channel_update(Some(counterparty_node_id), &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 persister;
	let new_chain_monitor;

	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_0_deserialized;
	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(ReconnectArgs::new(&nodes[0], &nodes[1]));

	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 logger;
	let fee_estimator;
	let persister;
	let new_chain_monitor;

	let legacy_cfg = test_legacy_channel_config();
	let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg.clone()), Some(legacy_cfg.clone()), Some(legacy_cfg)]);
	let nodes_0_deserialized;
	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::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) = <(BlockLocator, ChannelMonitor<TestChannelSigner>)>::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) = <(BlockLocator, ChannelMonitor<TestChannelSigner>)>::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::DangerousValue) =
		<(BlockLocator, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestMessageRouter, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
		config: UserConfig::default(),
		entropy_source: keys_manager,
		node_signer: keys_manager,
		signer_provider: keys_manager,
		fee_estimator: &fee_estimator,
		router: &nodes[0].router,
		message_router: &nodes[0].message_router,
		chain_monitor: nodes[0].chain_monitor,
		tx_broadcaster: nodes[0].tx_broadcaster,
		logger: &logger,
		channel_monitors: node_0_stale_monitors.iter().map(|monitor| { (monitor.channel_id(), monitor) }).collect(),
		reconstruct_manager_from_monitors: None,
	}) { } 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) =
		<(BlockLocator, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestMessageRouter, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
		config: UserConfig::default(),
		entropy_source: keys_manager,
		node_signer: keys_manager,
		signer_provider: keys_manager,
		fee_estimator: &fee_estimator,
		router: nodes[0].router,
		message_router: &nodes[0].message_router,
		chain_monitor: nodes[0].chain_monitor,
		tx_broadcaster: nodes[0].tx_broadcaster,
		logger: &logger,
		channel_monitors: node_0_monitors.iter().map(|monitor| { (monitor.channel_id(), monitor) }).collect(),
		reconstruct_manager_from_monitors: None,
	}).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.channel_id(), monitor),
			Ok(ChannelMonitorUpdateStatus::Completed));
	check_added_monitors(&nodes[0], 1);
	}
	nodes[0].node = &nodes_0_deserialized;

	check_closed_event(&nodes[0], 1, ClosureReason::OutdatedChannelManager, &[nodes[3].node.get_our_node_id()], 100000);
	{ // 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.compute_txid());
	}
	check_added_monitors(&nodes[0], 1);

	// nodes[1] and nodes[2] have no lost state with nodes[0]...
	reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
	reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[2]));
	//... 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);
}

#[cfg(feature = "std")]
fn do_test_data_loss_protect(reconnect_panicing: bool, substantially_old: bool, not_stale: bool) {
	use crate::ln::outbound_payment::Retry;
	use crate::types::string::UntrustedString;
	// 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 node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;

	let legacy_cfg = test_legacy_channel_config();
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg)]);
	let nodes_0_deserialized;

	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();

	assert!(!substantially_old || !not_stale, "substantially_old and not_stale doesn't make sense");
	if not_stale || !substantially_old {
		// Previously, we'd only hit the data_loss_protect assertion if we had a state which
		// revoked at least two revocations ago, not the latest revocation. Here, we use
		// `not_stale` to test the boundary condition.
		let pay_params = PaymentParameters::for_keysend(nodes[1].node.get_our_node_id(), 100, false);
		let route_params = RouteParameters::from_payment_params_and_value(pay_params, 40000);
		nodes[0].node.send_spontaneous_payment(None, RecipientOnionFields::spontaneous_empty(40000), PaymentId([0; 32]), route_params, Retry::Attempts(0)).unwrap();
		check_added_monitors(&nodes[0], 1);
		let update_add_commit = SendEvent::from_node(&nodes[0]);

		nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &update_add_commit.msgs[0]);
		nodes[1].node.handle_commitment_signed_batch_test(nodes[0].node.get_our_node_id(), &update_add_commit.commitment_msg);
		check_added_monitors(&nodes[1], 1);
		let (raa, cs) = get_revoke_commit_msgs(&nodes[1], &nodes[0].node.get_our_node_id());

		nodes[0].node.handle_revoke_and_ack(nodes[1].node.get_our_node_id(), &raa);
		check_added_monitors(&nodes[0], 1);
		assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
		if !not_stale {
			nodes[0].node.handle_commitment_signed_batch_test(nodes[1].node.get_our_node_id(), &cs);
			check_added_monitors(&nodes[0], 1);
			// A now revokes their original state, at which point reconnect should panic
			let raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
			nodes[1].node.handle_revoke_and_ack(nodes[0].node.get_our_node_id(), &raa);
			check_added_monitors(&nodes[1], 1);
			expect_htlc_failure_conditions(nodes[1].node.get_and_clear_pending_events(), &[]);
		}
	} else {
		send_payment(&nodes[0], &[&nodes[1]], 8000000);
		send_payment(&nodes[0], &[&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]);

		// If A has fallen behind substantially, B should send it a message letting it know
		// that.
		nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &reestablish_1[0]);
		let reestablish_msg;
		if substantially_old {
			let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction: 0 (received) vs 4 (expected)".to_owned();

			let warn_reestablish = nodes[1].node.get_and_clear_pending_msg_events();
			assert_eq!(warn_reestablish.len(), 2);
			match warn_reestablish[1] {
				MessageSendEvent::HandleError { action: ErrorAction::SendWarningMessage { ref msg, .. }, .. } => {
					assert_eq!(msg.data, warn_msg);
				},
				_ => panic!("Unexpected events: {:?}", warn_reestablish),
			}
			reestablish_msg = match &warn_reestablish[0] {
				MessageSendEvent::SendChannelReestablish { msg, .. } => msg.clone(),
				_ => panic!("Unexpected events: {:?}", warn_reestablish),
			};
		} else {
			let msgs = nodes[1].node.get_and_clear_pending_msg_events();
			assert!(msgs.len() >= 4);
			match msgs.last() {
				Some(MessageSendEvent::SendChannelUpdate { .. }) => {},
				_ => panic!("Unexpected events: {:?}", msgs),
			}
			assert!(msgs.iter().any(|msg| matches!(msg, MessageSendEvent::SendRevokeAndACK { .. })));
			assert!(msgs.iter().any(|msg| matches!(msg, MessageSendEvent::UpdateHTLCs { .. })));
			reestablish_msg = match &msgs[0] {
				MessageSendEvent::SendChannelReestablish { msg, .. } => msg.clone(),
				_ => panic!("Unexpected events: {:?}", msgs),
			};
		}

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

		// Check A panics upon seeing proof it has fallen behind.
		let reconnect_res = std::panic::catch_unwind(|| {
			nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &reestablish_msg);
		});
		if not_stale {
			assert!(reconnect_res.is_ok());
			// At this point A gets confused because B expects a commitment state newer than A
			// has sent, but not a newer revocation secret, so A just (correctly) closes.
			check_closed_broadcast(&nodes[0], 1, true);
			check_added_monitors(&nodes[0], 1);
			check_closed_event(&nodes[0], 1, ClosureReason::ProcessingError {
				err: "Peer attempted to reestablish channel with a future remote commitment transaction: 2 (received) vs 1 (expected)".to_owned()
			}, &[nodes[1].node.get_our_node_id()], 1000000);
		} else {
			assert!(reconnect_res.is_err());
			// Skip the `Drop` handler for `Node`s as some may be in an invalid (panicked) state.
			std::mem::forget(nodes);
		}
	} else {
		let message = "Channel force-closed".to_owned();
		assert!(!not_stale, "We only care about the stale case when not testing panicking");

		nodes[0]
			.node
			.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id(), message.clone())
			.unwrap();
		check_added_monitors(&nodes[0], 1);
		let reason =
			ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
		check_closed_event(&nodes[0], 1, reason, &[nodes[1].node.get_our_node_id()], 1000000);
		{
			let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
			assert_eq!(node_txn.len(), 1);
		}

		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);
		if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[1] {
			match action {
				&ErrorAction::SendErrorMessage { ref msg } => {
					let peer_msg = format!(
						"Got a message for a channel from the wrong node! No such channel_id {} for the passed counterparty_node_id {}",
						chan.2, nodes[1].node.get_our_node_id()
					);
					assert_eq!(msg.data, peer_msg);
					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);
		let peer_msg = format!(
			"Got a message for a channel from the wrong node! No such channel_id {} for the passed counterparty_node_id {}",
			chan.2, nodes[1].node.get_our_node_id()
		);
		let reason = ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(peer_msg) };
		check_closed_event(&nodes[1], 1, reason, &[nodes[0].node.get_our_node_id()], 1000000);
		check_closed_broadcast(&nodes[1], 1, false);
	}
}

#[test]
#[cfg(feature = "std")]
fn test_data_loss_protect() {
	do_test_data_loss_protect(true, false, true);
	do_test_data_loss_protect(true, true, false);
	do_test_data_loss_protect(true, false, false);
	do_test_data_loss_protect(false, true, false);
	do_test_data_loss_protect(false, false, false);
}

fn do_test_partial_claim_before_restart(persist_both_monitors: bool, double_restart: 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 (persist_d_1, persist_d_2);
	let (chain_d_1, chain_d_2);

	let mut config = test_default_channel_config();
	// Set the percentage to the default value at the time this test was written
	config.channel_handshake_config.announced_channel_max_inbound_htlc_value_in_flight_percentage = 10;
	let configs: [Option<UserConfig>; 4] = core::array::from_fn(|_| Some(config.clone()));
	let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &configs);
	let (node_d_1, node_d_2);

	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, 15_000_000), 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(PassAlongPathArgs::new(&nodes[0],&[&nodes[1], &nodes[3]], 15_000_000, payment_hash, node_1_msgs)
		.with_payment_secret(payment_secret)
		.without_clearing_recipient_events());
	do_pass_along_path(PassAlongPathArgs::new(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, node_2_msgs)
		.with_payment_secret(payment_secret)
		.without_clearing_recipient_events());

	// 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 channel_id in nodes[3].chain_monitor.chain_monitor.list_monitors() {
			if channel_id == chan_id_not_persisted {
				assert!(original_monitor.0.is_empty());
				nodes[3].chain_monitor.chain_monitor.get_monitor(channel_id).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 channel_id in nodes[3].chain_monitor.chain_monitor.list_monitors() {
		if channel_id == chan_id_persisted {
			assert!(updated_monitor.0.is_empty());
			nodes[3].chain_monitor.chain_monitor.get_monitor(channel_id).unwrap().write(&mut updated_monitor).unwrap();
		}
	}
	// If `persist_both_monitors` is set, get the second monitor here as well
	if persist_both_monitors {
		for channel_id in nodes[3].chain_monitor.chain_monitor.list_monitors() {
			if channel_id == chan_id_not_persisted {
				assert!(original_monitor.0.is_empty());
				nodes[3].chain_monitor.chain_monitor.get_monitor(channel_id).unwrap().write(&mut original_monitor).unwrap();
			}
		}
	}

	// Now restart nodes[3].
	reload_node!(nodes[3], original_manager.clone(), &[&updated_monitor.0, &original_monitor.0], persist_d_1, chain_d_1, node_d_1);
	nodes[3].disable_monitor_completeness_assertion();

	if double_restart {
		// Previously, we had a bug where we'd fail to reload if we re-persist the `ChannelManager`
		// without updating any `ChannelMonitor`s as we'd fail to double-initiate the claim replay.
		// We test that here ensuring that we can reload again.
		reload_node!(nodes[3], node_d_1.encode(), &[&updated_monitor.0, &original_monitor.0], persist_d_2, chain_d_2, node_d_2);
		nodes[3].disable_monitor_completeness_assertion();
	}

	// Until the startup background events are processed (in `get_and_clear_pending_events`,
	// below), the preimage is not copied to the non-persisted monitor...
	assert!(get_monitor!(nodes[3], chan_id_persisted).get_stored_preimages().contains_key(&payment_hash));
	assert_eq!(
		get_monitor!(nodes[3], chan_id_not_persisted).get_stored_preimages().contains_key(&payment_hash),
		persist_both_monitors,
	);

	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 as well as a PaymentClaimed event regenerated when we replayed the
	// preimage onto the non-persisted monitor.
	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!(); }
		if let Event::PaymentClaimed { amount_msat: 15_000_000, .. } = events[3] { } else { panic!(); }
		check_added_monitors(&nodes[3], 4);
	} else {
		if let Event::PaymentClaimed { amount_msat: 15_000_000, .. } = events[2] { } else { panic!(); }
		check_added_monitors(&nodes[3], 3);
	}

	// Now that we've processed background events, 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));

	// 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 mut 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 mut cs_updates = match ds_msgs.remove(1) {
			MessageSendEvent::UpdateHTLCs { mut updates, .. } => {
				let mut fulfill = updates.update_fulfill_htlcs.remove(0);
				nodes[2].node.handle_update_fulfill_htlc(nodes[3].node.get_our_node_id(), fulfill);
				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);
				do_commitment_signed_dance(&nodes[2], &nodes[3], &updates.commitment_signed, false, true);
				cs_updates
			}
			_ => panic!(),
		};

		let fulfill = cs_updates.update_fulfill_htlcs.remove(0);
		nodes[0].node.handle_update_fulfill_htlc(nodes[2].node.get_our_node_id(), fulfill);
		do_commitment_signed_dance(&nodes[0], &nodes[2], &cs_updates.commitment_signed, false, true);
		expect_payment_sent!(nodes[0], payment_preimage);

		// Ensure that the remaining channel is fully operation and not blocked (and that after a
		// cycle of commitment updates the payment preimage is ultimately pruned).
		nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
		send_payment(&nodes[0], &[&nodes[2], &nodes[3]], 100_000);
		assert!(!get_monitor!(nodes[3], chan_id_not_persisted).get_stored_preimages().contains_key(&payment_hash));
	}
}

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

#[test]
fn test_mpp_claim_htlc_fulfills_unblocked_on_reload() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_1_deserialized;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	// Open two independent channels between the same nodes. The payment below is large enough to
	// force the router to split it across both channels, which is what makes the MPP claim depend
	// on both ChannelMonitors durably learning the preimage.
	let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
	let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
	let chan_id_a = chan_a.2;
	let chan_id_b = chan_b.2;
	let scid_a = chan_a.0.contents.short_channel_id;
	let scid_b = chan_b.0.contents.short_channel_id;

	// Send an MPP payment to nodes[1]. `send_along_route_with_secret` leaves the payment
	// claimable but unclaimed, so nodes[1] still has both inbound HTLCs live when we start
	// manipulating monitor persistence below.
	let amt_msat = 50_000_000;
	let (route, payment_hash, payment_preimage, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[1], amt_msat);
	assert_eq!(route.paths.len(), 2);
	send_along_route_with_secret(
		&nodes[0], route, &[&[&nodes[1]], &[&nodes[1]]], amt_msat, payment_hash,
		payment_secret,
	);

	// Move both channels into `AWAITING_REMOTE_REVOKE` by having nodes[0] send fee updates and
	// withholding nodes[1]'s responding `commitment_signed`s. When nodes[1] later claims the
	// payment, the fulfill updates cannot be sent immediately and instead sit in each channel's
	// holding cell.
	{
		let mut fee_est = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
		*fee_est *= 2;
	}
	nodes[0].node.timer_tick_occurred();
	check_added_monitors(&nodes[0], 2);

	let node_0_id = nodes[0].node.get_our_node_id();
	let node_1_id = nodes[1].node.get_our_node_id();

	let fee_msgs = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(fee_msgs.len(), 2);
	for ev in &fee_msgs {
		match ev {
			MessageSendEvent::UpdateHTLCs { updates, .. } => {
				nodes[1].node.handle_update_fee(node_0_id, updates.update_fee.as_ref().unwrap());
				nodes[1].node.handle_commitment_signed_batch_test(
					node_0_id, &updates.commitment_signed,
				);
				check_added_monitors(&nodes[1], 1);
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}

	// nodes[1] responds to each fee update with a `revoke_and_ack` and a new
	// `commitment_signed`. Deliver only the `revoke_and_ack`s for now. The held
	// `commitment_signed`s are delivered after nodes[1] claims the payment, creating the blocked
	// post-claim monitor updates whose release is exercised after reload.
	let node_1_msgs = nodes[1].node.get_and_clear_pending_msg_events();
	let mut commitment_signed_msgs = Vec::new();
	for ev in &node_1_msgs {
		match ev {
			MessageSendEvent::SendRevokeAndACK { msg, .. } => {
				nodes[0].node.handle_revoke_and_ack(node_1_id, msg);
				check_added_monitors(&nodes[0], 1);
			},
			MessageSendEvent::UpdateHTLCs { updates, .. } => {
				commitment_signed_msgs.push(updates.commitment_signed.clone());
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}

	let node_0_msgs = nodes[0].node.get_and_clear_pending_msg_events();
	for ev in &node_0_msgs {
		match ev {
			MessageSendEvent::SendRevokeAndACK { msg, .. } => {
				nodes[1].node.handle_revoke_and_ack(node_0_id, msg);
				check_added_monitors(&nodes[1], 1);
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}

	// Snapshot channel B before the claim. The in-memory ChainMonitor applies updates even when
	// the persister returns `InProgress`, so taking this snapshot after the claim would not model a
	// crash between two separate monitor writes.
	let mon_b_serialized = get_monitor!(nodes[1], chan_id_b).encode();

	// Make both preimage monitor writes asynchronous. `claim_funds` attaches an in-memory MPP RAA
	// blocker so neither channel can release later monitor updates until all channels have the
	// preimage durably persisted.
	chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	nodes[1].node.claim_funds(payment_preimage);
	check_added_monitors(&nodes[1], 2);

	// Complete only channel A's preimage update. Channel B will be reloaded from the stale snapshot
	// above, simulating a crash where one monitor write completed and the other did not.
	let (update_id_a, _) = get_latest_mon_update_id(&nodes[1], chan_id_a);
	nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(chan_id_a, update_id_a);

	// Now finish the fee-update commitment dance we held back. nodes[1] receives nodes[0]'s
	// `revoke_and_ack`s while the MPP RAA blocker is still in place, so the resulting monitor
	// updates are blocked behind state that is not serialized in the ChannelManager.
	for commitment_signed in &commitment_signed_msgs {
		nodes[0].node.handle_commitment_signed_batch_test(node_1_id, commitment_signed);
		check_added_monitors(&nodes[0], 1);
	}
	let node_0_msgs = nodes[0].node.get_and_clear_pending_msg_events();
	for ev in &node_0_msgs {
		match ev {
			MessageSendEvent::SendRevokeAndACK { msg, .. } => {
				nodes[1].node.handle_revoke_and_ack(node_0_id, msg);
				check_added_monitors(&nodes[1], 0);
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}

	// Persist the ChannelManager after the blocked post-claim monitor updates have been recorded.
	// Reload with channel A's up-to-date monitor and channel B's stale monitor. The preimage update
	// for B is replayed during reload, putting both channels' preimages on disk. The remaining state
	// under test is the blocked post-claim `revoke_and_ack` monitor updates after the in-memory MPP
	// RAA blocker that created them is gone.
	let node_1_serialized = nodes[1].node.encode();
	let mon_a_serialized = get_monitor!(nodes[1], chan_id_a).encode();

	nodes[0].node.peer_disconnected(node_1_id);
	reload_node!(
		nodes[1],
		node_1_serialized,
		&[&mon_a_serialized, &mon_b_serialized],
		persister,
		new_chain_monitor,
		nodes_1_deserialized
	);

	// Reconnect both peers by manually exchanging `channel_reestablish`s. This avoids relying on a
	// more general reconnect helper while the channels intentionally have asymmetric monitor state.
	let node_1_id = nodes[1].node.get_our_node_id();
	nodes[0].node.peer_connected(node_1_id, &msgs::Init {
		features: nodes[1].node.init_features(), networks: None, remote_network_address: None,
	}, true).unwrap();
	nodes[1].node.peer_connected(node_0_id, &msgs::Init {
		features: nodes[0].node.init_features(), networks: None, remote_network_address: None,
	}, false).unwrap();

	let reestablish_0 = nodes[0].node.get_and_clear_pending_msg_events();
	let reestablish_1 = nodes[1].node.get_and_clear_pending_msg_events();
	let mut reestablish_0_chan_ids = Vec::new();
	let mut reestablish_1_chan_ids = Vec::new();
	for ev in &reestablish_1 {
		match ev {
			MessageSendEvent::SendChannelReestablish { node_id, msg } => {
				assert_eq!(*node_id, node_0_id);
				reestablish_1_chan_ids.push(msg.channel_id);
				nodes[0].node.handle_channel_reestablish(node_1_id, msg);
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}
	for ev in &reestablish_0 {
		match ev {
			MessageSendEvent::SendChannelReestablish { node_id, msg } => {
				assert_eq!(*node_id, node_1_id);
				reestablish_0_chan_ids.push(msg.channel_id);
				nodes[1].node.handle_channel_reestablish(node_0_id, msg);
			},
			_ => panic!("Unexpected message: {:?}", ev),
		}
	}
	assert_eq!(reestablish_0_chan_ids.len(), 2);
	assert!(reestablish_0_chan_ids.contains(&chan_id_a));
	assert!(reestablish_0_chan_ids.contains(&chan_id_b));
	assert_eq!(reestablish_1_chan_ids.len(), 2);
	assert!(reestablish_1_chan_ids.contains(&chan_id_a));
	assert!(reestablish_1_chan_ids.contains(&chan_id_b));
	// Only nodes[1] was reloaded with stale monitor state. nodes[0] responds to the
	// `channel_reestablish`s without touching its monitors. nodes[1] applies the replayed channel B
	// preimage update, releases channel A's held RAA update, and frees channel A's held fulfill
	// during startup processing.
	check_added_monitors(&nodes[0], 0);
	check_added_monitors(&nodes[1], 3);

	// The first message batch after reconnect contains channel updates from both nodes. nodes[1]
	// also sends the channel A fulfill that startup processing released from the holding cell.
	let restart_msgs_0 = nodes[0].node.get_and_clear_pending_msg_events();
	let restart_msgs_1 = nodes[1].node.get_and_clear_pending_msg_events();
	let mut restart_scids_0 = Vec::new();
	let mut restart_scids_1 = Vec::new();
	let mut startup_fulfill_chan_ids = Vec::new();
	for ev in &restart_msgs_0 {
		match ev {
			MessageSendEvent::SendChannelUpdate { node_id, msg } => {
				assert_eq!(*node_id, node_1_id);
				restart_scids_0.push(msg.contents.short_channel_id);
			},
			_ => panic!("Unexpected restart message from node 0: {:?}", ev),
		}
	}
	for ev in &restart_msgs_1 {
		match ev {
			MessageSendEvent::SendChannelUpdate { node_id, msg } => {
				assert_eq!(*node_id, node_0_id);
				restart_scids_1.push(msg.contents.short_channel_id);
			},
			MessageSendEvent::UpdateHTLCs { node_id, channel_id, updates } => {
				assert_eq!(*node_id, node_0_id);
				startup_fulfill_chan_ids.push(*channel_id);
				assert_eq!(updates.update_fulfill_htlcs.len(), 1);
				assert!(updates.update_add_htlcs.is_empty());
				assert!(updates.update_fail_htlcs.is_empty());
				assert!(updates.update_fail_malformed_htlcs.is_empty());
				assert!(updates.update_fee.is_none());
				for fulfill in &updates.update_fulfill_htlcs {
					nodes[0].node.handle_update_fulfill_htlc(node_1_id, fulfill.clone());
				}
				// Complete the standard commitment handshake for the released fulfill. The helper
				// checks nodes[0]'s incoming commitment monitor update, nodes[1]'s response monitor
				// updates, and nodes[0]'s held final monitor update.
				do_commitment_signed_dance(
					&nodes[0], &nodes[1], &updates.commitment_signed, false, false,
				);
			},
			_ => panic!("Unexpected restart message from node 1: {:?}", ev),
		}
	}
	assert_eq!(restart_scids_0.len(), 2);
	assert!(restart_scids_0.contains(&scid_a));
	assert!(restart_scids_0.contains(&scid_b));
	assert_eq!(restart_scids_1.len(), 2);
	assert!(restart_scids_1.contains(&scid_a));
	assert!(restart_scids_1.contains(&scid_b));
	assert_eq!(startup_fulfill_chan_ids, vec![chan_id_a]);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	check_added_monitors(&nodes[0], 0);
	check_added_monitors(&nodes[1], 0);

	// Receiving the startup-released fulfill gives nodes[0] the payment preimage. That is enough to
	// emit `PaymentSent`, even though channel B's path-level success still needs its own fulfill.
	let startup_payment_events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(startup_payment_events.len(), 2);
	let mut saw_startup_payment_sent = false;
	let mut startup_success_scids = Vec::new();
	for ev in &startup_payment_events {
		match ev {
			Event::PaymentSent {
				payment_preimage: sent_preimage,
				payment_hash: sent_hash,
				amount_msat: sent_amount,
				fee_paid_msat,
				..
			} => {
				assert_eq!(*sent_preimage, payment_preimage);
				assert_eq!(*sent_hash, payment_hash);
				assert_eq!(*sent_amount, Some(amt_msat));
				assert_eq!(*fee_paid_msat, Some(0));
				saw_startup_payment_sent = true;
			},
			Event::PaymentPathSuccessful { payment_hash: Some(path_hash), path, .. } => {
				assert_eq!(*path_hash, payment_hash);
				assert_eq!(path.hops.len(), 1);
				startup_success_scids.push(path.hops[0].short_channel_id);
			},
			_ => panic!("Unexpected startup payment event: {:?}", ev),
		}
	}
	assert!(saw_startup_payment_sent);
	assert_eq!(startup_success_scids, vec![scid_a]);

	// Handling the claim event runs the event-completion action that releases the remaining
	// RAA-blocked monitor update. The startup unblock path already released channel A, so channel B
	// is the only fulfill that should be emitted here.
	let claim_events = nodes[1].node.get_and_clear_pending_events();
	assert_eq!(claim_events.len(), 1);
	match &claim_events[0] {
		Event::PaymentClaimed { payment_hash: claimed_hash, amount_msat, htlcs, .. } => {
			assert_eq!(*claimed_hash, payment_hash);
			assert_eq!(*amount_msat, amt_msat);
			assert_eq!(htlcs.len(), 2);
		},
		_ => panic!("Unexpected event: {:?}", claim_events[0]),
	}
	// The `PaymentSent` event above releases the monitor update that nodes[0] held after the final
	// channel A startup revocation.
	check_added_monitors(&nodes[0], 1);
	// Handling `PaymentClaimed` releases channel B's held revocation update and then the fulfill
	// that was waiting behind it.
	check_added_monitors(&nodes[1], 2);

	// Channel A's fulfill was already sent during startup. The `PaymentClaimed` completion action
	// now frees channel B's held fulfill, and no other HTLC update should be bundled with it.
	let fulfill_msgs = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(fulfill_msgs.len(), 1);
	match &fulfill_msgs[0] {
		MessageSendEvent::UpdateHTLCs { node_id, channel_id, updates } => {
			assert_eq!(*node_id, node_0_id);
			assert_eq!(*channel_id, chan_id_b);
			assert_eq!(updates.update_fulfill_htlcs.len(), 1);
			assert!(updates.update_add_htlcs.is_empty());
			assert!(updates.update_fail_htlcs.is_empty());
			assert!(updates.update_fail_malformed_htlcs.is_empty());
			assert!(updates.update_fee.is_none());
			for fulfill in &updates.update_fulfill_htlcs {
				nodes[0].node.handle_update_fulfill_htlc(node_1_id, fulfill.clone());
			}
			// Complete the same commitment handshake for channel B. Here nodes[0]'s final monitor
			// update is persisted immediately because `PaymentSent` already ran for channel A.
			do_commitment_signed_dance(
				&nodes[0], &nodes[1], &updates.commitment_signed, false, false,
			);
		},
		_ => panic!("Unexpected fulfill message: {:?}", fulfill_msgs[0]),
	}
	check_added_monitors(&nodes[1], 0);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	let final_payment_events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(final_payment_events.len(), 1);
	match &final_payment_events[0] {
		Event::PaymentPathSuccessful { payment_hash: Some(path_hash), path, .. } => {
			assert_eq!(*path_hash, payment_hash);
			assert_eq!(path.hops.len(), 1);
			assert_eq!(path.hops[0].short_channel_id, scid_b);
		},
		_ => panic!("Unexpected final payment event: {:?}", final_payment_events[0]),
	}
	check_added_monitors(&nodes[0], 0);
	assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
	check_added_monitors(&nodes[0], 0);
	check_added_monitors(&nodes[1], 0);

	// Both MPP parts should have been fulfilled back to nodes[0]. If either channel still has a
	// pending outbound HTLC, its fulfill remained stuck in nodes[1]'s holding cell after reload.
	let pending: Vec<_> = nodes[0].node.list_channels().iter()
		.filter(|channel| channel.channel_id == chan_id_a || channel.channel_id == chan_id_b)
		.filter(|channel| !channel.pending_outbound_htlcs.is_empty())
		.map(|channel| channel.channel_id)
		.collect();
	assert!(pending.is_empty(), "HTLC fulfills remained stuck on channels {:?}", pending);
}

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 persister;
	let new_chain_monitor;

	let mut intercept_forwards_config = test_legacy_channel_config();
	intercept_forwards_config.htlc_interception_flags =
		HTLCInterceptionFlags::ToInterceptSCIDs as u8;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);
	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, 1_000_000), 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]);
	do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, false, false);

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

	expect_htlc_failure_conditions(nodes[1].node.get_and_clear_pending_events(), &[]);

	let mut intercept_id = None;
	let mut expected_outbound_amount_msat = None;
	if use_intercept {
		nodes[1].node.test_process_pending_update_add_htlcs();
		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();
	}

	nodes[1].node.process_pending_htlc_forwards();

	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_batch_test(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_unsafe_legacy(
			&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();
	let message = "Channel force-closed".to_owned();

	nodes[2]
		.node
		.force_close_broadcasting_latest_txn(&chan_id_2, &nodes[1].node.get_our_node_id(), message.clone())
		.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);
	let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
	check_closed_event(&nodes[2], 1, reason, &[nodes[1].node.get_our_node_id()], 100000);
	check_closed_broadcast(&nodes[2], 1, 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, &[nodes[2].node.get_our_node_id()], 100000);

	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(ReconnectArgs::new(&nodes[0], &nodes[1]));

	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 mut txn = nodes[1].tx_broadcaster.txn_broadcast();
			assert_eq!(txn.len(), if nodes[1].connect_style.borrow().updates_best_block_first() { 2 } else { 1 });
			let bs_htlc_timeout_tx = txn.pop().unwrap();
			confirm_transaction(&nodes[1], &bs_htlc_timeout_tx);
		}
	} else {
		confirm_transaction(&nodes[1], &bs_commitment_tx[0]);
	}

	if !claim_htlc {
		expect_and_process_pending_htlcs_and_htlc_handling_failed(
			&nodes[1],
			&[HTLCHandlingFailureType::Forward { 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 mut update = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
	if claim_htlc {
		nodes[0].node.handle_update_fulfill_htlc(nodes[1].node.get_our_node_id(), update.update_fulfill_htlcs.remove(0));
	} else {
		nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &update.update_fail_htlcs[0]);
	}
	do_commitment_signed_dance(&nodes[0], &nodes[1], &update.commitment_signed, false, false);

	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 persister;
	let new_chain_monitor;

	let legacy_cfg = test_legacy_channel_config();
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg.clone()), Some(legacy_cfg)]);
	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_and_process_pending_htlcs_and_htlc_handling_failed(
		&nodes[2],
		&[HTLCHandlingFailureType::Receive { 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]);
			do_commitment_signed_dance(&nodes[1], &nodes[2], &commitment_signed, false, 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(ReconnectArgs::new(&nodes[0], &nodes[1]));

	expect_and_process_pending_htlcs_and_htlc_handling_failed(
		&nodes[1],
		&[HTLCHandlingFailureType::Forward { 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]);
			do_commitment_signed_dance(&nodes[0], &nodes[1], &commitment_signed, false, false);
		},
		_ => panic!("Unexpected event"),
	}

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

#[test]
fn manager_persisted_pre_outbound_edge_forward() {
	do_manager_persisted_pre_outbound_edge_forward(false);
}

#[test]
fn manager_persisted_pre_outbound_edge_intercept_forward() {
	do_manager_persisted_pre_outbound_edge_forward(true);
}

fn do_manager_persisted_pre_outbound_edge_forward(intercept_htlc: bool) {
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let mut intercept_forwards_config = test_default_channel_config();
	intercept_forwards_config.htlc_interception_flags =
		HTLCInterceptionFlags::ToInterceptSCIDs as u8;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(intercept_forwards_config), None]);
	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;

	// Lock in the HTLC from node_a <> node_b.
	let amt_msat = 5000;
	let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
	if intercept_htlc {
		route.paths[0].hops[1].short_channel_id = nodes[1].node.get_intercept_scid();
	}
	nodes[0].node.send_payment_with_route(route, payment_hash, RecipientOnionFields::secret_only(payment_secret, amt_msat), PaymentId(payment_hash.0)).unwrap();
	check_added_monitors(&nodes[0], 1);
	let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
	nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
	do_commitment_signed_dance(&nodes[1], &nodes[0], &updates.commitment_signed, false, false);
	// While an inbound HTLC is committed in a channel but not yet forwarded, we store its onion in
	// the `Channel` in case we need to remember it on restart. Once it's irrevocably forwarded to the
	// outbound edge, we can prune it on the inbound edge.
	assert_eq!(
		nodes[1].node.test_get_inbound_committed_htlcs_with_onion(nodes[0].node.get_our_node_id(), chan_id_1),
		1
	);

	// Decode the HTLC onion but don't forward it to the next hop, such that the HTLC ends up in
	// `ChannelManager::forward_htlcs` or `ChannelManager::pending_intercepted_htlcs`.
	nodes[1].node.test_process_pending_update_add_htlcs();

	// Disconnect peers and reload the forwarding node_b.
	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 node_b_encoded = nodes[1].node.encode();

	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_b_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[0]));
	let mut args_b_c = ReconnectArgs::new(&nodes[1], &nodes[2]);
	args_b_c.send_channel_ready = (true, true);
	args_b_c.send_announcement_sigs = (true, true);
	reconnect_nodes(args_b_c);

	// Before an inbound HTLC is irrevocably forwarded, its onion should still be persisted within the
	// inbound edge channel.
	assert_eq!(
		nodes[1].node.test_get_inbound_committed_htlcs_with_onion(nodes[0].node.get_our_node_id(), chan_id_1),
		1
	);

	// Forward the HTLC and ensure we can claim it post-reload.
	nodes[1].node.process_pending_htlc_forwards();

	if intercept_htlc {
		let events = nodes[1].node.get_and_clear_pending_events();
		assert_eq!(events.len(), 1);
		let (intercept_id, expected_outbound_amt_msat) = match events[0] {
			Event::HTLCIntercepted { intercept_id, expected_outbound_amount_msat, .. } => {
				(intercept_id, expected_outbound_amount_msat)
			},
			_ => panic!()
		};
		nodes[1].node.forward_intercepted_htlc(intercept_id, &chan_id_2,
			nodes[2].node.get_our_node_id(), expected_outbound_amt_msat).unwrap();
		nodes[1].node.process_pending_htlc_forwards();
	}
	check_added_monitors(&nodes[1], 1);

	let updates = get_htlc_update_msgs(&nodes[1], &nodes[2].node.get_our_node_id());
	nodes[2].node.handle_update_add_htlc(nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
	do_commitment_signed_dance(&nodes[2], &nodes[1], &updates.commitment_signed, false, false);
	expect_and_process_pending_htlcs(&nodes[2], false);
	// After an inbound HTLC is irrevocably forwarded, its onion should be pruned within the inbound
	// edge channel.
	assert_eq!(
		nodes[1].node.test_get_inbound_committed_htlcs_with_onion(nodes[0].node.get_our_node_id(), chan_id_1),
		0
	);

	expect_payment_claimable!(nodes[2], payment_hash, payment_secret, amt_msat, None, nodes[2].node.get_our_node_id());
	let path: &[&[_]] = &[&[&nodes[1], &nodes[2]]];
	do_claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], path, payment_preimage));
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[test]
fn test_manager_persisted_post_outbound_edge_forward() {
	// Test that we will not double-forward an HTLC after restart if it has already been forwarded to
	// the outbound edge, which was previously broken.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	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;

	// Lock in the HTLC from node_a <> node_b.
	let amt_msat = 5000;
	let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
	nodes[0].node.send_payment_with_route(route, payment_hash, RecipientOnionFields::secret_only(payment_secret, amt_msat), PaymentId(payment_hash.0)).unwrap();
	check_added_monitors(&nodes[0], 1);
	let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
	nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
	do_commitment_signed_dance(&nodes[1], &nodes[0], &updates.commitment_signed, false, false);

	// Add the HTLC to the outbound edge, node_b <> node_c.
	nodes[1].node.process_pending_htlc_forwards();
	check_added_monitors(&nodes[1], 1);

	// Disconnect peers and reload the forwarding node_b.
	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 node_b_encoded = nodes[1].node.encode();
	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_b_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[0]));
	let mut args_b_c = ReconnectArgs::new(&nodes[1], &nodes[2]);
	args_b_c.send_channel_ready = (true, true);
	args_b_c.send_announcement_sigs = (true, true);
	args_b_c.pending_htlc_adds = (0, 1);
	// While reconnecting, we re-send node_b's outbound update_add and commit the HTLC to the b<>c
	// channel.
	reconnect_nodes(args_b_c);

	// Ensure node_b won't double-forward the outbound HTLC (this was previously broken).
	nodes[1].node.process_pending_htlc_forwards();
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	// Claim the HTLC backwards to node_a.
	expect_and_process_pending_htlcs(&nodes[2], false);
	expect_payment_claimable!(nodes[2], payment_hash, payment_secret, amt_msat, None, nodes[2].node.get_our_node_id());
	let path: &[&[_]] = &[&[&nodes[1], &nodes[2]]];
	do_claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], path, payment_preimage));
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[test]
fn test_manager_persisted_post_outbound_edge_holding_cell() {
	// Test that we will not double-forward an HTLC after restart if it is already in the outbound
	// edge's holding cell, which was previously broken.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	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;
	send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);

	// Lock in the HTLC from node_a <> node_b.
	let amt_msat = 1000;
	let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);
	let onion = RecipientOnionFields::secret_only(payment_secret, amt_msat);
	nodes[0].node.send_payment_with_route(route, payment_hash, onion, PaymentId(payment_hash.0)).unwrap();
	check_added_monitors(&nodes[0], 1);
	let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
	nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
	do_commitment_signed_dance(&nodes[1], &nodes[0], &updates.commitment_signed, false, false);

	// Send a 2nd HTLC node_c -> node_b, to force the first HTLC into the holding cell.
	chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[1], amt_msat);
	let onion = RecipientOnionFields::secret_only(payment_secret_2, amt_msat);
	nodes[2].node.send_payment_with_route(route_2, payment_hash_2, onion, PaymentId(payment_hash_2.0)).unwrap();
	let send_event =
		SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
	nodes[1].node.handle_update_add_htlc(nodes[2].node.get_our_node_id(), &send_event.msgs[0]);
	nodes[1].node.handle_commitment_signed_batch_test(nodes[2].node.get_our_node_id(), &send_event.commitment_msg);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	check_added_monitors(&nodes[1], 1);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	// Add the HTLC to the outbound edge, node_b <> node_c. Force the outbound HTLC into the b<>c
	// holding cell.
	nodes[1].node.process_pending_htlc_forwards();
	check_added_monitors(&nodes[1], 0);
	assert_eq!(
		nodes[1].node.test_holding_cell_outbound_htlc_forwards_count(nodes[2].node.get_our_node_id(), chan_id_2),
		1
	);

	// Disconnect peers and reload the forwarding node_b.
	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 node_b_encoded = nodes[1].node.encode();
	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_b_encoded, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);

	chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
	let (latest_update, _) = get_latest_mon_update_id(&nodes[1], chan_id_2);
	nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(chan_id_2, latest_update);

	reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[0]));

	// Reconnect b<>c. Node_b has pending RAA + commitment_signed from the incomplete c->b
	// commitment dance, plus an HTLC in the holding cell that will be released after the dance.
	let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[2]);
	reconnect_args.pending_raa = (false, true);
	reconnect_args.pending_responding_commitment_signed = (false, true);
	// Node_c needs a monitor update to catch up after processing node_b's reestablish.
	reconnect_args.expect_renegotiated_funding_locked_monitor_update = (false, true);
	// The holding cell HTLC will be released after the commitment dance - handle it below.
	reconnect_args.allow_post_commitment_dance_msgs = (false, true);
	reconnect_nodes(reconnect_args);

	// The holding cell HTLC was released during the reconnect. Complete its commitment dance.
	let holding_cell_htlc_msgs = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(holding_cell_htlc_msgs.len(), 1);
	match &holding_cell_htlc_msgs[0] {
		MessageSendEvent::UpdateHTLCs { node_id, updates, .. } => {
			assert_eq!(*node_id, nodes[2].node.get_our_node_id());
			assert_eq!(updates.update_add_htlcs.len(), 1);
			nodes[2].node.handle_update_add_htlc(nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
			do_commitment_signed_dance(&nodes[2], &nodes[1], &updates.commitment_signed, false, false);
		}
		_ => panic!("Unexpected message: {:?}", holding_cell_htlc_msgs[0]),
	}

	// Ensure node_b won't double-forward the outbound HTLC (this was previously broken).
	nodes[1].node.process_pending_htlc_forwards();
	let msgs = nodes[1].node.get_and_clear_pending_msg_events();
	assert!(msgs.is_empty(), "Expected 0 messages, got {:?}", msgs);

	// The a->b->c HTLC is now committed on node_c. The c->b HTLC is committed on node_b.
	// Both payments should now be claimable.
	expect_and_process_pending_htlcs(&nodes[2], false);
	expect_payment_claimable!(nodes[2], payment_hash, payment_secret, amt_msat, None, nodes[2].node.get_our_node_id());
	expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, amt_msat, None, nodes[1].node.get_our_node_id());

	// Claim the a->b->c payment on node_c.
	let path: &[&[_]] = &[&[&nodes[1], &nodes[2]]];
	do_claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], path, payment_preimage));
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);

	// Claim the c->b payment on node_b.
	nodes[1].node.claim_funds(payment_preimage_2);
	expect_payment_claimed!(nodes[1], payment_hash_2, amt_msat);
	check_added_monitors(&nodes[1], 1);
	let mut update = get_htlc_update_msgs(&nodes[1], &nodes[2].node.get_our_node_id());
	nodes[2].node.handle_update_fulfill_htlc(nodes[1].node.get_our_node_id(), update.update_fulfill_htlcs.remove(0));
	do_commitment_signed_dance(&nodes[2], &nodes[1], &update.commitment_signed, false, false);
	expect_payment_sent(&nodes[2], payment_preimage_2, None, true, true);
}

#[test]
fn test_reload_partial_funding_batch() {
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let new_persister;
	let new_chain_monitor;

	let legacy_cfg = test_legacy_channel_config();
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg.clone()), Some(legacy_cfg)]);
	let new_channel_manager;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	// Initiate channel opening and create the batch channel funding transaction.
	let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[
		(&nodes[1], 100_000, 0, 42, None),
		(&nodes[2], 200_000, 0, 43, None),
	]);

	// Go through the funding_created and funding_signed flow with node 1.
	nodes[1].node.handle_funding_created(nodes[0].node.get_our_node_id(), &funding_created_msgs[0]);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());

	// The monitor is persisted when receiving funding_signed.
	let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
	nodes[0].node.handle_funding_signed(nodes[1].node.get_our_node_id(), &funding_signed_msg);
	check_added_monitors(&nodes[0], 1);

	// The transaction should not have been broadcast before all channels are ready.
	assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);

	// Reload the node while a subset of the channels in the funding batch have persisted monitors.
	let channel_id_1 = ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.compute_txid(), index: 0 });
	let node_encoded = nodes[0].node.encode();
	let channel_monitor_1_serialized = get_monitor!(nodes[0], channel_id_1).encode();
	reload_node!(nodes[0], node_encoded, &[&channel_monitor_1_serialized], new_persister, new_chain_monitor, new_channel_manager);

	// Process monitor events.
	assert!(nodes[0].node.get_and_clear_pending_events().is_empty());

	// The monitor should become closed.
	check_added_monitors(&nodes[0], 1);
	{
		let mut monitor_updates = nodes[0].chain_monitor.monitor_updates.lock().unwrap();
		let monitor_updates_1 = monitor_updates.get(&channel_id_1).unwrap();
		assert_eq!(monitor_updates_1.len(), 1);
		assert_eq!(monitor_updates_1[0].updates.len(), 1);
		assert!(matches!(monitor_updates_1[0].updates[0], ChannelMonitorUpdateStep::ChannelForceClosed { .. }));
	}

	// The funding transaction should not have been broadcast, but we broadcast the force-close
	// transaction as part of closing the monitor.
	{
		let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
		assert_eq!(broadcasted_txs.len(), 1);
		assert!(broadcasted_txs[0].compute_txid() != tx.compute_txid());
		assert_eq!(broadcasted_txs[0].input.len(), 1);
		assert_eq!(broadcasted_txs[0].input[0].previous_output.txid, tx.compute_txid());
	}

	// Ensure the channels don't exist anymore.
	assert!(nodes[0].node.list_channels().is_empty());
}

#[test]
fn test_htlc_localremoved_persistence() {
	// Tests that if we fail an htlc back (update_fail_htlc message) and then restart the node, the node will resend the
	// exact same fail message.
	let chanmon_cfgs: Vec<TestChanMonCfg> = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);

	let persister;
	let chain_monitor;
	let deserialized_chanmgr;

	// Send a keysend payment that fails because of a preimage mismatch.
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	let payee_pubkey = nodes[1].node.get_our_node_id();

	let _chan = create_chan_between_nodes(&nodes[0], &nodes[1]);
	let route_params = RouteParameters::from_payment_params_and_value(
		PaymentParameters::for_keysend(payee_pubkey, 40, false), 10_000);
	let route = find_route(
		&nodes[0], &route_params
	).unwrap();

	let test_preimage = PaymentPreimage([42; 32]);
	let mismatch_payment_hash = PaymentHash([43; 32]);
	let session_privs = nodes[0].node.test_add_new_pending_payment(mismatch_payment_hash,
		RecipientOnionFields::spontaneous_empty(10_000), PaymentId(mismatch_payment_hash.0), &route).unwrap();
	nodes[0].node.test_send_payment_internal(&route, mismatch_payment_hash,
		RecipientOnionFields::spontaneous_empty(10_000), Some(test_preimage), PaymentId(mismatch_payment_hash.0), session_privs).unwrap();
	check_added_monitors(&nodes[0], 1);

	let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
	nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
	do_commitment_signed_dance(&nodes[1], &nodes[0], &updates.commitment_signed, false, false);
	expect_and_process_pending_htlcs(&nodes[1], false);
	expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCHandlingFailureType::Receive { payment_hash: mismatch_payment_hash }]);
	check_added_monitors(&nodes[1], 1);

	// Save the update_fail_htlc message for later comparison.
	let msgs = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
	let htlc_fail_msg = msgs.update_fail_htlcs[0].clone();

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

	let monitor_encoded = get_monitor!(nodes[1], _chan.3).encode();
	reload_node!(nodes[1], nodes[1].node.encode(), &[&monitor_encoded], persister, chain_monitor, deserialized_chanmgr);

	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]);
	assert_eq!(reestablish_1.len(), 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]);
	assert_eq!(reestablish_2.len(), 1);
	nodes[0].node.handle_channel_reestablish(nodes[1].node.get_our_node_id(), &reestablish_2[0]);
	handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
	nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), &reestablish_1[0]);

	// Assert that same failure message is resent after reload.
	let msgs = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
	let htlc_fail_msg_after_reload = msgs.2.unwrap().update_fail_htlcs[0].clone();
	assert_eq!(htlc_fail_msg, htlc_fail_msg_after_reload);
}



#[test]
#[cfg(peer_storage)]
fn test_peer_storage() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let (persister, chain_monitor);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let nodes_0_deserialized;
	let legacy_cfg = test_legacy_channel_config();
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg)]);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	let node_a_id = nodes[0].node.get_our_node_id();
	let node_b_id = nodes[1].node.get_our_node_id();

	let (_, _, cid, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
	send_payment(&nodes[0], &[&nodes[1]], 1000);
	let nodes_0_serialized = nodes[0].node.encode();
	let old_state_monitor = get_monitor!(nodes[0], cid).encode();
	send_payment(&nodes[0], &[&nodes[1]], 10000);
	send_payment(&nodes[0], &[&nodes[1]], 9999);

	// Update peer storage with latest commitment txns
	connect_blocks(&nodes[0], 1);
	connect_blocks(&nodes[0], 1);

	let peer_storage_msg_events_node0 =
		nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_msg_events();
	let peer_storage_msg_events_node1 =
		nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_msg_events();
	assert_ne!(peer_storage_msg_events_node0.len(), 0);
	assert_ne!(peer_storage_msg_events_node1.len(), 0);

	for ps_msg in peer_storage_msg_events_node0 {
		match ps_msg {
			MessageSendEvent::SendPeerStorage { ref node_id, ref msg } => {
				assert_eq!(*node_id, node_b_id);
				nodes[1].node.handle_peer_storage(node_a_id, msg.clone());
			},
			_ => panic!("Unexpected event"),
		}
	}

	for ps_msg in peer_storage_msg_events_node1 {
		match ps_msg {
			MessageSendEvent::SendPeerStorage { ref node_id, ref msg } => {
				assert_eq!(*node_id, node_a_id);
				nodes[0].node.handle_peer_storage(node_b_id, msg.clone());
			},
			_ => panic!("Unexpected event"),
		}
	}

	nodes[0].node.peer_disconnected(node_b_id);
	nodes[1].node.peer_disconnected(node_a_id);

	// Reload Node!
	// TODO: Handle the case where we've completely forgotten about an active channel.
	reload_node!(
		nodes[0],
		test_legacy_channel_config(),
		&nodes_0_serialized,
		&[&old_state_monitor[..]],
		persister,
		chain_monitor,
		nodes_0_deserialized
	);

	let init_msg = msgs::Init {
		features: nodes[1].node.init_features(),
		networks: None,
		remote_network_address: None,
	};

	nodes[0].node.peer_connected(node_b_id, &init_msg, true).unwrap();
	nodes[1].node.peer_connected(node_a_id, &init_msg, true).unwrap();

	let node_1_events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(node_1_events.len(), 2);

	let node_0_events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(node_0_events.len(), 1);

	match node_0_events[0] {
		MessageSendEvent::SendChannelReestablish { ref node_id, .. } => {
			assert_eq!(*node_id, node_b_id);
			// nodes[0] would send a stale channel reestablish, so there's no need to handle this.
		},
		_ => panic!("Unexpected event"),
	}

	if let MessageSendEvent::SendPeerStorageRetrieval { node_id, msg } = &node_1_events[0] {
		assert_eq!(*node_id, node_a_id);
		// Should Panic here!
		let res = std::panic::catch_unwind(|| {
			nodes[0].node.handle_peer_storage_retrieval(node_b_id, msg.clone());
		});
		assert!(res.is_err());
	} else {
		panic!("Unexpected event {node_1_events:?}")
	}

	if let MessageSendEvent::SendChannelReestablish { .. } = &node_1_events[1] {
		// After the `peer_storage_retreival` message would come a `channel_reestablish` (which
		// would also cause nodes[0] to panic) but it already went down due to lost state so
		// there's nothing to deliver.
	} else {
		panic!("Unexpected event {node_1_events:?}")
	}
	// When we panic'd, we expect to panic on `Drop`.
	let res = std::panic::catch_unwind(|| drop(nodes));
	assert!(res.is_err());
}

#[test]
fn test_hold_completed_inflight_monitor_updates_upon_manager_reload() {
	// Test that if a `ChannelMonitorUpdate` completes after the `ChannelManager` is serialized,
	// but before it is deserialized, we hold any completed in-flight updates until background event
	// processing. Previously, we would remove completed monitor updates from
	// `in_flight_monitor_updates` during deserialization, relying on
	// [`ChannelManager::process_background_events`] to eventually be called before the
	// `ChannelManager` is serialized again such that the channel is resumed and further updates can
	// be made.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let (persister_a, persister_b);
	let (chain_monitor_a, chain_monitor_b);

	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes_0_deserialized_a;
	let nodes_0_deserialized_b;

	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;

	send_payment(&nodes[0], &[&nodes[1]], 1_000_000);

	chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);

	// Send a payment that will be pending due to an async monitor update.
	let (route, payment_hash, _, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
	let payment_id = PaymentId(payment_hash.0);
	let onion = RecipientOnionFields::secret_only(payment_secret, 1_000_000);
	nodes[0].node.send_payment_with_route(route, payment_hash, onion, payment_id).unwrap();
	check_added_monitors(&nodes[0], 1);

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

	// Serialize the ChannelManager while the monitor update is still in-flight.
	let node_0_serialized = nodes[0].node.encode();

	// Now complete the monitor update by calling force_channel_monitor_updated.
	// This updates the monitor's state, but the ChannelManager still thinks it's pending.
	let (_, latest_update_id) = nodes[0].chain_monitor.get_latest_mon_update_id(chan_id);
	nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(chan_id, latest_update_id);
	let monitor_serialized_updated = get_monitor!(nodes[0], chan_id).encode();

	// Reload the node with the updated monitor. Upon deserialization, the ChannelManager will
	// detect that the monitor update completed (monitor's update_id >= the in-flight update_id)
	// and queue a `BackgroundEvent::MonitorUpdatesComplete`.
	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],
		test_default_channel_config(),
		&node_0_serialized,
		&[&monitor_serialized_updated[..]],
		persister_a,
		chain_monitor_a,
		nodes_0_deserialized_a
	);

	// If we serialize again, even though we haven't processed any background events yet, we should
	// still see the `BackgroundEvent::MonitorUpdatesComplete` be regenerated on startup.
	let node_0_serialized = nodes[0].node.encode();
	reload_node!(
		nodes[0],
		test_default_channel_config(),
		&node_0_serialized,
		&[&monitor_serialized_updated[..]],
		persister_b,
		chain_monitor_b,
		nodes_0_deserialized_b
	);

	// Reconnect the nodes. We should finally see the `update_add_htlc` go out, as the reconnection
	// should first process `BackgroundEvent::MonitorUpdatesComplete, allowing the channel to be
	// resumed.
	let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
	reconnect_args.pending_htlc_adds = (0, 1);
	reconnect_nodes(reconnect_args);
}

#[test]
fn outbound_removed_holding_cell_resolved_no_double_forward() {
	// Test that if a forwarding node has an HTLC that is fully removed on the outbound edge
	// but where the inbound edge resolution is in the holding cell, and we reload the node in this
	// state, that node will not double-forward the HTLC.

	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes_1_deserialized;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_0_id = nodes[0].node.get_our_node_id();
	let node_1_id = nodes[1].node.get_our_node_id();
	let node_2_id = nodes[2].node.get_our_node_id();

	let chan_0_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
	let chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);

	let chan_id_0_1 = chan_0_1.2;
	let chan_id_1_2 = chan_1_2.2;

	// Send a payment from nodes[0] to nodes[2] via nodes[1].
	let (route, payment_hash, payment_preimage, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
	send_along_route_with_secret(
		&nodes[0], route, &[&[&nodes[1], &nodes[2]]], 1_000_000, payment_hash, payment_secret,
	);

	// Claim the payment on nodes[2].
	nodes[2].node.claim_funds(payment_preimage);
	check_added_monitors(&nodes[2], 1);
	expect_payment_claimed!(nodes[2], payment_hash, 1_000_000);

	// Disconnect nodes[0] from nodes[1] BEFORE processing the fulfill.
	// This forces the inbound fulfill resolution go to into nodes[1]'s holding cell for the inbound
	// channel.
	nodes[0].node.peer_disconnected(node_1_id);
	nodes[1].node.peer_disconnected(node_0_id);

	// Process the fulfill from nodes[2] to nodes[1].
	let updates_2_1 = get_htlc_update_msgs(&nodes[2], &node_1_id);
	nodes[1].node.handle_update_fulfill_htlc(node_2_id, updates_2_1.update_fulfill_htlcs[0].clone());
	check_added_monitors(&nodes[1], 1);
	do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, false, false);
	expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);

	// At this point:
	// - The outbound HTLC nodes[1]->nodes[2] is resolved and removed
	// - The inbound HTLC nodes[0]->nodes[1] is still in a Committed state, with the fulfill
	//   resolution in nodes[1]'s chan_0_1 holding cell
	let node_1_serialized = nodes[1].node.encode();
	let mon_0_1_serialized = get_monitor!(nodes[1], chan_id_0_1).encode();
	let mon_1_2_serialized = get_monitor!(nodes[1], chan_id_1_2).encode();

	// Reload nodes[1].
	// During deserialization, we previously would have not noticed that the nodes[0]<>nodes[1] HTLC
	// had a resolution pending in the holding cell, and reconstructed the ChannelManager's pending
	// HTLC state indicating that the HTLC still needed to be forwarded to the outbound edge.
	reload_node!(
		nodes[1],
		node_1_serialized,
		&[&mon_0_1_serialized, &mon_1_2_serialized],
		persister,
		new_chain_monitor,
		nodes_1_deserialized
	);

	// Check that nodes[1] doesn't double-forward the HTLC.
	nodes[1].node.process_pending_htlc_forwards();

	// Reconnect nodes[1] to nodes[0]. The claim should be in nodes[1]'s holding cell.
	let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[0]);
	reconnect_args.pending_cell_htlc_claims = (0, 1);
	reconnect_nodes(reconnect_args);

	// nodes[0] should now have received the fulfill and generate PaymentSent.
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[test]
fn test_reload_node_with_preimage_in_monitor_claims_htlc() {
	// Test that if a forwarding node has an HTLC that was irrevocably removed on the outbound edge
	// via claim but is still forwarded-and-unresolved in the inbound edge, that HTLC will not be
	// failed back on the inbound edge on reload.
	//
	// For context, the ChannelManager is moving towards reconstructing the pending inbound HTLC set
	// from Channel data on startup. If we find an inbound HTLC that is flagged as already-forwarded,
	// we then check that the HTLC is either (a) still present in the outbound edge or (b) removed
	// from the outbound edge but with a preimage present in the corresponding ChannelMonitor,
	// indicating that it was removed from the outbound edge via claim. If neither of those are the
	// case, we infer that the HTLC was removed from the outbound edge via failure and fail the HTLC
	// backwards.
	//
	// Here we ensure that inbound HTLCs in case (b) above will not be failed backwards on manager
	// reload.

	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes_1_deserialized;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_0_id = nodes[0].node.get_our_node_id();
	let node_1_id = nodes[1].node.get_our_node_id();
	let node_2_id = nodes[2].node.get_our_node_id();

	let chan_0_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
	let chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);

	let chan_id_0_1 = chan_0_1.2;
	let chan_id_1_2 = chan_1_2.2;

	// Send a payment from nodes[0] to nodes[2] via nodes[1].
	let (route, payment_hash, payment_preimage, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
	send_along_route_with_secret(
		&nodes[0], route, &[&[&nodes[1], &nodes[2]]], 1_000_000, payment_hash, payment_secret,
	);

	// Claim the payment on nodes[2].
	nodes[2].node.claim_funds(payment_preimage);
	check_added_monitors(&nodes[2], 1);
	expect_payment_claimed!(nodes[2], payment_hash, 1_000_000);

	// Disconnect nodes[0] from nodes[1] BEFORE processing the fulfill.
	// This prevents the claim from propagating back, leaving the inbound HTLC in ::Forwarded state.
	nodes[0].node.peer_disconnected(node_1_id);
	nodes[1].node.peer_disconnected(node_0_id);

	// Process the fulfill from nodes[2] to nodes[1].
	// This stores the preimage in nodes[1]'s monitor for chan_1_2.
	let updates_2_1 = get_htlc_update_msgs(&nodes[2], &node_1_id);
	nodes[1].node.handle_update_fulfill_htlc(node_2_id, updates_2_1.update_fulfill_htlcs[0].clone());
	check_added_monitors(&nodes[1], 1);
	do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, false, false);
	expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);

	// Clear the holding cell's claim entry on chan_0_1 before serialization.
	// This simulates a crash where the HTLC was fully removed from the outbound edge but is still
	// present on the inbound edge without a resolution.
	nodes[1].node.test_clear_channel_holding_cell(node_0_id, chan_id_0_1);

	// At this point:
	// - The inbound HTLC on nodes[1] (from nodes[0]) is in ::Forwarded state
	// - The preimage IS in nodes[1]'s monitor for chan_1_2
	// - The outbound HTLC to nodes[2] is resolved
	//
	// Serialize nodes[1] state and monitors before reloading.
	let node_1_serialized = nodes[1].node.encode();
	let mon_0_1_serialized = get_monitor!(nodes[1], chan_id_0_1).encode();
	let mon_1_2_serialized = get_monitor!(nodes[1], chan_id_1_2).encode();

	// Reload nodes[1].
	// During deserialization, we track inbound HTLCs that purport to already be forwarded on the
	// outbound edge. If any are entirely missing from the outbound edge with no preimage available,
	// they will be failed backwards. Otherwise, as in this case where a preimage is available, the
	// payment should be claimed backwards.
	reload_node!(
		nodes[1],
		node_1_serialized,
		&[&mon_0_1_serialized, &mon_1_2_serialized],
		persister,
		new_chain_monitor,
		nodes_1_deserialized,
		Some(true)
	);

	// When the claim is reconstructed during reload, a PaymentForwarded event is generated.
	// Fetching events triggers the pending monitor update (adding preimage) to be applied.
	expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);
	check_added_monitors(&nodes[1], 1);

	// Reconnect nodes[1] to nodes[0]. The claim should be in nodes[1]'s holding cell.
	let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[0]);
	reconnect_args.pending_cell_htlc_claims = (0, 1);
	reconnect_nodes(reconnect_args);

	// nodes[0] should now have received the fulfill and generate PaymentSent.
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[test]
fn test_reload_node_without_preimage_fails_htlc() {
	// Test that if a forwarding node has an HTLC that was removed on the outbound edge via failure
	// but is still forwarded-and-unresolved in the inbound edge, that HTLC will be correctly
	// failed back on reload via the already_forwarded_htlcs mechanism.
	//
	// For context, the ChannelManager reconstructs the pending inbound HTLC set from Channel data
	// on startup. If an inbound HTLC is present but flagged as already-forwarded, we check that
	// the HTLC is either (a) still present in the outbound edge or (b) removed from the outbound
	// edge but with a preimage present in the corresponding ChannelMonitor, indicating it was
	// removed via claim. If neither, we infer the HTLC was removed via failure and fail it back.
	//
	// Here we test the failure case: no preimage is present, so the HTLC should be failed back.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes_1_deserialized;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_0_id = nodes[0].node.get_our_node_id();
	let node_1_id = nodes[1].node.get_our_node_id();
	let node_2_id = nodes[2].node.get_our_node_id();

	let chan_0_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
	let chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);

	let chan_id_0_1 = chan_0_1.2;
	let chan_id_1_2 = chan_1_2.2;

	// Send a payment from nodes[0] to nodes[2] via nodes[1].
	let (route, payment_hash, _, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
	send_along_route_with_secret(
		&nodes[0], route, &[&[&nodes[1], &nodes[2]]], 1_000_000, payment_hash, payment_secret,
	);

	// Disconnect nodes[0] from nodes[1] BEFORE processing the failure.
	// This prevents the fail from propagating back, leaving the inbound HTLC in ::Forwarded state.
	nodes[0].node.peer_disconnected(node_1_id);
	nodes[1].node.peer_disconnected(node_0_id);

	// Fail the payment on nodes[2] and process the failure to nodes[1].
	// This removes the outbound HTLC and queues a fail in the holding cell.
	nodes[2].node.fail_htlc_backwards(&payment_hash);
	expect_and_process_pending_htlcs_and_htlc_handling_failed(
		&nodes[2], &[HTLCHandlingFailureType::Receive { payment_hash }]
	);
	check_added_monitors(&nodes[2], 1);

	let updates_2_1 = get_htlc_update_msgs(&nodes[2], &node_1_id);
	nodes[1].node.handle_update_fail_htlc(node_2_id, &updates_2_1.update_fail_htlcs[0]);
	do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, false, false);
	expect_and_process_pending_htlcs_and_htlc_handling_failed(
		&nodes[1], &[HTLCHandlingFailureType::Forward { node_id: Some(node_2_id), channel_id: chan_id_1_2 }]
	);

	// Clear the holding cell's fail entry on chan_0_1 before serialization.
	// This simulates a crash where the HTLC was fully removed from the outbound edge but is still
	// present on the inbound edge without a resolution. Otherwise, we would not be able to exercise
	// the desired failure paths due to the holding cell failure resolution being present.
	nodes[1].node.test_clear_channel_holding_cell(node_0_id, chan_id_0_1);

	// Now serialize. The state has:
	// - Inbound HTLC on chan_0_1 in ::Forwarded state
	// - Outbound HTLC on chan_1_2 resolved (not present)
	// - No preimage in monitors (it was a failure)
	// - No holding cell entry for the fail (we cleared it)
	let node_1_serialized = nodes[1].node.encode();
	let mon_0_1_serialized = get_monitor!(nodes[1], chan_id_0_1).encode();
	let mon_1_2_serialized = get_monitor!(nodes[1], chan_id_1_2).encode();

	// Reload nodes[1].
	// The already_forwarded_htlcs mechanism should detect:
	// - Inbound HTLC is in ::Forwarded state
	// - Outbound HTLC is not present in outbound channel
	// - No preimage in monitors
	// Therefore it should fail the HTLC backwards.
	reload_node!(
		nodes[1],
		node_1_serialized,
		&[&mon_0_1_serialized, &mon_1_2_serialized],
		persister,
		new_chain_monitor,
		nodes_1_deserialized,
		Some(true)
	);

	// After reload, nodes[1] should have generated an HTLCHandlingFailed event.
	let events = nodes[1].node.get_and_clear_pending_events();
	assert!(!events.is_empty(), "Expected HTLCHandlingFailed event");
	for event in events {
		match event {
			Event::HTLCHandlingFailed { .. } => {},
			_ => panic!("Unexpected event {:?}", event),
		}
	}

	// Process the failure so it goes back into chan_0_1's holding cell.
	nodes[1].node.process_pending_htlc_forwards();
	check_added_monitors(&nodes[1], 0); // No monitor update yet (peer disconnected)

	// Reconnect nodes[1] to nodes[0]. The fail should be in nodes[1]'s holding cell.
	let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[0]);
	reconnect_args.pending_cell_htlc_fails = (0, 1);
	reconnect_nodes(reconnect_args);

	// nodes[0] should now have received the failure and generate PaymentFailed.
	expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new());
}

#[test]
fn test_reload_with_mpp_claims_on_same_channel() {
	// Test that if a forwarding node has two HTLCs for the same MPP payment that were both
	// irrevocably removed on the outbound edge via claim but are still forwarded-and-unresolved
	// on the inbound edge, both HTLCs will be claimed backwards on restart.
	//
	// Topology:
	//   nodes[0] ----chan_0_1----> nodes[1] ----chan_1_2_a----> nodes[2]
	//                                      \----chan_1_2_b---/
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let persister;
	let new_chain_monitor;
	let mut config = test_default_channel_config();
	// Set the percentage to the default value at the time this test was written
	config.channel_handshake_config.announced_channel_max_inbound_htlc_value_in_flight_percentage = 10;
	let configs: [Option<UserConfig>; 3] = core::array::from_fn(|_| Some(config.clone()));
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &configs);
	let nodes_1_deserialized;
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_0_id = nodes[0].node.get_our_node_id();
	let node_1_id = nodes[1].node.get_our_node_id();
	let node_2_id = nodes[2].node.get_our_node_id();

	let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 2_000_000, 0);
	let chan_1_2_a = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	let chan_1_2_b = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let chan_id_0_1 = chan_0_1.2;
	let chan_id_1_2_a = chan_1_2_a.2;
	let chan_id_1_2_b = chan_1_2_b.2;

	// Send an MPP payment large enough that the router must split it across both outbound channels.
	// Each 1M sat outbound channel has 100M msat max in-flight, so 150M msat requires splitting.
	let amt_msat = 150_000_000;
	let (route, payment_hash, payment_preimage, payment_secret) =
		get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat);

	let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
	let onion = RecipientOnionFields::secret_only(payment_secret, amt_msat);
	nodes[0].node.send_payment_with_route(route, payment_hash, onion, payment_id).unwrap();
	check_added_monitors(&nodes[0], 1);

	// Forward the first HTLC nodes[0] -> nodes[1] -> nodes[2]. Note that the second HTLC is released
	// from the holding cell during the first HTLC's commitment_signed_dance.
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let payment_event_1 = SendEvent::from_event(events.remove(0));

	nodes[1].node.handle_update_add_htlc(node_0_id, &payment_event_1.msgs[0]);
	check_added_monitors(&nodes[1], 0);
	nodes[1].node.handle_commitment_signed_batch_test(node_0_id, &payment_event_1.commitment_msg);
	check_added_monitors(&nodes[1], 1);
	let (_, raa, holding_cell_htlcs) =
		do_main_commitment_signed_dance(&nodes[1], &nodes[0], false);
	assert_eq!(holding_cell_htlcs.len(), 1);
	let payment_event_2 = holding_cell_htlcs.into_iter().next().unwrap();
	nodes[1].node.handle_revoke_and_ack(node_0_id, &raa);
	check_added_monitors(&nodes[1], 1);

	nodes[1].node.process_pending_htlc_forwards();
	check_added_monitors(&nodes[1], 1);
	let mut events = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev_1_2 = events.remove(0);
	pass_along_path(
		&nodes[1], &[&nodes[2]], amt_msat, payment_hash, Some(payment_secret), ev_1_2, false, None,
	);

	// Second HTLC: full path nodes[0] -> nodes[1] -> nodes[2]. PaymentClaimable expected at end.
	pass_along_path(
		&nodes[0], &[&nodes[1], &nodes[2]], amt_msat, payment_hash, Some(payment_secret),
		payment_event_2, true, None,
	);

	// Claim the HTLCs such that they're fully removed from the outbound edge, but disconnect
	// node_0<>node_1 so that they can't be claimed backwards by node_1.
	nodes[2].node.claim_funds(payment_preimage);
	check_added_monitors(&nodes[2], 2);
	expect_payment_claimed!(nodes[2], payment_hash, amt_msat);

	nodes[0].node.peer_disconnected(node_1_id);
	nodes[1].node.peer_disconnected(node_0_id);

	let mut events = nodes[2].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 2);
	for ev in events {
		match ev {
			MessageSendEvent::UpdateHTLCs { ref node_id, ref updates, .. } => {
				assert_eq!(*node_id, node_1_id);
				assert_eq!(updates.update_fulfill_htlcs.len(), 1);
				nodes[1].node.handle_update_fulfill_htlc(node_2_id, updates.update_fulfill_htlcs[0].clone());
				check_added_monitors(&nodes[1], 1);
				do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, false, false);
			},
			_ => panic!("Unexpected event"),
		}
	}

	let events = nodes[1].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 2);
	for event in events {
		expect_payment_forwarded(
			event, &nodes[1], &nodes[0], &nodes[2], Some(1000), None, false, false, false,
		);
	}

	// Clear the holding cell's claim entries on chan_0_1 before serialization.
	// This simulates a crash where both HTLCs were fully removed on the outbound edges but are
	// still present on the inbound edge without a resolution.
	nodes[1].node.test_clear_channel_holding_cell(node_0_id, chan_id_0_1);

	let node_1_serialized = nodes[1].node.encode();
	let mon_0_1_serialized = get_monitor!(nodes[1], chan_id_0_1).encode();
	let mon_1_2_a_serialized = get_monitor!(nodes[1], chan_id_1_2_a).encode();
	let mon_1_2_b_serialized = get_monitor!(nodes[1], chan_id_1_2_b).encode();

	reload_node!(
		nodes[1],
		node_1_serialized,
		&[&mon_0_1_serialized, &mon_1_2_a_serialized, &mon_1_2_b_serialized],
		persister,
		new_chain_monitor,
		nodes_1_deserialized,
		Some(true)
	);
	nodes[1].disable_monitor_completeness_assertion();

	// When the claims are reconstructed during reload, PaymentForwarded events are regenerated.
	let events = nodes[1].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 2);
	for event in events {
		expect_payment_forwarded(
			event, &nodes[1], &nodes[0], &nodes[2], Some(1000), None, false, false, false,
		);
	}
	// Fetching events triggers the pending monitor updates (one for each HTLC preimage) to be applied.
	check_added_monitors(&nodes[1], 2);

	// Reconnect nodes[1] to nodes[0]. Both claims should be in nodes[1]'s holding cell.
	let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[0]);
	reconnect_args.pending_cell_htlc_claims = (0, 2);
	reconnect_nodes(reconnect_args);

	// nodes[0] should now have received both fulfills and generate PaymentSent.
	expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}