lnrpc 0.1.0

RPC bindings for github.com/lightningnetwork/lnd
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
package lnwallet_test

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/btcsuite/btcd/btcec"
	"github.com/btcsuite/btcd/btcjson"
	"github.com/btcsuite/btcd/chaincfg"
	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"github.com/btcsuite/btcd/integration/rpctest"
	"github.com/btcsuite/btcd/mempool"
	"github.com/btcsuite/btcd/rpcclient"
	"github.com/btcsuite/btcd/txscript"
	"github.com/btcsuite/btcd/wire"
	"github.com/btcsuite/btcutil"
	"github.com/btcsuite/btcwallet/chain"
	"github.com/btcsuite/btcwallet/walletdb"
	_ "github.com/btcsuite/btcwallet/walletdb/bdb"
	"github.com/davecgh/go-spew/spew"
	"github.com/lightninglabs/neutrino"
	"github.com/lightningnetwork/lnd/chainntnfs"
	"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
	"github.com/lightningnetwork/lnd/channeldb"
	"github.com/lightningnetwork/lnd/channeldb/kvdb"
	"github.com/lightningnetwork/lnd/input"
	"github.com/lightningnetwork/lnd/keychain"
	"github.com/lightningnetwork/lnd/lntest/wait"
	"github.com/lightningnetwork/lnd/lnwallet"
	"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
	"github.com/lightningnetwork/lnd/lnwallet/chainfee"
	"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
	"github.com/lightningnetwork/lnd/lnwire"
)

var (
	bobsPrivKey = []byte{
		0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
		0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
		0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
		0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9,
	}

	// Use a hard-coded HD seed.
	testHdSeed = chainhash.Hash{
		0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
		0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
		0x4f, 0x2f, 0x6f, 0x25, 0x88, 0xa3, 0xef, 0xb9,
		0x6a, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
	}

	aliceHDSeed = chainhash.Hash{
		0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
		0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
		0x4f, 0x2f, 0x6f, 0x25, 0x18, 0xa3, 0xef, 0xb9,
		0x64, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
	}
	bobHDSeed = chainhash.Hash{
		0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
		0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
		0x4f, 0x2f, 0x6f, 0x25, 0x98, 0xa3, 0xef, 0xb9,
		0x69, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
	}

	netParams = &chaincfg.RegressionNetParams
	chainHash = netParams.GenesisHash

	_, alicePub = btcec.PrivKeyFromBytes(btcec.S256(), testHdSeed[:])
	_, bobPub   = btcec.PrivKeyFromBytes(btcec.S256(), bobsPrivKey)

	// The number of confirmations required to consider any created channel
	// open.
	numReqConfs uint16 = 1

	csvDelay uint16 = 4

	bobAddr, _   = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
	aliceAddr, _ = net.ResolveTCPAddr("tcp", "10.0.0.3:9000")
)

// assertProperBalance asserts than the total value of the unspent outputs
// within the wallet are *exactly* amount. If unable to retrieve the current
// balance, or the assertion fails, the test will halt with a fatal error.
func assertProperBalance(t *testing.T, lw *lnwallet.LightningWallet,
	numConfirms int32, amount float64) {

	balance, err := lw.ConfirmedBalance(numConfirms)
	if err != nil {
		t.Fatalf("unable to query for balance: %v", err)
	}
	if balance.ToBTC() != amount {
		t.Fatalf("wallet credits not properly loaded, should have 40BTC, "+
			"instead have %v", balance)
	}
}

func assertReservationDeleted(res *lnwallet.ChannelReservation, t *testing.T) {
	if err := res.Cancel(); err == nil {
		t.Fatalf("reservation wasn't deleted from wallet")
	}
}

// mineAndAssertTxInBlock asserts that a transaction is included within the next
// block mined.
func mineAndAssertTxInBlock(t *testing.T, miner *rpctest.Harness,
	txid chainhash.Hash) {

	t.Helper()

	// First, we'll wait for the transaction to arrive in the mempool.
	if err := waitForMempoolTx(miner, &txid); err != nil {
		t.Fatalf("unable to find %v in the mempool: %v", txid, err)
	}

	// We'll mined a block to confirm it.
	blockHashes, err := miner.Node.Generate(1)
	if err != nil {
		t.Fatalf("unable to generate new block: %v", err)
	}

	// Finally, we'll check it was actually mined in this block.
	block, err := miner.Node.GetBlock(blockHashes[0])
	if err != nil {
		t.Fatalf("unable to get block %v: %v", blockHashes[0], err)
	}
	if len(block.Transactions) != 2 {
		t.Fatalf("expected 2 transactions in block, found %d",
			len(block.Transactions))
	}
	txHash := block.Transactions[1].TxHash()
	if txHash != txid {
		t.Fatalf("expected transaction %v to be mined, found %v", txid,
			txHash)
	}
}

// newPkScript generates a new public key script of the given address type.
func newPkScript(t *testing.T, w *lnwallet.LightningWallet,
	addrType lnwallet.AddressType) []byte {

	t.Helper()

	addr, err := w.NewAddress(addrType, false)
	if err != nil {
		t.Fatalf("unable to create new address: %v", err)
	}
	pkScript, err := txscript.PayToAddrScript(addr)
	if err != nil {
		t.Fatalf("unable to create output script: %v", err)
	}

	return pkScript
}

// sendCoins is a helper function that encompasses all the things needed for two
// parties to send on-chain funds to each other.
func sendCoins(t *testing.T, miner *rpctest.Harness,
	sender, receiver *lnwallet.LightningWallet, output *wire.TxOut,
	feeRate chainfee.SatPerKWeight) *wire.MsgTx { //nolint:unparam

	t.Helper()

	tx, err := sender.SendOutputs([]*wire.TxOut{output}, 2500)
	if err != nil {
		t.Fatalf("unable to send transaction: %v", err)
	}

	mineAndAssertTxInBlock(t, miner, tx.TxHash())

	if err := waitForWalletSync(miner, sender); err != nil {
		t.Fatalf("unable to sync alice: %v", err)
	}
	if err := waitForWalletSync(miner, receiver); err != nil {
		t.Fatalf("unable to sync bob: %v", err)
	}

	return tx
}

// assertTxInWallet asserts that a transaction exists in the wallet with the
// expected confirmation status.
func assertTxInWallet(t *testing.T, w *lnwallet.LightningWallet,
	txHash chainhash.Hash, confirmed bool) {

	t.Helper()

	// If the backend is Neutrino, then we can't determine unconfirmed
	// transactions since it's not aware of the mempool.
	if !confirmed && w.BackEnd() == "neutrino" {
		return
	}

	// We'll fetch all of our transaction and go through each one until
	// finding the expected transaction with its expected confirmation
	// status.
	txs, err := w.ListTransactionDetails()
	if err != nil {
		t.Fatalf("unable to retrieve transactions: %v", err)
	}
	for _, tx := range txs {
		if tx.Hash != txHash {
			continue
		}
		if tx.NumConfirmations <= 0 && confirmed {
			t.Fatalf("expected transaction %v to be confirmed",
				txHash)
		}
		if tx.NumConfirmations > 0 && !confirmed {
			t.Fatalf("expected transaction %v to be unconfirmed",
				txHash)
		}

		// We've found the transaction and it matches the desired
		// confirmation status, so we can exit.
		return
	}

	t.Fatalf("transaction %v not found", txHash)
}

func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
	numOutputs int, btcPerOutput float64) error {

	// For initial neutrino connection, wait a second.
	// TODO(aakselrod): Eliminate the need for this.
	switch w.BackEnd() {
	case "neutrino":
		time.Sleep(time.Second)
	}
	// Using the mining node, spend from a coinbase output numOutputs to
	// give us btcPerOutput with each output.
	satoshiPerOutput, err := btcutil.NewAmount(btcPerOutput)
	if err != nil {
		return fmt.Errorf("unable to create amt: %v", err)
	}
	expectedBalance, err := w.ConfirmedBalance(1)
	if err != nil {
		return err
	}
	expectedBalance += btcutil.Amount(int64(satoshiPerOutput) * int64(numOutputs))
	addrs := make([]btcutil.Address, 0, numOutputs)
	for i := 0; i < numOutputs; i++ {
		// Grab a fresh address from the wallet to house this output.
		walletAddr, err := w.NewAddress(lnwallet.WitnessPubKey, false)
		if err != nil {
			return err
		}

		script, err := txscript.PayToAddrScript(walletAddr)
		if err != nil {
			return err
		}

		addrs = append(addrs, walletAddr)

		output := &wire.TxOut{
			Value:    int64(satoshiPerOutput),
			PkScript: script,
		}
		if _, err := miner.SendOutputs([]*wire.TxOut{output}, 2500); err != nil {
			return err
		}
	}

	// TODO(roasbeef): shouldn't hardcode 10, use config param that dictates
	// how many confs we wait before opening a channel.
	// Generate 10 blocks with the mining node, this should mine all
	// numOutputs transactions created above. We generate 10 blocks here
	// in order to give all the outputs a "sufficient" number of confirmations.
	if _, err := miner.Node.Generate(10); err != nil {
		return err
	}

	// Wait until the wallet has finished syncing up to the main chain.
	ticker := time.NewTicker(100 * time.Millisecond)
	timeout := time.After(30 * time.Second)

	for range ticker.C {
		balance, err := w.ConfirmedBalance(1)
		if err != nil {
			return err
		}
		if balance == expectedBalance {
			break
		}
		select {
		case <-timeout:
			synced, _, err := w.IsSynced()
			if err != nil {
				return err
			}
			return fmt.Errorf("timed out after 30 seconds "+
				"waiting for balance %v, current balance %v, "+
				"synced: %t", expectedBalance, balance, synced)
		default:
		}
	}
	ticker.Stop()

	return nil
}

// createTestWallet creates a test LightningWallet will a total of 20BTC
// available for funding channels.
func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
	netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier,
	wc lnwallet.WalletController, keyRing keychain.SecretKeyRing,
	signer input.Signer, bio lnwallet.BlockChainIO) (*lnwallet.LightningWallet, error) {

	dbDir := filepath.Join(tempTestDir, "cdb")
	cdb, err := channeldb.Open(dbDir)
	if err != nil {
		return nil, err
	}

	cfg := lnwallet.Config{
		Database:         cdb,
		Notifier:         notifier,
		SecretKeyRing:    keyRing,
		WalletController: wc,
		Signer:           signer,
		ChainIO:          bio,
		FeeEstimator:     chainfee.NewStaticEstimator(2500, 0),
		DefaultConstraints: channeldb.ChannelConstraints{
			DustLimit:        500,
			MaxPendingAmount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) * 100,
			ChanReserve:      100,
			MinHTLC:          400,
			MaxAcceptedHtlcs: 900,
		},
		NetParams: *netParams,
	}

	wallet, err := lnwallet.NewLightningWallet(cfg)
	if err != nil {
		return nil, err
	}

	if err := wallet.Startup(); err != nil {
		return nil, err
	}

	// Load our test wallet with 20 outputs each holding 4BTC.
	if err := loadTestCredits(miningNode, wallet, 20, 4); err != nil {
		return nil, err
	}

	return wallet, nil
}

