lightning 0.2.2

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
// 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.

//! Tests that test the channel open process.

use crate::chain::chaininterface::LowerBoundedFeeEstimator;
use crate::chain::channelmonitor::{self, ChannelMonitorUpdateStep};
use crate::chain::transaction::OutPoint;
use crate::chain::{self, ChannelMonitorUpdateStatus};
use crate::events::{ClosureReason, Event, FundingInfo};
use crate::ln::channel::{
	get_holder_selected_channel_reserve_satoshis, ChannelError, InboundV1Channel,
	OutboundV1Channel, COINBASE_MATURITY, UNFUNDED_CHANNEL_AGE_LIMIT_TICKS,
};
use crate::ln::channelmanager::{
	self, BREAKDOWN_TIMEOUT, MAX_UNFUNDED_CHANNEL_PEERS, MAX_UNFUNDED_CHANS_PER_PEER,
};
use crate::ln::msgs::{
	AcceptChannel, BaseMessageHandler, ChannelMessageHandler, ErrorAction, MessageSendEvent,
};
use crate::ln::types::ChannelId;
use crate::ln::{functional_test_utils::*, msgs};
use crate::sign::EntropySource;
use crate::util::config::{
	ChannelConfigOverrides, ChannelConfigUpdate, ChannelHandshakeConfigUpdate, UserConfig,
};
use crate::util::errors::APIError;
use crate::util::test_utils::{self, TestLogger};

use bitcoin::constants::ChainHash;
use bitcoin::hashes::Hash;
use bitcoin::locktime::absolute::LockTime;
use bitcoin::network::Network;
use bitcoin::script::ScriptBuf;
use bitcoin::secp256k1::{PublicKey, SecretKey};
use bitcoin::transaction::Version;
use bitcoin::OutPoint as BitcoinOutPoint;
use bitcoin::{Amount, Sequence, Transaction, TxIn, TxOut, Witness};

use lightning_macros::xtest;

use lightning_types::features::ChannelTypeFeatures;

#[test]
fn test_outbound_chans_unlimited() {
	// Test that we never refuse an outbound channel even if a peer is unfuned-channel-limited
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);

	// Note that create_network connects the nodes together for us
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let node_a = nodes[0].node.get_our_node_id();
	let node_b = nodes[1].node.get_our_node_id();
	nodes[0].node.create_channel(node_b, 100_000, 0, 42, None, None).unwrap();
	let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);

	for _ in 0..MAX_UNFUNDED_CHANS_PER_PEER {
		nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a);
		open_channel_msg.common_fields.temporary_channel_id =
			ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
	}

	// Once we have MAX_UNFUNDED_CHANS_PER_PEER unfunded channels, new inbound channels will be
	// rejected.
	nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
	assert_eq!(
		get_err_msg(&nodes[1], &node_a).channel_id,
		open_channel_msg.common_fields.temporary_channel_id
	);

	// but we can still open an outbound channel.
	nodes[1].node.create_channel(node_a, 100_000, 0, 42, None, None).unwrap();
	get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, node_a);

	// but even with such an outbound channel, additional inbound channels will still fail.
	nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
	assert_eq!(
		get_err_msg(&nodes[1], &node_a).channel_id,
		open_channel_msg.common_fields.temporary_channel_id
	);
}

#[test]
fn test_0conf_limiting() {
	// Tests that we properly limit inbound channels when we have the manual-channel-acceptance
	// flag set and (sometimes) accept channels as 0conf.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let mut settings = test_default_channel_config();
	settings.manually_accept_inbound_channels = true;
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(settings)]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

	// Note that create_network connects the nodes together for us
	let node_b = nodes[1].node.get_our_node_id();
	nodes[0].node.create_channel(node_b, 100_000, 0, 42, None, None).unwrap();
	let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);
	let init_msg = &msgs::Init {
		features: nodes[0].node.init_features(),
		networks: None,
		remote_network_address: None,
	};

	// First, get us up to MAX_UNFUNDED_CHANNEL_PEERS so we can test at the edge
	for _ in 0..MAX_UNFUNDED_CHANNEL_PEERS - 1 {
		let random_pk = PublicKey::from_secret_key(
			&nodes[0].node.secp_ctx,
			&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap(),
		);
		nodes[1].node.peer_connected(random_pk, init_msg, true).unwrap();

		nodes[1].node.handle_open_channel(random_pk, &open_channel_msg);
		let events = nodes[1].node.get_and_clear_pending_events();
		match events[0] {
			Event::OpenChannelRequest { temporary_channel_id, .. } => {
				nodes[1]
					.node
					.accept_inbound_channel(&temporary_channel_id, &random_pk, 23, None)
					.unwrap();
			},
			_ => panic!("Unexpected event"),
		}
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, random_pk);
		open_channel_msg.common_fields.temporary_channel_id =
			ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
	}

	// If we try to accept a channel from another peer non-0conf it will fail.
	let last_random_pk = PublicKey::from_secret_key(
		&nodes[0].node.secp_ctx,
		&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap(),
	);
	nodes[1].node.peer_connected(last_random_pk, init_msg, true).unwrap();
	nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			match nodes[1].node.accept_inbound_channel(
				&temporary_channel_id,
				&last_random_pk,
				23,
				None,
			) {
				Err(APIError::APIMisuseError { err }) => assert_eq!(
					err,
					"Too many peers with unfunded channels, refusing to accept new ones"
				),
				_ => panic!(),
			}
		},
		_ => panic!("Unexpected event"),
	}
	assert_eq!(
		get_err_msg(&nodes[1], &last_random_pk).channel_id,
		open_channel_msg.common_fields.temporary_channel_id
	);

	// ...however if we accept the same channel 0conf it should work just fine.
	nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			nodes[1]
				.node
				.accept_inbound_channel_from_trusted_peer_0conf(
					&temporary_channel_id,
					&last_random_pk,
					23,
					None,
				)
				.unwrap();
		},
		_ => panic!("Unexpected event"),
	}
	get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
}

#[test]
fn test_inbound_anchors_manual_acceptance() {
	let mut anchors_cfg = test_default_channel_config();
	anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	do_test_manual_inbound_accept_with_override(anchors_cfg, None);
}

#[test]
fn test_inbound_anchors_manual_acceptance_overridden() {
	let overrides = ChannelConfigOverrides {
		handshake_overrides: Some(ChannelHandshakeConfigUpdate {
			max_inbound_htlc_value_in_flight_percent_of_channel: Some(5),
			htlc_minimum_msat: Some(1000),
			minimum_depth: Some(2),
			to_self_delay: Some(200),
			max_accepted_htlcs: Some(5),
			channel_reserve_proportional_millionths: Some(20000),
		}),
		update_overrides: None,
	};

	let mut anchors_cfg = test_default_channel_config();
	anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;

	let accept_message = do_test_manual_inbound_accept_with_override(anchors_cfg, Some(overrides));
	assert_eq!(accept_message.common_fields.max_htlc_value_in_flight_msat, 5_000_000);
	assert_eq!(accept_message.common_fields.htlc_minimum_msat, 1_000);
	assert_eq!(accept_message.common_fields.minimum_depth, 2);
	assert_eq!(accept_message.common_fields.to_self_delay, 200);
	assert_eq!(accept_message.common_fields.max_accepted_htlcs, 5);
	assert_eq!(accept_message.channel_reserve_satoshis, 2_000);
}

#[test]
fn test_inbound_zero_fee_commitments_manual_acceptance() {
	let mut zero_fee_cfg = test_default_channel_config();
	zero_fee_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	do_test_manual_inbound_accept_with_override(zero_fee_cfg, None);
}

fn do_test_manual_inbound_accept_with_override(
	start_cfg: UserConfig, config_overrides: Option<ChannelConfigOverrides>,
) -> AcceptChannel {
	let mut mannual_accept_cfg = start_cfg.clone();
	mannual_accept_cfg.manually_accept_inbound_channels = true;

	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(
		3,
		&node_cfgs,
		&[Some(start_cfg.clone()), Some(start_cfg.clone()), Some(mannual_accept_cfg.clone())],
	);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_a = nodes[0].node.get_our_node_id();
	let node_b = nodes[1].node.get_our_node_id();
	nodes[0].node.create_channel(node_b, 100_000, 0, 42, None, None).unwrap();
	let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);

	nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
	assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
	let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
	match &msg_events[0] {
		MessageSendEvent::HandleError { node_id, action } => {
			assert_eq!(*node_id, node_a);
			match action {
				ErrorAction::SendErrorMessage { msg } => {
					assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned())
				},
				_ => panic!("Unexpected error action"),
			}
		},
		_ => panic!("Unexpected event"),
	}

	nodes[2].node.handle_open_channel(node_a, &open_channel_msg);
	let events = nodes[2].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => nodes[2]
			.node
			.accept_inbound_channel(&temporary_channel_id, &node_a, 23, config_overrides)
			.unwrap(),
		_ => panic!("Unexpected event"),
	}
	get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, node_a)
}

#[test]
fn test_anchors_zero_fee_htlc_tx_downgrade() {
	// Tests that if both nodes support anchors, but the remote node does not want to accept
	// anchor channels at the moment, an error it sent to the local node such that it can retry
	// the channel without the anchors feature.
	let mut initiator_cfg = test_default_channel_config();
	initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;

	let mut receiver_cfg = test_default_channel_config();
	receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	receiver_cfg.manually_accept_inbound_channels = true;

	let start_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
	let end_type = ChannelTypeFeatures::only_static_remote_key();
	do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
}

