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
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
// 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.

//! LDK sends, receives, and forwards onion messages via this [`OnionMessenger`], which lives here,
//! as well as various types, traits, and utilities that it uses.

use bitcoin::hashes::hmac::{Hmac, HmacEngine};
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::{Hash, HashEngine};
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};

use super::async_payments::{AsyncPaymentsMessage, AsyncPaymentsMessageHandler};
use super::dns_resolution::{DNSResolverMessage, DNSResolverMessageHandler};
use super::offers::{OffersMessage, OffersMessageHandler};
use super::packet::OnionMessageContents;
use super::packet::ParsedOnionMessageContents;
use super::packet::{
	ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, BIG_PACKET_HOP_DATA_LEN,
	SMALL_PACKET_HOP_DATA_LEN,
};
use crate::blinded_path::message::{
	AsyncPaymentsContext, BlindedMessagePath, DNSResolverContext, ForwardTlvs, MessageContext,
	MessageForwardNode, NextMessageHop, OffersContext, ReceiveTlvs,
};
use crate::blinded_path::utils;
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
use crate::ln::msgs::{
	self, BaseMessageHandler, MessageSendEvent, OnionMessage, OnionMessageHandler, SocketAddress,
};
use crate::ln::onion_utils;
use crate::routing::gossip::{NetworkGraph, NodeId, ReadOnlyNetworkGraph};
use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
use crate::types::features::{InitFeatures, NodeFeatures};
use crate::util::async_poll::{MultiResultFuturePoller, ResultFuture};
use crate::util::logger::{Logger, WithContext};
use crate::util::ser::Writeable;
use crate::util::wakers::{Future, Notifier};

use crate::io;
use crate::prelude::*;
use crate::sync::Mutex;
use core::fmt;
use core::ops::Deref;
use core::sync::atomic::{AtomicBool, Ordering};

#[cfg(not(c_bindings))]
use {
	crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager},
	crate::ln::peer_handler::IgnoringMessageHandler,
	crate::sign::KeysManager,
	crate::sync::Arc,
};

pub(super) const MAX_TIMER_TICKS: usize = 2;

/// A trivial trait which describes any [`OnionMessenger`].
///
/// This is not exported to bindings users as general cover traits aren't useful in other
/// languages.
pub trait AOnionMessenger {
	/// A type implementing [`EntropySource`]
	type EntropySource: EntropySource + ?Sized;
	/// A type that may be dereferenced to [`Self::EntropySource`]
	type ES: Deref<Target = Self::EntropySource>;
	/// A type implementing [`NodeSigner`]
	type NodeSigner: NodeSigner + ?Sized;
	/// A type that may be dereferenced to [`Self::NodeSigner`]
	type NS: Deref<Target = Self::NodeSigner>;
	/// A type implementing [`Logger`]
	type Logger: Logger + ?Sized;
	/// A type that may be dereferenced to [`Self::Logger`]
	type L: Deref<Target = Self::Logger>;
	/// A type implementing [`NodeIdLookUp`]
	type NodeIdLookUp: NodeIdLookUp + ?Sized;
	/// A type that may be dereferenced to [`Self::NodeIdLookUp`]
	type NL: Deref<Target = Self::NodeIdLookUp>;
	/// A type implementing [`MessageRouter`]
	type MessageRouter: MessageRouter + ?Sized;
	/// A type that may be dereferenced to [`Self::MessageRouter`]
	type MR: Deref<Target = Self::MessageRouter>;
	/// A type implementing [`OffersMessageHandler`]
	type OffersMessageHandler: OffersMessageHandler + ?Sized;
	/// A type that may be dereferenced to [`Self::OffersMessageHandler`]
	type OMH: Deref<Target = Self::OffersMessageHandler>;
	/// A type implementing [`AsyncPaymentsMessageHandler`]
	type AsyncPaymentsMessageHandler: AsyncPaymentsMessageHandler + ?Sized;
	/// A type that may be dereferenced to [`Self::AsyncPaymentsMessageHandler`]
	type APH: Deref<Target = Self::AsyncPaymentsMessageHandler>;
	/// A type implementing [`DNSResolverMessageHandler`]
	type DNSResolverMessageHandler: DNSResolverMessageHandler + ?Sized;
	/// A type that may be dereferenced to [`Self::DNSResolverMessageHandler`]
	type DRH: Deref<Target = Self::DNSResolverMessageHandler>;
	/// A type implementing [`CustomOnionMessageHandler`]
	type CustomOnionMessageHandler: CustomOnionMessageHandler + ?Sized;
	/// A type that may be dereferenced to [`Self::CustomOnionMessageHandler`]
	type CMH: Deref<Target = Self::CustomOnionMessageHandler>;
	/// Returns a reference to the actual [`OnionMessenger`] object.
	fn get_om(
		&self,
	) -> &OnionMessenger<
		Self::ES,
		Self::NS,
		Self::L,
		Self::NL,
		Self::MR,
		Self::OMH,
		Self::APH,
		Self::DRH,
		Self::CMH,
	>;
}

impl<
		ES: Deref,
		NS: Deref,
		L: Deref,
		NL: Deref,
		MR: Deref,
		OMH: Deref,
		APH: Deref,
		DRH: Deref,
		CMH: Deref,
	> AOnionMessenger for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	type EntropySource = ES::Target;
	type ES = ES;
	type NodeSigner = NS::Target;
	type NS = NS;
	type Logger = L::Target;
	type L = L;
	type NodeIdLookUp = NL::Target;
	type NL = NL;
	type MessageRouter = MR::Target;
	type MR = MR;
	type OffersMessageHandler = OMH::Target;
	type OMH = OMH;
	type AsyncPaymentsMessageHandler = APH::Target;
	type APH = APH;
	type DNSResolverMessageHandler = DRH::Target;
	type DRH = DRH;
	type CustomOnionMessageHandler = CMH::Target;
	type CMH = CMH;
	fn get_om(&self) -> &OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH> {
		self
	}
}

/// A sender, receiver and forwarder of [`OnionMessage`]s.
///
/// # Handling Messages
///
/// `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding
/// messages to peers or delegating to the appropriate handler for the message type. Currently, the
/// available handlers are:
/// * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s
/// * [`CustomOnionMessageHandler`], for handling user-defined message types
///
/// # Sending Messages
///
/// [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling
/// a message, the matched handler may return a response message which `OnionMessenger` will send
/// on its behalf.
///
/// # Example
///
/// ```
/// # extern crate bitcoin;
/// # use bitcoin::hashes::_export::_core::time::Duration;
/// # use bitcoin::hex::FromHex;
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey, self};
/// # use lightning::blinded_path::EmptyNodeIdLookUp;
/// # use lightning::blinded_path::message::{BlindedMessagePath, MessageForwardNode, MessageContext};
/// # use lightning::sign::{EntropySource, KeysManager};
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
/// # use lightning::onion_message::messenger::{Destination, MessageRouter, MessageSendInstructions, OnionMessagePath, OnionMessenger};
/// # use lightning::onion_message::packet::OnionMessageContents;
/// # use lightning::sign::{NodeSigner, ReceiveAuthKey};
/// # use lightning::util::logger::{Logger, Record};
/// # use lightning::util::ser::{Writeable, Writer};
/// # use lightning::io;
/// # use std::sync::Arc;
/// # struct FakeLogger;
/// # impl Logger for FakeLogger {
/// #     fn log(&self, record: Record) { println!("{:?}" , record); }
/// # }
/// # struct FakeMessageRouter {}
/// # impl MessageRouter for FakeMessageRouter {
/// #     fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
/// #         let secp_ctx = Secp256k1::new();
/// #         let node_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
/// #         let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
/// #         let hop_node_id2 = hop_node_id1;
/// #         Ok(OnionMessagePath {
/// #             intermediate_nodes: vec![hop_node_id1, hop_node_id2],
/// #             destination,
/// #             first_node_addresses: Vec::new(),
/// #         })
/// #     }
/// #     fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
/// #         &self, _recipient: PublicKey, _local_node_receive_key: ReceiveAuthKey,
/// #         _context: MessageContext, _peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>
/// #     ) -> Result<Vec<BlindedMessagePath>, ()> {
/// #         unreachable!()
/// #     }
/// # }
/// # let seed = [42u8; 32];
/// # let time = Duration::from_secs(123456);
/// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos(), true);
/// # let logger = Arc::new(FakeLogger {});
/// # let node_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
/// # let secp_ctx = Secp256k1::new();
/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
/// # let (hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1);
/// # let destination_node_id = hop_node_id1;
/// # let node_id_lookup = EmptyNodeIdLookUp {};
/// # let message_router = Arc::new(FakeMessageRouter {});
/// # let custom_message_handler = IgnoringMessageHandler {};
/// # let offers_message_handler = IgnoringMessageHandler {};
/// # let async_payments_message_handler = IgnoringMessageHandler {};
/// # let dns_resolution_message_handler = IgnoringMessageHandler {};
/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
/// // ChannelManager.
/// let onion_messenger = OnionMessenger::new(
///     &keys_manager, &keys_manager, logger, &node_id_lookup, message_router,
///     &offers_message_handler, &async_payments_message_handler, &dns_resolution_message_handler,
///     &custom_message_handler,
/// );
///
/// # #[derive(Debug)]
/// # struct YourCustomMessage {}
/// impl Writeable for YourCustomMessage {
/// 	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
/// 		# Ok(())
/// 		// Write your custom onion message to `w`
/// 	}
/// }
/// impl OnionMessageContents for YourCustomMessage {
/// 	fn tlv_type(&self) -> u64 {
/// 		# let your_custom_message_type = 42;
/// 		your_custom_message_type
/// 	}
/// 	fn msg_type(&self) -> &'static str { "YourCustomMessageType" }
/// }
/// // Send a custom onion message to a node id.
/// let destination = Destination::Node(destination_node_id);
/// let instructions = MessageSendInstructions::WithoutReplyPath { destination };
/// # let message = YourCustomMessage {};
/// onion_messenger.send_onion_message(message, instructions);
///
/// // Create a blinded path to yourself, for someone to send an onion message to.
/// # let your_node_id = hop_node_id1;
/// let hops = [
/// 	MessageForwardNode { node_id: hop_node_id3, short_channel_id: None },
/// 	MessageForwardNode { node_id: hop_node_id4, short_channel_id: None },
/// ];
/// let context = MessageContext::Custom(Vec::new());
/// let receive_key = keys_manager.get_receive_auth_key();
/// let blinded_path = BlindedMessagePath::new(&hops, your_node_id, receive_key, context, &keys_manager, &secp_ctx);
///
/// // Send a custom onion message to a blinded path.
/// let destination = Destination::BlindedPath(blinded_path);
/// let instructions = MessageSendInstructions::WithoutReplyPath { destination };
/// # let message = YourCustomMessage {};
/// onion_messenger.send_onion_message(message, instructions);
/// ```
///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
pub struct OnionMessenger<
	ES: Deref,
	NS: Deref,
	L: Deref,
	NL: Deref,
	MR: Deref,
	OMH: Deref,
	APH: Deref,
	DRH: Deref,
	CMH: Deref,