func testDualFundingReservationWorkflow(miner *rpctest.Harness,
	alice, bob *lnwallet.LightningWallet, t *testing.T) {

	fundingAmount, err := btcutil.NewAmount(5)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}

	// In this scenario, we'll test a dual funder reservation, with each
	// side putting in 10 BTC.

	// Alice initiates a channel funded with 5 BTC for each side, so 10 BTC
	// total. She also generates 2 BTC in change.
	feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
	if err != nil {
		t.Fatalf("unable to query fee estimator: %v", err)
	}
	aliceReq := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  fundingAmount,
		RemoteFundingAmt: fundingAmount,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
	}
	aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
	if err != nil {
		t.Fatalf("unable to initialize funding reservation: %v", err)
	}
	aliceChanReservation.SetNumConfsRequired(numReqConfs)
	channelConstraints := &channeldb.ChannelConstraints{
		DustLimit:        lnwallet.DefaultDustLimit(),
		ChanReserve:      fundingAmount / 100,
		MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmount),
		MinHTLC:          1,
		MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
		CsvDelay:         csvDelay,
	}
	err = aliceChanReservation.CommitConstraints(channelConstraints)
	if err != nil {
		t.Fatalf("unable to verify constraints: %v", err)
	}

	// The channel reservation should now be populated with a multi-sig key
	// from our HD chain, a change output with 3 BTC, and 2 outputs
	// selected of 4 BTC each. Additionally, the rest of the items needed
	// to fulfill a funding contribution should also have been filled in.
	aliceContribution := aliceChanReservation.OurContribution()
	if len(aliceContribution.Inputs) != 2 {
		t.Fatalf("outputs for funding tx not properly selected, have %v "+
			"outputs should have 2", len(aliceContribution.Inputs))
	}
	assertContributionInitPopulated(t, aliceContribution)

	// Bob does the same, generating his own contribution. He then also
	// receives' Alice's contribution, and consumes that so we can continue
	// the funding process.
	bobReq := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           alicePub,
		NodeAddr:         aliceAddr,
		LocalFundingAmt:  fundingAmount,
		RemoteFundingAmt: fundingAmount,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
	}
	bobChanReservation, err := bob.InitChannelReservation(bobReq)
	if err != nil {
		t.Fatalf("bob unable to init channel reservation: %v", err)
	}
	err = bobChanReservation.CommitConstraints(channelConstraints)
	if err != nil {
		t.Fatalf("unable to verify constraints: %v", err)
	}
	bobChanReservation.SetNumConfsRequired(numReqConfs)

	assertContributionInitPopulated(t, bobChanReservation.OurContribution())

	err = bobChanReservation.ProcessContribution(aliceContribution)
	if err != nil {
		t.Fatalf("bob unable to process alice's contribution: %v", err)
	}
	assertContributionInitPopulated(t, bobChanReservation.TheirContribution())

	bobContribution := bobChanReservation.OurContribution()

	// Bob then sends over his contribution, which will be consumed by
	// Alice. After this phase, Alice should have all the necessary
	// material required to craft the funding transaction and commitment
	// transactions.
	err = aliceChanReservation.ProcessContribution(bobContribution)
	if err != nil {
		t.Fatalf("alice unable to process bob's contribution: %v", err)
	}
	assertContributionInitPopulated(t, aliceChanReservation.TheirContribution())

	// At this point, all Alice's signatures should be fully populated.
	aliceFundingSigs, aliceCommitSig := aliceChanReservation.OurSignatures()
	if aliceFundingSigs == nil {
		t.Fatalf("alice's funding signatures not populated")
	}
	if aliceCommitSig == nil {
		t.Fatalf("alice's commit signatures not populated")
	}

	// Additionally, Bob's signatures should also be fully populated.
	bobFundingSigs, bobCommitSig := bobChanReservation.OurSignatures()
	if bobFundingSigs == nil {
		t.Fatalf("bob's funding signatures not populated")
	}
	if bobCommitSig == nil {
		t.Fatalf("bob's commit signatures not populated")
	}

	// To conclude, we'll consume first Alice's signatures with Bob, and
	// then the other way around.
	_, err = aliceChanReservation.CompleteReservation(
		bobFundingSigs, bobCommitSig,
	)
	if err != nil {
		for _, in := range aliceChanReservation.FinalFundingTx().TxIn {
			fmt.Println(in.PreviousOutPoint.String())
		}
		t.Fatalf("unable to consume alice's sigs: %v", err)
	}
	_, err = bobChanReservation.CompleteReservation(
		aliceFundingSigs, aliceCommitSig,
	)
	if err != nil {
		t.Fatalf("unable to consume bob's sigs: %v", err)
	}

	// At this point, the funding tx should have been populated.
	fundingTx := aliceChanReservation.FinalFundingTx()
	if fundingTx == nil {
		t.Fatalf("funding transaction never created!")
	}

	// The resulting active channel state should have been persisted to the
	// DB.
	fundingSha := fundingTx.TxHash()
	aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
	if err != nil {
		t.Fatalf("unable to retrieve channel from DB: %v", err)
	}
	if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
		t.Fatalf("channel state not properly saved")
	}
	if !aliceChannels[0].ChanType.IsDualFunder() {
		t.Fatalf("channel not detected as dual funder")
	}
	bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
	if err != nil {
		t.Fatalf("unable to retrieve channel from DB: %v", err)
	}
	if !bytes.Equal(bobChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
		t.Fatalf("channel state not properly saved")
	}
	if !bobChannels[0].ChanType.IsDualFunder() {
		t.Fatalf("channel not detected as dual funder")
	}

	// Let Alice publish the funding transaction.
	if err := alice.PublishTransaction(fundingTx); err != nil {
		t.Fatalf("unable to publish funding tx: %v", err)
	}

	// Mine a single block, the funding transaction should be included
	// within this block.
	err = waitForMempoolTx(miner, &fundingSha)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}
	blockHashes, err := miner.Node.Generate(1)
	if err != nil {
		t.Fatalf("unable to generate block: %v", err)
	}
	block, err := miner.Node.GetBlock(blockHashes[0])
	if err != nil {
		t.Fatalf("unable to find block: %v", err)
	}
	if len(block.Transactions) != 2 {
		t.Fatalf("funding transaction wasn't mined: %v", err)
	}
	blockTx := block.Transactions[1]
	if blockTx.TxHash() != fundingSha {
		t.Fatalf("incorrect transaction was mined")
	}

	assertReservationDeleted(aliceChanReservation, t)
	assertReservationDeleted(bobChanReservation, t)

	// Wait for wallets to catch up to prevent issues in subsequent tests.
	err = waitForWalletSync(miner, alice)
	if err != nil {
		t.Fatalf("unable to sync alice: %v", err)
	}
	err = waitForWalletSync(miner, bob)
	if err != nil {
		t.Fatalf("unable to sync bob: %v", err)
	}
}

func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// Create a single channel asking for 16 BTC total.
	fundingAmount, err := btcutil.NewAmount(8)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}
	feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
	if err != nil {
		t.Fatalf("unable to query fee estimator: %v", err)
	}
	req := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  fundingAmount,
		RemoteFundingAmt: 0,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
		PendingChanID:    [32]byte{0, 1, 2, 3},
	}
	if _, err := alice.InitChannelReservation(req); err != nil {
		t.Fatalf("unable to initialize funding reservation 1: %v", err)
	}

	// Now attempt to reserve funds for another channel, this time
	// requesting 900 BTC. We only have around 64BTC worth of outpoints
	// that aren't locked, so this should fail.
	amt, err := btcutil.NewAmount(900)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}
	failedReq := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  amt,
		RemoteFundingAmt: 0,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
		PendingChanID:    [32]byte{1, 2, 3, 4},
	}
	failedReservation, err := alice.InitChannelReservation(failedReq)
	if err == nil {
		t.Fatalf("not error returned, should fail on coin selection")
	}
	if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
		t.Fatalf("error not coinselect error: %v", err)
	}
	if failedReservation != nil {
		t.Fatalf("reservation should be nil")
	}
}

func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
	if err != nil {
		t.Fatalf("unable to query fee estimator: %v", err)
	}

	// Create a reservation for 44 BTC.
	fundingAmount, err := btcutil.NewAmount(44)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}
	req := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  fundingAmount,
		RemoteFundingAmt: 0,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
		PendingChanID:    [32]byte{2, 3, 4, 5},
	}
	chanReservation, err := alice.InitChannelReservation(req)
	if err != nil {
		t.Fatalf("unable to initialize funding reservation: %v", err)
	}

	// Attempt to create another channel with 44 BTC, this should fail.
	req.PendingChanID = [32]byte{3, 4, 5, 6}
	_, err = alice.InitChannelReservation(req)
	if _, ok := err.(*chanfunding.ErrInsufficientFunds); !ok {
		t.Fatalf("coin selection succeeded should have insufficient funds: %v",
			err)
	}

	// Now cancel that old reservation.
	if err := chanReservation.Cancel(); err != nil {
		t.Fatalf("unable to cancel reservation: %v", err)
	}

	// Those outpoints should no longer be locked.
	lockedOutPoints := alice.LockedOutpoints()
	if len(lockedOutPoints) != 0 {
		t.Fatalf("outpoints still locked")
	}

	// Reservation ID should no longer be tracked.
	numReservations := alice.ActiveReservations()
	if len(alice.ActiveReservations()) != 0 {
		t.Fatalf("should have 0 reservations, instead have %v",
			numReservations)
	}

	// TODO(roasbeef): create method like Balance that ignores locked
	// outpoints, will let us fail early/fast instead of querying and
	// attempting coin selection.

	// Request to fund a new channel should now succeed.
	req.PendingChanID = [32]byte{4, 5, 6, 7, 8}
	if _, err := alice.InitChannelReservation(req); err != nil {
		t.Fatalf("unable to initialize funding reservation: %v", err)
	}
}

func testCancelNonExistentReservation(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
	if err != nil {
		t.Fatalf("unable to query fee estimator: %v", err)
	}

	// Create our own reservation, give it some ID.
	res, err := lnwallet.NewChannelReservation(
		10000, 10000, feePerKw, alice, 22, 10, &testHdSeed,
		lnwire.FFAnnounceChannel, lnwallet.CommitmentTypeTweakless,
		nil, [32]byte{}, 0,
	)
	if err != nil {
		t.Fatalf("unable to create res: %v", err)
	}

	// Attempt to cancel this reservation. This should fail, we know
	// nothing of it.
	if err := res.Cancel(); err == nil {
		t.Fatalf("canceled non-existent reservation")
	}
}

func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// We'll attempt to create a new reservation with an extremely high
	// commitment fee rate. This should push our balance into the negative
	// and result in a failure to create the reservation.
	const numBTC = 4
	fundingAmount, err := btcutil.NewAmount(numBTC)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}

	feePerKw := chainfee.SatPerKWeight(
		numBTC * numBTC * btcutil.SatoshiPerBitcoin,
	)
	req := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  fundingAmount,
		RemoteFundingAmt: 0,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  1000,
		PushMSat:         0,
		Flags:            lnwire.FFAnnounceChannel,
		CommitType:       lnwallet.CommitmentTypeTweakless,
	}
	_, err = alice.InitChannelReservation(req)
	switch {
	case err == nil:
		t.Fatalf("initialization should have failed due to " +
			"insufficient local amount")

	case !strings.Contains(err.Error(), "funder balance too small"):
		t.Fatalf("incorrect error: %v", err)
	}
}

func assertContributionInitPopulated(t *testing.T, c *lnwallet.ChannelContribution) {
	_, _, line, _ := runtime.Caller(1)

	if c.FirstCommitmentPoint == nil {
		t.Fatalf("line #%v: commitment point not fond", line)
	}

	if c.CsvDelay == 0 {
		t.Fatalf("line #%v: csv delay not set", line)
	}

	if c.MultiSigKey.PubKey == nil {
		t.Fatalf("line #%v: multi-sig key not set", line)
	}
	if c.RevocationBasePoint.PubKey == nil {
		t.Fatalf("line #%v: revocation key not set", line)
	}
	if c.PaymentBasePoint.PubKey == nil {
		t.Fatalf("line #%v: payment key not set", line)
	}
	if c.DelayBasePoint.PubKey == nil {
		t.Fatalf("line #%v: delay key not set", line)
	}

	if c.DustLimit == 0 {
		t.Fatalf("line #%v: dust limit not set", line)
	}
	if c.MaxPendingAmount == 0 {
		t.Fatalf("line #%v: max pending amt not set", line)
	}
	if c.ChanReserve == 0 {
		t.Fatalf("line #%v: chan reserve not set", line)
	}
	if c.MinHTLC == 0 {
		t.Fatalf("line #%v: min htlc not set", line)
	}
	if c.MaxAcceptedHtlcs == 0 {
		t.Fatalf("line #%v: max accepted htlc's not set", line)
	}
}

