zingolib 7.0.0

Zingo backend library.
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
//! `LightClient` function `do_propose` generates a proposal to send to specified addresses.

use zcash_address::ZcashAddress;
use zcash_client_backend::data_api::error::Error as ProposalError;
use zcash_client_backend::zip321::TransactionRequest;
use zcash_protocol::value::Zatoshis;

use crate::data::proposal::OpReturnProposal;
use crate::data::proposal::ProportionalFeeProposal;
use crate::data::proposal::ProportionalFeeShieldProposal;
use crate::data::proposal::ZingoProposal;
use crate::data::receivers::Receiver;
use crate::data::receivers::transaction_request_from_receivers;
use crate::lightclient::LightClient;
use crate::lightclient::error::{LightClientError, SendError};
use crate::wallet::error::ProposeSendError;
use crate::wallet::error::ProposeShieldError;
use crate::wallet::error::WalletError;
use crate::wallet::propose::recipient_amount;
use crate::wallet::transparent::OpReturnData;

impl LightClient {
    /// Creates and stores a proposal from a transaction request.
    ///
    /// Pauses the sync engine before the first wallet read and holds that
    /// pause beside the stored proposal, so the state the proposal
    /// selected against cannot shift before the send builds it. A proposal
    /// that fails to come into existence releases the pause on the way
    /// out, restoring the engine to the mode it was found in.
    pub async fn propose_send(
        &mut self,
        request: TransactionRequest,
        account_id: zip32::AccountId,
    ) -> Result<ProportionalFeeProposal, ProposeSendError> {
        let minted = self.hold_proposal_pause();
        let result = {
            let mut wallet = self.wallet().write().await;
            wallet
                .create_send_proposal(request, account_id)
                .inspect(|proposal| {
                    wallet.store_proposal(ZingoProposal::Send {
                        proposal: proposal.clone(),
                        sending_account: account_id,
                    });
                })
        };
        if result.is_err() && minted {
            self.release_proposal_pause(true);
        }
        result
    }

    /// Creates and stores a proposal that sends all shielded funds of
    /// `account_id` to `address`, under the same pause as
    /// [`Self::propose_send`].
    pub async fn propose_send_all(
        &mut self,
        address: ZcashAddress,
        memo: Option<zcash_protocol::memo::MemoBytes>,
        account_id: zip32::AccountId,
    ) -> Result<ProportionalFeeProposal, ProposeSendError> {
        let minted = self.hold_proposal_pause();
        let result = {
            let mut wallet = self.wallet().write().await;
            wallet
                .create_send_all_proposal(address, memo, account_id)
                .inspect(|proposal| {
                    wallet.store_proposal(ZingoProposal::Send {
                        proposal: proposal.clone(),
                        sending_account: account_id,
                    });
                })
        };
        if result.is_err() && minted {
            self.release_proposal_pause(true);
        }
        result
    }

    /// Creates and stores a proposal for shielding all transparent funds,
    /// under the same stored-proposal pause as [`Self::propose_send`].
    /// The shield path previously read spendable coins without pausing
    /// the engine at all.
    pub async fn propose_shield(
        &mut self,
        account_id: zip32::AccountId,
    ) -> Result<ProportionalFeeShieldProposal, ProposeShieldError> {
        let minted = self.hold_proposal_pause();
        let result = {
            let mut wallet = self.wallet().write().await;
            wallet
                .create_shield_proposal(account_id)
                .inspect(|proposal| {
                    wallet.store_proposal(ZingoProposal::Shield {
                        proposal: proposal.clone(),
                        shielding_account: account_id,
                    });
                })
        };
        if result.is_err() && minted {
            self.release_proposal_pause(true);
        }
        result
    }

    /// Creates and stores a proposal to send `amount` to the transparent
    /// address `recipient` with `data` in an OP_RETURN (null-data) output.
    /// `recipient` is a P2PKH, P2SH, or TEX address. Holds the same
    /// stored-proposal pause as [`Self::propose_send`].
    ///
    /// The ephemeral source address is derived, not reserved. The wallet
    /// reserves it in [`Self::send_stored_proposal`], when the deshield
    /// is built. A dropped proposal leaves no trace in the address book.
    /// See [`OpReturnProposal`] for the two transactions the proposal
    /// describes and the fees it reports.
    pub async fn propose_send_with_op_return(
        &mut self,
        recipient: &str,
        amount: Zatoshis,
        data: OpReturnData,
        account_id: zip32::AccountId,
    ) -> Result<OpReturnProposal, LightClientError> {
        let minted = self.hold_proposal_pause();
        let result = self
            .create_op_return_proposal(recipient, amount, data, account_id)
            .await;
        match &result {
            Ok(proposal) => self
                .wallet()
                .write()
                .await
                .store_proposal(ZingoProposal::OpReturn(proposal.clone())),
            Err(_) if minted => self.release_proposal_pause(true),
            Err(_) => (),
        }
        result
    }

    /// Builds an [`OpReturnProposal`] without storing it. Derives the next
    /// ephemeral source address, sizes the OP_RETURN send fee, and creates
    /// the deshield proposal for `amount` plus that fee.
    pub(crate) async fn create_op_return_proposal(
        &mut self,
        recipient: &str,
        amount: Zatoshis,
        data: OpReturnData,
        account_id: zip32::AccountId,
    ) -> Result<OpReturnProposal, LightClientError> {
        use zcash_protocol::consensus::Parameters as _;

        let mut wallet = self.wallet().write().await;
        let chain_type = wallet.chain_type();

        let recipient = match ZcashAddress::try_from_encoded(recipient)
            .map_err(|e| SendError::OpReturn(WalletError::ParseError(e)))?
            .convert_if_network::<zcash_keys::address::Address>(chain_type.network_type())
        {
            Ok(zcash_keys::address::Address::Transparent(address)) => address,
            Ok(zcash_keys::address::Address::Tex(hash)) => {
                zcash_transparent::address::TransparentAddress::PublicKeyHash(hash)
            }
            _ => {
                return Err(
                    SendError::OpReturn(WalletError::OpReturnRecipientNotTransparent).into(),
                );
            }
        };

        let (source_address_id, source_address) = wallet
            .derive_refund_addresses(1, account_id)
            .map_err(|e| SendError::OpReturn(WalletError::from(e)))?
            .into_iter()
            .next()
            .expect("a request for one address yields one address");

        let target_height = wallet
            .get_migration_heights()
            .map_err(SendError::OpReturn)?
            .ok_or(SendError::OpReturn(WalletError::NoSyncData))?
            .0;

        let op_return_fee = wallet
            .op_return_send_fee(&recipient, &data, target_height)
            .map_err(SendError::OpReturn)?;

        let deshield_amount = (amount + op_return_fee).ok_or_else(|| {
            SendError::OpReturn(WalletError::TransparentBuild(
                "deshield amount overflows the zatoshi range".to_string(),
            ))
        })?;

        let source_zcash_address = ZcashAddress::try_from_encoded(
            &pepper_sync::keys::transparent::encode_address(&chain_type, source_address),
        )
        .map_err(|e| SendError::OpReturn(WalletError::ParseError(e)))?;

        let deshield_request = transaction_request_from_receivers(vec![Receiver::new(
            source_zcash_address,
            deshield_amount,
            None,
        )])
        .map_err(|e| {
            SendError::OpReturn(WalletError::TransparentBuild(format!(
                "deshield request: {e}"
            )))
        })?;

        let deshield = wallet
            .create_send_proposal(deshield_request, account_id)
            .map_err(SendError::ProposeSendError)?;

        Ok(OpReturnProposal::new(
            deshield,
            account_id,
            source_address_id,
            source_address,
            recipient,
            amount,
            data,
            op_return_fee,
        ))
    }

    /// Returns the maximum value that can be sent from `account_id` to
    /// `address`: the shielded spendable balance less the fee. A wallet
    /// that cannot cover the fee reports zero. A wallet that still needs
    /// a scan returns the `ScanRequired` error. To send that amount, use
    /// [`Self::propose_send_all`] rather than a `propose_send` request.
    pub async fn max_send_value(
        &self,
        address: ZcashAddress,
        account_id: zip32::AccountId,
    ) -> Result<Zatoshis, ProposeSendError> {
        let proposal = self
            .wallet()
            .write()
            .await
            .create_send_all_proposal(address, None, account_id);
        match proposal {
            Ok(proposal) => Ok(recipient_amount(&proposal)),
            Err(ProposeSendError::Proposal(ProposalError::InsufficientFunds { .. })) => {
                Ok(Zatoshis::ZERO)
            }
            Err(e) => Err(e),
        }
    }
}

#[cfg(test)]
mod shielding {
    use zcash_protocol::consensus::Parameters;
    use zingo_test_vectors::seeds;

    use crate::{
        config::{ClientConfig, WalletConfig},
        lightclient::LightClient,
        testutils::default_test_wallet_settings,
        wallet::error::ProposeShieldError,
    };

