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
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
// 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.

use crate::blinded_path::message::{
	BlindedMessagePath, MessageContext, NextMessageHop, OffersContext,
};
use crate::blinded_path::payment::PaymentContext;
use crate::blinded_path::payment::{AsyncBolt12OfferContext, BlindedPaymentTlvs};
use crate::chain::channelmonitor::{HTLC_FAIL_BACK_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
use crate::events::{
	Event, EventsProvider, HTLCHandlingFailureReason, HTLCHandlingFailureType, PaidBolt12Invoice,
	PaymentFailureReason, PaymentPurpose,
};
use crate::ln::blinded_payment_tests::{fail_blinded_htlc_backwards, get_blinded_route_parameters};
use crate::ln::channelmanager::{
	Bolt12PaymentError, OptionalOfferPaymentParams, PaymentId, RecipientOnionFields,
	MIN_CLTV_EXPIRY_DELTA,
};
use crate::ln::functional_test_utils::*;
use crate::ln::inbound_payment;
use crate::ln::msgs;
use crate::ln::msgs::{
	BaseMessageHandler, ChannelMessageHandler, MessageSendEvent, OnionMessageHandler,
};
use crate::ln::offers_tests;
use crate::ln::onion_utils::LocalHTLCFailureReason;
use crate::ln::outbound_payment::{
	PendingOutboundPayment, Retry, TEST_ASYNC_PAYMENT_TIMEOUT_RELATIVE_EXPIRY,
};
use crate::offers::async_receive_offer_cache::{
	TEST_INVOICE_REFRESH_THRESHOLD, TEST_MAX_CACHED_OFFERS_TARGET, TEST_MAX_UPDATE_ATTEMPTS,
	TEST_MIN_OFFER_PATHS_RELATIVE_EXPIRY_SECS, TEST_OFFER_REFRESH_THRESHOLD,
};
use crate::offers::flow::{
	TEST_DEFAULT_ASYNC_RECEIVE_OFFER_EXPIRY, TEST_OFFERS_MESSAGE_REQUEST_LIMIT,
	TEST_TEMP_REPLY_PATH_RELATIVE_EXPIRY,
};
use crate::offers::invoice_request::InvoiceRequest;
use crate::offers::nonce::Nonce;
use crate::offers::offer::{Amount, Offer};
use crate::offers::static_invoice::{
	StaticInvoice, StaticInvoiceBuilder,
	DEFAULT_RELATIVE_EXPIRY as STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY,
};
use crate::onion_message::async_payments::{AsyncPaymentsMessage, AsyncPaymentsMessageHandler};
use crate::onion_message::messenger::{
	Destination, MessageRouter, MessageSendInstructions, PeeledOnion,
};
use crate::onion_message::offers::OffersMessage;
use crate::onion_message::packet::ParsedOnionMessageContents;
use crate::prelude::*;
use crate::routing::router::{Payee, PaymentParameters};
use crate::sign::NodeSigner;
use crate::sync::Mutex;
use crate::types::features::Bolt12InvoiceFeatures;
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
use crate::util::config::UserConfig;
use crate::util::ser::Writeable;
use bitcoin::constants::ChainHash;
use bitcoin::network::Network;
use bitcoin::secp256k1;
use bitcoin::secp256k1::{PublicKey, Secp256k1};

use core::convert::Infallible;
use core::time::Duration;

struct StaticInvoiceServerFlowResult {
	invoice: StaticInvoice,
	invoice_slot: u16,
	invoice_request_path: BlindedMessagePath,

	// Returning messages that were sent along the way allows us to test handling duplicate messages.
	offer_paths_request: msgs::OnionMessage,
	static_invoice_persisted_message: msgs::OnionMessage,
}

// Go through the flow of interactively building a `StaticInvoice`, returning the
// AsyncPaymentsMessage::ServeStaticInvoice that has yet to be provided to the server node.
// Assumes that the sender and recipient are only peers with each other.
//
// Returns (offer_paths_req, serve_static_invoice)
fn invoice_flow_up_to_send_serve_static_invoice(
	server: &Node, recipient: &Node,
) -> (msgs::OnionMessage, msgs::OnionMessage) {
	// First provide an OfferPathsRequest from the recipient to the server.
	recipient.node.timer_tick_occurred();
	let offer_paths_req = loop {
		let msg = recipient
			.onion_messenger
			.next_onion_message_for_peer(server.node.get_our_node_id())
			.unwrap();
		// Ignore any messages that are updating the static invoice stored with the server here
		if matches!(
			server.onion_messenger.peel_onion_message(&msg).unwrap(),
			PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _)
		) {
			break msg;
		}
	};
	server.onion_messenger.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req);

	// Check that the right number of requests were queued and that they were only queued for the
	// server node.
	let mut pending_oms = recipient.onion_messenger.release_pending_msgs();
	let mut offer_paths_req_msgs = pending_oms.remove(&server.node.get_our_node_id()).unwrap();
	assert!(offer_paths_req_msgs.len() <= TEST_OFFERS_MESSAGE_REQUEST_LIMIT);
	for (_, msgs) in pending_oms {
		assert!(msgs.is_empty());
	}

	// The server responds with OfferPaths.
	let offer_paths = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient.onion_messenger.handle_onion_message(server.node.get_our_node_id(), &offer_paths);

	// Only one OfferPaths response should be queued.
	let mut pending_oms = server.onion_messenger.release_pending_msgs();
	for (_, msgs) in pending_oms {
		assert!(msgs.is_empty());
	}

	// After receiving the offer paths, the recipient constructs the static invoice and sends
	// ServeStaticInvoice to the server.
	let serve_static_invoice_om = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	(offer_paths_req, serve_static_invoice_om)
}

// Go through the flow of interactively building a `StaticInvoice` and storing it with the static
// invoice server, returning the invoice and messages that were exchanged along the way at the end.
fn pass_static_invoice_server_messages(
	server: &Node, recipient: &Node, recipient_id: Vec<u8>,
) -> StaticInvoiceServerFlowResult {
	// Force the server and recipient to send OMs directly to each other for testing simplicity.
	server.message_router.peers_override.lock().unwrap().push(recipient.node.get_our_node_id());
	recipient.message_router.peers_override.lock().unwrap().push(server.node.get_our_node_id());

	let (offer_paths_req, serve_static_invoice_om) =
		invoice_flow_up_to_send_serve_static_invoice(server, recipient);
	server
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &serve_static_invoice_om);

	// Upon handling the ServeStaticInvoice message, the server's node surfaces an event indicating
	// that the static invoice should be persisted.
	let mut events = server.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (invoice, invoice_slot, ack_path, invoice_request_path) = match events.pop().unwrap() {
		Event::PersistStaticInvoice {
			invoice,
			invoice_persisted_path,
			recipient_id: ev_id,
			invoice_slot,
			invoice_request_path,
		} => {
			assert_eq!(recipient_id, ev_id);
			(invoice, invoice_slot, invoice_persisted_path, invoice_request_path)
		},
		_ => panic!(),
	};

	// Once the static invoice is persisted, the server needs to call `static_invoice_persisted` with
	// the reply path to the ServeStaticInvoice message, to tell the recipient that their offer is
	// ready to be used for async payments.
	server.node.static_invoice_persisted(ack_path);
	let invoice_persisted_om = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient
		.onion_messenger
		.handle_onion_message(server.node.get_our_node_id(), &invoice_persisted_om);

	// Remove the peer restriction added above.
	server.message_router.peers_override.lock().unwrap().clear();
	recipient.message_router.peers_override.lock().unwrap().clear();

	StaticInvoiceServerFlowResult {
		offer_paths_request: offer_paths_req,
		static_invoice_persisted_message: invoice_persisted_om,
		invoice_request_path,
		invoice,
		invoice_slot,
	}
}

// Goes through the async receive onion message flow, returning the final release_held_htlc OM.
//
// Assumes the held_htlc_available message will be sent:
// 	 sender -> always_online_recipient_counterparty -> recipient.
//
// Returns: (held_htlc_available_om, release_held_htlc_om)
fn pass_async_payments_oms(
	static_invoice: StaticInvoice, sender: &Node, always_online_recipient_counterparty: &Node,
	recipient: &Node, recipient_id: Vec<u8>, invoice_request_path: BlindedMessagePath,
) -> (msgs::OnionMessage, msgs::OnionMessage) {
	let sender_node_id = sender.node.get_our_node_id();
	let always_online_node_id = always_online_recipient_counterparty.node.get_our_node_id();

	let invreq_om =
		sender.onion_messenger.next_onion_message_for_peer(always_online_node_id).unwrap();
	always_online_recipient_counterparty
		.onion_messenger
		.handle_onion_message(sender_node_id, &invreq_om);

	let mut events = always_online_recipient_counterparty.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id,
			invoice_slot: _,
			reply_path,
			invoice_request,
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	always_online_recipient_counterparty
		.node
		.respond_to_static_invoice_request(
			static_invoice,
			reply_path,
			invoice_request,
			invoice_request_path,
		)
		.unwrap();

	let _invreq_om = always_online_recipient_counterparty
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	let static_invoice_om = always_online_recipient_counterparty
		.onion_messenger
		.next_onion_message_for_peer(sender_node_id)
		.unwrap();
	sender.onion_messenger.handle_onion_message(always_online_node_id, &static_invoice_om);
	// Check that the node will not lock in HTLCs yet.
	sender.node.process_pending_htlc_forwards();
	assert!(sender.node.get_and_clear_pending_msg_events().is_empty());

	let held_htlc_available_om_0_1 =
		sender.onion_messenger.next_onion_message_for_peer(always_online_node_id).unwrap();
	always_online_recipient_counterparty
		.onion_messenger
		.handle_onion_message(sender_node_id, &held_htlc_available_om_0_1);
	let held_htlc_available_om_1_2 = always_online_recipient_counterparty
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient
		.onion_messenger
		.handle_onion_message(always_online_node_id, &held_htlc_available_om_1_2);

	let release_held_htlc =
		recipient.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();
	(held_htlc_available_om_1_2, release_held_htlc)
}

fn create_static_invoice_builder<'a>(
	recipient: &Node, offer: &'a Offer, offer_nonce: Nonce, relative_expiry: Option<Duration>,
) -> StaticInvoiceBuilder<'a> {
	let entropy = recipient.keys_manager;
	let amount_msat = offer.amount().and_then(|amount| match amount {
		Amount::Bitcoin { amount_msats } => Some(amount_msats),
		Amount::Currency { .. } => None,
	});

	let relative_expiry = relative_expiry.unwrap_or(STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY);
	let relative_expiry_secs: u32 = relative_expiry.as_secs().try_into().unwrap_or(u32::MAX);

	let created_at = recipient.node.duration_since_epoch();
	let payment_secret = inbound_payment::create_for_spontaneous_payment(
		&recipient.keys_manager.get_expanded_key(),
		amount_msat,
		relative_expiry_secs,
		created_at.as_secs(),
		None,
	)
	.unwrap();

	recipient
		.node
		.flow
		.create_static_invoice_builder(
			&recipient.router,
			entropy,
			offer,
			offer_nonce,
			payment_secret,
			relative_expiry_secs,
			recipient.node.list_usable_channels(),
			recipient.node.test_get_peers_for_blinded_path(),
		)
		.unwrap()
}

fn create_static_invoice<T: secp256k1::Signing + secp256k1::Verification>(
	always_online_counterparty: &Node, recipient: &Node, relative_expiry: Option<Duration>,
	secp_ctx: &Secp256k1<T>,
) -> (Offer, StaticInvoice) {
	let entropy_source = recipient.keys_manager;

	let blinded_paths_to_always_online_node = always_online_counterparty
		.message_router
		.create_blinded_paths(
			always_online_counterparty.node.get_our_node_id(),
			always_online_counterparty.keys_manager.get_receive_auth_key(),
			MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
			Vec::new(),
			&secp_ctx,
		)
		.unwrap();
	let (offer_builder, offer_nonce) = recipient
		.node
		.flow
		.create_async_receive_offer_builder(entropy_source, blinded_paths_to_always_online_node)
		.unwrap();
	let offer = offer_builder.build().unwrap();
	let static_invoice =
		create_static_invoice_builder(recipient, &offer, offer_nonce, relative_expiry)
			.build_and_sign(&secp_ctx)
			.unwrap();
	(offer, static_invoice)
}

fn extract_payment_hash(event: &MessageSendEvent) -> PaymentHash {
	match event {
		MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
			updates.update_add_htlcs[0].payment_hash
		},
		_ => panic!(),
	}
}

fn extract_payment_preimage(event: &Event) -> PaymentPreimage {
	match event {
		Event::PaymentClaimable {
			purpose: PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
			..
		} => payment_preimage.unwrap(),
		_ => panic!(),
	}
}

fn expect_offer_paths_requests(recipient: &Node, next_hop_nodes: &[&Node]) {
	// We want to check that the async recipient has enqueued at least one `OfferPathsRequest` and no
	// other message types. Check this by iterating through all their outbound onion messages, peeling
	// multiple times if the messages are forwarded through other nodes.
	let offer_paths_reqs = extract_expected_om(
		recipient,
		next_hop_nodes,
		|peeled_onion| {
			matches!(
				peeled_onion,
				PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _)
			)
		},
		|_| false,
	);
	assert!(!offer_paths_reqs.is_empty());
}