func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
	alice, bob *lnwallet.LightningWallet, t *testing.T,
	commitType lnwallet.CommitmentType,
	aliceChanFunder chanfunding.Assembler, fetchFundingTx func() *wire.MsgTx,
	pendingChanID [32]byte, thawHeight uint32) {

	// For this scenario, Alice will be the channel initiator while bob
	// will act as the responder to the workflow.

	// First, Alice will Initialize a reservation for a channel with 4 BTC
	// funded solely by us. We'll also initially push 1 BTC of the channel
	// towards Bob's side.
	fundingAmt, err := btcutil.NewAmount(4)
	if err != nil {
		t.Fatalf("unable to create amt: %v", err)
	}
	pushAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
	feePerKw, err := alice.Cfg.FeeEstimator.EstimateFeePerKW(1)
	if err != nil {
		t.Fatalf("unable to query fee estimator: %v", err)
	}
	aliceReq := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		PendingChanID:    pendingChanID,
		NodeID:           bobPub,
		NodeAddr:         bobAddr,
		LocalFundingAmt:  fundingAmt,
		RemoteFundingAmt: 0,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         pushAmt,
		Flags:            lnwire.FFAnnounceChannel,
		CommitType:       commitType,
		ChanFunder:       aliceChanFunder,
	}
	aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
	if err != nil {
		t.Fatalf("unable to init channel reservation: %v", err)
	}
	aliceChanReservation.SetNumConfsRequired(numReqConfs)
	channelConstraints := &channeldb.ChannelConstraints{
		DustLimit:        lnwallet.DefaultDustLimit(),
		ChanReserve:      fundingAmt / 100,
		MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmt),
		MinHTLC:          1,
		MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
		CsvDelay:         csvDelay,
	}
	err = aliceChanReservation.CommitConstraints(channelConstraints)
	if err != nil {
		t.Fatalf("unable to verify constraints: %v", err)
	}

	// Verify all contribution fields have been set properly, but only if
	// Alice is the funder herself.
	aliceContribution := aliceChanReservation.OurContribution()
	if fetchFundingTx == nil {
		if len(aliceContribution.Inputs) < 1 {
			t.Fatalf("outputs for funding tx not properly "+
				"selected, have %v outputs should at least 1",
				len(aliceContribution.Inputs))
		}
		if len(aliceContribution.ChangeOutputs) != 1 {
			t.Fatalf("coin selection failed, should have one "+
				"change outputs, instead have: %v",
				len(aliceContribution.ChangeOutputs))
		}
	}
	assertContributionInitPopulated(t, aliceContribution)

	// Next, Bob receives the initial request, generates a corresponding
	// reservation initiation, then consume Alice's contribution.
	bobReq := &lnwallet.InitFundingReserveMsg{
		ChainHash:        chainHash,
		PendingChanID:    pendingChanID,
		NodeID:           alicePub,
		NodeAddr:         aliceAddr,
		LocalFundingAmt:  0,
		RemoteFundingAmt: fundingAmt,
		CommitFeePerKw:   feePerKw,
		FundingFeePerKw:  feePerKw,
		PushMSat:         pushAmt,
		Flags:            lnwire.FFAnnounceChannel,
		CommitType:       commitType,
	}
	bobChanReservation, err := bob.InitChannelReservation(bobReq)
	if err != nil {
		t.Fatalf("unable to create bob reservation: %v", err)
	}
	err = bobChanReservation.CommitConstraints(channelConstraints)
	if err != nil {
		t.Fatalf("unable to verify constraints: %v", err)
	}
	bobChanReservation.SetNumConfsRequired(numReqConfs)

	// We'll ensure that Bob's contribution also gets generated properly.
	bobContribution := bobChanReservation.OurContribution()
	assertContributionInitPopulated(t, bobContribution)

	// With his contribution generated, he can now process Alice's
	// contribution.
	err = bobChanReservation.ProcessSingleContribution(aliceContribution)
	if err != nil {
		t.Fatalf("bob unable to process alice's contribution: %v", err)
	}
	assertContributionInitPopulated(t, bobChanReservation.TheirContribution())

	// Bob will next send over his contribution to Alice, we simulate this
	// by having Alice immediately process his contribution.
	err = aliceChanReservation.ProcessContribution(bobContribution)
	if err != nil {
		t.Fatalf("alice unable to process bob's contribution")
	}
	assertContributionInitPopulated(t, bobChanReservation.TheirContribution())

	// At this point, Alice should have generated all the signatures
	// required for the funding transaction, as well as Alice's commitment
	// signature to bob, but only if the funding transaction was
	// constructed internally.
	aliceRemoteContribution := aliceChanReservation.TheirContribution()
	aliceFundingSigs, aliceCommitSig := aliceChanReservation.OurSignatures()
	if fetchFundingTx == nil && aliceFundingSigs == nil {
		t.Fatalf("funding sigs not found")
	}
	if aliceCommitSig == nil {
		t.Fatalf("commitment sig not found")
	}

	// Additionally, the funding tx and the funding outpoint should have
	// been populated.
	if aliceChanReservation.FinalFundingTx() == nil && fetchFundingTx == nil {
		t.Fatalf("funding transaction never created!")
	}
	if aliceChanReservation.FundingOutpoint() == nil {
		t.Fatalf("funding outpoint never created!")
	}

	// Their funds should also be filled in.
	if len(aliceRemoteContribution.Inputs) != 0 {
		t.Fatalf("bob shouldn't have any inputs, instead has %v",
			len(aliceRemoteContribution.Inputs))
	}
	if len(aliceRemoteContribution.ChangeOutputs) != 0 {
		t.Fatalf("bob shouldn't have any change outputs, instead "+
			"has %v",
			aliceRemoteContribution.ChangeOutputs[0].Value)
	}

	// Next, Alice will send over her signature for Bob's commitment
	// transaction, as well as the funding outpoint.
	fundingPoint := aliceChanReservation.FundingOutpoint()
	_, err = bobChanReservation.CompleteReservationSingle(
		fundingPoint, aliceCommitSig,
	)
	if err != nil {
		t.Fatalf("bob unable to consume single reservation: %v", err)
	}

	// Finally, we'll conclude the reservation process by sending over
	// Bob's commitment signature, which is the final thing Alice needs to
	// be able to safely broadcast the funding transaction.
	_, bobCommitSig := bobChanReservation.OurSignatures()
	if bobCommitSig == nil {
		t.Fatalf("bob failed to generate commitment signature: %v", err)
	}
	_, err = aliceChanReservation.CompleteReservation(
		nil, bobCommitSig,
	)
	if err != nil {
		t.Fatalf("alice unable to complete reservation: %v", err)
	}

	// If the caller provided an alternative way to obtain the funding tx,
	// then we'll use that. Otherwise, we'll obtain it directly from Alice.
	var fundingTx *wire.MsgTx
	if fetchFundingTx != nil {
		fundingTx = fetchFundingTx()
	} else {
		fundingTx = aliceChanReservation.FinalFundingTx()
	}

	// The resulting active channel state should have been persisted to the
	// DB for both Alice and Bob.
	fundingSha := fundingTx.TxHash()
	aliceChannels, err := alice.Cfg.Database.FetchOpenChannels(bobPub)
	if err != nil {
		t.Fatalf("unable to retrieve channel from DB: %v", err)
	}
	if len(aliceChannels) != 1 {
		t.Fatalf("alice didn't save channel state: %v", err)
	}
	if !bytes.Equal(aliceChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
		t.Fatalf("channel state not properly saved: %v vs %v",
			hex.EncodeToString(aliceChannels[0].FundingOutpoint.Hash[:]),
			hex.EncodeToString(fundingSha[:]))
	}
	if !aliceChannels[0].IsInitiator {
		t.Fatalf("alice not detected as channel initiator")
	}
	if !aliceChannels[0].ChanType.IsSingleFunder() {
		t.Fatalf("channel type is incorrect, expected %v instead got %v",
			channeldb.SingleFunderBit, aliceChannels[0].ChanType)
	}

	bobChannels, err := bob.Cfg.Database.FetchOpenChannels(alicePub)
	if err != nil {
		t.Fatalf("unable to retrieve channel from DB: %v", err)
	}
	if len(bobChannels) != 1 {
		t.Fatalf("bob didn't save channel state: %v", err)
	}
	if !bytes.Equal(bobChannels[0].FundingOutpoint.Hash[:], fundingSha[:]) {
		t.Fatalf("channel state not properly saved: %v vs %v",
			hex.EncodeToString(bobChannels[0].FundingOutpoint.Hash[:]),
			hex.EncodeToString(fundingSha[:]))
	}
	if bobChannels[0].IsInitiator {
		t.Fatalf("bob not detected as channel responder")
	}
	if !bobChannels[0].ChanType.IsSingleFunder() {
		t.Fatalf("channel type is incorrect, expected %v instead got %v",
			channeldb.SingleFunderBit, bobChannels[0].ChanType)
	}

	// Let Alice publish the funding transaction.
	if err := alice.PublishTransaction(fundingTx); err != nil {
		t.Fatalf("unable to publish funding tx: %v", err)
	}

	// Mine a single block, the funding transaction should be included
	// within this block.
	err = waitForMempoolTx(miner, &fundingSha)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}
	blockHashes, err := miner.Node.Generate(1)
	if err != nil {
		t.Fatalf("unable to generate block: %v", err)
	}
	block, err := miner.Node.GetBlock(blockHashes[0])
	if err != nil {
		t.Fatalf("unable to find block: %v", err)
	}
	if len(block.Transactions) != 2 {
		t.Fatalf("funding transaction wasn't mined: %d",
			len(block.Transactions))
	}
	blockTx := block.Transactions[1]
	if blockTx.TxHash() != fundingSha {
		t.Fatalf("incorrect transaction was mined")
	}

	// If a frozen channel was requested, then we expect that both channel
	// types show as being a frozen channel type.
	aliceChanFrozen := aliceChannels[0].ChanType.IsFrozen()
	bobChanFrozen := bobChannels[0].ChanType.IsFrozen()
	if thawHeight != 0 && (!aliceChanFrozen || !bobChanFrozen) {
		t.Fatalf("expected both alice and bob to have frozen chans: "+
			"alice_frozen=%v, bob_frozen=%v", aliceChanFrozen,
			bobChanFrozen)
	}
	if thawHeight != bobChannels[0].ThawHeight {
		t.Fatalf("wrong thaw height: expected %v got %v", thawHeight,
			bobChannels[0].ThawHeight)
	}
	if thawHeight != aliceChannels[0].ThawHeight {
		t.Fatalf("wrong thaw height: expected %v got %v", thawHeight,
			aliceChannels[0].ThawHeight)
	}

	assertReservationDeleted(aliceChanReservation, t)
	assertReservationDeleted(bobChanReservation, t)
}

func testListTransactionDetails(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// Create 5 new outputs spendable by the wallet.
	const numTxns = 5
	const outputAmt = btcutil.SatoshiPerBitcoin
	txids := make(map[chainhash.Hash]struct{})
	for i := 0; i < numTxns; i++ {
		addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
		if err != nil {
			t.Fatalf("unable to create new address: %v", err)
		}
		script, err := txscript.PayToAddrScript(addr)
		if err != nil {
			t.Fatalf("unable to create output script: %v", err)
		}

		output := &wire.TxOut{
			Value:    outputAmt,
			PkScript: script,
		}
		txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
		if err != nil {
			t.Fatalf("unable to send coinbase: %v", err)
		}
		txids[*txid] = struct{}{}
	}

	// Generate 10 blocks to mine all the transactions created above.
	const numBlocksMined = 10
	blocks, err := miner.Node.Generate(numBlocksMined)
	if err != nil {
		t.Fatalf("unable to mine blocks: %v", err)
	}

	// Next, fetch all the current transaction details.
	err = waitForWalletSync(miner, alice)
	if err != nil {
		t.Fatalf("Couldn't sync Alice's wallet: %v", err)
	}
	txDetails, err := alice.ListTransactionDetails()
	if err != nil {
		t.Fatalf("unable to fetch tx details: %v", err)
	}

	// This is a mapping from:
	// blockHash -> transactionHash -> transactionOutputs
	blockTxOuts := make(map[chainhash.Hash]map[chainhash.Hash][]*wire.TxOut)

	// Each of the transactions created above should be found with the
	// proper details populated.
	for _, txDetail := range txDetails {
		if _, ok := txids[txDetail.Hash]; !ok {
			continue
		}

		if txDetail.NumConfirmations != numBlocksMined {
			t.Fatalf("num confs incorrect, got %v expected %v",
				txDetail.NumConfirmations, numBlocksMined)
		}
		if txDetail.Value != outputAmt {
			t.Fatalf("tx value incorrect, got %v expected %v",
				txDetail.Value, outputAmt)
		}

		if !bytes.Equal(txDetail.BlockHash[:], blocks[0][:]) {
			t.Fatalf("block hash mismatch, got %v expected %v",
				txDetail.BlockHash, blocks[0])
		}

		// This fetches the transactions in a block so that we can compare the
		// txouts stored in the mined transaction against the ones in the transaction
		// details
		if _, ok := blockTxOuts[*txDetail.BlockHash]; !ok {
			fetchedBlock, err := alice.Cfg.ChainIO.GetBlock(txDetail.BlockHash)
			if err != nil {
				t.Fatalf("err fetching block: %s", err)
			}

			transactions :=
				make(map[chainhash.Hash][]*wire.TxOut, len(fetchedBlock.Transactions))
			for _, tx := range fetchedBlock.Transactions {
				transactions[tx.TxHash()] = tx.TxOut
			}

			blockTxOuts[fetchedBlock.BlockHash()] = transactions
		}

		if txOuts, ok := blockTxOuts[*txDetail.BlockHash][txDetail.Hash]; !ok {
			t.Fatalf("tx (%v) not found in block (%v)",
				txDetail.Hash, txDetail.BlockHash)
		} else {
			var destinationAddresses []btcutil.Address

			for _, txOut := range txOuts {
				_, addrs, _, err :=
					txscript.ExtractPkScriptAddrs(txOut.PkScript, &alice.Cfg.NetParams)
				if err != nil {
					t.Fatalf("err extract script addresses: %s", err)
				}
				destinationAddresses = append(destinationAddresses, addrs...)
			}

			if !reflect.DeepEqual(txDetail.DestAddresses, destinationAddresses) {
				t.Fatalf("destination addresses mismatch, got %v expected %v",
					txDetail.DestAddresses, destinationAddresses)
			}
		}

		delete(txids, txDetail.Hash)
	}
	if len(txids) != 0 {
		t.Fatalf("all transactions not found in details: left=%v, "+
			"returned_set=%v", spew.Sdump(txids),
			spew.Sdump(txDetails))
	}

	// Next create a transaction paying to an output which isn't under the
	// wallet's control.
	minerAddr, err := miner.NewAddress()
	if err != nil {
		t.Fatalf("unable to generate address: %v", err)
	}
	outputScript, err := txscript.PayToAddrScript(minerAddr)
	if err != nil {
		t.Fatalf("unable to make output script: %v", err)
	}
	burnOutput := wire.NewTxOut(outputAmt, outputScript)
	burnTX, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
	if err != nil {
		t.Fatalf("unable to create burn tx: %v", err)
	}
	burnTXID := burnTX.TxHash()
	err = waitForMempoolTx(miner, &burnTXID)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}

	// Before we mine the next block, we'll ensure that the above
	// transaction shows up in the set of unconfirmed transactions returned
	// by ListTransactionDetails.
	err = waitForWalletSync(miner, alice)
	if err != nil {
		t.Fatalf("Couldn't sync Alice's wallet: %v", err)
	}

	// We should be able to find the transaction above in the set of
	// returned transactions, and it should have a confirmation of -1,
	// indicating that it's not yet mined.
	txDetails, err = alice.ListTransactionDetails()
	if err != nil {
		t.Fatalf("unable to fetch tx details: %v", err)
	}
	var mempoolTxFound bool
	for _, txDetail := range txDetails {
		if !bytes.Equal(txDetail.Hash[:], burnTXID[:]) {
			continue
		}

		// Now that we've found the transaction, ensure that it has a
		// negative number of confirmations to indicate that it's
		// unconfirmed.
		mempoolTxFound = true
		if txDetail.NumConfirmations != 0 {
			t.Fatalf("num confs incorrect, got %v expected %v",
				txDetail.NumConfirmations, 0)
		}

		// We test that each txDetail has destination addresses. This ensures
		// that even when we have 0 confirmation transactions, the destination
		// addresses are returned.
		var match bool
		for _, addr := range txDetail.DestAddresses {
			if addr.String() == minerAddr.String() {
				match = true
				break
			}
		}
		if !match {
			t.Fatalf("minerAddr: %v should have been a dest addr", minerAddr)
		}
	}
	if !mempoolTxFound {
		t.Fatalf("unable to find mempool tx in tx details!")
	}

	burnBlock, err := miner.Node.Generate(1)
	if err != nil {
		t.Fatalf("unable to mine block: %v", err)
	}

	// Fetch the transaction details again, the new transaction should be
	// shown as debiting from the wallet's balance.
	err = waitForWalletSync(miner, alice)
	if err != nil {
		t.Fatalf("Couldn't sync Alice's wallet: %v", err)
	}
	txDetails, err = alice.ListTransactionDetails()
	if err != nil {
		t.Fatalf("unable to fetch tx details: %v", err)
	}
	var burnTxFound bool
	for _, txDetail := range txDetails {
		if !bytes.Equal(txDetail.Hash[:], burnTXID[:]) {
			continue
		}

		burnTxFound = true
		if txDetail.NumConfirmations != 1 {
			t.Fatalf("num confs incorrect, got %v expected %v",
				txDetail.NumConfirmations, 1)
		}

		// We assert that the value is greater than the amount we
		// attempted to send, as the wallet should have paid some amount
		// of network fees.
		if txDetail.Value >= -outputAmt {
			fmt.Println(spew.Sdump(txDetail))
			t.Fatalf("tx value incorrect, got %v expected %v",
				int64(txDetail.Value), -int64(outputAmt))
		}
		if !bytes.Equal(txDetail.BlockHash[:], burnBlock[0][:]) {
			t.Fatalf("block hash mismatch, got %v expected %v",
				txDetail.BlockHash, burnBlock[0])
		}
	}
	if !burnTxFound {
		t.Fatal("tx burning btc not found")
	}
}