    async fn create_basic_client() -> LightClient {
        let config = ClientConfig::builder()
            .set_wallet_config(WalletConfig::MnemonicPhrase {
                mnemonic_phrase: seeds::HOSPITAL_MUSEUM_SEED.to_string(),
                no_of_accounts: 1.try_into().unwrap(),
                birthday: 419200,
                wallet_settings: default_test_wallet_settings(),
            })
            .build()
            .unwrap();
        LightClient::new(config, true).await.unwrap()
    }

    #[tokio::test]
    async fn propose_shield_missing_scan_prerequisite() {
        let basic_client = create_basic_client().await;
        let propose_shield_result = basic_client
            .wallet()
            .write()
            .await
            .create_shield_proposal(zip32::AccountId::ZERO);
        match propose_shield_result {
            Err(ProposeShieldError::Component(
                zcash_client_backend::data_api::error::Error::ScanRequired,
            )) => true,
            _ => panic!("Unexpected error state!"),
        };
    }
    #[tokio::test]
    async fn get_transparent_addresses() {
        let basic_client = create_basic_client().await;
        let network = basic_client.chain_type();

        // TODO: store t addrs as concrete types instead of encoded
        let transparent_addresses = basic_client
            .wallet()
            .read()
            .await
            .transparent_addresses()
            .values()
            .map(|address| {
                Ok(zcash_address::ZcashAddress::try_from_encoded(address)?
                    .convert_if_network::<zcash_transparent::address::TransparentAddress>(
                        network.network_type(),
                    )
                    .expect("incorrect network should be checked on wallet load"))
            })
            .collect::<Result<Vec<_>, zcash_address::ParseError>>()
            .unwrap();

        assert_eq!(
            transparent_addresses,
            [
                zcash_transparent::address::TransparentAddress::PublicKeyHash([
                    161, 138, 222, 242, 254, 121, 71, 105, 93, 131, 177, 31, 59, 185, 120, 148,
                    255, 189, 198, 33
                ])
            ]
        );
    }
}

/// Migrated from libtonode's `send_all` suite: these guarantees are pure
/// proposal logic over wallet state, so a synthetic wallet replaces the
/// LocalNet round trip.
#[cfg(test)]
mod send_all {
    use zcash_protocol::PoolType;
    use zcash_protocol::value::Zatoshis;

    use crate::{
        lightclient::LightClient, testutils::synthetic_wallet::SyntheticWalletBuilder,
        utils::conversion::address_from_str, wallet::error::ProposeSendError,
        wallet::keys::unified::ReceiverSelection,
    };

    /// An address belonging to a different wallet, so the send is external.
    fn external_address(pool: PoolType) -> zcash_address::ZcashAddress {
        let mut external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let selection = match pool {
            PoolType::ORCHARD | PoolType::IRONWOOD => ReceiverSelection::orchard_only(),
            PoolType::SAPLING => ReceiverSelection::sapling_only(),
            _ => unimplemented!("only shielded destinations are needed here"),
        };
        let (_, unified_address) = external_wallet
            .generate_unified_address(selection, zip32::AccountId::ZERO)
            .unwrap();
        address_from_str(&unified_address.encode(&external_wallet.chain_type())).unwrap()
    }

    /// Migrated from libtonode `send_all::ptfm_insufficient_funds`: a
    /// send-all whose only note cannot cover the fee of a cross-pool spend
    /// reports the fee plus one as the required amount.
    #[tokio::test]
    async fn ptfm_insufficient_funds() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(10_000)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let proposal_error = client
            .propose_send_all(
                external_address(PoolType::SAPLING),
                None,
                zip32::AccountId::ZERO,
            )
            .await;

