lightning 0.3.0-beta1

A Complete Bitcoin Lightning Library in Rust. Handles the core functionality of the Lightning Network, allowing clients to implement custom wallet, chain interactions, storage and network logic without enforcing a specific runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
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
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
// 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.

//! Types pertaining to funding channels.

use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Amount, FeeRate, OutPoint, ScriptBuf, SignedAmount, TxOut, WScriptHash, Weight};

use crate::ln::chan_utils::{
	make_funding_redeemscript, BASE_INPUT_WEIGHT, EMPTY_SCRIPT_SIG_WEIGHT,
	FUNDING_TRANSACTION_WITNESS_WEIGHT,
};
use crate::ln::interactivetxs::{get_output_weight, TX_COMMON_FIELDS_WEIGHT};
use crate::ln::msgs;
use crate::ln::types::ChannelId;
use crate::ln::LN_MAX_MSG_LEN;
use crate::prelude::*;
use crate::util::native_async::MaybeSend;
use crate::util::wallet_utils::{
	CoinSelection, CoinSelectionSource, CoinSelectionSourceSync, ConfirmedUtxo, Input,
};

/// Error returned when a [`FundingContribution`] cannot be adjusted to a target feerate.
///
/// This is used when re-estimating an already-built contribution at a different feerate than the
/// one used during coin selection. That includes, for example, acceptor-side adjustment to the
/// initiator's chosen feerate during splice tie-break resolution, as well as initiator-side
/// adjustment to a minimum RBF feerate for later attempts.
///
/// Callers decide how to handle the failure. Depending on the context, they may drop the
/// contribution, wait and retry later, or abort the splice negotiation.
///
/// See [`ChannelManager::splice_channel`] for further details.
///
/// [`ChannelManager::splice_channel`]: crate::ln::channelmanager::ChannelManager::splice_channel
#[derive(Debug)]
pub(super) enum FeeRateAdjustmentError {
	/// The counterparty's proposed feerate is below `min_feerate`, which was used as the feerate
	/// during coin selection. We'll retry via RBF at our preferred feerate.
	FeeRateTooLow { target_feerate: FeeRate, min_feerate: FeeRate },
	/// The counterparty's proposed feerate is above `max_feerate` and the re-estimated fee for
	/// our contributed inputs and outputs exceeds the original fee estimate (computed at
	/// `min_feerate` assuming initiator responsibility). If the re-estimated fee were within the
	/// original estimate, a feerate above `max_feerate` would be tolerable since the acceptor
	/// doesn't pay for common fields or the shared input/output.
	FeeRateTooHigh {
		target_feerate: FeeRate,
		max_feerate: FeeRate,
		target_fee: Amount,
		original_fee: Amount,
	},
	/// Arithmetic overflow when computing the fee buffer.
	FeeBufferOverflow,
	/// The re-estimated fee exceeds the available fee buffer regardless of `max_feerate`. The fee
	/// buffer is the maximum fee that can be accommodated:
	/// - **input-backed contributions**: the original fee plus any change output value
	/// - **input-less contributions**: the channel balance minus the withdrawal outputs
	FeeBufferInsufficient { source: &'static str, available: Amount, required: Amount },
}

impl core::fmt::Display for FeeRateAdjustmentError {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			FeeRateAdjustmentError::FeeRateTooLow { target_feerate, min_feerate } => {
				write!(
					f,
					"Target feerate {} is below our minimum {}; \
					 proceeding without contribution, will RBF later",
					target_feerate, min_feerate,
				)
			},
			FeeRateAdjustmentError::FeeRateTooHigh {
				target_feerate,
				max_feerate,
				target_fee,
				original_fee,
			} => {
				write!(
					f,
					"Target feerate {} exceeds our maximum {} and target fee {} exceeds original fee estimate {}",
					target_feerate, max_feerate, target_fee, original_fee,
				)
			},
			FeeRateAdjustmentError::FeeBufferOverflow => {
				write!(
					f,
					"Arithmetic overflow when computing available fee buffer; \
					 proceeding without contribution",
				)
			},
			FeeRateAdjustmentError::FeeBufferInsufficient { source, available, required } => {
				write!(
					f,
					"Fee buffer {} ({}) is insufficient for required fee {}; \
					 proceeding without contribution",
					available, source, required,
				)
			},
		}
	}
}

/// Error returned when building a [`FundingContribution`] from a [`FundingTemplate`].
#[derive(Debug)]
pub enum FundingContributionError {
	/// The feerate exceeds the maximum allowed feerate.
	FeeRateExceedsMaximum {
		/// The requested feerate.
		feerate: FeeRate,
		/// The maximum allowed feerate.
		max_feerate: FeeRate,
	},
	/// The feerate is below the minimum RBF feerate.
	///
	/// Note: [`FundingTemplate::min_rbf_feerate`] may be derived from an in-progress
	/// negotiation that later aborts, leaving a stale (higher than necessary) minimum. If
	/// this error occurs after receiving [`Event::SpliceNegotiationFailed`], call
	/// [`ChannelManager::splice_channel`] again to get a fresh template.
	///
	/// [`Event::SpliceNegotiationFailed`]: crate::events::Event::SpliceNegotiationFailed
	/// [`ChannelManager::splice_channel`]: crate::ln::channelmanager::ChannelManager::splice_channel
	FeeRateBelowRbfMinimum {
		/// The requested feerate.
		feerate: FeeRate,
		/// The minimum RBF feerate.
		min_rbf_feerate: FeeRate,
	},
	/// The splice value is invalid (zero, empty outputs, duplicate inputs or outputs, exceeds the
	/// maximum money supply, or splices out more than the available channel balance).
	InvalidSpliceValue,
	/// An input's `prevtx` is too large to fit in a `tx_add_input` message.
	PrevTxTooLarge,
	/// Coin selection failed to find suitable inputs.
	CoinSelectionFailed,
	/// Coin selection is required but no coin selection source was provided.
	///
	/// This can also be returned when reusing a prior contribution would otherwise satisfy the
	/// request, but that prior contribution cannot be adjusted in-place to the requested feerate.
	/// For example, an input-backed prior contribution may no longer have enough fee buffer in its
	/// change output to absorb the higher fee. In that case, providing a coin selection source lets
	/// the builder fall back to fresh coin selection, which may replace the prior input set instead
	/// of preserving it.
	MissingCoinSelectionSource,
	/// The request cannot be satisfied using the manually selected inputs.
	ManuallySelectedInputsInsufficient,
	/// This template cannot build an RBF contribution.
	NotRbfScenario,
}

impl core::fmt::Display for FundingContributionError {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			FundingContributionError::FeeRateExceedsMaximum { feerate, max_feerate } => {
				write!(f, "Feerate {} exceeds maximum {}", feerate, max_feerate)
			},
			FundingContributionError::FeeRateBelowRbfMinimum { feerate, min_rbf_feerate } => {
				write!(f, "Feerate {} is below minimum RBF feerate {}", feerate, min_rbf_feerate)
			},
			FundingContributionError::InvalidSpliceValue => {
				write!(
					f,
					"Invalid splice value (zero, empty, duplicate, exceeds limit, or overdraws balance)"
				)
			},
			FundingContributionError::PrevTxTooLarge => {
				write!(f, "Input prevtx is too large to fit in a tx_add_input message")
			},
			FundingContributionError::CoinSelectionFailed => {
				write!(f, "Coin selection failed to find suitable inputs")
			},
			FundingContributionError::MissingCoinSelectionSource => {
				write!(f, "Coin selection source required to build this contribution")
			},
			FundingContributionError::ManuallySelectedInputsInsufficient => {
				write!(f, "The request cannot be satisfied using the manually selected inputs")
			},
			FundingContributionError::NotRbfScenario => {
				write!(f, "This template cannot build an RBF contribution")
			},
		}
	}
}

/// A template for contributing to a channel's splice funding transaction.
///
/// This is returned from [`ChannelManager::splice_channel`] when a channel is ready to be
/// spliced. A [`FundingContribution`] must be obtained from it and passed to
/// [`ChannelManager::funding_contributed`] in order to resume the splicing process.
///
/// # Building a Contribution
///
/// For a fresh splice (no pending splice to replace), either use the convenience methods
/// [`FundingTemplate::splice_in_sync`] and [`FundingTemplate::splice_out`] or start with
/// [`FundingTemplate::without_prior_contribution`] to compose a request manually.
///
/// The builder API supports adding value, adding withdrawal outputs, or both. Attach a wallet
/// when the request may need new wallet inputs; pure splice-out requests can be built without one
/// and pay fees from the channel balance.
///
/// # Replace By Fee (RBF)
///
/// When a pending splice exists that hasn't been locked yet, use
/// [`FundingTemplate::rbf_prior_contribution_sync`] (or
/// [`FundingTemplate::rbf_prior_contribution`] for async) to retry the stored prior contribution
/// at an RBF-compatible feerate. To amend that prior request before building, start from
/// [`FundingTemplate::with_prior_contribution`] instead.
///
/// Check [`FundingTemplate::min_rbf_feerate`] for the minimum feerate required (the greater of
/// the previous feerate + 25 sat/kwu and the spec's 25/24 rule). Use
/// [`FundingTemplate::prior_contribution`] to inspect the stored contribution before deciding
/// whether to reuse it or replace it with a fresh request via
/// [`FundingTemplate::without_prior_contribution`].
///
/// [`ChannelManager::splice_channel`]: crate::ln::channelmanager::ChannelManager::splice_channel
/// [`ChannelManager::funding_contributed`]: crate::ln::channelmanager::ChannelManager::funding_contributed
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingTemplate {
	/// The shared input, which, if present indicates the funding template is for a splice funding
	/// transaction.
	shared_input: Option<Input>,

	/// The minimum RBF feerate (the greater of previous feerate + 25 sat/kwu and the spec's
	/// 25/24 rule), if this template is for an RBF attempt. `None` for fresh splices with no
	/// pending splice candidates.
	min_rbf_feerate: Option<FeeRate>,

	/// The user's prior contribution from a previous splice negotiation on this channel.
	prior_contribution: Option<FundingContribution>,

	/// The portion of the user's balance that can be spliced out.
	///
	/// This value is captured at [`ChannelManager::splice_channel`] time and may become stale
	/// if balances change before the contribution is used. Staleness is acceptable here because
	/// this is only used as an optimization to determine if the prior contribution can be
	/// reused with adjusted fees — the contribution is re-validated at
	/// [`ChannelManager::funding_contributed`] time and again at quiescence time against the
	/// current balances.
	///
	/// [`ChannelManager::splice_channel`]: crate::ln::channelmanager::ChannelManager::splice_channel
	/// [`ChannelManager::funding_contributed`]: crate::ln::channelmanager::ChannelManager::funding_contributed
	spliceable_balance: Amount,
}

impl FundingTemplate {
	/// Constructs a [`FundingTemplate`] for a splice using the provided shared input.
	pub(super) fn new(
		shared_input: Option<Input>, min_rbf_feerate: Option<FeeRate>,
		prior_contribution: Option<FundingContribution>, spliceable_balance: Amount,
	) -> Self {
		Self { shared_input, min_rbf_feerate, prior_contribution, spliceable_balance }
	}

	/// Returns the minimum RBF feerate, if this template is for an RBF attempt.
	///
	/// When set, the `min_feerate` passed to the splice/builder methods must be at least this
	/// value.
	pub fn min_rbf_feerate(&self) -> Option<FeeRate> {
		self.min_rbf_feerate
	}

	/// Returns a reference to the prior contribution from a previous splice negotiation, if
	/// available.
	///
	/// Use this to inspect the prior contribution's current parameters (for example,
	/// [`FundingContribution::outputs`], [`FundingContribution::change_output`], and
	/// [`FundingContribution::net_value`]) before deciding
	/// whether to reuse it via [`FundingTemplate::rbf_prior_contribution`] or build a fresh
	/// contribution with different parameters using
	/// [`FundingTemplate::without_prior_contribution`].
	///
	/// Note: the returned contribution may reflect a different feerate than originally provided,
	/// as it may have been adjusted for RBF or for the counterparty's feerate when acting as
	/// the acceptor. This can change other parameters too; for example, the amount added to the
	/// channel may increase if the change output was removed to cover a higher fee.
	pub fn prior_contribution(&self) -> Option<&FundingContribution> {
		self.prior_contribution.as_ref()
	}

	/// Creates a [`FundingBuilder`] for constructing a contribution.
	///
	/// If a prior contribution is available, the builder starts from it automatically and builder
	/// mutations amend that prior request. Use [`FundingTemplate::without_prior_contribution`] to
	/// start empty instead.
	///
	/// `feerate` is the feerate used for fee estimation and, if wallet inputs are needed, coin
	/// selection. When [`FundingTemplate::min_rbf_feerate`] is set, it must be at least that value.
	/// `max_feerate` is the highest feerate we are willing to tolerate if we end up as the
	/// acceptor, and must be at least `feerate`.
	pub fn with_prior_contribution(self, feerate: FeeRate, max_feerate: FeeRate) -> FundingBuilder {
		FundingBuilder::new(self, feerate, max_feerate)
	}

	/// Creates a [`FundingBuilder`] for constructing a contribution without using any prior
	/// contribution.
	///
	/// `feerate` and `max_feerate` have the same meaning as in
	/// [`FundingTemplate::with_prior_contribution`]. This is useful when an RBF template carries a
	/// prior contribution but the caller wants to replace, rather than amend, that request.
	pub fn without_prior_contribution(
		mut self, feerate: FeeRate, max_feerate: FeeRate,
	) -> FundingBuilder {
		self.prior_contribution.take();
		FundingBuilder::new(self, feerate, max_feerate)
	}

	/// Creates a [`FundingContribution`] for adding funds to a channel.
	///
	/// This is a convenience wrapper around [`FundingTemplate::with_prior_contribution`]. As a
	/// result, if this template carries a prior contribution, `value_added` is added on top of the
	/// amount that prior request was already adding to the channel instead of replacing it. Use
	/// [`FundingTemplate::without_prior_contribution`] if you want to replace the prior request
	/// instead.
	///
	/// `value_added` is the amount of additional value to add to the channel. `min_feerate` is the
	/// feerate used for fee estimation and, if needed, coin selection; when
	/// [`FundingTemplate::min_rbf_feerate`] is set, it must be at least that value. `max_feerate` is
	/// the highest feerate we are willing to tolerate if we end up as the acceptor, and must be at
	/// least `min_feerate`. `wallet` is only consulted if the request cannot be satisfied by
	/// reusing/amending the prior contribution. When this template carries a prior contribution,
	/// increasing its value may therefore re-run coin selection and yield a different input set than
	/// the prior contribution used. This is not supported when the prior contribution used manually
	/// selected inputs; use [`FundingTemplate::splice_in_inputs`] or
	/// [`FundingTemplate::without_prior_contribution`] in that case.
	pub async fn splice_in<W: CoinSelectionSource + MaybeSend>(
		self, value_added: Amount, min_feerate: FeeRate, max_feerate: FeeRate, wallet: W,
	) -> Result<FundingContribution, FundingContributionError> {
		self.with_prior_contribution(min_feerate, max_feerate)
			.with_coin_selection_source(wallet)
			.add_value(value_added)?
			.build()
			.await
	}

	/// Creates a [`FundingContribution`] for adding funds to a channel.
	///
	/// This is the synchronous variant of [`FundingTemplate::splice_in`]; `value_added`,
	/// `min_feerate`, `max_feerate`, and `wallet` have the same meaning, including the restriction
	/// on prior contributions with manually selected inputs.
	pub fn splice_in_sync<W: CoinSelectionSourceSync>(
		self, value_added: Amount, min_feerate: FeeRate, max_feerate: FeeRate, wallet: W,
	) -> Result<FundingContribution, FundingContributionError> {
		self.with_prior_contribution(min_feerate, max_feerate)
			.with_coin_selection_source_sync(wallet)
			.add_value(value_added)?
			.build()
	}