func testTransactionSubscriptions(miner *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// First, check to see if this wallet meets the TransactionNotifier
	// interface, if not then we'll skip this test for this particular
	// implementation of the WalletController.
	txClient, err := alice.SubscribeTransactions()
	if err != nil {
		t.Skipf("unable to generate tx subscription: %v", err)
	}
	defer txClient.Cancel()

	const (
		outputAmt = btcutil.SatoshiPerBitcoin
		numTxns   = 3
	)
	errCh1 := make(chan error, 1)
	switch alice.BackEnd() {
	case "neutrino":
		// Neutrino doesn't listen for unconfirmed transactions.
	default:
		go func() {
			for i := 0; i < numTxns; i++ {
				txDetail := <-txClient.UnconfirmedTransactions()
				if txDetail.NumConfirmations != 0 {
					errCh1 <- fmt.Errorf("incorrect number of confs, "+
						"expected %v got %v", 0,
						txDetail.NumConfirmations)
					return
				}
				if txDetail.Value != outputAmt {
					errCh1 <- fmt.Errorf("incorrect output amt, "+
						"expected %v got %v", outputAmt,
						txDetail.Value)
					return
				}
				if txDetail.BlockHash != nil {
					errCh1 <- fmt.Errorf("block hash should be nil, "+
						"is instead %v",
						txDetail.BlockHash)
					return
				}
			}
			errCh1 <- nil
		}()
	}

	// Next, fetch a fresh address from the wallet, create 3 new outputs
	// with the pkScript.
	for i := 0; i < numTxns; i++ {
		addr, err := alice.NewAddress(lnwallet.WitnessPubKey, false)
		if err != nil {
			t.Fatalf("unable to create new address: %v", err)
		}
		script, err := txscript.PayToAddrScript(addr)
		if err != nil {
			t.Fatalf("unable to create output script: %v", err)
		}

		output := &wire.TxOut{
			Value:    outputAmt,
			PkScript: script,
		}
		txid, err := miner.SendOutputs([]*wire.TxOut{output}, 2500)
		if err != nil {
			t.Fatalf("unable to send coinbase: %v", err)
		}
		err = waitForMempoolTx(miner, txid)
		if err != nil {
			t.Fatalf("tx not relayed to miner: %v", err)
		}
	}

	switch alice.BackEnd() {
	case "neutrino":
		// Neutrino doesn't listen for on unconfirmed transactions.
	default:
		// We should receive a notification for all three transactions
		// generated above.
		select {
		case <-time.After(time.Second * 10):
			t.Fatalf("transactions not received after 10 seconds")
		case err := <-errCh1:
			if err != nil {
				t.Fatal(err)
			}
		}
	}

	errCh2 := make(chan error, 1)
	go func() {
		for i := 0; i < numTxns; i++ {
			txDetail := <-txClient.ConfirmedTransactions()
			if txDetail.NumConfirmations != 1 {
				errCh2 <- fmt.Errorf("incorrect number of confs for %s, expected %v got %v",
					txDetail.Hash, 1, txDetail.NumConfirmations)
				return
			}
			if txDetail.Value != outputAmt {
				errCh2 <- fmt.Errorf("incorrect output amt, expected %v got %v in txid %s",
					outputAmt, txDetail.Value, txDetail.Hash)
				return
			}
		}
		errCh2 <- nil
	}()

	// Next mine a single block, all the transactions generated above
	// should be included.
	if _, err := miner.Node.Generate(1); err != nil {
		t.Fatalf("unable to generate block: %v", err)
	}

	// We should receive a notification for all three transactions
	// since they should be mined in the next block.
	select {
	case <-time.After(time.Second * 5):
		t.Fatalf("transactions not received after 5 seconds")
	case err := <-errCh2:
		if err != nil {
			t.Fatal(err)
		}
	}

	// We'll also ensure that the client is able to send our new
	// notifications when we _create_ transactions ourselves that spend our
	// own outputs.
	b := txscript.NewScriptBuilder()
	b.AddOp(txscript.OP_RETURN)
	outputScript, err := b.Script()
	if err != nil {
		t.Fatalf("unable to make output script: %v", err)
	}
	burnOutput := wire.NewTxOut(outputAmt, outputScript)
	tx, err := alice.SendOutputs([]*wire.TxOut{burnOutput}, 2500)
	if err != nil {
		t.Fatalf("unable to create burn tx: %v", err)
	}
	txid := tx.TxHash()
	err = waitForMempoolTx(miner, &txid)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}

	// Before we mine the next block, we'll ensure that the above
	// transaction shows up in the set of unconfirmed transactions returned
	// by ListTransactionDetails.
	err = waitForWalletSync(miner, alice)
	if err != nil {
		t.Fatalf("Couldn't sync Alice's wallet: %v", err)
	}

	// As we just sent the transaction and it was landed in the mempool, we
	// should get a notification for a new unconfirmed transactions
	select {
	case <-time.After(time.Second * 10):
		t.Fatalf("transactions not received after 10 seconds")
	case unConfTx := <-txClient.UnconfirmedTransactions():
		if unConfTx.Hash != txid {
			t.Fatalf("wrong txn notified: expected %v got %v",
				txid, unConfTx.Hash)
		}
	}
}

// scriptFromKey creates a P2WKH script from the given pubkey.
func scriptFromKey(pubkey *btcec.PublicKey) ([]byte, error) {
	pubkeyHash := btcutil.Hash160(pubkey.SerializeCompressed())
	keyAddr, err := btcutil.NewAddressWitnessPubKeyHash(
		pubkeyHash, &chaincfg.RegressionNetParams,
	)
	if err != nil {
		return nil, fmt.Errorf("unable to create addr: %v", err)
	}
	keyScript, err := txscript.PayToAddrScript(keyAddr)
	if err != nil {
		return nil, fmt.Errorf("unable to generate script: %v", err)
	}

	return keyScript, nil
}

// mineAndAssert mines a block and ensures the passed TX is part of that block.
func mineAndAssert(r *rpctest.Harness, tx *wire.MsgTx) error {
	txid := tx.TxHash()
	err := waitForMempoolTx(r, &txid)
	if err != nil {
		return fmt.Errorf("tx not relayed to miner: %v", err)
	}

	blockHashes, err := r.Node.Generate(1)
	if err != nil {
		return fmt.Errorf("unable to generate block: %v", err)
	}

	block, err := r.Node.GetBlock(blockHashes[0])
	if err != nil {
		return fmt.Errorf("unable to find block: %v", err)
	}

	if len(block.Transactions) != 2 {
		return fmt.Errorf("expected 2 txs in block, got %d",
			len(block.Transactions))
	}

	blockTx := block.Transactions[1]
	if blockTx.TxHash() != tx.TxHash() {
		return fmt.Errorf("incorrect transaction was mined")
	}

	// Sleep for a second before returning, to make sure the block has
	// propagated.
	time.Sleep(1 * time.Second)
	return nil
}

// txFromOutput takes a tx paying to fromPubKey, and creates a new tx that
// spends the output from this tx, to an address derived from payToPubKey.
func txFromOutput(tx *wire.MsgTx, signer input.Signer, fromPubKey,
	payToPubKey *btcec.PublicKey, txFee btcutil.Amount,
	rbf bool) (*wire.MsgTx, error) {

	// Generate the script we want to spend from.
	keyScript, err := scriptFromKey(fromPubKey)
	if err != nil {
		return nil, fmt.Errorf("unable to generate script: %v", err)
	}

	// We assume the output was paid to the keyScript made earlier.
	var outputIndex uint32
	if len(tx.TxOut) == 1 || bytes.Equal(tx.TxOut[0].PkScript, keyScript) {
		outputIndex = 0
	} else {
		outputIndex = 1
	}
	outputValue := tx.TxOut[outputIndex].Value

	// With the index located, we can create a transaction spending the
	// referenced output.
	tx1 := wire.NewMsgTx(2)

	// If we want to create a tx that signals replacement, set its
	// sequence number to the max one that signals replacement.
	// Otherwise we just use the standard max sequence.
	sequence := wire.MaxTxInSequenceNum
	if rbf {
		sequence = mempool.MaxRBFSequence
	}

	tx1.AddTxIn(&wire.TxIn{
		PreviousOutPoint: wire.OutPoint{
			Hash:  tx.TxHash(),
			Index: outputIndex,
		},
		Sequence: sequence,
	})

	// Create a script to pay to.
	payToScript, err := scriptFromKey(payToPubKey)
	if err != nil {
		return nil, fmt.Errorf("unable to generate script: %v", err)
	}
	tx1.AddTxOut(&wire.TxOut{
		Value:    outputValue - int64(txFee),
		PkScript: payToScript,
	})

	// Now we can populate the sign descriptor which we'll use to generate
	// the signature.
	signDesc := &input.SignDescriptor{
		KeyDesc: keychain.KeyDescriptor{
			PubKey: fromPubKey,
		},
		WitnessScript: keyScript,
		Output:        tx.TxOut[outputIndex],
		HashType:      txscript.SigHashAll,
		SigHashes:     txscript.NewTxSigHashes(tx1),
		InputIndex:    0, // Has only one input.
	}

	// With the descriptor created, we use it to generate a signature, then
	// manually create a valid witness stack we'll use for signing.
	spendSig, err := signer.SignOutputRaw(tx1, signDesc)
	if err != nil {
		return nil, fmt.Errorf("unable to generate signature: %v", err)
	}
	witness := make([][]byte, 2)
	witness[0] = append(spendSig.Serialize(), byte(txscript.SigHashAll))
	witness[1] = fromPubKey.SerializeCompressed()
	tx1.TxIn[0].Witness = witness

	// Finally, attempt to validate the completed transaction. This should
	// succeed if the wallet was able to properly generate the proper
	// private key.
	vm, err := txscript.NewEngine(
		keyScript, tx1, 0, txscript.StandardVerifyFlags, nil,
		nil, outputValue,
	)
	if err != nil {
		return nil, fmt.Errorf("unable to create engine: %v", err)
	}
	if err := vm.Execute(); err != nil {
		return nil, fmt.Errorf("spend is invalid: %v", err)
	}

	return tx1, nil
}