        match proposal_error {
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::InsufficientFunds {
                    available: a,
                    required: r,
                },
            )) => {
                assert_eq!(a, Zatoshis::const_from_u64(10_000));
                assert_eq!(r, Zatoshis::const_from_u64(20_001));
            }
            _ => panic!("expected an InsufficientFunds error"),
        }
    }

    /// Migrated from libtonode `send_all::ptfm_zero_value`: a send-all
    /// whose only note is entirely consumed by the fee is rejected as
    /// insufficient funds. The 20_000-zat note exactly covers the V6 fee
    /// (orchard input bundle plus ironwood payment bundle, two padded
    /// actions each), leaving zero to send.
    #[tokio::test]
    async fn ptfm_zero_value() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(20_000)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let proposal_error = client
            .propose_send_all(
                external_address(PoolType::ORCHARD),
                None,
                zip32::AccountId::ZERO,
            )
            .await;

        match proposal_error {
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::InsufficientFunds {
                    available: a,
                    required: r,
                },
            )) => {
                assert_eq!(a, Zatoshis::const_from_u64(20_000));
                assert_eq!(r, Zatoshis::const_from_u64(20_001));
            }
            _ => panic!("expected an InsufficientFunds error"),
        }
    }

    /// Migrated from libtonode `send_all::ptfm_general`: a send-all from a
    /// wallet fragmented across both shielded pools drains every non-dust
    /// note and leaves the dust behind. The original assembled the
    /// fragmentation with five LocalNet sends and asserted zero
    /// balances-excluding-dust after mining, which is this same contract,
    /// one mined round trip later: send-all pays out the dust-excluded
    /// spendable balance minus the fee, so notes at or below the 5_000-zat
    /// `MARGINAL_FEE` dust line (which net nothing after paying for their
    /// own input) are never selected.
    #[tokio::test]
    async fn ptfm_general() {
        let viable_values = [100_000u64, 50_000];
        let orchard_values = [100_000, 5_000, 4_000];
        let sapling_values = [50_000, 4_000];

        let mut builder =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED);
        for value in orchard_values {
            builder = builder.orchard_note(value);
        }
        for value in sapling_values {
            builder = builder.sapling_note(value);
        }
        let mut client = LightClient::new_for_test(builder.build()).await;

        let proposal = client
            .propose_send_all(
                external_address(PoolType::SAPLING),
                None,
                zip32::AccountId::ZERO,
            )
            .await
            .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let mut selected: Vec<u64> = step
            .shielded_inputs()
            .expect("a shielded-funds send-all selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        selected.sort_unstable();
        assert_eq!(
            selected,
            [50_000, 100_000],
            "send-all selects exactly the non-dust notes of both pools"
        );
        let fee = u64::from(step.balance().fee_required());
        let payment: u64 = step
            .transaction_request()
            .payments()
            .values()
            .map(|payment| u64::from(payment.amount().expect("send-all payments carry amounts")))
            .sum();
        assert_eq!(payment + fee, viable_values.iter().sum::<u64>());
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change, 0, "send-all leaves no non-dust value behind");
    }

    /// Migrated from libtonode `fast::send_not_fully_synced` (renamed: no
    /// sync exists offline). The original proposed a send-all to the
    /// wallet's own sapling address while the validator ran five blocks
    /// ahead, asserting only that propose-and-send succeeded. Proposing
    /// never consults the server, so the offline half is exactly this
    /// self-destination send-all. The behind-tip send half remains covered
    /// by `load_wallet::verify_old_wallet_uses_server_height_in_send`.
    #[tokio::test]
    async fn send_all_to_own_sapling_proposes() {
        let note_value = 100_000;
        let mut wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(note_value)
                .build();
        let (_, own_sapling) = wallet
            .generate_unified_address(ReceiverSelection::sapling_only(), zip32::AccountId::ZERO)
            .unwrap();
        let destination = address_from_str(&own_sapling.encode(&wallet.chain_type())).unwrap();
        let mut client = LightClient::new_for_test(wallet).await;

        let proposal = client
            .propose_send_all(destination, None, zip32::AccountId::ZERO)
            .await
            .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let fee = u64::from(step.balance().fee_required());
        let payment: u64 = step
            .transaction_request()
            .payments()
            .values()
            .map(|payment| u64::from(payment.amount().expect("send-all payments carry amounts")))
            .sum();
        assert_eq!(payment + fee, note_value);
    }

    fn external_transparent_address() -> zcash_address::ZcashAddress {
        let external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        address_from_str(&external_wallet.get_address(PoolType::Transparent)).unwrap()
    }

    #[tokio::test]
    async fn send_all_to_transparent_recipient() {
        let mut client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(100_000)
                .orchard_note(50_000)
                .build(),
        )
        .await;

        let proposal = client
            .propose_send_all(external_transparent_address(), None, zip32::AccountId::ZERO)
            .await
            .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let fee = u64::from(step.balance().fee_required());
        let payment: u64 = step
            .transaction_request()
            .payments()
            .values()
            .map(|payment| u64::from(payment.amount().unwrap()))
            .sum();
        assert_eq!(payment + fee, 150_000);
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change, 0, "send-all leaves no non-dust value behind");
    }

    #[tokio::test]
    async fn max_send_value_is_zero_for_an_empty_wallet() {
        let client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED).build(),
        )
        .await;

        for address in [
            external_transparent_address(),
            external_address(PoolType::ORCHARD),
        ] {
            assert_eq!(
                client
                    .max_send_value(address, zip32::AccountId::ZERO)
                    .await
                    .unwrap(),
                Zatoshis::ZERO
            );
        }
    }

    #[tokio::test]
    async fn max_send_value_requires_a_scan_on_an_unsynced_wallet() {
        let config = crate::config::ClientConfig::builder()
            .set_wallet_config(crate::config::WalletConfig::MnemonicPhrase {
                mnemonic_phrase: zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED.to_string(),
                no_of_accounts: 1.try_into().unwrap(),
                birthday: 419200,
                wallet_settings: crate::testutils::default_test_wallet_settings(),
            })
            .build()
            .unwrap();
        let client = LightClient::new(config, true).await.unwrap();

        assert!(matches!(
            client
                .max_send_value(external_transparent_address(), zip32::AccountId::ZERO)
                .await,
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::ScanRequired
            ))
        ));
    }

    #[tokio::test]
    async fn max_send_value_is_zero_when_fee_consumes_everything() {
        let client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(20_000)
                .build(),
        )
        .await;

        assert_eq!(
            client
                .max_send_value(external_address(PoolType::ORCHARD), zip32::AccountId::ZERO)
                .await
                .unwrap(),
            Zatoshis::ZERO
        );
    }

    #[tokio::test]
    async fn send_all_rejects_a_memo_to_a_transparent_recipient() {
        let mut client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(100_000)
                .build(),
        )
        .await;
        let memo = zcash_protocol::memo::MemoBytes::from_bytes(b"memo").unwrap();

        let proposal_error = client
            .propose_send_all(
                external_transparent_address(),
                Some(memo),
                zip32::AccountId::ZERO,
            )
            .await;
        assert!(matches!(
            proposal_error,
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::Payment(
                    zcash_client_backend::zip321::PaymentError::TransparentMemo
                )
            ))
        ));
    }

    #[tokio::test]
    async fn send_all_on_an_unsynced_wallet_requires_a_scan() {
        let config = crate::config::ClientConfig::builder()
            .set_wallet_config(crate::config::WalletConfig::MnemonicPhrase {
                mnemonic_phrase: zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED.to_string(),
                no_of_accounts: 1.try_into().unwrap(),
                birthday: 419200,
                wallet_settings: crate::testutils::default_test_wallet_settings(),
            })
            .build()
            .unwrap();
        let mut client = LightClient::new(config, true).await.unwrap();

        let proposal_error = client
            .propose_send_all(
                external_address(PoolType::ORCHARD),
                None,
                zip32::AccountId::ZERO,
            )
            .await;
        assert!(matches!(
            proposal_error,
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::ScanRequired
            ))
        ));
    }

    /// Returns a TEX-encoded taddr from an external wallet, as a
    /// `ZcashAddress` ready for a payment.
    fn external_tex_address() -> zcash_address::ZcashAddress {
        use pepper_sync::keys::decode_address;
        use zcash_client_backend::address::Address;
        use zcash_transparent::address::TransparentAddress;

        let external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let taddr = external_wallet
            .transparent_addresses()
            .values()
            .next()
            .unwrap()
            .clone();
        let Address::Transparent(TransparentAddress::PublicKeyHash(taddr_bytes)) =
            decode_address(&external_wallet.chain_type(), &taddr).unwrap()
        else {
            panic!("a wallet-generated first taddr is p2pkh")
        };
        let tex_address = crate::testutils::interpret_taddr_as_tex_addr(
            taddr_bytes,
            &external_wallet.chain_type(),
        );
        zcash_address::ZcashAddress::try_from_encoded(&tex_address).unwrap()
    }

    /// Pins the zero-valued change note the send path always writes,
    /// which the send-max sizing does not price. With two legacy Orchard
    /// notes a post-NU6.3 orchard_v3 bundle costs spends + outputs rather
    /// than max(spends, outputs), so that change output is worth one more
    /// logical action, and the reported max must still send to a TEX
    /// recipient.
    #[tokio::test]
    async fn max_send_value_to_tex_with_legacy_orchard_notes_is_sendable() {
        let mut client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(100_000)
                .orchard_note(50_000)
                .build(),
        )
        .await;
        let destination = external_tex_address();

        let max = client
            .max_send_value(destination.clone(), zip32::AccountId::ZERO)
            .await
            .unwrap();
        assert!(
            max > Zatoshis::ZERO,
            "two legacy orchard notes must report a sendable max to a TEX address"
        );

        let request = zcash_client_backend::zip321::TransactionRequest::new(vec![
            zcash_client_backend::zip321::Payment::without_memo(destination.clone(), max),
        ])
        .unwrap();
        client
            .propose_send(request, zip32::AccountId::ZERO)
            .await
            .expect("the reported max must actually propose");

        let past_max = (max + Zatoshis::const_from_u64(1)).unwrap();
        let past_request = zcash_client_backend::zip321::TransactionRequest::new(vec![
            zcash_client_backend::zip321::Payment::without_memo(destination, past_max),
        ])
        .unwrap();
        assert!(matches!(
            client
                .propose_send(past_request, zip32::AccountId::ZERO)
                .await,
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::InsufficientFunds { .. }
            ))
        ));
    }

    /// Pins the same unpriced change output against a shielded
    /// destination, where the two legacy Orchard notes still make it cost
    /// an extra action but the send carries no padded transparent leg.
    #[tokio::test]
    async fn max_send_value_to_shielded_with_legacy_orchard_notes_is_sendable() {
        let mut client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(100_000)
                .orchard_note(50_000)
                .build(),
        )
        .await;
        let destination = external_address(PoolType::ORCHARD);

        let max = client
            .max_send_value(destination.clone(), zip32::AccountId::ZERO)
            .await
            .unwrap();
        assert!(
            max > Zatoshis::ZERO,
            "two legacy orchard notes must report a sendable max to a shielded address"
        );

        let request = zcash_client_backend::zip321::TransactionRequest::new(vec![
            zcash_client_backend::zip321::Payment::without_memo(destination.clone(), max),
        ])
        .unwrap();
        client
            .propose_send(request, zip32::AccountId::ZERO)
            .await
            .expect("the reported max must actually propose");

        let past_max = (max + Zatoshis::const_from_u64(1)).unwrap();
        let past_request = zcash_client_backend::zip321::TransactionRequest::new(vec![
            zcash_client_backend::zip321::Payment::without_memo(destination, past_max),
        ])
        .unwrap();
        assert!(matches!(
            client
                .propose_send(past_request, zip32::AccountId::ZERO)
                .await,
            Err(ProposeSendError::Proposal(
                zcash_client_backend::data_api::error::Error::InsufficientFunds { .. }
            ))
        ));
    }

    /// The selector refuses 80_000 from the Ironwood note alone.
    #[tokio::test]
    async fn send_all_to_tex_falls_back_when_the_selector_refuses_the_sized_amount() {
        let mut client = LightClient::new_for_test(
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .ironwood_note(100_000)
                .sapling_note(15_000)
                .build(),
        )
        .await;
        let destination = external_tex_address();

        let proposal = client
            .propose_send_all(destination.clone(), None, zip32::AccountId::ZERO)
            .await
            .expect("the send-max proposal stands in for the refused request");
        let fee: u64 = proposal
            .steps()
            .iter()
            .map(|step| step.balance().fee_required().into_u64())
            .sum();
        assert_eq!(
            crate::wallet::propose::recipient_amount(&proposal),
            Zatoshis::const_from_u64(80_000)
        );
        assert_eq!(fee, 35_000);

        assert_eq!(
            client
                .max_send_value(destination, zip32::AccountId::ZERO)
                .await
                .unwrap(),
            Zatoshis::const_from_u64(80_000)
        );
    }
}

/// Migrated from the libtonode chain_generics simpool instantiations
/// (`simpool_insufficient_{1,10_000}_{orchard,sapling}_to_*` and
/// `simpool_no_fund_1_000_000_to_*`): the insufficient-funds and unfunded
/// propose errors are pure proposal logic over wallet state, so a synthetic
/// wallet replaces the LocalNet environment and its multi-hop funding
/// chain.
#[cfg(test)]
mod simpool {
    use zcash_protocol::{PoolType, ShieldedPool};

    use crate::{
        lightclient::LightClient,
        testutils::{
            fee_tables, lightclient::from_inputs, synthetic_wallet::SyntheticWalletBuilder,
        },
        wallet::keys::unified::ReceiverSelection,
    };

    /// An encoded destination of the given pool type, belonging to a
    /// different wallet so the send is external.
    fn external_address(pool: PoolType) -> String {
        let mut external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let selection = match pool {
            PoolType::Shielded(ShieldedPool::Orchard)
            | PoolType::Shielded(ShieldedPool::Ironwood) => ReceiverSelection::orchard_only(),
            PoolType::Shielded(ShieldedPool::Sapling) => ReceiverSelection::sapling_only(),
            PoolType::Transparent => return external_wallet.get_address(PoolType::Transparent),
        };
        let (_, unified_address) = external_wallet
            .generate_unified_address(selection, zip32::AccountId::ZERO)
            .unwrap();
        unified_address.encode(&external_wallet.chain_type())
    }