	/// Creates a [`FundingContribution`] for adding funds to a channel using manually selected
	/// inputs.
	///
	/// This is a convenience wrapper around [`FundingTemplate::with_prior_contribution`] with no
	/// wallet attached. Each input is fully consumed with no change output, so the amount added to
	/// the channel is derived from the total input value minus the estimated fee.
	///
	/// When a prior contribution with manually selected inputs is present, `inputs` are appended to
	/// the prior [`FundingContribution::inputs`] instead of replacing them. Use
	/// [`FundingTemplate::without_prior_contribution`] if you want to replace the prior request
	/// instead. If the template carries a coin-selected prior contribution, manual inputs are
	/// incompatible and this method returns [`FundingContributionError::InvalidSpliceValue`].
	///
	/// `inputs` are the additional manually selected inputs to fully consume. `min_feerate` is the
	/// feerate used for fee estimation and must be at least [`FundingTemplate::min_rbf_feerate`]
	/// when that is set. `max_feerate` is the highest feerate we are willing to tolerate if we end
	/// up as the acceptor, and must be at least `min_feerate`.
	pub fn splice_in_inputs(
		self, inputs: Vec<ConfirmedUtxo>, min_feerate: FeeRate, max_feerate: FeeRate,
	) -> Result<FundingContribution, FundingContributionError> {
		self.with_prior_contribution(min_feerate, max_feerate).add_inputs(inputs)?.build()
	}

	/// Creates a [`FundingContribution`] for removing funds from a channel.
	///
	/// This is a convenience wrapper around [`FundingTemplate::with_prior_contribution`] with no
	/// wallet attached. For a fresh splice, fees are paid from the channel balance, so this does
	/// not perform coin selection or spend wallet inputs. When a prior contribution is present,
	/// `outputs` are appended to the prior [`FundingContribution::outputs`] instead of replacing
	/// them. Use [`FundingTemplate::without_prior_contribution`] if you want to replace the prior
	/// outputs instead.
	///
	/// `outputs` are the additional withdrawal outputs to include. `min_feerate` is the feerate
	/// used for fee estimation and must be at least [`FundingTemplate::min_rbf_feerate`] when that
	/// is set. `max_feerate` is the highest feerate we are willing to tolerate if we end up as the
	/// acceptor, and must be at least `min_feerate`.
	///
	/// If amending a prior contribution would require selecting new wallet inputs, this method
	/// returns [`FundingContributionError::MissingCoinSelectionSource`]. This can happen, for
	/// example, when the prior contribution was input-backed and its existing change output cannot
	/// absorb the additional withdrawal outputs or the higher fee implied by `min_feerate`. In
	/// that case, use the builder APIs with a coin selection source instead.
	pub fn splice_out(
		self, outputs: Vec<TxOut>, min_feerate: FeeRate, max_feerate: FeeRate,
	) -> Result<FundingContribution, FundingContributionError> {
		self.with_prior_contribution(min_feerate, max_feerate).add_outputs(outputs).build()
	}

	/// Creates a [`FundingContribution`] for an RBF (Replace-By-Fee) attempt on a pending splice.
	///
	/// This requires [`FundingTemplate::prior_contribution`] to be available. `feerate` overrides
	/// the template's minimum RBF feerate; passing `None` uses
	/// [`FundingTemplate::min_rbf_feerate`]. `max_feerate` is the highest feerate we are willing to
	/// tolerate if we end up as the acceptor, and must be at least the effective feerate. `wallet`
	/// is only consulted if the prior contribution cannot be reused or adjusted directly. The
	/// chosen `max_feerate` is stored on the returned contribution so that any later acceptor-side
	/// fee adjustment for that contribution remains capped at the caller's chosen maximum, even if
	/// this RBF attempt had to fall back to a fresh coin selection.
	///
	/// This handles the prior contribution logic internally:
	/// - If the prior contribution's feerate can be adjusted to the effective target feerate, the
	///   adjusted contribution is returned directly. For splice-in, the change output absorbs
	///   the fee difference. For splice-out (no wallet inputs), the holder's channel balance
	///   covers the higher fees.
	/// - If adjustment fails, coin selection is re-run using the prior contribution's
	///   parameters and the caller's `max_feerate`. For prior contributions without inputs,
	///   this changes the funding source: wallet inputs are selected to cover the outputs and
	///   fees instead of deducting them from the channel balance.
	/// - If no prior contribution exists, coin selection is run for a fee-bump-only contribution
	///   (`value_added = 0`), covering fees for the common fields and shared input/output via
	///   a newly selected input. Check [`FundingTemplate::prior_contribution`] to see if this
	///   is intended.
	///
	/// # Errors
	///
	/// Returns a [`FundingContributionError`] if there is no reusable prior contribution, if no
	/// effective RBF feerate is available, if the effective feerate violates the template's fee
	/// constraints, or if coin selection fails.
	pub async fn rbf_prior_contribution<W: CoinSelectionSource + MaybeSend>(
		self, feerate: Option<FeeRate>, max_feerate: FeeRate, wallet: W,
	) -> Result<FundingContribution, FundingContributionError> {
		if self.prior_contribution().is_none() {
			return Err(FundingContributionError::NotRbfScenario);
		}
		let feerate = feerate
			.or_else(|| self.min_rbf_feerate())
			.ok_or(FundingContributionError::NotRbfScenario)?;
		self.with_prior_contribution(feerate, max_feerate)
			.with_coin_selection_source(wallet)
			.build()
			.await
	}

	/// Creates a [`FundingContribution`] for an RBF (Replace-By-Fee) attempt on a pending splice.
	///
	/// This is the synchronous variant of [`FundingTemplate::rbf_prior_contribution`]; `feerate`,
	/// `max_feerate`, and `wallet` have the same meaning.
	pub fn rbf_prior_contribution_sync<W: CoinSelectionSourceSync>(
		self, feerate: Option<FeeRate>, max_feerate: FeeRate, wallet: W,
	) -> Result<FundingContribution, FundingContributionError> {
		if self.prior_contribution().is_none() {
			return Err(FundingContributionError::NotRbfScenario);
		}
		let feerate = feerate
			.or_else(|| self.min_rbf_feerate())
			.ok_or(FundingContributionError::NotRbfScenario)?;

		self.with_prior_contribution(feerate, max_feerate)
			.with_coin_selection_source_sync(wallet)
			.build()
	}
}

fn estimate_transaction_fee(
	inputs: &[ConfirmedUtxo], outputs: &[TxOut], change_output: Option<&TxOut>, is_initiator: bool,
	is_splice: bool, feerate: FeeRate,
) -> Amount {
	let input_weight: u64 = inputs
		.iter()
		.map(|input| BASE_INPUT_WEIGHT.saturating_add(input.utxo.satisfaction_weight))
		.fold(0, |total_weight, input_weight| total_weight.saturating_add(input_weight));

	let output_weight: u64 = outputs
		.iter()
		.chain(change_output.into_iter())
		.map(|txout| txout.weight().to_wu())
		.fold(0, |total_weight, output_weight| total_weight.saturating_add(output_weight));

	let mut weight = input_weight.saturating_add(output_weight);

	// The initiator pays for all common fields and the shared output in the funding transaction.
	if is_initiator {
		weight = weight
			.saturating_add(TX_COMMON_FIELDS_WEIGHT)
			// The weight of the funding output, a P2WSH output
			// NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
			// to calculate the contributed weight, so we use an all-zero hash.
			//
			// TODO(taproot): Needs to consider different weights based on channel type
			.saturating_add(
				get_output_weight(&ScriptBuf::new_p2wsh(&WScriptHash::from_raw_hash(
					Hash::all_zeros(),
				)))
				.to_wu(),
			);

		// The splice initiator pays for the input spending the previous funding output.
		if is_splice {
			weight = weight
				.saturating_add(BASE_INPUT_WEIGHT)
				.saturating_add(EMPTY_SCRIPT_SIG_WEIGHT)
				.saturating_add(FUNDING_TRANSACTION_WITNESS_WEIGHT);
			#[cfg(feature = "grind_signatures")]
			{
				// Guarantees a low R signature
				weight -= 1;
			}
		}
	}

	Weight::from_wu(weight) * feerate
}

fn validate_inputs(inputs: &[ConfirmedUtxo]) -> Result<(), FundingContributionError> {
	let mut total_value = Amount::ZERO;
	for (idx, input) in inputs.iter().enumerate() {
		if inputs[..idx]
			.iter()
			.any(|existing_input| existing_input.utxo.outpoint == input.utxo.outpoint)
		{
			return Err(FundingContributionError::InvalidSpliceValue);
		}

		use crate::util::ser::Writeable;
		const MESSAGE_TEMPLATE: msgs::TxAddInput = msgs::TxAddInput {
			channel_id: ChannelId([0; 32]),
			serial_id: 0,
			prevtx: None,
			prevtx_out: 0,
			sequence: 0,
			// Mutually exclusive with prevtx, which is accounted for below.
			shared_input_txid: None,
		};
		let message_len = MESSAGE_TEMPLATE.serialized_length() + input.prevtx.serialized_length();
		(message_len <= LN_MAX_MSG_LEN)
			.then(|| ())
			.ok_or(FundingContributionError::PrevTxTooLarge)?;

		total_value = match total_value.checked_add(input.utxo.output.value) {
			Some(sum) if sum <= Amount::MAX_MONEY => sum,
			_ => return Err(FundingContributionError::InvalidSpliceValue),
		};
	}

	Ok(())
}

/// Describes how a contribution request should source its wallet-backed inputs.
#[derive(Debug, Clone, PartialEq, Eq)]
enum FundingInputs {
	/// Reuses the contribution's existing inputs while targeting at least `value_added` added to
	/// the channel after fees. If dropping the change output leaves surplus value, it remains in
	/// the channel contribution.
	CoinSelected { value_added: Amount },
	/// Replaces the contribution's inputs with the provided set and fully consumes them without a
	/// change output. The amount added to the channel is recomputed from the input total minus fees,
	/// while explicit withdrawal outputs still reduce the splice's net value.
	ManuallySelected { inputs: Vec<ConfirmedUtxo> },
}

impl FundingInputs {
	fn mode(&self) -> FundingInputMode {
		match self {
			FundingInputs::CoinSelected { .. } => FundingInputMode::CoinSelected,
			FundingInputs::ManuallySelected { .. } => FundingInputMode::ManuallySelected,
		}
	}

	fn is_empty(&self) -> bool {
		match self {
			FundingInputs::CoinSelected { value_added } => *value_added == Amount::ZERO,
			FundingInputs::ManuallySelected { inputs } => inputs.is_empty(),
		}
	}

	fn value_added(&self) -> Amount {
		match self {
			FundingInputs::CoinSelected { value_added } => *value_added,
			FundingInputs::ManuallySelected { .. } => Amount::ZERO,
		}
	}

	fn manually_selected_inputs(&self) -> &[ConfirmedUtxo] {
		match self {
			FundingInputs::ManuallySelected { inputs } => inputs,
			FundingInputs::CoinSelected { .. } => &[],
		}
	}
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
enum FundingInputMode {
	CoinSelected,
	ManuallySelected,
}

impl_writeable_tlv_based_enum!(FundingInputMode,
	(1, CoinSelected) => {},
	(3, ManuallySelected) => {}
);

/// The components of a funding transaction contributed by one party.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct FundingContribution {
	/// The estimate fees responsible to be paid for the contribution.
	estimated_fee: Amount,

	/// The inputs included in the funding transaction.
	///
	/// For coin-selected contributions, excess value is returned via [`Self::change_output`]. For
	/// manually selected inputs, the full input value is consumed and no change output is created.
	inputs: Vec<ConfirmedUtxo>,

	/// The outputs to include in the funding transaction.
	///
	/// When no wallet inputs are contributed, these outputs are paid from the channel balance.
	/// Otherwise, they are paid by the contributed inputs.
	outputs: Vec<TxOut>,

	/// The output where any change will be sent.
	change_output: Option<TxOut>,

	/// The fee rate used to select `inputs` (the minimum feerate).
	feerate: FeeRate,

	/// The maximum fee rate to accept as acceptor before rejecting the splice.
	max_feerate: FeeRate,

	/// Whether the contribution is for funding a splice.
	is_splice: bool,

	/// Whether this contribution currently uses coin-selected or manual-input semantics.
	///
	/// This is `None` when the contribution has no inputs and is set accordingly based on the first
	/// `add_value` or `add_input` call on the builder.
	input_mode: Option<FundingInputMode>,
}

impl_writeable_tlv_based!(FundingContribution, {
	(1, estimated_fee, required),
	(3, inputs, optional_vec),
	(5, outputs, optional_vec),
	(7, change_output, option),
	(9, feerate, required),
	(11, max_feerate, required),
	(13, is_splice, required),
	(15, input_mode, option),
});

impl FundingContribution {
	pub(super) fn is_splice(&self) -> bool {
		self.is_splice
	}