#[test]
fn test_scid_privacy_downgrade() {
	// Tests downgrade from `anchors_zero_fee_commitments` with `option_scid_alias` when the
	// remote node advertises the features but does not accept the channel, asserting that
	// `option_scid_alias` is the last feature to be downgraded.
	let mut initiator_cfg = test_default_channel_config();
	initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	initiator_cfg.channel_handshake_config.negotiate_scid_privacy = true;
	initiator_cfg.channel_handshake_config.announce_for_forwarding = false;

	let mut receiver_cfg = test_default_channel_config();
	receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	receiver_cfg.channel_handshake_config.negotiate_scid_privacy = true;
	receiver_cfg.manually_accept_inbound_channels = true;

	let mut start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
	start_type.set_scid_privacy_required();
	let mut with_anchors = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
	with_anchors.set_scid_privacy_required();
	let mut with_scid_privacy = ChannelTypeFeatures::only_static_remote_key();
	with_scid_privacy.set_scid_privacy_required();
	let static_remote = ChannelTypeFeatures::only_static_remote_key();
	let downgrade_types = vec![with_anchors, with_scid_privacy, static_remote];

	do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
}

#[test]
fn test_zero_fee_commitments_downgrade() {
	// Tests that the local node will retry without zero fee commitments in the case where the
	// remote node supports the feature but does not accept it.
	let mut initiator_cfg = test_default_channel_config();
	initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;

	let mut receiver_cfg = test_default_channel_config();
	receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	receiver_cfg.manually_accept_inbound_channels = true;

	let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
	let downgrade_types = vec![
		ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(),
		ChannelTypeFeatures::only_static_remote_key(),
	];
	do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
}

#[test]
fn test_zero_fee_commitments_downgrade_to_static_remote() {
	// Tests that the local node will retry with static remote key when zero fee commitments
	// are supported (but not accepted), but not legacy anchors.
	let mut initiator_cfg = test_default_channel_config();
	initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;

	let mut receiver_cfg = test_default_channel_config();
	receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
	receiver_cfg.manually_accept_inbound_channels = true;

	let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
	let end_type = ChannelTypeFeatures::only_static_remote_key();
	do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
}

fn do_test_channel_type_downgrade(
	initiator_cfg: UserConfig, acceptor_cfg: UserConfig, start_type: ChannelTypeFeatures,
	downgrade_types: Vec<ChannelTypeFeatures>,
) {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(acceptor_cfg)]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let error_message = "Channel force-closed";

	let node_a = nodes[0].node.get_our_node_id();
	let node_b = nodes[1].node.get_our_node_id();
	nodes[0].node.create_channel(node_b, 100_000, 0, 0, None, None).unwrap();
	let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);
	assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);

	for downgrade_type in downgrade_types {
		nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
		let events = nodes[1].node.get_and_clear_pending_events();
		match events[0] {
			Event::OpenChannelRequest { temporary_channel_id, .. } => {
				nodes[1]
					.node
					.force_close_broadcasting_latest_txn(
						&temporary_channel_id,
						&node_a,
						error_message.to_string(),
					)
					.unwrap();
			},
			_ => panic!("Unexpected event"),
		}

		let error_msg = get_err_msg(&nodes[1], &node_a);
		nodes[0].node.handle_error(node_b, &error_msg);

		open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);
		let channel_type = open_channel_msg.common_fields.channel_type.as_ref().unwrap();
		assert_eq!(channel_type, &downgrade_type);

		// Since nodes[1] should not have accepted the channel, it should
		// not have generated any events.
		assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
	}
}

#[test]
fn test_no_channel_downgrade() {
	// Tests that the local node will not retry when a `option_static_remote` channel is
	// rejected by a peer that advertises support for the feature.
	let initiator_cfg = test_default_channel_config();
	let mut receiver_cfg = test_default_channel_config();
	receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
	receiver_cfg.manually_accept_inbound_channels = true;

	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(receiver_cfg)]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let error_message = "Channel force-closed";

	let node_a = nodes[0].node.get_our_node_id();
	let node_b = nodes[1].node.get_our_node_id();

	nodes[0].node.create_channel(node_b, 100_000, 0, 0, None, None).unwrap();
	let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b);
	let start_type = ChannelTypeFeatures::only_static_remote_key();
	assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);

	nodes[1].node.handle_open_channel(node_a, &open_channel_msg);
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			nodes[1]
				.node
				.force_close_broadcasting_latest_txn(
					&temporary_channel_id,
					&node_a,
					error_message.to_string(),
				)
				.unwrap();
		},
		_ => panic!("Unexpected event"),
	}

	let error_msg = get_err_msg(&nodes[1], &node_a);
	nodes[0].node.handle_error(node_b, &error_msg);

	// Since nodes[0] could not retry the channel with a different type, it should close it.
	let chan_closed_events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(chan_closed_events.len(), 1);
	if let Event::ChannelClosed { .. } = chan_closed_events[0] {
	} else {
		panic!();
	}
}

#[xtest(feature = "_externalize_tests")]
fn test_channel_resumption_fail_post_funding() {
	// If we fail to exchange funding with a peer prior to it disconnecting we'll resume the
	// channel open on reconnect, however if we do exchange funding we do not currently support
	// replaying it and here test that the channel closes.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	nodes[0].node.create_channel(node_b_id, 1_000_000, 0, 42, None, None).unwrap();
	let open_chan = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_chan);
	let accept_chan = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);
	nodes[0].node.handle_accept_channel(node_b_id, &accept_chan);

	let (temp_chan_id, tx, funding_output) =
		create_funding_transaction(&nodes[0], &node_b_id, 1_000_000, 42);
	let new_chan_id = ChannelId::v1_from_funding_outpoint(funding_output);
	nodes[0].node.funding_transaction_generated(temp_chan_id, node_b_id, tx).unwrap();

	nodes[0].node.peer_disconnected(node_b_id);
	check_closed_events(
		&nodes[0],
		&[ExpectedCloseEvent::from_id_reason(new_chan_id, true, ClosureReason::DisconnectedPeer)],
	);

	// After ddf75afd16 we'd panic on reconnection if we exchanged funding info, so test that
	// explicitly here.
	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();
	assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new());
}

#[xtest(feature = "_externalize_tests")]
pub fn test_insane_channel_opens() {
	// Stand up a network of 2 nodes
	use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
	let mut cfg = UserConfig::default();
	cfg.channel_handshake_limits.max_funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(cfg.clone())]);
	let 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();

	// Instantiate channel parameters where we push the maximum msats given our
	// funding satoshis
	let channel_value_sat = 31337; // same as funding satoshis
	let channel_reserve_satoshis =
		get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
	let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;

	// Have node0 initiate a channel to node1 with aforementioned parameters
	nodes[0].node.create_channel(node_b_id, channel_value_sat, push_msat, 42, None, None).unwrap();

	// Extract the channel open message from node0 to node1
	let open_channel_message =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	// Test helper that asserts we get the correct error string given a mutator
	// that supposedly makes the channel open message insane
	let insane_open_helper =
		|expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
			let open_channel_mutated = message_mutator(open_channel_message.clone());
			nodes[1].node.handle_open_channel(node_a_id, &open_channel_mutated);
			let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
			assert_eq!(msg_events.len(), 1);
			let expected_regex = regex::Regex::new(expected_error_str).unwrap();
			if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
				match action {
					&ErrorAction::SendErrorMessage { .. } => {
						nodes[1].logger.assert_log_regex(
							"lightning::ln::channelmanager",
							expected_regex,
							1,
						);
					},
					_ => panic!("unexpected event!"),
				}
			} else {
				assert!(false);
			}
		};

	use crate::ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;

	// Test all mutations that would make the channel open message insane
	insane_open_helper(
		format!(
			"Per our config, funding must be at most {}. It was {}",
			TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1,
			TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2
		)
		.as_str(),
		|mut msg| {
			msg.common_fields.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2;
			msg
		},
	);
	insane_open_helper(
		format!(
			"Funding must be smaller than the total bitcoin supply. It was {}",
			TOTAL_BITCOIN_SUPPLY_SATOSHIS
		)
		.as_str(),
		|mut msg| {
			msg.common_fields.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS;
			msg
		},
	);

	insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| {
		msg.channel_reserve_satoshis = msg.common_fields.funding_satoshis + 1;
		msg
	});

	insane_open_helper(
		r"push_msat \d+ was larger than channel amount minus reserve \(\d+\)",
		|mut msg| {
			msg.push_msat =
				(msg.common_fields.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1;
			msg
		},
	);

	insane_open_helper("Peer never wants payout outputs?", |mut msg| {
		msg.common_fields.dust_limit_satoshis = msg.common_fields.funding_satoshis + 1;
		msg
	});

	insane_open_helper(
		r"Minimum htlc value \(\d+\) was larger than full channel value \(\d+\)",
		|mut msg| {
			msg.common_fields.htlc_minimum_msat =
				(msg.common_fields.funding_satoshis - msg.channel_reserve_satoshis) * 1000;
			msg
		},
	);

	insane_open_helper(
		"They wanted our payments to be delayed by a needlessly long period",
		|mut msg| {
			msg.common_fields.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1;
			msg
		},
	);

	insane_open_helper("0 max_accepted_htlcs makes for a useless channel", |mut msg| {
		msg.common_fields.max_accepted_htlcs = 0;
		msg
	});

	insane_open_helper("max_accepted_htlcs was 484. It must not be larger than 483", |mut msg| {
		msg.common_fields.max_accepted_htlcs = 484;
		msg
	});
}