    /// A wallet holding one `source`-pool note `underflow_amount` short of
    /// a 100_000 send to `pool` reports the exact shortfall.
    async fn insufficient(source: ShieldedPool, underflow_amount: u64, pool: PoolType) {
        let expected_fee = fee_tables::one_to_one(Some(source), pool, true);
        let secondary_fund = 100_000 + expected_fee - underflow_amount;
        let builder = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED);
        let wallet = match source {
            ShieldedPool::Ironwood => builder.ironwood_note(secondary_fund),
            ShieldedPool::Orchard => builder.orchard_note(secondary_fund),
            ShieldedPool::Sapling => builder.sapling_note(secondary_fund),
        }
        .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let tertiary_fund = 100_000;
        assert_eq!(
            from_inputs::propose(
                &mut client,
                vec![(external_address(pool).as_str(), tertiary_fund, None)],
            )
            .await
            .unwrap_err()
            .to_string(),
            format!(
                "Insufficient balance (have {}, need {} including fee)",
                secondary_fund,
                tertiary_fund + expected_fee
            )
        );
    }

    /// A wallet with no funds at all reports the full amount-plus-fee need.
    async fn unfunded_to(try_amount: u64, pool: PoolType) {
        let expected_fee = fee_tables::one_to_one(None, pool, true);
        let wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED).build();
        let mut client = LightClient::new_for_test(wallet).await;

        assert_eq!(
            from_inputs::propose(
                &mut client,
                vec![(external_address(pool).as_str(), try_amount, None)],
            )
            .await
            .unwrap_err()
            .to_string(),
            format!(
                "Insufficient balance (have {}, need {} including fee)",
                0,
                try_amount + expected_fee
            )
        );
    }

    #[tokio::test]
    async fn insufficient_1_ironwood_to_ironwood() {
        insufficient(ShieldedPool::Ironwood, 1, PoolType::IRONWOOD).await;
    }
    #[tokio::test]
    async fn insufficient_1_ironwood_to_sapling() {
        insufficient(ShieldedPool::Ironwood, 1, PoolType::SAPLING).await;
    }
    #[tokio::test]
    async fn insufficient_1_ironwood_to_transparent() {
        insufficient(ShieldedPool::Ironwood, 1, PoolType::Transparent).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_ironwood_to_ironwood() {
        insufficient(ShieldedPool::Ironwood, 10_000, PoolType::IRONWOOD).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_ironwood_to_sapling() {
        insufficient(ShieldedPool::Ironwood, 10_000, PoolType::SAPLING).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_ironwood_to_transparent() {
        insufficient(ShieldedPool::Ironwood, 10_000, PoolType::Transparent).await;
    }
    #[tokio::test]
    async fn no_fund_1_000_000_to_ironwood() {
        unfunded_to(1_000_000, PoolType::IRONWOOD).await;
    }
    #[tokio::test]
    async fn no_fund_1_000_000_to_sapling() {
        unfunded_to(1_000_000, PoolType::SAPLING).await;
    }
    #[tokio::test]
    async fn no_fund_1_000_000_to_transparent() {
        unfunded_to(1_000_000, PoolType::Transparent).await;
    }
    #[tokio::test]
    async fn insufficient_1_sapling_to_ironwood() {
        insufficient(ShieldedPool::Sapling, 1, PoolType::IRONWOOD).await;
    }
    #[tokio::test]
    async fn insufficient_1_sapling_to_sapling() {
        insufficient(ShieldedPool::Sapling, 1, PoolType::SAPLING).await;
    }
    #[tokio::test]
    async fn insufficient_1_sapling_to_transparent() {
        insufficient(ShieldedPool::Sapling, 1, PoolType::Transparent).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_sapling_to_ironwood() {
        insufficient(ShieldedPool::Sapling, 10_000, PoolType::IRONWOOD).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_sapling_to_sapling() {
        insufficient(ShieldedPool::Sapling, 10_000, PoolType::SAPLING).await;
    }
    #[tokio::test]
    async fn insufficient_10_000_sapling_to_transparent() {
        insufficient(ShieldedPool::Sapling, 10_000, PoolType::Transparent).await;
    }
}

/// Migrated from the libtonode chain_generics pool matrix: every shielded
/// source paired with every receiver pool, with and without change, plus
/// the transparent minimum-value and boundary-value rows. The matrix's
/// fee, value, and change assertions are pure proposer arithmetic, so a
/// synthetic wallet funded with exactly `value + change + fee` replaces
/// the LocalNet environment and its two-hop funding chain. Each case
/// asserts the proposal pays `value`, charges the `fee_tables::one_to_one`
/// fee, and returns exactly `change`. The Transmitted-to-Confirmed round
/// trip the matrix also exercised remains covered by the two surviving
/// chain_generics fixtures, which drive the same follow_proposal machinery
/// (with test_mempool off, so the Mempool-status leg is not asserted
/// there).
#[cfg(test)]
mod pool_matrix {
    use zcash_protocol::{PoolType, ShieldedPool};

    use crate::{
        lightclient::LightClient,
        testutils::{
            fee_tables, lightclient::from_inputs, synthetic_wallet::SyntheticWalletBuilder,
        },
        wallet::keys::unified::ReceiverSelection,
    };

    /// An encoded destination of the given pool type, belonging to a
    /// different wallet so the send is external.
    fn external_address(pool: PoolType) -> String {
        let mut external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let selection = match pool {
            PoolType::Shielded(ShieldedPool::Orchard)
            | PoolType::Shielded(ShieldedPool::Ironwood) => ReceiverSelection::orchard_only(),
            PoolType::Shielded(ShieldedPool::Sapling) => ReceiverSelection::sapling_only(),
            PoolType::Transparent => return external_wallet.get_address(PoolType::Transparent),
        };
        let (_, unified_address) = external_wallet
            .generate_unified_address(selection, zip32::AccountId::ZERO)
            .unwrap();
        unified_address.encode(&external_wallet.chain_type())
    }

    /// One matrix cell: a wallet holding a single `source` note of exactly
    /// `receiver_value + change + fee` proposes a `receiver_value` send to
    /// a `pool` destination, and the proposal's fee and change land on the
    /// fee table's prediction to the zatoshi.
    async fn matrix_case(source: ShieldedPool, pool: PoolType, receiver_value: u64, change: u64) {
        let expected_fee = fee_tables::one_to_one(
            Some(source),
            pool,
            !(source == ShieldedPool::Orchard && change == 0),
        );
        let funding = receiver_value + change + expected_fee;
        let builder = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED);
        let wallet = match source {
            ShieldedPool::Ironwood => builder.ironwood_note(funding),
            ShieldedPool::Orchard => builder.orchard_note(funding),
            ShieldedPool::Sapling => builder.sapling_note(funding),
        }
        .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(pool);
        let proposal = from_inputs::propose(
            &mut client,
            vec![(destination.as_str(), receiver_value, None)],
        )
        .await
        .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        assert_eq!(u64::from(step.balance().fee_required()), expected_fee);
        let proposed_change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(proposed_change, change);
        let payment: u64 = step
            .transaction_request()
            .payments()
            .values()
            .map(|payment| u64::from(payment.amount().expect("matrix payments carry amounts")))
            .sum();
        assert_eq!(payment, receiver_value);
    }

    macro_rules! pool_matrix_case {
        ($name:ident, $source:expr, $receiver:expr, $send_value:expr, $change:expr) => {
            #[tokio::test]
            async fn $name() {
                matrix_case($source, $receiver, $send_value, $change).await;
            }
        };
    }

    pool_matrix_case!(
        sapling_sends_to_transparent,
        ShieldedPool::Sapling,
        PoolType::TRANSPARENT,
        10_000,
        1_000
    );
    pool_matrix_case!(
        sapling_sends_to_sapling,
        ShieldedPool::Sapling,
        PoolType::SAPLING,
        10_000,
        1_000
    );
    pool_matrix_case!(
        sapling_sends_to_ironwood,
        ShieldedPool::Sapling,
        PoolType::IRONWOOD,
        10_000,
        1_000
    );
    pool_matrix_case!(
        ironwood_sends_to_transparent,
        ShieldedPool::Ironwood,
        PoolType::TRANSPARENT,
        10_000,
        1_000
    );
    pool_matrix_case!(
        ironwood_sends_to_sapling,
        ShieldedPool::Ironwood,
        PoolType::SAPLING,
        10_000,
        1_000
    );
    pool_matrix_case!(
        ironwood_sends_to_ironwood,
        ShieldedPool::Ironwood,
        PoolType::IRONWOOD,
        10_000,
        1_000
    );
    pool_matrix_case!(
        sapling_sends_to_transparent_no_change,
        ShieldedPool::Sapling,
        PoolType::TRANSPARENT,
        10_000,
        0
    );
    pool_matrix_case!(
        sapling_sends_to_sapling_no_change,
        ShieldedPool::Sapling,
        PoolType::SAPLING,
        10_000,
        0
    );
    pool_matrix_case!(
        sapling_sends_to_ironwood_no_change,
        ShieldedPool::Sapling,
        PoolType::IRONWOOD,
        10_000,
        0
    );
    pool_matrix_case!(
        orchard_sends_to_transparent_no_change,
        ShieldedPool::Orchard,
        PoolType::TRANSPARENT,
        10_000,
        0
    );
    pool_matrix_case!(
        orchard_sends_to_sapling_no_change,
        ShieldedPool::Orchard,
        PoolType::SAPLING,
        10_000,
        0
    );
    pool_matrix_case!(
        orchard_sends_to_orchard_no_change,
        ShieldedPool::Orchard,
        PoolType::ORCHARD,
        10_000,
        0
    );
    pool_matrix_case!(
        orchard_sends_to_ironwood_no_change,
        ShieldedPool::Orchard,
        PoolType::IRONWOOD,
        10_000,
        0
    );
    pool_matrix_case!(
        ironwood_sends_to_transparent_no_change,
        ShieldedPool::Ironwood,
        PoolType::TRANSPARENT,
        10_000,
        0
    );
    pool_matrix_case!(
        ironwood_sends_to_sapling_no_change,
        ShieldedPool::Ironwood,
        PoolType::SAPLING,
        10_000,
        0
    );
    // 546 zatoshis is the canonical transparent dust threshold. The
    // proposer permits it; whether a relay accepts it is the network's
    // business, and zebra's mempool dust rule was the reason this row's
    // LocalNet ancestor sent 546 rather than 1.
    pool_matrix_case!(
        sapling_sends_to_transparent_minimum_value,
        ShieldedPool::Sapling,
        PoolType::TRANSPARENT,
        546,
        0
    );
    pool_matrix_case!(
        sapling_sends_to_transparent_boundary_values,
        ShieldedPool::Sapling,
        PoolType::TRANSPARENT,
        49_999,
        9_999
    );
}

/// Offline proposal-shape tests over synthetic wallets: shield proposals
/// and note-selection behavior, migrated from LocalNet tests whose
/// assertions were all propose-stage.
#[cfg(test)]
mod proposal_shape {
    use zcash_primitives::transaction::fees::zip317::MARGINAL_FEE;
    use zcash_protocol::{PoolType, ShieldedPool};

    use crate::lightclient::LightClient;
    use crate::testutils::fee_tables;
    use crate::testutils::lightclient::from_inputs;
    use crate::testutils::synthetic_wallet::SyntheticWalletBuilder;
    use crate::wallet::keys::unified::ReceiverSelection;

    fn external_address(pool: PoolType) -> String {
        let mut external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let selection = match pool {
            PoolType::ORCHARD | PoolType::IRONWOOD => ReceiverSelection::orchard_only(),
            PoolType::SAPLING => ReceiverSelection::sapling_only(),
            _ => unimplemented!("only shielded destinations are needed here"),
        };
        let (_, unified_address) = external_wallet
            .generate_unified_address(selection, zip32::AccountId::ZERO)
            .unwrap();
        unified_address.encode(&external_wallet.chain_type())
    }

    /// A pool whose spendable list is exhausted mid-selection must still
    /// report the value it covered. The regression this pins: selecting the
    /// pool's last candidate note broke out of the selection loop before
    /// the remaining-needed figure was recomputed, so the caller saw a
    /// stale positive remainder and the next pool selected against a
    /// target that was already met. The wallet holds the covering note in
    /// Orchard and a larger note in Sapling (the two trailing pools of
    /// the selector's preference order for an Ironwood payment), so the
    /// assertion is independent of that order's head.
    #[tokio::test]
    async fn exhausted_pool_does_not_over_select() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(50_000)
            .sapling_note(100_000)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::Shielded(ShieldedPool::Ironwood));
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), 20_000, None)])
                .await
                .unwrap();
        let selected: Vec<u64> = proposal
            .steps()
            .first()
            .shielded_inputs()
            .expect("a shielded send selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        assert_eq!(
            selected,
            [50_000],
            "one note covers the send; an emptied pool must not spill selection into the next"
        );
    }

    /// A note bound to a pending migration part is withheld from ordinary
    /// input selection while a free note can satisfy the request (the
    /// ZIP 318 soft reservation). This is the unit-level twin of the
    /// libtonode `bound_note_reservation_and_external_spend_invalidation`
    /// scenario, and it fails alongside the exhausted-pool regression
    /// above: a stale remainder made the never-block fallback consume the
    /// bound note although the free note had already covered the target.
    #[tokio::test]
    async fn reservation_withholds_bound_note_while_a_free_note_suffices() {
        use pepper_sync::wallet::{NoteInterface as _, OrchardNote, OutputInterface as _};

        use crate::wallet::migration::{
            BoundNote, ConsentBinding, MigrationParams, MigrationPhase, MigrationState, PartId,
            PartRecord, SigningStrategy,
        };

        let mut wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
                .orchard_note(100_000)
                .orchard_note(50_000)
                .build();

        let (output_id, nullifier) = wallet
            .wallet_transactions
            .values()
            .flat_map(OrchardNote::transaction_outputs)
            .find(|note| note.value() == 100_000)
            .map(|note| {
                (
                    note.output_id(),
                    note.nullifier()
                        .expect("scanned notes carry nullifiers")
                        .to_bytes(),
                )
            })
            .expect("the wallet holds the 100_000 note");

        let params = MigrationParams::provisional(wallet.chain_type());
        wallet.migration = Some(MigrationState {
            consent: ConsentBinding {
                params_hash: params.params_hash(),
                plan_hash: [0; 32],
                consented_at: 0,
            },
            params,
            strategy: SigningStrategy::LazyAtBoundary,
            mode: crate::wallet::migration::MigrationMode::Scheduled,
            account: zip32::AccountId::ZERO,
            phase: MigrationPhase::PartsScheduled,
            parts: vec![PartRecord::new(
                PartId(0),
                100_000,
                BoundNote {
                    output_id,
                    nullifier,
                    commitment: [0; 32],
                },
            )],
        });
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::Shielded(ShieldedPool::Ironwood));
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), 20_000, None)])
                .await
                .unwrap();
        let selected: Vec<u64> = proposal
            .steps()
            .first()
            .shielded_inputs()
            .expect("a shielded send selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        assert_eq!(
            selected,
            [50_000],
            "the free note covers the send; the bound note stays reserved"
        );
    }

    /// Migrated from libtonode `slow::dust_sends_change_correctly`: a send
    /// of less than the fee still proposes, since the fee comes out of the
    /// selected note and the remainder returns as change. The original
    /// asserted nothing beyond the send call succeeding. The offline
    /// proposal now asserts the shape the name always promised.
    #[tokio::test]
    async fn dust_sends_change_correctly() {
        let note_value = 100_000;
        let sent_value = 1_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(note_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::IRONWOOD);
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), sent_value, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let fee = u64::from(step.balance().fee_required());
        assert_eq!(
            fee,
            fee_tables::one_to_one(Some(ShieldedPool::Ironwood), PoolType::IRONWOOD, true)
        );
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change, note_value - sent_value - fee);
    }

    /// Offline twin of libtonode
    /// `basic_transactions::send_and_sync_with_multiple_notes_no_panic`,
    /// which stays live as the pipeline control: a 50_000 payment from
    /// two 40_000 notes must gather both (either alone is consumed by
    /// the payment plus the 10_000 one-to-one fee) and return exactly
    /// 20_000 change.
    #[tokio::test]
    async fn payment_no_single_note_covers_gathers_both_and_changes() {
        let note_value = 40_000;
        let sent_value = 50_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(note_value)
            .ironwood_note(note_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::IRONWOOD);
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), sent_value, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let selected: Vec<u64> = step
            .shielded_inputs()
            .expect("a shielded payment selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        assert_eq!(selected, [note_value, note_value], "both notes gathered");
        let fee = u64::from(step.balance().fee_required());
        assert_eq!(
            fee,
            fee_tables::one_to_one(Some(ShieldedPool::Ironwood), PoolType::IRONWOOD, true)
        );
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change, 2 * note_value - sent_value - fee);
        assert_eq!(change, 20_000);
    }

    /// Offline twin of libtonode `slow::sapling_dust_fee_collection`,
    /// which stays live as the pipeline control: from 100_000 orchard
    /// plus a 1_000-zat sapling dust note, a 50_000 send selects only
    /// the orchard note (dust cannot pay for its own input), charges the
    /// plain one-to-one fee, and leaves 40_000 change, the live test's
    /// closing balance, derived here at proposal time. The dust note
    /// stays behind untouched.
    #[tokio::test]
    async fn sapling_dust_is_not_collected_toward_fees() {
        let ironwood_value = 100_000;
        let sapling_dust = 1_000;
        let sent_value = 50_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(ironwood_value)
            .sapling_note(sapling_dust)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::IRONWOOD);
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), sent_value, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let selected: Vec<u64> = step
            .shielded_inputs()
            .expect("a shielded payment selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        assert_eq!(selected, [ironwood_value], "the dust note is not selected");
        let fee = u64::from(step.balance().fee_required());
        assert_eq!(
            fee,
            fee_tables::one_to_one(Some(ShieldedPool::Ironwood), PoolType::IRONWOOD, true)
        );
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change, ironwood_value - sent_value - fee);
        assert_eq!(change, 40_000);
    }

    /// Migrated from libtonode `shield_transparent` (long `#[ignore]`d):
    /// shielding transparent funds proposes without error, consuming the
    /// coin into shielded change minus the fee. The original asserted
    /// nothing beyond the operations succeeding, so the offline proposal
    /// covers everything it protected.
    #[tokio::test]
    async fn shield_transparent() {
        let value = 100_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .transparent_coin(value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let proposal = client.propose_shield(zip32::AccountId::ZERO).await.unwrap();
        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        assert_eq!(step.transparent_inputs().len(), 1);
        let fee = u64::from(step.balance().fee_required());
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(change + fee, value);
    }

    /// Migrated from libtonode `fast::mine_to_transparent_and_propose_shielding`:
    /// a four-coin shield proposes as a single step spending all four
    /// coins into one change output, with the zip317 fee for four
    /// transparent inputs plus the orchard action pair. The original
    /// mined the coins and waited out coinbase maturity. Propose logic
    /// detects coinbase through the stored transaction's transparent
    /// bundle (`spendable_transparent_coins` demands 100 extra
    /// confirmations for it), and fabricated records carry no transparent
    /// bundle, so these coins present as ordinary mature ones and the
    /// proposal shape is identical.
    #[tokio::test]
    async fn four_coin_shield_proposal_shape() {
        let coin_value = 1_000_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .transparent_coin(coin_value)
            .transparent_coin(coin_value)
            .transparent_coin(coin_value)
            .transparent_coin(coin_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let proposal = client.propose_shield(zip32::AccountId::ZERO).await.unwrap();
        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        assert_eq!(step.transparent_inputs().len(), 4);
        assert_eq!(u64::from(step.balance().fee_required()), 30_000);
        assert_eq!(step.balance().proposed_change().len(), 1);
        assert_eq!(
            u64::from(step.balance().proposed_change()[0].value()),
            4 * coin_value - 30_000
        );
    }

    /// Migrated from libtonode `slow::zero_value_change`: a send that
    /// leaves exactly zero after the fee still proposes an orchard change
    /// output of value zero. The original mined the transaction and then
    /// counted one unspent zero-value note and one confirmed-spent funding
    /// note. The note-state bookkeeping it exercised is covered by the
    /// pepper-sync spend-status rig, and the load-bearing claim (the
    /// builder emits the zero-value change) is decided at proposal time.
    #[tokio::test]
    async fn zero_value_change() {
        let note_value = 100_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(note_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let fee = fee_tables::one_to_one(Some(ShieldedPool::Ironwood), PoolType::IRONWOOD, true);
        let destination = external_address(PoolType::IRONWOOD);
        let proposal = from_inputs::propose(
            &mut client,
            vec![(destination.as_str(), note_value - fee, None)],
        )
        .await
        .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        assert_eq!(u64::from(step.balance().fee_required()), fee);
        let change = step.balance().proposed_change();
        assert_eq!(change.len(), 1);
        assert_eq!(u64::from(change[0].value()), 0);
        assert_eq!(change[0].output_pool(), PoolType::IRONWOOD);
    }

    /// Migrated from libtonode `slow::zero_value_change_to_orchard_created`:
    /// same zero-value-change arithmetic on a cross-pool send. An 80_000
    /// payment to an external sapling address out of a 100_000 orchard note
    /// costs exactly the 20_000 cross-pool fee, so the ironwood change
    /// output is proposed at value zero.
    #[tokio::test]
    async fn zero_value_change_to_ironwood_created() {
        let note_value = 100_000;
        let sent_value = 80_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(note_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::SAPLING);
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), sent_value, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let fee = fee_tables::one_to_one(Some(ShieldedPool::Ironwood), PoolType::SAPLING, true);
        assert_eq!(u64::from(step.balance().fee_required()), fee);
        assert_eq!(note_value - sent_value - fee, 0);
        let change = step.balance().proposed_change();
        assert_eq!(change.len(), 1);
        assert_eq!(u64::from(change[0].value()), 0);
        assert_eq!(change[0].output_pool(), PoolType::IRONWOOD);
    }

    /// Spending a legacy Orchard note on a post-NU6.3 chain must leave the
    /// change in the **Orchard** pool, not route it to Ironwood. ZIP 318
    /// disables ordinary *payments* into the Orchard pool once NU6.3 activates
    /// "while still permitting change"; the turnstile (ZIP 2006) blocks value
    /// *entering* Orchard, and change funded by an Orchard input never leaves
    /// it, so an Orchard change output nets the pool down and stays
    /// consensus-valid. Keeping change in the source pool is the privacy policy
    /// for ordinary sends (a discussed decision): it reveals only the payment
    /// value crossing pools rather than migrating the whole note, and avoids
    /// stamping every send with an Orchard->Ironwood migration fingerprint.
    /// Deliberate Orchard->Ironwood movement is the migration engine's job, not
    /// an ordinary send's.
    #[tokio::test]
    async fn orchard_send_change_stays_orchard_post_nu6_3() {
        let note_value = 200_000;
        let sent_value = 50_000;
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(note_value)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        // Post-NU6.3 the payment itself routes through Ironwood (ZIP 318), but
        // that must not drag the change out of Orchard.
        let destination = external_address(PoolType::IRONWOOD);
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), sent_value, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let change = step.balance().proposed_change();
        assert_eq!(change.len(), 1);
        assert!(
            u64::from(change[0].value()) > 0,
            "the send must leave real change for the pool assertion to bite"
        );
        assert_eq!(
            change[0].output_pool(),
            PoolType::ORCHARD,
            "change from spending a legacy Orchard note must stay in Orchard \
             post-NU6.3: ZIP 318 permits Orchard change, and the privacy policy \
             keeps it in the source pool instead of migrating on every send"
        );
    }

    /// Migrated from libtonode `fast::tex::send_to_tex`: a payment to a
    /// TEX address proposes as the ZIP-320 two-step: shield to an
    /// ephemeral transparent output, then the transparent leg to the TEX
    /// destination funded entirely by step one. The original's only
    /// proposal-level assertion was the step count. The offline version
    /// also pins the inter-step wiring. The transmission half (both steps
    /// mined, three wallet records) retires with the LocalNet original.
    #[tokio::test]
    async fn send_to_tex() {
        use pepper_sync::keys::decode_address;
        use zcash_client_backend::address::Address;
        use zcash_client_backend::zip321::{Payment, TransactionRequest};
        use zcash_protocol::value::Zatoshis;
        use zcash_transparent::address::TransparentAddress;

        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .ironwood_note(5_000_000)
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        // A TEX destination derived from an external wallet's first
        // transparent address, as ZIP 320 prescribes.
        let external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let taddr = external_wallet
            .transparent_addresses()
            .values()
            .next()
            .unwrap()
            .clone();
        let Address::Transparent(TransparentAddress::PublicKeyHash(taddr_bytes)) =
            decode_address(&external_wallet.chain_type(), &taddr).unwrap()
        else {
            panic!("a wallet-generated first taddr is p2pkh")
        };
        let tex_address = crate::testutils::interpret_taddr_as_tex_addr(
            taddr_bytes,
            &external_wallet.chain_type(),
        );

        let request = TransactionRequest::new(vec![Payment::without_memo(
            zcash_address::ZcashAddress::try_from_encoded(&tex_address).unwrap(),
            Zatoshis::from_u64(100_000).unwrap(),
        )])
        .unwrap();
        let proposal = client
            .propose_send(request, zip32::AccountId::ZERO)
            .await
            .unwrap();

        assert_eq!(proposal.steps().len(), 2);
        let transparent_leg = proposal.steps().last();
        assert!(
            !transparent_leg.prior_step_inputs().is_empty(),
            "the transparent leg spends step one's ephemeral output"
        );
    }

    /// Migrated from the chain_generics `ignore_dust_inputs` fixture's
    /// load-bearing half: note selection excludes dust inputs. From a
    /// wallet holding four 1_000-zat dust notes and one 50_000-zat note
    /// in each shielded pool, a 10_000-zat send selects exactly the one
    /// viable ironwood note and none of the dust.
    #[tokio::test]
    async fn dust_inputs_are_ignored() {
        let builder = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED);
        let wallet = [1_000, 1_000, 1_000, 1_000, 50_000]
            .into_iter()
            .fold(builder, |builder, value| {
                builder.ironwood_note(value).sapling_note(value)
            })
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::Shielded(ShieldedPool::Ironwood));
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), 10_000, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let selected_values: Vec<u64> = step
            .shielded_inputs()
            .expect("a shielded-funds send selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        assert_eq!(
            selected_values,
            [50_000],
            "exactly the viable ironwood note, none of the dust"
        );
        assert_eq!(
            u64::from(step.balance().fee_required()),
            2 * u64::from(MARGINAL_FEE)
        );
    }

    /// Migrated from the chain_generics `note_selection_order` fixture's
    /// load-bearing half: from notes of 10/20/30/40 thousand zats, a
    /// 40_000-zat send selects the two-note covering set that leaves the
    /// least change, not more notes and not a higher-value covering set.
    #[tokio::test]
    async fn note_selection_covers_target_with_minimal_change() {
        let builder = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED);
        let wallet = [10_000, 20_000, 30_000, 40_000]
            .into_iter()
            .fold(builder, |builder, value| builder.sapling_note(value))
            .build();
        let mut client = LightClient::new_for_test(wallet).await;

        let destination = external_address(PoolType::Shielded(ShieldedPool::Ironwood));
        let proposal =
            from_inputs::propose(&mut client, vec![(destination.as_str(), 40_000, None)])
                .await
                .unwrap();

        assert_eq!(proposal.steps().len(), 1);
        let step = proposal.steps().first();
        let selected_values: Vec<u64> = step
            .shielded_inputs()
            .expect("a shielded-funds send selects shielded inputs")
            .notes()
            .iter()
            .map(|note| u64::from(note.note().value()))
            .collect();
        let selected_total: u64 = selected_values.iter().sum();
        let fee = u64::from(step.balance().fee_required());
        let change: u64 = step
            .balance()
            .proposed_change()
            .iter()
            .map(|change| u64::from(change.value()))
            .sum();
        assert_eq!(selected_values.len(), 2, "selected: {selected_values:?}");
        assert_eq!(selected_total, 40_000 + fee + change);
        // The fixture's guarantee: with 10_000-granular notes available,
        // any selection leaving 10_000 or more change used a bigger note
        // than necessary.
        assert!(change < 10_000, "change {change} implies oversized inputs");
    }
}

