zakura-client-sqlite 0.1.0-rc0

An SQLite-based Zcash light client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
//! SQLite-backed implementation of [`ShardStore`] for note commitment trees.

use rusqlite::{self, OptionalExtension, named_params};
use std::{
    collections::BTreeSet,
    error, fmt,
    io::{self, Cursor},
    marker::PhantomData,
    num::NonZeroU32,
    ops::Range,
    sync::Arc,
};

use incrementalmerkletree::{Address, Hashable, Level, Position, Retention};
use shardtree::{
    LocatedPrunableTree, LocatedTree, PrunableTree, RetentionFlags,
    error::{QueryError, ShardTreeError},
    store::{Checkpoint, ShardStore, TreeState},
};

use zcash_client_backend::{
    data_api::{chain::CommitmentTreeRoot, wallet::TargetHeight},
    serialization::shardtree::{read_shard, write_shard},
};
use zcash_primitives::merkle_tree::HashSer;
use zcash_protocol::{ShieldedPool, consensus::BlockHeight};

use crate::{error::SqliteClientError, sapling_tree};

#[cfg(feature = "orchard")]
use {
    crate::{IRONWOOD_TABLES_PREFIX, ORCHARD_TABLES_PREFIX, ironwood_tree, orchard_tree},
    incrementalmerkletree::Marking,
    shardtree::{ShardTree, store::memory::MemoryShardStore},
    zcash_client_backend::data_api::ORCHARD_SHARD_HEIGHT,
};

use super::common::{TableConstants, table_constants};

/// Errors that can appear in SQLite-back [`ShardStore`] implementation operations.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Errors in deserializing stored shard data
    Serialization(io::Error),
    /// Errors encountered querying stored shard data
    Query(rusqlite::Error),
    /// Raised when the caller attempts to add a checkpoint at a block height where a checkpoint
    /// already exists, but the tree state being checkpointed or the marks removed at that
    /// checkpoint conflict with the existing tree state.
    CheckpointConflict {
        /// The block height of the conflicting checkpoint.
        checkpoint_id: BlockHeight,
        /// The checkpoint data that was attempted to be inserted.
        checkpoint: Checkpoint,
        /// The tree state already stored at that checkpoint height.
        extant_tree_state: TreeState,
        /// The marks-removed set already stored at that checkpoint height, if any.
        extant_marks_removed: Option<BTreeSet<Position>>,
    },
    /// Raised when attempting to add shard roots to the database that
    /// are discontinuous with the existing roots in the database.
    SubtreeDiscontinuity {
        /// The index range of the subtree roots that were attempted to be inserted.
        attempted_insertion_range: Range<u64>,
        /// The index range of subtree roots already present in the database.
        existing_range: Range<u64>,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            Error::Serialization(err) => write!(f, "Commitment tree serialization error: {err}"),
            Error::Query(err) => write!(f, "Commitment tree query or update error: {err}"),
            Error::CheckpointConflict {
                checkpoint_id,
                checkpoint,
                extant_tree_state,
                extant_marks_removed,
            } => {
                write!(
                    f,
                    "Conflict at checkpoint id {checkpoint_id}, tried to insert {checkpoint:?}, which is incompatible with existing state ({extant_tree_state:?}, {extant_marks_removed:?})"
                )
            }
            Error::SubtreeDiscontinuity {
                attempted_insertion_range,
                existing_range,
            } => {
                write!(
                    f,
                    "Attempted to write subtree roots with indices {attempted_insertion_range:?} which is discontinuous with existing subtree range {existing_range:?}",
                )
            }
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self {
            Error::Serialization(e) => Some(e),
            Error::Query(e) => Some(e),
            Error::CheckpointConflict { .. } => None,
            Error::SubtreeDiscontinuity { .. } => None,
        }
    }
}

/// An implementation of [`ShardStore`] backed by an SQLite database.
pub struct SqliteShardStore<C, H, const SHARD_HEIGHT: u8> {
    pub(crate) conn: C,
    table_prefix: &'static str,
    _hash_type: PhantomData<H>,
}

impl<C, H, const SHARD_HEIGHT: u8> SqliteShardStore<C, H, SHARD_HEIGHT> {
    const SHARD_ROOT_LEVEL: Level = Level::new(SHARD_HEIGHT);

    pub(crate) fn from_connection(
        conn: C,
        table_prefix: &'static str,
    ) -> Result<Self, rusqlite::Error> {
        Ok(SqliteShardStore {
            conn,
            table_prefix,
            _hash_type: PhantomData,
        })
    }
}

impl<'conn, 'a: 'conn, H: HashSer, const SHARD_HEIGHT: u8> ShardStore
    for SqliteShardStore<&'a rusqlite::Transaction<'conn>, H, SHARD_HEIGHT>
{
    type H = H;
    type CheckpointId = BlockHeight;
    type Error = Error;

    fn get_shard(
        &self,
        shard_root: Address,
    ) -> Result<Option<LocatedPrunableTree<Self::H>>, Self::Error> {
        get_shard(self.conn, self.table_prefix, shard_root)
    }

    fn last_shard(&self) -> Result<Option<LocatedPrunableTree<Self::H>>, Self::Error> {
        last_shard(self.conn, self.table_prefix, Self::SHARD_ROOT_LEVEL)
    }

    fn put_shard(&mut self, subtree: LocatedPrunableTree<Self::H>) -> Result<(), Self::Error> {
        put_shard(self.conn, self.table_prefix, subtree)
    }

    fn get_shard_roots(&self) -> Result<Vec<Address>, Self::Error> {
        get_shard_roots(self.conn, self.table_prefix, Self::SHARD_ROOT_LEVEL)
    }

    fn truncate_shards(&mut self, shard_index: u64) -> Result<(), Self::Error> {
        truncate_shards(self.conn, self.table_prefix, shard_index)
    }

    fn get_cap(&self) -> Result<PrunableTree<Self::H>, Self::Error> {
        get_cap(self.conn, self.table_prefix)
    }

    fn put_cap(&mut self, cap: PrunableTree<Self::H>) -> Result<(), Self::Error> {
        put_cap(self.conn, self.table_prefix, cap)
    }

    fn min_checkpoint_id(&self) -> Result<Option<Self::CheckpointId>, Self::Error> {
        min_checkpoint_id(self.conn, self.table_prefix)
    }

    fn max_checkpoint_id(&self) -> Result<Option<Self::CheckpointId>, Self::Error> {
        max_checkpoint_id(self.conn, self.table_prefix)
    }

    fn add_checkpoint(
        &mut self,
        checkpoint_id: Self::CheckpointId,
        checkpoint: Checkpoint,
    ) -> Result<(), Self::Error> {
        add_checkpoint(self.conn, self.table_prefix, checkpoint_id, checkpoint)
    }

    fn checkpoint_count(&self) -> Result<usize, Self::Error> {
        checkpoint_count(self.conn, self.table_prefix)
    }

    fn get_checkpoint_at_depth(
        &self,
        checkpoint_depth: usize,
    ) -> Result<Option<(Self::CheckpointId, Checkpoint)>, Self::Error> {
        get_checkpoint_at_depth(self.conn, self.table_prefix, checkpoint_depth)
            .map_err(Error::Query)
    }

    fn get_checkpoint(
        &self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<Option<Checkpoint>, Self::Error> {
        get_checkpoint(self.conn, self.table_prefix, *checkpoint_id)
    }

    fn with_checkpoints<F>(&mut self, limit: usize, callback: F) -> Result<(), Self::Error>
    where
        F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
    {
        with_checkpoints(self.conn, self.table_prefix, limit, callback)
    }

    fn for_each_checkpoint<F>(&self, limit: usize, callback: F) -> Result<(), Self::Error>
    where
        F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
    {
        with_checkpoints(self.conn, self.table_prefix, limit, callback)
    }

    fn update_checkpoint_with<F>(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
        update: F,
    ) -> Result<bool, Self::Error>
    where
        F: Fn(&mut Checkpoint) -> Result<(), Self::Error>,
    {
        update_checkpoint_with(self.conn, self.table_prefix, *checkpoint_id, update)
    }

    fn remove_checkpoint(&mut self, checkpoint_id: &Self::CheckpointId) -> Result<(), Self::Error> {
        remove_checkpoint(self.conn, self.table_prefix, *checkpoint_id)
    }

    fn add_retained_checkpoint(
        &mut self,
        checkpoint_id: Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        add_retained_checkpoint(self.conn, self.table_prefix, checkpoint_id)
    }

    fn remove_retained_checkpoint(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        remove_retained_checkpoint(self.conn, self.table_prefix, *checkpoint_id)
    }

    fn retained_checkpoints(&self) -> Result<BTreeSet<Self::CheckpointId>, Self::Error> {
        retained_checkpoints(self.conn, self.table_prefix)
    }

    fn truncate_checkpoints_retaining(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        truncate_checkpoints_retaining(self.conn, self.table_prefix, *checkpoint_id)
    }
}

impl<H: HashSer, const SHARD_HEIGHT: u8> ShardStore
    for SqliteShardStore<rusqlite::Connection, H, SHARD_HEIGHT>
{
    type H = H;
    type CheckpointId = BlockHeight;
    type Error = Error;

    fn get_shard(
        &self,
        shard_root: Address,
    ) -> Result<Option<LocatedPrunableTree<Self::H>>, Self::Error> {
        get_shard(&self.conn, self.table_prefix, shard_root)
    }

    fn last_shard(&self) -> Result<Option<LocatedPrunableTree<Self::H>>, Self::Error> {
        last_shard(&self.conn, self.table_prefix, Self::SHARD_ROOT_LEVEL)
    }

    fn put_shard(&mut self, subtree: LocatedPrunableTree<Self::H>) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        put_shard(&tx, self.table_prefix, subtree)?;
        tx.commit().map_err(Error::Query)?;
        Ok(())
    }

    fn get_shard_roots(&self) -> Result<Vec<Address>, Self::Error> {
        get_shard_roots(&self.conn, self.table_prefix, Self::SHARD_ROOT_LEVEL)
    }

    fn truncate_shards(&mut self, shard_index: u64) -> Result<(), Self::Error> {
        truncate_shards(&self.conn, self.table_prefix, shard_index)
    }

    fn get_cap(&self) -> Result<PrunableTree<Self::H>, Self::Error> {
        get_cap(&self.conn, self.table_prefix)
    }

    fn put_cap(&mut self, cap: PrunableTree<Self::H>) -> Result<(), Self::Error> {
        put_cap(&self.conn, self.table_prefix, cap)
    }

    fn min_checkpoint_id(&self) -> Result<Option<Self::CheckpointId>, Self::Error> {
        min_checkpoint_id(&self.conn, self.table_prefix)
    }

    fn max_checkpoint_id(&self) -> Result<Option<Self::CheckpointId>, Self::Error> {
        max_checkpoint_id(&self.conn, self.table_prefix)
    }

    fn add_checkpoint(
        &mut self,
        checkpoint_id: Self::CheckpointId,
        checkpoint: Checkpoint,
    ) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        add_checkpoint(&tx, self.table_prefix, checkpoint_id, checkpoint)?;
        tx.commit().map_err(Error::Query)
    }

    fn checkpoint_count(&self) -> Result<usize, Self::Error> {
        checkpoint_count(&self.conn, self.table_prefix)
    }

    fn get_checkpoint_at_depth(
        &self,
        checkpoint_depth: usize,
    ) -> Result<Option<(Self::CheckpointId, Checkpoint)>, Self::Error> {
        get_checkpoint_at_depth(&self.conn, self.table_prefix, checkpoint_depth)
            .map_err(Error::Query)
    }

    fn get_checkpoint(
        &self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<Option<Checkpoint>, Self::Error> {
        get_checkpoint(&self.conn, self.table_prefix, *checkpoint_id)
    }

    fn with_checkpoints<F>(&mut self, limit: usize, callback: F) -> Result<(), Self::Error>
    where
        F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
    {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        with_checkpoints(&tx, self.table_prefix, limit, callback)?;
        tx.commit().map_err(Error::Query)
    }

    fn for_each_checkpoint<F>(&self, limit: usize, callback: F) -> Result<(), Self::Error>
    where
        F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
    {
        let tx = self.conn.unchecked_transaction().map_err(Error::Query)?;
        with_checkpoints(&tx, self.table_prefix, limit, callback)?;
        // Here, we use `tx.rollback` as the semantics of this method is that the callback must
        // not mutate the data store.
        tx.rollback().map_err(Error::Query)
    }

    fn update_checkpoint_with<F>(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
        update: F,
    ) -> Result<bool, Self::Error>
    where
        F: Fn(&mut Checkpoint) -> Result<(), Self::Error>,
    {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        let result = update_checkpoint_with(&tx, self.table_prefix, *checkpoint_id, update)?;
        tx.commit().map_err(Error::Query)?;
        Ok(result)
    }

    fn remove_checkpoint(&mut self, checkpoint_id: &Self::CheckpointId) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        remove_checkpoint(&tx, self.table_prefix, *checkpoint_id)?;
        tx.commit().map_err(Error::Query)
    }

    fn add_retained_checkpoint(
        &mut self,
        checkpoint_id: Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        add_retained_checkpoint(&tx, self.table_prefix, checkpoint_id)?;
        tx.commit().map_err(Error::Query)
    }

    fn remove_retained_checkpoint(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        remove_retained_checkpoint(&tx, self.table_prefix, *checkpoint_id)?;
        tx.commit().map_err(Error::Query)
    }

    fn retained_checkpoints(&self) -> Result<BTreeSet<Self::CheckpointId>, Self::Error> {
        retained_checkpoints(&self.conn, self.table_prefix)
    }

    fn truncate_checkpoints_retaining(
        &mut self,
        checkpoint_id: &Self::CheckpointId,
    ) -> Result<(), Self::Error> {
        let tx = self.conn.transaction().map_err(Error::Query)?;
        truncate_checkpoints_retaining(&tx, self.table_prefix, *checkpoint_id)?;
        tx.commit().map_err(Error::Query)
    }
}