#[xtest(feature = "_externalize_tests")]
fn test_insane_zero_fee_channel_open() {
	let mut cfg = UserConfig::default();
	cfg.manually_accept_inbound_channels = true;
	cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;

	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[Some(cfg.clone()), Some(cfg.clone())]);
	let 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();

	nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();

	let open_channel_message =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	let insane_open_helper =
		|expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
			let open_channel_mutated = message_mutator(open_channel_message.clone());
			nodes[1].node.handle_open_channel(node_a_id, &open_channel_mutated);

			let events = nodes[1].node.get_and_clear_pending_events();
			match events[0] {
				Event::OpenChannelRequest { temporary_channel_id, .. } => {
					match nodes[1].node.accept_inbound_channel(
						&temporary_channel_id,
						&nodes[0].node.get_our_node_id(),
						23,
						None,
					) {
						Ok(_) => panic!("Unexpected successful channel accept"),
						Err(e) => assert!(format!("{:?}", e).contains(expected_error_str)),
					}
				},
				_ => panic!("Unexpected event"),
			}

			let events = nodes[1].node.get_and_clear_pending_msg_events();
			assert_eq!(events.len(), 1);
			assert!(matches!(events[0], MessageSendEvent::HandleError { .. }));
		};

	insane_open_helper(
		"max_accepted_htlcs was 115. It must not be larger than 114".into(),
		|mut msg| {
			msg.common_fields.max_accepted_htlcs = 115;
			msg
		},
	);

	insane_open_helper("Zero Fee Channels must never attempt to use a fee".into(), |mut msg| {
		msg.common_fields.commitment_feerate_sat_per_1000_weight = 123;
		msg
	});
}

#[xtest(feature = "_externalize_tests")]
pub fn test_funding_exceeds_no_wumbo_limit() {
	// Test that if a peer does not support wumbo channels, we'll refuse to open a wumbo channel to
	// them.
	use crate::ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let mut features = channelmanager::provided_init_features(&test_default_channel_config());
	features.clear_wumbo();
	*node_cfgs[1].override_init_features.borrow_mut() = Some(features);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

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

	match nodes[0].node.create_channel(
		node_b_id,
		MAX_FUNDING_SATOSHIS_NO_WUMBO + 1,
		0,
		42,
		None,
		None,
	) {
		Err(APIError::APIMisuseError { err }) => {
			let exp_err = format!(
				"funding_value must not exceed {}, it was {}",
				MAX_FUNDING_SATOSHIS_NO_WUMBO,
				MAX_FUNDING_SATOSHIS_NO_WUMBO + 1
			);
			assert_eq!(err, exp_err);
		},
		_ => panic!(),
	}
}