/// The propose/send family must never read wallet state a running engine
/// could be mutating, and a pause it takes must serve a pending proposal,
/// never outliving one. Each test here pins one way the imperative pause
/// discipline violates that contract. All four run red until the stored
/// pause discipline lands later on this branch.
#[cfg(test)]
mod sync_pause_contract {
    use std::sync::atomic;

    use pepper_sync::wallet::SyncMode;
    use zcash_protocol::PoolType;
    use zcash_protocol::value::Zatoshis;

    use crate::data::receivers::{Receiver, transaction_request_from_receivers};
    use crate::lightclient::LightClient;
    use crate::testutils::synthetic_wallet::SyntheticWalletBuilder;
    use crate::wallet::LightWallet;
    use crate::wallet::keys::unified::ReceiverSelection;

    /// An address belonging to a different wallet, so the send is external.
    fn external_address(pool: PoolType) -> zcash_address::ZcashAddress {
        let mut external_wallet =
            SyntheticWalletBuilder::new(zingo_test_vectors::seeds::ABANDON_ART_SEED).build();
        let selection = match pool {
            PoolType::ORCHARD | PoolType::IRONWOOD => ReceiverSelection::orchard_only(),
            PoolType::SAPLING => ReceiverSelection::sapling_only(),
            _ => unimplemented!("only shielded destinations are needed here"),
        };
        let (_, unified_address) = external_wallet
            .generate_unified_address(selection, zip32::AccountId::ZERO)
            .unwrap();
        crate::utils::conversion::address_from_str(
            &unified_address.encode(&external_wallet.chain_type()),
        )
        .unwrap()
    }