fn extract_invoice_request_om<'a>(
	payer: &'a Node, next_hop_nodes: &[&'a Node],
) -> (PublicKey, msgs::OnionMessage) {
	extract_expected_om(
		payer,
		next_hop_nodes,
		|peeled_onion| {
			matches!(peeled_onion, &PeeledOnion::Offers(OffersMessage::InvoiceRequest(_), _, _))
		},
		|_| false,
	)
	.pop()
	.unwrap()
}

fn extract_static_invoice_om<'a>(
	invoice_server: &'a Node, next_hop_nodes: &[&'a Node],
) -> (PublicKey, msgs::OnionMessage, StaticInvoice) {
	let mut static_invoice = None;
	let mut expected_msg_type = |peeled_onion: &_| {
		if let PeeledOnion::Offers(OffersMessage::StaticInvoice(inv), _, _) = peeled_onion {
			static_invoice = Some(inv.clone());
			true
		} else {
			false
		}
	};
	let expected_msg_type_to_ignore = |peeled_onion: &_| {
		matches!(peeled_onion, &PeeledOnion::Offers(OffersMessage::InvoiceRequest(_), _, _))
	};
	let (peer_id, om) = extract_expected_om(
		invoice_server,
		next_hop_nodes,
		expected_msg_type,
		expected_msg_type_to_ignore,
	)
	.pop()
	.unwrap();
	(peer_id, om, static_invoice.unwrap())
}

fn extract_held_htlc_available_oms<'a>(
	payer: &'a Node, next_hop_nodes: &[&'a Node],
) -> Vec<(PublicKey, msgs::OnionMessage)> {
	extract_expected_om(
		payer,
		next_hop_nodes,
		|peeled_onion| {
			matches!(
				peeled_onion,
				&PeeledOnion::AsyncPayments(AsyncPaymentsMessage::HeldHtlcAvailable(_), _, _)
			)
		},
		|_| false,
	)
}

fn extract_release_htlc_oms<'a>(
	recipient: &'a Node, next_hop_nodes: &[&'a Node],
) -> Vec<(PublicKey, msgs::OnionMessage)> {
	extract_expected_om(
		recipient,
		next_hop_nodes,
		|peeled_onion| {
			matches!(
				peeled_onion,
				&PeeledOnion::AsyncPayments(AsyncPaymentsMessage::ReleaseHeldHtlc(_), _, _)
			)
		},
		|_| false,
	)
}

fn extract_expected_om<F1, F2>(
	msg_sender: &Node, next_hop_nodes: &[&Node], mut expected_msg_type: F1,
	expected_msg_type_to_ignore: F2,
) -> Vec<(PublicKey, msgs::OnionMessage)>
where
	F1: FnMut(&PeeledOnion<Infallible>) -> bool,
	F2: Fn(&PeeledOnion<Infallible>) -> bool,
{
	let per_msg_recipient_msgs = msg_sender.onion_messenger.release_pending_msgs();
	let mut pk_to_msg = Vec::new();
	for (pk, msgs) in per_msg_recipient_msgs {
		for msg in msgs {
			pk_to_msg.push((pk, msg));
		}
	}
	let mut msgs = Vec::new();
	while let Some((pk, msg)) = pk_to_msg.pop() {
		let node = next_hop_nodes.iter().find(|node| node.node.get_our_node_id() == pk).unwrap();
		let peeled_msg = node.onion_messenger.peel_onion_message(&msg).unwrap();
		match peeled_msg {
			PeeledOnion::Forward(next_hop, msg) => {
				let next_pk = match next_hop {
					NextMessageHop::NodeId(pk) => pk,
					NextMessageHop::ShortChannelId(scid) => {
						let mut next_pk = None;
						for node in next_hop_nodes {
							if node.node.get_our_node_id() == pk {
								continue;
							}
							for channel in node.node.list_channels() {
								if channel.short_channel_id.unwrap() == scid
									|| channel.inbound_scid_alias.unwrap_or(0) == scid
								{
									next_pk = Some(node.node.get_our_node_id());
								}
							}
						}
						next_pk.unwrap()
					},
				};
				pk_to_msg.push((next_pk, msg));
			},
			peeled_onion if expected_msg_type(&peeled_onion) => msgs.push((pk, msg)),
			peeled_onion if expected_msg_type_to_ignore(&peeled_onion) => {},
			peeled_onion => panic!("Unexpected message: {:?}", peeled_onion),
		}
	}
	assert!(!msgs.is_empty());
	msgs
}

fn advance_time_by(duration: Duration, node: &Node) {
	let target_time = (node.node.duration_since_epoch() + duration).as_secs() as u32;
	let block = create_dummy_block(node.best_block_hash(), target_time, Vec::new());
	connect_block(node, &block);
}

fn often_offline_node_cfg() -> UserConfig {
	let mut cfg = test_default_channel_config();
	cfg.channel_handshake_config.announce_for_forwarding = false;
	cfg.channel_handshake_limits.force_announced_channel_preference = true;
	cfg.hold_outbound_htlcs_at_next_hop = true;
	cfg
}

fn unify_blockheight_across_nodes(nodes: &[Node]) {
	// Make sure all nodes are at the same block height
	let node_max_height =
		nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
	for node in nodes.iter() {
		connect_blocks(node, node_max_height - node.best_block_info().1);
	}
}

// Interactively builds an async offer and initiates payment to it from an often-offline sender,
// up to but not including providing the static invoice to the sender.
//
// Assumes that the first node is the async sender and the last hop is the async recipient, with the
// middle node(s) being announced nodes acting as LSP and/or invoice server. At least 1 middle node
// must be present.
//
// Returns the `StaticInvoice` and the onion message containing it, as well as the direct peer
// sending the static invoice OM to the sender.
fn build_async_offer_and_init_payment(
	amt_msat: u64, nodes: &[Node],
) -> (StaticInvoice, PublicKey, msgs::OnionMessage) {
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];
	let invoice_server = &nodes[nodes.len() - 2];
	let recipient = nodes.last().unwrap();

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		invoice_server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(recipient, &[sender, sender_lsp, invoice_server]);
	let invoice_flow_res =
		pass_static_invoice_server_messages(invoice_server, recipient, recipient_id.clone());
	let invoice = invoice_flow_res.invoice;
	let invreq_path = invoice_flow_res.invoice_request_path;

	let offer = recipient.node.get_async_receive_offer().unwrap();
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	// Forward invreq to server, pass static invoice back
	let (peer_id, invreq_om) = extract_invoice_request_om(sender, &[sender_lsp, invoice_server]);
	invoice_server.onion_messenger.handle_onion_message(peer_id, &invreq_om);

	let mut events = invoice_server.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invreq) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id, reply_path, invoice_request, ..
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};
	invoice_server
		.node
		.respond_to_static_invoice_request(invoice.clone(), reply_path, invreq, invreq_path)
		.unwrap();
	let (peer_node_id, static_invoice_om, _) =
		extract_static_invoice_om(invoice_server, &[sender_lsp, sender, recipient]);

	(invoice, peer_node_id, static_invoice_om)
}

fn lock_in_htlc_for_static_invoice(
	static_invoice_om: &msgs::OnionMessage, om_peer: PublicKey, sender: &Node, sender_lsp: &Node,
) -> PaymentHash {
	// The sender should lock in the held HTLC with their LSP right after receiving the static invoice.
	sender.onion_messenger.handle_onion_message(om_peer, &static_invoice_om);
	check_added_monitors(sender, 1);
	let commitment_update = get_htlc_update_msgs!(sender, sender_lsp.node.get_our_node_id());
	let update_add = commitment_update.update_add_htlcs[0].clone();
	let payment_hash = update_add.payment_hash;
	assert!(update_add.hold_htlc.is_some());
	sender_lsp.node.handle_update_add_htlc(sender.node.get_our_node_id(), &update_add);
	commitment_signed_dance!(sender_lsp, sender, &commitment_update.commitment_signed, false, true);
	payment_hash
}

#[test]
fn invalid_keysend_payment_secret() {
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let chan_upd_1_2 =
		create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;

	let invalid_payment_secret = PaymentSecret([42; 32]);
	let amt_msat = 5000;
	let keysend_preimage = PaymentPreimage([42; 32]);
	let route_params = get_blinded_route_parameters(
		amt_msat,
		invalid_payment_secret,
		1,
		1_0000_0000,
		nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(),
		&[&chan_upd_1_2],
		&chanmon_cfgs[2].keys_manager,
	);

	let payment_hash = nodes[0]
		.node
		.send_spontaneous_payment(
			Some(keysend_preimage),
			RecipientOnionFields::spontaneous_empty(),
			PaymentId(keysend_preimage.0),
			route_params,
			Retry::Attempts(0),
		)
		.unwrap();
	check_added_monitors(&nodes[0], 1);

	let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);

	let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	let args =
		PassAlongPathArgs::new(&nodes[0], &expected_route[0], amt_msat, payment_hash, ev.clone())
			.with_payment_secret(invalid_payment_secret)
			.with_payment_preimage(keysend_preimage)
			.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
	do_pass_along_path(args);

	let updates_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
	assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
	let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
	assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
	assert_eq!(
		update_malformed.failure_code,
		LocalHTLCFailureReason::InvalidOnionBlinding.failure_code()
	);
	nodes[1]
		.node
		.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
	do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, true, false);

	let updates_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
	assert_eq!(updates_1_0.update_fail_htlcs.len(), 1);
	nodes[0].node.handle_update_fail_htlc(
		nodes[1].node.get_our_node_id(),
		&updates_1_0.update_fail_htlcs[0],
	);
	do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
	expect_payment_failed_conditions(
		&nodes[0],
		payment_hash,
		false,
		PaymentFailedConditions::new()
			.expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]),
	);
}

#[test]
fn static_invoice_unknown_required_features() {
	// Test that we will fail to pay a static invoice with unsupported required features.
	let secp_ctx = Secp256k1::new();
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	let entropy_source = nodes[2].keys_manager;
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	// Manually construct a static invoice so we can set unknown required features.
	let blinded_paths_to_always_online_node = nodes[1]
		.message_router
		.create_blinded_paths(
			nodes[1].node.get_our_node_id(),
			nodes[1].keys_manager.get_receive_auth_key(),
			MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
			Vec::new(),
			&secp_ctx,
		)
		.unwrap();
	let (offer_builder, nonce) = nodes[2]
		.node
		.flow
		.create_async_receive_offer_builder(entropy_source, blinded_paths_to_always_online_node)
		.unwrap();
	let offer = offer_builder.build().unwrap();
	let static_invoice_unknown_req_features =
		create_static_invoice_builder(&nodes[2], &offer, nonce, None)
			.features_unchecked(Bolt12InvoiceFeatures::unknown())
			.build_and_sign(&secp_ctx)
			.unwrap();

	// Initiate payment to the offer corresponding to the manually-constructed invoice that has
	// unknown required features.
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	// Don't forward the invreq since the invoice was created outside of the normal flow, instead
	// manually construct the response.
	let invreq_om = nodes[0]
		.onion_messenger
		.next_onion_message_for_peer(nodes[1].node.get_our_node_id())
		.unwrap();
	let invreq_reply_path = offers_tests::extract_invoice_request(&nodes[1], &invreq_om).1;
	nodes[1]
		.onion_messenger
		.send_onion_message(
			ParsedOnionMessageContents::<Infallible>::Offers(OffersMessage::StaticInvoice(
				static_invoice_unknown_req_features,
			)),
			MessageSendInstructions::WithoutReplyPath {
				destination: Destination::BlindedPath(invreq_reply_path),
			},
		)
		.unwrap();

	// Check that paying the static invoice fails as expected with
	// `PaymentFailureReason::UnknownRequiredFeatures`.
	let static_invoice_om = nodes[1]
		.onion_messenger
		.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
		.unwrap();
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);
	let events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	match events[0] {
		Event::PaymentFailed { payment_hash, payment_id: ev_payment_id, reason } => {
			assert_eq!(payment_hash, None);
			assert_eq!(payment_id, ev_payment_id);
			assert_eq!(reason, Some(PaymentFailureReason::UnknownRequiredFeatures));
		},
		_ => panic!(),
	}
}