> where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	entropy_source: ES,
	#[cfg(test)]
	pub(super) node_signer: NS,
	#[cfg(not(test))]
	node_signer: NS,
	logger: L,
	message_recipients: Mutex<HashMap<PublicKey, OnionMessageRecipient>>,
	secp_ctx: Secp256k1<secp256k1::All>,
	node_id_lookup: NL,
	message_router: MR,
	offers_handler: OMH,
	#[allow(unused)]
	async_payments_handler: APH,
	dns_resolver_handler: DRH,
	custom_handler: CMH,
	intercept_messages_for_offline_peers: bool,
	pending_intercepted_msgs_events: Mutex<Vec<Event>>,
	pending_peer_connected_events: Mutex<Vec<Event>>,
	pending_events_processor: AtomicBool,
	/// A [`Notifier`] used to wake up the background processor in case we have any [`Event`]s for
	/// it to give to users.
	event_notifier: Notifier,
}

/// [`OnionMessage`]s buffered to be sent.
enum OnionMessageRecipient {
	/// Messages for a node connected as a peer.
	ConnectedPeer(VecDeque<OnionMessage>),

	/// Messages for a node that is not yet connected, which are dropped after [`MAX_TIMER_TICKS`]
	/// and tracked here.
	PendingConnection(VecDeque<OnionMessage>, Option<Vec<SocketAddress>>, usize),
}

impl OnionMessageRecipient {
	fn pending_connection(addresses: Vec<SocketAddress>) -> Self {
		Self::PendingConnection(VecDeque::new(), Some(addresses), 0)
	}

	fn pending_messages(&self) -> &VecDeque<OnionMessage> {
		match self {
			OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
			OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
		}
	}

	fn enqueue_message(&mut self, message: OnionMessage) {
		let pending_messages = match self {
			OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
			OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
		};

		pending_messages.push_back(message);
	}

	fn dequeue_message(&mut self) -> Option<OnionMessage> {
		let pending_messages = match self {
			OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
			OnionMessageRecipient::PendingConnection(pending_messages, _, _) => {
				debug_assert!(false);
				pending_messages
			},
		};

		pending_messages.pop_front()
	}

	#[cfg(test)]
	fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
		let pending_messages = match self {
			OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
			OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
		};

		core::mem::take(pending_messages)
	}

	fn mark_connected(&mut self) {
		if let OnionMessageRecipient::PendingConnection(pending_messages, _, _) = self {
			let mut new_pending_messages = VecDeque::new();
			core::mem::swap(pending_messages, &mut new_pending_messages);
			*self = OnionMessageRecipient::ConnectedPeer(new_pending_messages);
		}
	}

	fn is_connected(&self) -> bool {
		match self {
			OnionMessageRecipient::ConnectedPeer(..) => true,
			OnionMessageRecipient::PendingConnection(..) => false,
		}
	}
}

/// The `Responder` struct creates an appropriate [`ResponseInstruction`] for responding to a
/// message.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Responder {
	/// The path along which a response can be sent.
	reply_path: BlindedMessagePath,
}

impl_writeable_tlv_based!(Responder, {
	(0, reply_path, required),
});

impl Responder {
	/// Creates a new [`Responder`] instance with the provided reply path.
	pub(super) fn new(reply_path: BlindedMessagePath) -> Self {
		Responder { reply_path }
	}

	/// Creates a [`ResponseInstruction`] for responding without including a reply path.
	///
	/// Use when the recipient doesn't need to send back a reply to us.
	pub fn respond(self) -> ResponseInstruction {
		ResponseInstruction {
			destination: Destination::BlindedPath(self.reply_path),
			context: None,
		}
	}

	/// Creates a [`ResponseInstruction`] for responding including a reply path.
	///
	/// Use when the recipient needs to send back a reply to us.
	pub fn respond_with_reply_path(self, context: MessageContext) -> ResponseInstruction {
		ResponseInstruction {
			destination: Destination::BlindedPath(self.reply_path),
			context: Some(context),
		}
	}

	/// Converts a [`Responder`] into its inner [`BlindedMessagePath`].
	pub(crate) fn into_blinded_path(self) -> BlindedMessagePath {
		self.reply_path
	}
}

/// Instructions for how and where to send the response to an onion message.
#[derive(Clone)]
pub struct ResponseInstruction {
	/// The destination in a response is always a [`Destination::BlindedPath`] but using a
	/// [`Destination`] rather than an explicit [`BlindedMessagePath`] simplifies the logic in
	/// [`OnionMessenger::send_onion_message_internal`] somewhat.
	destination: Destination,
	context: Option<MessageContext>,
}

impl ResponseInstruction {
	/// Converts this [`ResponseInstruction`] into a [`MessageSendInstructions`] so that it can be
	/// used to send the response via a normal message sending method.
	pub fn into_instructions(self) -> MessageSendInstructions {
		MessageSendInstructions::ForReply { instructions: self }
	}
}

/// Instructions for how and where to send a message.
#[derive(Clone)]
pub enum MessageSendInstructions {
	/// Indicates that a message should be sent including the provided reply path for the recipient
	/// to respond.
	WithSpecifiedReplyPath {
		/// The destination where we need to send our message.
		destination: Destination,
		/// The reply path which should be included in the message.
		reply_path: BlindedMessagePath,
	},
	/// Indicates that a message should be sent including a reply path for the recipient to
	/// respond.
	WithReplyPath {
		/// The destination where we need to send our message.
		destination: Destination,
		/// The context to include in the reply path we'll give the recipient so they can respond
		/// to us.
		context: MessageContext,
	},
	/// Indicates that a message should be sent without including a reply path, preventing the
	/// recipient from responding.
	WithoutReplyPath {
		/// The destination where we need to send our message.
		destination: Destination,
	},
	/// Indicates that a message is being sent as a reply to a received message.
	ForReply {
		/// The instructions provided by the [`Responder`].
		instructions: ResponseInstruction,
	},
	/// Indicates that this onion message did not originate from our node and is being forwarded
	/// through us from another node on the network to the destination.
	///
	/// We separate out this case because forwarded onion messages are treated differently from
	/// outbound onion messages initiated by our node. Outbounds are buffered internally, whereas, for
	/// DoS protection, forwards should never be buffered internally and instead will either be
	/// dropped or generate an [`Event::OnionMessageIntercepted`] if the next-hop node is
	/// disconnected.
	ForwardedMessage {
		/// The destination where we need to send the forwarded onion message.
		destination: Destination,
		/// The reply path which should be included in the message, that terminates at the original
		/// sender of this forwarded message.
		reply_path: Option<BlindedMessagePath>,
	},
}

/// A trait defining behavior for routing an [`OnionMessage`].
pub trait MessageRouter {
	/// Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
	fn find_path(
		&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
	) -> Result<OnionMessagePath, ()>;

	/// Creates [`BlindedMessagePath`]s to the `recipient` node. The nodes in `peers` are assumed to
	/// be direct peers with the `recipient`.
	fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
		&self, recipient: PublicKey, local_node_receive_key: ReceiveAuthKey,
		context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
	) -> Result<Vec<BlindedMessagePath>, ()>;
}

/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
///
/// [`DefaultMessageRouter`] constructs compact [`BlindedMessagePath`]s on a best-effort basis.
/// That is, if appropriate SCID information is available for the intermediate peers, it will
/// default to creating compact paths.
///
/// # Compact Blinded Paths
///
/// Compact blinded paths use short channel IDs (SCIDs) instead of pubkeys, resulting in smaller
/// serialization. This is particularly useful when encoding data into space-constrained formats
/// such as QR codes. The SCID is communicated via a [`MessageForwardNode`], but may be `None`
/// to allow for graceful degradation.
///
/// **Note:**
/// If any SCID in the blinded path becomes invalid, the entire compact blinded path may fail to route.
/// For the immediate hop, this can happen if the corresponding channel is closed.
/// For other intermediate hops, it can happen if the channel is closed or modified (e.g., due to splicing).
///
/// # Privacy
///
/// Creating [`BlindedMessagePath`]s may affect privacy since, if a suitable path cannot be found,
/// it will create a one-hop path using the recipient as the introduction node if it is an announced
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
/// message, and thus an `Err` is returned.
pub struct DefaultMessageRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	network_graph: G,
	entropy_source: ES,
}