    /// A synthetic-wallet client whose sync-mode atomic reads `Running`,
    /// simulating a consumer whose background sync engine is between
    /// batches. No engine task exists, so every mode transition observed
    /// (or leaked) is the code under test's own.
    async fn client_with_running_engine(wallet: LightWallet) -> LightClient {
        let client = LightClient::new_for_test(wallet).await;
        client
            .sync_mode
            .store(SyncMode::Running as u8, atomic::Ordering::Release);
        client
    }

    /// The pause a proposal takes exists for the proposal it stores. A
    /// proposing call that fails stores nothing, so it must restore the
    /// engine on its way out. The imperative discipline pauses first and
    /// error-returns past every resume, leaving a consumer's background
    /// sync silently suspended with nothing pending.
    #[tokio::test]
    async fn failed_proposal_leaves_no_leaked_pause() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(10_000)
            .build();
        let mut client = client_with_running_engine(wallet).await;
        let request = transaction_request_from_receivers(vec![Receiver::new(
            external_address(PoolType::ORCHARD),
            Zatoshis::const_from_u64(50_000),
            None,
        )])
        .unwrap();

        let result = client.propose_send(request, zip32::AccountId::ZERO).await;

        assert!(result.is_err(), "a 50_000 send from 10_000 must fail");
        assert_eq!(
            client.sync_mode(),
            SyncMode::Running,
            "a failed proposal must leave the engine as it found it"
        );
    }

    /// A shield proposal must not come into existence while the engine is
    /// running: the proposal reads spendable coins the engine mutates.
    /// `propose_send` pauses for exactly this reason. The shield path
    /// never did.
    #[tokio::test]
    async fn shield_proposal_is_never_created_under_a_running_engine() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .transparent_coin(100_000)
            .build();
        let mut client = client_with_running_engine(wallet).await;

        let result = client.propose_shield(zip32::AccountId::ZERO).await;

        assert!(
            result.is_err() || client.sync_mode() != SyncMode::Running,
            "a shield proposal was created while the engine ran"
        );
    }

    /// `quick_shield` goes further than proposing: it builds and stores
    /// signed transactions, so its first wallet read must already run
    /// under a pause. Simulate the engine mid-batch by holding the
    /// wallet write lock: the call blocks on that lock, so if the mode
    /// still reads `Running` while the call is in flight, the build began
    /// without pausing the engine first.
    #[tokio::test]
    async fn quick_shield_never_builds_under_a_running_engine() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .transparent_coin(100_000)
            .build();
        let mut client = client_with_running_engine(wallet).await;
        let sync_mode = client.sync_mode.clone();
        let wallet_handle = client.wallet().clone();
        let engine_holds_wallet = wallet_handle.write().await;

        let call = tokio::spawn(async move {
            let _offline_transmission_failure = client.quick_shield(zip32::AccountId::ZERO).await;
        });
        // The current-thread runtime polls the spawned call until it
        // blocks on the held wallet lock.
        tokio::task::yield_now().await;

        assert_ne!(
            SyncMode::from_atomic_u8(&sync_mode).unwrap(),
            SyncMode::Running,
            "quick_shield began its wallet reads while the engine ran"
        );

        drop(engine_holds_wallet);
        call.await.unwrap();
    }

    /// The send-all proposal is `propose_send_all`'s first wallet read
    /// and must already run under a pause. Simulate the engine mid-batch
    /// by holding the wallet write lock: the proposal blocks on that lock,
    /// so if the engine's mode still reads `Running` while the call is in
    /// flight, the call began its reads without pausing the engine first.
    #[tokio::test]
    async fn send_all_sizing_runs_under_pause() {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(100_000)
            .build();
        let mut client = client_with_running_engine(wallet).await;
        let sync_mode = client.sync_mode.clone();
        let wallet_handle = client.wallet().clone();
        let engine_holds_wallet = wallet_handle.write().await;

        let address = external_address(PoolType::ORCHARD);
        let call = tokio::spawn(async move {
            client
                .propose_send_all(address, None, zip32::AccountId::ZERO)
                .await
        });
        // The current-thread runtime polls the spawned call until it
        // blocks on the held wallet lock.
        tokio::task::yield_now().await;

        assert_ne!(
            SyncMode::from_atomic_u8(&sync_mode).unwrap(),
            SyncMode::Running,
            "the send-all sizing began while the engine ran"
        );

        drop(engine_holds_wallet);
        call.await.unwrap().unwrap();
    }

    /// A request against a running-engine client that a proposal succeeds
    /// for, shared by the stored-pause protocol tests below.
    async fn client_with_stored_proposal() -> LightClient {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(100_000)
            .build();
        let mut client = client_with_running_engine(wallet).await;
        let request = transaction_request_from_receivers(vec![Receiver::new(
            external_address(PoolType::ORCHARD),
            Zatoshis::const_from_u64(10_000),
            None,
        )])
        .unwrap();
        client
            .propose_send(request, zip32::AccountId::ZERO)
            .await
            .unwrap();
        client
    }

    /// A successful proposal holds its pause for as long as the
    /// proposal is stored: the engine stays paused, so the state the
    /// proposal selected against cannot shift before the send builds it.
    #[tokio::test]
    async fn proposing_holds_the_pause_for_the_stored_proposal() {
        let client = client_with_stored_proposal().await;

        assert_eq!(
            client.sync_mode(),
            SyncMode::Paused,
            "a stored proposal must hold the engine paused"
        );
    }

    /// Clearing a stored proposal (the decline path of the two-phase
    /// send) restores the engine to the mode it held before proposing.
    /// Previously the pause outlived the declined proposal until some
    /// later send opted into resuming.
    #[tokio::test]
    async fn clearing_the_proposal_restores_the_engine() {
        let mut client = client_with_stored_proposal().await;

        client.clear_proposal().await;

        assert_eq!(
            client.sync_mode(),
            SyncMode::Running,
            "declining a proposal must restore the engine"
        );
    }

    /// An Indexerless send attempt fails before consuming the stored
    /// proposal (ADR 0006), so the proposal and the pause guarding
    /// it survive for retry once an Indexer is configured.
    #[tokio::test]
    async fn offline_send_failure_keeps_the_proposal_guarded() {
        let mut client = client_with_stored_proposal().await;

        let result = client.send_stored_proposal(true).await;

        assert!(result.is_err(), "an Indexerless send must fail");
        assert_eq!(
            client.sync_mode(),
            SyncMode::Paused,
            "a send that consumed nothing must release nothing"
        );
    }
}