fn do_test_sanity_on_in_flight_opens(steps: u8) {
	// Previously, we had issues deserializing channels when we hadn't connected the first block
	// after creation. To catch that and similar issues, we lean on the Node::drop impl to test
	// serialization round-trips and simply do steps towards opening a channel and then drop the
	// Node objects.

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

	if steps & 0b1000_0000 != 0 {
		let block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
		connect_block(&nodes[0], &block);
		connect_block(&nodes[1], &block);
	}

	if steps & 0x0f == 0 {
		return;
	}
	nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	if steps & 0x0f == 1 {
		return;
	}
	nodes[1].node.handle_open_channel(node_a_id, &open_channel);
	let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);

	if steps & 0x0f == 2 {
		return;
	}
	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);

	let (temporary_channel_id, tx, _) =
		create_funding_transaction(&nodes[0], &node_b_id, 100000, 42);

	if steps & 0x0f == 3 {
		return;
	}
	nodes[0]
		.node
		.funding_transaction_generated(temporary_channel_id, node_b_id, tx.clone())
		.unwrap();
	check_added_monitors(&nodes[0], 0);
	let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);

	let channel_id = ChannelId::v1_from_funding_txid(
		funding_created.funding_txid.as_byte_array(),
		funding_created.funding_output_index,
	);

	if steps & 0x0f == 4 {
		return;
	}
	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	{
		let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, channel_id);
		added_monitors.clear();
	}
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);

	if steps & 0x0f == 5 {
		return;
	}
	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed);
	{
		let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, channel_id);
		added_monitors.clear();
	}

	expect_channel_pending_event(&nodes[0], &node_b_id);
	let events_4 = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events_4.len(), 0);

	if steps & 0x0f == 6 {
		return;
	}
	create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);

	if steps & 0x0f == 7 {
		return;
	}
	confirm_transaction_at(&nodes[0], &tx, 2);
	connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
	create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
	expect_channel_ready_event(&nodes[0], &node_b_id);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_sanity_on_in_flight_opens() {
	do_test_sanity_on_in_flight_opens(0);
	do_test_sanity_on_in_flight_opens(0 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(1);
	do_test_sanity_on_in_flight_opens(1 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(2);
	do_test_sanity_on_in_flight_opens(2 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(3);
	do_test_sanity_on_in_flight_opens(3 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(4);
	do_test_sanity_on_in_flight_opens(4 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(5);
	do_test_sanity_on_in_flight_opens(5 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(6);
	do_test_sanity_on_in_flight_opens(6 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(7);
	do_test_sanity_on_in_flight_opens(7 | 0b1000_0000);
	do_test_sanity_on_in_flight_opens(8);
	do_test_sanity_on_in_flight_opens(8 | 0b1000_0000);
}

#[xtest(feature = "_externalize_tests")]
#[should_panic]
pub fn bolt2_open_channel_sending_node_checks_part1() {
	//This test needs to be on its own as we are catching a panic
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	// Force duplicate randomness for every get-random call
	for node in nodes.iter() {
		*node.keys_manager.override_random_bytes.lock().unwrap() = Some([0; 32]);
	}

	// BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
	let channel_value_satoshis = 10000;
	let push_msat = 10001;
	nodes[0]
		.node
		.create_channel(node_b_id, channel_value_satoshis, push_msat, 42, None, None)
		.unwrap();
	let node0_to_1_send_open_channel =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &node0_to_1_send_open_channel);
	get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);

	// Create a second channel with the same random values. This used to panic due to a colliding
	// channel_id, but now panics due to a colliding outbound SCID alias.
	assert!(nodes[0]
		.node
		.create_channel(node_b_id, channel_value_satoshis, push_msat, 42, None, None)
		.is_err());
}

#[xtest(feature = "_externalize_tests")]
pub fn bolt2_open_channel_sending_node_checks_part2() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

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

	// BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
	let channel_value_satoshis = 10000;
	// Test when push_msat is equal to 1000 * funding_satoshis.
	let push_msat = 1000 * channel_value_satoshis + 1;
	assert!(nodes[0]
		.node
		.create_channel(node_b_id, channel_value_satoshis, push_msat, 42, None, None)
		.is_err());

	nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();

	let node0_to_1_send_open_channel =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	// BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
	assert!(BREAKDOWN_TIMEOUT > 0);
	assert!(node0_to_1_send_open_channel.common_fields.to_self_delay == BREAKDOWN_TIMEOUT);

	// BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
	let chain_hash = ChainHash::using_genesis_block(Network::Testnet);
	assert_eq!(node0_to_1_send_open_channel.common_fields.chain_hash, chain_hash);
}

#[xtest(feature = "_externalize_tests")]
pub fn bolt2_open_channel_sane_dust_limit() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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 value_sats = 1000000;
	let push_msat = 10001;
	nodes[0].node.create_channel(node_b_id, value_sats, push_msat, 42, None, None).unwrap();
	let mut node0_to_1_send_open_channel =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	node0_to_1_send_open_channel.common_fields.dust_limit_satoshis = 547;
	node0_to_1_send_open_channel.channel_reserve_satoshis = 100001;

	nodes[1].node.handle_open_channel(node_a_id, &node0_to_1_send_open_channel);
	let events = nodes[1].node.get_and_clear_pending_msg_events();
	let err_msg = match events[0] {
		MessageSendEvent::HandleError {
			action: ErrorAction::SendErrorMessage { ref msg }, ..
		} => msg.clone(),
		_ => panic!("Unexpected event"),
	};
	assert_eq!(
		err_msg.data,
		"dust_limit_satoshis (547) is greater than the implementation limit (546)"
	);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_user_configurable_csv_delay() {
	// We test our channel constructors yield errors when we pass them absurd csv delay

	let mut low_our_to_self_config = UserConfig::default();
	low_our_to_self_config.channel_handshake_config.our_to_self_delay = 6;
	let mut high_their_to_self_config = UserConfig::default();
	high_their_to_self_config.channel_handshake_limits.their_to_self_delay = 100;
	let user_cfgs = [Some(high_their_to_self_config.clone()), None];
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
	let 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 logger = TestLogger::new();

	// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
	if let Err(error) = OutboundV1Channel::new(
		&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
		&nodes[0].keys_manager,
		&nodes[0].keys_manager,
		node_b_id,
		&nodes[1].node.init_features(),
		1000000,
		1000000,
		0,
		&low_our_to_self_config,
		0,
		42,
		None,
		&logger,
	) {
		match error {
			APIError::APIMisuseError { err } => {
				assert!(regex::Regex::new(
					r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks"
				)
				.unwrap()
				.is_match(err.as_str()));
			},
			_ => panic!("Unexpected event"),
		}
	} else {
		assert!(false)
	}

	// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in InboundV1Channel::new()
	nodes[1].node.create_channel(node_a_id, 1000000, 1000000, 42, None, None).unwrap();
	let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, node_a_id);
	open_channel.common_fields.to_self_delay = 200;
	if let Err(error) = InboundV1Channel::new(
		&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
		&nodes[0].keys_manager,
		&nodes[0].keys_manager,
		node_b_id,
		&nodes[0].node.channel_type_features(),
		&nodes[1].node.init_features(),
		&open_channel,
		0,
		&low_our_to_self_config,
		0,
		&nodes[0].logger,
		/*is_0conf=*/ false,
	) {
		match error {
			ChannelError::Close((err, _)) => {
				let regex = regex::Regex::new(
					r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks",
				)
				.unwrap();
				assert!(regex.is_match(err.as_str()));
			},
			_ => panic!("Unexpected event"),
		}
	} else {
		assert!(false);
	}

	// We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
	nodes[0].node.create_channel(node_b_id, 1000000, 1000000, 42, None, None).unwrap();
	let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_channel);

	let mut accept_channel =
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);
	accept_channel.common_fields.to_self_delay = 200;
	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);
	let reason_msg;
	if let MessageSendEvent::HandleError { ref action, .. } =
		nodes[0].node.get_and_clear_pending_msg_events()[0]
	{
		match action {
			&ErrorAction::SendErrorMessage { ref msg } => {
				assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(msg.data.as_str()));
				reason_msg = msg.data.clone();
			},
			_ => {
				panic!();
			},
		}
	} else {
		panic!();
	}
	let reason = ClosureReason::ProcessingError { err: reason_msg };
	check_closed_event!(nodes[0], 1, reason, [node_b_id], 1000000);

	// We test msg.to_self_delay <= config.their_to_self_delay is enforced in InboundV1Channel::new()
	nodes[1].node.create_channel(node_a_id, 1000000, 1000000, 42, None, None).unwrap();
	let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, node_a_id);
	open_channel.common_fields.to_self_delay = 200;
	if let Err(error) = InboundV1Channel::new(
		&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator::new(253)),
		&nodes[0].keys_manager,
		&nodes[0].keys_manager,
		node_b_id,
		&nodes[0].node.channel_type_features(),
		&nodes[1].node.init_features(),
		&open_channel,
		0,
		&high_their_to_self_config,
		0,
		&nodes[0].logger,
		/*is_0conf=*/ false,
	) {
		match error {
			ChannelError::Close((err, _)) => {
				let regex = regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap();
				assert!(regex.is_match(err.as_str()));
			},
			_ => panic!("Unexpected event"),
		}
	} else {
		assert!(false);
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_manually_accept_inbound_channel_request() {
	let mut manually_accept_conf = UserConfig::default();
	manually_accept_conf.manually_accept_inbound_channels = true;
	manually_accept_conf.channel_handshake_config.minimum_depth = 1;

	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
	let 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();

	nodes[0]
		.node
		.create_channel(node_b_id, 100000, 10001, 42, None, Some(manually_accept_conf))
		.unwrap();
	let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &res);

	// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
	// accepting the inbound channel request.
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	let config_overrides = ChannelConfigOverrides {
		handshake_overrides: Some(ChannelHandshakeConfigUpdate {
			max_inbound_htlc_value_in_flight_percent_of_channel: None,
			htlc_minimum_msat: None,
			minimum_depth: None,
			to_self_delay: None,
			max_accepted_htlcs: Some(3),
			channel_reserve_proportional_millionths: None,
		}),
		update_overrides: Some(ChannelConfigUpdate {
			forwarding_fee_proportional_millionths: None,
			forwarding_fee_base_msat: Some(555),
			cltv_expiry_delta: None,
			max_dust_htlc_exposure_msat: None,
			force_close_avoidance_max_fee_satoshis: None,
			accept_underpaying_htlcs: None,
		}),
	};
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			let config = Some(config_overrides);
			nodes[1]
				.node
				.accept_inbound_channel(&temporary_channel_id, &node_a_id, 23, config)
				.unwrap();
		},
		_ => panic!("Unexpected event"),
	}

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

	let ref accept_channel: AcceptChannel;
	match accept_msg_ev[0] {
		MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => {
			assert_eq!(*node_id, node_a_id);

			// Assert overriden handshake parameter.
			assert_eq!(msg.common_fields.max_accepted_htlcs, 3);

			accept_channel = msg;
		},
		_ => panic!("Unexpected event"),
	}

	// Continue channel opening process until channel update messages are sent.
	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);
	let (temp_channel_id, tx, funding_outpoint) =
		create_funding_transaction(&nodes[0], &node_b_id, 100_000, 42);
	nodes[0]
		.node
		.unsafe_manual_funding_transaction_generated(temp_channel_id, node_b_id, funding_outpoint)
		.unwrap();
	check_added_monitors(&nodes[0], 0);

	let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);
	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed);
	check_added_monitors(&nodes[0], 1);
	let events = &nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 2);
	match &events[0] {
		crate::events::Event::FundingTxBroadcastSafe { funding_txo, .. } => {
			assert_eq!(funding_txo.txid, funding_outpoint.txid);
			assert_eq!(funding_txo.vout, funding_outpoint.index.into());
		},
		_ => panic!("Unexpected event"),
	};
	match &events[1] {
		crate::events::Event::ChannelPending { counterparty_node_id, .. } => {
			assert_eq!(node_b_id, *counterparty_node_id);
		},
		_ => panic!("Unexpected event"),
	};

	mine_transaction(&nodes[0], &tx);
	mine_transaction(&nodes[1], &tx);

	let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, node_a_id);
	nodes[1].node.handle_channel_ready(node_a_id, &as_channel_ready);
	let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, node_b_id);
	nodes[0].node.handle_channel_ready(node_b_id, &as_channel_ready);

	expect_channel_ready_event(&nodes[0], &node_b_id);
	expect_channel_ready_event(&nodes[1], &node_a_id);

	// Assert that the overriden base fee surfaces in the channel update.
	let channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, node_a_id);
	assert_eq!(channel_update.contents.fee_base_msat, 555);

	get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, node_b_id);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_manually_reject_inbound_channel_request() {
	let mut manually_accept_conf = UserConfig::default();
	manually_accept_conf.manually_accept_inbound_channels = true;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
	let 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();

	nodes[0]
		.node
		.create_channel(node_b_id, 100000, 10001, 42, None, Some(manually_accept_conf))
		.unwrap();
	let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &res);

	// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
	// rejecting the inbound channel request.
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	let err = "Channel force-closed".to_string();
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			nodes[1]
				.node
				.force_close_broadcasting_latest_txn(&temporary_channel_id, &node_a_id, err)
				.unwrap();
		},
		_ => panic!("Unexpected event"),
	}

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

	match close_msg_ev[0] {
		MessageSendEvent::HandleError { ref node_id, .. } => {
			assert_eq!(*node_id, node_a_id);
		},
		_ => panic!("Unexpected event"),
	}

	// There should be no more events to process, as the channel was never opened.
	assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
}