#[test]
fn ignore_unexpected_static_invoice() {
	// Test that we'll ignore unexpected static invoices, invoices that don't match our invoice
	// request, and duplicate invoices.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	// Initiate payment to the sender's intended offer.
	let valid_static_invoice =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone()).invoice;
	let offer = nodes[2].node.get_async_receive_offer().unwrap();

	// Create a static invoice to be sent over the reply path containing the original payment_id, but
	// the static invoice corresponds to a different offer than was originally paid.
	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let unexpected_static_invoice = invoice_flow_res.invoice;

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let invreq_om = nodes[0]
		.onion_messenger
		.next_onion_message_for_peer(nodes[1].node.get_our_node_id())
		.unwrap();
	nodes[1].onion_messenger.handle_onion_message(nodes[0].node.get_our_node_id(), &invreq_om);

	let mut events = nodes[1].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id,
			invoice_slot: _,
			reply_path,
			invoice_request,
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	// Check that the sender will ignore the unexpected static invoice.
	nodes[1]
		.node
		.respond_to_static_invoice_request(
			unexpected_static_invoice,
			reply_path.clone(),
			invoice_request.clone(),
			invoice_flow_res.invoice_request_path.clone(),
		)
		.unwrap();
	let unexpected_static_invoice_om = nodes[1]
		.onion_messenger
		.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
		.unwrap();
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &unexpected_static_invoice_om);
	let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
	assert!(async_pmts_msgs.is_empty());
	assert!(nodes[0].node.get_and_clear_pending_events().is_empty());

	// A valid static invoice corresponding to the correct offer will succeed and cause us to send a
	// held_htlc_available onion message.
	nodes[1]
		.node
		.respond_to_static_invoice_request(
			valid_static_invoice.clone(),
			reply_path.clone(),
			invoice_request.clone(),
			invoice_flow_res.invoice_request_path.clone(),
		)
		.unwrap();
	let static_invoice_om = nodes[1]
		.onion_messenger
		.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
		.unwrap();
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);
	let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
	assert!(!async_pmts_msgs.is_empty());
	assert!(async_pmts_msgs
		.into_iter()
		.all(|(msg, _)| matches!(msg, AsyncPaymentsMessage::HeldHtlcAvailable(_))));

	// Receiving a duplicate invoice will have no effect.
	nodes[1]
		.node
		.respond_to_static_invoice_request(
			valid_static_invoice,
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path,
		)
		.unwrap();
	let dup_static_invoice_om = nodes[1]
		.onion_messenger
		.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
		.unwrap();
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &dup_static_invoice_om);
	let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
	assert!(async_pmts_msgs.is_empty());
}

#[test]
fn ignore_duplicate_invoice() {
	// When a sender tries to pay an async recipient it could potentially end up receiving two
	// invoices: one static invoice that it received from always-online node and a fresh invoice
	// received from async recipient in case it was online to reply to request. Test that it
	// will only pay one of the two invoices.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(3, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None]);

	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let sender = &nodes[0];
	let always_online_node = &nodes[1];
	let async_recipient = &nodes[2];

	let recipient_id = vec![42; 32];
	let inv_server_paths = always_online_node
		.node
		.blinded_paths_for_async_recipient(recipient_id.clone(), None)
		.unwrap();
	async_recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(async_recipient, &[sender, always_online_node]);

	let invoice_flow_res = pass_static_invoice_server_messages(
		always_online_node,
		async_recipient,
		recipient_id.clone(),
	);
	let static_invoice = invoice_flow_res.invoice;
	assert!(static_invoice.invoice_features().supports_basic_mpp());
	let offer = async_recipient.node.get_async_receive_offer().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let sender_node_id = sender.node.get_our_node_id();
	let always_online_node_id = always_online_node.node.get_our_node_id();
	let async_recipient_id = async_recipient.node.get_our_node_id();

	let invreq_om =
		sender.onion_messenger.next_onion_message_for_peer(always_online_node_id).unwrap();
	always_online_node.onion_messenger.handle_onion_message(sender_node_id, &invreq_om);

	let mut events = always_online_node.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id,
			invoice_slot: _,
			reply_path,
			invoice_request,
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	always_online_node
		.node
		.respond_to_static_invoice_request(
			static_invoice.clone(),
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path.clone(),
		)
		.unwrap();

	// After calling `respond_to_static_invoice_request` the next two messages should be the
	// invoice request to the intended for the async recipient and the static invoice to the
	// payer.
	let invreq_om =
		always_online_node.onion_messenger.next_onion_message_for_peer(async_recipient_id).unwrap();
	let peeled_msg = async_recipient.onion_messenger.peel_onion_message(&invreq_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::InvoiceRequest(_), _, _)));

	let static_invoice_om =
		always_online_node.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();
	let peeled_msg = sender.onion_messenger.peel_onion_message(&static_invoice_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::StaticInvoice(_), _, _)));

	// Handling the `invoice_request` from the async recipient we should get back an invoice.
	async_recipient.onion_messenger.handle_onion_message(always_online_node_id, &invreq_om);
	let invoice_om =
		async_recipient.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();

	// First pay the static invoice.
	sender.onion_messenger.handle_onion_message(always_online_node_id, &static_invoice_om);

	let held_htlc_available_om_0_1 =
		sender.onion_messenger.next_onion_message_for_peer(always_online_node_id).unwrap();
	always_online_node
		.onion_messenger
		.handle_onion_message(sender_node_id, &held_htlc_available_om_0_1);
	let held_htlc_available_om_1_2 =
		always_online_node.onion_messenger.next_onion_message_for_peer(async_recipient_id).unwrap();
	async_recipient
		.onion_messenger
		.handle_onion_message(always_online_node_id, &held_htlc_available_om_1_2);

	let release_held_htlc_om =
		async_recipient.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();
	sender.onion_messenger.handle_onion_message(async_recipient_id, &release_held_htlc_om);

	let mut events = sender.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&always_online_node_id, &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(sender, 1);

	let route: &[&[&Node]] = &[&[always_online_node, async_recipient]];
	let args = PassAlongPathArgs::new(sender, route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let (res, _) =
		claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice.clone())));

	// After paying the static invoice, check that regular invoice received from async recipient is ignored.
	match sender.onion_messenger.peel_onion_message(&invoice_om) {
		Ok(PeeledOnion::Offers(OffersMessage::Invoice(invoice), context, _)) => {
			assert!(matches!(
				sender.node.send_payment_for_bolt12_invoice(&invoice, context.as_ref()),
				Err(Bolt12PaymentError::DuplicateInvoice)
			))
		},
		_ => panic!(),
	}

	// Now handle case where the sender pays regular invoice and ignores static invoice.
	let payment_id = PaymentId([2; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let invreq_om =
		sender.onion_messenger.next_onion_message_for_peer(always_online_node_id).unwrap();
	always_online_node.onion_messenger.handle_onion_message(sender_node_id, &invreq_om);

	let mut events = always_online_node.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id,
			invoice_slot: _,
			reply_path,
			invoice_request,
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	always_online_node
		.node
		.respond_to_static_invoice_request(
			static_invoice.clone(),
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path,
		)
		.unwrap();

	let invreq_om =
		always_online_node.onion_messenger.next_onion_message_for_peer(async_recipient_id).unwrap();
	let peeled_msg = async_recipient.onion_messenger.peel_onion_message(&invreq_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::InvoiceRequest(_), _, _)));

	let static_invoice_om =
		always_online_node.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();
	let peeled_msg = sender.onion_messenger.peel_onion_message(&static_invoice_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::StaticInvoice(_), _, _)));

	async_recipient.onion_messenger.handle_onion_message(always_online_node_id, &invreq_om);
	let invoice_om =
		async_recipient.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();

	let invoice = match sender.onion_messenger.peel_onion_message(&invoice_om) {
		Ok(PeeledOnion::Offers(OffersMessage::Invoice(invoice), _, _)) => invoice,
		_ => panic!(),
	};

	sender.onion_messenger.handle_onion_message(async_recipient_id, &invoice_om);

	let mut events = sender.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&always_online_node_id, &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(sender, 1);

	let args = PassAlongPathArgs::new(sender, route[0], amt_msat, payment_hash, ev)
		.without_clearing_recipient_events();
	do_pass_along_path(args);

	let payment_preimage = match get_event!(async_recipient, Event::PaymentClaimable) {
		Event::PaymentClaimable { purpose, .. } => purpose.preimage().unwrap(),
		_ => panic!("No Event::PaymentClaimable"),
	};

	// After paying invoice, check that static invoice is ignored.
	let res = claim_payment(sender, route[0], payment_preimage);
	assert_eq!(res, Some(PaidBolt12Invoice::Bolt12Invoice(invoice)));

	sender.onion_messenger.handle_onion_message(always_online_node_id, &static_invoice_om);
	let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(sender.node);
	assert!(async_pmts_msgs.is_empty());
	assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
}