	pub(crate) fn contributed_inputs(&self) -> impl Iterator<Item = OutPoint> + '_ {
		self.inputs.iter().map(|input| input.utxo.outpoint)
	}

	pub(crate) fn contributed_outputs(&self) -> impl Iterator<Item = &bitcoin::Script> + '_ {
		self.outputs
			.iter()
			.chain(self.change_output.iter())
			.map(|output| output.script_pubkey.as_script())
	}

	/// The positive value added to the channel after explicit outputs and fees.
	///
	/// This saturates at zero for net-negative contributions. See [`Self::net_value`] for the full
	/// signed contribution to the channel.
	pub fn value_added(&self) -> Amount {
		let total_input_value = self.inputs.iter().map(|i| i.utxo.output.value).sum::<Amount>();
		let total_output_value = self.outputs.iter().map(|output| output.value).sum();
		total_input_value
			.checked_sub(total_output_value)
			.and_then(|v| v.checked_sub(self.estimated_fee))
			.and_then(|v| {
				v.checked_sub(
					self.change_output.as_ref().map_or(Amount::ZERO, |output| output.value),
				)
			})
			.unwrap_or(Amount::ZERO)
	}

	/// Returns the estimated on-chain fee this contribution is responsible for paying.
	pub fn estimated_fee(&self) -> Amount {
		self.estimated_fee
	}

	/// Returns the inputs included in this contribution.
	pub fn inputs(&self) -> &[ConfirmedUtxo] {
		&self.inputs
	}

	/// Returns the outputs (e.g., withdrawal destinations) included in this contribution.
	///
	/// This does not include the change output; see [`FundingContribution::change_output`].
	pub fn outputs(&self) -> &[TxOut] {
		&self.outputs
	}

	/// Returns the change output included in this contribution, if any.
	///
	/// When coin selection provides more value than needed for the funding contribution and fees,
	/// the surplus is returned to the wallet via this change output.
	pub fn change_output(&self) -> Option<&TxOut> {
		self.change_output.as_ref()
	}

	/// Returns the fee rate used to select `inputs` (the minimum feerate).
	pub fn feerate(&self) -> FeeRate {
		self.feerate
	}

	/// Returns the maximum fee rate this contribution will accept as acceptor before rejecting
	/// the splice.
	pub fn max_feerate(&self) -> FeeRate {
		self.max_feerate
	}

	/// Tries to satisfy a new request using only this contribution's existing inputs.
	///
	/// For input-backed contributions, this reuses the current inputs, adjusts the explicit
	/// outputs, and shrinks or drops the change output as needed before applying
	/// `target_feerate`. If dropping change leaves surplus value, that surplus remains in the
	/// channel contribution.
	///
	/// For input-less contributions, `spliceable_balance` must be provided to cover the outputs and
	/// fees from the channel balance.
	///
	/// Returns `None` if the request would require new wallet inputs or cannot accommodate the
	/// requested feerate.
	fn amend_without_coin_selection(
		self, funding_inputs: Option<FundingInputs>, outputs: &[TxOut], target_feerate: FeeRate,
		max_feerate: FeeRate, spliceable_balance: Amount,
	) -> Option<Self> {
		// NOTE: The contribution returned is not guaranteed to be valid. We defer doing so until
		// `compute_feerate_adjustment`.
		let adjust_for_inputs_and_outputs = |contribution: Self,
		                                     inputs: Option<FundingInputs>,
		                                     outputs: &[TxOut]|
		 -> Option<Self> {
			let input_mode = inputs.as_ref().map(FundingInputs::mode);
			let (target_value_added, inputs) = match inputs {
				None => (None, Vec::new()),
				Some(FundingInputs::CoinSelected { value_added }) => {
					// We track the prior contribution's inputs here to see if they can cover the
					// new `value_added` without running coin selection.
					(Some(value_added), contribution.inputs)
				},
				Some(FundingInputs::ManuallySelected { inputs }) => (None, inputs),
			};

			if inputs.is_empty() && target_value_added.unwrap_or(Amount::ZERO) != Amount::ZERO {
				// Prior contribution didn't have any inputs, but now we need some.
				return None;
			}

			// When inputs are coin-selected, adjust the existing change output, if any, to account
			// for the requested value added and any explicit outputs that must also be funded by
			// the inputs.
			if let Some(value_added) = target_value_added {
				let estimated_fee = estimate_transaction_fee(
					&inputs,
					&outputs,
					contribution.change_output.as_ref(),
					true,
					contribution.is_splice,
					contribution.feerate,
				);
				let total_output_value: Amount = outputs.iter().map(|output| output.value).sum();
				let required_value =
					value_added.checked_add(total_output_value)?.checked_add(estimated_fee)?;

				if let Some(change_output) = contribution.change_output.as_ref() {
					let dust_limit = change_output.script_pubkey.minimal_non_dust();
					let total_input_value: Amount =
						inputs.iter().map(|input| input.utxo.output.value).sum();
					match total_input_value.checked_sub(required_value) {
						Some(new_change_value) if new_change_value >= dust_limit => {
							let new_change_output = TxOut {
								value: new_change_value,
								script_pubkey: change_output.script_pubkey.clone(),
							};
							return Some(FundingContribution {
								estimated_fee,
								inputs,
								outputs: outputs.to_vec(),
								change_output: Some(new_change_output),
								input_mode,
								..contribution
							});
						},
						_ => {},
					}
				}
			}

			let estimated_fee_no_change = estimate_transaction_fee(
				&inputs,
				&outputs,
				None,
				true,
				contribution.is_splice,
				contribution.feerate,
			);
			Some(FundingContribution {
				estimated_fee: estimated_fee_no_change,
				outputs: outputs.to_vec(),
				inputs,
				change_output: None,
				input_mode,
				..contribution
			})
		};

		let new_contribution_at_current_feerate =
			adjust_for_inputs_and_outputs(self, funding_inputs, outputs)?;
		let mut new_contribution_at_target_feerate = new_contribution_at_current_feerate
			.at_feerate(target_feerate, spliceable_balance, true)
			.ok()?;
		new_contribution_at_target_feerate.max_feerate = max_feerate;

		Some(new_contribution_at_target_feerate)
	}

	pub(super) fn into_tx_parts(self) -> (Vec<ConfirmedUtxo>, Vec<TxOut>) {
		let FundingContribution { inputs, mut outputs, change_output, .. } = self;

		if let Some(change_output) = change_output {
			outputs.push(change_output);
		}

		(inputs, outputs)
	}

	pub(super) fn into_contributed_inputs_and_outputs(self) -> (Vec<OutPoint>, Vec<ScriptBuf>) {
		let FundingContribution { inputs, outputs, change_output, .. } = self;
		let contributed_inputs = inputs.into_iter().map(|input| input.utxo.outpoint).collect();
		let contributed_outputs = outputs.into_iter().chain(change_output.into_iter());
		(contributed_inputs, contributed_outputs.map(|output| output.script_pubkey).collect())
	}

	/// Returns this contribution's inputs and outputs after removing any that overlap
	/// with the provided `existing_inputs`/`existing_outputs`.
	///
	/// Multiple contribution outputs sharing a `script_pubkey` are all dropped when any
	/// existing output uses the same script.
	///
	/// Returns `None` if every input and output was filtered as overlapping.
	pub(crate) fn into_unique_contributions<'a>(
		self, existing_inputs: impl Iterator<Item = OutPoint>,
		existing_outputs: impl Iterator<Item = &'a bitcoin::Script>,
	) -> Option<(Vec<OutPoint>, Vec<ScriptBuf>)> {
		let FundingContribution { mut inputs, mut outputs, mut change_output, .. } = self;
		for existing in existing_inputs {
			inputs.retain(|input| input.outpoint() != existing);
		}
		for existing in existing_outputs {
			outputs.retain(|output| output.script_pubkey.as_script() != existing);
			// TODO: Replace with `take_if` once our MSRV is >= 1.80.
			if change_output
				.as_ref()
				.filter(|output| output.script_pubkey.as_script() == existing)
				.is_some()
			{
				change_output.take();
			}
		}
		if inputs.is_empty() && outputs.is_empty() && change_output.as_ref().is_none() {
			None
		} else {
			let inputs = inputs.into_iter().map(|input| input.outpoint()).collect();
			let outputs = outputs
				.into_iter()
				.chain(change_output.into_iter())
				.map(|output| output.script_pubkey)
				.collect();
			Some((inputs, outputs))
		}
	}

	/// Computes the adjusted fee and change output value at the given target feerate, which may
	/// differ from the feerate used during coin selection.
	///
	/// The `is_initiator` parameter determines fee responsibility: the initiator pays for common
	/// transaction fields, the shared input, and the shared output, while the acceptor only pays
	/// for their own contributed inputs and outputs.
	///
	/// On success, returns the new estimated fee and, if applicable, the new change output value:
	/// - `Some(change)` — the adjusted change output value
	/// - `None` — no change output (no inputs or change fell below dust)
	///
	/// Returns `Err` if the contribution cannot accommodate the target feerate.
	fn compute_feerate_adjustment(
		&self, target_feerate: FeeRate, spliceable_balance: Amount, is_initiator: bool,
	) -> Result<(Amount, Option<Amount>), FeeRateAdjustmentError> {
		if target_feerate < self.feerate {
			return Err(FeeRateAdjustmentError::FeeRateTooLow {
				target_feerate,
				min_feerate: self.feerate,
			});
		}

		// If the target fee rate exceeds our max fee rate, we may still add our contribution
		// if we pay less in fees at the target feerate than at the original feerate. This can
		// happen when adjusting as acceptor, since the acceptor doesn't pay for common fields
		// and the shared input / output.
		if target_feerate > self.max_feerate {
			let target_fee = estimate_transaction_fee(
				&self.inputs,
				&self.outputs,
				self.change_output.as_ref(),
				is_initiator,
				self.is_splice,
				target_feerate,
			);
			if target_fee > self.estimated_fee {
				return Err(FeeRateAdjustmentError::FeeRateTooHigh {
					target_feerate,
					max_feerate: self.max_feerate,
					target_fee,
					original_fee: self.estimated_fee,
				});
			}
		}

		let target_fee = estimate_transaction_fee(
			&self.inputs,
			&self.outputs,
			self.change_output.as_ref(),
			is_initiator,
			self.is_splice,
			target_feerate,
		);

		if !self.inputs.is_empty() && self.input_mode == Some(FundingInputMode::CoinSelected) {
			// Any withdrawal outputs and fees always come from the coin-selected inputs, as we want
			// to guarantee the net contribution adds the desired value.
			let fee_buffer = self
				.estimated_fee
				.checked_add(
					self.change_output.as_ref().map_or(Amount::ZERO, |output| output.value),
				)
				.ok_or(FeeRateAdjustmentError::FeeBufferOverflow)?;

			if let Some(change_output) = self.change_output.as_ref() {
				let dust_limit = change_output.script_pubkey.minimal_non_dust();
				if let Some(new_change_value) = fee_buffer.checked_sub(target_fee) {
					if new_change_value >= dust_limit {
						return Ok((target_fee, Some(new_change_value)));
					}

					// Our remaining change was not enough to be a valid output, fallthrough to the
					// no remaining change case.
				}

				let target_fee_no_change = estimate_transaction_fee(
					&self.inputs,
					&self.outputs,
					None,
					is_initiator,
					self.is_splice,
					target_feerate,
				);
				if target_fee_no_change > fee_buffer {
					Err(FeeRateAdjustmentError::FeeBufferInsufficient {
						source: "estimated fee + change value",
						available: fee_buffer,
						required: target_fee_no_change,
					})
				} else {
					Ok((target_fee_no_change, None))
				}
			} else if let Some(_surplus) = fee_buffer.checked_sub(target_fee) {
				Ok((target_fee, None))
			} else {
				Err(FeeRateAdjustmentError::FeeBufferInsufficient {
					source: "estimated fee",
					available: fee_buffer,
					required: target_fee,
				})
			}
		} else {
			// Manually selected inputs may either add value to the channel or offset some of the
			// withdrawal outputs. Any remaining fee cost must come from the channel balance.
			let net_value_without_fee = self.net_value_without_fee();
			let fee_buffer = if net_value_without_fee.is_negative() {
				spliceable_balance
					.checked_sub(net_value_without_fee.unsigned_abs())
					.unwrap_or(Amount::ZERO)
			} else {
				spliceable_balance
					.checked_add(net_value_without_fee.unsigned_abs())
					.ok_or(FeeRateAdjustmentError::FeeBufferOverflow)?
			};
			if fee_buffer < target_fee {
				return Err(FeeRateAdjustmentError::FeeBufferInsufficient {
					source: "channel balance",
					available: fee_buffer,
					required: target_fee,
				});
			}
			Ok((target_fee, None))
		}
	}

	/// Adjusts the contribution for a different feerate, updating the change output, fee
	/// estimate, and feerate. Returns the adjusted contribution, or an error if the feerate
	/// can't be accommodated.
	fn at_feerate(
		mut self, feerate: FeeRate, spliceable_balance: Amount, is_initiator: bool,
	) -> Result<Self, FeeRateAdjustmentError> {
		let (new_estimated_fee, new_change) =
			self.compute_feerate_adjustment(feerate, spliceable_balance, is_initiator)?;
		match new_change {
			Some(value) => self.change_output.as_mut().unwrap().value = value,
			None => self.change_output = None,
		}
		self.estimated_fee = new_estimated_fee;
		self.feerate = feerate;
		Ok(self)
	}

	/// Adjusts the contribution's change output for the initiator's feerate.
	///
	/// When the acceptor has a pending contribution (from the quiescence tie-breaker scenario),
	/// the initiator's proposed feerate may differ from the feerate used during coin selection.
	/// This adjusts the change output so the acceptor pays their target fee at the target
	/// feerate.
	pub(super) fn for_acceptor_at_feerate(
		self, feerate: FeeRate, spliceable_balance: Amount,
	) -> Result<Self, FeeRateAdjustmentError> {
		self.at_feerate(feerate, spliceable_balance, false)
	}

	/// Adjusts the contribution's change output for the minimum RBF feerate.
	///
	/// When a pending splice exists with negotiated candidates and the contribution's feerate is
	/// below the minimum RBF feerate, this adjusts the change output so the initiator pays fees
	/// at the minimum RBF feerate.
	pub(super) fn for_initiator_at_feerate(
		self, feerate: FeeRate, spliceable_balance: Amount,
	) -> Result<Self, FeeRateAdjustmentError> {
		self.at_feerate(feerate, spliceable_balance, true)
	}

	/// Returns the net value at the given target feerate without mutating `self`.
	///
	/// This serves double duty: it checks feerate compatibility (returning `Err` if the feerate
	/// can't be accommodated) and computes the adjusted net value (returning `Ok` with the value
	/// accounting for the target feerate).
	fn net_value_at_feerate(
		&self, target_feerate: FeeRate, spliceable_balance: Amount, is_initiator: bool,
	) -> Result<SignedAmount, FeeRateAdjustmentError> {
		let (new_estimated_fee, new_change) =
			self.compute_feerate_adjustment(target_feerate, spliceable_balance, is_initiator)?;

		let prev_fee = self
			.estimated_fee
			.to_signed()
			.expect("total input amount cannot exceed Amount::MAX_MONEY");
		let prev_change = self
			.change_output
			.as_ref()
			.map_or(Amount::ZERO, |output| output.value)
			.to_signed()
			.expect("total input amount cannot exceed Amount::MAX_MONEY");

		let new_fee = new_estimated_fee
			.to_signed()
			.expect("total input amount cannot exceed Amount::MAX_MONEY");
		let new_change = new_change
			.unwrap_or(Amount::ZERO)
			.to_signed()
			.expect("total input amount cannot exceed Amount::MAX_MONEY");

		let prev_net_value = self.net_value();
		Ok(prev_net_value + prev_fee + prev_change - new_fee - new_change)
	}

	/// Returns the net value at the given target feerate without mutating `self`,
	/// assuming acceptor fee responsibility.
	pub(super) fn net_value_for_acceptor_at_feerate(
		&self, target_feerate: FeeRate, spliceable_balance: Amount,
	) -> Result<SignedAmount, FeeRateAdjustmentError> {
		self.net_value_at_feerate(target_feerate, spliceable_balance, false)
	}

	/// Returns the net value at the given target feerate without mutating `self`,
	/// assuming initiator fee responsibility.
	pub(super) fn net_value_for_initiator_at_feerate(
		&self, target_feerate: FeeRate, spliceable_balance: Amount,
	) -> Result<SignedAmount, FeeRateAdjustmentError> {
		self.net_value_at_feerate(target_feerate, spliceable_balance, true)
	}

	/// The net value contributed to a channel by the splice.
	pub fn net_value(&self) -> SignedAmount {
		let estimated_fee = self
			.estimated_fee
			.to_signed()
			.expect("total_input_value is validated to not exceed Amount::MAX_MONEY");
		self.net_value_without_fee()
			.checked_sub(estimated_fee)
			.expect("all amounts are validated to not exceed Amount::MAX_MONEY")
	}

	fn net_value_without_fee(&self) -> SignedAmount {
		let total_input_value = self
			.inputs
			.iter()
			.map(|input| input.utxo.output.value)
			.sum::<Amount>()
			.to_signed()
			.expect("total_input_value is validated to not exceed Amount::MAX_MONEY");
		let total_output_value = self
			.outputs
			.iter()
			.chain(self.change_output.iter())
			.map(|txout| txout.value)
			.sum::<Amount>()
			.to_signed()
			.expect("total_output_value is validated to not exceed Amount::MAX_MONEY");
		total_input_value
			.checked_sub(total_output_value)
			.expect("all amounts are validated to not exceed Amount::MAX_MONEY")
	}
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct NoCoinSelectionSource;
#[derive(Debug, Clone, PartialEq, Eq)]
struct AsyncCoinSelectionSource<W>(W);
#[derive(Debug, Clone, PartialEq, Eq)]
struct SyncCoinSelectionSource<W>(W);

#[derive(Debug, Clone, PartialEq, Eq)]
struct FundingBuilderInner<State> {
	shared_input: Option<Input>,
	min_rbf_feerate: Option<FeeRate>,
	prior_contribution: Option<FundingContribution>,
	spliceable_balance: Amount,
	funding_inputs: Option<FundingInputs>,
	outputs: Vec<TxOut>,
	feerate: FeeRate,
	max_feerate: FeeRate,
	state: State,
}

/// A builder for composing or amending a [`FundingContribution`].
///
/// The builder tracks either a requested amount to add to the channel or a fixed set of manually
/// selected inputs, together with any explicit withdrawal outputs. Building without an attached
/// wallet only succeeds when the request can be satisfied by reusing or amending a prior
/// contribution, by using only manually selected inputs, or by constructing a splice-out that
/// pays fees from the channel balance.
///
/// Attach a wallet via [`FundingBuilder::with_coin_selection_source`] or
/// [`FundingBuilder::with_coin_selection_source_sync`] when the request may need new wallet
/// inputs. Manually selected inputs are not supplemented with coin selection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingBuilder(FundingBuilderInner<NoCoinSelectionSource>);

/// A [`FundingBuilder`] with an attached asynchronous [`CoinSelectionSource`].
///
/// Created by [`FundingBuilder::with_coin_selection_source`]. The attached wallet is only used
/// if the request cannot be satisfied by reusing a prior contribution, by using only manually
/// selected inputs, or by building a pure splice-out directly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AsyncFundingBuilder<W>(FundingBuilderInner<AsyncCoinSelectionSource<W>>);