/// Returns the stored root hash of the completed subtree with the given index, as most
/// recently written by [`put_shard_roots`] or recorded by [`put_shard`], or `Ok(None)` if
/// no row exists for the subtree or its `root_hash` is unknown.
pub(crate) fn get_subtree_root<H: HashSer>(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    index: u64,
) -> Result<Option<H>, Error> {
    conn.query_row(
        &format!(
            "SELECT root_hash
             FROM {table_prefix}_tree_shards
             WHERE shard_index = :shard_index"
        ),
        named_params![":shard_index": index],
        |row| row.get::<_, Option<Vec<u8>>>(0),
    )
    .optional()
    .map_err(Error::Query)?
    .flatten()
    .map(|bytes| H::read(Cursor::new(bytes)).map_err(Error::Serialization))
    .transpose()
}

pub(crate) fn get_shard<H: HashSer>(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    shard_root_addr: Address,
) -> Result<Option<LocatedPrunableTree<H>>, Error> {
    conn.query_row(
        &format!(
            "SELECT shard_data, root_hash
             FROM {table_prefix}_tree_shards
             WHERE shard_index = :shard_index"
        ),
        named_params![":shard_index": shard_root_addr.index()],
        |row| Ok((row.get::<_, Vec<u8>>(0)?, row.get::<_, Option<Vec<u8>>>(1)?)),
    )
    .optional()
    .map_err(Error::Query)?
    .map(|(shard_data, root_hash)| {
        let shard_tree = read_shard(&mut Cursor::new(shard_data)).map_err(Error::Serialization)?;
        let located_tree =
            LocatedPrunableTree::from_parts(shard_root_addr, shard_tree).map_err(|e| {
                Error::Serialization(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Tree contained invalid data at address {e:?}"),
                ))
            })?;
        if let Some(root_hash_data) = root_hash {
            let root_hash = H::read(Cursor::new(root_hash_data)).map_err(Error::Serialization)?;
            Ok(located_tree.reannotate_root(Some(Arc::new(root_hash))))
        } else {
            Ok(located_tree)
        }
    })
    .transpose()
}

pub(crate) fn last_shard<H: HashSer>(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    shard_root_level: Level,
) -> Result<Option<LocatedPrunableTree<H>>, Error> {
    conn.query_row(
        &format!(
            "SELECT shard_index, shard_data
             FROM {table_prefix}_tree_shards
             ORDER BY shard_index DESC
             LIMIT 1"
        ),
        [],
        |row| {
            let shard_index: u64 = row.get(0)?;
            let shard_data: Vec<u8> = row.get(1)?;
            Ok((shard_index, shard_data))
        },
    )
    .optional()
    .map_err(Error::Query)?
    .map(|(shard_index, shard_data)| {
        let shard_root = Address::from_parts(shard_root_level, shard_index);
        let shard_tree = read_shard(&mut Cursor::new(shard_data)).map_err(Error::Serialization)?;
        LocatedPrunableTree::from_parts(shard_root, shard_tree).map_err(|e| {
            Error::Serialization(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Tree contained invalid data at address {e:?}"),
            ))
        })
    })
    .transpose()
}

/// Returns an error iff the proposed insertion range
/// for the tree shards would create a discontinuity
/// in the database.
#[tracing::instrument(skip(conn))]
fn check_shard_discontinuity(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    proposed_insertion_range: Range<u64>,
) -> Result<(), Error> {
    if let Ok((Some(stored_min), Some(stored_max))) = conn
        .query_row(
            &format!("SELECT MIN(shard_index), MAX(shard_index) FROM {table_prefix}_tree_shards"),
            [],
            |row| {
                let min = row.get::<_, Option<u64>>(0)?;
                let max = row.get::<_, Option<u64>>(1)?;
                Ok((min, max))
            },
        )
        .map_err(Error::Query)
    {
        // If the ranges overlap, or are directly adjacent, then we aren't creating a
        // discontinuity. We can check this by comparing their start-inclusive,
        // end-exclusive bounds:
        // - If `cur_start == ins_end` then the proposed insertion range is immediately
        //   before the current shards. If `cur_start > ins_end` then there is a gap.
        // - If `ins_start == cur_end` then the proposed insertion range is immediately
        //   after the current shards. If `ins_start > cur_end` then there is a gap.
        let (cur_start, cur_end) = (stored_min, stored_max + 1);
        let (ins_start, ins_end) = (proposed_insertion_range.start, proposed_insertion_range.end);
        if cur_start > ins_end || ins_start > cur_end {
            return Err(Error::SubtreeDiscontinuity {
                attempted_insertion_range: proposed_insertion_range,
                existing_range: cur_start..cur_end,
            });
        }
    }

    Ok(())
}