// Target total length (in hops) for non-compact blinded paths.
// We pad with dummy hops until the path reaches this length,
// obscuring the recipient's true position.
//
// Compact paths are optimized for minimal size, so we avoid
// adding dummy hops to them.
pub(crate) const PADDED_PATH_LENGTH: usize = 4;

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> DefaultMessageRouter<G, L, ES>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	/// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
	pub fn new(network_graph: G, entropy_source: ES) -> Self {
		Self { network_graph, entropy_source }
	}

	pub(crate) fn create_blinded_paths_from_iter<
		I: ExactSizeIterator<Item = MessageForwardNode>,
		T: secp256k1::Signing + secp256k1::Verification,
	>(
		network_graph: &G, recipient: PublicKey, local_node_receive_key: ReceiveAuthKey,
		context: MessageContext, peers: I, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
		compact_paths: bool,
	) -> Result<Vec<BlindedMessagePath>, ()> {
		// Limit the number of blinded paths that are computed.
		const MAX_PATHS: usize = 3;

		// Ensure peers have at least three channels so that it is more difficult to infer the
		// recipient's node_id.
		const MIN_PEER_CHANNELS: usize = 3;

		let network_graph = network_graph.deref().read_only();
		let is_recipient_announced =
			network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));

		let has_one_peer = peers.len() == 1;
		let mut peer_info = peers
			.map(|peer| MessageForwardNode {
				short_channel_id: if compact_paths { peer.short_channel_id } else { None },
				..peer
			})
			// Limit to peers with announced channels unless the recipient is unannounced.
			.filter_map(|peer| {
				network_graph
					.node(&NodeId::from_pubkey(&peer.node_id))
					.filter(|info| {
						!is_recipient_announced || info.channels.len() >= MIN_PEER_CHANNELS
					})
					.map(|info| (peer, info.is_tor_only(), info.channels.len()))
					// Allow messages directly with the only peer when unannounced.
					.or_else(|| (!is_recipient_announced && has_one_peer).then(|| (peer, false, 0)))
			})
			// Exclude Tor-only nodes when the recipient is announced.
			.filter(|(_, is_tor_only, _)| !(*is_tor_only && is_recipient_announced))
			.collect::<Vec<_>>();

		// Prefer using non-Tor nodes with the most channels as the introduction node.
		peer_info.sort_unstable_by(|(_, a_tor_only, a_channels), (_, b_tor_only, b_channels)| {
			a_tor_only.cmp(b_tor_only).then(a_channels.cmp(b_channels).reverse())
		});

		let build_path = |intermediate_hops: &[MessageForwardNode]| {
			let dummy_hops_count = if compact_paths {
				0
			} else {
				// Add one for the final recipient TLV
				PADDED_PATH_LENGTH.saturating_sub(intermediate_hops.len() + 1)
			};

			BlindedMessagePath::new_with_dummy_hops(
				intermediate_hops,
				recipient,
				dummy_hops_count,
				local_node_receive_key,
				context.clone(),
				&**entropy_source,
				secp_ctx,
			)
		};

		// Try to create paths from peer info, fall back to direct path if needed
		let mut paths = peer_info
			.into_iter()
			.map(|(peer, _, _)| build_path(&[peer]))
			.take(MAX_PATHS)
			.collect::<Vec<_>>();
		if paths.is_empty() {
			if is_recipient_announced {
				paths = vec![build_path(&[])];
			} else {
				return Err(());
			}
		}

		// Sanity check: Ones the paths are created for the non-compact case, ensure
		// each of them are of the length `PADDED_PATH_LENGTH`.
		if !compact_paths {
			debug_assert!(paths.iter().all(|path| path.blinded_hops().len() == PADDED_PATH_LENGTH));
		}

		if compact_paths {
			for path in &mut paths {
				path.use_compact_introduction_node(&network_graph);
			}
		}

		Ok(paths)
	}

	pub(crate) fn find_path(
		network_graph: &G, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination,
	) -> Result<OnionMessagePath, ()> {
		let network_graph = network_graph.deref().read_only();
		destination.resolve(&network_graph);

		let first_node = match destination.first_node() {
			Some(first_node) => first_node,
			None => return Err(()),
		};

		if peers.contains(&first_node) || sender == first_node {
			Ok(OnionMessagePath {
				intermediate_nodes: vec![],
				destination,
				first_node_addresses: vec![],
			})
		} else {
			let node_details = network_graph
				.node(&NodeId::from_pubkey(&first_node))
				.and_then(|node_info| node_info.announcement_info.as_ref())
				.map(|announcement_info| {
					(announcement_info.features(), announcement_info.addresses())
				});

			match node_details {
				Some((features, addresses))
					if features.supports_onion_messages() && addresses.len() > 0 =>
				{
					Ok(OnionMessagePath {
						intermediate_nodes: vec![],
						destination,
						first_node_addresses: addresses.to_vec(),
					})
				},
				None => {
					// If the destination is an unannounced node, they may be a known peer that is offline and
					// can be woken by the sender.
					Ok(OnionMessagePath {
						intermediate_nodes: vec![],
						destination,
						first_node_addresses: vec![],
					})
				},
				_ => Err(()),
			}
		}
	}
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter
	for DefaultMessageRouter<G, L, ES>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	fn find_path(
		&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
	) -> Result<OnionMessagePath, ()> {
		Self::find_path(&self.network_graph, sender, peers, destination)
	}

	fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
		&self, recipient: PublicKey, local_node_receive_key: ReceiveAuthKey,
		context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
	) -> Result<Vec<BlindedMessagePath>, ()> {
		Self::create_blinded_paths_from_iter(
			&self.network_graph,
			recipient,
			local_node_receive_key,
			context,
			peers.into_iter(),
			&self.entropy_source,
			secp_ctx,
			true,
		)
	}
}

/// This message router is similar to [`DefaultMessageRouter`], but it always creates
/// full-length blinded paths, using the peer's [`NodeId`].
///
/// This message router can only route to a directly connected [`Destination`].
///
/// # Privacy
///
/// Creating [`BlindedMessagePath`]s may affect privacy since, if a suitable path cannot be found,
/// it will create a one-hop path using the recipient as the introduction node if it is an announced
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
/// message, and thus an `Err` is returned.
pub struct NodeIdMessageRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	network_graph: G,
	entropy_source: ES,
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> NodeIdMessageRouter<G, L, ES>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	/// Creates a [`NodeIdMessageRouter`] using the given [`NetworkGraph`].
	pub fn new(network_graph: G, entropy_source: ES) -> Self {
		Self { network_graph, entropy_source }
	}
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter
	for NodeIdMessageRouter<G, L, ES>
where
	L::Target: Logger,
	ES::Target: EntropySource,
{
	fn find_path(
		&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
	) -> Result<OnionMessagePath, ()> {
		DefaultMessageRouter::<G, L, ES>::find_path(&self.network_graph, sender, peers, destination)
	}

	fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
		&self, recipient: PublicKey, local_node_receive_key: ReceiveAuthKey,
		context: MessageContext, peers: Vec<MessageForwardNode>, secp_ctx: &Secp256k1<T>,
	) -> Result<Vec<BlindedMessagePath>, ()> {
		DefaultMessageRouter::create_blinded_paths_from_iter(
			&self.network_graph,
			recipient,
			local_node_receive_key,
			context,
			peers.into_iter(),
			&self.entropy_source,
			secp_ctx,
			false,
		)
	}
}

/// A special [`MessageRouter`] that performs no routing and does not create blinded paths.
/// Its purpose is to enable the creation of [`Offer`]s and [`Refund`]s without blinded paths,
/// where the user's `node_id` is used directly as the [`Destination`].
///
/// # Note
/// [`NullMessageRouter`] **must not** be used as the type parameter for [`ChannelManager`],
/// since [`ChannelManager`] requires a functioning [`MessageRouter`] to create blinded paths,
/// which are necessary for constructing reply paths in onion message communication.
/// However, [`NullMessageRouter`] *can* still be passed as an argument to [`ChannelManager`]
/// methods that accepts a [`MessageRouter`], such as [`ChannelManager::create_offer_builder_using_router`],
/// when blinded paths are not needed.
///
/// [`Offer`]: crate::offers::offer::Offer
/// [`Refund`]: crate::offers::refund::Refund
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`ChannelManager::create_offer_builder_using_router`]: crate::ln::channelmanager::ChannelManager::create_offer_builder_using_router
pub struct NullMessageRouter {}

impl MessageRouter for NullMessageRouter {
	fn find_path(
		&self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
	) -> Result<OnionMessagePath, ()> {
		Err(())
	}

	fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
		&self, _recipient: PublicKey, _local_node_receive_key: ReceiveAuthKey,
		_context: MessageContext, _peers: Vec<MessageForwardNode>, _secp_ctx: &Secp256k1<T>,
	) -> Result<Vec<BlindedMessagePath>, ()> {
		Ok(vec![])
	}
}

/// A path for sending an [`OnionMessage`].
#[derive(Clone)]
pub struct OnionMessagePath {
	/// Nodes on the path between the sender and the destination.
	pub intermediate_nodes: Vec<PublicKey>,

	/// The recipient of the message.
	pub destination: Destination,

	/// Addresses that may be used to connect to [`OnionMessagePath::first_node`].
	///
	/// Only needs to be filled in if a connection to the node is required and it is not a known peer.
	/// [`OnionMessenger`] may use this to initiate such a connection.
	pub first_node_addresses: Vec<SocketAddress>,
}

impl OnionMessagePath {
	/// Returns the first node in the path.
	pub fn first_node(&self) -> Option<PublicKey> {
		self.intermediate_nodes.first().copied().or_else(|| self.destination.first_node())
	}
}

/// The destination of an onion message.
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum Destination {
	/// We're sending this onion message to a node.
	Node(PublicKey),
	/// We're sending this onion message to a blinded path.
	BlindedPath(BlindedMessagePath),
}

impl Destination {
	/// Attempts to resolve the [`IntroductionNode::DirectedShortChannelId`] of a
	/// [`Destination::BlindedPath`] to a [`IntroductionNode::NodeId`], if applicable, using the
	/// provided [`ReadOnlyNetworkGraph`].
	pub fn resolve(&mut self, network_graph: &ReadOnlyNetworkGraph) {
		if let Destination::BlindedPath(path) = self {
			if let IntroductionNode::DirectedShortChannelId(..) = path.introduction_node() {
				if let Some(pubkey) = path
					.public_introduction_node_id(network_graph)
					.and_then(|node_id| node_id.as_pubkey().ok())
				{
					*path.introduction_node_mut() = IntroductionNode::NodeId(pubkey);
				}
			}
		}
	}

	pub(super) fn num_hops(&self) -> usize {
		match self {
			Destination::Node(_) => 1,
			Destination::BlindedPath(path) => path.blinded_hops().len(),
		}
	}

	fn first_node(&self) -> Option<PublicKey> {
		match self {
			Destination::Node(node_id) => Some(*node_id),
			Destination::BlindedPath(path) => match path.introduction_node() {
				IntroductionNode::NodeId(pubkey) => Some(*pubkey),
				IntroductionNode::DirectedShortChannelId(..) => None,
			},
		}
	}
}

/// Result of successfully [sending an onion message].
///
/// [sending an onion message]: OnionMessenger::send_onion_message
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum SendSuccess {
	/// The message was buffered and will be sent once it is processed by
	/// [`OnionMessageHandler::next_onion_message_for_peer`].
	Buffered,
	/// The message was buffered and will be sent once the node is connected as a peer and it is
	/// processed by [`OnionMessageHandler::next_onion_message_for_peer`].
	BufferedAwaitingConnection(PublicKey),
}

/// Errors that may occur when [sending an onion message].
///
/// [sending an onion message]: OnionMessenger::send_onion_message
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum SendError {
	/// Errored computing onion message packet keys.
	Secp256k1(secp256k1::Error),
	/// Because implementations such as Eclair will drop onion messages where the message packet
	/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
	TooBigPacket,
	/// The provided [`Destination`] was an invalid [`BlindedMessagePath`] due to not having any
	/// blinded hops.
	TooFewBlindedHops,
	/// The first hop is not a peer and doesn't have a known [`SocketAddress`].
	InvalidFirstHop(PublicKey),
	/// Indicates that a path could not be found by the [`MessageRouter`].
	///
	/// This occurs when either:
	/// - No path from the sender to the destination was found to send the onion message
	/// - No reply path to the sender could be created when responding to an onion message
	PathNotFound,
	/// Onion message contents must have a TLV type >= 64.
	InvalidMessage,
	/// Our next-hop peer's buffer was full or our total outbound buffer was full.
	BufferFull,
	/// Failed to retrieve our node id from the provided [`NodeSigner`].
	///
	/// [`NodeSigner`]: crate::sign::NodeSigner
	GetNodeIdFailed,
	/// The provided [`Destination`] has a blinded path with an unresolved introduction node. An
	/// attempt to resolve it in the [`MessageRouter`] when finding an [`OnionMessagePath`] likely
	/// failed.
	UnresolvedIntroductionNode,
	/// We attempted to send to a blinded path where we are the introduction node, and failed to
	/// advance the blinded path to make the second hop the new introduction node. Either
	/// [`NodeSigner::ecdh`] failed, we failed to tweak the current blinding point to get the
	/// new blinding point, or we were attempting to send to ourselves.
	BlindedPathAdvanceFailed,
}

/// Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
/// [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
/// [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
/// implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
/// message types.
///
/// See [`OnionMessenger`] for example usage.
///
/// [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
/// [`CustomMessage`]: Self::CustomMessage
pub trait CustomOnionMessageHandler {
	/// The message known to the handler. To support multiple message types, you may want to make this
	/// an enum with a variant for each supported message.
	type CustomMessage: OnionMessageContents;

	/// Called with the custom message that was received, returning a response to send, if any.
	///
	/// If the provided `context` is `Some`, then the message was sent to a blinded path that we
	/// created and was authenticated as such by the [`OnionMessenger`].
	///
	/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
	fn handle_custom_message(
		&self, message: Self::CustomMessage, context: Option<Vec<u8>>, responder: Option<Responder>,
	) -> Option<(Self::CustomMessage, ResponseInstruction)>;

	/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
	/// message type is unknown.
	fn read_custom_message<R: io::Read>(
		&self, message_type: u64, buffer: &mut R,
	) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;

	/// Releases any [`Self::CustomMessage`]s that need to be sent.
	///
	/// Typically, this is used for messages initiating a message flow rather than in response to
	/// another message. The latter should use the return value of [`Self::handle_custom_message`].
	fn release_pending_custom_messages(
		&self,
	) -> Vec<(Self::CustomMessage, MessageSendInstructions)>;
}

/// A processed incoming onion message, containing either a Forward (another onion message)
/// or a Receive payload with decrypted contents.
#[derive(Clone, Debug)]
pub enum PeeledOnion<T: OnionMessageContents> {
	/// Forwarded onion, with the next node id and a new onion
	Forward(NextMessageHop, OnionMessage),
	/// Received offers onion message, with decrypted contents, context, and reply path
	Offers(OffersMessage, Option<OffersContext>, Option<BlindedMessagePath>),
	/// Received async payments onion message, with decrypted contents, context, and reply path
	AsyncPayments(AsyncPaymentsMessage, AsyncPaymentsContext, Option<BlindedMessagePath>),
	/// Received DNS resolver onion message, with decrypted contents, context, and reply path
	DNSResolver(DNSResolverMessage, Option<DNSResolverContext>, Option<BlindedMessagePath>),
	/// Received custom onion message, with decrypted contents, context, and reply path
	Custom(T, Option<Vec<u8>>, Option<BlindedMessagePath>),
}

/// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
/// `path`, first calling [`Destination::resolve`] on `path.destination` with the given
/// [`ReadOnlyNetworkGraph`].
///
/// Returns the node id of the peer to send the message to, the message itself, and any addresses
/// needed to connect to the first node.
pub fn create_onion_message_resolving_destination<
	ES: Deref,
	NS: Deref,
	NL: Deref,
	T: OnionMessageContents,
>(
	entropy_source: &ES, node_signer: &NS, node_id_lookup: &NL,
	network_graph: &ReadOnlyNetworkGraph, secp_ctx: &Secp256k1<secp256k1::All>,
	mut path: OnionMessagePath, contents: T, reply_path: Option<BlindedMessagePath>,
) -> Result<(PublicKey, OnionMessage, Vec<SocketAddress>), SendError>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	NL::Target: NodeIdLookUp,
{
	path.destination.resolve(network_graph);
	create_onion_message(
		entropy_source,
		node_signer,
		node_id_lookup,
		secp_ctx,
		path,
		contents,
		reply_path,
	)
}

/// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
/// `path`.
///
/// Returns the node id of the peer to send the message to, the message itself, and any addresses
/// needed to connect to the first node.
///
/// Returns [`SendError::UnresolvedIntroductionNode`] if:
/// - `destination` contains a blinded path with an [`IntroductionNode::DirectedShortChannelId`],
/// - unless it can be resolved by [`NodeIdLookUp::next_node_id`].
/// Use [`create_onion_message_resolving_destination`] instead to resolve the introduction node
/// first with a [`ReadOnlyNetworkGraph`].
pub fn create_onion_message<ES: Deref, NS: Deref, NL: Deref, T: OnionMessageContents>(
	entropy_source: &ES, node_signer: &NS, node_id_lookup: &NL,
	secp_ctx: &Secp256k1<secp256k1::All>, path: OnionMessagePath, contents: T,
	reply_path: Option<BlindedMessagePath>,
) -> Result<(PublicKey, OnionMessage, Vec<SocketAddress>), SendError>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	NL::Target: NodeIdLookUp,
{
	let OnionMessagePath { intermediate_nodes, mut destination, first_node_addresses } = path;
	if let Destination::BlindedPath(ref path) = destination {
		if path.blinded_hops().is_empty() {
			return Err(SendError::TooFewBlindedHops);
		}
	}

	if contents.tlv_type() < 64 {
		return Err(SendError::InvalidMessage);
	}

	// If we are sending straight to a blinded path and we are the introduction node, we need to
	// advance the blinded path by 1 hop so the second hop is the new introduction node.
	if intermediate_nodes.len() == 0 {
		if let Destination::BlindedPath(ref mut blinded_path) = destination {
			let our_node_id = node_signer
				.get_node_id(Recipient::Node)
				.map_err(|()| SendError::GetNodeIdFailed)?;
			let introduction_node_id = match blinded_path.introduction_node() {
				IntroductionNode::NodeId(pubkey) => *pubkey,
				IntroductionNode::DirectedShortChannelId(direction, scid) => {
					match node_id_lookup.next_node_id(*scid) {
						Some(next_node_id) => *direction.select_pubkey(&our_node_id, &next_node_id),
						None => return Err(SendError::UnresolvedIntroductionNode),
					}
				},
			};
			if introduction_node_id == our_node_id {
				blinded_path
					.advance_path_by_one(node_signer, node_id_lookup, &secp_ctx)
					.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
			}
		}
	}

	let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
	let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
	let (first_node_id, blinding_point) = if let Some(first_node_id) = intermediate_nodes.first() {
		(*first_node_id, PublicKey::from_secret_key(&secp_ctx, &blinding_secret))
	} else {
		match &destination {
			Destination::Node(pk) => (*pk, PublicKey::from_secret_key(&secp_ctx, &blinding_secret)),
			Destination::BlindedPath(path) => match path.introduction_node() {
				IntroductionNode::NodeId(pubkey) => (*pubkey, path.blinding_point()),
				IntroductionNode::DirectedShortChannelId(..) => {
					return Err(SendError::UnresolvedIntroductionNode);
				},
			},
		}
	};
	let (packet_payloads, packet_keys) = packet_payloads_and_keys(
		&secp_ctx,
		intermediate_nodes,
		destination,
		contents,
		reply_path,
		&blinding_secret,
	)?;

	let prng_seed = entropy_source.get_secure_random_bytes();
	let onion_routing_packet =
		construct_onion_message_packet(packet_payloads, packet_keys, prng_seed)
			.map_err(|()| SendError::TooBigPacket)?;

	let message = OnionMessage { blinding_point, onion_routing_packet };
	Ok((first_node_id, message, first_node_addresses))
}

/// Decode one layer of an incoming [`OnionMessage`].
///
/// Returns either the next layer of the onion for forwarding or the decrypted content for the
/// receiver.
pub fn peel_onion_message<NS: Deref, L: Deref, CMH: Deref>(
	msg: &OnionMessage, secp_ctx: &Secp256k1<secp256k1::All>, node_signer: NS, logger: L,
	custom_handler: CMH,
) -> Result<PeeledOnion<<<CMH>::Target as CustomOnionMessageHandler>::CustomMessage>, ()>
where
	NS::Target: NodeSigner,
	L::Target: Logger,
	CMH::Target: CustomOnionMessageHandler,
{
	let control_tlvs_ss = match node_signer.ecdh(Recipient::Node, &msg.blinding_point, None) {
		Ok(ss) => ss,
		Err(e) => {
			log_error!(logger, "Failed to retrieve node secret: {:?}", e);
			return Err(());
		},
	};
	let onion_decode_ss = {
		let blinding_factor = {
			let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
			hmac.input(control_tlvs_ss.as_ref());
			let hmac = Hmac::from_engine(hmac).to_byte_array();
			Scalar::from_be_bytes(hmac).unwrap()
		};
		let packet_pubkey = &msg.onion_routing_packet.public_key;
		match node_signer.ecdh(Recipient::Node, packet_pubkey, Some(&blinding_factor)) {
			Ok(ss) => ss.secret_bytes(),
			Err(()) => {
				log_trace!(logger, "Failed to compute onion packet shared secret");
				return Err(());
			},
		}
	};
	let receiving_context_auth_key = node_signer.get_receive_auth_key();
	let next_hop = onion_utils::decode_next_untagged_hop(
		onion_decode_ss,
		&msg.onion_routing_packet.hop_data[..],
		msg.onion_routing_packet.hmac,
		(control_tlvs_ss, custom_handler.deref(), receiving_context_auth_key, logger.deref()),
	);

	// Constructs the next onion message using packet data and blinding logic.
	let build_outbound_onion_message = |packet_pubkey: PublicKey,
	                                    next_hop_hmac: [u8; 32],
	                                    new_packet_bytes: Vec<u8>,
	                                    blinding_point_opt: Option<PublicKey>|
	 -> Result<OnionMessage, ()> {
		let new_pubkey =
			match onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss) {
				Ok(pk) => pk,
				Err(e) => {
					log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
					return Err(());
				},
			};
		let outgoing_packet = Packet {
			version: 0,
			public_key: new_pubkey,
			hop_data: new_packet_bytes,
			hmac: next_hop_hmac,
		};
		let blinding_point = match blinding_point_opt {
			Some(bp) => bp,
			None => match onion_utils::next_hop_pubkey(
				&secp_ctx,
				msg.blinding_point,
				control_tlvs_ss.as_ref(),
			) {
				Ok(bp) => bp,
				Err(e) => {
					log_trace!(logger, "Failed to compute next blinding point: {}", e);
					return Err(());
				},
			},
		};
		Ok(OnionMessage { blinding_point, onion_routing_packet: outgoing_packet })
	};

	match next_hop {
		Ok((
			Payload::Receive {
				message,
				control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context }),
				reply_path,
				control_tlvs_authenticated,
			},
			None,
		)) => match (message, context) {
			(ParsedOnionMessageContents::Offers(msg), Some(MessageContext::Offers(ctx))) => {
				match ctx {
					OffersContext::InvoiceRequest { .. } => {
						// Note: We introduced the `control_tlvs_authenticated` check in LDK v0.2
						// to simplify and standardize onion message authentication.
						// To continue supporting offers created before v0.2, we allow
						// unauthenticated control TLVs for these messages, as they can be
						// verified using the legacy method.
					},
					_ => {
						if !control_tlvs_authenticated {
							log_trace!(logger, "Received an unauthenticated offers onion message");
							return Err(());
						}
					},
				}
				Ok(PeeledOnion::Offers(msg, Some(ctx), reply_path))
			},
			(ParsedOnionMessageContents::Offers(msg), None) => {
				Ok(PeeledOnion::Offers(msg, None, reply_path))
			},
			(
				ParsedOnionMessageContents::AsyncPayments(msg),
				Some(MessageContext::AsyncPayments(ctx)),
			) => {
				if !control_tlvs_authenticated {
					log_trace!(logger, "Received an unauthenticated async payments onion message");
					return Err(());
				}
				Ok(PeeledOnion::AsyncPayments(msg, ctx, reply_path))
			},
			(ParsedOnionMessageContents::Custom(msg), Some(MessageContext::Custom(ctx))) => {
				if !control_tlvs_authenticated {
					log_trace!(logger, "Received an unauthenticated custom onion message");
					return Err(());
				}
				Ok(PeeledOnion::Custom(msg, Some(ctx), reply_path))
			},
			(ParsedOnionMessageContents::Custom(msg), None) => {
				Ok(PeeledOnion::Custom(msg, None, reply_path))
			},
			(
				ParsedOnionMessageContents::DNSResolver(msg),
				Some(MessageContext::DNSResolver(ctx)),
			) => {
				if !control_tlvs_authenticated {
					log_trace!(logger, "Received an unauthenticated DNS resolver onion message");
					return Err(());
				}
				Ok(PeeledOnion::DNSResolver(msg, Some(ctx), reply_path))
			},
			(ParsedOnionMessageContents::DNSResolver(msg), None) => {
				Ok(PeeledOnion::DNSResolver(msg, None, reply_path))
			},
			_ => {
				log_trace!(
					logger,
					"Received message was sent on a blinded path with wrong or missing context."
				);
				Err(())
			},
		},
		Ok((
			Payload::Dummy { control_tlvs_authenticated },
			Some((next_hop_hmac, new_packet_bytes)),
		)) => {
			if !control_tlvs_authenticated {
				log_trace!(logger, "Received an unauthenticated dummy onion message");
				return Err(());
			}

			let onion_message = build_outbound_onion_message(
				msg.onion_routing_packet.public_key,
				next_hop_hmac,
				new_packet_bytes,
				None,
			)?;
			peel_onion_message(&onion_message, secp_ctx, node_signer, logger, custom_handler)
		},
		Ok((
			Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
				next_hop,
				next_blinding_override,
			})),
			Some((next_hop_hmac, new_packet_bytes)),
		)) => {
			let onion_message = build_outbound_onion_message(
				msg.onion_routing_packet.public_key,
				next_hop_hmac,
				new_packet_bytes,
				next_blinding_override,
			)?;

			Ok(PeeledOnion::Forward(next_hop, onion_message))
		},
		Err(e) => {
			log_trace!(logger, "Errored decoding onion message packet: {:?}", e);
			Err(())
		},
		_ => {
			log_trace!(logger, "Received bogus onion message packet, either the sender encoded a final hop as a forwarding hop or vice versa");
			Err(())
		},
	}
}