/// A [`FundingBuilder`] with an attached synchronous [`CoinSelectionSourceSync`].
///
/// Created by [`FundingBuilder::with_coin_selection_source_sync`]. The attached wallet is only
/// used if the request cannot be satisfied by reusing a prior contribution, by using only
/// manually selected inputs, or by building a pure splice-out directly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncFundingBuilder<W>(FundingBuilderInner<SyncCoinSelectionSource<W>>);

impl<State> FundingBuilderInner<State> {
	fn request_matches_prior(&self, prior_contribution: &FundingContribution) -> bool {
		let request_matches_prior_inputs =
			match (self.funding_inputs.as_ref(), prior_contribution.input_mode) {
				(
					Some(FundingInputs::ManuallySelected { inputs }),
					Some(FundingInputMode::ManuallySelected),
				) => {
					let request_inputs = inputs.iter().map(|input| input.utxo.outpoint);
					let prior_inputs =
						prior_contribution.inputs.iter().map(|input| input.utxo.outpoint);
					request_inputs.eq(prior_inputs)
				},
				(
					Some(FundingInputs::CoinSelected { value_added }),
					Some(FundingInputMode::CoinSelected),
				) => *value_added == prior_contribution.value_added(),
				(None, None) => true,
				_ => false,
			};
		request_matches_prior_inputs && self.outputs == prior_contribution.outputs
	}

	fn build_from_prior_contribution(
		&self, contribution: FundingContribution,
	) -> Result<FundingContribution, FundingContributionError> {
		let input_mode = self.funding_inputs.as_ref().map(FundingInputs::mode);

		if self.request_matches_prior(&contribution) {
			// Same request, but the feerate may have changed. Adjust the prior contribution
			// to the new feerate if possible.
			return contribution
				.for_initiator_at_feerate(self.feerate, self.spliceable_balance)
				.map(|mut adjusted| {
					adjusted.max_feerate = self.max_feerate;
					adjusted
				})
				.map_err(|_| {
					if input_mode == Some(FundingInputMode::ManuallySelected) {
						FundingContributionError::ManuallySelectedInputsInsufficient
					} else {
						FundingContributionError::MissingCoinSelectionSource
					}
				});
		}

		return contribution
			.amend_without_coin_selection(
				self.funding_inputs.clone(),
				&self.outputs,
				self.feerate,
				self.max_feerate,
				self.spliceable_balance,
			)
			.ok_or_else(|| {
				if input_mode == Some(FundingInputMode::ManuallySelected) {
					FundingContributionError::ManuallySelectedInputsInsufficient
				} else {
					FundingContributionError::MissingCoinSelectionSource
				}
			});
	}

	/// Tries to build the current request without selecting any new wallet inputs.
	///
	/// This first attempts to reuse or amend any prior contribution. If there is no prior
	/// contribution, it also supports manually selected inputs and pure splice-out requests by
	/// building a contribution without coin selection.
	///
	/// Returns [`FundingContributionError::MissingCoinSelectionSource`] if the request is
	/// otherwise valid but needs wallet inputs, or
	/// [`FundingContributionError::ManuallySelectedInputsInsufficient`] if the manually selected
	/// inputs cannot satisfy the request.
	fn try_build_without_coin_selection(
		&self,
	) -> Result<FundingContribution, FundingContributionError> {
		if let Some(contribution) = self.prior_contribution.as_ref() {
			return self.build_from_prior_contribution(contribution.clone());
		}

		let value_added =
			self.funding_inputs.as_ref().map_or(Amount::ZERO, FundingInputs::value_added);
		if value_added == Amount::ZERO {
			let inputs = self
				.funding_inputs
				.as_ref()
				.map_or(&[][..], FundingInputs::manually_selected_inputs);
			let input_mode =
				if inputs.is_empty() { None } else { Some(FundingInputMode::ManuallySelected) };

			let estimated_fee = estimate_transaction_fee(
				inputs,
				&self.outputs,
				None,
				true,
				self.shared_input.is_some(),
				self.feerate,
			);

			let contribution = FundingContribution {
				estimated_fee,
				inputs: match self.funding_inputs {
					Some(FundingInputs::ManuallySelected { ref inputs }) => inputs.clone(),
					None | Some(FundingInputs::CoinSelected { .. }) => Vec::new(),
				},
				outputs: self.outputs.clone(),
				change_output: None,
				feerate: self.feerate,
				max_feerate: self.max_feerate,
				is_splice: self.shared_input.is_some(),
				input_mode,
			};
			let net_value = contribution.net_value();
			if net_value.is_negative() {
				self.spliceable_balance.checked_sub(net_value.unsigned_abs()).ok_or_else(|| {
					if contribution.inputs.is_empty() {
						FundingContributionError::InvalidSpliceValue
					} else {
						FundingContributionError::ManuallySelectedInputsInsufficient
					}
				})?;
			}

			return Ok(contribution);
		}

		Err(FundingContributionError::MissingCoinSelectionSource)
	}

	fn prepare_coin_selection_request(
		&self,
	) -> Result<(Vec<Input>, Vec<TxOut>), FundingContributionError> {
		let value_added =
			self.funding_inputs.as_ref().map_or(Amount::ZERO, FundingInputs::value_added);
		let dummy_pubkey = PublicKey::from_slice(&[2; 33]).unwrap();
		let shared_output = bitcoin::TxOut {
			value: self
				.shared_input
				.as_ref()
				.map(|shared_input| shared_input.previous_utxo.value)
				.unwrap_or(Amount::ZERO)
				.checked_add(value_added)
				.ok_or(FundingContributionError::InvalidSpliceValue)?,
			script_pubkey: make_funding_redeemscript(&dummy_pubkey, &dummy_pubkey).to_p2wsh(),
		};

		let must_spend = self.shared_input.clone().map(|input| vec![input]).unwrap_or_default();
		let must_pay_to = if self.outputs.is_empty() {
			vec![shared_output]
		} else {
			self.outputs.iter().cloned().chain(core::iter::once(shared_output)).collect()
		};

		Ok((must_spend, must_pay_to))
	}

	fn validate_contribution_parameters(&self) -> Result<(), FundingContributionError> {
		if self.feerate > self.max_feerate {
			return Err(FundingContributionError::FeeRateExceedsMaximum {
				feerate: self.feerate,
				max_feerate: self.max_feerate,
			});
		}

		if let Some(min_rbf_feerate) = self.min_rbf_feerate.as_ref() {
			if self.feerate < *min_rbf_feerate {
				return Err(FundingContributionError::FeeRateBelowRbfMinimum {
					feerate: self.feerate,
					min_rbf_feerate: *min_rbf_feerate,
				});
			}
		}

		if self.funding_inputs.as_ref().map_or(true, FundingInputs::is_empty)
			&& self.outputs.is_empty()
		{
			return Err(FundingContributionError::InvalidSpliceValue);
		}

		// Validate user-provided amounts are within MAX_MONEY before coin selection to
		// ensure FundingContribution::net_value() arithmetic cannot overflow. With all
		// amounts bounded by MAX_MONEY (~2.1e15 sat), the worst-case net_value()
		// computation is -2 * MAX_MONEY (~-4.2e15), well within i64::MIN (~-9.2e18).
		if self.funding_inputs.as_ref().map_or(Amount::ZERO, FundingInputs::value_added)
			> Amount::MAX_MONEY
		{
			return Err(FundingContributionError::InvalidSpliceValue);
		}

		validate_inputs(
			self.funding_inputs.as_ref().map_or(&[][..], FundingInputs::manually_selected_inputs),
		)?;

		let mut value_removed = Amount::ZERO;
		for (idx, output) in self.outputs.iter().enumerate() {
			if self.outputs[..idx]
				.iter()
				.any(|existing_output| existing_output.script_pubkey == output.script_pubkey)
			{
				return Err(FundingContributionError::InvalidSpliceValue);
			}

			value_removed = match value_removed.checked_add(output.value) {
				Some(sum) if sum <= Amount::MAX_MONEY => sum,
				_ => return Err(FundingContributionError::InvalidSpliceValue),
			};
		}

		Ok(())
	}
}

impl FundingBuilder {
	fn new(template: FundingTemplate, feerate: FeeRate, max_feerate: FeeRate) -> FundingBuilder {
		let FundingTemplate {
			shared_input,
			min_rbf_feerate,
			prior_contribution,
			spliceable_balance,
		} = template;
		let (funding_inputs, outputs) = match prior_contribution.as_ref() {
			Some(prior_contribution) => {
				let funding_inputs = match prior_contribution.input_mode {
					Some(FundingInputMode::ManuallySelected) => {
						Some(FundingInputs::ManuallySelected {
							inputs: prior_contribution.inputs.clone(),
						})
					},
					Some(FundingInputMode::CoinSelected) => Some(FundingInputs::CoinSelected {
						value_added: prior_contribution.value_added(),
					}),
					None => None,
				};
				(funding_inputs, prior_contribution.outputs.clone())
			},
			None => (None, Vec::new()),
		};

		FundingBuilder(FundingBuilderInner {
			shared_input,
			min_rbf_feerate,
			prior_contribution,
			spliceable_balance,
			funding_inputs,
			outputs,
			feerate,
			max_feerate,
			state: NoCoinSelectionSource,
		})
	}

	/// Attaches an asynchronous [`CoinSelectionSource`] for later use.
	///
	/// The wallet is only consulted if [`AsyncFundingBuilder::build`] cannot satisfy the request by
	/// reusing a prior contribution, by using only manually selected inputs, or by constructing a
	/// pure splice-out directly.
	pub fn with_coin_selection_source<W: CoinSelectionSource + MaybeSend>(
		self, wallet: W,
	) -> AsyncFundingBuilder<W> {
		AsyncFundingBuilder(self.0.with_state(AsyncCoinSelectionSource(wallet)))
	}

	/// Attaches a synchronous [`CoinSelectionSourceSync`] for later use.
	///
	/// The wallet is only consulted if [`SyncFundingBuilder::build`] cannot satisfy the request by
	/// reusing a prior contribution, by using only manually selected inputs, or by constructing a
	/// pure splice-out directly.
	pub fn with_coin_selection_source_sync<W: CoinSelectionSourceSync>(
		self, wallet: W,
	) -> SyncFundingBuilder<W> {
		SyncFundingBuilder(self.0.with_state(SyncCoinSelectionSource(wallet)))
	}

	/// Adds a manually selected input to the request.
	///
	/// Each input is fully consumed with no change output. When built without additional coin
	/// selection, the inputs and explicit outputs are modeled by their net effect on the channel:
	/// the contribution may be net-positive or net-negative before fees.
	///
	/// Manually selected inputs are a separate request mode and cannot be combined with requesting
	/// additional coin-selected value. If the manually selected inputs cannot satisfy the request,
	/// [`FundingBuilder::build`] returns
	/// [`FundingContributionError::ManuallySelectedInputsInsufficient`] instead of falling back to
	/// coin selection.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has a
	/// coin-selected value request.
	pub fn add_input(self, input: ConfirmedUtxo) -> Result<Self, FundingContributionError> {
		self.0.add_input_inner(input).map(FundingBuilder)
	}

	/// Adds manually selected inputs to the request.
	///
	/// Each input is fully consumed with no change output. When built without additional coin
	/// selection, the inputs and explicit outputs are modeled by their net effect on the channel:
	/// the contribution may be net-positive or net-negative before fees.
	///
	/// Manually selected inputs are a separate request mode and cannot be combined with requesting
	/// additional coin-selected value. If the manually selected inputs cannot satisfy the request,
	/// [`FundingBuilder::build`] returns
	/// [`FundingContributionError::ManuallySelectedInputsInsufficient`] instead of falling back to
	/// coin selection.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has a
	/// coin-selected value request.
	pub fn add_inputs(self, inputs: Vec<ConfirmedUtxo>) -> Result<Self, FundingContributionError> {
		self.0.add_inputs_inner(inputs).map(FundingBuilder)
	}

	/// Removes all manually selected inputs whose outpoint matches `outpoint`.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has a
	/// coin-selected value request.
	pub fn remove_input(self, outpoint: &OutPoint) -> Result<Self, FundingContributionError> {
		self.0.remove_input_inner(outpoint).map(FundingBuilder)
	}

	/// Adds a withdrawal output to the request.
	///
	/// `output` is appended to the current set of explicit outputs. If the builder was seeded from
	/// a prior contribution, this adds an additional withdrawal on top of the prior outputs. This
	/// does not affect any change output derived when the contribution is built.
	pub fn add_output(self, output: TxOut) -> Self {
		FundingBuilder(self.0.add_output_inner(output))
	}

	/// Adds withdrawal outputs to the request.
	///
	/// `outputs` are appended to the current set of explicit outputs. If the builder was seeded
	/// from a prior contribution, this adds additional withdrawals on top of the prior outputs.
	/// This does not affect any change output derived when the contribution is built.
	pub fn add_outputs(self, outputs: Vec<TxOut>) -> Self {
		FundingBuilder(self.0.add_outputs_inner(outputs))
	}

	/// Removes all explicit withdrawal outputs whose script pubkey matches `script_pubkey`.
	///
	/// This only affects outputs returned by [`FundingContribution::outputs`]; it never removes the
	/// change output returned by [`FundingContribution::change_output`].
	pub fn remove_outputs(self, script_pubkey: &ScriptBuf) -> Self {
		FundingBuilder(self.0.remove_outputs_inner(script_pubkey))
	}

	/// Builds a [`FundingContribution`] without coin selection.
	///
	/// This succeeds when the request can be satisfied by reusing or amending a prior
	/// contribution, by using only manually selected inputs, or by building a splice-out
	/// contribution that pays fees from the channel balance.
	///
	/// Returns [`FundingContributionError::MissingCoinSelectionSource`] if additional wallet
	/// inputs are needed, or [`FundingContributionError::ManuallySelectedInputsInsufficient`] if
	/// the manually selected inputs cannot satisfy the request.
	pub fn build(self) -> Result<FundingContribution, FundingContributionError> {
		self.0.build_without_coin_selection()
	}
}

impl<State> FundingBuilderInner<State> {
	fn with_state<NewState>(self, state: NewState) -> FundingBuilderInner<NewState> {
		FundingBuilderInner {
			shared_input: self.shared_input,
			min_rbf_feerate: self.min_rbf_feerate,
			prior_contribution: self.prior_contribution,
			spliceable_balance: self.spliceable_balance,
			funding_inputs: self.funding_inputs,
			outputs: self.outputs,
			feerate: self.feerate,
			max_feerate: self.max_feerate,
			state,
		}
	}

	fn add_value_inner(mut self, value: Amount) -> Result<Self, FundingContributionError> {
		match &mut self.funding_inputs {
			None => self.funding_inputs = Some(FundingInputs::CoinSelected { value_added: value }),
			Some(FundingInputs::CoinSelected { value_added }) => {
				*value_added =
					Amount::from_sat(value_added.to_sat().saturating_add(value.to_sat()));
			},
			Some(FundingInputs::ManuallySelected { .. }) => {
				return Err(FundingContributionError::InvalidSpliceValue);
			},
		}
		Ok(self)
	}

	fn remove_value_inner(mut self, value: Amount) -> Result<Self, FundingContributionError> {
		match &mut self.funding_inputs {
			None => {},
			Some(FundingInputs::CoinSelected { value_added }) => {
				*value_added =
					Amount::from_sat(value_added.to_sat().saturating_sub(value.to_sat()));
			},
			Some(FundingInputs::ManuallySelected { .. }) => {
				return Err(FundingContributionError::InvalidSpliceValue);
			},
		}
		Ok(self)
	}

	fn add_input_inner(mut self, input: ConfirmedUtxo) -> Result<Self, FundingContributionError> {
		match &mut self.funding_inputs {
			None => {
				self.funding_inputs = Some(FundingInputs::ManuallySelected { inputs: vec![input] })
			},
			Some(FundingInputs::ManuallySelected { inputs }) => inputs.push(input),
			Some(FundingInputs::CoinSelected { .. }) => {
				return Err(FundingContributionError::InvalidSpliceValue);
			},
		}
		Ok(self)
	}