#[cfg(test)]
mod op_return {
    use pepper_sync::keys::transparent::TransparentScope;
    use zcash_protocol::value::Zatoshis;

    use crate::data::proposal::{ZingoProposal, total_payment_amount};
    use crate::lightclient::LightClient;
    use crate::lightclient::error::{LightClientError, SendError};
    use crate::testutils::synthetic_wallet::SyntheticWalletBuilder;
    use crate::wallet::error::WalletError;
    use crate::wallet::keys::unified::ReceiverSelection;
    use crate::wallet::transparent::OpReturnData;

    const ACCOUNT: zip32::AccountId = zip32::AccountId::ZERO;
    const PAYLOAD: &[u8] = b"zingolib op_return payload";

    async fn client() -> LightClient {
        let wallet = SyntheticWalletBuilder::new(zingo_test_vectors::seeds::HOSPITAL_MUSEUM_SEED)
            .orchard_note(1_000_000)
            .build();
        LightClient::new_for_test(wallet).await
    }

    fn data() -> OpReturnData {
        OpReturnData::new(PAYLOAD.to_vec()).unwrap()
    }

    /// The proposal is stored as an OP_RETURN proposal. The deshield pays
    /// the amount plus the OP_RETURN fee to the next Refund-scope address.
    /// The total fee is the sum of both fees. The address is not reserved.
    #[tokio::test]
    async fn proposal_is_stored_and_reports_both_fees() {
        let mut client = client().await;
        let amount = Zatoshis::const_from_u64(100_000);

        let proposal = client
            .propose_send_with_op_return(zingo_test_vectors::EXT_TADDR, amount, data(), ACCOUNT)
            .await
            .unwrap();

        assert_eq!(proposal.amount(), amount);
        assert_eq!(proposal.data(), &data());
        assert!(u64::from(proposal.op_return_fee()) >= 10_000);
        assert_eq!(u64::from(proposal.op_return_fee()) % 5_000, 0);
        assert_eq!(
            total_payment_amount(proposal.deshield()).unwrap(),
            (amount + proposal.op_return_fee()).unwrap(),
            "the deshield pays the amount plus the OP_RETURN fee"
        );
        assert_eq!(
            proposal.total_fee().unwrap(),
            (proposal.deshield_fee().unwrap() + proposal.op_return_fee()).unwrap()
        );

        let mut wallet = client.wallet().write().await;
        let stored = wallet.take_proposal().expect("proposal stored");
        assert!(matches!(stored, ZingoProposal::OpReturn(_)));
        let (next_id, next_address) = wallet.derive_refund_addresses(1, ACCOUNT).unwrap()[0];
        assert_eq!(
            proposal.source_address(),
            &next_address,
            "the deshield pays the next Refund-scope address"
        );
        assert!(
            !wallet.transparent_addresses().contains_key(&next_id),
            "proposing does not reserve the address"
        );
    }