macro_rules! drop_handled_events_and_abort {
	($self: expr, $res_iter: expr, $event_queue: expr) => {
		// We want to make sure to cleanly abort upon event handling failure. To this end, we drop all
		// successfully handled events from the given queue, reset the events processing flag, and
		// return, to have the events eventually replayed upon next invocation.
		{
			let mut queue_lock = $event_queue.lock().unwrap();

			// Keep all events which previously error'd *or* any that have been added since we dropped
			// the Mutex before.
			let mut any_error = false;
			queue_lock.retain(|_| {
				$res_iter.next().map_or(true, |r| {
					let is_err = r.is_err();
					any_error |= is_err;
					is_err
				})
			});

			if any_error {
				// We failed handling some events. Return to have them eventually replayed.
				$self.pending_events_processor.store(false, Ordering::Release);
				$self.event_notifier.notify();
				return;
			}
		}
	};
}

impl<
		ES: Deref,
		NS: Deref,
		L: Deref,
		NL: Deref,
		MR: Deref,
		OMH: Deref,
		APH: Deref,
		DRH: Deref,
		CMH: Deref,
	> OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	/// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
	/// their respective handlers.
	pub fn new(
		entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL, message_router: MR,
		offers_handler: OMH, async_payments_handler: APH, dns_resolver: DRH, custom_handler: CMH,
	) -> Self {
		Self::new_inner(
			entropy_source,
			node_signer,
			logger,
			node_id_lookup,
			message_router,
			offers_handler,
			async_payments_handler,
			dns_resolver,
			custom_handler,
			false,
		)
	}

	/// Similar to [`Self::new`], but rather than dropping onion messages that are
	/// intended to be forwarded to offline peers, we will intercept them for
	/// later forwarding.
	///
	/// Interception flow:
	/// 1. If an onion message for an offline peer is received, `OnionMessenger` will
	///    generate an [`Event::OnionMessageIntercepted`]. Event handlers can
	///    then choose to persist this onion message for later forwarding, or drop
	///    it.
	/// 2. When the offline peer later comes back online, `OnionMessenger` will
	///    generate an [`Event::OnionMessagePeerConnected`]. Event handlers will
	///    then fetch all previously intercepted onion messages for this peer.
	/// 3. Once the stored onion messages are fetched, they can finally be
	///    forwarded to the now-online peer via [`Self::forward_onion_message`].
	///
	/// # Note
	///
	/// LDK will not rate limit how many [`Event::OnionMessageIntercepted`]s
	/// are generated, so it is the caller's responsibility to limit how many
	/// onion messages are persisted and only persist onion messages for relevant
	/// peers.
	pub fn new_with_offline_peer_interception(
		entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL, message_router: MR,
		offers_handler: OMH, async_payments_handler: APH, dns_resolver: DRH, custom_handler: CMH,
	) -> Self {
		Self::new_inner(
			entropy_source,
			node_signer,
			logger,
			node_id_lookup,
			message_router,
			offers_handler,
			async_payments_handler,
			dns_resolver,
			custom_handler,
			true,
		)
	}

	fn new_inner(
		entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL, message_router: MR,
		offers_handler: OMH, async_payments_handler: APH, dns_resolver: DRH, custom_handler: CMH,
		intercept_messages_for_offline_peers: bool,
	) -> Self {
		let mut secp_ctx = Secp256k1::new();
		secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
		OnionMessenger {
			entropy_source,
			node_signer,
			message_recipients: Mutex::new(new_hash_map()),
			secp_ctx,
			logger,
			node_id_lookup,
			message_router,
			offers_handler,
			async_payments_handler,
			dns_resolver_handler: dns_resolver,
			custom_handler,
			intercept_messages_for_offline_peers,
			pending_intercepted_msgs_events: Mutex::new(Vec::new()),
			pending_peer_connected_events: Mutex::new(Vec::new()),
			pending_events_processor: AtomicBool::new(false),
			event_notifier: Notifier::new(),
		}
	}

	#[cfg(any(test, feature = "_test_utils"))]
	pub fn set_offers_handler(&mut self, offers_handler: OMH) {
		self.offers_handler = offers_handler;
	}

	#[cfg(any(test, feature = "_test_utils"))]
	pub fn set_async_payments_handler(&mut self, async_payments_handler: APH) {
		self.async_payments_handler = async_payments_handler;
	}

	/// Sends an [`OnionMessage`] based on its [`MessageSendInstructions`].
	pub fn send_onion_message<T: OnionMessageContents>(
		&self, contents: T, instructions: MessageSendInstructions,
	) -> Result<SendSuccess, SendError> {
		self.send_onion_message_internal(contents, instructions, format_args!(""))
	}

	fn send_onion_message_internal<T: OnionMessageContents>(
		&self, contents: T, instructions: MessageSendInstructions, log_suffix: fmt::Arguments,
	) -> Result<SendSuccess, SendError> {
		let is_forward = matches!(instructions, MessageSendInstructions::ForwardedMessage { .. });
		let (destination, reply_path) = match instructions {
			MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path } => {
				(destination, Some(reply_path))
			},
			MessageSendInstructions::WithReplyPath { destination, context }
			| MessageSendInstructions::ForReply {
				instructions: ResponseInstruction { destination, context: Some(context) },
			} => match self.create_blinded_path(context) {
				Ok(reply_path) => (destination, Some(reply_path)),
				Err(err) => {
					log_trace!(
						self.logger,
						"Failed to create reply path {}: {:?}",
						log_suffix,
						err
					);
					return Err(err);
				},
			},
			MessageSendInstructions::WithoutReplyPath { destination }
			| MessageSendInstructions::ForReply {
				instructions: ResponseInstruction { destination, context: None },
			} => (destination, None),
			MessageSendInstructions::ForwardedMessage { destination, reply_path } => {
				(destination, reply_path)
			},
		};

		let path = if is_forward {
			// If this onion message is being treated as a forward, we shouldn't pathfind to the next hop.
			OnionMessagePath {
				intermediate_nodes: Vec::new(),
				first_node_addresses: Vec::new(),
				destination,
			}
		} else {
			self.find_path(destination).map_err(|e| {
				log_trace!(self.logger, "Failed to find path {}", log_suffix);
				e
			})?
		};
		let first_hop = path.intermediate_nodes.get(0).map(|p| *p);
		let logger = WithContext::from(&self.logger, first_hop, None, None);

		log_trace!(logger, "Constructing onion message {}: {:?}", log_suffix, contents);
		let (first_node_id, onion_message, addresses) = create_onion_message(
			&self.entropy_source,
			&self.node_signer,
			&self.node_id_lookup,
			&self.secp_ctx,
			path,
			contents,
			reply_path,
		)
		.map_err(|e| {
			log_warn!(logger, "Failed to create onion message with {:?} {}", e, log_suffix);
			e
		})?;

		let result = if is_forward {
			self.enqueue_forwarded_onion_message(
				NextMessageHop::NodeId(first_node_id),
				onion_message,
				log_suffix,
			)
			.map(|()| SendSuccess::Buffered)
		} else {
			self.enqueue_outbound_onion_message(onion_message, first_node_id, addresses)
		};

		match result.as_ref() {
			Err(SendError::GetNodeIdFailed) => {
				log_warn!(logger, "Unable to retrieve node id {}", log_suffix);
			},
			Err(SendError::PathNotFound) => {
				log_trace!(logger, "Failed to find path {}", log_suffix);
			},
			Err(e) => {
				log_trace!(logger, "Failed sending onion message {}: {:?}", log_suffix, e);
			},
			Ok(SendSuccess::Buffered) => {
				log_trace!(logger, "Buffered onion message {}", log_suffix);
			},
			Ok(SendSuccess::BufferedAwaitingConnection(node_id)) => {
				log_trace!(
					logger,
					"Buffered onion message waiting on peer connection {}: {}",
					log_suffix,
					node_id
				);
			},
		}

		result
	}

	fn find_path(&self, destination: Destination) -> Result<OnionMessagePath, SendError> {
		let sender = self
			.node_signer
			.get_node_id(Recipient::Node)
			.map_err(|_| SendError::GetNodeIdFailed)?;

		let peers = self
			.message_recipients
			.lock()
			.unwrap()
			.iter()
			.filter(|(_, recipient)| matches!(recipient, OnionMessageRecipient::ConnectedPeer(_)))
			.map(|(node_id, _)| *node_id)
			.collect();

		self.message_router
			.find_path(sender, peers, destination)
			.map_err(|_| SendError::PathNotFound)
	}

	fn create_blinded_path(
		&self, context: MessageContext,
	) -> Result<BlindedMessagePath, SendError> {
		let recipient = self
			.node_signer
			.get_node_id(Recipient::Node)
			.map_err(|_| SendError::GetNodeIdFailed)?;
		let secp_ctx = &self.secp_ctx;

		let peers = {
			let message_recipients = self.message_recipients.lock().unwrap();
			message_recipients
				.iter()
				.filter(|(_, peer)| matches!(peer, OnionMessageRecipient::ConnectedPeer(_)))
				.map(|(node_id, _)| MessageForwardNode {
					node_id: *node_id,
					short_channel_id: None,
				})
				.collect::<Vec<_>>()
		};

		self.message_router
			.create_blinded_paths(
				recipient,
				self.node_signer.get_receive_auth_key(),
				context,
				peers,
				secp_ctx,
			)
			.and_then(|paths| paths.into_iter().next().ok_or(()))
			.map_err(|_| SendError::PathNotFound)
	}

	fn enqueue_outbound_onion_message(
		&self, onion_message: OnionMessage, first_node_id: PublicKey, addresses: Vec<SocketAddress>,
	) -> Result<SendSuccess, SendError> {
		let mut message_recipients = self.message_recipients.lock().unwrap();
		if outbound_buffer_full(&first_node_id, &message_recipients) {
			return Err(SendError::BufferFull);
		}

		match message_recipients.entry(first_node_id) {
			hash_map::Entry::Vacant(e) => {
				e.insert(OnionMessageRecipient::pending_connection(addresses))
					.enqueue_message(onion_message);
				self.event_notifier.notify();
				Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
			},
			hash_map::Entry::Occupied(mut e) => {
				e.get_mut().enqueue_message(onion_message);
				if e.get().is_connected() {
					Ok(SendSuccess::Buffered)
				} else {
					Ok(SendSuccess::BufferedAwaitingConnection(first_node_id))
				}
			},
		}
	}

	fn enqueue_forwarded_onion_message(
		&self, next_hop: NextMessageHop, onion_message: OnionMessage, log_suffix: fmt::Arguments,
	) -> Result<(), SendError> {
		let next_node_id = match next_hop {
			NextMessageHop::NodeId(pubkey) => pubkey,
			NextMessageHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) {
				Some(pubkey) => pubkey,
				None => {
					log_trace!(self.logger, "Dropping forwarded onion messager: unable to resolve next hop using SCID {} {}", scid, log_suffix);
					return Err(SendError::GetNodeIdFailed);
				},
			},
		};

		let mut message_recipients = self.message_recipients.lock().unwrap();
		if outbound_buffer_full(&next_node_id, &message_recipients) {
			log_trace!(
				self.logger,
				"Dropping forwarded onion message to peer {}: outbound buffer full {}",
				next_node_id,
				log_suffix
			);
			return Err(SendError::BufferFull);
		}

		#[cfg(fuzzing)]
		message_recipients
			.entry(next_node_id)
			.or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()));

		match message_recipients.entry(next_node_id) {
			hash_map::Entry::Occupied(mut e)
				if matches!(e.get(), OnionMessageRecipient::ConnectedPeer(..)) =>
			{
				e.get_mut().enqueue_message(onion_message);
				log_trace!(
					self.logger,
					"Forwarding an onion message to peer {} {}",
					next_node_id,
					log_suffix
				);
				Ok(())
			},
			_ if self.intercept_messages_for_offline_peers => {
				log_trace!(
					self.logger,
					"Generating OnionMessageIntercepted event for peer {} {}",
					next_node_id,
					log_suffix
				);
				self.enqueue_intercepted_event(Event::OnionMessageIntercepted {
					peer_node_id: next_node_id,
					message: onion_message,
				});
				Ok(())
			},
			_ => {
				log_trace!(
					self.logger,
					"Dropping forwarded onion message to disconnected peer {} {}",
					next_node_id,
					log_suffix
				);
				Err(SendError::InvalidFirstHop(next_node_id))
			},
		}
	}

	/// Forwards an [`OnionMessage`] to `peer_node_id`. Useful if we initialized
	/// the [`OnionMessenger`] with [`Self::new_with_offline_peer_interception`]
	/// and want to forward a previously intercepted onion message to a peer that
	/// has just come online.
	pub fn forward_onion_message(
		&self, message: OnionMessage, peer_node_id: &PublicKey,
	) -> Result<(), SendError> {
		let mut message_recipients = self.message_recipients.lock().unwrap();
		if outbound_buffer_full(&peer_node_id, &message_recipients) {
			return Err(SendError::BufferFull);
		}

		match message_recipients.entry(*peer_node_id) {
			hash_map::Entry::Occupied(mut e) if e.get().is_connected() => {
				e.get_mut().enqueue_message(message);
				Ok(())
			},
			_ => Err(SendError::InvalidFirstHop(*peer_node_id)),
		}
	}

	#[cfg(any(test, feature = "_test_utils"))]
	pub fn send_onion_message_using_path<T: OnionMessageContents>(
		&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedMessagePath>,
	) -> Result<SendSuccess, SendError> {
		let (first_node_id, onion_message, addresses) = create_onion_message(
			&self.entropy_source,
			&self.node_signer,
			&self.node_id_lookup,
			&self.secp_ctx,
			path,
			contents,
			reply_path,
		)?;
		self.enqueue_outbound_onion_message(onion_message, first_node_id, addresses)
	}

	pub(crate) fn peel_onion_message(
		&self, msg: &OnionMessage,
	) -> Result<PeeledOnion<<<CMH>::Target as CustomOnionMessageHandler>::CustomMessage>, ()> {
		peel_onion_message(
			msg,
			&self.secp_ctx,
			&*self.node_signer,
			&*self.logger,
			&*self.custom_handler,
		)
	}

	/// Handles the response to an [`OnionMessage`] based on its [`ResponseInstruction`],
	/// enqueueing any response for sending.
	///
	/// This function is useful for asynchronous handling of [`OnionMessage`]s.
	/// Handlers have the option to return `None`, indicating that no immediate response should be
	/// sent. Then, they can transfer the associated [`Responder`] to another task responsible for
	/// generating the response asynchronously. Subsequently, when the response is prepared and
	/// ready for sending, that task can invoke this method to enqueue the response for delivery.
	pub fn handle_onion_message_response<T: OnionMessageContents>(
		&self, response: T, instructions: ResponseInstruction,
	) -> Result<SendSuccess, SendError> {
		let message_type = response.msg_type();
		self.send_onion_message_internal(
			response,
			instructions.into_instructions(),
			format_args!("when responding with {} to an onion message", message_type,),
		)
	}

	#[cfg(test)]
	pub(crate) fn release_pending_msgs(&self) -> HashMap<PublicKey, VecDeque<OnionMessage>> {
		self.enqueue_messages_from_handlers();

		let mut message_recipients = self.message_recipients.lock().unwrap();
		let mut msgs = new_hash_map();
		// We don't want to disconnect the peers by removing them entirely from the original map, so we
		// release the pending message buffers individually.
		for (node_id, recipient) in &mut *message_recipients {
			msgs.insert(*node_id, recipient.release_pending_messages());
		}
		msgs
	}

	// Pull pending messages from each onion message handler and enqueue them in the messenger.
	fn enqueue_messages_from_handlers(&self) {
		// Enqueue any initiating `OffersMessage`s to send.
		for (message, instructions) in self.offers_handler.release_pending_messages() {
			let _ = self.send_onion_message_internal(
				message,
				instructions,
				format_args!("when sending OffersMessage"),
			);
		}

		for (message, instructions) in self.async_payments_handler.release_pending_messages() {
			let _ = self.send_onion_message_internal(
				message,
				instructions,
				format_args!("when sending AsyncPaymentsMessage"),
			);
		}

		// Enqueue any initiating `DNSResolverMessage`s to send.
		for (message, instructions) in self.dns_resolver_handler.release_pending_messages() {
			let _ = self.send_onion_message_internal(
				message,
				instructions,
				format_args!("when sending DNSResolverMessage"),
			);
		}

		// Enqueue any initiating `CustomMessage`s to send.
		for (message, instructions) in self.custom_handler.release_pending_custom_messages() {
			let _ = self.send_onion_message_internal(
				message,
				instructions,
				format_args!("when sending CustomMessage"),
			);
		}
	}

	fn enqueue_intercepted_event(&self, event: Event) {
		const MAX_EVENTS_BUFFER_SIZE: usize = (1 << 10) * 256;
		let mut pending_intercepted_msgs_events =
			self.pending_intercepted_msgs_events.lock().unwrap();
		let total_buffered_bytes: usize =
			pending_intercepted_msgs_events.iter().map(|ev| ev.serialized_length()).sum();
		if total_buffered_bytes >= MAX_EVENTS_BUFFER_SIZE {
			log_trace!(self.logger, "Dropping event {:?}: buffer full", event);
			return;
		}
		pending_intercepted_msgs_events.push(event);
		self.event_notifier.notify();
	}

	/// Gets a [`Future`] that completes when an event is available via
	/// [`EventsProvider::process_pending_events`] or [`Self::process_pending_events_async`].
	///
	/// Note that callbacks registered on the [`Future`] MUST NOT call back into this
	/// [`OnionMessenger`] and should instead register actions to be taken later.
	///
	/// [`EventsProvider::process_pending_events`]: crate::events::EventsProvider::process_pending_events
	pub fn get_update_future(&self) -> Future {
		self.event_notifier.get_future()
	}

	/// Processes any events asynchronously using the given handler.
	///
	/// Note that the event handler is called in the order each event was generated, however
	/// futures are polled in parallel for some events to allow for parallelism where events do not
	/// have an ordering requirement.
	///
	/// See the trait-level documentation of [`EventsProvider`] for requirements.
	pub async fn process_pending_events_async<
		Future: core::future::Future<Output = Result<(), ReplayEvent>> + core::marker::Unpin,
		H: Fn(Event) -> Future,
	>(
		&self, handler: H,
	) {
		if self
			.pending_events_processor
			.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
			.is_err()
		{
			return;
		}

		{
			let intercepted_msgs = self.pending_intercepted_msgs_events.lock().unwrap().clone();
			let mut futures = Vec::with_capacity(intercepted_msgs.len());
			for (node_id, recipient) in self.message_recipients.lock().unwrap().iter_mut() {
				if let OnionMessageRecipient::PendingConnection(_, addresses, _) = recipient {
					if let Some(addresses) = addresses.take() {
						let event = Event::ConnectionNeeded { node_id: *node_id, addresses };
						log_trace!(self.logger, "Handling event {:?} async...", event);
						let future = ResultFuture::Pending(handler(event));
						futures.push(future);
					}
				}
			}

			// The offset in the `futures` vec at which `intercepted_msgs` start. We don't bother
			// replaying `ConnectionNeeded` events.
			let intercepted_msgs_offset = futures.len();

			for ev in intercepted_msgs {
				if let Event::OnionMessageIntercepted { .. } = ev {
				} else {
					debug_assert!(false);
				}
				log_trace!(self.logger, "Handling event {:?} async...", ev);
				let future = ResultFuture::Pending(handler(ev));
				futures.push(future);
			}

			if !futures.is_empty() {
				// Let the `OnionMessageIntercepted` events finish before moving on to peer_connecteds
				let res = MultiResultFuturePoller::new(futures).await;
				log_trace!(self.logger, "Done handling events async, results: {:?}", res);
				let mut res_iter = res.iter().skip(intercepted_msgs_offset);
				drop_handled_events_and_abort!(
					self,
					res_iter,
					self.pending_intercepted_msgs_events
				);
			}
		}

		{
			let peer_connecteds = self.pending_peer_connected_events.lock().unwrap().clone();
			let num_peer_connecteds = peer_connecteds.len();
			if num_peer_connecteds <= 1 {
				for event in peer_connecteds {
					if handler(event).await.is_ok() {
						let mut pending_peer_connected_events =
							self.pending_peer_connected_events.lock().unwrap();
						pending_peer_connected_events.drain(..num_peer_connecteds);
					} else {
						// We failed handling the event. Return to have it eventually replayed.
						self.pending_events_processor.store(false, Ordering::Release);
						return;
					}
				}
			} else {
				let mut futures = Vec::new();
				for event in peer_connecteds {
					log_trace!(self.logger, "Handling event {:?} async...", event);
					let future = ResultFuture::Pending(handler(event));
					futures.push(future);
				}

				if !futures.is_empty() {
					let res = MultiResultFuturePoller::new(futures).await;
					log_trace!(self.logger, "Done handling events async, results: {:?}", res);
					let mut res_iter = res.iter();
					drop_handled_events_and_abort!(
						self,
						res_iter,
						self.pending_peer_connected_events
					);
				}
			}
		}
		self.pending_events_processor.store(false, Ordering::Release);
	}
}