#[test]
fn async_receive_flow_success() {
	// Test that an always-online sender can successfully pay an async receiver.

	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(3, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None]);

	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	assert!(static_invoice.invoice_features().supports_basic_mpp());
	let offer = nodes[2].node.get_async_receive_offer().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();
	let release_held_htlc_om = pass_async_payments_oms(
		static_invoice.clone(),
		&nodes[0],
		&nodes[1],
		&nodes[2],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	)
	.1;
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[2].node.get_our_node_id(), &release_held_htlc_om);

	// Check that we've queued the HTLCs of the async keysend payment.
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(nodes[0], 1);

	// Receiving a duplicate release_htlc message doesn't result in duplicate payment.
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[2].node.get_our_node_id(), &release_held_htlc_om);
	assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());

	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let (res, _) =
		claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
	assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice)));
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn expired_static_invoice_fail() {
	// Test that if we receive an expired static invoice we'll fail the payment.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[2].node.get_async_receive_offer().unwrap();

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let invreq_om = nodes[0]
		.onion_messenger
		.next_onion_message_for_peer(nodes[1].node.get_our_node_id())
		.unwrap();
	nodes[1].onion_messenger.handle_onion_message(nodes[0].node.get_our_node_id(), &invreq_om);

	let mut events = nodes[1].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested { reply_path, invoice_request, .. } => {
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	nodes[1]
		.node
		.respond_to_static_invoice_request(
			static_invoice.clone(),
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path,
		)
		.unwrap();
	let static_invoice_om = nodes[1]
		.onion_messenger
		.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
		.unwrap();

	// Wait until the static invoice expires before providing it to the sender.
	let block = create_dummy_block(
		nodes[0].best_block_hash(),
		(static_invoice.created_at() + static_invoice.relative_expiry()).as_secs() as u32 + 1u32,
		Vec::new(),
	);
	connect_block(&nodes[0], &block);
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);

	let events = nodes[0].node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	match events[0] {
		Event::PaymentFailed { payment_id: ev_payment_id, reason, .. } => {
			assert_eq!(reason.unwrap(), PaymentFailureReason::PaymentExpired);
			assert_eq!(ev_payment_id, payment_id);
		},
		_ => panic!(),
	}
	// TODO: the sender doesn't reply with InvoiceError right now because the always-online node
	// doesn't currently provide them with a reply path to do so.
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn timeout_unreleased_payment() {
	// If a server holds a pending HTLC for too long, payment is considered expired.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let sender = &nodes[0];
	let server = &nodes[1];
	let recipient = &nodes[2];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = recipient.node.get_async_receive_offer().unwrap();

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let invreq_om =
		sender.onion_messenger.next_onion_message_for_peer(server.node.get_our_node_id()).unwrap();
	server.onion_messenger.handle_onion_message(sender.node.get_our_node_id(), &invreq_om);

	let mut events = server.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested { reply_path, invoice_request, .. } => {
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	server
		.node
		.respond_to_static_invoice_request(
			static_invoice.clone(),
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path,
		)
		.unwrap();
	let static_invoice_om =
		server.onion_messenger.next_onion_message_for_peer(sender.node.get_our_node_id()).unwrap();

	// We handle the static invoice to held the pending HTLC
	sender.onion_messenger.handle_onion_message(server.node.get_our_node_id(), &static_invoice_om);

	// We advance enough time to expire the payment.
	// We add 2 hours as is the margin added to remove stale payments in non-std implementation.
	let timeout_time_expiry = TEST_ASYNC_PAYMENT_TIMEOUT_RELATIVE_EXPIRY
		+ Duration::from_secs(7200)
		+ Duration::from_secs(1);
	advance_time_by(timeout_time_expiry, sender);
	sender.node.timer_tick_occurred();
	let events = sender.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	match events[0] {
		Event::PaymentFailed { payment_id: ev_payment_id, reason, .. } => {
			assert_eq!(reason.unwrap(), PaymentFailureReason::PaymentExpired);
			assert_eq!(ev_payment_id, payment_id);
		},
		_ => panic!(),
	}
}

#[test]
fn async_receive_mpp() {
	// An MPP payment from an always-online sender to an often-offline recipient.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[None, Some(allow_priv_chan_fwds_cfg.clone()), Some(allow_priv_chan_fwds_cfg), None],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);

	// Create this network topology:
	//      n1
	//    /    \
	// n0       n3
	//    \    /
	//      n2
	create_announced_chan_between_nodes(&nodes, 0, 1);
	create_announced_chan_between_nodes(&nodes, 0, 2);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);

	// Ensure all nodes start at the same height.
	connect_blocks(&nodes[0], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
	connect_blocks(&nodes[1], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
	connect_blocks(&nodes[2], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
	connect_blocks(&nodes[3], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[3].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[3], &[&nodes[0], &nodes[1], &nodes[2]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[3], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[3].node.get_async_receive_offer().unwrap();

	let amt_msat = 15_000_000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();
	let release_held_htlc_om_3_0 = pass_async_payments_oms(
		static_invoice,
		&nodes[0],
		&nodes[1],
		&nodes[3],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	)
	.1;
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[3].node.get_our_node_id(), &release_held_htlc_om_3_0);
	check_added_monitors(&nodes[0], 2);

	let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 2);

	let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	let payment_hash = match ev {
		MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
			updates.update_add_htlcs[0].payment_hash
		},
		_ => panic!(),
	};

	let args = PassAlongPathArgs::new(&nodes[0], expected_route[0], amt_msat, payment_hash, ev)
		.without_claimable_event();
	do_pass_along_path(args);

	let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
	let args = PassAlongPathArgs::new(&nodes[0], expected_route[1], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = match claimable_ev {
		Event::PaymentClaimable {
			purpose: PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
			..
		} => payment_preimage.unwrap(),
		_ => panic!(),
	};
	claim_payment_along_route(ClaimAlongRouteArgs::new(
		&nodes[0],
		expected_route,
		keysend_preimage,
	));
}

#[test]
fn amount_doesnt_match_invreq() {
	// Ensure that we'll fail an async payment backwards if the amount in the HTLC is lower than the
	// amount from the original invoice request.
	let secp_ctx = Secp256k1::new();
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	// Make one blinded path's fees slightly higher so they are tried in a deterministic order.
	let mut higher_fee_chan_cfg = allow_priv_chan_fwds_cfg.clone();
	higher_fee_chan_cfg.channel_config.forwarding_fee_base_msat += 5000;
	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[None, Some(allow_priv_chan_fwds_cfg), Some(higher_fee_chan_cfg), None],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);

	// Create this network topology so nodes[0] has a blinded route hint to retry over.
	//      n1
	//    /    \
	// n0       n3
	//    \    /
	//      n2
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);

	// Ensure all nodes start at the same height.
	connect_blocks(&nodes[0], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
	connect_blocks(&nodes[1], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
	connect_blocks(&nodes[2], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
	connect_blocks(&nodes[3], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[3].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[3], &[&nodes[0], &nodes[1], &nodes[2]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[3], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[3].node.get_async_receive_offer().unwrap();

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();
	let release_held_htlc_om_3_0 = pass_async_payments_oms(
		static_invoice,
		&nodes[0],
		&nodes[1],
		&nodes[3],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	)
	.1;

	// Replace the invoice request contained within outbound_payments before sending so the invreq
	// amount doesn't match the onion amount when the HTLC gets to the recipient.
	let mut valid_invreq = None;
	nodes[0].node.test_modify_pending_payment(&payment_id, |pmt| {
		if let PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. } = pmt {
			valid_invreq = Some(invoice_request.clone());
			*invoice_request = offer
				.request_invoice(
					&nodes[0].keys_manager.get_expanded_key(),
					Nonce::from_entropy_source(nodes[0].keys_manager),
					&secp_ctx,
					payment_id,
				)
				.unwrap()
				.amount_msats(amt_msat + 1)
				.unwrap()
				.chain_hash(ChainHash::using_genesis_block(Network::Testnet))
				.unwrap()
				.build_and_sign()
				.unwrap();
		} else {
			panic!()
		}
	});

	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[3].node.get_our_node_id(), &release_held_htlc_om_3_0);
	check_added_monitors(&nodes[0], 1);

	// Check that we've queued the HTLCs of the async keysend payment.
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	assert!(matches!(
			ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
	let payment_hash = extract_payment_hash(&ev);

	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
		.without_claimable_event()
		.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
	do_pass_along_path(args);

	// Modify the invoice request stored in our outbounds to be the correct one, to make sure the
	// payment retry will succeed after we finish failing the invalid HTLC back.
	nodes[0].node.test_modify_pending_payment(&payment_id, |pmt| {
		if let PendingOutboundPayment::Retryable { invoice_request, .. } = pmt {
			*invoice_request = valid_invreq.take();
		} else {
			panic!()
		}
	});

	fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1], &nodes[3]], true);

	// The retry with the correct invoice request should succeed.
	nodes[0].node.process_pending_htlc_forwards();
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let mut ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
	assert!(matches!(
				ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
	check_added_monitors!(nodes[0], 1);
	let route: &[&[&Node]] = &[&[&nodes[2], &nodes[3]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
}

#[test]
fn reject_missing_invreq() {
	// Ensure we'll fail an async payment backwards if the HTLC onion doesn't contain the sender's
	// original invoice request.
	let mut valid_invreq: Mutex<Option<InvoiceRequest>> = Mutex::new(None);

	invalid_async_receive_with_retry(
		|sender, _, payment_id| {
			// Remove the invoice request from our Retryable payment so we don't include it in the onion on
			// retry.
			sender.node.test_modify_pending_payment(&payment_id, |pmt| {
				if let PendingOutboundPayment::Retryable { invoice_request, .. } = pmt {
					assert!(invoice_request.is_some());
					*valid_invreq.lock().unwrap() = invoice_request.take();
				} else {
					panic!()
				}
			});
		},
		|sender, payment_id| {
			// Re-add the invoice request so we include it in the onion on the next retry.
			sender.node.test_modify_pending_payment(&payment_id, |pmt| {
				if let PendingOutboundPayment::Retryable { invoice_request, .. } = pmt {
					*invoice_request = valid_invreq.lock().unwrap().take();
				} else {
					panic!()
				}
			});
		},
	);
}

#[test]
fn reject_bad_payment_secret() {
	// Ensure we'll fail an async payment backwards if the payment secret in the onion is invalid.

	let mut valid_payment_params: Mutex<Option<PaymentParameters>> = Mutex::new(None);
	invalid_async_receive_with_retry(
		|sender, recipient, payment_id| {
			// Store invalid payment paths in the sender's outbound Retryable payment to induce the failure
			// on the recipient's end. Store multiple paths so the sender still thinks they can retry after
			// the failure we're about to cause below.
			let mut invalid_blinded_payment_paths = Vec::new();
			for i in 0..2 {
				let mut paths = recipient
					.node
					.test_create_blinded_payment_paths(
						None,
						PaymentSecret([42; 32]), // invalid payment secret
						PaymentContext::AsyncBolt12Offer(AsyncBolt12OfferContext {
							// We don't reach the point of checking the invreq nonce due to the invalid payment secret
							offer_nonce: Nonce([i; Nonce::LENGTH]),
						}),
						u32::MAX,
					)
					.unwrap();
				invalid_blinded_payment_paths.append(&mut paths);
			}

			// Modify the outbound payment parameters to use payment paths with an invalid payment secret.
			sender.node.test_modify_pending_payment(&payment_id, |pmt| {
				if let PendingOutboundPayment::Retryable { ref mut payment_params, .. } = pmt {
					assert!(payment_params.is_some());
					let valid_params = payment_params.clone();
					if let Payee::Blinded { ref mut route_hints, .. } =
						&mut payment_params.as_mut().unwrap().payee
					{
						core::mem::swap(route_hints, &mut invalid_blinded_payment_paths);
					} else {
						panic!()
					}
					*valid_payment_params.lock().unwrap() = valid_params;
				} else {
					panic!()
				}
			});
		},
		|sender, payment_id| {
			// Re-add the valid payment params so we use the right payment secret on the next retry.
			sender.node.test_modify_pending_payment(&payment_id, |pmt| {
				if let PendingOutboundPayment::Retryable { payment_params, .. } = pmt {
					*payment_params = valid_payment_params.lock().unwrap().take();
				} else {
					panic!()
				}
			});
		},
	);
}

fn invalid_async_receive_with_retry<F1, F2>(
	mut modify_outbounds_for_failure: F1, mut modify_outbounds_for_success: F2,
) where
	F1: FnMut(&Node, &Node, PaymentId),
	F2: FnMut(&Node, PaymentId),
{
	let secp_ctx = Secp256k1::new();
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(3, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None]);

	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	let entropy_source = nodes[2].keys_manager;
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	// Set the random bytes so we can predict the offer nonce.
	let hardcoded_random_bytes = [42; 32];
	*nodes[2].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);

	// Ensure all nodes start at the same height.
	connect_blocks(&nodes[0], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
	connect_blocks(&nodes[1], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
	connect_blocks(&nodes[2], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);

	let blinded_paths_to_always_online_node = nodes[1]
		.message_router
		.create_blinded_paths(
			nodes[1].node.get_our_node_id(),
			nodes[1].keys_manager.get_receive_auth_key(),
			MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
			Vec::new(),
			&secp_ctx,
		)
		.unwrap();
	let (offer_builder, offer_nonce) = nodes[2]
		.node
		.flow
		.create_async_receive_offer_builder(entropy_source, blinded_paths_to_always_online_node)
		.unwrap();
	let offer = offer_builder.build().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);

	// Hardcode the payment paths so nodes[0] has something to retry over. Set all of these paths to
	// use the same nodes to avoid complicating the test with a bunch of extra nodes.
	let mut static_invoice_paths = Vec::new();
	for _ in 0..3 {
		let static_inv_for_path =
			create_static_invoice_builder(&nodes[2], &offer, offer_nonce, None)
				.build_and_sign(&secp_ctx)
				.unwrap();
		static_invoice_paths.push(static_inv_for_path.payment_paths()[0].clone());
	}
	nodes[2].router.expect_blinded_payment_paths(static_invoice_paths);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[2].node.get_async_receive_offer().unwrap();

	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();
	let release_held_htlc_om_2_0 = pass_async_payments_oms(
		static_invoice,
		&nodes[0],
		&nodes[1],
		&nodes[2],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	)
	.1;
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[2].node.get_our_node_id(), &release_held_htlc_om_2_0);
	check_added_monitors(&nodes[0], 1);

	// Check that we've queued the HTLCs of the async keysend payment.
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	assert!(matches!(
					ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
	let payment_hash = extract_payment_hash(&ev);

	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
	do_pass_along_path(args);

	// Fail the HTLC backwards to enable us to more easily modify the now-Retryable outbound to test
	// failures on the recipient's end.
	nodes[2].node.fail_htlc_backwards(&payment_hash);
	expect_htlc_failure_conditions(
		nodes[2].node.get_and_clear_pending_events(),
		&[HTLCHandlingFailureType::Receive { payment_hash }],
	);
	nodes[2].node.process_pending_htlc_forwards();
	check_added_monitors!(nodes[2], 1);
	fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1], &nodes[2]], true);

	// Trigger a retry and make sure it fails after calling the closure that induces recipient
	// failure.
	modify_outbounds_for_failure(&nodes[0], &nodes[2], payment_id);
	nodes[0].node.process_pending_htlc_forwards();
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	assert!(matches!(
						ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
	check_added_monitors!(nodes[0], 1);
	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
		.without_claimable_event()
		.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
	do_pass_along_path(args);
	fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1], &nodes[2]], true);

	// The retry after calling the 2nd closure should succeed.
	modify_outbounds_for_success(&nodes[0], payment_id);
	nodes[0].node.process_pending_htlc_forwards();
	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	check_added_monitors!(nodes[0], 1);
	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn expired_static_invoice_message_path() {
	// Test that if we receive a held_htlc_available message over an expired blinded path, we'll
	// ignore it.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[2].node.get_async_receive_offer().unwrap();

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	// While the invoice is unexpired, respond with release_held_htlc.
	let (held_htlc_available_om, _release_held_htlc_om) = pass_async_payments_oms(
		static_invoice.clone(),
		&nodes[0],
		&nodes[1],
		&nodes[2],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	);

	// After the invoice is expired, ignore inbound held_htlc_available messages over the path.
	let path_absolute_expiry = crate::ln::inbound_payment::calculate_absolute_expiry(
		nodes[2].node.duration_since_epoch().as_secs(),
		static_invoice.relative_expiry().as_secs() as u32,
	);
	let block = create_dummy_block(
		nodes[2].best_block_hash(),
		(path_absolute_expiry + 1) as u32,
		Vec::new(),
	);
	connect_block(&nodes[2], &block);
	nodes[2]
		.onion_messenger
		.handle_onion_message(nodes[1].node.get_our_node_id(), &held_htlc_available_om);
	for i in 0..2 {
		assert!(nodes[2]
			.onion_messenger
			.next_onion_message_for_peer(nodes[i].node.get_our_node_id())
			.is_none());
	}
}

#[test]
fn expired_static_invoice_payment_path() {
	// Test that we'll reject inbound payments to expired payment paths.
	let secp_ctx = Secp256k1::new();
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(3, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None]);

	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[1].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[2].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	// Make sure all nodes are at the same block height in preparation for CLTV timeout things.
	let node_max_height =
		nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
	connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
	connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
	connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);

	// Hardcode the blinded payment path returned by the router so we can expire it via mining blocks.
	let (_, static_invoice_expired_paths) =
		create_static_invoice(&nodes[1], &nodes[2], None, &secp_ctx);
	nodes[2]
		.router
		.expect_blinded_payment_paths(static_invoice_expired_paths.payment_paths().to_vec());

	// Extract the expiry height from the to-be-expired blinded payment path.
	let final_max_cltv_expiry = {
		let mut blinded_path = static_invoice_expired_paths.payment_paths().to_vec().pop().unwrap();
		blinded_path
			.advance_path_by_one(&nodes[1].keys_manager, &nodes[1].node, &secp_ctx)
			.unwrap();
		match blinded_path.decrypt_intro_payload(&nodes[2].keys_manager).unwrap().0 {
			BlindedPaymentTlvs::Receive(tlvs) => tlvs.tlvs.payment_constraints.max_cltv_expiry,
			_ => panic!(),
		}
	};

	// Mine a bunch of blocks so the hardcoded path's `max_cltv_expiry` is expired at the recipient's
	// end by the time the payment arrives.
	let min_cltv_expiry_delta = test_default_channel_config().channel_config.cltv_expiry_delta;
	connect_blocks(
		&nodes[0],
		final_max_cltv_expiry
			- nodes[0].best_block_info().1
			- min_cltv_expiry_delta as u32
			- HTLC_FAIL_BACK_BUFFER
			- LATENCY_GRACE_PERIOD_BLOCKS
			- 1,
	);
	connect_blocks(
		&nodes[1],
		final_max_cltv_expiry
			- nodes[1].best_block_info().1
			// Don't expire the path for nodes[1]
			- min_cltv_expiry_delta as u32
			- HTLC_FAIL_BACK_BUFFER
			- LATENCY_GRACE_PERIOD_BLOCKS
			- 1,
	);
	connect_blocks(&nodes[2], final_max_cltv_expiry - nodes[2].best_block_info().1);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	let offer = nodes[2].node.get_async_receive_offer().unwrap();

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	let mut params: OptionalOfferPaymentParams = Default::default();
	params.retry_strategy = Retry::Attempts(0);
	nodes[0].node.pay_for_offer(&offer, Some(amt_msat), payment_id, params).unwrap();
	let release_held_htlc_om = pass_async_payments_oms(
		static_invoice,
		&nodes[0],
		&nodes[1],
		&nodes[2],
		recipient_id,
		invoice_flow_res.invoice_request_path,
	)
	.1;
	nodes[0]
		.onion_messenger
		.handle_onion_message(nodes[2].node.get_our_node_id(), &release_held_htlc_om);

	let mut events = nodes[0].node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(nodes[0], 1);

	let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
	let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
		.without_claimable_event()
		.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
	do_pass_along_path(args);
	fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1], &nodes[2]], false);
	nodes[2].logger.assert_log_contains(
		"lightning::ln::channelmanager",
		"violated blinded payment constraints",
		1,
	);
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn ignore_expired_offer_paths_request() {
	// Ignore an incoming `OfferPathsRequest` if it is sent over a blinded path that is expired.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	const OFFER_PATHS_REQ_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60);
	let recipient_id = vec![42; 32];
	let inv_server_paths = server
		.node
		.blinded_paths_for_async_recipient(recipient_id, Some(OFFER_PATHS_REQ_RELATIVE_EXPIRY))
		.unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// Retrieve the offer paths request, and check that before the path that the recipient was
	// configured with expires the server will respond to it, and after the config path expires they
	// won't.
	recipient.node.timer_tick_occurred();
	let offer_paths_req = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	assert!(matches!(
		server.onion_messenger.peel_onion_message(&offer_paths_req).unwrap(),
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _)
	));
	recipient.onion_messenger.release_pending_msgs(); // Ignore redundant paths requests

	// Prior to the config path expiry the server will respond with offer_paths:
	server.onion_messenger.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req);
	let offer_paths = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	assert!(matches!(
		recipient.onion_messenger.peel_onion_message(&offer_paths).unwrap(),
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPaths(_), _, _)
	));
	server.onion_messenger.release_pending_msgs(); // Ignore redundant offer_paths

	// After the config path expiry the offer paths request will be ignored:
	advance_time_by(OFFER_PATHS_REQ_RELATIVE_EXPIRY + Duration::from_secs(1), server);
	server.onion_messenger.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req);
	assert!(server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.is_none());
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn ignore_expired_offer_paths_message() {
	// If the recipient receives an offer_paths message over an expired reply path, it should be ignored.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id, None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// First retrieve the offer_paths_request and corresponding offer_paths response from the server.
	recipient.node.timer_tick_occurred();
	let offer_paths_req = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	recipient.onion_messenger.release_pending_msgs(); // Ignore redundant paths requests
	server.onion_messenger.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req);
	let offer_paths = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	assert!(matches!(
		recipient.onion_messenger.peel_onion_message(&offer_paths).unwrap(),
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPaths(_), _, _)
	));

	// Prior to expiry of the offer_paths_request reply path, the recipient will respond to
	// offer_paths with serve_static_invoice.
	recipient.onion_messenger.handle_onion_message(server.node.get_our_node_id(), &offer_paths);
	let serve_static_invoice = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	assert!(matches!(
		server.onion_messenger.peel_onion_message(&serve_static_invoice).unwrap(),
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::ServeStaticInvoice(_), _, _)
	));

	// Manually advance time for the recipient so they will perceive the offer_paths message as being
	// sent over an expired reply path, and not respond with serve_static_invoice.
	advance_time_by(TEST_TEMP_REPLY_PATH_RELATIVE_EXPIRY + Duration::from_secs(1), recipient);
	recipient.onion_messenger.handle_onion_message(server.node.get_our_node_id(), &offer_paths);
	assert!(recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.is_none());
}