    /// Proposing twice derives the same source address. Neither call
    /// reserves it.
    #[tokio::test]
    async fn proposing_does_not_reserve_the_source_address() {
        let mut client = client().await;
        let amount = Zatoshis::const_from_u64(100_000);

        let first = client
            .propose_send_with_op_return(zingo_test_vectors::EXT_TADDR, amount, data(), ACCOUNT)
            .await
            .unwrap();
        let second = client
            .propose_send_with_op_return(zingo_test_vectors::EXT_TADDR, amount, data(), ACCOUNT)
            .await
            .unwrap();

        assert_eq!(first.source_address(), second.source_address());
        let wallet = client.wallet().read().await;
        assert!(
            wallet
                .transparent_addresses()
                .keys()
                .all(|id| id.scope() != TransparentScope::Refund),
            "no Refund-scope address was reserved"
        );
    }

    /// The network of the test wallet.
    async fn network(client: &LightClient) -> zcash_protocol::consensus::NetworkType {
        use zcash_protocol::consensus::Parameters as _;
        client.wallet().read().await.chain_type().network_type()
    }

    /// A TEX recipient is accepted. The proposal pays the P2PKH hash it
    /// wraps.
    #[tokio::test]
    async fn tex_recipient_is_accepted() {
        use zcash_address::ToAddress as _;
        use zcash_transparent::address::TransparentAddress;
        let mut client = client().await;
        let hash = [4u8; 20];
        let tex = zcash_address::ZcashAddress::from_tex(network(&client).await, hash).encode();

        let proposal = client
            .propose_send_with_op_return(&tex, Zatoshis::const_from_u64(100_000), data(), ACCOUNT)
            .await
            .unwrap();

        assert_eq!(
            proposal.recipient(),
            &TransparentAddress::PublicKeyHash(hash)
        );
    }

    /// A P2SH recipient is accepted.
    #[tokio::test]
    async fn p2sh_recipient_is_accepted() {
        use zcash_address::ToAddress as _;
        use zcash_transparent::address::TransparentAddress;
        let mut client = client().await;
        let hash = [5u8; 20];
        let p2sh = zcash_address::ZcashAddress::from_transparent_p2sh(network(&client).await, hash)
            .encode();

        let proposal = client
            .propose_send_with_op_return(&p2sh, Zatoshis::const_from_u64(100_000), data(), ACCOUNT)
            .await
            .unwrap();

        assert_eq!(proposal.recipient(), &TransparentAddress::ScriptHash(hash));
    }

    /// A shielded recipient is refused. Nothing is stored.
    #[tokio::test]
    async fn shielded_recipient_is_refused() {
        let mut client = client().await;
        let unified = {
            let mut wallet = client.wallet().write().await;
            let (_, address) = wallet
                .generate_unified_address(ReceiverSelection::orchard_only(), ACCOUNT)
                .unwrap();
            address.encode(&wallet.chain_type())
        };

        let result = client
            .propose_send_with_op_return(
                &unified,
                Zatoshis::const_from_u64(100_000),
                data(),
                ACCOUNT,
            )
            .await;

        assert!(matches!(
            result,
            Err(LightClientError::SendError(SendError::OpReturn(
                WalletError::OpReturnRecipientNotTransparent
            )))
        ));
        assert!(client.wallet().write().await.take_proposal().is_none());
    }

    /// Insufficient shielded funds fail at the deshield proposal.
    #[tokio::test]
    async fn insufficient_funds_fail_at_the_deshield() {
        let mut client = client().await;

        let result = client
            .propose_send_with_op_return(
                zingo_test_vectors::EXT_TADDR,
                Zatoshis::const_from_u64(5_000_000),
                data(),
                ACCOUNT,
            )
            .await;

        assert!(matches!(
            result,
            Err(LightClientError::SendError(SendError::ProposeSendError(_)))
        ));
    }

    /// Offline calculation refuses an OP_RETURN proposal and keeps it
    /// stored.
    #[tokio::test]
    async fn calculate_refuses_and_preserves_the_proposal() {
        let mut client = client().await;
        client
            .propose_send_with_op_return(
                zingo_test_vectors::EXT_TADDR,
                Zatoshis::const_from_u64(100_000),
                data(),
                ACCOUNT,
            )
            .await
            .unwrap();

        let result = client.calculate_stored_proposal().await;

        assert!(matches!(
            result,
            Err(LightClientError::SendError(
                SendError::OpReturnNotCalculable
            ))
        ));
        assert!(matches!(
            client.wallet().write().await.take_proposal(),
            Some(ZingoProposal::OpReturn(_))
        ));
    }
}