const MAX_TOTAL_BUFFER_SIZE: usize = (1 << 20) * 128;
pub(super) const MAX_PER_PEER_BUFFER_SIZE: usize = (1 << 10) * 256;

fn outbound_buffer_full(
	peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, OnionMessageRecipient>,
) -> bool {
	let mut total_buffered_bytes = 0;
	let mut peer_buffered_bytes = 0;
	for (pk, peer_buf) in buffer {
		for om in peer_buf.pending_messages() {
			let om_len = om.serialized_length();
			if pk == peer_node_id {
				peer_buffered_bytes += om_len;
			}
			total_buffered_bytes += om_len;

			if total_buffered_bytes >= MAX_TOTAL_BUFFER_SIZE
				|| peer_buffered_bytes >= MAX_PER_PEER_BUFFER_SIZE
			{
				return true;
			}
		}
	}
	false
}

impl<
		ES: Deref,
		NS: Deref,
		L: Deref,
		NL: Deref,
		MR: Deref,
		OMH: Deref,
		APH: Deref,
		DRH: Deref,
		CMH: Deref,
	> EventsProvider for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	fn process_pending_events<H: Deref>(&self, handler: H)
	where
		H::Target: EventHandler,
	{
		if self
			.pending_events_processor
			.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
			.is_err()
		{
			return;
		}

		for (node_id, recipient) in self.message_recipients.lock().unwrap().iter_mut() {
			if let OnionMessageRecipient::PendingConnection(_, addresses, _) = recipient {
				if let Some(addresses) = addresses.take() {
					let event = Event::ConnectionNeeded { node_id: *node_id, addresses };
					log_trace!(self.logger, "Handling event {:?}...", event);
					let res = handler.handle_event(event);
					log_trace!(self.logger, "Done handling event, ignoring result: {:?}", res);
				}
			}
		}
		let intercepted_msgs;
		let peer_connecteds;
		{
			let pending_intercepted_msgs_events =
				self.pending_intercepted_msgs_events.lock().unwrap();
			intercepted_msgs = pending_intercepted_msgs_events.clone();
			let pending_peer_connected_events = self.pending_peer_connected_events.lock().unwrap();
			peer_connecteds = pending_peer_connected_events.clone();
			#[cfg(debug_assertions)]
			{
				for ev in pending_intercepted_msgs_events.iter() {
					if let Event::OnionMessageIntercepted { .. } = ev {
					} else {
						panic!();
					}
				}
				for ev in pending_peer_connected_events.iter() {
					if let Event::OnionMessagePeerConnected { .. } = ev {
					} else {
						panic!();
					}
				}
			}
		}

		let mut handling_intercepted_msgs_failed = false;
		let mut num_handled_intercepted_events = 0;
		for ev in intercepted_msgs {
			log_trace!(self.logger, "Handling event {:?}...", ev);
			let res = handler.handle_event(ev);
			log_trace!(self.logger, "Done handling event, result: {:?}", res);
			match res {
				Ok(()) => num_handled_intercepted_events += 1,
				Err(ReplayEvent()) => {
					handling_intercepted_msgs_failed = true;
					break;
				},
			}
		}

		{
			let mut pending_intercepted_msgs_events =
				self.pending_intercepted_msgs_events.lock().unwrap();
			pending_intercepted_msgs_events.drain(..num_handled_intercepted_events);
		}

		if handling_intercepted_msgs_failed {
			self.pending_events_processor.store(false, Ordering::Release);
			self.event_notifier.notify();
			return;
		}

		let mut num_handled_peer_connecteds = 0;
		for ev in peer_connecteds {
			log_trace!(self.logger, "Handling event {:?}...", ev);
			let res = handler.handle_event(ev);
			log_trace!(self.logger, "Done handling event, result: {:?}", res);
			match res {
				Ok(()) => num_handled_peer_connecteds += 1,
				Err(ReplayEvent()) => {
					self.event_notifier.notify();
					break;
				},
			}
		}

		{
			let mut pending_peer_connected_events =
				self.pending_peer_connected_events.lock().unwrap();
			pending_peer_connected_events.drain(..num_handled_peer_connecteds);
			pending_peer_connected_events.shrink_to(10); // Limit total heap usage
		}

		self.pending_events_processor.store(false, Ordering::Release);
	}
}