#[xtest(feature = "_externalize_tests")]
pub fn test_can_not_accept_inbound_channel_twice() {
	let mut manually_accept_conf = UserConfig::default();
	manually_accept_conf.manually_accept_inbound_channels = true;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs =
		create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
	let 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();

	nodes[0]
		.node
		.create_channel(node_b_id, 100000, 10001, 42, None, Some(manually_accept_conf))
		.unwrap();
	let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &res);

	// Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
	// accepting the inbound channel request.
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());

	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			nodes[1]
				.node
				.accept_inbound_channel(&temporary_channel_id, &node_a_id, 0, None)
				.unwrap();
			let api_res =
				nodes[1].node.accept_inbound_channel(&temporary_channel_id, &node_a_id, 0, None);
			match api_res {
				Err(APIError::APIMisuseError { err }) => {
					assert_eq!(err, "No such channel awaiting to be accepted.");
				},
				Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
				Err(e) => panic!("Unexpected Error {:?}", e),
			}
		},
		_ => panic!("Unexpected event"),
	}

	// Ensure that the channel wasn't closed after attempting to accept it twice.
	let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(accept_msg_ev.len(), 1);

	match accept_msg_ev[0] {
		MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
			assert_eq!(*node_id, node_a_id);
		},
		_ => panic!("Unexpected event"),
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_can_not_accept_unknown_inbound_channel() {
	let chanmon_cfg = create_chanmon_cfgs(2);
	let node_cfg = create_node_cfgs(2, &chanmon_cfg);
	let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
	let nodes = create_network(2, &node_cfg, &node_chanmgr);

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

	let unknown_channel_id = ChannelId::new_zero();
	let api_res = nodes[0].node.accept_inbound_channel(&unknown_channel_id, &node_b_id, 0, None);
	match api_res {
		Err(APIError::APIMisuseError { err }) => {
			assert_eq!(err, "No such channel awaiting to be accepted.");
		},
		Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
		Err(e) => panic!("Unexpected Error: {:?}", e),
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_duplicate_temporary_channel_id_from_different_peers() {
	// Tests that we can accept two different `OpenChannel` requests with the same
	// `temporary_channel_id`, as long as they are from different peers.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &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 node_c_id = nodes[2].node.get_our_node_id();

	// Create an first channel channel
	nodes[1].node.create_channel(node_a_id, 100000, 10001, 42, None, None).unwrap();
	let mut open_chan_msg_chan_1_0 =
		get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, node_a_id);

	// Create an second channel
	nodes[2].node.create_channel(node_a_id, 100000, 10001, 43, None, None).unwrap();
	let mut open_chan_msg_chan_2_0 =
		get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, node_a_id);

	// Modify the `OpenChannel` from `nodes[2]` to `nodes[0]` to ensure that it uses the same
	// `temporary_channel_id` as the `OpenChannel` from nodes[1] to nodes[0].
	open_chan_msg_chan_2_0.common_fields.temporary_channel_id =
		open_chan_msg_chan_1_0.common_fields.temporary_channel_id;

	// Assert that `nodes[0]` can accept both `OpenChannel` requests, even though they use the same
	// `temporary_channel_id` as they are from different peers.
	nodes[0].node.handle_open_channel(node_b_id, &open_chan_msg_chan_1_0);
	{
		let events = nodes[0].node.get_and_clear_pending_msg_events();
		assert_eq!(events.len(), 1);
		match &events[0] {
			MessageSendEvent::SendAcceptChannel { node_id, msg } => {
				assert_eq!(node_id, &node_b_id);
				assert_eq!(
					msg.common_fields.temporary_channel_id,
					open_chan_msg_chan_1_0.common_fields.temporary_channel_id
				);
			},
			_ => panic!("Unexpected event"),
		}
	}

	nodes[0].node.handle_open_channel(node_c_id, &open_chan_msg_chan_2_0);
	{
		let events = nodes[0].node.get_and_clear_pending_msg_events();
		assert_eq!(events.len(), 1);
		match &events[0] {
			MessageSendEvent::SendAcceptChannel { node_id, msg } => {
				assert_eq!(node_id, &node_c_id);
				assert_eq!(
					msg.common_fields.temporary_channel_id,
					open_chan_msg_chan_1_0.common_fields.temporary_channel_id
				);
			},
			_ => panic!("Unexpected event"),
		}
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_duplicate_funding_err_in_funding() {
	// Test that if we have a live channel with one peer, then another peer comes along and tries
	// to create a second channel with the same txid we'll fail and not overwrite the
	// outpoint_to_peer map in `ChannelManager`.
	//
	// This was previously broken.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);

	let node_b_id = nodes[1].node.get_our_node_id();
	let node_c_id = nodes[2].node.get_our_node_id();

	let (_, _, _, real_channel_id, funding_tx) = create_chan_between_nodes(&nodes[0], &nodes[1]);
	let real_chan_funding_txo =
		chain::transaction::OutPoint { txid: funding_tx.compute_txid(), index: 0 };
	assert_eq!(ChannelId::v1_from_funding_outpoint(real_chan_funding_txo), real_channel_id);

	nodes[2].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
	let mut open_chan_msg = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, node_b_id);
	let node_c_temp_chan_id = open_chan_msg.common_fields.temporary_channel_id;
	open_chan_msg.common_fields.temporary_channel_id = real_channel_id;
	nodes[1].node.handle_open_channel(node_c_id, &open_chan_msg);
	let mut accept_chan_msg =
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_c_id);
	accept_chan_msg.common_fields.temporary_channel_id = node_c_temp_chan_id;
	nodes[2].node.handle_accept_channel(node_b_id, &accept_chan_msg);

	// Now that we have a second channel with the same funding txo, send a bogus funding message
	// and let nodes[1] remove the inbound channel.
	let (_, fund_tx, _) = create_funding_transaction(&nodes[2], &node_b_id, 100_000, 42);

	nodes[2].node.funding_transaction_generated(node_c_temp_chan_id, node_b_id, fund_tx).unwrap();

	let mut funding_created_msg =
		get_event_msg!(nodes[2], MessageSendEvent::SendFundingCreated, node_b_id);
	funding_created_msg.temporary_channel_id = real_channel_id;
	// Make the signature invalid by changing the funding output
	funding_created_msg.funding_output_index += 10;
	nodes[1].node.handle_funding_created(node_c_id, &funding_created_msg);
	get_err_msg(&nodes[1], &node_c_id);
	let err = "Invalid funding_created signature from peer".to_owned();
	let reason = ClosureReason::ProcessingError { err };
	let expected_closing = ExpectedCloseEvent::from_id_reason(real_channel_id, false, reason);
	check_closed_events(&nodes[1], &[expected_closing]);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_duplicate_chan_id() {
	// Test that if a given peer tries to open a channel with the same channel_id as one that is
	// already open we reject it and keep the old channel.
	//
	// Previously, full_stack_target managed to figure out that if you tried to open two channels
	// with the same funding output (ie post-funding channel_id), we'd create a monitor update for
	// the existing channel when we detect the duplicate new channel, screwing up our monitor
	// updating logic for the existing channel.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	// Create an initial channel
	nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_chan_msg);
	nodes[0].node.handle_accept_channel(
		node_b_id,
		&get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id),
	);

	// Try to create a second channel with the same temporary_channel_id as the first and check
	// that it is rejected.
	nodes[1].node.handle_open_channel(node_a_id, &open_chan_msg);
	{
		let events = nodes[1].node.get_and_clear_pending_msg_events();
		assert_eq!(events.len(), 1);
		match events[0] {
			MessageSendEvent::HandleError {
				action: ErrorAction::SendErrorMessage { ref msg },
				node_id,
			} => {
				// Technically, at this point, nodes[1] would be justified in thinking both the
				// first (valid) and second (invalid) channels are closed, given they both have
				// the same non-temporary channel_id. However, currently we do not, so we just
				// move forward with it.
				assert_eq!(msg.channel_id, open_chan_msg.common_fields.temporary_channel_id);
				assert_eq!(node_id, node_a_id);
			},
			_ => panic!("Unexpected event"),
		}
	}

	// Move the first channel through the funding flow...
	let (temp_channel_id, tx, _) = create_funding_transaction(&nodes[0], &node_b_id, 100000, 42);

	nodes[0].node.funding_transaction_generated(temp_channel_id, node_b_id, tx.clone()).unwrap();
	check_added_monitors(&nodes[0], 0);

	let mut funding_created_msg =
		get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);
	let channel_id = ChannelId::v1_from_funding_txid(
		funding_created_msg.funding_txid.as_byte_array(),
		funding_created_msg.funding_output_index,
	);

	nodes[1].node.handle_funding_created(node_a_id, &funding_created_msg);
	{
		let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, channel_id);
		added_monitors.clear();
	}
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed_msg =
		get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);

	let funding_outpoint = crate::chain::transaction::OutPoint {
		txid: funding_created_msg.funding_txid,
		index: funding_created_msg.funding_output_index,
	};
	let channel_id = ChannelId::v1_from_funding_outpoint(funding_outpoint);

	// Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
	// temporary one).

	// First try to open a second channel with a temporary channel id equal to the txid-based one.
	// Technically this is allowed by the spec, but we don't support it and there's little reason
	// to. Still, it shouldn't cause any other issues.
	open_chan_msg.common_fields.temporary_channel_id = channel_id;
	nodes[1].node.handle_open_channel(node_a_id, &open_chan_msg);
	{
		let events = nodes[1].node.get_and_clear_pending_msg_events();
		assert_eq!(events.len(), 1);
		match events[0] {
			MessageSendEvent::HandleError {
				action: ErrorAction::SendErrorMessage { ref msg },
				node_id,
			} => {
				// Technically, at this point, nodes[1] would be justified in thinking both
				// channels are closed, but currently we do not, so we just move forward with it.
				assert_eq!(msg.channel_id, open_chan_msg.common_fields.temporary_channel_id);
				assert_eq!(node_id, node_a_id);
			},
			_ => panic!("Unexpected event"),
		}
	}

	// Now try to create a second channel which has a duplicate funding output.
	nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let open_chan_2_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_chan_2_msg);
	nodes[0].node.handle_accept_channel(
		node_b_id,
		&get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id),
	);
	create_funding_transaction(&nodes[0], &node_b_id, 100000, 42); // Get and check the FundingGenerationReady event

	let funding_created = {
		let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
		let mut a_peer_state = per_peer_state.get(&node_b_id).unwrap().lock().unwrap();
		// Once we call `get_funding_created` the channel has a duplicate channel_id as
		// another channel in the ChannelManager - an invalid state. Thus, we'd panic later when we
		// try to create another channel. Instead, we drop the channel entirely here (leaving the
		// channelmanager in a possibly nonsense state instead).
		let chan_id = open_chan_2_msg.common_fields.temporary_channel_id;
		let mut channel = a_peer_state.channel_by_id.remove(&chan_id).unwrap();

		if let Some(mut chan) = channel.as_unfunded_outbound_v1_mut() {
			let logger = test_utils::TestLogger::new();
			chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger)
				.map_err(|_| ())
				.unwrap()
		} else {
			panic!("Unexpected Channel phase")
		}
		.unwrap()
	};
	check_added_monitors(&nodes[0], 0);
	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	// At this point we'll look up if the channel_id is present and immediately fail the channel
	// without trying to persist the `ChannelMonitor`.
	check_added_monitors(&nodes[1], 0);

	let reason = ClosureReason::ProcessingError {
		err: "Already had channel with the new channel_id".to_owned(),
	};
	let close_event =
		ExpectedCloseEvent::from_id_reason(funding_created.temporary_channel_id, false, reason);
	check_closed_events(&nodes[1], &[close_event]);

	// ...still, nodes[1] will reject the duplicate channel.
	{
		let events = nodes[1].node.get_and_clear_pending_msg_events();
		assert_eq!(events.len(), 1);
		match events[0] {
			MessageSendEvent::HandleError {
				action: ErrorAction::SendErrorMessage { ref msg },
				node_id,
			} => {
				// Technically, at this point, nodes[1] would be justified in thinking both
				// channels are closed, but currently we do not, so we just move forward with it.
				assert_eq!(msg.channel_id, funding_created.temporary_channel_id);
				assert_eq!(node_id, node_a_id);
			},
			_ => panic!("Unexpected event"),
		}
	}

	// finally, finish creating the original channel and send a payment over it to make sure
	// everything is functional.
	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed_msg);
	{
		let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
		assert_eq!(added_monitors.len(), 1);
		assert_eq!(added_monitors[0].0, channel_id);
		added_monitors.clear();
	}
	expect_channel_pending_event(&nodes[0], &node_b_id);

	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], tx);

	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);
	update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);

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