#[test]
fn limit_offer_paths_requests() {
	// Limit the number of offer_paths_requests sent to the server if they aren't responding.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id, None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[1], &[&nodes[0]]);

	// Up to TEST_MAX_UPDATE_ATTEMPTS offer_paths_requests are allowed to be sent out before the async
	// recipient should give up.
	// Subtract 1 because we sent the first request when invoice server paths were set above.
	for _ in 0..TEST_MAX_UPDATE_ATTEMPTS - 1 {
		recipient.node.test_check_refresh_async_receive_offers();
		let offer_paths_req = recipient
			.onion_messenger
			.next_onion_message_for_peer(server.node.get_our_node_id())
			.unwrap();
		assert!(matches!(
			server.onion_messenger.peel_onion_message(&offer_paths_req).unwrap(),
			PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _)
		));
		recipient.onion_messenger.release_pending_msgs(); // Ignore redundant paths requests
	}

	// After the recipient runs out of attempts to request offer paths, they will give up until the
	// next timer tick.
	recipient.node.test_check_refresh_async_receive_offers();
	assert!(recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.is_none());

	// On the next timer tick, more offer paths requests should be allowed to go through.
	recipient.node.timer_tick_occurred();
	let offer_paths_req = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	assert!(matches!(
		server.onion_messenger.peel_onion_message(&offer_paths_req).unwrap(),
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _)
	));
}

#[test]
fn limit_serve_static_invoice_requests() {
	// If we have enough async receive offers cached already, the recipient should stop sending out
	// offer_paths_requests.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// Build the target number of offers interactively with the static invoice server.
	let mut offer_paths_req = None;
	let mut invoice_slots = new_hash_set();
	for expected_inv_slot in 0..TEST_MAX_CACHED_OFFERS_TARGET {
		let flow_res = pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
		assert_eq!(flow_res.invoice_slot, expected_inv_slot as u16);

		offer_paths_req = Some(flow_res.offer_paths_request);
		invoice_slots.insert(flow_res.invoice_slot);

		// Trigger a cache refresh
		recipient.node.timer_tick_occurred();
	}
	assert_eq!(
		recipient.node.flow.test_get_async_receive_offers().len(),
		TEST_MAX_CACHED_OFFERS_TARGET
	);
	// Check that all invoice slot numbers are unique.
	assert_eq!(invoice_slots.len(), TEST_MAX_CACHED_OFFERS_TARGET);

	// Force allowing more offer paths request attempts so we can check that the recipient will not
	// attempt to build any further offers.
	recipient.node.timer_tick_occurred();
	assert!(recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.is_none());

	// If the recipient now receives new offer_paths, they should not attempt to build new offers as
	// they already have enough.
	server
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req.unwrap());
	let offer_paths = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient.onion_messenger.handle_onion_message(server.node.get_our_node_id(), &offer_paths);
	assert!(recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.is_none());
}

#[test]
fn offer_cache_round_trip_ser() {
	// Check that the async payments offer cache survives round trip serialization within the
	// `ChannelManager`.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let persister;
	let chain_monitor;
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let payee_node_deserialized;
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	let chan_id =
		create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.channel_id;
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// Build the target number of offers interactively with the static invoice server.
	for _ in 0..TEST_MAX_CACHED_OFFERS_TARGET {
		pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
		// Trigger a cache refresh
		recipient.node.timer_tick_occurred();
	}

	// Check that round trip serialization of the ChannelManager will result in identical stored
	// offers.
	let cached_offers_pre_ser = recipient.node.flow.test_get_async_receive_offers();
	let config = test_default_channel_config();
	let serialized_monitor = get_monitor!(recipient, chan_id).encode();
	reload_node!(
		nodes[1],
		config,
		recipient.node.encode(),
		&[&serialized_monitor],
		persister,
		chain_monitor,
		payee_node_deserialized
	);
	let recipient = &nodes[1];
	let cached_offers_post_ser = recipient.node.flow.test_get_async_receive_offers();
	assert_eq!(cached_offers_pre_ser, cached_offers_post_ser);
}

#[test]
fn refresh_static_invoices_for_pending_offers() {
	// Check that an invoice for an  offer that is pending persistence with the server will be updated
	// every timer tick.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[1], &[&nodes[0]]);

	// Set up the recipient to have one offer pending with the static invoice server.
	invoice_flow_up_to_send_serve_static_invoice(server, recipient);

	// Every timer tick, we'll send a fresh invoice to the server.
	for _ in 0..10 {
		recipient.node.timer_tick_occurred();
		let pending_oms = recipient.onion_messenger.release_pending_msgs();
		pending_oms
			.get(&server.node.get_our_node_id())
			.unwrap()
			.iter()
			.find(|msg| match server.onion_messenger.peel_onion_message(&msg).unwrap() {
				PeeledOnion::AsyncPayments(AsyncPaymentsMessage::ServeStaticInvoice(_), _, _) => {
					true
				},
				PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _) => {
					false
				},
				_ => panic!("Unexpected message"),
			})
			.unwrap();
	}
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn refresh_static_invoices_for_used_offers() {
	// Check that an invoice for a used offer stored with the server will be updated every
	// INVOICE_REFRESH_THRESHOLD.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(3, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None]);

	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	let sender = &nodes[0];
	let server = &nodes[1];
	let recipient = &nodes[2];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	// Set up the recipient to have one offer and an invoice with the static invoice server.
	let flow_res = pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
	let original_invoice = flow_res.invoice;
	// Mark the offer as used so we'll update the invoice after INVOICE_REFRESH_THRESHOLD.
	let _offer = recipient.node.get_async_receive_offer().unwrap();

	// Force the server and recipient to send OMs directly to each other for testing simplicity.
	server.message_router.peers_override.lock().unwrap().push(recipient.node.get_our_node_id());
	recipient.message_router.peers_override.lock().unwrap().push(server.node.get_our_node_id());

	// Prior to INVOICE_REFRESH_THRESHOLD, we won't refresh the invoice.
	advance_time_by(TEST_INVOICE_REFRESH_THRESHOLD, recipient);
	recipient.node.timer_tick_occurred();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	// After INVOICE_REFRESH_THRESHOLD, we will refresh the invoice.
	advance_time_by(Duration::from_secs(1), recipient);
	recipient.node.timer_tick_occurred();
	let pending_oms = recipient.onion_messenger.release_pending_msgs();
	let serve_static_invoice_om = pending_oms
		.get(&server.node.get_our_node_id())
		.unwrap()
		.iter()
		.find(|msg| match server.onion_messenger.peel_onion_message(&msg).unwrap() {
			PeeledOnion::AsyncPayments(AsyncPaymentsMessage::ServeStaticInvoice(_), _, _) => true,
			PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPathsRequest(_), _, _) => false,
			_ => panic!("Unexpected message"),
		})
		.unwrap();

	server
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &serve_static_invoice_om);
	let mut events = server.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (updated_invoice, ack_path, invoice_request_path) = match events.pop().unwrap() {
		Event::PersistStaticInvoice {
			invoice,
			invoice_slot,
			invoice_persisted_path,
			recipient_id: ev_id,
			invoice_request_path,
		} => {
			assert_ne!(original_invoice, invoice);
			assert_eq!(recipient_id, ev_id);
			assert_eq!(invoice_slot, flow_res.invoice_slot);
			// When we update the invoice corresponding to a specific offer, the invoice_slot stays the
			// same.
			assert_eq!(invoice_slot, flow_res.invoice_slot);
			(invoice, invoice_persisted_path, invoice_request_path)
		},
		_ => panic!(),
	};
	server.node.static_invoice_persisted(ack_path);
	let invoice_persisted_om = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient
		.onion_messenger
		.handle_onion_message(server.node.get_our_node_id(), &invoice_persisted_om);
	assert_eq!(recipient.node.flow.test_get_async_receive_offers().len(), 1);

	// Remove the peer restriction added above.
	server.message_router.peers_override.lock().unwrap().clear();
	recipient.message_router.peers_override.lock().unwrap().clear();

	// Complete a payment to the new invoice.
	let offer = recipient.node.get_async_receive_offer().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let release_held_htlc_om = pass_async_payments_oms(
		updated_invoice.clone(),
		sender,
		server,
		recipient,
		recipient_id,
		invoice_request_path,
	)
	.1;
	sender
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &release_held_htlc_om);

	let mut events = sender.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&server.node.get_our_node_id(), &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(sender, 1);

	let route: &[&[&Node]] = &[&[server, recipient]];
	let args = PassAlongPathArgs::new(sender, route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let res = claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res.0, Some(PaidBolt12Invoice::StaticInvoice(updated_invoice)));
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn ignore_expired_static_invoice() {
	// If a server receives an expired static invoice to persist, they should ignore it and not
	// generate an event.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];
	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id, None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	let (_, serve_static_invoice_om) =
		invoice_flow_up_to_send_serve_static_invoice(server, recipient);

	// Advance time for the server so that by the time it receives the serve_static_invoice message,
	// the invoice within has expired.
	advance_time_by(TEST_DEFAULT_ASYNC_RECEIVE_OFFER_EXPIRY + Duration::from_secs(1), server);

	// Check that no Event::PersistStaticInvoice is generated.
	server
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &serve_static_invoice_om);
	let mut events = server.node.get_and_clear_pending_events();
	assert!(events.is_empty());
}