	fn add_inputs_inner(
		mut self, inputs: Vec<ConfirmedUtxo>,
	) -> Result<Self, FundingContributionError> {
		match &mut self.funding_inputs {
			None => self.funding_inputs = Some(FundingInputs::ManuallySelected { inputs }),
			Some(FundingInputs::ManuallySelected { inputs: existing_inputs }) => {
				existing_inputs.extend(inputs)
			},
			Some(FundingInputs::CoinSelected { .. }) => {
				return Err(FundingContributionError::InvalidSpliceValue);
			},
		}
		Ok(self)
	}

	fn remove_input_inner(mut self, outpoint: &OutPoint) -> Result<Self, FundingContributionError> {
		match &mut self.funding_inputs {
			None => {},
			Some(FundingInputs::ManuallySelected { inputs }) => {
				inputs.retain(|input| input.utxo.outpoint != *outpoint);
			},
			Some(FundingInputs::CoinSelected { .. }) => {
				return Err(FundingContributionError::InvalidSpliceValue);
			},
		}
		Ok(self)
	}

	fn add_output_inner(mut self, output: TxOut) -> Self {
		self.outputs.push(output);
		self
	}

	fn add_outputs_inner(mut self, outputs: Vec<TxOut>) -> Self {
		self.outputs.extend(outputs);
		self
	}

	fn remove_outputs_inner(mut self, script_pubkey: &ScriptBuf) -> Self {
		self.outputs.retain(|output| output.script_pubkey != *script_pubkey);
		self
	}

	/// Validates the current request and then tries to build it without selecting new wallet
	/// inputs.
	///
	/// Returns [`FundingContributionError::MissingCoinSelectionSource`] if the request is valid but
	/// cannot be satisfied without wallet inputs, or
	/// [`FundingContributionError::ManuallySelectedInputsInsufficient`] if the manually selected
	/// inputs cannot satisfy the request.
	fn build_without_coin_selection(
		&self,
	) -> Result<FundingContribution, FundingContributionError> {
		self.validate_contribution_parameters()?;
		self.try_build_without_coin_selection()
	}
}

impl<W> AsyncFundingBuilder<W> {
	/// Adds a withdrawal output to the request.
	///
	/// `output` is appended to the current set of explicit outputs. If the builder was seeded from
	/// a prior contribution, this adds an additional withdrawal on top of the prior outputs. This
	/// does not affect any change output derived when the contribution is built.
	pub fn add_output(self, output: TxOut) -> Self {
		AsyncFundingBuilder(self.0.add_output_inner(output))
	}

	/// Adds withdrawal outputs to the request.
	///
	/// `outputs` are appended to the current set of explicit outputs. If the builder was seeded
	/// from a prior contribution, this adds additional withdrawals on top of the prior outputs.
	/// This does not affect any change output derived when the contribution is built.
	pub fn add_outputs(self, outputs: Vec<TxOut>) -> Self {
		AsyncFundingBuilder(self.0.add_outputs_inner(outputs))
	}

	/// Removes all explicit withdrawal outputs whose script pubkey matches `script_pubkey`.
	///
	/// This only affects outputs returned by [`FundingContribution::outputs`]; it never removes the
	/// change output returned by [`FundingContribution::change_output`].
	pub fn remove_outputs(self, script_pubkey: &ScriptBuf) -> Self {
		AsyncFundingBuilder(self.0.remove_outputs_inner(script_pubkey))
	}

	/// Increases the requested amount to add to the channel.
	///
	/// `value` is added on top of the builder's current request. If the builder was seeded from a
	/// prior contribution, this increases that prior contribution's current amount added to the
	/// channel. If the updated request cannot be satisfied in-place, [`AsyncFundingBuilder::build`]
	/// may re-run coin selection and return a contribution with a different input set.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has manually
	/// selected inputs.
	pub fn add_value(self, value: Amount) -> Result<Self, FundingContributionError> {
		self.0.add_value_inner(value).map(AsyncFundingBuilder)
	}

	/// Decreases the requested amount to add to the channel.
	///
	/// `value` is subtracted from the builder's current request, saturating at zero. If the builder
	/// was seeded from a prior contribution, this decreases that prior contribution's current
	/// amount added to the channel. If the updated request cannot be satisfied in-place,
	/// [`AsyncFundingBuilder::build`] may re-run coin selection and return a contribution with a
	/// different input set.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has manually
	/// selected inputs.
	pub fn remove_value(self, value: Amount) -> Result<Self, FundingContributionError> {
		self.0.remove_value_inner(value).map(AsyncFundingBuilder)
	}
}

impl<W: CoinSelectionSource + MaybeSend> AsyncFundingBuilder<W> {
	/// Builds a [`FundingContribution`], using the attached asynchronous wallet only when needed.
	///
	/// If the request can be satisfied by reusing or amending a prior contribution, or by building
	/// a pure splice-out directly, or by using only manually selected inputs, the attached wallet is
	/// ignored.
	pub async fn build(self) -> Result<FundingContribution, FundingContributionError> {
		let inner = self.0;
		match inner.build_without_coin_selection() {
			Err(FundingContributionError::MissingCoinSelectionSource) => {},
			other => return other,
		}

		let (must_spend, must_pay_to) = inner.prepare_coin_selection_request()?;
		let AsyncCoinSelectionSource(wallet) = inner.state;
		let coin_selection = wallet
			.select_confirmed_utxos(
				None,
				must_spend,
				&must_pay_to,
				inner.feerate.to_sat_per_kwu() as u32,
				u64::MAX,
			)
			.await
			.map_err(|_| FundingContributionError::CoinSelectionFailed)?;

		let CoinSelection { confirmed_utxos: inputs, change_output } = coin_selection;
		validate_inputs(&inputs)?;

		let outputs = inner.outputs;
		let is_splice = inner.shared_input.is_some();
		let estimated_fee = estimate_transaction_fee(
			&inputs,
			&outputs,
			change_output.as_ref(),
			true,
			is_splice,
			inner.feerate,
		);

		return Ok(FundingContribution {
			estimated_fee,
			inputs,
			outputs,
			change_output,
			feerate: inner.feerate,
			max_feerate: inner.max_feerate,
			is_splice,
			input_mode: Some(FundingInputMode::CoinSelected),
		});
	}
}

impl<W> SyncFundingBuilder<W> {
	/// Adds a withdrawal output to the request.
	///
	/// `output` is appended to the current set of explicit outputs. If the builder was seeded from
	/// a prior contribution, this adds an additional withdrawal on top of the prior outputs. This
	/// does not affect any change output derived when the contribution is built.
	pub fn add_output(self, output: TxOut) -> Self {
		SyncFundingBuilder(self.0.add_output_inner(output))
	}

	/// Adds withdrawal outputs to the request.
	///
	/// `outputs` are appended to the current set of explicit outputs. If the builder was seeded
	/// from a prior contribution, this adds additional withdrawals on top of the prior outputs.
	/// This does not affect any change output derived when the contribution is built.
	pub fn add_outputs(self, outputs: Vec<TxOut>) -> Self {
		SyncFundingBuilder(self.0.add_outputs_inner(outputs))
	}

	/// Removes all explicit withdrawal outputs whose script pubkey matches `script_pubkey`.
	///
	/// This only affects outputs returned by [`FundingContribution::outputs`]; it never removes the
	/// change output returned by [`FundingContribution::change_output`].
	pub fn remove_outputs(self, script_pubkey: &ScriptBuf) -> Self {
		SyncFundingBuilder(self.0.remove_outputs_inner(script_pubkey))
	}

	/// Increases the requested amount to add to the channel.
	///
	/// `value` is added on top of the builder's current request. If the builder was seeded from a
	/// prior contribution, this increases that prior contribution's current amount added to the
	/// channel. If the updated request cannot be satisfied in-place, [`SyncFundingBuilder::build`]
	/// may re-run coin selection and return a contribution with a different input set.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has manually
	/// selected inputs.
	pub fn add_value(self, value: Amount) -> Result<Self, FundingContributionError> {
		self.0.add_value_inner(value).map(SyncFundingBuilder)
	}

	/// Decreases the requested amount to add to the channel.
	///
	/// `value` is subtracted from the builder's current request, saturating at zero. If the builder
	/// was seeded from a prior contribution, this decreases that prior contribution's current
	/// amount added to the channel. If the updated request cannot be satisfied in-place,
	/// [`SyncFundingBuilder::build`] may re-run coin selection and return a contribution with a
	/// different input set.
	///
	/// Returns [`FundingContributionError::InvalidSpliceValue`] if the builder already has manually
	/// selected inputs.
	pub fn remove_value(self, value: Amount) -> Result<Self, FundingContributionError> {
		self.0.remove_value_inner(value).map(SyncFundingBuilder)
	}
}

impl<W: CoinSelectionSourceSync> SyncFundingBuilder<W> {
	/// Builds a [`FundingContribution`], using the attached synchronous wallet only when needed.
	///
	/// If the request can be satisfied by reusing or amending a prior contribution, or by building
	/// a pure splice-out directly, or by using only manually selected inputs, the attached wallet is
	/// ignored.
	pub fn build(self) -> Result<FundingContribution, FundingContributionError> {
		let inner = self.0;
		match inner.build_without_coin_selection() {
			Err(FundingContributionError::MissingCoinSelectionSource) => {},
			other => return other,
		}

		let (must_spend, must_pay_to) = inner.prepare_coin_selection_request()?;
		let SyncCoinSelectionSource(wallet) = inner.state;
		let coin_selection = wallet
			.select_confirmed_utxos(
				None,
				must_spend,
				&must_pay_to,
				inner.feerate.to_sat_per_kwu() as u32,
				u64::MAX,
			)
			.map_err(|_| FundingContributionError::CoinSelectionFailed)?;

		let CoinSelection { confirmed_utxos: inputs, change_output } = coin_selection;
		validate_inputs(&inputs)?;

		let outputs = inner.outputs;
		let is_splice = inner.shared_input.is_some();
		let estimated_fee = estimate_transaction_fee(
			&inputs,
			&outputs,
			change_output.as_ref(),
			true,
			is_splice,
			inner.feerate,
		);

		return Ok(FundingContribution {
			estimated_fee,
			inputs,
			outputs,
			change_output,
			feerate: inner.feerate,
			max_feerate: inner.max_feerate,
			is_splice,
			input_mode: Some(FundingInputMode::CoinSelected),
		});
	}
}

#[cfg(test)]
mod tests {
	use super::{
		estimate_transaction_fee, FeeRateAdjustmentError, FundingBuilder, FundingContribution,
		FundingContributionError, FundingInputMode, FundingTemplate, SyncCoinSelectionSource,
		SyncFundingBuilder,
	};
	use crate::chain::ClaimId;
	use crate::util::wallet_utils::{CoinSelection, CoinSelectionSourceSync, ConfirmedUtxo, Input};
	use bitcoin::hashes::Hash;
	use bitcoin::transaction::{Transaction, TxOut, Version};
	use bitcoin::{Amount, FeeRate, Psbt, ScriptBuf, SignedAmount, WPubkeyHash, WScriptHash};

	#[test]
	#[rustfmt::skip]
	fn test_estimate_transaction_fee() {
		let one_input = [funding_input_sats(1_000)];
		let two_inputs = [funding_input_sats(1_000), funding_input_sats(1_000)];

		// 2 inputs, initiator, 2000 sat/kw feerate
		assert_eq!(
			estimate_transaction_fee(&two_inputs, &[], None, true, false, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 1512 } else { 1516 }),
		);

		// higher feerate
		assert_eq!(
			estimate_transaction_fee(&two_inputs, &[], None, true, false, FeeRate::from_sat_per_kwu(3000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 2268 } else { 2274 }),
		);