// newTx sends coins from Alice's wallet, mines this transaction, and creates a
// new, unconfirmed tx that spends this output to pubKey.
func newTx(t *testing.T, r *rpctest.Harness, pubKey *btcec.PublicKey,
	alice *lnwallet.LightningWallet, rbf bool) *wire.MsgTx {
	t.Helper()

	keyScript, err := scriptFromKey(pubKey)
	if err != nil {
		t.Fatalf("unable to generate script: %v", err)
	}

	// Instruct the wallet to fund the output with a newly created
	// transaction.
	newOutput := &wire.TxOut{
		Value:    btcutil.SatoshiPerBitcoin,
		PkScript: keyScript,
	}
	tx, err := alice.SendOutputs([]*wire.TxOut{newOutput}, 2500)
	if err != nil {
		t.Fatalf("unable to create output: %v", err)
	}

	// Query for the transaction generated above so we can located the
	// index of our output.
	if err := mineAndAssert(r, tx); err != nil {
		t.Fatalf("unable to mine tx: %v", err)
	}

	// Create a new unconfirmed tx that spends this output.
	txFee := btcutil.Amount(0.001 * btcutil.SatoshiPerBitcoin)
	tx1, err := txFromOutput(
		tx, alice.Cfg.Signer, pubKey, pubKey, txFee, rbf,
	)
	if err != nil {
		t.Fatal(err)
	}

	return tx1
}

// testPublishTransaction checks that PublishTransaction returns the expected
// error types in case the transaction being published conflicts with the
// current mempool or chain.
func testPublishTransaction(r *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// Generate a pubkey, and pay-to-addr script.
	keyDesc, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
	if err != nil {
		t.Fatalf("unable to obtain public key: %v", err)
	}

	// We will first check that publishing a transaction already in the
	// mempool does NOT return an error. Create the tx.
	tx1 := newTx(t, r, keyDesc.PubKey, alice, false)

	// Publish the transaction.
	if err := alice.PublishTransaction(tx1); err != nil {
		t.Fatalf("unable to publish: %v", err)
	}

	txid1 := tx1.TxHash()
	err = waitForMempoolTx(r, &txid1)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}

	// Publish the exact same transaction again. This should not return an
	// error, even though the transaction is already in the mempool.
	if err := alice.PublishTransaction(tx1); err != nil {
		t.Fatalf("unable to publish: %v", err)
	}

	// Mine the transaction.
	if _, err := r.Node.Generate(1); err != nil {
		t.Fatalf("unable to generate block: %v", err)
	}

	// We'll now test that we don't get an error if we try to publish a
	// transaction that is already mined.
	//
	// Create a new transaction. We must do this to properly test the
	// reject messages from our peers. They might only send us a reject
	// message for a given tx once, so we create a new to make sure it is
	// not just immediately rejected.
	tx2 := newTx(t, r, keyDesc.PubKey, alice, false)

	// Publish this tx.
	if err := alice.PublishTransaction(tx2); err != nil {
		t.Fatalf("unable to publish: %v", err)
	}

	// Mine the transaction.
	if err := mineAndAssert(r, tx2); err != nil {
		t.Fatalf("unable to mine tx: %v", err)
	}

	// Publish the transaction again. It is already mined, and we don't
	// expect this to return an error.
	if err := alice.PublishTransaction(tx2); err != nil {
		t.Fatalf("unable to publish: %v", err)
	}

	// We'll do the next mempool check on both RBF and non-RBF enabled
	// transactions.
	var (
		txFee         = btcutil.Amount(0.005 * btcutil.SatoshiPerBitcoin)
		tx3, tx3Spend *wire.MsgTx
	)

	for _, rbf := range []bool{false, true} {
		// Now we'll try to double spend an output with a different
		// transaction. Create a new tx and publish it. This is the
		// output we'll try to double spend.
		tx3 = newTx(t, r, keyDesc.PubKey, alice, false)
		if err := alice.PublishTransaction(tx3); err != nil {
			t.Fatalf("unable to publish: %v", err)
		}

		// Mine the transaction.
		if err := mineAndAssert(r, tx3); err != nil {
			t.Fatalf("unable to mine tx: %v", err)
		}

		// Now we create a transaction that spends the output from the
		// tx just mined.
		tx4, err := txFromOutput(
			tx3, alice.Cfg.Signer, keyDesc.PubKey,
			keyDesc.PubKey, txFee, rbf,
		)
		if err != nil {
			t.Fatal(err)
		}

		// This should be accepted into the mempool.
		if err := alice.PublishTransaction(tx4); err != nil {
			t.Fatalf("unable to publish: %v", err)
		}

		// Keep track of the last successfully published tx to spend
		// tx3.
		tx3Spend = tx4

		txid4 := tx4.TxHash()
		err = waitForMempoolTx(r, &txid4)
		if err != nil {
			t.Fatalf("tx not relayed to miner: %v", err)
		}

		// Create a new key we'll pay to, to ensure we create a unique
		// transaction.
		keyDesc2, err := alice.DeriveNextKey(
			keychain.KeyFamilyMultiSig,
		)
		if err != nil {
			t.Fatalf("unable to obtain public key: %v", err)
		}

		// Create a new transaction that spends the output from tx3,
		// and that pays to a different address. We expect this to be
		// rejected because it is a double spend.
		tx5, err := txFromOutput(
			tx3, alice.Cfg.Signer, keyDesc.PubKey,
			keyDesc2.PubKey, txFee, rbf,
		)
		if err != nil {
			t.Fatal(err)
		}

		err = alice.PublishTransaction(tx5)
		if err != lnwallet.ErrDoubleSpend {
			t.Fatalf("expected ErrDoubleSpend, got: %v", err)
		}

		// Create another transaction that spends the same output, but
		// has a higher fee. We expect also this tx to be rejected for
		// non-RBF enabled transactions, while it should succeed
		// otherwise.
		pubKey3, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
		if err != nil {
			t.Fatalf("unable to obtain public key: %v", err)
		}
		tx6, err := txFromOutput(
			tx3, alice.Cfg.Signer, keyDesc.PubKey,
			pubKey3.PubKey, 2*txFee, rbf,
		)
		if err != nil {
			t.Fatal(err)
		}

		// Expect rejection in non-RBF case.
		expErr := lnwallet.ErrDoubleSpend
		if rbf {
			// Expect success in rbf case.
			expErr = nil
			tx3Spend = tx6
		}
		err = alice.PublishTransaction(tx6)
		if err != expErr {
			t.Fatalf("expected ErrDoubleSpend, got: %v", err)
		}

		// Mine the tx spending tx3.
		if err := mineAndAssert(r, tx3Spend); err != nil {
			t.Fatalf("unable to mine tx: %v", err)
		}
	}

	// At last we try to spend an output already spent by a confirmed
	// transaction.
	// TODO(halseth): we currently skip this test for neutrino, as the
	// backing btcd node will consider the tx being an orphan, and will
	// accept it. Should look into if this is the behavior also for
	// bitcoind, and update test accordingly.
	if alice.BackEnd() != "neutrino" {
		// Create another tx spending tx3.
		pubKey4, err := alice.DeriveNextKey(
			keychain.KeyFamilyMultiSig,
		)
		if err != nil {
			t.Fatalf("unable to obtain public key: %v", err)
		}
		tx7, err := txFromOutput(
			tx3, alice.Cfg.Signer, keyDesc.PubKey,
			pubKey4.PubKey, txFee, false,
		)

		if err != nil {
			t.Fatal(err)
		}

		// Expect rejection.
		err = alice.PublishTransaction(tx7)
		if err != lnwallet.ErrDoubleSpend {
			t.Fatalf("expected ErrDoubleSpend, got: %v", err)
		}
	}
}

func testSignOutputUsingTweaks(r *rpctest.Harness,
	alice, _ *lnwallet.LightningWallet, t *testing.T) {

	// We'd like to test the ability of the wallet's Signer implementation
	// to be able to sign with a private key derived from tweaking the
	// specific public key. This scenario exercises the case when the
	// wallet needs to sign for a sweep of a revoked output, or just claim
	// any output that pays to a tweaked key.

	// First, generate a new public key under the control of the wallet,
	// then generate a revocation key using it.
	pubKey, err := alice.DeriveNextKey(
		keychain.KeyFamilyMultiSig,
	)
	if err != nil {
		t.Fatalf("unable to obtain public key: %v", err)
	}

	// As we'd like to test both single tweak, and double tweak spends,
	// we'll generate a commitment pre-image, then derive a revocation key
	// and single tweak from that.
	commitPreimage := bytes.Repeat([]byte{2}, 32)
	commitSecret, commitPoint := btcec.PrivKeyFromBytes(btcec.S256(),
		commitPreimage)

	revocationKey := input.DeriveRevocationPubkey(pubKey.PubKey, commitPoint)
	commitTweak := input.SingleTweakBytes(commitPoint, pubKey.PubKey)

	tweakedPub := input.TweakPubKey(pubKey.PubKey, commitPoint)

	// As we'd like to test both single and double tweaks, we'll repeat
	// the same set up twice. The first will use a regular single tweak,
	// and the second will use a double tweak.
	baseKey := pubKey
	for i := 0; i < 2; i++ {
		var tweakedKey *btcec.PublicKey
		if i == 0 {
			tweakedKey = tweakedPub
		} else {
			tweakedKey = revocationKey
		}

		// Using the given key for the current iteration, we'll
		// generate a regular p2wkh from that.
		pubkeyHash := btcutil.Hash160(tweakedKey.SerializeCompressed())
		keyAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubkeyHash,
			&chaincfg.RegressionNetParams)
		if err != nil {
			t.Fatalf("unable to create addr: %v", err)
		}
		keyScript, err := txscript.PayToAddrScript(keyAddr)
		if err != nil {
			t.Fatalf("unable to generate script: %v", err)
		}

		// With the script fully assembled, instruct the wallet to fund
		// the output with a newly created transaction.
		newOutput := &wire.TxOut{
			Value:    btcutil.SatoshiPerBitcoin,
			PkScript: keyScript,
		}
		tx, err := alice.SendOutputs([]*wire.TxOut{newOutput}, 2500)
		if err != nil {
			t.Fatalf("unable to create output: %v", err)
		}
		txid := tx.TxHash()
		// Query for the transaction generated above so we can located
		// the index of our output.
		err = waitForMempoolTx(r, &txid)
		if err != nil {
			t.Fatalf("tx not relayed to miner: %v", err)
		}
		var outputIndex uint32
		if bytes.Equal(tx.TxOut[0].PkScript, keyScript) {
			outputIndex = 0
		} else {
			outputIndex = 1
		}

		// With the index located, we can create a transaction spending
		// the referenced output.
		sweepTx := wire.NewMsgTx(2)
		sweepTx.AddTxIn(&wire.TxIn{
			PreviousOutPoint: wire.OutPoint{
				Hash:  txid,
				Index: outputIndex,
			},
		})
		sweepTx.AddTxOut(&wire.TxOut{
			Value:    1000,
			PkScript: keyScript,
		})

		// Now we can populate the sign descriptor which we'll use to
		// generate the signature. Within the descriptor we set the
		// private tweak value as the key in the script is derived
		// based on this tweak value and the key we originally
		// generated above.
		signDesc := &input.SignDescriptor{
			KeyDesc: keychain.KeyDescriptor{
				PubKey: baseKey.PubKey,
			},
			WitnessScript: keyScript,
			Output:        newOutput,
			HashType:      txscript.SigHashAll,
			SigHashes:     txscript.NewTxSigHashes(sweepTx),
			InputIndex:    0,
		}

		// If this is the first, loop, we'll use the generated single
		// tweak, otherwise, we'll use the double tweak.
		if i == 0 {
			signDesc.SingleTweak = commitTweak
		} else {
			signDesc.DoubleTweak = commitSecret
		}

		// With the descriptor created, we use it to generate a
		// signature, then manually create a valid witness stack we'll
		// use for signing.
		spendSig, err := alice.Cfg.Signer.SignOutputRaw(sweepTx, signDesc)
		if err != nil {
			t.Fatalf("unable to generate signature: %v", err)
		}
		witness := make([][]byte, 2)
		witness[0] = append(spendSig.Serialize(), byte(txscript.SigHashAll))
		witness[1] = tweakedKey.SerializeCompressed()
		sweepTx.TxIn[0].Witness = witness

		// Finally, attempt to validate the completed transaction. This
		// should succeed if the wallet was able to properly generate
		// the proper private key.
		vm, err := txscript.NewEngine(keyScript,
			sweepTx, 0, txscript.StandardVerifyFlags, nil,
			nil, int64(btcutil.SatoshiPerBitcoin))
		if err != nil {
			t.Fatalf("unable to create engine: %v", err)
		}
		if err := vm.Execute(); err != nil {
			t.Fatalf("spend #%v is invalid: %v", i, err)
		}
	}
}