#[test]
fn ignore_offer_paths_expiry_too_soon() {
	// Recipents should ignore received offer_paths that expire too soon.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];
	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id, None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// Get a legit offer_paths message from the server.
	recipient.node.timer_tick_occurred();
	let offer_paths_req = recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.unwrap();
	recipient.onion_messenger.release_pending_msgs();
	server.onion_messenger.handle_onion_message(recipient.node.get_our_node_id(), &offer_paths_req);
	let offer_paths = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();

	// Get the blinded path use when manually sending the modified offer_paths message to the
	// recipient.
	let offer_paths_req_reply_path =
		match server.onion_messenger.peel_onion_message(&offer_paths_req) {
			Ok(PeeledOnion::AsyncPayments(
				AsyncPaymentsMessage::OfferPathsRequest(_),
				_,
				reply_path,
			)) => reply_path.unwrap(),
			_ => panic!(),
		};

	// Modify the offer_paths message from the server to indicate that the offer paths expire too
	// soon.
	let (mut offer_paths_unwrapped, ctx) = match recipient
		.onion_messenger
		.peel_onion_message(&offer_paths)
	{
		Ok(PeeledOnion::AsyncPayments(AsyncPaymentsMessage::OfferPaths(msg), ctx, _)) => (msg, ctx),
		_ => panic!(),
	};
	let too_soon_expiry_secs = recipient
		.node
		.duration_since_epoch()
		.as_secs()
		.saturating_add(TEST_MIN_OFFER_PATHS_RELATIVE_EXPIRY_SECS - 1);
	offer_paths_unwrapped.paths_absolute_expiry = Some(too_soon_expiry_secs);

	// Deliver the expired paths to the recipient and make sure they don't construct a
	// serve_static_invoice message in response.
	server
		.onion_messenger
		.send_onion_message(
			ParsedOnionMessageContents::<Infallible>::AsyncPayments(
				AsyncPaymentsMessage::OfferPaths(offer_paths_unwrapped),
			),
			MessageSendInstructions::WithReplyPath {
				destination: Destination::BlindedPath(offer_paths_req_reply_path),
				// This context isn't used because the recipient doesn't reply to the message
				context: MessageContext::AsyncPayments(ctx),
			},
		)
		.unwrap();
	let offer_paths_expiry_too_soon = server
		.onion_messenger
		.next_onion_message_for_peer(recipient.node.get_our_node_id())
		.unwrap();
	recipient
		.onion_messenger
		.handle_onion_message(server.node.get_our_node_id(), &offer_paths_expiry_too_soon);
	assert!(recipient
		.onion_messenger
		.next_onion_message_for_peer(server.node.get_our_node_id())
		.is_none());
}

#[test]
fn ignore_duplicate_offer() {
	// Test that if an async receiver gets notified that the invoice for an offer was persisted twice,
	// they won't cache the offer twice.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[0].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[1].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[0], &nodes[1], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;
	assert!(static_invoice.invoice_features().supports_basic_mpp());
	assert!(nodes[1].node.get_async_receive_offer().is_ok());

	// Check that the recipient will ignore duplicate offers received.
	nodes[1].onion_messenger.handle_onion_message(
		nodes[1].node.get_our_node_id(),
		&invoice_flow_res.static_invoice_persisted_message,
	);
	assert_eq!(nodes[1].node.flow.test_get_async_receive_offers().len(), 1);
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn remove_expired_offer_from_cache() {
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		nodes[0].node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	nodes[1].node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	pass_static_invoice_server_messages(&nodes[0], &nodes[1], recipient_id.clone());

	// We'll be able to retrieve the offer before it expires.
	assert!(nodes[1].node.get_async_receive_offer().is_ok());

	// After the offer expires we'll no longer return it from the API.
	advance_time_by(TEST_DEFAULT_ASYNC_RECEIVE_OFFER_EXPIRY + Duration::from_secs(1), recipient);
	assert!(nodes[1].node.get_async_receive_offer().is_err());
}

#[cfg_attr(feature = "std", ignore)]
#[test]
fn refresh_unused_offers() {
	// Check that if a user has an unused offer older than TEST_OFFER_REFRESH_THRESHOLD, they will
	// replace it with a fresh offer.
	let chanmon_cfgs = create_chanmon_cfgs(2);
	let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
	let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	let server = &nodes[0];
	let recipient = &nodes[1];

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();

	// First fill up the offer cache.
	for _ in 0..(TEST_MAX_CACHED_OFFERS_TARGET - 1) {
		// Trigger a cache refresh
		recipient.node.timer_tick_occurred();

		pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
	}

	// Have the last offer expire later than the others.
	advance_time_by(Duration::from_secs(1), recipient);
	pass_static_invoice_server_messages(server, recipient, recipient_id.clone());

	// Before the threshold, the recipient will not attempt to update any offers.
	advance_time_by(TEST_OFFER_REFRESH_THRESHOLD - Duration::from_secs(2), recipient);
	assert!(recipient
		.onion_messenger
		.release_pending_msgs()
		.get(&server.node.get_our_node_id())
		.unwrap()
		.is_empty());

	// After the threshold time passes, the recipient will attempt to replace all of their offers
	// (which are all unused) except the last.
	advance_time_by(Duration::from_secs(2), recipient);
	for expected_invoice_slot in 0..(TEST_MAX_CACHED_OFFERS_TARGET - 1) {
		// Trigger a cache refresh
		recipient.node.timer_tick_occurred();

		let flow_res = pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
		assert_eq!(flow_res.invoice_slot, expected_invoice_slot as u16);
	}
	recipient.node.timer_tick_occurred();
	assert!(recipient
		.onion_messenger
		.release_pending_msgs()
		.get(&server.node.get_our_node_id())
		.unwrap()
		.is_empty());

	// The recipient will update the last offer after the threshold time has passed.
	advance_time_by(Duration::from_secs(1), recipient);
	recipient.node.timer_tick_occurred();
	let flow_res = pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
	assert_eq!(flow_res.invoice_slot, TEST_MAX_CACHED_OFFERS_TARGET as u16 - 1);

	// If an offer is used, we shouldn't replace it after the threshold time.
	let offer = recipient.node.get_async_receive_offer().unwrap();
	advance_time_by(TEST_OFFER_REFRESH_THRESHOLD + Duration::from_secs(100), recipient);

	// All offers besides the used one should be successfully replaced.
	for _ in 0..(TEST_MAX_CACHED_OFFERS_TARGET - 1) {
		recipient.node.timer_tick_occurred();
		pass_static_invoice_server_messages(server, recipient, recipient_id.clone());
	}

	// The used offer should only get an invoice update.
	recipient.node.timer_tick_occurred();
	let mut pending_oms = recipient
		.onion_messenger
		.release_pending_msgs()
		.remove(&server.node.get_our_node_id())
		.unwrap();
	assert_eq!(pending_oms.len(), 1);
	let peeled_om = server.onion_messenger.peel_onion_message(&pending_oms[0]).unwrap();
	match peeled_om {
		PeeledOnion::AsyncPayments(AsyncPaymentsMessage::ServeStaticInvoice(msg), _, _) => {
			assert_eq!(msg.invoice.offer_message_paths(), offer.paths());
		},
		_ => panic!(),
	}

	// Check that the used offer is still in the cache
	assert!(recipient.node.flow.test_get_async_receive_offers().contains(&offer));
}

#[test]
fn invoice_server_is_not_channel_peer() {
	// Test that the async recipient's static invoice server does not need to be a channel peer for an
	// async payment to successfully complete.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let mut allow_priv_chan_fwds_cfg = test_default_channel_config();
	allow_priv_chan_fwds_cfg.accept_forwards_to_priv_channels = true;
	let node_chanmgrs =
		create_node_chanmgrs(4, &node_cfgs, &[None, Some(allow_priv_chan_fwds_cfg), None, None]);

	// Set up a network:
	//
	//         static_invoice_server
	//        /
	// sender -- forwarding_node ---- recipient
	//
	// So the static invoice server has no channels with the recipient.

	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	let sender = &nodes[0];
	let forwarding_node = &nodes[1];
	let recipient = &nodes[2];
	let invoice_server = &nodes[3];
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 3, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let recipient_id = vec![42; 32];
	let inv_server_paths =
		invoice_server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1], &nodes[3]]);
	let flow_res =
		pass_static_invoice_server_messages(invoice_server, recipient, recipient_id.clone());
	let invoice = flow_res.invoice;

	let offer = recipient.node.get_async_receive_offer().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	// Do the held_htlc_available --> release_held_htlc dance.
	let release_held_htlc_om = pass_async_payments_oms(
		invoice.clone(),
		sender,
		invoice_server,
		recipient,
		recipient_id,
		flow_res.invoice_request_path,
	)
	.1;
	sender
		.onion_messenger
		.handle_onion_message(recipient.node.get_our_node_id(), &release_held_htlc_om);

	// Check that the sender has queued the HTLCs of the async keysend payment.
	let mut events = sender.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&forwarding_node.node.get_our_node_id(), &mut events);
	let payment_hash = extract_payment_hash(&ev);
	check_added_monitors!(sender, 1);

	let route: &[&[&Node]] = &[&[forwarding_node, recipient]];
	let args = PassAlongPathArgs::new(sender, route[0], amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let res = claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res.0, Some(PaidBolt12Invoice::StaticInvoice(invoice)));
}

#[test]
fn invoice_request_forwarded_to_async_recipient() {
	// Test that when an always-online node receives a static invoice request on behalf of an async
	// recipient it forwards the invoice request to the async recipient and also sends back the
	// static invoice to the payer.
	let chanmon_cfgs = create_chanmon_cfgs(3);
	let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
	let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
	let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
	create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);

	let sender = &nodes[0];
	let always_online_node = &nodes[1];
	let async_recipient = &nodes[2];

	let recipient_id = vec![42; 32];
	let inv_server_paths = always_online_node
		.node
		.blinded_paths_for_async_recipient(recipient_id.clone(), None)
		.unwrap();
	async_recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(&nodes[2], &[&nodes[0], &nodes[1]]);

	let invoice_flow_res =
		pass_static_invoice_server_messages(&nodes[1], &nodes[2], recipient_id.clone());
	let static_invoice = invoice_flow_res.invoice;

	let offer = async_recipient.node.get_async_receive_offer().unwrap();
	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	let sender_node_id = sender.node.get_our_node_id();

	// `invoice_request` message intended for the always-online node that receives requests on
	// behalf of async recipient.
	let invreq_om = sender
		.onion_messenger
		.next_onion_message_for_peer(always_online_node.node.get_our_node_id())
		.unwrap();

	always_online_node.onion_messenger.handle_onion_message(sender_node_id, &invreq_om);

	let mut events = always_online_node.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invoice_request) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id,
			invoice_slot: _,
			reply_path,
			invoice_request,
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	always_online_node
		.node
		.respond_to_static_invoice_request(
			static_invoice,
			reply_path,
			invoice_request,
			invoice_flow_res.invoice_request_path,
		)
		.unwrap();

	// Check that the next onion messages are the invoice request that will be forwarded to the async
	// recipient and the static invoice to the payer.
	let invreq_om = always_online_node
		.onion_messenger
		.next_onion_message_for_peer(async_recipient.node.get_our_node_id())
		.unwrap();

	let static_invoice_om =
		always_online_node.onion_messenger.next_onion_message_for_peer(sender_node_id).unwrap();

	let peeled_msg = async_recipient.onion_messenger.peel_onion_message(&invreq_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::InvoiceRequest(_), _, _)));

	let peeled_msg = sender.onion_messenger.peel_onion_message(&static_invoice_om).unwrap();
	assert!(matches!(peeled_msg, PeeledOnion::Offers(OffersMessage::StaticInvoice(_), _, _)));
}