pub(crate) fn put_shard<H: HashSer>(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    subtree: LocatedPrunableTree<H>,
) -> Result<(), Error> {
    let subtree_root_hash = subtree
        .root()
        .annotation()
        .and_then(|ann| {
            ann.as_ref().map(|rc| {
                let mut root_hash = vec![];
                rc.write(&mut root_hash)?;
                Ok(root_hash)
            })
        })
        .transpose()
        .map_err(Error::Serialization)?;

    let mut subtree_data = vec![];
    write_shard(&mut subtree_data, subtree.root()).map_err(Error::Serialization)?;

    let shard_index = subtree.root_addr().index();

    check_shard_discontinuity(conn, table_prefix, shard_index..shard_index + 1)?;

    let mut stmt_put_shard = conn
        .prepare_cached(&format!(
            "INSERT INTO {table_prefix}_tree_shards (shard_index, root_hash, shard_data)
             VALUES (:shard_index, :root_hash, :shard_data)
             ON CONFLICT (shard_index) DO UPDATE
             SET root_hash = :root_hash,
             shard_data = :shard_data"
        ))
        .map_err(Error::Query)?;

    stmt_put_shard
        .execute(named_params![
            ":shard_index": shard_index,
            ":root_hash": subtree_root_hash,
            ":shard_data": subtree_data
        ])
        .map_err(Error::Query)?;

    Ok(())
}

pub(crate) fn get_shard_roots(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    shard_root_level: Level,
) -> Result<Vec<Address>, Error> {
    let mut stmt = conn
        .prepare(&format!(
            "SELECT shard_index FROM {table_prefix}_tree_shards ORDER BY shard_index"
        ))
        .map_err(Error::Query)?;
    let mut rows = stmt.query([]).map_err(Error::Query)?;

    let mut res = vec![];
    while let Some(row) = rows.next().map_err(Error::Query)? {
        res.push(Address::from_parts(
            shard_root_level,
            row.get(0).map_err(Error::Query)?,
        ));
    }
    Ok(res)
}

pub(crate) fn truncate_shards(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    shard_index: u64,
) -> Result<(), Error> {
    conn.execute(
        &format!("DELETE FROM {table_prefix}_tree_shards WHERE shard_index >= ?"),
        [shard_index],
    )
    .map_err(Error::Query)
    .map(|_| ())
}

#[tracing::instrument(skip(conn))]
pub(crate) fn get_cap<H: HashSer>(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
) -> Result<PrunableTree<H>, Error> {
    conn.query_row(
        &format!("SELECT cap_data FROM {table_prefix}_tree_cap"),
        [],
        |row| row.get::<_, Vec<u8>>(0),
    )
    .optional()
    .map_err(Error::Query)?
    .map_or_else(
        || Ok(PrunableTree::empty()),
        |cap_data| read_shard(&mut Cursor::new(cap_data)).map_err(Error::Serialization),
    )
}

#[tracing::instrument(skip(conn, cap))]
pub(crate) fn put_cap<H: HashSer>(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    cap: PrunableTree<H>,
) -> Result<(), Error> {
    let mut stmt = conn
        .prepare_cached(&format!(
            "INSERT INTO {table_prefix}_tree_cap (cap_id, cap_data)
             VALUES (0, :cap_data)
             ON CONFLICT (cap_id) DO UPDATE
             SET cap_data = :cap_data"
        ))
        .map_err(Error::Query)?;

    let mut cap_data = vec![];
    write_shard(&mut cap_data, &cap).map_err(Error::Serialization)?;
    stmt.execute([cap_data]).map_err(Error::Query)?;

    Ok(())
}

pub(crate) fn min_checkpoint_id(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
) -> Result<Option<BlockHeight>, Error> {
    conn.query_row(
        &format!("SELECT MIN(checkpoint_id) FROM {table_prefix}_tree_checkpoints"),
        [],
        |row| {
            row.get::<_, Option<u32>>(0)
                .map(|opt| opt.map(BlockHeight::from))
        },
    )
    .map_err(Error::Query)
}

pub(crate) fn max_checkpoint_id(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
) -> Result<Option<BlockHeight>, Error> {
    conn.query_row(
        &format!("SELECT MAX(checkpoint_id) FROM {table_prefix}_tree_checkpoints"),
        [],
        |row| {
            row.get::<_, Option<u32>>(0)
                .map(|opt| opt.map(BlockHeight::from))
        },
    )
    .map_err(Error::Query)
}

/// Returns the lowest retained checkpoint id that is at or above `floor`, or `None`
/// if the pool's checkpoint table contains no checkpoint within `[floor, ∞)`.
pub(crate) fn min_checkpoint_id_at_or_above(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    floor: BlockHeight,
) -> Result<Option<BlockHeight>, Error> {
    conn.query_row(
        &format!(
            "SELECT MIN(checkpoint_id) FROM {table_prefix}_tree_checkpoints
             WHERE checkpoint_id >= :floor"
        ),
        named_params![":floor": u32::from(floor)],
        |row| {
            row.get::<_, Option<u32>>(0)
                .map(|opt| opt.map(BlockHeight::from))
        },
    )
    .map_err(Error::Query)
}

/// Resets the note commitment tree with the given table prefix to contain only the roots of
/// subtrees completed at or below `truncation_height`, discarding all of its other state:
/// scanned shard contents, the cap, and all checkpoints.
///
/// This is the truncation outcome for a tree whose retained checkpoints all lie above the
/// truncation height: no checkpoint exists at or below that height for
/// `ShardTree::truncate_to_checkpoint` to target, so the tree's scanned contents postdate the
/// truncation point and must be discarded; the rescan of heights above that point re-creates
/// them. The roots of subtrees completed at or below the truncation height remain facts
/// about the retained portion of the chain, however, so they are preserved (in the form
/// fast sync would deliver them, via [`put_shard_roots`]); discarding them would leave the
/// wallet unable to construct witnesses spanning those subtrees until they had been
/// re-downloaded.
pub(crate) fn truncate_tree_to_subtree_roots<
    H: Hashable + HashSer + Clone + Eq,
    const DEPTH: u8,
    const SHARD_HEIGHT: u8,
>(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    truncation_height: BlockHeight,
) -> Result<(), ShardTreeError<Error>> {
    // Collect the roots of subtrees completed at or below the truncation height. Subtree end
    // heights increase with shard index, so these form a prefix of the shard sequence;
    // re-insertion via `put_shard_roots` requires contiguity from index zero, so collection
    // stops at the first shard for which no completed root is recorded.
    let roots = {
        let mut stmt = conn
            .prepare(&format!(
                "SELECT shard_index, subtree_end_height, root_hash
                 FROM {table_prefix}_tree_shards
                 WHERE subtree_end_height IS NOT NULL
                 AND subtree_end_height <= :truncation_height
                 AND root_hash IS NOT NULL
                 ORDER BY shard_index"
            ))
            .map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;

        let rows = stmt
            .query_map(
                named_params![":truncation_height": u32::from(truncation_height)],
                |row| {
                    Ok((
                        row.get::<_, u64>(0)?,
                        row.get::<_, u32>(1)?,
                        row.get::<_, Vec<u8>>(2)?,
                    ))
                },
            )
            .map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;

        let mut roots = vec![];
        for row in rows {
            let (shard_index, subtree_end_height, root_hash) =
                row.map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;
            if shard_index != u64::try_from(roots.len()).expect("vec length fits in u64") {
                break;
            }
            let root = H::read(Cursor::new(root_hash))
                .map_err(|e| ShardTreeError::Storage(Error::Serialization(e)))?;
            roots.push(CommitmentTreeRoot::from_parts(
                BlockHeight::from(subtree_end_height),
                root,
            ));
        }
        roots
    };

    conn.execute_batch(&format!(
        "DELETE FROM {table_prefix}_tree_checkpoint_marks_removed;
         DELETE FROM {table_prefix}_tree_checkpoints;
         DELETE FROM {table_prefix}_tree_shards;
         DELETE FROM {table_prefix}_tree_cap;"
    ))
    .map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;

    put_shard_roots::<H, DEPTH, SHARD_HEIGHT>(conn, table_prefix, 0, &roots)
}