func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
	_ *lnwallet.LightningWallet, t *testing.T) {

	// We first mine a few blocks to ensure any transactions still in the
	// mempool confirm, and then get the original balance, before a
	// reorganization that doesn't invalidate any existing transactions or
	// create any new non-coinbase transactions. We'll then check if it's
	// the same after the empty reorg.
	_, err := r.Node.Generate(5)
	if err != nil {
		t.Fatalf("unable to generate blocks on passed node: %v", err)
	}

	// Give wallet time to catch up.
	err = waitForWalletSync(r, w)
	if err != nil {
		t.Fatalf("unable to sync wallet: %v", err)
	}

	// Send some money from the miner to the wallet
	err = loadTestCredits(r, w, 20, 4)
	if err != nil {
		t.Fatalf("unable to send money to lnwallet: %v", err)
	}

	// Send some money from the wallet back to the miner.
	// Grab a fresh address from the miner to house this output.
	minerAddr, err := r.NewAddress()
	if err != nil {
		t.Fatalf("unable to generate address for miner: %v", err)
	}
	script, err := txscript.PayToAddrScript(minerAddr)
	if err != nil {
		t.Fatalf("unable to create pay to addr script: %v", err)
	}
	output := &wire.TxOut{
		Value:    1e8,
		PkScript: script,
	}
	tx, err := w.SendOutputs([]*wire.TxOut{output}, 2500)
	if err != nil {
		t.Fatalf("unable to send outputs: %v", err)
	}
	txid := tx.TxHash()
	err = waitForMempoolTx(r, &txid)
	if err != nil {
		t.Fatalf("tx not relayed to miner: %v", err)
	}
	_, err = r.Node.Generate(50)
	if err != nil {
		t.Fatalf("unable to generate blocks on passed node: %v", err)
	}

	// Give wallet time to catch up.
	err = waitForWalletSync(r, w)
	if err != nil {
		t.Fatalf("unable to sync wallet: %v", err)
	}

	// Get the original balance.
	origBalance, err := w.ConfirmedBalance(1)
	if err != nil {
		t.Fatalf("unable to query for balance: %v", err)
	}

	// Now we cause a reorganization as follows.
	// Step 1: create a new miner and start it.
	r2, err := rpctest.New(r.ActiveNet, nil, []string{"--txindex"})
	if err != nil {
		t.Fatalf("unable to create mining node: %v", err)
	}
	err = r2.SetUp(false, 0)
	if err != nil {
		t.Fatalf("unable to set up mining node: %v", err)
	}
	defer r2.TearDown()
	newBalance, err := w.ConfirmedBalance(1)
	if err != nil {
		t.Fatalf("unable to query for balance: %v", err)
	}
	if origBalance != newBalance {
		t.Fatalf("wallet balance incorrect, should have %v, "+
			"instead have %v", origBalance, newBalance)
	}

	// Step 2: connect the miner to the passed miner and wait for
	// synchronization.
	err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANAdd)
	if err != nil {
		t.Fatalf("unable to connect mining nodes together: %v", err)
	}
	err = rpctest.JoinNodes([]*rpctest.Harness{r2, r}, rpctest.Blocks)
	if err != nil {
		t.Fatalf("unable to synchronize mining nodes: %v", err)
	}

	// Step 3: Do a set of reorgs by disconnecting the two miners, mining
	// one block on the passed miner and two on the created miner,
	// connecting them, and waiting for them to sync.
	for i := 0; i < 5; i++ {
		// Wait for disconnection
		timeout := time.After(30 * time.Second)
		stillConnected := true
		var peers []btcjson.GetPeerInfoResult
		for stillConnected {
			// Allow for timeout
			time.Sleep(100 * time.Millisecond)
			select {
			case <-timeout:
				t.Fatalf("timeout waiting for miner disconnect")
			default:
			}
			err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANRemove)
			if err != nil {
				t.Fatalf("unable to disconnect mining nodes: %v",
					err)
			}
			peers, err = r2.Node.GetPeerInfo()
			if err != nil {
				t.Fatalf("unable to get peer info: %v", err)
			}
			stillConnected = false
			for _, peer := range peers {
				if peer.Addr == r.P2PAddress() {
					stillConnected = true
					break
				}
			}
		}
		_, err = r.Node.Generate(2)
		if err != nil {
			t.Fatalf("unable to generate blocks on passed node: %v",
				err)
		}
		_, err = r2.Node.Generate(3)
		if err != nil {
			t.Fatalf("unable to generate blocks on created node: %v",
				err)
		}

		// Step 5: Reconnect the miners and wait for them to synchronize.
		err = r2.Node.AddNode(r.P2PAddress(), rpcclient.ANAdd)
		if err != nil {
			switch err := err.(type) {
			case *btcjson.RPCError:
				if err.Code != -8 {
					t.Fatalf("unable to connect mining "+
						"nodes together: %v", err)
				}
			default:
				t.Fatalf("unable to connect mining nodes "+
					"together: %v", err)
			}
		}
		err = rpctest.JoinNodes([]*rpctest.Harness{r2, r},
			rpctest.Blocks)
		if err != nil {
			t.Fatalf("unable to synchronize mining nodes: %v", err)
		}

		// Give wallet time to catch up.
		err = waitForWalletSync(r, w)
		if err != nil {
			t.Fatalf("unable to sync wallet: %v", err)
		}
	}

	// Now we check that the wallet balance stays the same.
	newBalance, err = w.ConfirmedBalance(1)
	if err != nil {
		t.Fatalf("unable to query for balance: %v", err)
	}
	if origBalance != newBalance {
		t.Fatalf("wallet balance incorrect, should have %v, "+
			"instead have %v", origBalance, newBalance)
	}
}

// testChangeOutputSpendConfirmation ensures that when we attempt to spend a
// change output created by the wallet, the wallet receives its confirmation
// once included in the chain.
func testChangeOutputSpendConfirmation(r *rpctest.Harness,
	alice, bob *lnwallet.LightningWallet, t *testing.T) {

	// In order to test that we see the confirmation of a transaction that
	// spends an output created by SendOutputs, we'll start by emptying
	// Alice's wallet so that no other UTXOs can be picked. To do so, we'll
	// generate an address for Bob, who will receive all the coins.
	// Assuming a balance of 80 BTC and a transaction fee of 2500 sat/kw,
	// we'll craft the following transaction so that Alice doesn't have any
	// UTXOs left.
	aliceBalance, err := alice.ConfirmedBalance(0)
	if err != nil {
		t.Fatalf("unable to retrieve alice's balance: %v", err)
	}
	bobPkScript := newPkScript(t, bob, lnwallet.WitnessPubKey)

	// We'll use a transaction fee of 13020 satoshis, which will allow us to
	// sweep all of Alice's balance in one transaction containing 1 input
	// and 1 output.
	//
	// TODO(wilmer): replace this once SendOutputs easily supports sending
	// all funds in one transaction.
	txFeeRate := chainfee.SatPerKWeight(2500)
	txFee := btcutil.Amount(14380)
	output := &wire.TxOut{
		Value:    int64(aliceBalance - txFee),
		PkScript: bobPkScript,
	}
	tx := sendCoins(t, r, alice, bob, output, txFeeRate)
	txHash := tx.TxHash()
	assertTxInWallet(t, alice, txHash, true)
	assertTxInWallet(t, bob, txHash, true)

	// With the transaction sent and confirmed, Alice's balance should now
	// be 0.
	aliceBalance, err = alice.ConfirmedBalance(0)
	if err != nil {
		t.Fatalf("unable to retrieve alice's balance: %v", err)
	}
	if aliceBalance != 0 {
		t.Fatalf("expected alice's balance to be 0 BTC, found %v",
			aliceBalance)
	}

	// Now, we'll send an output back to Alice from Bob of 1 BTC.
	alicePkScript := newPkScript(t, alice, lnwallet.WitnessPubKey)
	output = &wire.TxOut{
		Value:    btcutil.SatoshiPerBitcoin,
		PkScript: alicePkScript,
	}
	tx = sendCoins(t, r, bob, alice, output, txFeeRate)
	txHash = tx.TxHash()
	assertTxInWallet(t, alice, txHash, true)
	assertTxInWallet(t, bob, txHash, true)

	// Alice now has an available output to spend, but it was not a change
	// output, which is what the test expects. Therefore, we'll generate one
	// by sending Bob back some coins.
	output = &wire.TxOut{
		Value:    btcutil.SatoshiPerBitcent,
		PkScript: bobPkScript,
	}
	tx = sendCoins(t, r, alice, bob, output, txFeeRate)
	txHash = tx.TxHash()
	assertTxInWallet(t, alice, txHash, true)
	assertTxInWallet(t, bob, txHash, true)

	// Then, we'll spend the change output and ensure we see its
	// confirmation come in.
	tx = sendCoins(t, r, alice, bob, output, txFeeRate)
	txHash = tx.TxHash()
	assertTxInWallet(t, alice, txHash, true)
	assertTxInWallet(t, bob, txHash, true)

	// Finally, we'll replenish Alice's wallet with some more coins to
	// ensure she has enough for any following test cases.
	if err := loadTestCredits(r, alice, 20, 4); err != nil {
		t.Fatalf("unable to replenish alice's wallet: %v", err)
	}
}

// testLastUnusedAddr tests that the LastUnusedAddress returns the address if
// it isn't used, and also that once the address becomes used, then it's
// properly rotated.
func testLastUnusedAddr(miner *rpctest.Harness,
	alice, bob *lnwallet.LightningWallet, t *testing.T) {

	if _, err := miner.Node.Generate(1); err != nil {
		t.Fatalf("unable to generate block: %v", err)
	}

	// We'll repeat this test for each address type to ensure they're all
	// rotated properly.
	addrTypes := []lnwallet.AddressType{
		lnwallet.WitnessPubKey, lnwallet.NestedWitnessPubKey,
	}
	for _, addrType := range addrTypes {
		addr1, err := alice.LastUnusedAddress(addrType)
		if err != nil {
			t.Fatalf("unable to get addr: %v", err)
		}
		addr2, err := alice.LastUnusedAddress(addrType)
		if err != nil {
			t.Fatalf("unable to get addr: %v", err)
		}

		// If we generate two addresses back to back, then we should
		// get the same addr, as none of them have been used yet.
		if addr1.String() != addr2.String() {
			t.Fatalf("addresses changed w/o use: %v vs %v", addr1, addr2)
		}

		// Next, we'll have Bob pay to Alice's new address. This should
		// trigger address rotation at the backend wallet.
		addrScript, err := txscript.PayToAddrScript(addr1)
		if err != nil {
			t.Fatalf("unable to convert addr to script: %v", err)
		}
		feeRate := chainfee.SatPerKWeight(2500)
		output := &wire.TxOut{
			Value:    1000000,
			PkScript: addrScript,
		}
		sendCoins(t, miner, bob, alice, output, feeRate)

		// If we make a new address, then it should be brand new, as
		// the prior address has been used.
		addr3, err := alice.LastUnusedAddress(addrType)
		if err != nil {
			t.Fatalf("unable to get addr: %v", err)
		}
		if addr1.String() == addr3.String() {
			t.Fatalf("address should have changed but didn't")
		}
	}
}