#[test]
fn async_payment_e2e() {
	// Test the end-to-end flow of an async sender paying an async recipient.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut sender_lsp_cfg = test_default_channel_config();
	sender_lsp_cfg.enable_htlc_hold = true;
	let mut invoice_server_cfg = test_default_channel_config();
	invoice_server_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(sender_lsp_cfg), Some(invoice_server_cfg), Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];
	let invoice_server = &nodes[2];
	let recipient = &nodes[3];

	// Retrieve the offer then disconnect the recipient from their LSP to simulate them going offline.
	let recipient_id = vec![42; 32];
	let inv_server_paths =
		invoice_server.node.blinded_paths_for_async_recipient(recipient_id.clone(), None).unwrap();
	recipient.node.set_paths_to_static_invoice_server(inv_server_paths).unwrap();
	expect_offer_paths_requests(recipient, &[invoice_server, sender_lsp]);
	let invoice_flow_res =
		pass_static_invoice_server_messages(invoice_server, recipient, recipient_id.clone());
	let invoice = invoice_flow_res.invoice;
	let invreq_path = invoice_flow_res.invoice_request_path;

	let offer = recipient.node.get_async_receive_offer().unwrap();
	recipient.node.peer_disconnected(invoice_server.node.get_our_node_id());
	recipient.onion_messenger.peer_disconnected(invoice_server.node.get_our_node_id());
	invoice_server.node.peer_disconnected(recipient.node.get_our_node_id());
	invoice_server.onion_messenger.peer_disconnected(recipient.node.get_our_node_id());

	let amt_msat = 5000;
	let payment_id = PaymentId([1; 32]);
	sender.node.pay_for_offer(&offer, Some(amt_msat), payment_id, Default::default()).unwrap();

	// Forward invreq to server, pass static invoice back, check that htlc was locked in/monitor was
	// added
	let (peer_id, invreq_om) = extract_invoice_request_om(sender, &[sender_lsp, invoice_server]);
	invoice_server.onion_messenger.handle_onion_message(peer_id, &invreq_om);

	let mut events = invoice_server.node.get_and_clear_pending_events();
	assert_eq!(events.len(), 1);
	let (reply_path, invreq) = match events.pop().unwrap() {
		Event::StaticInvoiceRequested {
			recipient_id: ev_id, reply_path, invoice_request, ..
		} => {
			assert_eq!(recipient_id, ev_id);
			(reply_path, invoice_request)
		},
		_ => panic!(),
	};

	invoice_server
		.node
		.respond_to_static_invoice_request(invoice, reply_path, invreq, invreq_path)
		.unwrap();
	let (peer_node_id, static_invoice_om, static_invoice) =
		extract_static_invoice_om(invoice_server, &[sender_lsp, sender]);
	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_node_id, sender, sender_lsp);

	// Ensure that after the held HTLC is locked in, the sender's lsp does not forward it immediately.
	sender_lsp.node.process_pending_htlc_forwards();
	assert!(sender_lsp.node.get_and_clear_pending_msg_events().is_empty());

	// Forward the held_htlc OM through to the invoice_server node, who should generate an
	// OnionMessageIntercepted event since the recipient is disconnected.
	let held_htlc_om_to_inv_server = sender
		.onion_messenger
		.next_onion_message_for_peer(invoice_server.node.get_our_node_id())
		.unwrap();
	invoice_server
		.onion_messenger
		.handle_onion_message(sender_lsp.node.get_our_node_id(), &held_htlc_om_to_inv_server);

	// Get the held_htlc OM from the interception event.
	let mut events_rc = core::cell::RefCell::new(Vec::new());
	invoice_server.onion_messenger.process_pending_events(&|e| Ok(events_rc.borrow_mut().push(e)));
	let events = events_rc.into_inner();
	let held_htlc_om = events
		.into_iter()
		.find_map(|ev| {
			if let Event::OnionMessageIntercepted { message, .. } = ev {
				// At least one of the intercepted onion messages will be an invoice request that the
				// invoice server is attempting to forward to the recipient, ignore that as we're testing
				// the static invoice flow
				let peeled_onion = recipient.onion_messenger.peel_onion_message(&message).unwrap();
				if matches!(
					peeled_onion,
					PeeledOnion::Offers(OffersMessage::InvoiceRequest { .. }, _, _)
				) {
					return None;
				}

				assert!(matches!(
					peeled_onion,
					PeeledOnion::AsyncPayments(AsyncPaymentsMessage::HeldHtlcAvailable(_), _, _)
				));
				Some(message)
			} else {
				None
			}
		})
		.unwrap();

	// Reconnect the recipient to the invoice_server so the held_htlc OM can be delivered.
	let mut reconnect_args = ReconnectArgs::new(invoice_server, recipient);
	reconnect_args.send_channel_ready = (true, true);
	reconnect_nodes(reconnect_args);

	// On reconnect, the invoice server should get an `OnionMessagePeerConnected` event and the
	// recipient should generate more offer_paths_requests.
	let events = core::cell::RefCell::new(Vec::new());
	invoice_server.onion_messenger.process_pending_events(&|e| Ok(events.borrow_mut().push(e)));
	assert_eq!(events.borrow().len(), 1);
	assert!(matches!(events.into_inner().pop().unwrap(), Event::OnionMessagePeerConnected { .. }));
	expect_offer_paths_requests(recipient, &[invoice_server]);

	// Now that the recipient is online, the payment can complete.
	recipient
		.onion_messenger
		.handle_onion_message(invoice_server.node.get_our_node_id(), &held_htlc_om);
	let (peer_id, release_htlc_om) =
		extract_release_htlc_oms(recipient, &[sender, sender_lsp, invoice_server]).pop().unwrap();
	sender_lsp.onion_messenger.handle_onion_message(peer_id, &release_htlc_om);

	sender_lsp.node.process_pending_htlc_forwards();
	let mut events = sender_lsp.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&invoice_server.node.get_our_node_id(), &mut events);
	check_added_monitors!(sender_lsp, 1);

	let path: &[&Node] = &[invoice_server, recipient];
	let args = PassAlongPathArgs::new(sender_lsp, path, amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();

	let route: &[&[&Node]] = &[&[sender_lsp, invoice_server, recipient]];
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let (res, _) =
		claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice)));
}

#[test]
fn held_htlc_timeout() {
	// Test that if a held HTLC doesn't get released for a long time, it will eventually time out and
	// be failed backwards by the sender's LSP.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut sender_lsp_cfg = test_default_channel_config();
	sender_lsp_cfg.enable_htlc_hold = true;
	let mut invoice_server_cfg = test_default_channel_config();
	invoice_server_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(sender_lsp_cfg), Some(invoice_server_cfg), Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];
	let invoice_server = &nodes[2];
	let recipient = &nodes[3];

	let amt_msat = 5000;
	let (_, peer_node_id, static_invoice_om) = build_async_offer_and_init_payment(amt_msat, &nodes);
	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_node_id, sender, sender_lsp);

	// Ensure that after the held HTLC is locked in, the sender's lsp does not forward it immediately.
	sender_lsp.node.process_pending_htlc_forwards();
	assert!(sender_lsp.node.get_and_clear_pending_msg_events().is_empty());

	let (peer_id, held_htlc_om) =
		extract_held_htlc_available_oms(sender, &[sender_lsp, invoice_server, recipient])
			.pop()
			.unwrap();
	recipient.onion_messenger.handle_onion_message(peer_id, &held_htlc_om);

	// Extract the release_htlc_om, but don't deliver it to the sender's LSP.
	let _ = extract_release_htlc_oms(recipient, &[sender, sender_lsp, invoice_server]);

	// Connect blocks to the sender's LSP until they timeout the HTLC.
	connect_blocks(
		sender_lsp,
		MIN_CLTV_EXPIRY_DELTA as u32
			+ TEST_FINAL_CLTV
			+ HTLC_FAIL_BACK_BUFFER
			+ LATENCY_GRACE_PERIOD_BLOCKS,
	);
	sender_lsp.node.process_pending_htlc_forwards();

	let expected_path = &[sender_lsp];
	let expected_route = &[&expected_path[..]];
	let mut evs = sender_lsp.node.get_and_clear_pending_events();
	assert_eq!(evs.len(), 1);
	match evs.pop().unwrap() {
		Event::HTLCHandlingFailed { failure_type, failure_reason, .. } => {
			assert!(matches!(failure_type, HTLCHandlingFailureType::InvalidForward { .. }));
			assert!(matches!(
				failure_reason,
				Some(HTLCHandlingFailureReason::Local {
					reason: LocalHTLCFailureReason::ForwardExpiryBuffer
				})
			));
		},
		_ => panic!(),
	}
	// Note that we won't retry the failed HTLC even though we originally allowed 1 retry attempt,
	// because held_htlc payments to static invoices aren't going to be retried ever until we support
	// trampoline.
	pass_failed_payment_back(
		sender,
		&expected_route[..],
		false,
		payment_hash,
		PaymentFailureReason::RetriesExhausted,
	);
}

#[test]
fn fail_held_htlc_on_reconnect() {
	// Test that if a held HTLC by the sender LSP fails but the async sender is offline, the HTLC
	// is failed on reconnect instead of FC the channel.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut sender_lsp_cfg = test_default_channel_config();
	sender_lsp_cfg.enable_htlc_hold = true;
	let mut invoice_server_cfg = test_default_channel_config();
	invoice_server_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(sender_lsp_cfg), Some(invoice_server_cfg), Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	let chan = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];
	let invoice_server = &nodes[2];
	let recipient = &nodes[3];

	let amt_msat = 5000;
	let (_, peer_node_id, static_invoice_om) = build_async_offer_and_init_payment(amt_msat, &nodes);
	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_node_id, sender, sender_lsp);

	sender_lsp.node.process_pending_htlc_forwards();
	let (peer_id, held_htlc_om) =
		extract_held_htlc_available_oms(sender, &[sender_lsp, invoice_server, recipient])
			.pop()
			.unwrap();
	recipient.onion_messenger.handle_onion_message(peer_id, &held_htlc_om);

	let _ = extract_release_htlc_oms(recipient, &[sender, sender_lsp, invoice_server]);

	// Disconnect async sender <-> sender LSP
	sender.node.peer_disconnected(sender_lsp.node.get_our_node_id());
	sender_lsp.node.peer_disconnected(sender.node.get_our_node_id());

	// Connect blocks such that they cause the HTLC to timeout
	let chan_id = chan.0.channel_id;
	let channel =
		sender.node.list_channels().iter().find(|c| c.channel_id == chan_id).unwrap().clone();
	let htlc_expiry = channel
		.pending_outbound_htlcs
		.iter()
		.find(|htlc| htlc.payment_hash == payment_hash)
		.unwrap()
		.cltv_expiry;
	let blocks_to_connect = htlc_expiry - sender.best_block_info().1 + 100;
	connect_blocks(sender, blocks_to_connect);
	connect_blocks(sender_lsp, blocks_to_connect);

	sender_lsp.node.process_pending_htlc_forwards();
	let mut evs = sender_lsp.node.get_and_clear_pending_events();
	assert_eq!(evs.len(), 1);
	match evs.pop().unwrap() {
		Event::HTLCHandlingFailed { failure_type, failure_reason, .. } => {
			assert!(matches!(failure_type, HTLCHandlingFailureType::InvalidForward { .. }));
			assert!(matches!(
				failure_reason,
				Some(HTLCHandlingFailureReason::Local {
					reason: LocalHTLCFailureReason::ForwardExpiryBuffer
				})
			));
		},
		_ => panic!(),
	}

	// After reconnecting, check that HTLC was failed and channel is open.
	let mut reconnect_args = ReconnectArgs::new(&sender, &sender_lsp);
	reconnect_args.pending_cell_htlc_fails.0 = 1;
	reconnect_nodes(reconnect_args);

	expect_payment_failed!(sender, payment_hash, false);
	assert_eq!(sender.node.list_channels().len(), 1);
	assert_eq!(sender_lsp.node.list_channels().len(), 2);
}