#[xtest(feature = "_externalize_tests")]
pub fn test_invalid_funding_tx() {
	// Test that we properly handle invalid funding transactions sent to us from a peer.
	//
	// Previously, all other major lightning implementations had failed to properly sanitize
	// funding transactions from their counterparties, leading to a multi-implementation critical
	// security vulnerability (though we always sanitized properly, we've previously had
	// un-released crashes in the sanitization process).
	//
	// Further, if the funding transaction is consensus-valid, confirms, and is later spent, we'd
	// previously have crashed in `ChannelMonitor` even though we closed the channel as bogus and
	// gave up on it. We test this here by generating such a transaction.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	nodes[0].node.create_channel(node_b_id, 100_000, 10_000, 42, None, None).unwrap();
	nodes[1].node.handle_open_channel(
		node_a_id,
		&get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id),
	);
	nodes[0].node.handle_accept_channel(
		node_b_id,
		&get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id),
	);

	let (temporary_channel_id, mut tx, _) =
		create_funding_transaction(&nodes[0], &node_b_id, 100_000, 42);

	// Create a witness program which can be spent by a 4-empty-stack-elements witness and which is
	// 136 bytes long. This matches our "accepted HTLC preimage spend" matching, previously causing
	// a panic as we'd try to extract a 32 byte preimage from a witness element without checking
	// its length.
	let mut wit_program: Vec<u8> =
		channelmonitor::deliberately_bogus_accepted_htlc_witness_program();
	let wit_program_script: ScriptBuf = wit_program.into();
	for output in tx.output.iter_mut() {
		// Make the confirmed funding transaction have a bogus script_pubkey
		output.script_pubkey = ScriptBuf::new_p2wsh(&wit_program_script.wscript_hash());
	}

	nodes[0]
		.node
		.funding_transaction_generated_unchecked(temporary_channel_id, node_b_id, tx.clone(), 0)
		.unwrap();
	nodes[1].node.handle_funding_created(
		node_a_id,
		&get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id),
	);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	nodes[0].node.handle_funding_signed(
		node_b_id,
		&get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id),
	);
	check_added_monitors(&nodes[0], 1);
	expect_channel_pending_event(&nodes[0], &node_b_id);

	let events_1 = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events_1.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], tx);
	nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();

	let expected_err = "funding tx had wrong script/value or output index";
	confirm_transaction_at(&nodes[1], &tx, 1);

	let reason = ClosureReason::ProcessingError { err: expected_err.to_string() };
	check_closed_event!(nodes[1], 1, reason, [node_a_id], 100000);

	check_added_monitors(&nodes[1], 1);
	let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
	assert_eq!(events_2.len(), 1);
	if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
		assert_eq!(*node_id, node_a_id);
		if let msgs::ErrorAction::SendErrorMessage { msg } = action {
			assert_eq!(
				msg.data,
				"Channel closed because of an exception: ".to_owned() + expected_err
			);
		} else {
			panic!();
		}
	} else {
		panic!();
	}
	assert_eq!(nodes[1].node.list_channels().len(), 0);

	// Now confirm a spend of the (bogus) funding transaction. As long as the witness is 5 elements
	// long the ChannelMonitor will try to read 32 bytes from the second-to-last element, panicing
	// as its not 32 bytes long.
	let mut spend_tx = Transaction {
		version: Version::TWO,
		lock_time: LockTime::ZERO,
		input: tx
			.output
			.iter()
			.enumerate()
			.map(|(idx, _)| TxIn {
				previous_output: BitcoinOutPoint { txid: tx.compute_txid(), vout: idx as u32 },
				script_sig: ScriptBuf::new(),
				sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
				witness: Witness::from_slice(
					&channelmonitor::deliberately_bogus_accepted_htlc_witness(),
				),
			})
			.collect(),
		output: vec![TxOut { value: Amount::from_sat(1000), script_pubkey: ScriptBuf::new() }],
	};
	check_spends!(spend_tx, tx);
	mine_transaction(&nodes[1], &spend_tx);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_coinbase_funding_tx() {
	// Miners are able to fund channels directly from coinbase transactions, however
	// by consensus rules, outputs of a coinbase transaction are encumbered by a 100
	// block maturity timelock. To ensure that a (non-0conf) channel like this is enforceable
	// on-chain, the minimum depth is updated to 100 blocks for coinbase funding transactions.
	//
	// Note that 0conf channels with coinbase funding transactions are unaffected and are
	// immediately operational after opening.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &open_channel);
	let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);

	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);

	// Create the coinbase funding transaction.
	let (channel_id, tx, _) =
		create_coinbase_funding_transaction(&nodes[0], &node_b_id, 100000, 42);

	nodes[0].node.funding_transaction_generated(channel_id, node_b_id, tx.clone()).unwrap();
	check_added_monitors(&nodes[0], 0);
	let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);

	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);

	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed);
	check_added_monitors(&nodes[0], 1);

	expect_channel_pending_event(&nodes[0], &node_b_id);
	assert!(nodes[0].node.get_and_clear_pending_events().is_empty());

	// Starting at height 0, we "confirm" the coinbase at height 1.
	confirm_transaction_at(&nodes[0], &tx, 1);
	// We connect 98 more blocks to have 99 confirmations for the coinbase transaction.
	connect_blocks(&nodes[0], COINBASE_MATURITY - 2);
	// Check that we have no pending message events (we have not queued a `channel_ready` yet).
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
	// Now connect one more block which results in 100 confirmations of the coinbase transaction.
	connect_blocks(&nodes[0], 1);
	// There should now be a `channel_ready` which can be handled.
	let _ = &nodes[1].node.handle_channel_ready(
		node_a_id,
		&get_event_msg!(&nodes[0], MessageSendEvent::SendChannelReady, node_b_id),
	);

	confirm_transaction_at(&nodes[1], &tx, 1);
	connect_blocks(&nodes[1], COINBASE_MATURITY - 2);
	assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
	connect_blocks(&nodes[1], 1);
	expect_channel_ready_event(&nodes[1], &node_a_id);
	create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_non_final_funding_tx() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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 temp_channel_id =
		nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
	let open_channel_message =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_channel_message);
	let accept_channel_message =
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);
	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel_message);

	let best_height = nodes[0].node.best_block.read().unwrap().height;

	let chan_id = *nodes[0].network_chan_count.borrow();
	let events = nodes[0].node.get_and_clear_pending_events();
	let input = TxIn {
		previous_output: BitcoinOutPoint::null(),
		script_sig: bitcoin::ScriptBuf::new(),
		sequence: Sequence(1),
		witness: Witness::from_slice(&[&[1]]),
	};
	assert_eq!(events.len(), 1);
	let mut tx = match events[0] {
		Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
			// Timelock the transaction _beyond_ the best client height + 1.
			Transaction {
				version: Version(chan_id as i32),
				lock_time: LockTime::from_height(best_height + 2).unwrap(),
				input: vec![input],
				output: vec![TxOut {
					value: Amount::from_sat(*channel_value_satoshis),
					script_pubkey: output_script.clone(),
				}],
			}
		},
		_ => panic!("Unexpected event"),
	};
	// Transaction should fail as it's evaluated as non-final for propagation.
	match nodes[0].node.funding_transaction_generated(temp_channel_id, node_b_id, tx.clone()) {
		Err(APIError::APIMisuseError { err }) => {
			assert_eq!(format!("Funding transaction absolute timelock is non-final"), err);
		},
		_ => panic!(),
	}
	let err = "Error in transaction funding: Misuse error: Funding transaction absolute timelock is non-final";
	let reason = ClosureReason::ProcessingError { err: err.to_owned() };
	let event = ExpectedCloseEvent::from_id_reason(temp_channel_id, false, reason);
	check_closed_events(&nodes[0], &[event]);
	assert_eq!(get_err_msg(&nodes[0], &node_b_id).data, err);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_non_final_funding_tx_within_headroom() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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 temp_channel_id =
		nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
	let open_channel_message =
		get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	nodes[1].node.handle_open_channel(node_a_id, &open_channel_message);
	let accept_channel_message =
		get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);
	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel_message);

	let best_height = nodes[0].node.best_block.read().unwrap().height;

	let chan_id = *nodes[0].network_chan_count.borrow();
	let events = nodes[0].node.get_and_clear_pending_events();
	let input = TxIn {
		previous_output: BitcoinOutPoint::null(),
		script_sig: bitcoin::ScriptBuf::new(),
		sequence: Sequence(1),
		witness: Witness::from_slice(&[[1]]),
	};
	assert_eq!(events.len(), 1);
	let mut tx = match events[0] {
		Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
			// Timelock the transaction within a +1 headroom from the best block.
			Transaction {
				version: Version(chan_id as i32),
				lock_time: LockTime::from_consensus(best_height + 1),
				input: vec![input],
				output: vec![TxOut {
					value: Amount::from_sat(*channel_value_satoshis),
					script_pubkey: output_script.clone(),
				}],
			}
		},
		_ => panic!("Unexpected event"),
	};

	// Transaction should be accepted if it's in a +1 headroom from best block.
	nodes[0].node.funding_transaction_generated(temp_channel_id, node_b_id, tx.clone()).unwrap();
	get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_channel_close_when_not_timely_accepted() {
	// Create network of two nodes
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	// Simulate peer-disconnects mid-handshake
	// The channel is initiated from the node 0 side,
	// but the nodes disconnect before node 1 could send accept channel
	let create_chan_id =
		nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	assert_eq!(open_channel_msg.common_fields.temporary_channel_id, create_chan_id);

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

	// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
	assert_eq!(nodes[0].node.list_channels().len(), 1);

	// Since channel was inbound from node[1] perspective, it should have been dropped immediately.
	assert_eq!(nodes[1].node.list_channels().len(), 0);

	// In the meantime, some time passes.
	for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS {
		nodes[0].node.timer_tick_occurred();
	}

	// Since we disconnected from peer and did not connect back within time,
	// we should have forced-closed the channel by now.
	let reason = ClosureReason::FundingTimedOut;
	check_closed_event!(nodes[0], 1, reason, [node_b_id], 100000);
	assert_eq!(nodes[0].node.list_channels().len(), 0);

	{
		// Since accept channel message was never received
		// The channel should be forced close by now from node 0 side
		// and the peer removed from per_peer_state
		let node_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
		assert_eq!(node_0_per_peer_state.len(), 0);
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_rebroadcast_open_channel_when_reconnect_mid_handshake() {
	// Create network of two nodes
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let 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();

	// Simulate peer-disconnects mid-handshake
	// The channel is initiated from the node 0 side,
	// but the nodes disconnect before node 1 could send accept channel
	let create_chan_id =
		nodes[0].node.create_channel(node_b_id, 100000, 10001, 42, None, None).unwrap();
	let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
	assert_eq!(open_channel_msg.common_fields.temporary_channel_id, create_chan_id);

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

	// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
	assert_eq!(nodes[0].node.list_channels().len(), 1);

	// Since channel was inbound from node[1] perspective, it should have been immediately dropped.
	assert_eq!(nodes[1].node.list_channels().len(), 0);

	// The peers now reconnect
	let init_msg = msgs::Init {
		features: nodes[0].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, false).unwrap();

	// Make sure the SendOpenChannel message is added to node_0 pending message events
	let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(msg_events.len(), 1);
	match &msg_events[0] {
		MessageSendEvent::SendOpenChannel { msg, .. } => assert_eq!(msg, &open_channel_msg),
		_ => panic!("Unexpected message."),
	}
}

#[xtest(feature = "_externalize_tests")]
pub fn test_batch_channel_open() {
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &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 node_c_id = nodes[2].node.get_our_node_id();

	// 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(node_a_id, &funding_created_msgs[0]);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed_msg =
		get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	nodes[0].node.handle_funding_signed(node_b_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_broadcasted.lock().unwrap().len(), 0);

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

	let funding_signed_msg =
		get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, node_a_id);
	chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	nodes[0].node.handle_funding_signed(node_c_id, &funding_signed_msg);
	check_added_monitors(&nodes[0], 1);

	// The transaction should not have been broadcast before persisting all monitors has been
	// completed.
	assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
	assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);

	// Complete the persistence of the monitor.
	nodes[0].chain_monitor.complete_sole_pending_chan_update(&ChannelId::v1_from_funding_outpoint(
		OutPoint { txid: tx.compute_txid(), index: 1 },
	));
	let events = nodes[0].node.get_and_clear_pending_events();

	// The transaction should only have been broadcast now.
	let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
	assert_eq!(broadcasted_txs.len(), 1);
	assert_eq!(broadcasted_txs[0], tx);

	assert_eq!(events.len(), 2);
	assert!(events.iter().any(|e| matches!(
		*e,
		crate::events::Event::ChannelPending {
			ref counterparty_node_id,
			..
		} if counterparty_node_id == &node_b_id,
	)));
	assert!(events.iter().any(|e| matches!(
		*e,
		crate::events::Event::ChannelPending {
			ref counterparty_node_id,
			..
		} if counterparty_node_id == &node_c_id,
	)));
}