impl<
		ES: Deref,
		NS: Deref,
		L: Deref,
		NL: Deref,
		MR: Deref,
		OMH: Deref,
		APH: Deref,
		DRH: Deref,
		CMH: Deref,
	> BaseMessageHandler for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	fn provided_node_features(&self) -> NodeFeatures {
		let mut features = NodeFeatures::empty();
		features.set_onion_messages_optional();
		features | self.dns_resolver_handler.provided_node_features()
	}

	fn provided_init_features(&self, _their_node_id: PublicKey) -> InitFeatures {
		let mut features = InitFeatures::empty();
		features.set_onion_messages_optional();
		features
	}

	fn peer_connected(
		&self, their_node_id: PublicKey, init: &msgs::Init, _inbound: bool,
	) -> Result<(), ()> {
		if init.features.supports_onion_messages() {
			{
				let mut message_recipients = self.message_recipients.lock().unwrap();
				message_recipients
					.entry(their_node_id)
					.or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()))
					.mark_connected();
			}
			if self.intercept_messages_for_offline_peers {
				let mut pending_peer_connected_events =
					self.pending_peer_connected_events.lock().unwrap();
				pending_peer_connected_events
					.push(Event::OnionMessagePeerConnected { peer_node_id: their_node_id });
				self.event_notifier.notify();
			}
		} else {
			self.message_recipients.lock().unwrap().remove(&their_node_id);
		}

		Ok(())
	}

	fn peer_disconnected(&self, their_node_id: PublicKey) {
		match self.message_recipients.lock().unwrap().remove(&their_node_id) {
			Some(OnionMessageRecipient::ConnectedPeer(..)) => {},
			Some(_) => debug_assert!(false),
			None => {},
		}
	}

	fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
		Vec::new()
	}
}

impl<
		ES: Deref,
		NS: Deref,
		L: Deref,
		NL: Deref,
		MR: Deref,
		OMH: Deref,
		APH: Deref,
		DRH: Deref,
		CMH: Deref,
	> OnionMessageHandler for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, DRH, CMH>