#[test]
fn intercepted_hold_htlc() {
	// Test a payment `sender --> LSP --> recipient` such that the HTLC is both a hold htlc and an
	// intercept htlc, i.e. the HTLC needs be held until the recipient comes online *and* the LSP
	// needs to open a JIT channel to the recipient for the payment to complete.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
	let (sender_cfg, mut recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	recipient_cfg.manually_accept_inbound_channels = true;
	recipient_cfg.channel_handshake_limits.force_announced_channel_preference = false;

	let mut lsp_cfg = test_default_channel_config();
	lsp_cfg.accept_intercept_htlcs = true;
	lsp_cfg.accept_forwards_to_priv_channels = true;
	lsp_cfg.enable_htlc_hold = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(lsp_cfg), None, Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);

	let sender = &nodes[0];
	let lsp = &nodes[1];
	let recipient = &nodes[3];

	// Only open a channel from sender <> LSP, not recipient <> LSP. The recipient <> LSP channel will
	// be a JIT channel created in response to an `HTLCIntercepted` event below.
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);

	// Create an unused announced channel for the LSP node so it is an announced node for the purposes
	// of blinded pathfinding, etc. nodes[2] will never be used otherwise.
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);

	// Typically, JIT channels are created by an LSP providing the recipient with special "intercept"
	// scids out-of-band, to be put in the recipient's BOLT 11 invoice route hints. The intercept
	// scids signal to the LSP to open a JIT channel.
	//
	// Below we hardcode blinded payment paths containing intercept scids, to be used in the
	// recipient's eventual static invoice, because we don't yet support intercept scids in the normal
	// static invoice server flow.

	// We need to be able to predict the offer_nonce in order to hardcode acceptable blinded payment
	// paths containing a JIT channel scid for the recipient below.
	// Without the offer_nonce used below in the `AsyncBolt12OfferContext` matching the eventual offer
	// that gets generated, the payment will be rejected.
	let hardcoded_random_bytes = [42; 32];
	*recipient.keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);

	// Pass a dummy `ChannelDetails` when creating a blinded payment path, with an scid that indicates
	// to the LSP that they should open a 0-conf JIT channel to the recipient.
	let mut first_hops = sender.node.list_channels();
	let intercept_scid = lsp.node.get_intercept_scid();
	first_hops[0].short_channel_id = Some(intercept_scid);
	first_hops[0].inbound_scid_alias = Some(intercept_scid);

	let created_at = recipient.node.duration_since_epoch();
	let payment_secret = inbound_payment::create_for_spontaneous_payment(
		&recipient.keys_manager.get_expanded_key(),
		None,
		STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY.as_secs() as u32,
		created_at.as_secs(),
		None,
	)
	.unwrap();
	let mut offer_nonce = Nonce([0; Nonce::LENGTH]);
	offer_nonce.0.copy_from_slice(&hardcoded_random_bytes[..Nonce::LENGTH]);
	let payment_context = PaymentContext::AsyncBolt12Offer(AsyncBolt12OfferContext { offer_nonce });
	let blinded_payment_path_with_jit_channel_scid = recipient
		.node
		.flow
		.test_create_blinded_payment_paths(
			&recipient.router,
			recipient.keys_manager,
			first_hops,
			None,
			payment_secret,
			payment_context,
			u32::MAX,
		)
		.unwrap();
	recipient.router.expect_blinded_payment_paths(blinded_payment_path_with_jit_channel_scid);

	let amt_msat = 5000;
	let (static_invoice, peer_node_id, static_invoice_om) =
		build_async_offer_and_init_payment(amt_msat, &nodes);
	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_node_id, sender, lsp);

	// Ensure that after the held HTLC is locked in, the sender's lsp does not forward it immediately.
	lsp.node.process_pending_htlc_forwards();
	assert!(lsp.node.get_and_clear_pending_msg_events().is_empty());

	// Ensure we don't generate an `HTLCIntercepted` for the HTLC until the recipient sends
	// release_held_htlc.
	assert!(lsp.node.get_and_clear_pending_events().is_empty());

	let (peer_id, held_htlc_om) =
		extract_held_htlc_available_oms(sender, &[lsp, recipient, &nodes[2]]).pop().unwrap();
	recipient.onion_messenger.handle_onion_message(peer_id, &held_htlc_om);
	let (peer_id, release_htlc_om) =
		extract_release_htlc_oms(recipient, &[sender, lsp]).pop().unwrap();
	lsp.onion_messenger.handle_onion_message(peer_id, &release_htlc_om);
	lsp.node.process_pending_htlc_forwards();

	// After the sender's LSP receives release_held_htlc from the recipient, the HTLC will be
	// transitioned from a held HTLC to an intercept HTLC and we will generate an `HTLCIntercepted`
	// event.
	assert!(lsp.node.get_and_clear_pending_msg_events().is_empty());
	let evs = lsp.node.get_and_clear_pending_events();
	assert_eq!(evs.len(), 1);
	let (intercept_id, outbound_amt) = match evs[0] {
		Event::HTLCIntercepted {
			intercept_id,
			requested_next_hop_scid,
			expected_outbound_amount_msat,
			..
		} => {
			assert_eq!(requested_next_hop_scid, intercept_scid);
			(intercept_id, expected_outbound_amount_msat)
		},
		_ => panic!(),
	};

	// Open the just-in-time channel so the payment can then be forwarded.
	let (_, chan_id) = open_zero_conf_channel(&lsp, &recipient, None);
	lsp.node
		.forward_intercepted_htlc(
			intercept_id,
			&chan_id,
			recipient.node.get_our_node_id(),
			outbound_amt,
		)
		.unwrap();
	lsp.node.process_pending_htlc_forwards();

	let mut events = lsp.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&recipient.node.get_our_node_id(), &mut events);
	check_added_monitors!(lsp, 1);

	let path: &[&Node] = &[recipient];
	let args = PassAlongPathArgs::new(lsp, path, amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();

	let route: &[&[&Node]] = &[&[lsp, recipient]];
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let (res, _) =
		claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice)));
}

#[test]
fn async_payment_mpp() {
	// An MPP payment from an often-offline sender to an often-offline recipient.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut lsp_cfg = test_default_channel_config();
	lsp_cfg.enable_htlc_hold = true;
	lsp_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(lsp_cfg.clone()), Some(lsp_cfg), Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);

	// Create this network topology:
	//         LSP1
	//        / |  \
	// sender   |   recipient
	//        \ |  /
	//      	LSP2
	// We open a public channel between LSP1 and LSP2 to ensure they are announced nodes.
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let lsp_a = &nodes[1];
	let lsp_b = &nodes[2];
	let recipient = &nodes[3];

	let amt_msat = 120_000_000;
	let (_, peer_id, static_invoice_om) = build_async_offer_and_init_payment(amt_msat, &nodes);

	// The sender should lock in the held HTLCs with their LSPs right after receiving the static invoice.
	sender.onion_messenger.handle_onion_message(peer_id, &static_invoice_om);
	check_added_monitors(sender, 2);
	let mut events = sender.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 2);

	// HTLC 1
	let ev = remove_first_msg_event_to_node(&lsp_a.node.get_our_node_id(), &mut events);
	let commitment_update = match ev {
		MessageSendEvent::UpdateHTLCs { ref updates, .. } => updates,
		_ => panic!(),
	};
	let update_add = commitment_update.update_add_htlcs[0].clone();
	let payment_hash = update_add.payment_hash;
	assert!(update_add.hold_htlc.is_some());
	lsp_a.node.handle_update_add_htlc(sender.node.get_our_node_id(), &update_add);
	commitment_signed_dance!(lsp_a, sender, &commitment_update.commitment_signed, false, true);
	lsp_a.node.process_pending_htlc_forwards();

	// HTLC 2
	let ev = remove_first_msg_event_to_node(&lsp_b.node.get_our_node_id(), &mut events);
	let commitment_update = match ev {
		MessageSendEvent::UpdateHTLCs { ref updates, .. } => updates,
		_ => panic!(),
	};
	let update_add = commitment_update.update_add_htlcs[0].clone();
	assert!(update_add.hold_htlc.is_some());
	lsp_b.node.handle_update_add_htlc(sender.node.get_our_node_id(), &update_add);
	commitment_signed_dance!(lsp_b, sender, &commitment_update.commitment_signed, false, true);
	lsp_b.node.process_pending_htlc_forwards();

	// held htlc <> release_htlc dance
	let held_htlc_oms = extract_held_htlc_available_oms(sender, &[lsp_a, lsp_b, recipient]);
	// Expect at least 1 held_htlc OM per HTLC
	assert!(held_htlc_oms.len() >= 2);
	for (peer_id, held_htlc_om) in held_htlc_oms {
		recipient.onion_messenger.handle_onion_message(peer_id, &held_htlc_om);
	}
	let release_htlc_oms = extract_release_htlc_oms(recipient, &[sender, lsp_a, lsp_b]);
	assert!(release_htlc_oms.len() >= 2);
	for (peer_id, release_htlc_om) in release_htlc_oms {
		// Just give the OM to both LSPs for testing simplicity, only the correct one will successfully
		// parse it
		lsp_a.onion_messenger.handle_onion_message(peer_id, &release_htlc_om);
		lsp_b.onion_messenger.handle_onion_message(peer_id, &release_htlc_om);
	}

	let expected_path: &[&Node] = &[recipient];
	lsp_a.node.process_pending_htlc_forwards();
	check_added_monitors!(lsp_a, 1);
	let mut events = lsp_a.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&recipient.node.get_our_node_id(), &mut events);
	let args = PassAlongPathArgs::new(lsp_a, expected_path, amt_msat, payment_hash, ev)
		.without_claimable_event();
	do_pass_along_path(args);

	lsp_b.node.process_pending_htlc_forwards();
	check_added_monitors!(lsp_b, 1);
	let mut events = lsp_b.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&recipient.node.get_our_node_id(), &mut events);
	let args = PassAlongPathArgs::new(lsp_b, expected_path, amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();

	let keysend_preimage = match claimable_ev {
		Event::PaymentClaimable {
			purpose: PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
			..
		} => payment_preimage.unwrap(),
		_ => panic!(),
	};

	let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
	claim_payment_along_route(ClaimAlongRouteArgs::new(sender, expected_route, keysend_preimage));
}

#[test]
fn fail_held_htlcs_when_cfg_unset() {
	// Test that if we receive a held HTLC but `UserConfig::enable_htlc_hold` is unset, we will fail
	// it backwards.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut sender_lsp_cfg = test_default_channel_config();
	sender_lsp_cfg.enable_htlc_hold = true;
	let mut inv_server_cfg = test_default_channel_config();
	inv_server_cfg.accept_forwards_to_priv_channels = true;

	let cfgs = &[
		Some(sender_cfg),
		Some(sender_lsp_cfg.clone()),
		Some(inv_server_cfg),
		Some(recipient_cfg),
	];
	let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, cfgs);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];

	let (_, peer_node_id, static_invoice_om) = build_async_offer_and_init_payment(5000, &nodes);

	// Just before the sender sends the HTLC to their LSP, their LSP disables support for the feature.
	sender_lsp_cfg.enable_htlc_hold = false;
	sender_lsp.node.set_current_config(sender_lsp_cfg);

	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_node_id, sender, sender_lsp);

	// The LSP will then fail the HTLC back to the sender.
	sender_lsp.node.process_pending_htlc_forwards();
	let expected_path = &[sender_lsp];
	let expected_route = &[&expected_path[..]];
	let mut evs = sender_lsp.node.get_and_clear_pending_events();
	assert_eq!(evs.len(), 1);
	match evs.pop().unwrap() {
		Event::HTLCHandlingFailed { failure_type, failure_reason, .. } => {
			assert!(matches!(failure_type, HTLCHandlingFailureType::Forward { .. }));
			assert!(matches!(
				failure_reason,
				Some(HTLCHandlingFailureReason::Local {
					reason: LocalHTLCFailureReason::TemporaryNodeFailure
				})
			));
		},
		_ => panic!(),
	}
	pass_failed_payment_back(
		sender,
		&expected_route[..],
		false,
		payment_hash,
		PaymentFailureReason::RetriesExhausted,
	);
}

#[test]
fn release_htlc_races_htlc_onion_decode() {
	// Test that an async sender's LSP will release held HTLCs even if they receive the
	// release_held_htlc message before they have a chance to process the held HTLC's onion. This was
	// previously broken.
	let chanmon_cfgs = create_chanmon_cfgs(4);
	let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);

	let (sender_cfg, recipient_cfg) = (often_offline_node_cfg(), often_offline_node_cfg());
	let mut sender_lsp_cfg = test_default_channel_config();
	sender_lsp_cfg.enable_htlc_hold = true;
	let mut invoice_server_cfg = test_default_channel_config();
	invoice_server_cfg.accept_forwards_to_priv_channels = true;

	let node_chanmgrs = create_node_chanmgrs(
		4,
		&node_cfgs,
		&[Some(sender_cfg), Some(sender_lsp_cfg), Some(invoice_server_cfg), Some(recipient_cfg)],
	);
	let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
	create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
	create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
	create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0);
	unify_blockheight_across_nodes(&nodes);
	let sender = &nodes[0];
	let sender_lsp = &nodes[1];
	let invoice_server = &nodes[2];
	let recipient = &nodes[3];

	let amt_msat = 5000;
	let (static_invoice, peer_id, static_invoice_om) =
		build_async_offer_and_init_payment(amt_msat, &nodes);
	let payment_hash =
		lock_in_htlc_for_static_invoice(&static_invoice_om, peer_id, sender, sender_lsp);

	// The LSP has not transitioned the HTLC to the intercepts map internally because
	// process_pending_htlc_forwards has not been called.
	let (peer_id, held_htlc_om) =
		extract_held_htlc_available_oms(sender, &[sender_lsp, invoice_server, recipient])
			.pop()
			.unwrap();
	recipient.onion_messenger.handle_onion_message(peer_id, &held_htlc_om);

	// Extract the release_htlc_om and ensure the sender's LSP will release the HTLC on the next call
	// to process_pending_htlc_forwards, even though the HTLC was not yet officially intercepted when
	// the release message arrived.
	let (peer_id, release_htlc_om) =
		extract_release_htlc_oms(recipient, &[sender, sender_lsp, invoice_server]).pop().unwrap();
	sender_lsp.onion_messenger.handle_onion_message(peer_id, &release_htlc_om);

	sender_lsp.node.process_pending_htlc_forwards();
	let mut events = sender_lsp.node.get_and_clear_pending_msg_events();
	assert_eq!(events.len(), 1);
	let ev = remove_first_msg_event_to_node(&invoice_server.node.get_our_node_id(), &mut events);
	check_added_monitors!(sender_lsp, 1);

	let path: &[&Node] = &[invoice_server, recipient];
	let args = PassAlongPathArgs::new(sender_lsp, path, amt_msat, payment_hash, ev);
	let claimable_ev = do_pass_along_path(args).unwrap();

	let route: &[&[&Node]] = &[&[sender_lsp, invoice_server, recipient]];
	let keysend_preimage = extract_payment_preimage(&claimable_ev);
	let (res, _) =
		claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
	assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice)));
}