#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
	// This test ensures that if one of the channels
	// in the batch closes, the complete batch will close.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &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();

	// 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(node_a_id, &funding_created_msgs[0]);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed_msg =
		get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	nodes[0].node.handle_funding_signed(node_b_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);

	// Force-close the channel for which we've completed the initial monitor.
	let funding_txo_1 = OutPoint { txid: tx.compute_txid(), index: 0 };
	let funding_txo_2 = OutPoint { txid: tx.compute_txid(), index: 1 };
	let channel_id_1 = ChannelId::v1_from_funding_outpoint(funding_txo_1);
	let channel_id_2 = ChannelId::v1_from_funding_outpoint(funding_txo_2);
	let err = "Channel force-closed".to_string();
	nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &node_b_id, err).unwrap();

	// 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 { .. }
		));
	}

	let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
	match msg_events[0] {
		MessageSendEvent::HandleError { .. } => (),
		_ => panic!("Unexpected message."),
	}

	// Because the funding was never broadcasted, we should never bother to broadcast the
	// commitment transactions either.
	let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
	assert_eq!(broadcasted_txs.len(), 0);

	// All channels in the batch should close immediately.
	check_closed_events(
		&nodes[0],
		&[
			ExpectedCloseEvent {
				channel_id: Some(channel_id_1),
				discard_funding: true,
				channel_funding_txo: Some(funding_txo_1),
				user_channel_id: Some(42),
				..Default::default()
			},
			ExpectedCloseEvent {
				channel_id: Some(channel_id_2),
				discard_funding: true,
				channel_funding_txo: Some(funding_txo_2),
				user_channel_id: Some(43),
				..Default::default()
			},
		],
	);

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

#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &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 node_c_id = nodes[2].node.get_our_node_id();

	// 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(node_a_id, &funding_created_msgs[0]);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed_msg =
		get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed_msg);
	check_added_monitors(&nodes[0], 1);

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

	let funding_signed_msg =
		get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, node_a_id);
	chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
	nodes[0].node.handle_funding_signed(node_c_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);

	// Force-close the channel for which we've completed the initial monitor.
	let funding_txo_1 = OutPoint { txid: tx.compute_txid(), index: 0 };
	let funding_txo_2 = OutPoint { txid: tx.compute_txid(), index: 1 };
	let channel_id_1 = ChannelId::v1_from_funding_outpoint(funding_txo_1);
	let channel_id_2 = ChannelId::v1_from_funding_outpoint(funding_txo_2);
	let err = "Channel force-closed".to_string();
	nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &node_b_id, err).unwrap();
	check_added_monitors(&nodes[0], 2);
	{
		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 { .. }
		));
		let monitor_updates_2 = monitor_updates.get(&channel_id_2).unwrap();
		assert_eq!(monitor_updates_2.len(), 1);
		assert_eq!(monitor_updates_2[0].updates.len(), 1);
		assert!(matches!(
			monitor_updates_2[0].updates[0],
			ChannelMonitorUpdateStep::ChannelForceClosed { .. }
		));
	}
	let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
	match msg_events[0] {
		MessageSendEvent::HandleError { .. } => (),
		_ => panic!("Unexpected message."),
	}

	// Because the funding was never broadcasted, we should never bother to broadcast the
	// commitment transactions either.
	let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
	assert_eq!(broadcasted_txs.len(), 0);

	// All channels in the batch should close immediately.
	check_closed_events(
		&nodes[0],
		&[
			ExpectedCloseEvent {
				channel_id: Some(channel_id_1),
				discard_funding: true,
				channel_funding_txo: Some(funding_txo_1),
				user_channel_id: Some(42),
				..Default::default()
			},
			ExpectedCloseEvent {
				channel_id: Some(channel_id_2),
				discard_funding: true,
				channel_funding_txo: Some(funding_txo_2),
				user_channel_id: Some(43),
				..Default::default()
			},
		],
	);

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