// testCreateSimpleTx checks that a call to CreateSimpleTx will return a
// transaction that is equal to the one that is being created by SendOutputs in
// a subsequent call.
func testCreateSimpleTx(r *rpctest.Harness, w *lnwallet.LightningWallet,
	_ *lnwallet.LightningWallet, t *testing.T) {

	// Send some money from the miner to the wallet
	err := loadTestCredits(r, w, 20, 4)
	if err != nil {
		t.Fatalf("unable to send money to lnwallet: %v", err)
	}

	// The test cases we will run through for all backends.
	testCases := []struct {
		outVals []int64
		feeRate chainfee.SatPerKWeight
		valid   bool
	}{
		{
			outVals: []int64{},
			feeRate: 2500,
			valid:   false, // No outputs.
		},

		{
			outVals: []int64{200},
			feeRate: 2500,
			valid:   false, // Dust output.
		},

		{
			outVals: []int64{1e8},
			feeRate: 2500,
			valid:   true,
		},
		{
			outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
			feeRate: 2500,
			valid:   true,
		},
		{
			outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
			feeRate: 12500,
			valid:   true,
		},
		{
			outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5},
			feeRate: 50000,
			valid:   true,
		},
		{
			outVals: []int64{1e8, 2e8, 1e8, 2e7, 3e5, 1e8, 2e8,
				1e8, 2e7, 3e5},
			feeRate: 44250,
			valid:   true,
		},
	}

	for i, test := range testCases {
		feeRate := test.feeRate

		// Grab some fresh addresses from the miner that we will send
		// to.
		outputs := make([]*wire.TxOut, len(test.outVals))
		for i, outVal := range test.outVals {
			minerAddr, err := r.NewAddress()
			if err != nil {
				t.Fatalf("unable to generate address for "+
					"miner: %v", err)
			}
			script, err := txscript.PayToAddrScript(minerAddr)
			if err != nil {
				t.Fatalf("unable to create pay to addr "+
					"script: %v", err)
			}
			output := &wire.TxOut{
				Value:    outVal,
				PkScript: script,
			}

			outputs[i] = output
		}

		// Now try creating a tx spending to these outputs.
		createTx, createErr := w.CreateSimpleTx(
			outputs, feeRate, true,
		)
		switch {
		case test.valid && createErr != nil:
			fmt.Println(spew.Sdump(createTx.Tx))
			t.Fatalf("got unexpected error when creating tx: %v",
				createErr)

		case !test.valid && createErr == nil:
			t.Fatalf("test #%v should have failed on tx "+
				"creation", i)
		}

		// Also send to these outputs. This should result in a tx
		// _very_ similar to the one we just created being sent. The
		// only difference is that the dry run tx is not signed, and
		// that the change output position might be different.
		tx, sendErr := w.SendOutputs(outputs, feeRate)
		switch {
		case test.valid && sendErr != nil:
			t.Fatalf("got unexpected error when sending tx: %v",
				sendErr)

		case !test.valid && sendErr == nil:
			t.Fatalf("test #%v should fail for tx sending", i)
		}

		// We expected either both to not fail, or both to fail with
		// the same error.
		if createErr != sendErr {
			t.Fatalf("error creating tx (%v) different "+
				"from error sending outputs (%v)",
				createErr, sendErr)
		}

		// If we expected the creation to fail, then this test is over.
		if !test.valid {
			continue
		}

		txid := tx.TxHash()
		err = waitForMempoolTx(r, &txid)
		if err != nil {
			t.Fatalf("tx not relayed to miner: %v", err)
		}

		// Helper method to check that the two txs are similar.
		assertSimilarTx := func(a, b *wire.MsgTx) error {
			if a.Version != b.Version {
				return fmt.Errorf("different versions: "+
					"%v vs %v", a.Version, b.Version)
			}
			if a.LockTime != b.LockTime {
				return fmt.Errorf("different locktimes: "+
					"%v vs %v", a.LockTime, b.LockTime)
			}
			if len(a.TxIn) != len(b.TxIn) {
				return fmt.Errorf("different number of "+
					"inputs: %v vs %v", len(a.TxIn),
					len(b.TxIn))
			}
			if len(a.TxOut) != len(b.TxOut) {
				return fmt.Errorf("different number of "+
					"outputs: %v vs %v", len(a.TxOut),
					len(b.TxOut))
			}

			// They should be spending the same inputs.
			for i := range a.TxIn {
				prevA := a.TxIn[i].PreviousOutPoint
				prevB := b.TxIn[i].PreviousOutPoint
				if prevA != prevB {
					return fmt.Errorf("different inputs: "+
						"%v vs %v", spew.Sdump(prevA),
						spew.Sdump(prevB))
				}
			}

			// They should have the same outputs. Since the change
			// output position gets randomized, they are not
			// guaranteed to be in the same order.
			for _, outA := range a.TxOut {
				found := false
				for _, outB := range b.TxOut {
					if reflect.DeepEqual(outA, outB) {
						found = true
						break
					}
				}
				if !found {
					return fmt.Errorf("did not find "+
						"output %v", spew.Sdump(outA))
				}
			}
			return nil
		}

		// Assert that our "template tx" was similar to the one that
		// ended up being sent.
		if err := assertSimilarTx(createTx.Tx, tx); err != nil {
			t.Fatalf("transactions not similar: %v", err)
		}
	}
}

// testSignOutputCreateAccount tests that we're able to properly sign for an
// output if the target account hasn't yet been created on disk. In this case,
// we'll create the account, then sign.
func testSignOutputCreateAccount(r *rpctest.Harness, w *lnwallet.LightningWallet,
	_ *lnwallet.LightningWallet, t *testing.T) {

	// First, we'll create a sign desc that references a non-default key
	// family. Under the hood, key families are actually accounts, so this
	// should force create of the account so we can sign with it.
	fakeTx := wire.NewMsgTx(2)
	fakeTx.AddTxIn(&wire.TxIn{
		PreviousOutPoint: wire.OutPoint{
			Hash:  chainhash.Hash{},
			Index: 0,
		},
	})
	signDesc := &input.SignDescriptor{
		KeyDesc: keychain.KeyDescriptor{
			KeyLocator: keychain.KeyLocator{
				Family: 99,
				Index:  1,
			},
		},
		WitnessScript: []byte{},
		Output: &wire.TxOut{
			Value: 1000,
		},
		HashType:   txscript.SigHashAll,
		SigHashes:  txscript.NewTxSigHashes(fakeTx),
		InputIndex: 0,
	}

	// We'll now sign and expect this to succeed, as even though the
	// account doesn't exist atm, it should be created in order to process
	// the inbound signing request.
	_, err := w.Cfg.Signer.SignOutputRaw(fakeTx, signDesc)
	if err != nil {
		t.Fatalf("unable to sign for output with non-existent "+
			"account: %v", err)
	}
}

type walletTestCase struct {
	name string
	test func(miner *rpctest.Harness, alice, bob *lnwallet.LightningWallet,
		test *testing.T)
}

var walletTests = []walletTestCase{
	{
		// TODO(wilmer): this test should remain first until the wallet
		// can properly craft a transaction that spends all of its
		// on-chain funds.
		name: "change output spend confirmation",
		test: testChangeOutputSpendConfirmation,
	},
	{
		name: "insane fee reject",
		test: testReservationInitiatorBalanceBelowDustCancel,
	},
	{
		name: "single funding workflow",
		test: func(miner *rpctest.Harness, alice,
			bob *lnwallet.LightningWallet, t *testing.T) {

			testSingleFunderReservationWorkflow(
				miner, alice, bob, t,
				lnwallet.CommitmentTypeLegacy, nil,
				nil, [32]byte{}, 0,
			)
		},
	},
	{
		name: "single funding workflow tweakless",
		test: func(miner *rpctest.Harness, alice,
			bob *lnwallet.LightningWallet, t *testing.T) {

			testSingleFunderReservationWorkflow(
				miner, alice, bob, t,
				lnwallet.CommitmentTypeTweakless, nil,
				nil, [32]byte{}, 0,
			)
		},
	},
	{
		name: "single funding workflow external funding tx",
		test: testSingleFunderExternalFundingTx,
	},
	{
		name: "dual funder workflow",
		test: testDualFundingReservationWorkflow,
	},
	{
		name: "output locking",
		test: testFundingTransactionLockedOutputs,
	},
	{
		name: "reservation insufficient funds",
		test: testFundingCancellationNotEnoughFunds,
	},
	{
		name: "transaction subscriptions",
		test: testTransactionSubscriptions,
	},
	{
		name: "transaction details",
		test: testListTransactionDetails,
	},
	{
		name: "publish transaction",
		test: testPublishTransaction,
	},
	{
		name: "signed with tweaked pubkeys",
		test: testSignOutputUsingTweaks,
	},
	{
		name: "test cancel non-existent reservation",
		test: testCancelNonExistentReservation,
	},
	{
		name: "last unused addr",
		test: testLastUnusedAddr,
	},
	{
		name: "reorg wallet balance",
		test: testReorgWalletBalance,
	},
	{
		name: "create simple tx",
		test: testCreateSimpleTx,
	},
	{
		name: "test sign create account",
		test: testSignOutputCreateAccount,
	},
}

func clearWalletStates(a, b *lnwallet.LightningWallet) error {
	a.ResetReservations()
	b.ResetReservations()

	if err := a.Cfg.Database.Wipe(); err != nil {
		return err
	}

	return b.Cfg.Database.Wipe()
}

func waitForMempoolTx(r *rpctest.Harness, txid *chainhash.Hash) error {
	var found bool
	var tx *btcutil.Tx
	var err error
	timeout := time.After(30 * time.Second)
	for !found {
		// Do a short wait
		select {
		case <-timeout:
			return fmt.Errorf("timeout after 10s")
		default:
		}
		time.Sleep(100 * time.Millisecond)

		// Check for the harness' knowledge of the txid
		tx, err = r.Node.GetRawTransaction(txid)
		if err != nil {
			switch e := err.(type) {
			case *btcjson.RPCError:
				if e.Code == btcjson.ErrRPCNoTxInfo {
					continue
				}
			default:
			}
			return err
		}
		if tx != nil && tx.MsgTx().TxHash() == *txid {
			found = true
		}
	}
	return nil
}

func waitForWalletSync(r *rpctest.Harness, w *lnwallet.LightningWallet) error {
	var (
		synced                  bool
		err                     error
		bestHash, knownHash     *chainhash.Hash
		bestHeight, knownHeight int32
	)
	timeout := time.After(10 * time.Second)
	for !synced {
		// Do a short wait
		select {
		case <-timeout:
			return fmt.Errorf("timeout after 30s")
		case <-time.Tick(100 * time.Millisecond):
		}

		// Check whether the chain source of the wallet is caught up to
		// the harness it's supposed to be catching up to.
		bestHash, bestHeight, err = r.Node.GetBestBlock()
		if err != nil {
			return err
		}
		knownHash, knownHeight, err = w.Cfg.ChainIO.GetBestBlock()
		if err != nil {
			return err
		}
		if knownHeight != bestHeight {
			continue
		}
		if *knownHash != *bestHash {
			return fmt.Errorf("hash at height %d doesn't match: "+
				"expected %s, got %s", bestHeight, bestHash,
				knownHash)
		}

		// Check for synchronization.
		synced, _, err = w.IsSynced()
		if err != nil {
			return err
		}
	}
	return nil
}

// testSingleFunderExternalFundingTx tests that the wallet is able to properly
// carry out a funding flow backed by a channel point that has been crafted
// outside the wallet.
func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
	alice, bob *lnwallet.LightningWallet, t *testing.T) {

	// First, we'll obtain multi-sig keys from both Alice and Bob which
	// simulates them exchanging keys on a higher level.
	aliceFundingKey, err := alice.DeriveNextKey(keychain.KeyFamilyMultiSig)
	if err != nil {
		t.Fatalf("unable to obtain alice funding key: %v", err)
	}
	bobFundingKey, err := bob.DeriveNextKey(keychain.KeyFamilyMultiSig)
	if err != nil {
		t.Fatalf("unable to obtain bob funding key: %v", err)
	}

	// We'll now set up for them to open a 4 BTC channel, with 1 BTC pushed
	// to Bob's side.
	chanAmt := 4 * btcutil.SatoshiPerBitcoin

	// Simulating external funding negotiation, we'll now create the
	// funding transaction for both parties. Utilizing existing tools,
	// we'll create a new chanfunding.Assembler hacked by Alice's wallet.
	aliceChanFunder := chanfunding.NewWalletAssembler(chanfunding.WalletConfig{
		CoinSource:       lnwallet.NewCoinSource(alice),
		CoinSelectLocker: alice,
		CoinLocker:       alice,
		Signer:           alice.Cfg.Signer,
		DustLimit:        600,
	})

	// With the chan funder created, we'll now provision a funding intent,
	// bind the keys we obtained above, and finally obtain our funding
	// transaction and outpoint.
	fundingIntent, err := aliceChanFunder.ProvisionChannel(&chanfunding.Request{
		LocalAmt: btcutil.Amount(chanAmt),
		MinConfs: 1,
		FeeRate:  253,
		ChangeAddr: func() (btcutil.Address, error) {
			return alice.NewAddress(lnwallet.WitnessPubKey, true)
		},
	})
	if err != nil {
		t.Fatalf("unable to perform coin selection: %v", err)
	}

	// With our intent created, we'll instruct it to finalize the funding
	// transaction, and also hand us the outpoint so we can simulate
	// external crafting of the funding transaction.
	var (
		fundingTx *wire.MsgTx
		chanPoint *wire.OutPoint
	)
	if fullIntent, ok := fundingIntent.(*chanfunding.FullIntent); ok {
		fullIntent.BindKeys(&aliceFundingKey, bobFundingKey.PubKey)

		fundingTx, err = fullIntent.CompileFundingTx(nil, nil)
		if err != nil {
			t.Fatalf("unable to compile funding tx: %v", err)
		}
		chanPoint, err = fullIntent.ChanPoint()
		if err != nil {
			t.Fatalf("unable to obtain chan point: %v", err)
		}
	} else {
		t.Fatalf("expected full intent, instead got: %T", fullIntent)
	}

	// Now that we have the fully constructed funding transaction, we'll
	// create a new shim external funder out of it for Alice, and prep a
	// shim intent for Bob.
	thawHeight := uint32(200)
	aliceExternalFunder := chanfunding.NewCannedAssembler(
		thawHeight, *chanPoint, btcutil.Amount(chanAmt), &aliceFundingKey,
		bobFundingKey.PubKey, true,
	)
	bobShimIntent, err := chanfunding.NewCannedAssembler(
		thawHeight, *chanPoint, btcutil.Amount(chanAmt), &bobFundingKey,
		aliceFundingKey.PubKey, false,
	).ProvisionChannel(&chanfunding.Request{
		LocalAmt: btcutil.Amount(chanAmt),
		MinConfs: 1,
		FeeRate:  253,
		ChangeAddr: func() (btcutil.Address, error) {
			return bob.NewAddress(lnwallet.WitnessPubKey, true)
		},
	})
	if err != nil {
		t.Fatalf("unable to create shim intent for bob: %v", err)
	}

	// At this point, we have everything we need to carry out our test, so
	// we'll being the funding flow between Alice and Bob.
	//
	// However, before we do so, we'll register a new shim intent for Bob,
	// so he knows what keys to use when he receives the funding request
	// from Alice.
	pendingChanID := testHdSeed
	err = bob.RegisterFundingIntent(pendingChanID, bobShimIntent)
	if err != nil {
		t.Fatalf("unable to register intent: %v", err)
	}

	// Now we can carry out the single funding flow as normal, we'll
	// specify our external funder and funding transaction, as well as the
	// pending channel ID generated above to allow Alice and Bob to track
	// the funding flow externally.
	testSingleFunderReservationWorkflow(
		miner, alice, bob, t, lnwallet.CommitmentTypeTweakless,
		aliceExternalFunder, func() *wire.MsgTx {
			return fundingTx
		}, pendingChanID, thawHeight,
	)
}