where
	ES::Target: EntropySource,
	NS::Target: NodeSigner,
	L::Target: Logger,
	NL::Target: NodeIdLookUp,
	MR::Target: MessageRouter,
	OMH::Target: OffersMessageHandler,
	APH::Target: AsyncPaymentsMessageHandler,
	DRH::Target: DNSResolverMessageHandler,
	CMH::Target: CustomOnionMessageHandler,
{
	fn handle_onion_message(&self, peer_node_id: PublicKey, msg: &OnionMessage) {
		let logger = WithContext::from(&self.logger, Some(peer_node_id), None, None);
		macro_rules! log_receive {
			($message: expr, $with_reply_path: expr) => {
				log_trace!(
					logger,
					"Received an onion message with {} reply_path: {:?}",
					if $with_reply_path { "a" } else { "no" },
					$message
				);
			};
		}

		match self.peel_onion_message(msg) {
			Ok(PeeledOnion::Offers(message, context, reply_path)) => {
				log_receive!(message, reply_path.is_some());
				let responder = reply_path.map(Responder::new);
				let response_instructions =
					self.offers_handler.handle_message(message, context, responder);
				if let Some((msg, instructions)) = response_instructions {
					let _ = self.handle_onion_message_response(msg, instructions);
				}
			},
			Ok(PeeledOnion::AsyncPayments(message, context, reply_path)) => {
				log_receive!(message, reply_path.is_some());
				let responder = reply_path.map(Responder::new);
				match message {
					AsyncPaymentsMessage::OfferPathsRequest(msg) => {
						let response_instructions = self
							.async_payments_handler
							.handle_offer_paths_request(msg, context, responder);
						if let Some((msg, instructions)) = response_instructions {
							let _ = self.handle_onion_message_response(msg, instructions);
						}
					},
					AsyncPaymentsMessage::OfferPaths(msg) => {
						let response_instructions =
							self.async_payments_handler.handle_offer_paths(msg, context, responder);
						if let Some((msg, instructions)) = response_instructions {
							let _ = self.handle_onion_message_response(msg, instructions);
						}
					},
					AsyncPaymentsMessage::ServeStaticInvoice(msg) => {
						self.async_payments_handler
							.handle_serve_static_invoice(msg, context, responder);
					},
					AsyncPaymentsMessage::StaticInvoicePersisted(msg) => {
						self.async_payments_handler.handle_static_invoice_persisted(msg, context);
					},
					AsyncPaymentsMessage::HeldHtlcAvailable(msg) => {
						let response_instructions = self
							.async_payments_handler
							.handle_held_htlc_available(msg, context, responder);
						if let Some((msg, instructions)) = response_instructions {
							let _ = self.handle_onion_message_response(msg, instructions);
						}
					},
					AsyncPaymentsMessage::ReleaseHeldHtlc(msg) => {
						self.async_payments_handler.handle_release_held_htlc(msg, context);
					},
				}
			},
			Ok(PeeledOnion::DNSResolver(message, context, reply_path)) => {
				log_receive!(message, reply_path.is_some());
				let responder = reply_path.map(Responder::new);
				match message {
					DNSResolverMessage::DNSSECQuery(msg) => {
						if context.is_some() {
							log_trace!(
								logger,
								"Ignoring DNSSECQuery onion message with unexpected context: {:?}",
								context.unwrap()
							);
							return;
						}
						let response_instructions =
							self.dns_resolver_handler.handle_dnssec_query(msg, responder);
						if let Some((msg, instructions)) = response_instructions {
							let _ = self.handle_onion_message_response(msg, instructions);
						}
					},
					DNSResolverMessage::DNSSECProof(msg) => {
						let context = match context {
							Some(ctx) => ctx,
							None => {
								log_trace!(
									logger,
									"Ignoring DNSSECProof onion message due to missing context"
								);
								return;
							},
						};
						self.dns_resolver_handler.handle_dnssec_proof(msg, context);
					},
				}
			},
			Ok(PeeledOnion::Custom(message, context, reply_path)) => {
				log_receive!(message, reply_path.is_some());
				let responder = reply_path.map(Responder::new);
				let response_instructions =
					self.custom_handler.handle_custom_message(message, context, responder);
				if let Some((msg, instructions)) = response_instructions {
					let _ = self.handle_onion_message_response(msg, instructions);
				}
			},
			Ok(PeeledOnion::Forward(next_hop, onion_message)) => {
				let _ = self.enqueue_forwarded_onion_message(
					next_hop,
					onion_message,
					format_args!("when forwarding peeled onion message from {}", peer_node_id),
				);
			},
			Err(e) => {
				log_error!(logger, "Failed to process onion message {:?}", e);
			},
		}
	}

	fn timer_tick_occurred(&self) {
		let mut message_recipients = self.message_recipients.lock().unwrap();

		// Drop any pending recipients since the last call to avoid retaining buffered messages for
		// too long.
		message_recipients.retain(|_, recipient| match recipient {
			OnionMessageRecipient::PendingConnection(_, None, ticks) => *ticks < MAX_TIMER_TICKS,
			OnionMessageRecipient::PendingConnection(_, Some(_), _) => true,
			_ => true,
		});

		// Increment a timer tick for pending recipients so that their buffered messages are dropped
		// at MAX_TIMER_TICKS.
		for recipient in message_recipients.values_mut() {
			if let OnionMessageRecipient::PendingConnection(_, None, ticks) = recipient {
				*ticks += 1;
			}
		}
	}

	// Before returning any messages to send for the peer, this method will see if any messages were
	// enqueued in the handler by users, find a path to the corresponding blinded path's introduction
	// node, and then enqueue the message for sending to the first peer in the full path.
	fn next_onion_message_for_peer(&self, peer_node_id: PublicKey) -> Option<OnionMessage> {
		self.enqueue_messages_from_handlers();

		let mut message_recipients = self.message_recipients.lock().unwrap();
		message_recipients.get_mut(&peer_node_id).and_then(|buffer| buffer.dequeue_message())
	}
}

// TODO: parameterize the below Simple* types with OnionMessenger and handle the messages it
// produces
/// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and
/// [`SimpleArcPeerManager`]. See their docs for more details.
///
/// This is not exported to bindings users as type aliases aren't supported in most languages.
///
/// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager
/// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager
#[cfg(not(c_bindings))]
#[cfg(feature = "dnssec")]
pub type SimpleArcOnionMessenger<M, T, F, L> = OnionMessenger<
	Arc<KeysManager>,
	Arc<KeysManager>,
	Arc<L>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>, Arc<KeysManager>>>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	IgnoringMessageHandler,
>;

/// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and
/// [`SimpleArcPeerManager`]. See their docs for more details.
///
/// This is not exported to bindings users as type aliases aren't supported in most languages.
///
/// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager
/// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager
#[cfg(not(c_bindings))]
#[cfg(not(feature = "dnssec"))]
pub type SimpleArcOnionMessenger<M, T, F, L> = OnionMessenger<
	Arc<KeysManager>,
	Arc<KeysManager>,
	Arc<L>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>, Arc<KeysManager>>>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	Arc<SimpleArcChannelManager<M, T, F, L>>,
	IgnoringMessageHandler,
	IgnoringMessageHandler,
>;

/// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
/// [`SimpleRefPeerManager`]. See their docs for more details.
///
/// This is not exported to bindings users as type aliases aren't supported in most languages.
///
/// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager
/// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager
#[cfg(not(c_bindings))]
#[cfg(feature = "dnssec")]
pub type SimpleRefOnionMessenger<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, M, T, F, L> =
	OnionMessenger<
		&'a KeysManager,
		&'a KeysManager,
		&'b L,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		&'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L, &'a KeysManager>,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		IgnoringMessageHandler,
	>;

/// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
/// [`SimpleRefPeerManager`]. See their docs for more details.
///
/// This is not exported to bindings users as type aliases aren't supported in most languages.
///
/// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager
/// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager
#[cfg(not(c_bindings))]
#[cfg(not(feature = "dnssec"))]
pub type SimpleRefOnionMessenger<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, M, T, F, L> =
	OnionMessenger<
		&'a KeysManager,
		&'a KeysManager,
		&'b L,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		&'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L, &'a KeysManager>,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>,
		IgnoringMessageHandler,
		IgnoringMessageHandler,
	>;

/// Construct onion packet payloads and keys for sending an onion message along the given
/// `unblinded_path` to the given `destination`.
fn packet_payloads_and_keys<
	T: OnionMessageContents,
	S: secp256k1::Signing + secp256k1::Verification,
>(
	secp_ctx: &Secp256k1<S>, unblinded_path: Vec<PublicKey>, destination: Destination, message: T,
	mut reply_path: Option<BlindedMessagePath>, session_priv: &SecretKey,
) -> Result<(Vec<(Payload<T>, [u8; 32])>, Vec<onion_utils::OnionKeys>), SendError> {
	let num_hops = unblinded_path.len() + destination.num_hops();
	let mut payloads = Vec::with_capacity(num_hops);
	let mut onion_packet_keys = Vec::with_capacity(num_hops);

	let (mut intro_node_id_blinding_pt, num_blinded_hops) = match &destination {
		Destination::Node(_) => (None, 0),
		Destination::BlindedPath(path) => {
			let introduction_node_id = match path.introduction_node() {
				IntroductionNode::NodeId(pubkey) => pubkey,
				IntroductionNode::DirectedShortChannelId(..) => {
					return Err(SendError::UnresolvedIntroductionNode);
				},
			};
			(Some((*introduction_node_id, path.blinding_point())), path.blinded_hops().len())
		},
	};
	let num_unblinded_hops = num_hops - num_blinded_hops;

	let mut unblinded_path_idx = 0;
	let mut blinded_path_idx = 0;
	let mut prev_control_tlvs_ss = None;
	let mut final_control_tlvs = None;
	utils::construct_keys_for_onion_message(
		secp_ctx,
		unblinded_path.into_iter(),
		destination,
		session_priv,
		|onion_packet_ss, ephemeral_pubkey, control_tlvs_ss, unblinded_pk_opt, enc_payload_opt| {
			if num_unblinded_hops != 0 && unblinded_path_idx < num_unblinded_hops {
				if let Some(ss) = prev_control_tlvs_ss.take() {
					payloads.push((
						Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
							next_hop: NextMessageHop::NodeId(unblinded_pk_opt.unwrap()),
							next_blinding_override: None,
						})),
						ss,
					));
				}
				prev_control_tlvs_ss = Some(control_tlvs_ss);
				unblinded_path_idx += 1;
			} else if let Some((intro_node_id, blinding_pt)) = intro_node_id_blinding_pt.take() {
				if let Some(control_tlvs_ss) = prev_control_tlvs_ss.take() {
					payloads.push((
						Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
							next_hop: NextMessageHop::NodeId(intro_node_id),
							next_blinding_override: Some(blinding_pt),
						})),
						control_tlvs_ss,
					));
				}
			}
			if blinded_path_idx < num_blinded_hops.saturating_sub(1) && enc_payload_opt.is_some() {
				payloads.push((
					Payload::Forward(ForwardControlTlvs::Blinded(enc_payload_opt.unwrap())),
					control_tlvs_ss,
				));
				blinded_path_idx += 1;
			} else if let Some(encrypted_payload) = enc_payload_opt {
				final_control_tlvs = Some(ReceiveControlTlvs::Blinded(encrypted_payload));
				prev_control_tlvs_ss = Some(control_tlvs_ss);
			}

			let (rho, mu) = onion_utils::gen_rho_mu_from_shared_secret(onion_packet_ss.as_ref());
			onion_packet_keys.push(onion_utils::OnionKeys {
				#[cfg(test)]
				shared_secret: onion_packet_ss,
				#[cfg(test)]
				blinding_factor: [0; 32],
				ephemeral_pubkey,
				rho,
				mu,
			});
		},
	);

	if let Some(control_tlvs) = final_control_tlvs {
		payloads.push((
			Payload::Receive {
				control_tlvs,
				reply_path: reply_path.take(),
				message,
				control_tlvs_authenticated: false,
			},
			prev_control_tlvs_ss.unwrap(),
		));
	} else {
		payloads.push((
			Payload::Receive {
				control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context: None }),
				reply_path: reply_path.take(),
				message,
				control_tlvs_authenticated: false,
			},
			prev_control_tlvs_ss.unwrap(),
		));
	}

	Ok((payloads, onion_packet_keys))
}

/// Errors if the serialized payload size exceeds onion_message::BIG_PACKET_HOP_DATA_LEN
fn construct_onion_message_packet<T: OnionMessageContents>(
	payloads: Vec<(Payload<T>, [u8; 32])>, onion_keys: Vec<onion_utils::OnionKeys>,
	prng_seed: [u8; 32],
) -> Result<Packet, ()> {
	// Spec rationale:
	// "`len` allows larger messages to be sent than the standard 1300 bytes allowed for an HTLC
	// onion, but this should be used sparingly as it is reduces anonymity set, hence the
	// recommendation that it either look like an HTLC onion, or if larger, be a fixed size."
	let payloads_ser_len = onion_utils::payloads_serialized_length(&payloads);
	let hop_data_len = if payloads_ser_len <= SMALL_PACKET_HOP_DATA_LEN {
		SMALL_PACKET_HOP_DATA_LEN
	} else if payloads_ser_len <= BIG_PACKET_HOP_DATA_LEN {
		BIG_PACKET_HOP_DATA_LEN
	} else {
		return Err(());
	};

	onion_utils::construct_onion_message_packet::<_, _>(
		payloads,
		onion_keys,
		prng_seed,
		hop_data_len,
	)
}