		// only 1 input
		assert_eq!(
			estimate_transaction_fee(&one_input, &[], None, true, false, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 970 } else { 972 }),
		);

		// 0 inputs
		assert_eq!(
			estimate_transaction_fee(&[], &[], None, true, false, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(428),
		);

		// not initiator
		assert_eq!(
			estimate_transaction_fee(&[], &[], None, false, false, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(0),
		);

		// splice initiator
		assert_eq!(
			estimate_transaction_fee(&one_input, &[], None, true, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 1736 } else { 1740 }),
		);

		// splice acceptor
		assert_eq!(
			estimate_transaction_fee(&one_input, &[], None, false, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 542 } else { 544 }),
		);

		// splice initiator, 1 input, 1 output
		let outputs = [funding_output_sats(500)];
		assert_eq!(
			estimate_transaction_fee(&one_input, &outputs, None, true, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 1984 } else { 1988 }),
		);

		// splice acceptor, 1 input, 1 output
		assert_eq!(
			estimate_transaction_fee(&one_input, &outputs, None, false, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 790 } else { 792 }),
		);

		// splice initiator, 1 input, 1 output, 1 change via change_output parameter
		let change = funding_output_sats(1_000);
		assert_eq!(
			estimate_transaction_fee(&one_input, &outputs, Some(&change), true, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 2232 } else { 2236 }),
		);

		// splice acceptor, 1 input, 1 output, 1 change via change_output parameter
		assert_eq!(
			estimate_transaction_fee(&one_input, &outputs, Some(&change), false, true, FeeRate::from_sat_per_kwu(2000)),
			Amount::from_sat(if cfg!(feature = "grind_signatures") { 1038 } else { 1040 }),
		);
	}

	#[rustfmt::skip]
	fn funding_input_sats(input_value_sats: u64) -> ConfirmedUtxo {
		let prevout = TxOut {
			value: Amount::from_sat(input_value_sats),
			script_pubkey: ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros()),
		};
		let prevtx = Transaction {
			input: vec![], output: vec![prevout],
			version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
		};

		ConfirmedUtxo::new_p2wpkh(prevtx, 0).unwrap()
	}

	fn funding_output_sats(output_value_sats: u64) -> TxOut {
		TxOut {
			value: Amount::from_sat(output_value_sats),
			script_pubkey: ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros()),
		}
	}

	struct UnreachableWallet;

	impl CoinSelectionSourceSync for UnreachableWallet {
		fn select_confirmed_utxos(
			&self, _claim_id: Option<ClaimId>, _must_spend: Vec<Input>, _must_pay_to: &[TxOut],
			_target_feerate_sat_per_1000_weight: u32, _max_tx_weight: u64,
		) -> Result<CoinSelection, ()> {
			unreachable!("should not reach coin selection")
		}
		fn sign_psbt(&self, _psbt: Psbt) -> Result<Transaction, ()> {
			unreachable!("should not reach signing")
		}
	}

	struct MustPayToWallet {
		utxo: ConfirmedUtxo,
		change_output: Option<TxOut>,
		expected_must_pay_to_values: Vec<Amount>,
	}

	impl CoinSelectionSourceSync for MustPayToWallet {
		fn select_confirmed_utxos(
			&self, _claim_id: Option<ClaimId>, _must_spend: Vec<Input>, must_pay_to: &[TxOut],
			_target_feerate_sat_per_1000_weight: u32, _max_tx_weight: u64,
		) -> Result<CoinSelection, ()> {
			assert_eq!(
				must_pay_to.iter().map(|output| output.value).collect::<Vec<_>>(),
				self.expected_must_pay_to_values,
			);
			Ok(CoinSelection {
				confirmed_utxos: vec![self.utxo.clone()],
				change_output: self.change_output.clone(),
			})
		}

		fn sign_psbt(&self, _psbt: Psbt) -> Result<Transaction, ()> {
			unreachable!("should not reach signing")
		}
	}

	#[test]
	fn test_funding_builder_builds_splice_out_without_wallet() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let output = funding_output_sats(25_000);

		let contribution = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::MAX_MONEY),
			feerate,
			FeeRate::MAX,
		)
		.add_output(output.clone())
		.build()
		.unwrap();

		let expected_fee = estimate_transaction_fee(
			&[],
			std::slice::from_ref(&output),
			None,
			true,
			false,
			feerate,
		);
		assert!(contribution.inputs.is_empty());
		assert_eq!(contribution.outputs, vec![output.clone()]);
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.estimated_fee, expected_fee);
		assert_eq!(
			contribution.net_value(),
			-output.value.to_signed().unwrap() - expected_fee.to_signed().unwrap(),
		);
	}

	#[test]
	fn test_funding_builder_rejects_splice_out_over_balance() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let output = funding_output_sats(25_000);
		let expected_fee = estimate_transaction_fee(
			&[],
			std::slice::from_ref(&output),
			None,
			true,
			false,
			feerate,
		);
		let exact_balance = output.value + expected_fee;

		let contribution = FundingTemplate::new(None, None, None, exact_balance)
			.splice_out(vec![output.clone()], feerate, FeeRate::MAX)
			.unwrap();
		assert_eq!(contribution.net_value(), -exact_balance.to_signed().unwrap());

		let result = FundingTemplate::new(None, None, None, exact_balance - Amount::from_sat(1))
			.splice_out(vec![output], feerate, FeeRate::MAX);
		assert!(matches!(result, Err(FundingContributionError::InvalidSpliceValue)));
	}

	#[test]
	fn test_funding_builder_requires_wallet_for_splice_in() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let builder = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::ZERO),
			feerate,
			FeeRate::MAX,
		);
		let builder = FundingBuilder(builder.0.add_value_inner(Amount::from_sat(25_000)).unwrap());

		assert!(matches!(
			builder.build(),
			Err(FundingContributionError::MissingCoinSelectionSource),
		));
	}

	#[test]
	fn test_funding_builder_amends_prior_by_dropping_subdust_change() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(500);
		let dust_limit = change.script_pubkey.minimal_non_dust();
		assert!(change.value >= dust_limit);

		let estimated_fee_with_change =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, feerate);
		let estimated_fee_no_change =
			estimate_transaction_fee(&inputs, &[], None, true, true, feerate);
		let prior = FundingContribution {
			estimated_fee: estimated_fee_with_change,
			inputs: inputs.clone(),
			outputs: vec![],
			change_output: Some(change.clone()),
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let delta = Amount::from_sat(change.value.to_sat() - dust_limit.to_sat() + 1);
		let target_value_added = prior.value_added().checked_add(delta).unwrap();
		let total_input_value: Amount = inputs.iter().map(|input| input.utxo.output.value).sum();
		let remaining_change = total_input_value
			.checked_sub(target_value_added.checked_add(estimated_fee_with_change).unwrap())
			.unwrap();
		assert_eq!(remaining_change.to_sat(), dust_limit.to_sat() - 1);
		assert!(
			total_input_value >= target_value_added.checked_add(estimated_fee_no_change).unwrap()
		);

		let builder = FundingTemplate::new(None, None, Some(prior), Amount::MAX_MONEY)
			.with_prior_contribution(feerate, FeeRate::MAX);
		let contribution =
			FundingBuilder(builder.0.add_value_inner(delta).unwrap()).build().unwrap();

		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.inputs, inputs);
		assert!(contribution.outputs.is_empty());
		assert_eq!(contribution.estimated_fee, estimated_fee_no_change);
		assert_eq!(
			contribution.value_added(),
			total_input_value.checked_sub(estimated_fee_no_change).unwrap()
		);
		assert!(contribution.value_added() > target_value_added);
	}

	#[test]
	fn test_funding_builder_remove_outputs_removes_all_matching_scripts() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let removed_script = ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros());
		let kept_script = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
		let removed_output_1 =
			TxOut { value: Amount::from_sat(10_000), script_pubkey: removed_script.clone() };
		let removed_output_2 =
			TxOut { value: Amount::from_sat(12_000), script_pubkey: removed_script.clone() };
		let kept_output = TxOut { value: Amount::from_sat(15_000), script_pubkey: kept_script };

		let contribution = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::MAX_MONEY),
			feerate,
			FeeRate::MAX,
		)
		.add_output(removed_output_1)
		.add_output(kept_output.clone())
		.add_output(removed_output_2)
		.remove_outputs(&removed_script)
		.build()
		.unwrap();

		assert_eq!(contribution.outputs, vec![kept_output]);
	}

	#[test]
	fn test_funding_builder_add_and_remove_value_update_request() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let value_added = Amount::from_sat(15_000);
		let input_template = funding_input_sats(1);
		let estimated_fee = estimate_transaction_fee(
			std::slice::from_ref(&input_template),
			&[],
			None,
			true,
			false,
			feerate,
		);
		let selected_amount = value_added + estimated_fee;
		let input = funding_input_sats(selected_amount.to_sat());
		let wallet = MustPayToWallet {
			utxo: input.clone(),
			change_output: None,
			expected_must_pay_to_values: vec![value_added],
		};

		let contribution = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::ZERO),
			feerate,
			FeeRate::MAX,
		)
		.with_coin_selection_source_sync(wallet)
		.add_value(Amount::from_sat(20_000))
		.unwrap()
		.add_value(Amount::from_sat(5_000))
		.unwrap()
		.remove_value(Amount::from_sat(10_000))
		.unwrap()
		.build()
		.unwrap();

		assert_eq!(contribution.inputs, vec![input]);
		assert!(contribution.outputs.is_empty());
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.value_added(), value_added);
	}

	#[test]
	fn test_coin_selection_request_funds_outputs_from_inputs() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let value_added = Amount::from_sat(15_000);
		let output = funding_output_sats(8_000);
		let input = funding_input_sats(50_000);
		let change_template = funding_output_sats(1_000);
		let estimated_fee = estimate_transaction_fee(
			std::slice::from_ref(&input),
			std::slice::from_ref(&output),
			Some(&change_template),
			true,
			false,
			feerate,
		);
		let change_value = input.utxo.output.value - value_added - output.value - estimated_fee;
		let wallet = MustPayToWallet {
			utxo: input,
			change_output: Some(TxOut {
				value: change_value,
				script_pubkey: change_template.script_pubkey,
			}),
			expected_must_pay_to_values: vec![output.value, value_added],
		};

		let contribution = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::MAX_MONEY),
			feerate,
			FeeRate::MAX,
		)
		.with_coin_selection_source_sync(wallet)
		.add_value(value_added)
		.unwrap()
		.add_output(output.clone())
		.build()
		.unwrap();

		assert_eq!(contribution.value_added(), value_added);
		assert_eq!(contribution.outputs, vec![output]);
		assert_eq!(contribution.change_output.as_ref().unwrap().value, change_value);
	}

	#[test]
	fn test_funding_builder_remove_value_saturates_at_zero() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let output = funding_output_sats(8_000);
		let contribution = FundingBuilder::new(
			FundingTemplate::new(None, None, None, Amount::MAX_MONEY),
			feerate,
			FeeRate::MAX,
		)
		.with_coin_selection_source_sync(UnreachableWallet)
		.add_value(Amount::from_sat(10_000))
		.unwrap()
		.remove_value(Amount::from_sat(15_000))
		.unwrap()
		.add_output(output.clone())
		.build()
		.unwrap();

		assert!(contribution.inputs.is_empty());
		assert_eq!(contribution.outputs, vec![output]);
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.value_added(), Amount::ZERO);
	}

	#[test]
	fn test_funding_builder_builds_manual_input_contribution_without_change() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let input = funding_input_sats(100_000);
		let output = funding_output_sats(25_000);

		let contribution = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_input(input.clone())
			.unwrap()
			.add_output(output.clone())
			.build()
			.unwrap();

		let expected_fee = estimate_transaction_fee(
			std::slice::from_ref(&input),
			std::slice::from_ref(&output),
			None,
			true,
			false,
			feerate,
		);
		assert_eq!(contribution.inputs, vec![input]);
		assert_eq!(contribution.outputs, vec![output.clone()]);
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
		assert_eq!(contribution.estimated_fee, expected_fee);
		assert_eq!(
			contribution.value_added(),
			Amount::from_sat(100_000) - output.value - expected_fee,
		);
		assert_eq!(
			contribution.net_value(),
			Amount::from_sat(100_000).to_signed().unwrap()
				- output.value.to_signed().unwrap()
				- expected_fee.to_signed().unwrap(),
		);
	}

	#[test]
	fn test_funding_builder_add_inputs_builds_manual_input_contribution() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let first_input = funding_input_sats(40_000);
		let second_input = funding_input_sats(60_000);
		let output = funding_output_sats(25_000);

		let contribution = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_inputs(vec![first_input.clone(), second_input.clone()])
			.unwrap()
			.add_output(output.clone())
			.build()
			.unwrap();

		let expected_fee = estimate_transaction_fee(
			&[first_input.clone(), second_input.clone()],
			std::slice::from_ref(&output),
			None,
			true,
			false,
			feerate,
		);
		assert_eq!(contribution.inputs, vec![first_input, second_input]);
		assert_eq!(contribution.outputs, vec![output.clone()]);
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
		assert_eq!(contribution.estimated_fee, expected_fee);
		assert_eq!(
			contribution.value_added(),
			Amount::from_sat(100_000) - output.value - expected_fee,
		);
	}

	#[test]
	fn test_funding_builder_rejects_duplicate_inputs() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let input = funding_input_sats(100_000);

		let result = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_inputs(vec![input.clone(), input])
			.unwrap()
			.build();

		assert!(matches!(result, Err(FundingContributionError::InvalidSpliceValue),));
	}

	#[test]
	fn test_funding_builder_rejects_duplicate_outputs() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let first_output = funding_output_sats(25_000);
		let second_output = funding_output_sats(30_000);
		assert_ne!(first_output, second_output);
		assert_eq!(first_output.script_pubkey, second_output.script_pubkey);

		let result = FundingTemplate::new(None, None, None, Amount::MAX_MONEY)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_outputs(vec![first_output, second_output])
			.build();

		assert!(matches!(result, Err(FundingContributionError::InvalidSpliceValue),));
	}

	#[test]
	fn test_funding_builder_remove_input_updates_manual_input_request() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let first_input = funding_input_sats(40_000);
		let second_input = funding_input_sats(60_000);
		let output = funding_output_sats(25_000);

		let contribution = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_inputs(vec![first_input.clone(), second_input.clone()])
			.unwrap()
			.remove_input(&first_input.utxo.outpoint)
			.unwrap()
			.add_output(output.clone())
			.build()
			.unwrap();

		let expected_fee = estimate_transaction_fee(
			std::slice::from_ref(&second_input),
			std::slice::from_ref(&output),
			None,
			true,
			false,
			feerate,
		);
		assert_eq!(contribution.inputs, vec![second_input]);
		assert_eq!(contribution.outputs, vec![output.clone()]);
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
		assert_eq!(
			contribution.value_added(),
			Amount::from_sat(60_000) - output.value - expected_fee,
		);
	}

	#[test]
	fn test_splice_in_inputs_builds_manual_input_contribution() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let first_input = funding_input_sats(40_000);
		let second_input = funding_input_sats(60_000);

		let contribution = FundingTemplate::new(None, None, None, Amount::ZERO)
			.splice_in_inputs(
				vec![first_input.clone(), second_input.clone()],
				feerate,
				FeeRate::MAX,
			)
			.unwrap();

		let expected_fee = estimate_transaction_fee(
			&[first_input.clone(), second_input.clone()],
			&[],
			None,
			true,
			false,
			feerate,
		);
		assert_eq!(contribution.inputs, vec![first_input, second_input]);
		assert!(contribution.outputs.is_empty());
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
		assert_eq!(contribution.value_added(), Amount::from_sat(100_000) - expected_fee);
	}

	#[test]
	fn test_splice_in_inputs_appends_to_prior_manual_inputs() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let prior_input = funding_input_sats(40_000);
		let additional_input = funding_input_sats(60_000);
		let prior_fee = estimate_transaction_fee(
			std::slice::from_ref(&prior_input),
			&[],
			None,
			true,
			false,
			feerate,
		);
		let prior = FundingContribution {
			estimated_fee: prior_fee,
			inputs: vec![prior_input.clone()],
			outputs: vec![],
			change_output: None,
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: false,
			input_mode: Some(FundingInputMode::ManuallySelected),
		};

		let contribution = FundingTemplate::new(None, None, Some(prior), Amount::MAX_MONEY)
			.splice_in_inputs(vec![additional_input.clone()], feerate, FeeRate::MAX)
			.unwrap();

		assert_eq!(contribution.inputs, vec![prior_input, additional_input]);
		assert!(contribution.outputs.is_empty());
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
	}

	#[test]
	fn test_sync_funding_builder_manual_inputs_insufficient_do_not_fallback_to_coin_selection() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let builder = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_input(funding_input_sats(1))
			.unwrap();
		let builder =
			SyncFundingBuilder(builder.0.with_state(SyncCoinSelectionSource(UnreachableWallet)));

		assert!(matches!(
			builder.build(),
			Err(FundingContributionError::ManuallySelectedInputsInsufficient),
		));
	}

	#[test]
	fn test_funding_builder_rejects_manual_inputs_with_value_request() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let builder = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_input(funding_input_sats(100_000))
			.unwrap();
		let result = builder.clone().0.add_value_inner(Amount::from_sat(1_000));
		assert!(matches!(result, Err(FundingContributionError::InvalidSpliceValue),));

		let builder =
			SyncFundingBuilder(builder.0.with_state(SyncCoinSelectionSource(UnreachableWallet)));
		let result = builder.remove_value(Amount::from_sat(1_000));
		assert!(matches!(result, Err(FundingContributionError::InvalidSpliceValue),));
	}

	#[test]
	fn test_funding_builder_rejects_manual_inputs_on_coin_selected_prior() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let prior_input = funding_input_sats(100_000);
		let prior_outpoint = prior_input.utxo.outpoint;
		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(1_000),
			inputs: vec![prior_input],
			outputs: vec![],
			change_output: Some(funding_output_sats(10_000)),
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: false,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let builder = FundingTemplate::new(None, None, Some(prior), Amount::MAX_MONEY)
			.with_prior_contribution(feerate, FeeRate::MAX);

		assert!(matches!(
			builder.clone().add_input(funding_input_sats(50_000)),
			Err(FundingContributionError::InvalidSpliceValue),
		));
		assert!(matches!(
			builder.remove_input(&prior_outpoint),
			Err(FundingContributionError::InvalidSpliceValue),
		));
	}

	#[test]
	fn test_funding_builder_validates_manual_input_max_money() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let inputs = vec![funding_input_sats(Amount::MAX_MONEY.to_sat()), funding_input_sats(1)];

		let builder = FundingTemplate::new(None, None, None, Amount::ZERO)
			.without_prior_contribution(feerate, FeeRate::MAX)
			.add_inputs(inputs)
			.unwrap();

		assert!(matches!(builder.build(), Err(FundingContributionError::InvalidSpliceValue),));
	}

	#[test]
	fn test_build_from_prior_manual_inputs_exact_match_reuses_and_adjusts() {
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let input = funding_input_sats(100_000);
		let output = funding_output_sats(20_000);
		let estimated_fee = estimate_transaction_fee(
			std::slice::from_ref(&input),
			std::slice::from_ref(&output),
			None,
			true,
			false,
			original_feerate,
		);
		let prior = FundingContribution {
			estimated_fee,
			inputs: vec![input.clone()],
			outputs: vec![output.clone()],
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: false,
			input_mode: Some(FundingInputMode::ManuallySelected),
		};

		let contribution = FundingTemplate::new(None, None, Some(prior), Amount::MAX_MONEY)
			.with_prior_contribution(target_feerate, FeeRate::MAX)
			.build()
			.unwrap();

		assert_eq!(contribution.inputs, vec![input]);
		assert_eq!(contribution.outputs, vec![output]);
		assert_eq!(contribution.feerate, target_feerate);
		assert_eq!(contribution.input_mode, Some(FundingInputMode::ManuallySelected));
	}

	#[test]
	fn test_build_from_prior_manual_inputs_changed_request_insufficient_maps_error() {
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let input = funding_input_sats(50_000);
		let estimated_fee =
			estimate_transaction_fee(std::slice::from_ref(&input), &[], None, true, false, feerate);
		let prior = FundingContribution {
			estimated_fee,
			inputs: vec![input],
			outputs: vec![],
			change_output: None,
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: false,
			input_mode: Some(FundingInputMode::ManuallySelected),
		};

		let result = FundingTemplate::new(None, None, Some(prior), Amount::ZERO)
			.with_prior_contribution(feerate, FeeRate::MAX)
			.add_output(funding_output_sats(60_000))
			.build();

		assert!(matches!(
			result,
			Err(FundingContributionError::ManuallySelectedInputsInsufficient),
		));
	}

	#[test]
	fn test_for_acceptor_at_feerate_manual_inputs_balance_insufficient() {
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(100_000);
		let inputs = vec![funding_input_sats(100_000)];
		let outputs = vec![funding_output_sats(80_000)];
		let net_value_without_fee = Amount::from_sat(20_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &outputs, None, true, true, original_feerate);
		let target_fee =
			estimate_transaction_fee(&inputs, &outputs, None, false, true, target_feerate);
		assert!(target_fee > net_value_without_fee);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs,
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::ManuallySelected),
		};

		let holder_balance = target_fee
			.checked_sub(net_value_without_fee)
			.and_then(|shortfall| shortfall.checked_sub(Amount::from_sat(1)))
			.unwrap();
		match contribution.for_acceptor_at_feerate(target_feerate, holder_balance) {
			Err(FeeRateAdjustmentError::FeeBufferInsufficient { source, available, required }) => {
				assert_eq!(source, "channel balance");
				assert_eq!(available, target_fee - Amount::from_sat(1));
				assert_eq!(required, target_fee);
			},
			other => panic!("Expected channel-balance shortfall, got {other:?}"),
		}
	}

	#[test]
	fn test_for_acceptor_at_feerate_manual_inputs_balance_sufficient() {
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(100_000);
		let inputs = vec![funding_input_sats(100_000)];
		let outputs = vec![funding_output_sats(80_000)];
		let net_value_without_fee = Amount::from_sat(20_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &outputs, None, true, true, original_feerate);
		let target_fee =
			estimate_transaction_fee(&inputs, &outputs, None, false, true, target_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: inputs.clone(),
			outputs: outputs.clone(),
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::ManuallySelected),
		};

		let holder_balance = target_fee.checked_sub(net_value_without_fee).unwrap();
		let adjusted =
			contribution.for_acceptor_at_feerate(target_feerate, holder_balance).unwrap();

		assert_eq!(adjusted.inputs, inputs);
		assert_eq!(adjusted.outputs, outputs);
		assert_eq!(adjusted.estimated_fee, target_fee);
		assert_eq!(
			adjusted.net_value(),
			net_value_without_fee.to_signed().unwrap() - target_fee.to_signed().unwrap(),
		);
	}

	#[test]
	fn test_build_funding_contribution_validates_max_money() {
		let over_max = Amount::MAX_MONEY + Amount::from_sat(1);
		let feerate = FeeRate::from_sat_per_kwu(2000);

		// splice_in_sync with value_added > MAX_MONEY
		{
			let template = FundingTemplate::new(None, None, None, Amount::ZERO);
			assert!(matches!(
				template.splice_in_sync(over_max, feerate, feerate, UnreachableWallet),
				Err(FundingContributionError::InvalidSpliceValue),
			));
		}

		// splice_out with single output value > MAX_MONEY
		{
			let template = FundingTemplate::new(None, None, None, Amount::ZERO);
			let outputs = vec![funding_output_sats(over_max.to_sat())];
			assert!(matches!(
				template.splice_out(outputs, feerate, feerate),
				Err(FundingContributionError::InvalidSpliceValue),
			));
		}

		// splice_out with multiple outputs summing > MAX_MONEY
		{
			let template = FundingTemplate::new(None, None, None, Amount::ZERO);
			let half_over = Amount::MAX_MONEY / 2 + Amount::from_sat(1);
			let outputs = vec![
				funding_output_sats(half_over.to_sat()),
				funding_output_sats(half_over.to_sat()),
			];
			assert!(matches!(
				template.splice_out(outputs, feerate, feerate),
				Err(FundingContributionError::InvalidSpliceValue),
			));
		}
	}

	#[test]
	fn test_funding_builder_validates_mixed_request_max_money() {
		let over_max = Amount::MAX_MONEY + Amount::from_sat(1);
		let feerate = FeeRate::from_sat_per_kwu(2000);

		// Mixed add/remove request with value_added > MAX_MONEY.
		assert!(matches!(
			FundingTemplate::new(None, None, None, Amount::ZERO)
				.without_prior_contribution(feerate, feerate)
				.with_coin_selection_source_sync(UnreachableWallet)
				.add_value(over_max)
				.unwrap()
				.add_outputs(vec![funding_output_sats(1_000)])
				.build(),
			Err(FundingContributionError::InvalidSpliceValue),
		));

		// Mixed add/remove request with outputs summing > MAX_MONEY.
		let half_over = Amount::MAX_MONEY / 2 + Amount::from_sat(1);
		assert!(matches!(
			FundingTemplate::new(None, None, None, Amount::ZERO)
				.without_prior_contribution(feerate, feerate)
				.with_coin_selection_source_sync(UnreachableWallet)
				.add_value(Amount::from_sat(1_000))
				.unwrap()
				.add_outputs(vec![
					funding_output_sats(half_over.to_sat()),
					funding_output_sats(half_over.to_sat()),
				])
				.build(),
			Err(FundingContributionError::InvalidSpliceValue),
		));
	}

	#[test]
	fn test_build_funding_contribution_validates_feerate_range() {
		let low = FeeRate::from_sat_per_kwu(1000);
		let high = FeeRate::from_sat_per_kwu(2000);

		// min_feerate > max_feerate is rejected
		{
			let template = FundingTemplate::new(None, None, None, Amount::ZERO);
			assert!(matches!(
				template.splice_in_sync(Amount::from_sat(10_000), high, low, UnreachableWallet),
				Err(FundingContributionError::FeeRateExceedsMaximum { .. }),
			));
		}

		// min_feerate < min_rbf_feerate is rejected
		{
			let template = FundingTemplate::new(None, Some(high), None, Amount::ZERO);
			assert!(matches!(
				template.splice_in_sync(
					Amount::from_sat(10_000),
					low,
					FeeRate::MAX,
					UnreachableWallet
				),
				Err(FundingContributionError::FeeRateBelowRbfMinimum { .. }),
			));
		}
	}

	#[test]
	fn test_build_funding_contribution_rejects_oversized_prevtx() {
		use crate::util::ser::Writeable;

		let feerate = FeeRate::from_sat_per_kwu(2000);
		let prevtx = Transaction {
			input: vec![],
			output: vec![funding_output_sats(50_000); 2_200],
			version: Version::TWO,
			lock_time: bitcoin::absolute::LockTime::ZERO,
		};
		assert!(prevtx.serialized_length() > crate::ln::LN_MAX_MSG_LEN);

		let wallet = SingleUtxoWallet {
			utxo: ConfirmedUtxo::new_p2wpkh(prevtx, 0).unwrap(),
			change_output: None,
		};
		assert!(matches!(
			FundingTemplate::new(None, None, None, Amount::ZERO)
				.with_prior_contribution(feerate, feerate)
				.with_coin_selection_source_sync(wallet)
				.add_value(Amount::from_sat(10_000))
				.unwrap()
				.build(),
			Err(FundingContributionError::PrevTxTooLarge),
		));
	}

	#[test]
	fn test_for_acceptor_at_feerate_higher_change_adjusted() {
		// Splice-in: higher target feerate reduces the change output.
		// The fee overestimates (with is_initiator=true) by including common TX fields, shared
		// output, and shared input weight. So we need a sufficiently high target feerate for the
		// acceptor's target fee to exceed the original fee estimate, causing the change to decrease.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(6000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		// Fee estimate computed as initiator (overestimate), including change output weight.
		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: inputs.clone(),
			outputs: vec![],
			change_output: Some(change.clone()),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let net_value_before = contribution.net_value();
		let contribution =
			contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY).unwrap();

		// Target fee at target feerate for acceptor (is_initiator=false), including change weight.
		let expected_target_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), false, true, target_feerate);
		let expected_change = estimated_fee + Amount::from_sat(10_000) - expected_target_fee;

		assert_eq!(contribution.estimated_fee, expected_target_fee);
		assert!(contribution.change_output.is_some());
		assert_eq!(contribution.change_output.as_ref().unwrap().value, expected_change);
		assert!(expected_change < Amount::from_sat(10_000)); // Change reduced
		assert_eq!(contribution.net_value(), net_value_before);
	}

	#[test]
	fn test_for_acceptor_at_feerate_lower_rejected_too_low() {
		// Splice-in: target feerate below our minimum is rejected as FeeRateTooLow.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(1000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeRateTooLow { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_change_removed() {
		// Splice-in: feerate high enough that change drops below dust and is removed,
		// but the fee buffer (estimated_fee + change) still covers the fee without the change output.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(7000);
		let value_added = Amount::from_sat(50_000);
		let change_value = Amount::from_sat(500);

		// Compute estimated_fee first (weight-based, independent of input value).
		let dummy_inputs = vec![funding_input_sats(1)];
		let change = funding_output_sats(change_value.to_sat());
		let estimated_fee = estimate_transaction_fee(
			&dummy_inputs,
			&[],
			Some(&change),
			true,
			true,
			original_feerate,
		);

		// Realistic input: value_added + estimated_fee + change (what coin selection produces).
		let input_value = value_added + estimated_fee + change_value;
		let inputs = vec![funding_input_sats(input_value.to_sat())];
		let change = funding_output_sats(change_value.to_sat());

		let contribution = FundingContribution {
			estimated_fee,
			inputs: inputs.clone(),
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let net_value_before = contribution.net_value();
		let contribution =
			contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY).unwrap();

		// Change should be removed; estimated_fee updated to no-change target fee.
		assert!(contribution.change_output.is_none());
		let expected_fee_no_change =
			estimate_transaction_fee(&inputs, &[], None, false, true, target_feerate);
		assert_eq!(contribution.estimated_fee, expected_fee_no_change);
		// The surplus (old fee buffer - new fee) goes to value_added, increasing net_value.
		let surplus = estimated_fee + change_value - expected_fee_no_change;
		assert_eq!(contribution.net_value(), net_value_before + surplus.to_signed().unwrap());
	}

	#[test]
	fn test_for_acceptor_at_feerate_too_high_rejected() {
		// Splice-in: feerate so high that even without change, the fee can't be covered.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(100_000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(500);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_splice_out_sufficient() {
		// Splice-out (no inputs): the fee estimate from the is_initiator=true overestimate covers
		// the acceptor's target fee at a moderately higher target feerate.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs: outputs.clone(),
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let contribution =
			contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY).unwrap();
		// estimated_fee is updated to the target fee; surplus goes back to channel balance.
		let expected_target_fee =
			estimate_transaction_fee(&[], &outputs, None, false, true, target_feerate);
		assert_eq!(contribution.estimated_fee, expected_target_fee);
		assert!(expected_target_fee <= estimated_fee);
	}

	#[test]
	fn test_for_acceptor_at_feerate_splice_out_insufficient() {
		// Splice-out: channel balance too small for outputs + target fee at high target feerate.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(50_000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs,
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// Balance of 55,000 sats can't cover outputs (50,000) + target_fee at 50k sat/kwu.
		let spliceable_balance = Amount::from_sat(55_000);
		let result = contribution.for_acceptor_at_feerate(target_feerate, spliceable_balance);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_net_value_for_acceptor_at_feerate_splice_in() {
		// Splice-in: net_value_for_acceptor_at_feerate returns the same value as net_value() since
		// splice-in fees are paid by inputs, not from channel balance.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);
		let change_value = change.value;

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// For splice-in with change that stays above dust, the surplus is absorbed by the change
		// output so net_value_for_acceptor_at_feerate equals net_value.
		let net_at_feerate = contribution
			.net_value_for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY)
			.unwrap();
		assert_eq!(net_at_feerate, contribution.net_value());
		assert_eq!(
			net_at_feerate,
			(Amount::from_sat(100_000) - estimated_fee - change_value).to_signed().unwrap(),
		);
	}

	#[test]
	fn test_net_value_for_acceptor_at_feerate_splice_out() {
		// Splice-out: net_value_for_acceptor_at_feerate returns the adjusted value using the target fee
		// at the target feerate.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs: outputs.clone(),
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let net_at_feerate = contribution
			.net_value_for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY)
			.unwrap();

		// The target fee at target feerate should be less than the initiator's fee estimate.
		let target_fee = estimate_transaction_fee(&[], &outputs, None, false, true, target_feerate);
		let expected_net = SignedAmount::ZERO
			- Amount::from_sat(50_000).to_signed().unwrap()
			- target_fee.to_signed().unwrap();
		assert_eq!(net_at_feerate, expected_net);

		// Should be less negative than net_value() which uses the higher fee estimate.
		assert!(net_at_feerate > contribution.net_value());
	}

	#[test]
	fn test_net_value_for_acceptor_at_feerate_does_not_mutate() {
		// Verify net_value_for_acceptor_at_feerate does not modify the contribution.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(5000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let net_before = contribution.net_value();
		let fee_before = contribution.estimated_fee;
		let change_before = contribution.change_output.as_ref().unwrap().value;

		let _ = contribution.net_value_for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);

		// Nothing should have changed.
		assert_eq!(contribution.net_value(), net_before);
		assert_eq!(contribution.estimated_fee, fee_before);
		assert_eq!(contribution.change_output.as_ref().unwrap().value, change_before);
	}

	#[test]
	fn test_net_value_for_acceptor_at_feerate_too_high() {
		// net_value_for_acceptor_at_feerate returns Err when feerate can't be accommodated.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(100_000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(500);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result =
			contribution.net_value_for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_exceeds_max_rejected() {
		// Splice-in: target feerate exceeds max_feerate and target fee exceeds the fee buffer,
		// so the adjustment is rejected as FeeRateTooHigh.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let max_feerate = FeeRate::from_sat_per_kwu(3000);
		let target_feerate = FeeRate::from_sat_per_kwu(100_000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeRateTooHigh { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_exceeds_max_allowed() {
		// Splice-in: target feerate exceeds max_feerate but the acceptor's target fee
		// (is_initiator=false at target) is less than the fee buffer (is_initiator=true at
		// original feerate). This works because the initiator fee estimate includes ~598 WU of
		// extra weight (common TX fields, funding output, shared input) that the acceptor
		// doesn't pay for, so the fee buffer is ~2.5x larger than the acceptor's target fee at
		// the same feerate.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let max_feerate = FeeRate::from_sat_per_kwu(3000);
		let target_feerate = FeeRate::from_sat_per_kwu(4000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change.clone()),
			feerate: original_feerate,
			max_feerate,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(result.is_ok());
		let adjusted = result.unwrap();

		// The acceptor's target fee at target (4000, is_initiator=false) is less than the
		// fee estimate at original (2000, is_initiator=true) due to the ~2.5x weight ratio,
		// so change increases despite the higher feerate.
		assert!(adjusted.change_output.is_some());
		assert!(adjusted.change_output.as_ref().unwrap().value > Amount::from_sat(10_000));
	}

	#[test]
	fn test_for_acceptor_at_feerate_within_range() {
		// Splice-in: target feerate is between min and max, so the min/max checks
		// don't interfere and the normal adjustment logic applies.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let max_feerate = FeeRate::from_sat_per_kwu(5000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(result.is_ok());
		let adjusted = result.unwrap();

		// At a higher target feerate, the target fee increases so change should decrease
		// (or stay the same if the fee estimate absorbs the difference).
		// The key assertion is that the adjustment succeeds with a valid change output.
		assert!(adjusted.change_output.is_some());
	}

	#[test]
	fn test_for_acceptor_at_feerate_no_change_shortfall_from_value_added() {
		// Inputs present, no change output. Higher target feerate makes target_fee > estimated_fee.
		// With realistic inputs (no coin selection surplus), the fee buffer is just estimated_fee,
		// so the shortfall cannot be absorbed and the contribution is dropped.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(20_000);
		let value_added = Amount::from_sat(50_000);

		// Compute estimated_fee first (weight-based, independent of input value).
		let dummy_inputs = vec![funding_input_sats(1)];
		let estimated_fee =
			estimate_transaction_fee(&dummy_inputs, &[], None, true, true, original_feerate);

		// Realistic input: value_added + estimated_fee (what coin selection produces, no surplus).
		let inputs = vec![funding_input_sats((value_added + estimated_fee).to_sat())];
		let target_fee = estimate_transaction_fee(&inputs, &[], None, false, true, target_feerate);

		// Verify our setup: target_fee > estimated_fee (shortfall exists) and the fee buffer
		// (estimated_fee, with no coin selection surplus) cannot cover it.
		assert!(target_fee > estimated_fee);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_no_change_insufficient() {
		// Inputs present, no change output. The target feerate is so high that the fee buffer
		// (total input value minus value_added) cannot cover the target fee.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(20_000);
		let value_added = Amount::from_sat(1);

		// Compute estimated_fee first (weight-based, independent of input value).
		let dummy_inputs = vec![funding_input_sats(1)];
		let estimated_fee =
			estimate_transaction_fee(&dummy_inputs, &[], None, true, true, original_feerate);

		// Realistic input: value_added + estimated_fee (no surplus).
		let inputs = vec![funding_input_sats((value_added + estimated_fee).to_sat())];
		let target_fee = estimate_transaction_fee(&inputs, &[], None, false, true, target_feerate);
		assert!(target_fee > estimated_fee);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_no_change_surplus_below_dust() {
		// Inputs present, no change output. The acceptor built their contribution at a low
		// feerate as if they were the initiator (including common TX fields in estimated_fee).
		// The initiator proposes a ~3x higher feerate. At that rate, the acceptor's target fee
		// (only their personal input weight) nearly matches the original fee estimate, leaving a
		// small surplus below the dust limit.
		let original_feerate = FeeRate::from_sat_per_kwu(1000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let inputs = vec![funding_input_sats(100_000)];

		// estimated_fee includes common TX fields (is_initiator=true) at the original feerate.
		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], None, true, true, original_feerate);

		// target_fee only includes the acceptor's contributed weight (is_initiator=false) at the
		// higher target feerate.
		let target_fee = estimate_transaction_fee(&inputs, &[], None, false, true, target_feerate);

		// Verify our setup: surplus is positive and below the P2WPKH dust limit (294 sats).
		assert!(estimated_fee > target_fee);
		let dust_limit = ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros()).minimal_non_dust();
		assert!(estimated_fee - target_fee < dust_limit);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY);
		assert!(result.is_ok());
		let adjusted = result.unwrap();
		assert!(adjusted.change_output.is_none());
		assert_eq!(adjusted.estimated_fee, target_fee);
	}

	#[test]
	fn test_for_acceptor_at_feerate_no_change_surplus_absorbed() {
		// Inputs, no change. The estimated_fee (is_initiator=true) far exceeds the acceptor's
		// target fee (is_initiator=false). The surplus stays in the channel contribution rather
		// than being burned as excess fees.
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let value_added = Amount::from_sat(50_000);

		// Compute estimated_fee first (weight-based, independent of input value).
		let dummy_inputs = vec![funding_input_sats(1)];
		let estimated_fee = estimate_transaction_fee(&dummy_inputs, &[], None, true, true, feerate);

		// Realistic input: value_added + estimated_fee (no surplus).
		let inputs = vec![funding_input_sats((value_added + estimated_fee).to_sat())];

		// Initiator fee estimate includes common TX fields + shared output + shared input weight,
		// making it ~3x the acceptor's target fee at the same feerate.
		let target_fee = estimate_transaction_fee(&inputs, &[], None, false, true, feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: None,
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// target == min feerate, so FeeRateTooLow check passes.
		// The surplus (estimated_fee - target_fee) goes to value_added (shared output).
		let net_value_before = contribution.net_value();
		let result = contribution.for_acceptor_at_feerate(feerate, Amount::MAX_MONEY);
		assert!(result.is_ok());
		let adjusted = result.unwrap();
		assert!(adjusted.change_output.is_none());
		assert_eq!(adjusted.estimated_fee, target_fee);
		let surplus = estimated_fee - target_fee;
		assert_eq!(adjusted.value_added(), value_added + surplus);
		assert_eq!(adjusted.net_value(), net_value_before + surplus.to_signed().unwrap());
	}

	#[test]
	fn test_for_acceptor_at_feerate_fee_buffer_overflow_with_change() {
		// Overflow in estimated_fee + change value should surface as FeeBufferOverflow.
		let feerate = FeeRate::from_sat_per_kwu(2000);
		let contribution = FundingContribution {
			estimated_fee: Amount::MAX,
			inputs: vec![funding_input_sats(100_000)],
			outputs: vec![],
			change_output: Some(funding_output_sats(1)),
			feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let result = contribution.for_acceptor_at_feerate(feerate, Amount::MAX_MONEY);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferOverflow)));
	}

	#[test]
	fn test_for_acceptor_at_feerate_splice_out_balance_insufficient() {
		// Splice-out: channel balance too small to cover outputs + target fee.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs: outputs.clone(),
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// Balance of 40,000 sats is less than outputs (50,000) + target_fee.
		let spliceable_balance = Amount::from_sat(40_000);
		let result = contribution.for_acceptor_at_feerate(target_feerate, spliceable_balance);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_acceptor_at_feerate_splice_out_balance_sufficient() {
		// Splice-out: channel balance large enough to cover outputs + target fee.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs: outputs.clone(),
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// Balance of 100,000 sats is more than outputs (50,000) + target_fee.
		let spliceable_balance = Amount::from_sat(100_000);
		let contribution =
			contribution.for_acceptor_at_feerate(target_feerate, spliceable_balance).unwrap();
		let expected_target_fee =
			estimate_transaction_fee(&[], &outputs, None, false, true, target_feerate);
		assert_eq!(contribution.estimated_fee, expected_target_fee);
	}

	#[test]
	fn test_net_value_for_acceptor_at_feerate_splice_out_balance_insufficient() {
		// Splice-out: net_value_for_acceptor_at_feerate returns Err when channel balance
		// is too small to cover outputs + target fee.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let outputs = vec![funding_output_sats(50_000)];

		let estimated_fee =
			estimate_transaction_fee(&[], &outputs, None, true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs: vec![],
			outputs,
			change_output: None,
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// Balance of 40,000 sats is less than outputs (50,000) + target_fee.
		let spliceable_balance = Amount::from_sat(40_000);
		let result =
			contribution.net_value_for_acceptor_at_feerate(target_feerate, spliceable_balance);
		assert!(matches!(result, Err(FeeRateAdjustmentError::FeeBufferInsufficient { .. })));
	}

	#[test]
	fn test_for_initiator_at_feerate_higher_fee_than_acceptor() {
		// Verify that the initiator fee estimate is higher than the acceptor estimate at the
		// same feerate, since the initiator pays for common fields + shared input/output.
		let original_feerate = FeeRate::from_sat_per_kwu(2000);
		let target_feerate = FeeRate::from_sat_per_kwu(3000);
		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);

		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, original_feerate);

		let contribution = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: original_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let acceptor = contribution
			.clone()
			.for_acceptor_at_feerate(target_feerate, Amount::MAX_MONEY)
			.unwrap();
		let initiator =
			contribution.for_initiator_at_feerate(target_feerate, Amount::MAX_MONEY).unwrap();

		// Initiator pays more in fees (common fields + shared input/output weight).
		assert!(initiator.estimated_fee > acceptor.estimated_fee);
		// Initiator has less change remaining.
		assert!(
			initiator.change_output.as_ref().unwrap().value
				< acceptor.change_output.as_ref().unwrap().value
		);
		// Both have the adjusted feerate.
		assert_eq!(initiator.feerate, target_feerate);
		assert_eq!(acceptor.feerate, target_feerate);
	}

	#[test]
	fn test_rbf_rejects_max_feerate_below_min_rbf_feerate() {
		// When the caller's max_feerate is below the minimum RBF feerate,
		// rbf_prior_contribution_sync should return an error.
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let max_feerate = FeeRate::from_sat_per_kwu(2020);

		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(1_000),
			inputs: vec![funding_input_sats(100_000)],
			outputs: vec![],
			change_output: None,
			feerate: prior_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		// max_feerate (2020) < min_rbf_feerate (2025).
		let template =
			FundingTemplate::new(None, Some(min_rbf_feerate), Some(prior), Amount::MAX_MONEY);
		assert!(matches!(
			template.rbf_prior_contribution_sync(None, max_feerate, UnreachableWallet),
			Err(FundingContributionError::FeeRateExceedsMaximum { .. }),
		));
	}

	#[test]
	fn test_rbf_adjusts_prior_to_rbf_feerate() {
		// When the prior contribution's feerate is below the minimum RBF feerate and holder
		// balance is available, rbf_prior_contribution_sync should adjust the prior to the
		// RBF feerate.
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let max_feerate = FeeRate::from_sat_per_kwu(5000);

		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);
		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, prior_feerate);

		let prior = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: prior_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template =
			FundingTemplate::new(None, Some(min_rbf_feerate), Some(prior), Amount::MAX_MONEY);
		let contribution =
			template.rbf_prior_contribution_sync(None, max_feerate, UnreachableWallet).unwrap();
		assert_eq!(contribution.feerate, min_rbf_feerate);
		assert_eq!(contribution.max_feerate, max_feerate);
	}

	#[test]
	fn test_rbf_uses_explicit_override_feerate() {
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let override_feerate = FeeRate::from_sat_per_kwu(2100);
		let max_feerate = FeeRate::from_sat_per_kwu(5000);

		let inputs = vec![funding_input_sats(100_000)];
		let change = funding_output_sats(10_000);
		let estimated_fee =
			estimate_transaction_fee(&inputs, &[], Some(&change), true, true, prior_feerate);

		let prior = FundingContribution {
			estimated_fee,
			inputs,
			outputs: vec![],
			change_output: Some(change),
			feerate: prior_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template =
			FundingTemplate::new(None, Some(min_rbf_feerate), Some(prior), Amount::MAX_MONEY);
		let contribution = template
			.rbf_prior_contribution_sync(Some(override_feerate), max_feerate, UnreachableWallet)
			.unwrap();
		assert_eq!(contribution.feerate, override_feerate);
		assert_eq!(contribution.max_feerate, max_feerate);
	}

	#[test]
	fn test_rbf_rejects_explicit_override_below_min_rbf_feerate() {
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let override_feerate = FeeRate::from_sat_per_kwu(2024);

		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(1_000),
			inputs: vec![funding_input_sats(100_000)],
			outputs: vec![],
			change_output: None,
			feerate: prior_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template =
			FundingTemplate::new(None, Some(min_rbf_feerate), Some(prior), Amount::MAX_MONEY);
		assert!(matches!(
			template.rbf_prior_contribution_sync(
				Some(override_feerate),
				FeeRate::MAX,
				UnreachableWallet,
			),
			Err(FundingContributionError::FeeRateBelowRbfMinimum { .. }),
		));
	}

	#[test]
	fn test_rbf_rejects_explicit_override_above_max_feerate() {
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let override_feerate = FeeRate::from_sat_per_kwu(2100);
		let max_feerate = FeeRate::from_sat_per_kwu(2099);

		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(1_000),
			inputs: vec![funding_input_sats(100_000)],
			outputs: vec![],
			change_output: None,
			feerate: prior_feerate,
			max_feerate: FeeRate::MAX,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template =
			FundingTemplate::new(None, Some(min_rbf_feerate), Some(prior), Amount::MAX_MONEY);
		assert!(matches!(
			template.rbf_prior_contribution_sync(
				Some(override_feerate),
				max_feerate,
				UnreachableWallet,
			),
			Err(FundingContributionError::FeeRateExceedsMaximum { .. }),
		));
	}

	/// A mock wallet that returns a single UTXO for coin selection.
	struct SingleUtxoWallet {
		utxo: ConfirmedUtxo,
		change_output: Option<TxOut>,
	}

	impl CoinSelectionSourceSync for SingleUtxoWallet {
		fn select_confirmed_utxos(
			&self, _claim_id: Option<ClaimId>, _must_spend: Vec<Input>, _must_pay_to: &[TxOut],
			_target_feerate_sat_per_1000_weight: u32, _max_tx_weight: u64,
		) -> Result<CoinSelection, ()> {
			Ok(CoinSelection {
				confirmed_utxos: vec![self.utxo.clone()],
				change_output: self.change_output.clone(),
			})
		}
		fn sign_psbt(&self, _psbt: Psbt) -> Result<Transaction, ()> {
			unreachable!("should not reach signing")
		}
	}

	fn shared_input(value_sats: u64) -> Input {
		Input {
			outpoint: bitcoin::OutPoint::null(),
			previous_utxo: TxOut {
				value: Amount::from_sat(value_sats),
				script_pubkey: ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros()),
			},
			satisfaction_weight: 107,
		}
	}

	#[test]
	fn test_rbf_unadjusted_splice_out_runs_coin_selection() {
		// When the prior contribution's feerate is below the minimum RBF feerate and no
		// holder balance is available, rbf_prior_contribution_sync should run coin selection to
		// add inputs that cover the higher RBF fee.
		let prior_feerate = FeeRate::from_sat_per_kwu(2000);
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let withdrawal = funding_output_sats(20_000);

		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(500),
			inputs: vec![],
			outputs: vec![withdrawal.clone()],
			change_output: None,
			feerate: prior_feerate,
			max_feerate: prior_feerate,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template = FundingTemplate::new(
			Some(shared_input(100_000)),
			Some(min_rbf_feerate),
			Some(prior),
			Amount::ZERO,
		);

		let wallet = SingleUtxoWallet {
			utxo: funding_input_sats(50_000),
			change_output: Some(funding_output_sats(25_000)),
		};

		// rbf_prior_contribution_sync should succeed and the contribution should have inputs from
		// coin selection.
		let contribution =
			template.rbf_prior_contribution_sync(None, FeeRate::MAX, &wallet).unwrap();
		assert!(!contribution.inputs.is_empty(), "coin selection should have added inputs");
		assert!(contribution.value_added() > Amount::ZERO);
		assert_eq!(contribution.outputs, vec![withdrawal]);
		assert_eq!(contribution.feerate, min_rbf_feerate);
	}

	#[test]
	fn test_rbf_unadjusted_uses_callers_max_feerate() {
		// When the prior contribution's feerate is below the minimum RBF feerate and no
		// holder balance is available, rbf_prior_contribution_sync should use the caller's
		// max_feerate (not the prior's) for the resulting contribution.
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let prior_max_feerate = FeeRate::from_sat_per_kwu(50_000);
		let callers_max_feerate = FeeRate::from_sat_per_kwu(10_000);
		let withdrawal = funding_output_sats(20_000);

		let prior = FundingContribution {
			estimated_fee: Amount::from_sat(500),
			inputs: vec![],
			outputs: vec![withdrawal.clone()],
			change_output: None,
			feerate: FeeRate::from_sat_per_kwu(2000),
			max_feerate: prior_max_feerate,
			is_splice: true,
			input_mode: Some(FundingInputMode::CoinSelected),
		};

		let template = FundingTemplate::new(
			Some(shared_input(100_000)),
			Some(min_rbf_feerate),
			Some(prior),
			Amount::MAX_MONEY,
		);

		let wallet = SingleUtxoWallet {
			utxo: funding_input_sats(50_000),
			change_output: Some(funding_output_sats(25_000)),
		};

		let contribution =
			template.rbf_prior_contribution_sync(None, callers_max_feerate, &wallet).unwrap();
		assert_eq!(
			contribution.max_feerate, callers_max_feerate,
			"should use caller's max_feerate, not prior's"
		);
	}

	#[test]
	fn test_splice_out_skips_coin_selection_during_rbf() {
		// When splice_out is called on a template with min_rbf_feerate set (user choosing a
		// fresh splice-out instead of rbf_prior_contribution_sync), coin selection should NOT
		// run.
		// Fees come from the channel balance.
		let min_rbf_feerate = FeeRate::from_sat_per_kwu(2025);
		let feerate = FeeRate::from_sat_per_kwu(2025);
		let withdrawal = funding_output_sats(20_000);

		let template = FundingTemplate::new(
			Some(shared_input(100_000)),
			Some(min_rbf_feerate),
			None,
			Amount::MAX_MONEY,
		);

		let contribution =
			template.splice_out(vec![withdrawal.clone()], feerate, FeeRate::MAX).unwrap();
		assert_eq!(contribution.value_added(), Amount::ZERO);
		assert!(contribution.inputs.is_empty());
		assert!(contribution.change_output.is_none());
		assert_eq!(contribution.outputs, vec![withdrawal]);
	}
}