// TestInterfaces tests all registered interfaces with a unified set of tests
// which exercise each of the required methods found within the WalletController
// interface.
//
// NOTE: In the future, when additional implementations of the WalletController
// interface have been implemented, in order to ensure the new concrete
// implementation is automatically tested, two steps must be undertaken. First,
// one needs add a "non-captured" (_) import from the new sub-package. This
// import should trigger an init() method within the package which registers
// the interface. Second, an additional case in the switch within the main loop
// below needs to be added which properly initializes the interface.
//
// TODO(roasbeef): purge bobNode in favor of dual lnwallet's
func TestLightningWallet(t *testing.T) {
	t.Parallel()

	// Initialize the harness around a btcd node which will serve as our
	// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
	// up this node with a chain length of 125, so we have plenty of BTC
	// to play around with.
	miningNode, err := rpctest.New(netParams, nil, []string{"--txindex"})
	if err != nil {
		t.Fatalf("unable to create mining node: %v", err)
	}
	defer miningNode.TearDown()
	if err := miningNode.SetUp(true, 25); err != nil {
		t.Fatalf("unable to set up mining node: %v", err)
	}

	// Next mine enough blocks in order for segwit and the CSV package
	// soft-fork to activate on RegNet.
	numBlocks := netParams.MinerConfirmationWindow * 2
	if _, err := miningNode.Node.Generate(numBlocks); err != nil {
		t.Fatalf("unable to generate blocks: %v", err)
	}

	rpcConfig := miningNode.RPCConfig()

	tempDir, err := ioutil.TempDir("", "channeldb")
	if err != nil {
		t.Fatalf("unable to create temp dir: %v", err)
	}
	db, err := channeldb.Open(tempDir)
	if err != nil {
		t.Fatalf("unable to create db: %v", err)
	}
	hintCache, err := chainntnfs.NewHeightHintCache(db)
	if err != nil {
		t.Fatalf("unable to create height hint cache: %v", err)
	}
	chainNotifier, err := btcdnotify.New(
		&rpcConfig, netParams, hintCache, hintCache,
	)
	if err != nil {
		t.Fatalf("unable to create notifier: %v", err)
	}
	if err := chainNotifier.Start(); err != nil {
		t.Fatalf("unable to start notifier: %v", err)
	}

	for _, walletDriver := range lnwallet.RegisteredWallets() {
		for _, backEnd := range walletDriver.BackEnds() {
			if !runTests(t, walletDriver, backEnd, miningNode,
				rpcConfig, chainNotifier) {
				return
			}
		}
	}
}

// runTests runs all of the tests for a single interface implementation and
// chain back-end combination. This makes it easier to use `defer` as well as
// factoring out the test logic from the loop which cycles through the
// interface implementations.
func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
	backEnd string, miningNode *rpctest.Harness,
	rpcConfig rpcclient.ConnConfig,
	chainNotifier chainntnfs.ChainNotifier) bool {

	var (
		bio lnwallet.BlockChainIO

		aliceSigner input.Signer
		bobSigner   input.Signer

		aliceKeyRing keychain.SecretKeyRing
		bobKeyRing   keychain.SecretKeyRing

		aliceWalletController lnwallet.WalletController
		bobWalletController   lnwallet.WalletController
	)

	tempTestDirAlice, err := ioutil.TempDir("", "lnwallet")
	if err != nil {
		t.Fatalf("unable to create temp directory: %v", err)
	}
	defer os.RemoveAll(tempTestDirAlice)

	tempTestDirBob, err := ioutil.TempDir("", "lnwallet")
	if err != nil {
		t.Fatalf("unable to create temp directory: %v", err)
	}
	defer os.RemoveAll(tempTestDirBob)

	walletType := walletDriver.WalletType
	switch walletType {
	case "btcwallet":
		var aliceClient, bobClient chain.Interface
		switch backEnd {
		case "btcd":
			aliceClient, err = chain.NewRPCClient(netParams,
				rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
				rpcConfig.Certificates, false, 20)
			if err != nil {
				t.Fatalf("unable to make chain rpc: %v", err)
			}
			bobClient, err = chain.NewRPCClient(netParams,
				rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
				rpcConfig.Certificates, false, 20)
			if err != nil {
				t.Fatalf("unable to make chain rpc: %v", err)
			}

		case "neutrino":
			// Set some package-level variable to speed up
			// operation for tests.
			neutrino.BanDuration = time.Millisecond * 100
			neutrino.QueryTimeout = time.Millisecond * 500
			neutrino.QueryNumRetries = 1

			// Start Alice - open a database, start a neutrino
			// instance, and initialize a btcwallet driver for it.
			aliceDB, err := walletdb.Create(
				"bdb", tempTestDirAlice+"/neutrino.db", true,
			)
			if err != nil {
				t.Fatalf("unable to create DB: %v", err)
			}
			defer aliceDB.Close()
			aliceChain, err := neutrino.NewChainService(
				neutrino.Config{
					DataDir:     tempTestDirAlice,
					Database:    aliceDB,
					ChainParams: *netParams,
					ConnectPeers: []string{
						miningNode.P2PAddress(),
					},
				},
			)
			if err != nil {
				t.Fatalf("unable to make neutrino: %v", err)
			}
			aliceChain.Start()
			defer aliceChain.Stop()
			aliceClient = chain.NewNeutrinoClient(
				netParams, aliceChain,
			)

			// Start Bob - open a database, start a neutrino
			// instance, and initialize a btcwallet driver for it.
			bobDB, err := walletdb.Create(
				"bdb", tempTestDirBob+"/neutrino.db", true,
			)
			if err != nil {
				t.Fatalf("unable to create DB: %v", err)
			}
			defer bobDB.Close()
			bobChain, err := neutrino.NewChainService(
				neutrino.Config{
					DataDir:     tempTestDirBob,
					Database:    bobDB,
					ChainParams: *netParams,
					ConnectPeers: []string{
						miningNode.P2PAddress(),
					},
				},
			)
			if err != nil {
				t.Fatalf("unable to make neutrino: %v", err)
			}
			bobChain.Start()
			defer bobChain.Stop()
			bobClient = chain.NewNeutrinoClient(
				netParams, bobChain,
			)

		case "bitcoind":
			// Start a bitcoind instance.
			tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
			if err != nil {
				t.Fatalf("unable to create temp directory: %v", err)
			}
			zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
			zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
			defer os.RemoveAll(tempBitcoindDir)
			rpcPort := rand.Int()%(65536-1024) + 1024
			bitcoind := exec.Command(
				"bitcoind",
				"-datadir="+tempBitcoindDir,
				"-regtest",
				"-connect="+miningNode.P2PAddress(),
				"-txindex",
				"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6f"+
					"d$507c670e800a95284294edb5773b05544b"+
					"220110063096c221be9933c82d38e1",
				fmt.Sprintf("-rpcport=%d", rpcPort),
				"-disablewallet",
				"-zmqpubrawblock="+zmqBlockHost,
				"-zmqpubrawtx="+zmqTxHost,
			)
			err = bitcoind.Start()
			if err != nil {
				t.Fatalf("couldn't start bitcoind: %v", err)
			}
			defer bitcoind.Wait()
			defer bitcoind.Process.Kill()

			// Wait for the bitcoind instance to start up.

			host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
			var chainConn *chain.BitcoindConn
			err = wait.NoError(func() error {
				chainConn, err = chain.NewBitcoindConn(
					netParams, host, "weks", "weks",
					zmqBlockHost, zmqTxHost,
					100*time.Millisecond,
				)
				if err != nil {
					return err
				}

				return chainConn.Start()
			}, 10*time.Second)
			if err != nil {
				t.Fatalf("unable to establish connection to "+
					"bitcoind: %v", err)
			}
			defer chainConn.Stop()

			// Create a btcwallet bitcoind client for both Alice and
			// Bob.
			aliceClient = chainConn.NewBitcoindClient()
			bobClient = chainConn.NewBitcoindClient()
		default:
			t.Fatalf("unknown chain driver: %v", backEnd)
		}

		aliceSeed := sha256.New()
		aliceSeed.Write([]byte(backEnd))
		aliceSeed.Write(aliceHDSeed[:])
		aliceSeedBytes := aliceSeed.Sum(nil)

		aliceWalletConfig := &btcwallet.Config{
			PrivatePass: []byte("alice-pass"),
			HdSeed:      aliceSeedBytes,
			DataDir:     tempTestDirAlice,
			NetParams:   netParams,
			ChainSource: aliceClient,
			CoinType:    keychain.CoinTypeTestnet,
		}
		aliceWalletController, err = walletDriver.New(aliceWalletConfig)
		if err != nil {
			t.Fatalf("unable to create btcwallet: %v", err)
		}
		aliceSigner = aliceWalletController.(*btcwallet.BtcWallet)
		aliceKeyRing = keychain.NewBtcWalletKeyRing(
			aliceWalletController.(*btcwallet.BtcWallet).InternalWallet(),
			keychain.CoinTypeTestnet,
		)

		bobSeed := sha256.New()
		bobSeed.Write([]byte(backEnd))
		bobSeed.Write(bobHDSeed[:])
		bobSeedBytes := bobSeed.Sum(nil)

		bobWalletConfig := &btcwallet.Config{
			PrivatePass: []byte("bob-pass"),
			HdSeed:      bobSeedBytes,
			DataDir:     tempTestDirBob,
			NetParams:   netParams,
			ChainSource: bobClient,
			CoinType:    keychain.CoinTypeTestnet,
		}
		bobWalletController, err = walletDriver.New(bobWalletConfig)
		if err != nil {
			t.Fatalf("unable to create btcwallet: %v", err)
		}
		bobSigner = bobWalletController.(*btcwallet.BtcWallet)
		bobKeyRing = keychain.NewBtcWalletKeyRing(
			bobWalletController.(*btcwallet.BtcWallet).InternalWallet(),
			keychain.CoinTypeTestnet,
		)
		bio = bobWalletController.(*btcwallet.BtcWallet)
	default:
		t.Fatalf("unknown wallet driver: %v", walletType)
	}

	// Funding via 20 outputs with 4BTC each.
	alice, err := createTestWallet(
		tempTestDirAlice, miningNode, netParams,
		chainNotifier, aliceWalletController, aliceKeyRing,
		aliceSigner, bio,
	)
	if err != nil {
		t.Fatalf("unable to create test ln wallet: %v", err)
	}
	defer alice.Shutdown()

	bob, err := createTestWallet(
		tempTestDirBob, miningNode, netParams,
		chainNotifier, bobWalletController, bobKeyRing, bobSigner, bio,
	)
	if err != nil {
		t.Fatalf("unable to create test ln wallet: %v", err)
	}
	defer bob.Shutdown()

	// Both wallets should now have 80BTC available for
	// spending.
	assertProperBalance(t, alice, 1, 80)
	assertProperBalance(t, bob, 1, 80)

	// Execute every test, clearing possibly mutated
	// wallet state after each step.
	for _, walletTest := range walletTests {

		walletTest := walletTest

		testName := fmt.Sprintf("%v/%v:%v", walletType, backEnd,
			walletTest.name)
		success := t.Run(testName, func(t *testing.T) {
			if backEnd == "neutrino" &&
				strings.Contains(walletTest.name, "dual funder") {
				t.Skip("skipping dual funder tests for neutrino")
			}

			walletTest.test(miningNode, alice, bob, t)
		})
		if !success {
			return false
		}

		// TODO(roasbeef): possible reset mining
		// node's chainstate to initial level, cleanly
		// wipe buckets
		if err := clearWalletStates(alice, bob); err !=
			nil && err != kvdb.ErrBucketNotFound {
			t.Fatalf("unable to wipe wallet state: %v", err)
		}
	}

	return true
}