pub(crate) fn add_checkpoint(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
    checkpoint: Checkpoint,
) -> Result<(), Error> {
    let extant_tree_state = conn
        .query_row(
            &format!(
                "SELECT position FROM {table_prefix}_tree_checkpoints WHERE checkpoint_id = :checkpoint_id"
            ),
            named_params![":checkpoint_id": u32::from(checkpoint_id),],
            |row| {
                row.get::<_, Option<u64>>(0).map(|opt| {
                    opt.map_or_else(
                        || TreeState::Empty,
                        |pos| TreeState::AtPosition(Position::from(pos)),
                    )
                })
            },
        )
        .optional()
        .map_err(Error::Query)?;

    match extant_tree_state {
        Some(current) => {
            if current != checkpoint.tree_state() {
                // If the checkpoint position for a given checkpoint identifier has changed, we treat
                // this as an error because the wallet should have detected a chain reorg and truncated
                // the tree.
                Err(Error::CheckpointConflict {
                    checkpoint_id,
                    checkpoint,
                    extant_tree_state: current,
                    extant_marks_removed: None,
                })
            } else {
                // if the existing spends are the same, we can skip the insert; if the
                // existing spends have changed, this is also a conflict.
                let marks_removed = get_marks_removed(conn, table_prefix, checkpoint_id)?;
                if &marks_removed == checkpoint.marks_removed() {
                    Ok(())
                } else {
                    Err(Error::CheckpointConflict {
                        checkpoint_id,
                        checkpoint,
                        extant_tree_state: current,
                        extant_marks_removed: Some(marks_removed),
                    })
                }
            }
        }
        None => {
            let mut stmt_insert_checkpoint = conn
                .prepare_cached(&format!(
                    "INSERT INTO {table_prefix}_tree_checkpoints (checkpoint_id, position)
                     VALUES (:checkpoint_id, :position)"
                ))
                .map_err(Error::Query)?;

            stmt_insert_checkpoint
                .execute(named_params![
                    ":checkpoint_id": u32::from(checkpoint_id),
                    ":position": checkpoint.position().map(u64::from)
                ])
                .map_err(Error::Query)?;

            let mut stmt_insert_mark_removed = conn
                .prepare_cached(&format!(
                    "INSERT INTO {table_prefix}_tree_checkpoint_marks_removed (checkpoint_id, mark_removed_position)
                     VALUES (:checkpoint_id, :position)"
                ))
                .map_err(Error::Query)?;

            for pos in checkpoint.marks_removed() {
                stmt_insert_mark_removed
                    .execute(named_params![
                        ":checkpoint_id": u32::from(checkpoint_id),
                        ":position": u64::from(*pos)
                    ])
                    .map_err(Error::Query)?;
            }

            Ok(())
        }
    }
}

pub(crate) fn checkpoint_count(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
) -> Result<usize, Error> {
    conn.query_row(
        &format!("SELECT COUNT(*) FROM {table_prefix}_tree_checkpoints"),
        [],
        |row| row.get::<_, usize>(0),
    )
    .map_err(Error::Query)
}