#[xtest(feature = "_externalize_tests")]
pub fn test_funding_and_commitment_tx_confirm_same_block() {
	// Tests that a node will forget the channel (when it only requires 1 confirmation) if the
	// funding and commitment transaction confirm in the same block.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let mut min_depth_1_block_cfg = test_default_channel_config();
	min_depth_1_block_cfg.channel_handshake_config.minimum_depth = 1;
	let node_chanmgrs = create_node_chanmgrs(
		2,
		&node_cfgs,
		&[Some(min_depth_1_block_cfg.clone()), Some(min_depth_1_block_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 funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
	let chan_id = ChannelId::v1_from_funding_outpoint(chain::transaction::OutPoint {
		txid: funding_tx.compute_txid(),
		index: 0,
	});

	assert_eq!(nodes[0].node.list_channels().len(), 1);
	assert_eq!(nodes[1].node.list_channels().len(), 1);

	let commitment_tx = {
		let mon = get_monitor!(nodes[0], chan_id);
		let mut txn = mon.unsafe_get_latest_holder_commitment_txn(&nodes[0].logger);
		assert_eq!(txn.len(), 1);
		txn.pop().unwrap()
	};

	mine_transactions(&nodes[0], &[&funding_tx, &commitment_tx]);
	mine_transactions(&nodes[1], &[&funding_tx, &commitment_tx]);

	let check_msg_events = |node: &Node| {
		let mut msg_events = node.node.get_and_clear_pending_msg_events();
		assert_eq!(msg_events.len(), 3, "{msg_events:?}");
		if let MessageSendEvent::SendChannelReady { .. } = msg_events.remove(0) {
		} else {
			panic!();
		}
		if let MessageSendEvent::HandleError {
			action: msgs::ErrorAction::SendErrorMessage { .. },
			node_id: _,
		} = msg_events.remove(0)
		{
		} else {
			panic!();
		}
		if let MessageSendEvent::BroadcastChannelUpdate { ref msg } = msg_events.remove(0) {
			assert_eq!(msg.contents.channel_flags & 2, 2);
		} else {
			panic!();
		}
	};
	check_msg_events(&nodes[0]);
	check_added_monitors(&nodes[0], 1);
	let reason = ClosureReason::CommitmentTxConfirmed;
	check_closed_event(&nodes[0], 1, reason, false, &[node_b_id], 1_000_000);

	check_msg_events(&nodes[1]);
	check_added_monitors(&nodes[1], 1);
	let reason = ClosureReason::CommitmentTxConfirmed;
	check_closed_event(&nodes[1], 1, reason, false, &[node_a_id], 1_000_000);

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

#[xtest(feature = "_externalize_tests")]
pub fn test_accept_inbound_channel_errors_queued() {
	// For manually accepted inbound channels, tests that a close error is correctly handled
	// and the channel fails for the initiator.
	let mut config0 = test_default_channel_config();
	let mut config1 = config0.clone();
	config1.channel_handshake_limits.their_to_self_delay = 1000;
	config1.manually_accept_inbound_channels = true;
	config0.channel_handshake_config.our_to_self_delay = 2000;

	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config0), Some(config1)]);
	let 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();

	nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
	let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &open_channel_msg);
	let events = nodes[1].node.get_and_clear_pending_events();
	match events[0] {
		Event::OpenChannelRequest { temporary_channel_id, .. } => {
			match nodes[1].node.accept_inbound_channel(&temporary_channel_id, &node_a_id, 23, None)
			{
				Err(APIError::ChannelUnavailable { err: _ }) => (),
				_ => panic!(),
			}
		},
		_ => panic!("Unexpected event"),
	}
	assert_eq!(
		get_err_msg(&nodes[1], &node_a_id).channel_id,
		open_channel_msg.common_fields.temporary_channel_id
	);
}

#[xtest(feature = "_externalize_tests")]
pub fn test_manual_funding_abandon() {
	let mut cfg = UserConfig::default();
	cfg.channel_handshake_config.minimum_depth = 1;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(cfg.clone()), Some(cfg)]);
	let 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();

	assert!(nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).is_ok());
	let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &open_channel);
	let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);

	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);
	let (temp_channel_id, _tx, funding_outpoint) =
		create_funding_transaction(&nodes[0], &node_b_id, 100_000, 42);
	nodes[0]
		.node
		.unsafe_manual_funding_transaction_generated(temp_channel_id, node_b_id, funding_outpoint)
		.unwrap();
	check_added_monitors(&nodes[0], 0);

	let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);
	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	let err = msgs::ErrorMessage { channel_id: funding_signed.channel_id, data: "".to_string() };
	nodes[0].node.handle_error(node_b_id, &err);

	let close_events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(close_events.len(), 2);
	assert!(close_events.iter().any(|ev| matches!(ev, Event::ChannelClosed { .. })));
	assert!(close_events.iter().any(|ev| match ev {
		Event::DiscardFunding { channel_id, funding_info: FundingInfo::OutPoint { outpoint } } => {
			assert_eq!(*channel_id, err.channel_id);
			assert_eq!(*outpoint, funding_outpoint);
			true
		},
		_ => false,
	}));
}

#[xtest(feature = "_externalize_tests")]
pub fn test_funding_signed_event() {
	let mut cfg = UserConfig::default();
	cfg.channel_handshake_config.minimum_depth = 1;
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(cfg.clone()), Some(cfg)]);
	let 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();

	assert!(nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).is_ok());
	let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	nodes[1].node.handle_open_channel(node_a_id, &open_channel);
	let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, node_a_id);

	nodes[0].node.handle_accept_channel(node_b_id, &accept_channel);
	let (temp_channel_id, tx, funding_outpoint) =
		create_funding_transaction(&nodes[0], &node_b_id, 100_000, 42);
	nodes[0]
		.node
		.unsafe_manual_funding_transaction_generated(temp_channel_id, node_b_id, funding_outpoint)
		.unwrap();
	check_added_monitors(&nodes[0], 0);

	let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, node_b_id);
	nodes[1].node.handle_funding_created(node_a_id, &funding_created);
	check_added_monitors(&nodes[1], 1);
	expect_channel_pending_event(&nodes[1], &node_a_id);

	let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, node_a_id);
	nodes[0].node.handle_funding_signed(node_b_id, &funding_signed);
	check_added_monitors(&nodes[0], 1);
	let events = &nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 2);
	match &events[0] {
		crate::events::Event::FundingTxBroadcastSafe { funding_txo, .. } => {
			assert_eq!(funding_txo.txid, funding_outpoint.txid);
			assert_eq!(funding_txo.vout, funding_outpoint.index.into());
		},
		_ => panic!("Unexpected event"),
	};
	match &events[1] {
		crate::events::Event::ChannelPending { counterparty_node_id, .. } => {
			assert_eq!(node_b_id, *counterparty_node_id);
		},
		_ => panic!("Unexpected event"),
	};

	mine_transaction(&nodes[0], &tx);
	mine_transaction(&nodes[1], &tx);

	let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, node_a_id);
	nodes[1].node.handle_channel_ready(node_a_id, &as_channel_ready);
	let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, node_b_id);
	nodes[0].node.handle_channel_ready(node_b_id, &as_channel_ready);

	expect_channel_ready_event(&nodes[0], &node_b_id);
	expect_channel_ready_event(&nodes[1], &node_a_id);
	nodes[0].node.get_and_clear_pending_msg_events();
	nodes[1].node.get_and_clear_pending_msg_events();
}

#[xtest(feature = "_externalize_tests")]
fn test_fund_pending_channel() {
	// Previously, we would panic if a user called `batch_funding_transaction_generated` for a
	// channel which wasn't ready to receive funding. While this isn't a major bug - users
	// shouldn't do that - we shouldn't panic and should instead return an error.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

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

	nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
	let open_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);

	let tx = Transaction {
		version: Version(2),
		lock_time: LockTime::ZERO,
		input: vec![],
		output: vec![],
	};
	let pending_chan = [(&open_msg.common_fields.temporary_channel_id, &node_b_id)];
	let res = nodes[0].node.batch_funding_transaction_generated(&pending_chan, tx);
	if let Err(APIError::APIMisuseError { err }) = res {
		assert_eq!(err, "Channel f7fee84016d554015f5166c0a0df6479942ef55fd70713883b0493493a38e13a with counterparty 0355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23 is not an unfunded, outbound channel ready to fund");
	} else {
		panic!("Unexpected result {res:?}");
	}
	get_err_msg(&nodes[0], &node_b_id);
	let reason = ClosureReason::ProcessingError {
		err: "Error in transaction funding: Misuse error: Channel f7fee84016d554015f5166c0a0df6479942ef55fd70713883b0493493a38e13a with counterparty 0355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c23 is not an unfunded, outbound channel ready to fund".to_owned(),
	};
	check_closed_event!(nodes[0], 1, reason, [node_b_id], 100_000);
}