fn get_marks_removed(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<BTreeSet<Position>, Error> {
    let mut stmt = conn
        .prepare_cached(&format!(
            "SELECT mark_removed_position
            FROM {table_prefix}_tree_checkpoint_marks_removed
            WHERE checkpoint_id = ?"
        ))
        .map_err(Error::Query)?;
    let mark_removed_rows = stmt
        .query([u32::from(checkpoint_id)])
        .map_err(Error::Query)?;

    mark_removed_rows
        .mapped(|row| row.get::<_, u64>(0).map(Position::from))
        .collect::<Result<BTreeSet<_>, _>>()
        .map_err(Error::Query)
}

pub(crate) fn get_checkpoint(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<Option<Checkpoint>, Error> {
    let checkpoint_position = conn
        .query_row(
            &format!(
                "SELECT position
                 FROM {table_prefix}_tree_checkpoints
                 WHERE checkpoint_id = ?"
            ),
            [u32::from(checkpoint_id)],
            |row| {
                row.get::<_, Option<u64>>(0)
                    .map(|opt| opt.map(Position::from))
            },
        )
        .optional()
        .map_err(Error::Query)?;

    checkpoint_position
        .map(|pos_opt| {
            Ok(Checkpoint::from_parts(
                pos_opt.map_or(TreeState::Empty, TreeState::AtPosition),
                get_marks_removed(conn, table_prefix, checkpoint_id)?,
            ))
        })
        .transpose()
}

pub(crate) fn get_max_checkpointed_height(
    conn: &rusqlite::Connection,
    protocol: ShieldedPool,
    target_height: TargetHeight,
    min_confirmations: NonZeroU32,
) -> Result<Option<BlockHeight>, SqliteClientError> {
    let TableConstants { table_prefix, .. } = table_constants::<SqliteClientError>(protocol)?;
    let max_checkpoint_height = target_height - u32::from(min_confirmations);

    // We exclude from consideration all checkpoints having heights greater than the maximum
    // checkpoint height. The checkpoint depth is the number of excluded checkpoints + 1.
    conn.query_row(
        &format!(
            "SELECT checkpoint_id
             FROM {table_prefix}_tree_checkpoints
             WHERE checkpoint_id <= :max_checkpoint_height
             ORDER BY checkpoint_id DESC
             LIMIT 1",
        ),
        named_params![":max_checkpoint_height": u32::from(max_checkpoint_height)],
        |row| row.get::<_, u32>(0).map(BlockHeight::from),
    )
    .optional()
    .map_err(SqliteClientError::from)
}

pub(crate) fn get_checkpoint_at_depth(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    checkpoint_depth: usize,
) -> Result<Option<(BlockHeight, Checkpoint)>, rusqlite::Error> {
    let checkpoint_parts = conn
        .query_row(
            &format!(
                "SELECT checkpoint_id, position
                FROM {table_prefix}_tree_checkpoints
                ORDER BY checkpoint_id DESC
                LIMIT 1
                OFFSET :offset",
            ),
            named_params![":offset": checkpoint_depth],
            |row| {
                let checkpoint_id: u32 = row.get(0)?;
                let position: Option<u64> = row.get(1)?;
                Ok((
                    BlockHeight::from(checkpoint_id),
                    position.map(Position::from),
                ))
            },
        )
        .optional()?;

    checkpoint_parts
        .map(|(checkpoint_id, pos_opt)| {
            let mut stmt = conn.prepare_cached(&format!(
                "SELECT mark_removed_position
                    FROM {table_prefix}_tree_checkpoint_marks_removed
                    WHERE checkpoint_id = ?"
            ))?;
            let mark_removed_rows = stmt.query([u32::from(checkpoint_id)])?;

            let marks_removed = mark_removed_rows
                .mapped(|row| row.get::<_, u64>(0).map(Position::from))
                .collect::<Result<BTreeSet<_>, _>>()?;

            Ok((
                checkpoint_id,
                Checkpoint::from_parts(
                    pos_opt.map_or(TreeState::Empty, TreeState::AtPosition),
                    marks_removed,
                ),
            ))
        })
        .transpose()
}

pub(crate) fn with_checkpoints<F>(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    limit: usize,
    mut callback: F,
) -> Result<(), Error>
where
    F: FnMut(&BlockHeight, &Checkpoint) -> Result<(), Error>,
{
    let mut stmt_get_checkpoints = conn
        .prepare_cached(&format!(
            "SELECT checkpoint_id, position
            FROM {table_prefix}_tree_checkpoints
            ORDER BY position
            LIMIT :limit"
        ))
        .map_err(Error::Query)?;

    let mut stmt_get_checkpoint_marks_removed = conn
        .prepare_cached(&format!(
            "SELECT mark_removed_position
            FROM {table_prefix}_tree_checkpoint_marks_removed
            WHERE checkpoint_id = :checkpoint_id"
        ))
        .map_err(Error::Query)?;

    let mut rows = stmt_get_checkpoints
        .query(named_params![":limit": limit])
        .map_err(Error::Query)?;

    while let Some(row) = rows.next().map_err(Error::Query)? {
        let checkpoint_id = row.get::<_, u32>(0).map_err(Error::Query)?;
        let tree_state = row
            .get::<_, Option<u64>>(1)
            .map(|opt| opt.map_or_else(|| TreeState::Empty, |p| TreeState::AtPosition(p.into())))
            .map_err(Error::Query)?;

        let mark_removed_rows = stmt_get_checkpoint_marks_removed
            .query(named_params![":checkpoint_id": checkpoint_id])
            .map_err(Error::Query)?;

        let marks_removed = mark_removed_rows
            .mapped(|row| row.get::<_, u64>(0).map(Position::from))
            .collect::<Result<BTreeSet<_>, _>>()
            .map_err(Error::Query)?;

        callback(
            &BlockHeight::from(checkpoint_id),
            &Checkpoint::from_parts(tree_state, marks_removed),
        )?
    }

    Ok(())
}

pub(crate) fn update_checkpoint_with<F>(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
    update: F,
) -> Result<bool, Error>
where
    F: Fn(&mut Checkpoint) -> Result<(), Error>,
{
    if let Some(mut c) = get_checkpoint(conn, table_prefix, checkpoint_id)? {
        update(&mut c)?;
        remove_checkpoint(conn, table_prefix, checkpoint_id)?;
        add_checkpoint(conn, table_prefix, checkpoint_id, c)?;
        Ok(true)
    } else {
        Ok(false)
    }
}

pub(crate) fn remove_checkpoint(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<(), Error> {
    // cascading delete here obviates the need to manually delete from
    // `tree_checkpoint_marks_removed`
    let mut stmt_delete_checkpoint = conn
        .prepare_cached(&format!(
            "DELETE FROM {table_prefix}_tree_checkpoints
             WHERE checkpoint_id = :checkpoint_id"
        ))
        .map_err(Error::Query)?;

    stmt_delete_checkpoint
        .execute(named_params![":checkpoint_id": u32::from(checkpoint_id),])
        .map_err(Error::Query)?;

    Ok(())
}

pub(crate) fn add_retained_checkpoint(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<(), Error> {
    conn.prepare_cached(&format!(
        "INSERT OR IGNORE INTO {table_prefix}_tree_retained_checkpoints (checkpoint_id)
         VALUES (:checkpoint_id)"
    ))
    .map_err(Error::Query)?
    .execute(named_params![":checkpoint_id": u32::from(checkpoint_id)])
    .map_err(Error::Query)?;

    Ok(())
}

pub(crate) fn remove_retained_checkpoint(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<(), Error> {
    conn.prepare_cached(&format!(
        "DELETE FROM {table_prefix}_tree_retained_checkpoints
         WHERE checkpoint_id = :checkpoint_id"
    ))
    .map_err(Error::Query)?
    .execute(named_params![":checkpoint_id": u32::from(checkpoint_id)])
    .map_err(Error::Query)?;

    Ok(())
}

pub(crate) fn retained_checkpoints(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
) -> Result<BTreeSet<BlockHeight>, Error> {
    // The retained-checkpoints table is created by a dedicated migration. Tree operations may run
    // against a schema that predates that migration (e.g. when a migration test drives the tree at
    // an intermediate state); such a wallet simply has no retained checkpoints, so report an empty
    // set rather than failing on the missing table.
    let table_name = format!("{table_prefix}_tree_retained_checkpoints");
    let table_exists = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = :table_name",
            named_params![":table_name": table_name],
            |_| Ok(()),
        )
        .optional()
        .map_err(Error::Query)?
        .is_some();
    if !table_exists {
        return Ok(BTreeSet::new());
    }

    let mut stmt = conn
        .prepare_cached(&format!("SELECT checkpoint_id FROM {table_name}"))
        .map_err(Error::Query)?;
    let rows = stmt.query([]).map_err(Error::Query)?;

    rows.mapped(|row| row.get::<_, u32>(0).map(BlockHeight::from))
        .collect::<Result<BTreeSet<_>, _>>()
        .map_err(Error::Query)
}

pub(crate) fn truncate_checkpoints_retaining(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    checkpoint_id: BlockHeight,
) -> Result<(), Error> {
    // cascading delete here obviates the need to manually delete from
    // `<protocol>_tree_checkpoint_marks_removed`
    conn.execute(
        &format!("DELETE FROM {table_prefix}_tree_checkpoints WHERE checkpoint_id > ?"),
        [u32::from(checkpoint_id)],
    )
    .map_err(Error::Query)?;

    // we do however need to manually delete any marks associated with the retained checkpoint
    conn.execute(
        &format!(
            "DELETE FROM {table_prefix}_tree_checkpoint_marks_removed WHERE checkpoint_id = ?"
        ),
        [u32::from(checkpoint_id)],
    )
    .map_err(Error::Query)?;

    Ok(())
}

#[tracing::instrument(skip(conn, roots))]
pub(crate) fn put_shard_roots<
    H: Hashable + HashSer + Clone + Eq,
    const DEPTH: u8,
    const SHARD_HEIGHT: u8,
>(
    conn: &rusqlite::Transaction<'_>,
    table_prefix: &'static str,
    start_index: u64,
    roots: &[CommitmentTreeRoot<H>],
) -> Result<(), ShardTreeError<Error>> {
    if roots.is_empty() {
        // nothing to do
        return Ok(());
    }

    // We treat the cap as a tree with `DEPTH - SHARD_HEIGHT` levels, so that we can make a
    // batch insertion of root data using `Position::from(start_index)` as the starting position
    // and treating the roots as level-0 leaves.
    #[derive(Clone, Debug, PartialEq, Eq)]
    struct LevelShifter<H, const SHARD_HEIGHT: u8>(H);
    impl<H: Hashable, const SHARD_HEIGHT: u8> Hashable for LevelShifter<H, SHARD_HEIGHT> {
        fn empty_leaf() -> Self {
            Self(H::empty_root(SHARD_HEIGHT.into()))
        }

        fn combine(level: Level, a: &Self, b: &Self) -> Self {
            Self(H::combine(level + SHARD_HEIGHT, &a.0, &b.0))
        }

        fn empty_root(level: Level) -> Self
        where
            Self: Sized,
        {
            Self(H::empty_root(level + SHARD_HEIGHT))
        }
    }
    impl<H: HashSer, const SHARD_HEIGHT: u8> HashSer for LevelShifter<H, SHARD_HEIGHT> {
        fn read<R: io::Read>(reader: R) -> io::Result<Self>
        where
            Self: Sized,
        {
            H::read(reader).map(Self)
        }

        fn write<W: io::Write>(&self, writer: W) -> io::Result<()> {
            self.0.write(writer)
        }
    }

    let cap = LocatedTree::from_parts(
        Address::from_parts((DEPTH - SHARD_HEIGHT).into(), 0),
        get_cap::<LevelShifter<H, SHARD_HEIGHT>>(conn, table_prefix)
            .map_err(ShardTreeError::Storage)?,
    )
    .map_err(|e| {
        ShardTreeError::Storage(Error::Serialization(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Note commitment tree cap was invalid at address {e:?}"),
        )))
    })?;

    let insert_into_cap = tracing::info_span!("insert_into_cap").entered();
    let cap_result = cap
        .batch_insert::<(), _>(
            Position::from(start_index),
            roots
                .iter()
                .map(|r| (LevelShifter(r.root_hash().clone()), Retention::Reference)),
        )
        .map_err(ShardTreeError::Insert)?
        .expect("slice of inserted roots was verified to be nonempty");
    drop(insert_into_cap);

    put_cap(conn, table_prefix, cap_result.subtree.take_root()).map_err(ShardTreeError::Storage)?;

    check_shard_discontinuity(
        conn,
        table_prefix,
        start_index..start_index + (roots.len() as u64),
    )
    .map_err(ShardTreeError::Storage)?;

    // We want to avoid deserializing the subtree just to annotate its root node, so we simply
    // cache the downloaded root alongside of any already-persisted subtree. We will update the
    // subtree data itself by reannotating the root node of the tree, handling conflicts, at
    // the time that we deserialize the tree.
    let mut stmt = conn
        .prepare_cached(&format!(
            "INSERT INTO {table_prefix}_tree_shards (shard_index, subtree_end_height, root_hash, shard_data)
            VALUES (:shard_index, :subtree_end_height, :root_hash, :shard_data)
            ON CONFLICT (shard_index) DO UPDATE
            SET subtree_end_height = :subtree_end_height, root_hash = :root_hash"
        ))
        .map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;

    let put_roots = tracing::info_span!("write_shards").entered();
    for (root, i) in roots.iter().zip(0u64..) {
        // The `shard_data` value will only be used in the case that no tree already exists.
        let mut shard_data: Vec<u8> = vec![];
        let tree = PrunableTree::leaf((root.root_hash().clone(), RetentionFlags::EPHEMERAL));
        write_shard(&mut shard_data, &tree)
            .map_err(|e| ShardTreeError::Storage(Error::Serialization(e)))?;

        let mut root_hash_data: Vec<u8> = vec![];
        root.root_hash()
            .write(&mut root_hash_data)
            .map_err(|e| ShardTreeError::Storage(Error::Serialization(e)))?;

        stmt.execute(named_params![
            ":shard_index": start_index + i,
            ":subtree_end_height": u32::from(root.subtree_end_height()),
            ":root_hash": root_hash_data,
            ":shard_data": shard_data,
        ])
        .map_err(|e| ShardTreeError::Storage(Error::Query(e)))?;
    }
    drop(put_roots);

    Ok(())
}

pub(crate) fn check_witnesses(
    conn: &rusqlite::Transaction<'_>,
    anchor_height: BlockHeight,
) -> Result<Vec<Range<BlockHeight>>, SqliteClientError> {
    let wallet_birthday = super::wallet_birthday(conn)?.ok_or(SqliteClientError::AccountUnknown)?;
    let unspent_sapling_note_meta =
        super::sapling::select_unspent_note_meta(conn, wallet_birthday, anchor_height)?;

    let mut scan_ranges = vec![];
    let mut sapling_incomplete = vec![];
    let sapling_tree = sapling_tree(conn)?;
    for m in unspent_sapling_note_meta.iter() {
        match sapling_tree.witness_at_checkpoint_depth(m.commitment_tree_position(), 0) {
            Ok(_) => {}
            Err(ShardTreeError::Query(QueryError::TreeIncomplete(mut addrs))) => {
                sapling_incomplete.append(&mut addrs);
            }
            Err(other) => {
                return Err(SqliteClientError::CommitmentTree(other));
            }
        }
    }

    for addr in sapling_incomplete {
        let range = super::get_block_range(conn, ShieldedPool::Sapling, addr)?;
        scan_ranges.extend(range);
    }

    #[cfg(feature = "orchard")]
    {
        let unspent_orchard_note_meta =
            super::orchard::select_unspent_note_meta(conn, wallet_birthday, anchor_height)?;
        let mut orchard_incomplete = vec![];
        let orchard_tree = orchard_tree(conn)?;
        for m in unspent_orchard_note_meta.iter() {
            match orchard_tree.witness_at_checkpoint_depth(m.commitment_tree_position(), 0) {
                Ok(_) => {}
                Err(ShardTreeError::Query(QueryError::TreeIncomplete(mut addrs))) => {
                    orchard_incomplete.append(&mut addrs);
                }
                Err(other) => {
                    return Err(SqliteClientError::CommitmentTree(other));
                }
            }
        }

        for addr in orchard_incomplete {
            let range = super::get_block_range(conn, ShieldedPool::Orchard, addr)?;
            scan_ranges.extend(range);
        }

        let unspent_ironwood_note_meta = super::common::select_unspent_note_meta(
            conn,
            ShieldedPool::Ironwood,
            wallet_birthday,
            anchor_height,
        )?;
        let mut ironwood_incomplete = vec![];
        let ironwood_tree = ironwood_tree(conn)?;
        for m in unspent_ironwood_note_meta.iter() {
            match ironwood_tree.witness_at_checkpoint_depth(m.commitment_tree_position(), 0) {
                Ok(_) => {}
                Err(ShardTreeError::Query(QueryError::TreeIncomplete(mut addrs))) => {
                    ironwood_incomplete.append(&mut addrs);
                }
                Err(other) => {
                    return Err(SqliteClientError::CommitmentTree(other));
                }
            }
        }

        for addr in ironwood_incomplete {
            let range = super::get_block_range(conn, ShieldedPool::Ironwood, addr)?;
            scan_ranges.extend(range);
        }
    }

    Ok(scan_ranges)
}

/// Generate Orchard Merkle witnesses at a historical height.
///
/// Loads the wallet's Orchard shard data into an ephemeral in-memory
/// [`MemoryShardStore`], inserts the provided frontier at that height as a
/// checkpoint, and generates a witness for each of the given note positions.
///
/// It is assumed that the caller provides the valid frontier at the given height.
///
/// How it works:
/// To construct witnesses at a historical height, we need:
/// 1. Authentication path within each note's shard — the scanner marks the
///    wallet's notes as MARKED, preventing them and their siblings within a
///    shard from being pruned.
/// 2. Cap — the upper tree above the shard level.
/// 3. Frontier — the right edge at the historical height. It lets ShardTree
///    know exactly where the tree ended at that height.
///
/// The wallet automatically prunes the tree after PRUNING_DEPTH checkpoints.
/// These three components are sufficient to reconstruct the tree structure
/// needed for witness generation even after pruning has occurred.
///
/// The wallet DB is strictly read-only. Shard data is read, decoded, and
/// inserted into an ephemeral in-memory [`ShardStore`] to avoid tampering with
/// the primary wallet DB.
///
/// Example application: token holder voting. The wallet tree may have advanced past
/// the historical height, but we need witnesses anchored at that frontier.
///
/// # Errors
///
/// - [`SqliteClientError::CommitmentTree`] if reading the wallet's shard or
///   cap data fails, or if the shard data reconstructed from the wallet is
///   internally inconsistent at a node the computation requires.
/// - [`SqliteClientError::HistoricalFrontierInvalid`] if `frontier_at_height`
///   is inconsistent with the shard data reconstructed from the wallet.
/// - [`SqliteClientError::HistoricalWitnessUnavailable`] if a witness cannot
///   be generated for one of the requested positions at `height` (most
///   commonly because the wallet has not yet synced through that height).
#[cfg(feature = "orchard")]
pub(crate) fn generate_orchard_witnesses_at_historical_height(
    conn: &rusqlite::Connection,
    note_positions: &[Position],
    frontier_at_height: incrementalmerkletree::frontier::NonEmptyFrontier<
        orchard::tree::MerkleHashOrchard,
    >,
    height: BlockHeight,
) -> Result<
    Vec<
        incrementalmerkletree::MerklePath<
            orchard::tree::MerkleHashOrchard,
            { orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
        >,
    >,
    SqliteClientError,
> {
    generate_orchard_like_witnesses_at_historical_height(
        conn,
        ORCHARD_TABLES_PREFIX,
        note_positions,
        frontier_at_height,
        height,
    )
}

/// Generates Ironwood Merkle witnesses at a historical height.
///
/// This is identical to [`generate_orchard_witnesses_at_historical_height`],
/// except that it reconstructs witness paths from the Ironwood shard tables.
#[cfg(feature = "orchard")]
pub(crate) fn generate_ironwood_witnesses_at_historical_height(
    conn: &rusqlite::Connection,
    note_positions: &[Position],
    frontier_at_height: incrementalmerkletree::frontier::NonEmptyFrontier<
        orchard::tree::MerkleHashOrchard,
    >,
    height: BlockHeight,
) -> Result<
    Vec<
        incrementalmerkletree::MerklePath<
            orchard::tree::MerkleHashOrchard,
            { orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
        >,
    >,
    SqliteClientError,
> {
    generate_orchard_like_witnesses_at_historical_height(
        conn,
        IRONWOOD_TABLES_PREFIX,
        note_positions,
        frontier_at_height,
        height,
    )
}

#[cfg(feature = "orchard")]
fn generate_orchard_like_witnesses_at_historical_height(
    conn: &rusqlite::Connection,
    table_prefix: &'static str,
    note_positions: &[Position],
    frontier_at_height: incrementalmerkletree::frontier::NonEmptyFrontier<
        orchard::tree::MerkleHashOrchard,
    >,
    height: BlockHeight,
) -> Result<
    Vec<
        incrementalmerkletree::MerklePath<
            orchard::tree::MerkleHashOrchard,
            { orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
        >,
    >,
    SqliteClientError,
> {
    // `get_shard_roots` returns addresses ordered by shard index, matching the
    // ascending insertion order required by `MemoryShardStore::put_shard`.
    // Storage errors flow through `From<ShardTreeError<commitment_tree::Error>>`
    // into `SqliteClientError::CommitmentTree`.
    let mut store = MemoryShardStore::<orchard::tree::MerkleHashOrchard, BlockHeight>::empty();
    let shard_root_level = Level::new(ORCHARD_SHARD_HEIGHT);
    let shard_roots =
        get_shard_roots(conn, table_prefix, shard_root_level).map_err(ShardTreeError::Storage)?;
    for shard_root in shard_roots {
        if let Some(shard) =
            get_shard::<orchard::tree::MerkleHashOrchard>(conn, table_prefix, shard_root)
                .map_err(ShardTreeError::Storage)?
        {
            store.put_shard(shard).expect("put_shard is infallible");
        }
    }
    let cap = get_cap::<orchard::tree::MerkleHashOrchard>(conn, table_prefix)
        .map_err(ShardTreeError::Storage)?;
    store.put_cap(cap).expect("put_cap is infallible");

    // Only one checkpoint is needed (the historical frontier), but `ShardTree`
    // requires a nonzero checkpoint limit.
    //
    // Pruning-safety invariant: `MemoryShardStore::empty()` starts with zero
    // checkpoints in its internal `BTreeMap`, and the only mutations above
    // (`put_shard` / `put_cap`) do not touch that map (the `CHECKPOINT`
    // retention flags stored *inside* shard leaves are independent of the
    // store's `checkpoint_count()`). So when `insert_frontier_nodes` below
    // calls `add_checkpoint` exactly once and then `prune_excess_checkpoints`,
    // we have `1 > 1 == false` and the freshly inserted historical checkpoint
    // is NOT pruned away before `witness_at_checkpoint_id` reads it.
    //
    // If this code is changed to (a) pre-load wallet checkpoints into the
    // in-memory store, (b) add an extra `add_checkpoint` call here, or
    // (c) drop `max_checkpoints` below `1`, the historical checkpoint will be
    // pruned immediately and witness generation will return
    // `HistoricalWitnessUnavailable` at the `witness_at_checkpoint_id` call
    // below. The `witnesses_at_historical_height_with_many_wallet_checkpoints`
    // test in `mod tests` exists to catch exactly that regression.
    let mut tree =
        ShardTree::<_, { orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 }, ORCHARD_SHARD_HEIGHT>::new(
            store, 1,
        );

    // Insert the frontier. `Retention::Checkpoint` causes `ShardTree` to
    // register a checkpoint at `height` internally; the pruning-safety
    // invariant above guarantees it survives the `prune_excess_checkpoints`
    // pass that runs at the end of `insert_frontier_nodes`.
    //
    // `MemoryShardStore::Error` is `Infallible`, so the only variants of
    // `ShardTreeError` we can observe are `Insert` (a caller-supplied
    // frontier inconsistent with the loaded shards) and `Query` (an
    // inconsistency in the loaded shards themselves).
    tree.insert_frontier_nodes(
        frontier_at_height,
        Retention::Checkpoint {
            id: height,
            marking: Marking::None,
        },
    )
    .map_err(|e| match e {
        ShardTreeError::Insert(e) => SqliteClientError::HistoricalFrontierInvalid(e),
        ShardTreeError::Query(q) => SqliteClientError::CommitmentTree(ShardTreeError::Query(q)),
        ShardTreeError::Storage(inf) => match inf {},
    })?;

    // Generate a witness per note position. Any `Query` failure (or a `None`
    // result) at this stage means the tree reconstructed from the wallet's
    // shards does not contain enough information to compute a witness at
    // `height`; we surface that as `HistoricalWitnessUnavailable` so the
    // caller can either sync further or stop requesting that position.
    let mut witnesses = Vec::with_capacity(note_positions.len());
    for &pos in note_positions {
        let merkle_path = tree
            .witness_at_checkpoint_id(pos, &height)
            .map_err(|e| match e {
                ShardTreeError::Query(_) => SqliteClientError::HistoricalWitnessUnavailable {
                    position: pos,
                    height,
                },
                ShardTreeError::Insert(i) => {
                    SqliteClientError::CommitmentTree(ShardTreeError::Insert(i))
                }
                ShardTreeError::Storage(inf) => match inf {},
            })?
            .ok_or(SqliteClientError::HistoricalWitnessUnavailable {
                position: pos,
                height,
            })?;

        witnesses.push(merkle_path);
    }

    Ok(witnesses)
}

#[cfg(test)]
mod tests {
    use tempfile::NamedTempFile;

    use incrementalmerkletree::{Marking, Position, Retention};
    use incrementalmerkletree_testing::{
        check_append, check_checkpoint_rewind, check_remove_mark, check_rewind_remove_mark,
        check_root_hashes, check_witness_consistency, check_witnesses,
    };
    use shardtree::{ShardTree, error::ShardTreeError, store::ShardStore};
    use zcash_client_backend::data_api::{
        WalletCommitmentTrees,
        chain::CommitmentTreeRoot,
        testing::{pool::ShieldedPoolTester, sapling::SaplingPoolTester},
    };
    use zcash_protocol::consensus::{BlockHeight, Network};

    use super::SqliteShardStore;
    use crate::{
        WalletDb,
        testing::{
            db::{test_clock, test_rng},
            pool::ShieldedPoolPersistence,
        },
        wallet::init::WalletMigrator,
    };
    // Used only by the orchard-gated `HistoricalWitnessGenerator` type alias below.
    use std::collections::BTreeSet;
    #[cfg(feature = "orchard")]
    use {
        crate::error::SqliteClientError, ::orchard::tree::MerkleHashOrchard,
        incrementalmerkletree::frontier::Frontier, rand::SeedableRng, rand_chacha::ChaChaRng,
        zcash_client_backend::data_api::ORCHARD_SHARD_HEIGHT,
    };

    fn new_tree<T: ShieldedPoolTester + ShieldedPoolPersistence>(
        m: usize,
    ) -> ShardTree<SqliteShardStore<rusqlite::Connection, String, 3>, 4, 3> {
        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();
        data_file.keep().unwrap();

        WalletMigrator::new().init_or_migrate(&mut db_data).unwrap();
        let store =
            SqliteShardStore::<_, String, 3>::from_connection(db_data.conn, T::TABLES_PREFIX)
                .unwrap();
        ShardTree::new(store, m)
    }

    fn check_retained_checkpoints(
        mut tree: ShardTree<SqliteShardStore<rusqlite::Connection, String, 3>, 4, 3>,
    ) {
        let h1 = BlockHeight::from(100);
        let h2 = BlockHeight::from(200);

        assert!(tree.store().retained_checkpoints().unwrap().is_empty());

        tree.ensure_retained(h1).unwrap();
        tree.ensure_retained(h2).unwrap();
        // Retaining an already-retained checkpoint is idempotent.
        tree.ensure_retained(h1).unwrap();
        assert_eq!(
            tree.store().retained_checkpoints().unwrap(),
            BTreeSet::from([h1, h2])
        );

        tree.remove_retained_checkpoint(&h1).unwrap();
        assert_eq!(
            tree.store().retained_checkpoints().unwrap(),
            BTreeSet::from([h2])
        );
    }

    #[cfg(feature = "orchard")]
    mod orchard {
        use super::new_tree;
        use zcash_client_backend::data_api::testing::orchard::OrchardPoolTester;

        #[test]
        fn append() {
            super::check_append(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn root_hashes() {
            super::check_root_hashes(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn witnesses() {
            super::check_witnesses(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn witness_consistency() {
            super::check_witness_consistency(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn checkpoint_rewind() {
            super::check_checkpoint_rewind(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn remove_mark() {
            super::check_remove_mark(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn rewind_remove_mark() {
            super::check_rewind_remove_mark(new_tree::<OrchardPoolTester>);
        }

        #[test]
        fn witnesses_at_historical_height() {
            super::witnesses_at_historical_height()
        }

        #[test]
        fn ironwood_witnesses_at_historical_height() {
            super::ironwood_witnesses_at_historical_height()
        }

        #[test]
        fn witnesses_at_historical_height_with_many_wallet_checkpoints() {
            super::witnesses_at_historical_height_with_many_wallet_checkpoints()
        }

        #[test]
        fn put_shard_roots() {
            super::put_shard_roots::<OrchardPoolTester>()
        }

        #[test]
        fn retained_checkpoints() {
            super::check_retained_checkpoints(super::new_tree::<OrchardPoolTester>(10));
        }
    }

    #[test]
    fn sapling_append() {
        check_append(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_retained_checkpoints() {
        check_retained_checkpoints(new_tree::<SaplingPoolTester>(10));
    }

    #[test]
    fn remove_retained_checkpoints_below() {
        let data_file = NamedTempFile::new().unwrap();
        let mut db = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();
        WalletMigrator::new().init_or_migrate(&mut db).unwrap();

        db.with_sapling_tree_mut(|tree| {
            for h in [100u32, 200, 300] {
                tree.ensure_retained(BlockHeight::from(h))?;
            }
            Ok::<_, ShardTreeError<_>>(())
        })
        .unwrap();

        #[cfg(feature = "orchard")]
        {
            db.with_orchard_tree_mut(|tree| {
                for h in [100u32, 200, 300] {
                    tree.ensure_retained(BlockHeight::from(h))?;
                }
                Ok::<_, ShardTreeError<_>>(())
            })
            .unwrap();

            db.with_ironwood_tree_mut(|tree| {
                for h in [100u32, 200, 300] {
                    tree.ensure_retained(BlockHeight::from(h))?;
                }
                Ok::<_, ShardTreeError<_>>(())
            })
            .unwrap();
        }

        db.remove_retained_checkpoints_below(BlockHeight::from(250))
            .unwrap();

        let remaining = db
            .with_sapling_tree_mut(|tree| {
                tree.store()
                    .retained_checkpoints()
                    .map_err(ShardTreeError::Storage)
            })
            .unwrap();
        assert_eq!(remaining, BTreeSet::from([BlockHeight::from(300)]));

        // The retained checkpoints must be pruned in the Orchard and Ironwood trees as well, not
        // just Sapling.
        #[cfg(feature = "orchard")]
        {
            let orchard_remaining = db
                .with_orchard_tree_mut(|tree| {
                    tree.store()
                        .retained_checkpoints()
                        .map_err(ShardTreeError::Storage)
                })
                .unwrap();
            assert_eq!(orchard_remaining, BTreeSet::from([BlockHeight::from(300)]));

            let ironwood_remaining = db
                .with_ironwood_tree_mut(|tree| {
                    tree.store()
                        .retained_checkpoints()
                        .map_err(ShardTreeError::Storage)
                })
                .unwrap()
                .expect("the wallet tracks an Ironwood tree");
            assert_eq!(
                ironwood_remaining,
                BTreeSet::from([BlockHeight::from(300)]),
                "retained Ironwood checkpoints below the max height must be released",
            );
        }
    }

    #[test]
    fn sapling_root_hashes() {
        check_root_hashes(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_witnesses() {
        check_witnesses(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_witness_consistency() {
        check_witness_consistency(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_checkpoint_rewind() {
        check_checkpoint_rewind(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_remove_mark() {
        check_remove_mark(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_rewind_remove_mark() {
        check_rewind_remove_mark(new_tree::<SaplingPoolTester>);
    }

    #[test]
    fn sapling_put_shard_roots() {
        put_shard_roots::<SaplingPoolTester>()
    }

    fn put_shard_roots<T: ShieldedPoolTester + ShieldedPoolPersistence>() {
        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();
        data_file.keep().unwrap();

        WalletMigrator::new().init_or_migrate(&mut db_data).unwrap();
        let tx = db_data.conn.transaction().unwrap();
        let store =
            SqliteShardStore::<_, String, 3>::from_connection(&tx, T::TABLES_PREFIX).unwrap();

        // introduce some roots
        let roots = (0u32..4)
            .map(|idx| {
                CommitmentTreeRoot::from_parts(
                    BlockHeight::from((idx + 1) * 3),
                    if idx == 3 {
                        "abcdefgh".to_string()
                    } else {
                        idx.to_string()
                    },
                )
            })
            .collect::<Vec<_>>();
        super::put_shard_roots::<_, 6, 3>(store.conn, T::TABLES_PREFIX, 0, &roots).unwrap();

        // simulate discovery of a note
        let mut tree = ShardTree::<_, 6, 3>::new(store, 10);
        let checkpoint_height = BlockHeight::from(3);
        tree.batch_insert(
            Position::from(24),
            ('a'..='h').map(|c| {
                (
                    c.to_string(),
                    match c {
                        'c' => Retention::Marked,
                        'h' => Retention::Checkpoint {
                            id: checkpoint_height,
                            marking: Marking::None,
                        },
                        _ => Retention::Ephemeral,
                    },
                )
            }),
        )
        .unwrap();

        // construct a witness for the note
        let witness = tree
            .witness_at_checkpoint_id(Position::from(26), &checkpoint_height)
            .unwrap();
        assert_eq!(
            witness
                .expect("an anchor exists at the expected checkpoint height")
                .path_elems(),
            &[
                "d",
                "ab",
                "efgh",
                "2",
                "01",
                "________________________________"
            ]
        );
    }

    /// Test that `generate_orchard_witnesses_at_historical_height` produces valid
    /// witnesses when given a frontier extracted from an earlier tree state.
    #[cfg(feature = "orchard")]
    fn witnesses_at_historical_height() {
        witnesses_at_historical_height_for_table(
            crate::ORCHARD_TABLES_PREFIX,
            super::generate_orchard_witnesses_at_historical_height,
        )
    }

    /// Test that `generate_ironwood_witnesses_at_historical_height` uses the
    /// Ironwood shard tables rather than the Orchard shard tables.
    #[cfg(feature = "orchard")]
    fn ironwood_witnesses_at_historical_height() {
        witnesses_at_historical_height_for_table(
            crate::IRONWOOD_TABLES_PREFIX,
            super::generate_ironwood_witnesses_at_historical_height,
        )
    }

    #[cfg(feature = "orchard")]
    type OrchardFrontier =
        incrementalmerkletree::frontier::NonEmptyFrontier<::orchard::tree::MerkleHashOrchard>;

    #[cfg(feature = "orchard")]
    type OrchardMerklePath = incrementalmerkletree::MerklePath<
        ::orchard::tree::MerkleHashOrchard,
        { ::orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
    >;

    #[cfg(feature = "orchard")]
    type HistoricalWitnessGenerator = fn(
        &rusqlite::Connection,
        &[Position],
        OrchardFrontier,
        BlockHeight,
    ) -> Result<Vec<OrchardMerklePath>, SqliteClientError>;

    #[cfg(feature = "orchard")]
    fn witnesses_at_historical_height_for_table(
        table_prefix: &'static str,
        generate_witnesses: HistoricalWitnessGenerator,
    ) {
        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();
        data_file.keep().unwrap();

        WalletMigrator::new().init_or_migrate(&mut db_data).unwrap();

        let mut rng = ChaChaRng::seed_from_u64(0);

        // We build two parallel trees: the wallet's ShardTree (persisted to the DB)
        // and a lightweight Frontier that captures the tree state at the historical height.
        let mut frontier_tree: Frontier<MerkleHashOrchard, 32> = Frontier::empty();
        let historical_height = BlockHeight::from(100);
        let note_position = Position::from(2);
        let note_leaf;

        {
            let tx = db_data.conn.transaction().unwrap();
            let store =
                SqliteShardStore::<_, MerkleHashOrchard, ORCHARD_SHARD_HEIGHT>::from_connection(
                    &tx,
                    table_prefix,
                )
                .unwrap();
            let mut tree = ShardTree::<
                _,
                { ::orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
                ORCHARD_SHARD_HEIGHT,
            >::new(store, 100);

            let mut leaves = Vec::new();
            for _ in 0u64..5 {
                leaves.push(MerkleHashOrchard::random(&mut rng));
            }
            note_leaf = leaves[u64::from(note_position) as usize];

            for (i, &leaf) in leaves.iter().enumerate() {
                let retention = if i == u64::from(note_position) as usize {
                    Retention::Marked
                } else {
                    Retention::Ephemeral
                };
                tree.append(leaf, retention).unwrap();
                frontier_tree.append(leaf);
            }

            // Advance the tree past the historical height, simulating the
            // wallet continuing to sync afterward.
            tree.checkpoint(historical_height).unwrap();
            for _ in 0..5 {
                tree.append(MerkleHashOrchard::random(&mut rng), Retention::Ephemeral)
                    .unwrap();
            }
            tree.checkpoint(BlockHeight::from(200)).unwrap();

            tx.commit().unwrap();
        }

        let expected_root = frontier_tree.root();
        let frontier = frontier_tree.take().expect("frontier is non-empty");

        let witnesses =
            generate_witnesses(&db_data.conn, &[note_position], frontier, historical_height)
                .expect("witness generation should succeed");

        assert_eq!(witnesses.len(), 1);
        assert_eq!(witnesses[0].root(note_leaf), expected_root);
    }

    /// Regression test: `generate_orchard_witnesses_at_historical_height` must not
    /// lose its freshly inserted historical checkpoint to pruning, even when the
    /// wallet has advanced many checkpoints past the historical height (so that
    /// the historical checkpoint has long since been pruned from the wallet's own
    /// `ShardTree`).
    ///
    /// Flow:
    /// 1. Seed: append 2 leaves to the wallet's `ShardTree` (position 0
    ///    `Marked`, position 1 `Ephemeral`), mirror them into a parallel
    ///    `Frontier` to capture the ground-truth state, then `checkpoint` at
    ///    `historical_height = 10`.
    /// 2. Bury: append 249 more `Ephemeral`-only blocks, each with its own
    ///    checkpoint. With `WALLET_MAX_CHECKPOINTS = 100` the wallet's pruner
    ///    evicts checkpoints 10..=159, including the one at
    ///    `historical_height`. The `Marked` retention at position 0 survives
    ///    because no surviving checkpoint schedules it for unmarking.
    /// 3. Precondition: assert `min_checkpoint_id > historical_height` so the
    ///    test fails loudly if a future tweak (e.g. shrinking `TOTAL_BLOCKS`
    ///    or growing `WALLET_MAX_CHECKPOINTS`) accidentally leaves the
    ///    historical checkpoint alive in the DB.
    /// 4. Exercise: call `generate_orchard_witnesses_at_historical_height`
    ///    with the captured frontier. Internally it builds a fresh
    ///    `MemoryShardStore` (no checkpoints), `ShardTree::new(store, 1)`,
    ///    and `insert_frontier_nodes(.., Retention::Checkpoint { id: 10 })`,
    ///    which adds exactly one checkpoint and then runs
    ///    `prune_excess_checkpoints` (`1 > 1` is false, so no eviction).
    /// 5. Verify: witness generation returns `Ok`, and
    ///    `witness.root(note_leaf) == frontier_tree.root()` confirms the
    ///    reconstructed anchor matches the historical state captured in (1).
    ///
    /// If anyone later changes the in-memory `max_checkpoints` to `0`, copies
    /// wallet checkpoints into the in-memory store, or otherwise inflates
    /// `checkpoint_count` before the frontier insertion, the freshly added
    /// historical checkpoint would be pruned and `witness_at_checkpoint_id`
    /// would return `None`, surfacing the regression as
    /// `SqliteClientError::HistoricalWitnessUnavailable`.
    #[cfg(feature = "orchard")]
    fn witnesses_at_historical_height_with_many_wallet_checkpoints() {
        // Wallet tree capacity << number of blocks we sync, so the historical
        // checkpoint is forcibly pruned from the wallet's own ShardTree before
        // we attempt to generate a witness at that height.
        const WALLET_MAX_CHECKPOINTS: usize = 100;
        const TOTAL_BLOCKS: u32 = 250;
        const LEAVES_PER_BLOCK: usize = 2;

        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();
        data_file.keep().unwrap();

        WalletMigrator::new().init_or_migrate(&mut db_data).unwrap();

        let mut rng = ChaChaRng::seed_from_u64(1);

        let mut frontier_tree: Frontier<MerkleHashOrchard, 32> = Frontier::empty();
        let historical_height = BlockHeight::from(10);
        let note_position = Position::from(0);
        let note_leaf;

        {
            let tx = db_data.conn.transaction().unwrap();
            let store =
                SqliteShardStore::<_, MerkleHashOrchard, ORCHARD_SHARD_HEIGHT>::from_connection(
                    &tx, "orchard",
                )
                .unwrap();
            let mut tree = ShardTree::<
                _,
                { ::orchard::NOTE_COMMITMENT_TREE_DEPTH as u8 },
                ORCHARD_SHARD_HEIGHT,
            >::new(store, WALLET_MAX_CHECKPOINTS);

            // First block: mark the note, snapshot the frontier, then checkpoint
            // at `historical_height`.
            let note_idx = u64::from(note_position) as usize;
            let mut first_leaves = Vec::with_capacity(LEAVES_PER_BLOCK);
            for _ in 0..LEAVES_PER_BLOCK {
                first_leaves.push(MerkleHashOrchard::random(&mut rng));
            }
            note_leaf = first_leaves[note_idx];
            for (i, &leaf) in first_leaves.iter().enumerate() {
                let retention = if i == note_idx {
                    Retention::Marked
                } else {
                    Retention::Ephemeral
                };
                tree.append(leaf, retention).unwrap();
                frontier_tree.append(leaf);
            }
            tree.checkpoint(historical_height).unwrap();

            // Drive the wallet far past the historical height so its pruner
            // evicts the historical_height checkpoint from the SQLite store.
            for block in 1..TOTAL_BLOCKS {
                for _ in 0..LEAVES_PER_BLOCK {
                    tree.append(MerkleHashOrchard::random(&mut rng), Retention::Ephemeral)
                        .unwrap();
                }
                tree.checkpoint(historical_height + block).unwrap();
            }
            tx.commit().unwrap();
        }

        // Sanity-check the precondition: the historical checkpoint must no
        // longer be present in the wallet's checkpoint table.
        let min_ckpt = super::min_checkpoint_id(&db_data.conn, "orchard")
            .unwrap()
            .expect("wallet has checkpoints");
        assert!(
            min_ckpt > historical_height,
            "test precondition: historical checkpoint should have been pruned, \
             but min retained checkpoint is {min_ckpt:?} <= {historical_height:?}",
        );

        let expected_root = frontier_tree.root();
        let frontier = frontier_tree.take().expect("frontier is non-empty");

        let witnesses = super::generate_orchard_witnesses_at_historical_height(
            &db_data.conn,
            &[note_position],
            frontier,
            historical_height,
        )
        .expect("witness generation should succeed despite deep wallet pruning");

        assert_eq!(witnesses.len(), 1);
        assert_eq!(witnesses[0].root(note_leaf), expected_root);
    }
}