ant-node 0.20.0

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

use crate::ant_protocol::{XorName, MAX_CHUNK_SIZE};
use crate::error::{Error, Result};
use crate::logging::{debug, info, trace, warn};
use heed::types::Bytes;
use heed::{Database, Env, EnvOpenOptions, MdbError};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;
use tokio::task::spawn_blocking;
use tokio_util::task::TaskTracker;

use crate::ant_protocol::XORNAME_LEN;
use crate::storage::StorageStats;

use crate::storage::{GIB, MIB};

/// Default minimum free disk space to preserve on the storage partition.
const DEFAULT_DISK_RESERVE: u64 = 500 * MIB;

/// Convert a byte count to GiB for human-readable log messages.
#[allow(clippy::cast_precision_loss)] // display only — sub-byte precision is irrelevant
fn bytes_to_gib(bytes: u64) -> f64 {
    bytes as f64 / GIB as f64
}

/// Absolute minimum LMDB map size.
///
/// Even on a nearly-full disk the database must be able to open.
/// Set to 256 MiB — enough for millions of LMDB pages.
const MIN_MAP_SIZE: usize = 256 * 1024 * 1024;

/// Maximum head-room (beyond the current data footprint) to reserve for the
/// LMDB map **on Windows**.
///
/// On Windows a node's committed / private memory scales with the *mapped*
/// size rather than with the data actually stored. Measured on a live node:
/// ~7.4 GB of extra commit for a ~3.7 TiB map, versus ~181 MB for a ~55 GiB
/// map — roughly 0.2% of the mapped size, resident and attributed to *no*
/// user-space allocation. That size-proportional cost is consistent with
/// kernel page tables / section metadata for the mapping; the exact kernel
/// structure was inferred from the scaling rather than measured directly, but
/// the size-proportional *effect* is what this cap targets. Linux keeps the
/// mapping sparse, so a disk-sized map is nearly free there — the overhead is
/// Windows-specific, which is why it only surfaced in Windows reports.
///
/// Sizing the map to the whole disk therefore costs ~0.2% of *free disk* per
/// node, multiplied by every node sharing the host (e.g. a 10 TiB partition ≈
/// 20 GiB). We instead cap the head-room on Windows and lean on
/// `LmdbStorage::try_resize` to extend the map on demand as data accumulates,
/// keeping the overhead proportional to *stored data* rather than *disk
/// capacity*. At 32 GiB the extra commit is ~100 MB, and a resize happens at
/// most once per 32 GiB written.
#[cfg(windows)]
const WINDOWS_MAP_HEADROOM: u64 = 32 * GIB;

/// How often to re-query available disk space (in seconds).
///
/// Between checks the cached result is trusted.  Disk space changes slowly
/// relative to chunk-write throughput, so a multi-second window is safe.
const DISK_CHECK_INTERVAL_SECS: u64 = 5;

/// Ceiling raise offered to a single *delete* that cannot copy-on-write inside
/// the pinned map.
///
/// A delete is itself a write: LMDB copies the B-tree path before it frees the
/// leaf pages, and it may need a page for the free-list's own bookkeeping. On a
/// map pinned exactly to the file size a delete therefore has nowhere to go,
/// and the node could not prune its way back to health.
///
/// Granted **only** on the delete retry path and taken away again inside the
/// same locked scope, so an ordinary store can never allocate from it. Leaving
/// it permanently in the ceiling would hand every node a little more of the
/// very reserve this mode exists to protect, multiplied by the nodes sharing
/// the volume.
const DELETE_COW_SLACK: u64 = 256 * 1024;

/// Total permanent file growth deletes may cause per low-disk episode.
///
/// What actually needs bounding is *growth*, not grants. Most slack-assisted
/// deletes reuse pages already inside `data.mdb` and grow it by nothing, and
/// those must stay free: a node has to be able to prune indefinitely, and page
/// reuse is not reliably available to the very next delete because LMDB cannot
/// hand back pages a still-recent transaction freed. Charging per grant instead
/// of per byte stops a node pruning after its first assisted delete.
///
/// Only bytes the file actually gained are charged here. Reset when the store
/// leaves no-growth mode. A rounding error against [`DEFAULT_DISK_RESERVE`].
const DELETE_COW_GROWTH_BUDGET: u64 = 1024 * 1024;

/// Configuration for LMDB storage.
#[derive(Debug, Clone)]
pub struct LmdbStorageConfig {
    /// Root directory for storage (LMDB env lives at `{root_dir}/chunks.mdb/`).
    pub root_dir: PathBuf,
    /// Whether to verify content on read (compares hash to address).
    pub verify_on_read: bool,
    /// Explicit LMDB map size cap in bytes.
    ///
    /// When 0 (default), the map size is computed automatically from available
    /// disk space and grows on demand when more storage becomes available.
    pub max_map_size: usize,
    /// Minimum free disk space (in bytes) to preserve on the storage partition.
    ///
    /// Writes are refused when available space drops below this threshold.
    pub disk_reserve: u64,
}

impl Default for LmdbStorageConfig {
    fn default() -> Self {
        Self {
            root_dir: PathBuf::from(".ant/chunks"),
            verify_on_read: true,
            max_map_size: 0,
            disk_reserve: DEFAULT_DISK_RESERVE,
        }
    }
}

impl LmdbStorageConfig {
    /// A test-friendly default with `disk_reserve` set to 0 so unit tests
    /// don't depend on the host having >= 1 GiB free disk space.
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn test_default() -> Self {
        Self {
            disk_reserve: 0,
            ..Self::default()
        }
    }
}

/// Content-addressed LMDB storage.
///
/// Uses heed (LMDB wrapper) for memory-mapped, transactional chunk storage.
/// Keys are 32-byte `XorName` addresses, values are raw chunk bytes.
pub struct LmdbStorage {
    /// LMDB environment.
    env: Env,
    /// The unnamed default database (key=XorName bytes, value=chunk bytes).
    db: Database<Bytes, Bytes>,
    /// Storage configuration.
    config: LmdbStorageConfig,
    /// Path to the LMDB environment directory (for disk-space queries).
    env_dir: PathBuf,
    /// Operation statistics.
    stats: parking_lot::RwLock<StorageStats>,
    /// Serialises access to the LMDB environment during a map resize.
    ///
    /// Normal read/write operations acquire a **shared** lock.  The rare
    /// resize path acquires an **exclusive** lock, ensuring no transactions
    /// are active when `env.resize()` is called (an LMDB safety requirement).
    env_lock: Arc<parking_lot::RwLock<()>>,
    /// Timestamp of the last successful disk-space check.
    ///
    /// `None` means "never checked — check on next write".  Updated only
    /// after a passing check, so a low-space result is always rechecked.
    last_disk_ok: parking_lot::Mutex<Option<Instant>>,
    /// Whether the map is currently pinned to the file's high-water mark.
    ///
    /// Set once available disk drops below the reserve. While pinned, LMDB can
    /// still serve a write from its own free list but cannot extend
    /// `data.mdb`, so the reserve is preserved by the allocator itself rather
    /// than by refusing every write up front.
    no_growth: Arc<AtomicBool>,
    /// Keep the map pinned whatever the free space says.
    ///
    /// Set for the whole of the migration bridge. While both stores are open they each
    /// measure the same free space and neither knows what the other is about to spend, so
    /// a chunk written to both can be admitted twice against one lot of headroom and the
    /// pair can cross the reserve together. Pinned, this environment cannot claim any new
    /// disk at all: a write it cannot satisfy from its own free list is refused, and the
    /// caller stores the chunk in files alone. That is the right answer anyway, because
    /// this copy exists to make a rollback survivable, not to be the one that must
    /// succeed.
    growth_pinned: Arc<AtomicBool>,
    /// Serialises entering and leaving no-growth mode.
    ///
    /// Setting `no_growth` and resizing the map is one compound transition
    /// spanning an await. Without this, two callers straddling the threshold
    /// can interleave so the flag ends up describing a map size that was never
    /// applied, leaving the store unpinned while it believes it is pinned.
    growth_mode_lock: tokio::sync::Mutex<()>,
    /// Bytes `data.mdb` has permanently gained to slack-assisted deletes in
    /// this low-disk episode.
    ///
    /// A delete's copy-on-write can extend the file, and LMDB never gives file
    /// space back, so that growth is permanent. Bounding it stops repeated
    /// fill-then-delete cycles walking the file into the reserve. Deletes that
    /// find room inside the file cost nothing. Reset on leaving no-growth mode.
    delete_growth_charged: Arc<AtomicU64>,
    /// Tracks every LMDB blocking task spawned by this storage.
    ///
    /// A `spawn_blocking` closure owns a cloned [`Env`] and keeps running
    /// even when its async awaiter is dropped (e.g. by a `select!` losing to
    /// a shutdown token).  Tracking the blocking task itself — not the async
    /// wrapper — lets [`Self::wait_idle`] wait for true quiescence before
    /// the environment may be reopened.
    blocking_tracker: TaskTracker,
    /// Test-only gate read-acquired at the top of the put blocking closure.
    ///
    /// Tests hold the write half to deterministically park an in-flight put
    /// on the blocking pool (e.g. to prove [`Self::wait_idle`] waits for a
    /// detached write).
    #[cfg(any(test, feature = "test-utils"))]
    test_put_gate: Arc<parking_lot::RwLock<()>>,
    /// Test-only gate read-acquired inside the raw-read blocking closures,
    /// immediately after the shared `env_lock` guard is taken.
    ///
    /// Tests hold the write half to deterministically park an in-flight raw
    /// read while it still holds the shared environment lock (e.g. to prove
    /// [`Self::try_resize`] waits for active raw reads before calling
    /// `env.resize()`).
    #[cfg(any(test, feature = "test-utils"))]
    test_read_gate: Arc<parking_lot::RwLock<()>>,
}

impl LmdbStorage {
    /// Create a new LMDB storage instance.
    ///
    /// Opens (or creates) an LMDB environment at `{root_dir}/chunks.mdb/`.
    ///
    /// When `config.max_map_size` is 0 (the default) the map size is derived
    /// from the available disk space on the partition that hosts the database,
    /// minus `config.disk_reserve`.  This allows a node to use all available
    /// storage without a fixed cap.  If the operator adds more storage later
    /// the map is resized on demand (see [`Self::put`]).
    ///
    /// # Errors
    ///
    /// Returns an error if the LMDB environment cannot be opened.
    #[allow(unsafe_code)]
    pub async fn new(config: LmdbStorageConfig) -> Result<Self> {
        let env_dir = config.root_dir.join("chunks.mdb");

        // Create the directory synchronously before opening LMDB
        std::fs::create_dir_all(&env_dir)
            .map_err(|e| Error::Storage(format!("Failed to create LMDB directory: {e}")))?;

        let map_size = if config.max_map_size > 0 {
            // Operator provided an explicit cap.
            config.max_map_size
        } else {
            // Auto-scale: current DB footprint + available space − reserve.
            let computed = compute_map_size(&env_dir, config.disk_reserve)?;
            info!(
                "Auto-computed LMDB map size: {:.2} GiB (data + available disk minus {:.2} GiB \
                 reserve, head-room capped on Windows to bound page-table overhead)",
                bytes_to_gib(computed as u64),
                bytes_to_gib(config.disk_reserve),
            );
            computed
        };

        let env_dir_clone = env_dir.clone();
        // Constructor-only blocking task: it runs before `self` (and its
        // `blocking_tracker`) exists, so it is deliberately untracked.  The
        // constructor awaits it right here, so it cannot outlive this call.
        let (env, db) = spawn_blocking(move || -> Result<(Env, Database<Bytes, Bytes>)> {
            // SAFETY: `EnvOpenOptions::open()` is unsafe because LMDB uses memory-mapped
            // I/O and relies on OS file-locking to prevent corruption from concurrent
            // access by multiple processes. We satisfy this by giving each node instance
            // a unique `root_dir` (typically a directory named by its full 64-hex peer
            // ID), ensuring no two processes open the same LMDB environment. Callers
            // who manually configure `--root-dir` must not point multiple nodes at the
            // same directory.
            let env = unsafe {
                EnvOpenOptions::new()
                    .map_size(map_size)
                    .max_dbs(1)
                    .open(&env_dir_clone)
                    .map_err(|e| Error::Storage(format!("Failed to open LMDB env: {e}")))?
            };

            let mut wtxn = env
                .write_txn()
                .map_err(|e| Error::Storage(format!("Failed to create write txn: {e}")))?;
            let db: Database<Bytes, Bytes> = env
                .create_database(&mut wtxn, None)
                .map_err(|e| Error::Storage(format!("Failed to create database: {e}")))?;
            wtxn.commit()
                .map_err(|e| Error::Storage(format!("Failed to commit db creation: {e}")))?;

            Ok((env, db))
        })
        .await
        .map_err(|e| Error::Storage(format!("LMDB init task failed: {e}")))??;

        let storage = Self {
            env,
            db,
            config,
            env_dir,
            stats: parking_lot::RwLock::new(StorageStats::default()),
            env_lock: Arc::new(parking_lot::RwLock::new(())),
            last_disk_ok: parking_lot::Mutex::new(None),
            no_growth: Arc::new(AtomicBool::new(false)),
            growth_pinned: Arc::new(AtomicBool::new(false)),
            growth_mode_lock: tokio::sync::Mutex::new(()),
            delete_growth_charged: Arc::new(AtomicU64::new(0)),
            blocking_tracker: TaskTracker::new(),
            #[cfg(any(test, feature = "test-utils"))]
            test_put_gate: Arc::new(parking_lot::RwLock::new(())),
            #[cfg(any(test, feature = "test-utils"))]
            test_read_gate: Arc::new(parking_lot::RwLock::new(())),
        };

        debug!(
            "Initialized LMDB storage at {:?} ({} existing chunks)",
            storage.env_dir,
            storage.current_chunks()?
        );

        Ok(storage)
    }

    /// Store a chunk.
    ///
    /// Before writing, verifies that available disk space exceeds the
    /// configured reserve.  If the LMDB map is full but more disk space
    /// exists (e.g. the operator added storage), the map is resized
    /// automatically and the write is retried.
    ///
    /// # Returns
    ///
    /// Returns `true` if the chunk was newly stored, `false` if it already existed.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails, content doesn't match address,
    /// or the disk is too full to accept new chunks.
    pub async fn put(&self, address: &XorName, content: &[u8]) -> Result<bool> {
        self.put_inner(address, content, true).await
    }

    /// Store bytes under a key they do not hash to. Tests only.
    ///
    /// Stands in for a record that rotted in place, which is the one shape the ordinary
    /// path refuses to create and the migration has to survive finding.
    ///
    /// # Errors
    ///
    /// As [`Self::put`], minus the address check.
    #[cfg(test)]
    pub(crate) async fn put_unchecked(&self, address: &XorName, content: &[u8]) -> Result<bool> {
        self.put_inner(address, content, false).await
    }

    async fn put_inner(&self, address: &XorName, content: &[u8], verify: bool) -> Result<bool> {
        // Verify content address
        let computed = Self::compute_address(content);
        if verify && computed != *address {
            return Err(Error::Storage(format!(
                "Content address mismatch: expected {}, computed {}",
                hex::encode(address),
                hex::encode(computed)
            )));
        }

        // Fast-path duplicate check (read-only, no write lock needed).
        // This is an optimistic hint — the authoritative check happens inside
        // the write transaction below to prevent TOCTOU races.
        if self.exists(address)? {
            trace!("Chunk {} already exists", hex::encode(address));
            self.stats.write().duplicates += 1;
            return Ok(false);
        }

        // ── Capacity guard (cached — at most one syscall per interval) ──
        // Placed after the duplicate check so that re-storing an existing
        // chunk remains a harmless no-op even when disk space is low.
        //
        // Below the reserve this pins the map instead of refusing outright, so
        // the write is still attempted and LMDB decides whether a freed page
        // can take it. A node that has pruned heavily keeps serving the network
        // from the space it already occupies.
        let no_growth = self.sync_growth_mode().await?;

        // ── Write (with resize-on-demand) ───────────────────────────────
        match self.try_put(address, content).await? {
            PutOutcome::New => {}
            PutOutcome::Duplicate => {
                trace!("Chunk {} already exists", hex::encode(address));
                self.stats.write().duplicates += 1;
                return Ok(false);
            }
            PutOutcome::MapFull if no_growth => {
                // Both halves are now true: the volume is below the reserve and
                // no free page can take *this* value. Resizing would extend the
                // file into the reserve, so refuse.
                //
                // The refusal is not remembered. `MapFull` is specific to the
                // size just attempted — a smaller value may still fit a smaller
                // run — so caching it would let one maximum-sized chunk lock out
                // every subsequent write. `check_capacity` estimates instead.
                return Err(Error::Storage(format!(
                    "Insufficient disk space: {:.2} GiB reserve required and no reusable page \
                     in the local store fits this {} B value. \
                     Free disk space or increase the partition to continue storing chunks.",
                    bytes_to_gib(self.config.disk_reserve),
                    content.len(),
                )));
            }
            PutOutcome::MapFull => {
                // The map ceiling was reached but there may be more disk space
                // available (e.g. operator expanded the partition).
                //
                // Guarded: `no_growth` was sampled before the write, so the
                // store may have entered no-growth mode since. Growing the map
                // outside the transition lock could undo a pin that a
                // concurrent `sync_growth_mode` had just applied, handing the
                // reserve back to ordinary writes.
                self.try_resize_for_growth().await?;
                // Retry once after resize.
                match self.try_put(address, content).await? {
                    PutOutcome::New => {}
                    PutOutcome::Duplicate => {
                        self.stats.write().duplicates += 1;
                        return Ok(false);
                    }
                    PutOutcome::MapFull => {
                        return Err(Error::Storage(
                            "LMDB map full after resize — disk may be at capacity".into(),
                        ));
                    }
                }
            }
        }

        {
            let mut stats = self.stats.write();
            stats.chunks_stored += 1;
            stats.bytes_stored += content.len() as u64;
        }

        debug!(
            "Stored chunk {} ({} bytes)",
            hex::encode(address),
            content.len()
        );

        Ok(true)
    }

    /// Attempt a single put inside a write transaction.
    ///
    /// Returns [`PutOutcome::MapFull`] instead of an error when the LMDB map
    /// ceiling is reached, so the caller can resize and retry.
    async fn try_put(&self, address: &XorName, content: &[u8]) -> Result<PutOutcome> {
        let key = *address;
        let value = content.to_vec();
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);
        #[cfg(any(test, feature = "test-utils"))]
        let test_put_gate = Arc::clone(&self.test_put_gate);

        self.blocking_tracker
            .spawn_blocking(move || -> Result<PutOutcome> {
                // Test-only: parks here while a test holds the write half.
                #[cfg(any(test, feature = "test-utils"))]
                let _test_put_gate = test_put_gate.read();
                let _guard = lock.read();

                let mut wtxn = env
                    .write_txn()
                    .map_err(|e| Error::Storage(format!("Failed to create write txn: {e}")))?;

                // Authoritative existence check inside the serialized write txn
                if db
                    .get(&wtxn, &key)
                    .map_err(|e| Error::Storage(format!("Failed to check existence: {e}")))?
                    .is_some()
                {
                    return Ok(PutOutcome::Duplicate);
                }

                match db.put(&mut wtxn, &key, &value) {
                    Ok(()) => {}
                    Err(heed::Error::Mdb(MdbError::MapFull)) => return Ok(PutOutcome::MapFull),
                    Err(e) => {
                        return Err(Error::Storage(format!("Failed to put chunk: {e}")));
                    }
                }

                match wtxn.commit() {
                    Ok(()) => Ok(PutOutcome::New),
                    Err(heed::Error::Mdb(MdbError::MapFull)) => Ok(PutOutcome::MapFull),
                    Err(e) => Err(Error::Storage(format!("Failed to commit put: {e}"))),
                }
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB put task failed: {e}")))?
    }

    /// Retrieve a chunk.
    ///
    /// # Returns
    ///
    /// Returns `Some(content)` if found, `None` if not found.
    ///
    /// # Errors
    ///
    /// Returns an error if read fails or verification fails.
    pub async fn get(&self, address: &XorName) -> Result<Option<Vec<u8>>> {
        let key = *address;
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);

        let content = self
            .blocking_tracker
            .spawn_blocking(move || -> Result<Option<Vec<u8>>> {
                let _guard = lock.read();
                let rtxn = env
                    .read_txn()
                    .map_err(|e| Error::Storage(format!("Failed to create read txn: {e}")))?;
                let value = db
                    .get(&rtxn, &key)
                    .map_err(|e| Error::Storage(format!("Failed to get chunk: {e}")))?;
                Ok(value.map(Vec::from))
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB get task failed: {e}")))??;

        let Some(content) = content else {
            trace!("Chunk {} not found", hex::encode(address));
            return Ok(None);
        };

        // Verify content if configured
        if self.config.verify_on_read {
            let computed = Self::compute_address(&content);
            if computed != *address {
                self.stats.write().verification_failures += 1;
                warn!(
                    "Chunk verification failed: expected {}, computed {}",
                    hex::encode(address),
                    hex::encode(computed)
                );
                return Err(Error::Storage(format!(
                    "Chunk verification failed for {}",
                    hex::encode(address)
                )));
            }
        }

        {
            let mut stats = self.stats.write();
            stats.chunks_retrieved += 1;
            stats.bytes_retrieved += content.len() as u64;
        }

        debug!(
            "Retrieved chunk {} ({} bytes)",
            hex::encode(address),
            content.len()
        );

        Ok(Some(content))
    }

    /// Check if a chunk exists.
    ///
    /// # Errors
    ///
    /// Returns an error if the LMDB read transaction fails.
    pub fn exists(&self, address: &XorName) -> Result<bool> {
        let _guard = self.env_lock.read();
        let rtxn = self
            .env
            .read_txn()
            .map_err(|e| Error::Storage(format!("Failed to create read txn: {e}")))?;
        let found = self
            .db
            .get(&rtxn, address.as_ref())
            .map_err(|e| Error::Storage(format!("Failed to check existence: {e}")))?
            .is_some();
        Ok(found)
    }

    /// Delete a chunk.
    ///
    /// # Errors
    ///
    /// Returns an error if deletion fails.
    pub async fn delete(&self, address: &XorName) -> Result<bool> {
        let key = *address;

        // Establish growth mode first, exactly as `put` does. Otherwise a
        // delete arriving while the volume is low but before any write has
        // pinned the map would copy-on-write into whatever head-room the
        // ceiling still had, growing `data.mdb` into the reserve without
        // passing through the budgeted allowance below.
        self.sync_growth_mode().await?;

        let deleted = match self.try_delete(&key).await? {
            DeleteOutcome::Done(existed) => existed,
            DeleteOutcome::MapFull => {
                // A delete is a write: LMDB copies the B-tree path before it
                // frees the leaf pages, so a store with no free page at all
                // cannot delete inside a map pinned to the file size. Without a
                // way through, a node that filled up before it ever pruned
                // could never prune its way out.
                //
                // Serialised against `sync_growth_mode` so the two cannot
                // interleave their resizes.
                let _transition = self.growth_mode_lock.lock().await;
                self.delete_with_slack(&key).await?
            }
        };

        if deleted {
            debug!("Deleted chunk {}", hex::encode(address));
        }

        Ok(deleted)
    }

    /// Attempt one delete, reporting `MapFull` rather than raising it.
    async fn try_delete(&self, key: &XorName) -> Result<DeleteOutcome> {
        let key = *key;
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);

        self.blocking_tracker
            .spawn_blocking(move || -> Result<DeleteOutcome> {
                let _guard = lock.read();
                delete_in_txn(&env, db, &key)
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB delete task failed: {e}")))?
    }

    /// Get storage statistics.
    #[must_use]
    pub fn stats(&self) -> StorageStats {
        let mut stats = self.stats.read().clone();
        match self.current_chunks() {
            Ok(count) => stats.current_chunks = count,
            Err(e) => {
                warn!("Failed to read current_chunks for stats: {e}");
                stats.current_chunks = 0;
            }
        }
        stats
    }

    /// Return the number of chunks currently stored, queried from LMDB metadata.
    ///
    /// This is an O(1) read of the B-tree page header — not a full scan.
    ///
    /// # Errors
    ///
    /// Returns an error if the LMDB read transaction fails.
    pub fn current_chunks(&self) -> Result<u64> {
        let _guard = self.env_lock.read();
        let rtxn = self
            .env
            .read_txn()
            .map_err(|e| Error::Storage(format!("Failed to create read txn: {e}")))?;
        let entries = self
            .db
            .stat(&rtxn)
            .map_err(|e| Error::Storage(format!("Failed to read db stats: {e}")))?
            .entries;
        Ok(entries as u64)
    }

    /// Compute content address (BLAKE3 hash).
    #[must_use]
    pub fn compute_address(content: &[u8]) -> XorName {
        crate::client::compute_address(content)
    }

    /// Get the root directory.
    #[must_use]
    pub fn root_dir(&self) -> &Path {
        &self.config.root_dir
    }

    /// Return all stored record keys.
    ///
    /// Iterates the LMDB database in a read transaction. Used by the
    /// replication subsystem for hint construction and audit sampling.
    ///
    /// # Errors
    ///
    /// Returns an error if the LMDB read transaction fails.
    pub async fn all_keys(&self) -> Result<Vec<XorName>> {
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);

        let keys = self
            .blocking_tracker
            .spawn_blocking(move || -> Result<Vec<XorName>> {
                // Hold the shared lock for the whole read so try_resize() (which
                // takes the exclusive lock before the unsafe Env::resize()) cannot
                // unmap the environment while this txn and its cursor are live.
                let _guard = lock.read();
                let rtxn = env
                    .read_txn()
                    .map_err(|e| Error::Storage(format!("Failed to create read txn: {e}")))?;
                let mut keys = Vec::new();
                let iter = db
                    .iter(&rtxn)
                    .map_err(|e| Error::Storage(format!("Failed to iterate database: {e}")))?;
                for result in iter {
                    let (key_bytes, _) =
                        result.map_err(|e| Error::Storage(format!("Failed to read entry: {e}")))?;
                    if key_bytes.len() == XORNAME_LEN {
                        let mut key = [0u8; XORNAME_LEN];
                        key.copy_from_slice(key_bytes);
                        keys.push(key);
                    } else {
                        crate::logging::warn!(
                            "LmdbStorage: skipping entry with unexpected key length {} (expected {XORNAME_LEN})",
                            key_bytes.len()
                        );
                    }
                }
                Ok(keys)
            })
            .await
            .map_err(|e| Error::Storage(format!("all_keys task failed: {e}")))?;

        keys
    }

    /// Retrieve raw chunk bytes without content-address verification.
    ///
    /// Used by the audit subsystem to compute digests over stored bytes.
    /// Unlike [`Self::get`], this does not verify `hash(content) == address`.
    ///
    /// # Errors
    ///
    /// Returns an error if the LMDB read transaction fails.
    pub async fn get_raw(&self, address: &XorName) -> Result<Option<Vec<u8>>> {
        let key = *address;
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);
        #[cfg(any(test, feature = "test-utils"))]
        let test_read_gate = Arc::clone(&self.test_read_gate);

        let value = self
            .blocking_tracker
            .spawn_blocking(move || -> Result<Option<Vec<u8>>> {
                // Shared lock held until the bytes are copied out, so a concurrent
                // try_resize() cannot unmap the environment mid-read. See all_keys.
                let _guard = lock.read();
                // Test-only: parks here, still holding the shared lock, while a
                // test holds the write half — used to prove a resize waits.
                #[cfg(any(test, feature = "test-utils"))]
                let _test_read_gate = test_read_gate.read();
                let rtxn = env
                    .read_txn()
                    .map_err(|e| Error::Storage(format!("Failed to create read txn: {e}")))?;
                let val = db
                    .get(&rtxn, key.as_ref())
                    .map_err(|e| Error::Storage(format!("Failed to get chunk: {e}")))?;
                Ok(val.map(Vec::from))
            })
            .await
            .map_err(|e| Error::Storage(format!("get_raw task failed: {e}")))?;

        value
    }

    /// Cheap capacity pre-check for callers that want to reject work *before*
    /// doing expensive setup (e.g. the PUT handler skipping payment
    /// verification on a full node — see `V2-411`).
    ///
    /// A node is full only when **both** halves are true: the volume is below
    /// the reserve *and* the store has no reusable page left. Deleting a record
    /// returns its pages to LMDB's free list and never to the filesystem, so a
    /// node that has pruned heavily sits on reusable capacity while `statvfs`
    /// still reports the volume as full. Refusing on the disk half alone stops
    /// such a node from writing into space it already owns.
    ///
    /// This is a **hint**, deliberately biased towards admitting: it estimates
    /// reusable bytes and only refuses when there is not even one chunk's worth.
    /// The authority on whether a given write fits stays with LMDB's allocator
    /// in [`Self::put`], because no page count can account for the
    /// copy-on-write of the B-tree path, the contiguous run a multi-megabyte
    /// value needs, or pages still pinned by an open read transaction. An
    /// over-optimistic hint costs one refused write; an over-pessimistic one
    /// would recreate the bug this exists to fix.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Storage`] when the volume is below the reserve and the
    /// store holds less than one chunk of reusable space, or when the
    /// disk-space query itself fails.
    /// Unused while the node is moving off this store.
    ///
    /// Capacity is now the file store's question, because that is where writes land, and
    /// this predicate deliberately answers a different one: it counts pages this store can
    /// reuse internally, which says nothing about whether the *file* about to be written
    /// will fit. [`Self::capacity_verdict`] is still used, to decide whether the bridge's
    /// copy into this store is worth attempting. Both go when this store does.
    #[allow(dead_code)]
    pub(crate) fn check_capacity(&self) -> Result<()> {
        let Some(available) = self.available_space_cached()? else {
            return Ok(());
        };

        let reusable = self.reusable_bytes()?;
        if reusable >= MAX_CHUNK_SIZE as u64 {
            return Ok(());
        }

        Err(Error::Storage(format!(
            "Insufficient disk space: {:.2} GiB available, {:.2} GiB reserve required, \
             and only {reusable} B reusable inside the local store. \
             Free disk space or increase the partition to continue storing chunks.",
            bytes_to_gib(available),
            bytes_to_gib(self.config.disk_reserve),
        )))
    }

    /// Capacity as a three-way verdict, distinguishing a full node from a
    /// query that failed.
    ///
    /// Refuses on the same two-part predicate as [`Self::check_capacity`]:
    /// `Full` only when the volume is below the reserve *and* the store holds
    /// less than one chunk of reusable space. Deleted records return their
    /// pages to LMDB's free list and never to the filesystem, so a pruned node
    /// reads as full to `statvfs` while still able to store chunks — such a
    /// node must keep discovering holders for the keys it owes.
    ///
    /// Shares the same TTL cache as [`Self::check_capacity`]: a passing disk
    /// reading is cached, a failing one is always rechecked, so freed space is
    /// noticed promptly. An admit that rests on reusable pages is deliberately
    /// not cached, matching the pre-check, because the free list can drain a
    /// chunk at a time.
    pub(crate) fn capacity_verdict(&self) -> CapacityVerdict {
        {
            let last = self.last_disk_ok.lock();
            if let Some(t) = *last {
                if t.elapsed().as_secs() < DISK_CHECK_INTERVAL_SECS {
                    return CapacityVerdict::Writable;
                }
            }
        }
        let disk = verdict_from_available_space(
            fs2::available_space(&self.env_dir),
            self.config.disk_reserve,
        );
        match disk {
            CapacityVerdict::Writable => {
                *self.last_disk_ok.lock() = Some(Instant::now());
                CapacityVerdict::Writable
            }
            CapacityVerdict::Unknown => CapacityVerdict::Unknown,
            // Below the reserve is only half the predicate: the store may
            // still hold pages it can reuse without growing the file.
            CapacityVerdict::Full => match self.reusable_bytes() {
                Ok(reusable) if reusable >= MAX_CHUNK_SIZE as u64 => CapacityVerdict::Writable,
                Ok(_) => CapacityVerdict::Full,
                Err(e) => {
                    warn!("Could not query the store's reusable space: {e}");
                    CapacityVerdict::Unknown
                }
            },
        }
    }

    /// Estimated bytes inside `data.mdb` that LMDB could write without growing
    /// the file: the file size minus the pages currently holding data.
    ///
    /// Deliberately an over-estimate. `stat()` counts only the branch, leaf and
    /// overflow pages of the unnamed database, so the free-list's own pages and
    /// the environment metadata fall on the "reusable" side. Erring high keeps
    /// [`Self::check_capacity`] biased towards admitting the attempt.
    ///
    /// Uses `stat()` rather than heed's `non_free_pages_size()`, which walks the
    /// unnamed database calling `String::from_utf8(key).unwrap()` on every key
    /// without a zero byte. Our keys are 32 random bytes, so that call panics
    /// almost immediately. A single unnamed database makes `stat()` equivalent.
    fn reusable_bytes(&self) -> Result<u64> {
        // Order matters. The two samples are not atomic, so read the live pages
        // first and the file length second: a write committing in between then
        // pairs an older (smaller) live count with a newer (larger) file, which
        // over-estimates. Sampling the other way round pairs a stale file
        // length with a fresh live count and can under-estimate, which would
        // refuse a node that has room — the very bug this fixes.
        let stat = self.env.stat();
        let live_pages = (stat.branch_pages as u64)
            .saturating_add(stat.leaf_pages as u64)
            .saturating_add(stat.overflow_pages as u64);
        let live_bytes = live_pages.saturating_mul(u64::from(stat.page_size));

        let file_bytes = self
            .env
            .real_disk_size()
            .map_err(|e| Error::Storage(format!("Failed to query LMDB file size: {e}")))?;

        Ok(file_bytes.saturating_sub(live_bytes))
    }

    /// Available bytes on the storage volume, or `None` when a recent check
    /// already showed it above the reserve.
    ///
    /// Only *passing* results are cached, so a low-space condition is always
    /// re-measured and freed space is detected promptly.
    fn available_space_cached(&self) -> Result<Option<u64>> {
        {
            let last = self.last_disk_ok.lock();
            if let Some(t) = *last {
                if t.elapsed().as_secs() < DISK_CHECK_INTERVAL_SECS {
                    return Ok(None);
                }
            }
        }

        let available = fs2::available_space(&self.env_dir)
            .map_err(|e| Error::Storage(format!("Failed to query available disk space: {e}")))?;

        if available >= self.config.disk_reserve {
            *self.last_disk_ok.lock() = Some(Instant::now());
            return Ok(None);
        }

        Ok(Some(available))
    }

    /// Align the map ceiling with the current disk state, returning whether the
    /// store is in no-growth mode.
    ///
    /// Below the reserve the map is pinned to the file's high-water mark, so a
    /// put succeeds exactly when LMDB can satisfy it from the free list and
    /// returns `MapFull` the moment it would need to extend `data.mdb`. That
    /// makes the allocator the authority on "can this write fit".
    ///
    /// The whole transition runs under `growth_mode_lock`. Setting the flag and
    /// resizing the map is one compound change spanning an await, so without
    /// serialisation two callers straddling the threshold can interleave and
    /// leave the flag describing a map that was never applied.
    async fn sync_growth_mode(&self) -> Result<bool> {
        let _transition = self.growth_mode_lock.lock().await;

        // Re-measured inside the lock: a caller that queued behind a transition
        // must act on the state that transition left behind, not the one it saw
        // before waiting.
        // Pinned for the bridge: never unpinned by having room, because the room is not
        // this environment's to spend while another store is measuring the same disk.
        if self.growth_pinned.load(Ordering::Acquire) {
            self.no_growth.store(true, Ordering::Release);
            self.pin_map_to_high_water().await?;
            return Ok(true);
        }
        if self.available_space_cached()?.is_none() {
            // At or above the reserve: restore normal head-room if we pinned it.
            if self.no_growth.load(Ordering::Acquire) {
                // Intent first, work second. A `spawn_blocking` body outlives a
                // cancelled awaiter, so ordering between two resizes cannot be
                // guaranteed by holding an async lock. Publishing the intent
                // before the work lets each closure re-read it under the
                // exclusive lock and decline if it has since been reversed.
                self.no_growth.store(false, Ordering::Release);
                self.try_resize().await?;
            }
            // Real disk again: the maintenance allowance is refreshed. Done on
            // every healthy pass, not just the transition, so an allowance
            // spent while the flag happened to be clear is still returned.
            self.delete_growth_charged.store(0, Ordering::Release);
            return Ok(false);
        }

        // Called unconditionally, not just on the transition. A re-pin that
        // failed, or a transition whose caller was cancelled while its detached
        // resize was still in flight, can leave the flag set while the map is
        // not actually pinned; re-asserting it here repairs that instead of
        // trusting the flag. The call is a no-op when already pinned.
        self.no_growth.store(true, Ordering::Release);
        self.pin_map_to_high_water().await?;

        Ok(true)
    }

    /// Keep this environment from ever claiming new disk, until the process ends.
    ///
    /// For the migration bridge, where a second store measures the same free space and
    /// neither knows what the other is about to spend. Pinned, this one writes only from
    /// pages it already holds, so the other's accounting is the only claim on free disk.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Storage`] if the map cannot be pinned.
    pub async fn pin_growth(&self) -> Result<()> {
        self.growth_pinned.store(true, Ordering::Release);
        self.sync_growth_mode().await.map(|_| ())
    }

    /// Pin the LMDB map to the size of `data.mdb` on disk.
    ///
    /// Every page already in the file stays usable, including free ones, but
    /// the file cannot grow, so the configured reserve is preserved by LMDB
    /// itself rather than by refusing writes it could have served.
    ///
    /// Deliberately leaves **no** head-room: any slack in the ceiling is
    /// ordinary put capacity, so it would be spent on the next chunk rather
    /// than kept for maintenance, and on a shared volume every node would take
    /// its own slice out of the reserve. Deletes get their copy-on-write room
    /// on demand instead, see [`Self::delete`].
    ///
    /// Takes the **exclusive** `env_lock` for the same reason
    /// [`Self::try_resize`] does: `mdb_env_set_mapsize` requires that no
    /// transaction is active. Callers hold `growth_mode_lock`.
    #[allow(unsafe_code)]
    async fn pin_map_to_high_water(&self) -> Result<()> {
        // The "is it already pinned?" test lives inside the exclusive lock
        // below, not out here. An unlocked pre-check can observe "already
        // pinned" moments before a detached resize from a cancelled transition
        // lands, after which the flag would claim a pin that no longer holds.
        // Callers invoke this on every low-disk write so the pinned state
        // repairs itself; the locked section is a few reads when nothing is to
        // be done.
        let env = self.env.clone();
        let lock = Arc::clone(&self.env_lock);
        let no_growth = Arc::clone(&self.no_growth);

        self.blocking_tracker
            .spawn_blocking(move || -> Result<()> {
                // Exclusive lock guarantees no concurrent transactions.
                let _guard = lock.write();

                // Re-read under the lock: this closure may have been queued
                // behind others, or its awaiter cancelled, and the store may
                // have left no-growth mode since it was spawned.
                if !no_growth.load(Ordering::Acquire) {
                    return Ok(());
                }

                let current_map = env.info().map_size;
                let file_bytes = env
                    .real_disk_size()
                    .map_err(|e| Error::Storage(format!("Failed to query LMDB file size: {e}")))?;

                let page = page_size::get() as u64;
                let aligned = file_bytes.div_ceil(page) * page;
                let target = usize::try_from(aligned).unwrap_or(usize::MAX);

                // Re-checked under the lock: the state may have moved between
                // the cheap check and here.
                if target >= current_map {
                    return Ok(());
                }

                // SAFETY: We hold an exclusive lock, so no transactions are active.
                unsafe {
                    env.resize(target)
                        .map_err(|e| Error::Storage(format!("Failed to pin LMDB map: {e}")))?;
                }

                info!(
                    "Disk below reserve: pinned LMDB map to {:.2} GiB (was {:.2} GiB). \
                     Writes that fit in already-freed pages still succeed; \
                     only writes that would grow the file are refused.",
                    bytes_to_gib(target as u64),
                    bytes_to_gib(current_map as u64),
                );
                Ok(())
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB map pin task failed: {e}")))?
    }

    /// Grow the map for a write, unless the store is pinned below the reserve.
    ///
    /// Serialised against [`Self::sync_growth_mode`] so a resize cannot land
    /// after a pin and quietly undo it. If the store entered no-growth mode
    /// while the write was in flight, the caller's `MapFull` is final and no
    /// growth happens.
    async fn try_resize_for_growth(&self) -> Result<()> {
        let _transition = self.growth_mode_lock.lock().await;

        if self.no_growth.load(Ordering::Acquire) {
            return Ok(());
        }

        self.try_resize().await
    }

    /// Delete `key` with [`DELETE_COW_SLACK`] of temporary map head-room, then
    /// take the head-room straight back.
    ///
    /// The raise, the delete and the re-pin all happen inside **one** exclusive
    /// `env_lock` scope. Doing them as three separate locked steps would leave
    /// windows in which an ordinary put could allocate from the raised ceiling,
    /// spending the reserve on a chunk instead of on the maintenance it was
    /// granted for, and an error or cancellation between the steps would leave
    /// the ceiling raised for good.
    ///
    /// What is budgeted is the *growth*, not the grant. If the copy-on-write
    /// does extend `data.mdb` that growth is permanent, since LMDB never
    /// returns file space, so repeated fill-then-delete cycles could otherwise
    /// walk the file into the reserve a slice at a time. A delete that finds
    /// room inside the file is charged nothing.
    ///
    /// Charging per grant instead would be wrong, and was: page reuse is not
    /// reliably available to the very next delete, because LMDB will not hand
    /// back pages a still-recent transaction freed. A one-grant budget
    /// therefore stopped a node pruning after its first assisted delete, which
    /// showed up as every delete failing on 4 KiB-page hosts while passing on
    /// 16 KiB-page ones. The budget resets when the store leaves no-growth
    /// mode, i.e. when there is real disk to work with again.
    #[allow(unsafe_code)]
    async fn delete_with_slack(&self, key: &XorName) -> Result<bool> {
        let key = *key;
        let env = self.env.clone();
        let db = self.db;
        let lock = Arc::clone(&self.env_lock);
        let budget = Arc::clone(&self.delete_growth_charged);

        let outcome = self
            .blocking_tracker
            .spawn_blocking(move || -> Result<DeleteOutcome> {
                // Checked and charged entirely inside the closure. A
                // `spawn_blocking` body keeps running when its awaiter is
                // dropped, so accounting split across the await could be
                // skipped, permanently costing the node its ability to prune.
                if budget.load(Ordering::Acquire) >= DELETE_COW_GROWTH_BUDGET {
                    return Err(Error::Storage(format!(
                        "Cannot delete: the local store is full and deletes have already used \
                         their {DELETE_COW_GROWTH_BUDGET} B growth allowance. \
                         Free disk space to continue."
                    )));
                }

                // Exclusive for the whole sequence: no transaction may be
                // active across either resize, and no put may observe the
                // raised ceiling.
                let _guard = lock.write();

                let page = page_size::get() as u64;
                let previous_map = env.info().map_size;
                let file_before = env
                    .real_disk_size()
                    .map_err(|e| Error::Storage(format!("Failed to query LMDB file size: {e}")))?;
                let raised = (previous_map as u64)
                    .saturating_add(DELETE_COW_SLACK)
                    .div_ceil(page)
                    .saturating_mul(page);

                // SAFETY: exclusive lock held, so no transactions are active.
                let granted = unsafe {
                    env.resize(usize::try_from(raised).unwrap_or(usize::MAX))
                        .map_err(|e| Error::Storage(format!("Failed to grant delete slack: {e}")))
                };
                granted?;

                // Armed across the delete so an unwind still restores the
                // ceiling; disarmed once the explicit restore below succeeds.
                let mut ceiling_guard = MapCeilingRestorer {
                    env: &env,
                    previous: previous_map,
                    armed: true,
                };

                let outcome = delete_in_txn(&env, db, &key);

                // Charge what the file actually gained, not the fact that slack
                // was offered. A delete that found room inside `data.mdb` costs
                // nothing and must not consume the allowance, otherwise a node
                // stops being able to prune after its first assisted delete.
                // Measured before the ceiling is restored, and before any error
                // is propagated, so a committed delete is always accounted for.
                //
                // A measurement that fails is charged the whole slack rather than nothing.
                // Reading it back as the size before the delete would say the file did not
                // grow, and a delete that did grow would then spend disk the budget never
                // saw. Repeat that and the ceiling stops meaning anything. Over-charging
                // costs at worst one assisted delete; under-charging costs the reserve.
                let grew = match env.real_disk_size() {
                    Ok(file_after) => file_after.saturating_sub(file_before),
                    Err(e) => {
                        warn!(
                            "Could not measure the LMDB file after an assisted delete \
                             ({e}); charging the whole slack rather than assuming it cost \
                             nothing"
                        );
                        DELETE_COW_SLACK
                    }
                };
                if grew > 0 {
                    budget.fetch_add(grew, Ordering::AcqRel);
                }

                // Undo the raise before releasing the lock, on every path and
                // whatever the delete did. Restoring to the previous ceiling
                // rather than to a freshly measured file size keeps this
                // unconditional: it is exactly the inverse of the raise, needs
                // no second syscall that could itself fail, and is correct
                // whether or not the store was pinned. If the copy-on-write did
                // extend the file, LMDB clamps a request below the space in use,
                // so the map still covers the data.
                //
                // SAFETY: exclusive lock held, so no transactions are active.
                let restored = unsafe {
                    env.resize(previous_map)
                        .map_err(|e| Error::Storage(format!("Failed to restore LMDB map: {e}")))
                };
                if restored.is_ok() {
                    ceiling_guard.armed = false;
                }

                // A failed restore is reported ahead of a failed delete, so the
                // failure is not lost behind the delete's own error.
                match (outcome, restored) {
                    (Ok(outcome), Ok(())) => Ok(outcome),
                    (_, Err(e)) | (Err(e), Ok(())) => Err(e),
                }
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB delete-slack task failed: {e}")))?;

        match outcome? {
            DeleteOutcome::Done(existed) => Ok(existed),
            DeleteOutcome::MapFull => Err(Error::Storage(
                "LMDB map full during delete even with the maintenance allowance".into(),
            )),
        }
    }

    /// Grow the LMDB map to match currently available disk space.
    ///
    /// The new size is the **larger** of:
    ///   1. the current map size (so existing data is never truncated), and
    ///   2. `current_db_file_size + available_space − reserve`
    ///      (so all reachable disk space can be used).
    ///
    /// Acquires an **exclusive** lock on `env_lock` so that no read or write
    /// transactions are active when the underlying `mdb_env_set_mapsize` is
    /// called (an LMDB safety requirement).
    #[allow(unsafe_code)]
    async fn try_resize(&self) -> Result<()> {
        let env = self.env.clone();
        let lock = Arc::clone(&self.env_lock);
        let no_growth = Arc::clone(&self.no_growth);
        let env_dir = self.env_dir.clone();
        let reserve = self.config.disk_reserve;

        self.blocking_tracker
            .spawn_blocking(move || -> Result<()> {
                // Exclusive lock guarantees no concurrent transactions.
                let _guard = lock.write();

                // Re-read under the lock. A `spawn_blocking` body outlives a
                // cancelled awaiter, so this closure may land after the store
                // entered no-growth mode. Growing then would hand back the
                // head-room a pin had just taken away, and with it the disk
                // reserve.
                if no_growth.load(Ordering::Acquire) {
                    return Ok(());
                }

                // Measured here rather than before the spawn, so a late closure
                // sizes from the disk as it is now, not as it was when queued.
                let from_disk = compute_map_size(&env_dir, reserve)?;

                // Never shrink below the current map — existing data must remain
                // addressable regardless of what the disk-space calculation says.
                let current_map = env.info().map_size;
                let new_size = from_disk.max(current_map);

                if new_size <= current_map {
                    debug!("LMDB map resize skipped — no additional disk space available");
                    return Ok(());
                }

                // SAFETY: We hold an exclusive lock, so no transactions are active.
                unsafe {
                    env.resize(new_size)
                        .map_err(|e| Error::Storage(format!("Failed to resize LMDB map: {e}")))?;
                }

                info!(
                    "Resized LMDB map to {:.2} GiB (was {:.2} GiB)",
                    bytes_to_gib(new_size as u64),
                    bytes_to_gib(current_map as u64),
                );
                Ok(())
            })
            .await
            .map_err(|e| Error::Storage(format!("LMDB resize task failed: {e}")))?
    }

    /// Wait until every tracked LMDB blocking task has finished.
    ///
    /// Dropping an async caller (e.g. a `select!` losing to a shutdown token)
    /// does not cancel an already-spawned blocking closure — the closure keeps
    /// running on the blocking pool with a cloned [`Env`].  This method waits
    /// for those detached closures too, so when it returns no blocking
    /// operation still holds the environment.
    ///
    /// Quiescence is only meaningful once callers have stopped issuing new
    /// operations; concurrent traffic can keep the tracker non-empty
    /// indefinitely.  The storage remains fully usable afterwards (the
    /// internal tracker is reopened before returning).
    pub async fn wait_idle(&self) {
        self.blocking_tracker.close();
        self.blocking_tracker.wait().await;
        self.blocking_tracker.reopen();
    }

    /// Test-only handle to the put gate.
    ///
    /// Hold the write half to deterministically park the next put inside its
    /// blocking closure (e.g. to exercise [`Self::wait_idle`] with a write
    /// still in flight).
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn test_put_gate(&self) -> Arc<parking_lot::RwLock<()>> {
        Arc::clone(&self.test_put_gate)
    }

    /// Test-only handle to the raw-read gate.
    ///
    /// Hold the write half to deterministically park the next raw read
    /// (`get_raw`) inside its blocking closure while it still holds the shared
    /// environment lock (e.g. to prove `try_resize` waits for active
    /// raw reads).
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn test_read_gate(&self) -> Arc<parking_lot::RwLock<()>> {
        Arc::clone(&self.test_read_gate)
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Helpers
// ────────────────────────────────────────────────────────────────────────────

/// Outcome of a single `try_put` attempt.
enum PutOutcome {
    /// Chunk was newly stored.
    New,
    /// Chunk already existed (idempotent).
    Duplicate,
    /// The LMDB map ceiling was reached — caller should resize and retry.
    MapFull,
}

/// Restores an LMDB map ceiling when dropped, including while unwinding.
///
/// The explicit restore in [`LmdbStorage::delete_with_slack`] is the normal
/// path, because it can report a failure to the caller. This exists so a panic
/// between the raise and that restore cannot leave the ceiling raised, which
/// would quietly hand ordinary writes the disk reserve.
struct MapCeilingRestorer<'a> {
    env: &'a Env,
    previous: usize,
    armed: bool,
}

impl Drop for MapCeilingRestorer<'_> {
    #[allow(unsafe_code)]
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        // SAFETY: the owner holds the exclusive `env_lock` for this whole
        // scope, so no transaction is active.
        unsafe {
            if let Err(e) = self.env.resize(self.previous) {
                warn!("Failed to restore the LMDB map ceiling while unwinding: {e}");
            }
        }
    }
}

/// Run one delete in its own write transaction, reporting `MapFull` rather than
/// raising it.
///
/// The caller owns the `env_lock` discipline: [`LmdbStorage::try_delete`] holds
/// the shared guard, [`LmdbStorage::delete_with_slack`] the exclusive one.
fn delete_in_txn(env: &Env, db: Database<Bytes, Bytes>, key: &XorName) -> Result<DeleteOutcome> {
    let mut wtxn = match env.write_txn() {
        Ok(wtxn) => wtxn,
        Err(heed::Error::Mdb(MdbError::MapFull)) => return Ok(DeleteOutcome::MapFull),
        Err(e) => return Err(Error::Storage(format!("Failed to create write txn: {e}"))),
    };
    let existed = match db.delete(&mut wtxn, key) {
        Ok(existed) => existed,
        Err(heed::Error::Mdb(MdbError::MapFull)) => return Ok(DeleteOutcome::MapFull),
        Err(e) => return Err(Error::Storage(format!("Failed to delete chunk: {e}"))),
    };
    match wtxn.commit() {
        Ok(()) => Ok(DeleteOutcome::Done(existed)),
        Err(heed::Error::Mdb(MdbError::MapFull)) => Ok(DeleteOutcome::MapFull),
        Err(e) => Err(Error::Storage(format!("Failed to commit delete: {e}"))),
    }
}

/// Outcome of one delete attempt.
enum DeleteOutcome {
    /// The delete committed; the flag is whether the key had existed.
    Done(bool),
    /// The map ceiling left no room for the delete's copy-on-write.
    MapFull,
}

/// Compute the LMDB map size from the disk hosting `db_dir`.
///
/// The result covers **all existing data** plus all remaining usable disk
/// space:
///
/// ```text
/// map_size = current_db_file_size + max(0, available_space − reserve)
/// ```
///
/// `available_space` (from `statvfs`) reports only the *free* bytes on the
/// partition — the DB file's own footprint is **not** included, so adding
/// it back ensures the map is always large enough for the data already
/// stored.
///
/// On Windows the disk-headroom term is additionally capped at
/// `WINDOWS_MAP_HEADROOM` to bound the map-proportional commit overhead (see
/// that constant); [`LmdbStorage::try_resize`] extends the map on demand as
/// data grows.
///
/// The result is page-aligned and never falls below [`MIN_MAP_SIZE`].
fn compute_map_size(db_dir: &Path, reserve: u64) -> Result<usize> {
    let available = fs2::available_space(db_dir)
        .map_err(|e| Error::Storage(format!("Failed to query available disk space: {e}")))?;

    // The MDB data file may not exist yet on first run, and that is the only reason to
    // read zero here. Any other failure is a question that was not answered, and answering
    // it with zero sizes the map as though the database were empty, which on a node with a
    // large one is a map far too small to open it.
    let mdb_file = db_dir.join("data.mdb");
    let current_db_bytes = match std::fs::metadata(&mdb_file) {
        Ok(meta) => meta.len(),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => 0,
        Err(e) => {
            return Err(Error::Storage(format!(
                "Failed to measure {}: {e}. Refusing to size the map as though it were \
                 empty.",
                mdb_file.display()
            )))
        }
    };

    let target = map_target_bytes(current_db_bytes, available, reserve);

    // Align up to system page size (required by heed's resize).
    let page = page_size::get() as u64;
    let aligned = target.div_ceil(page) * page;

    let result = usize::try_from(aligned).unwrap_or(usize::MAX);
    Ok(result.max(MIN_MAP_SIZE))
}

/// Head-room policy for the LMDB map, split out from [`compute_map_size`] so it
/// is unit-testable without touching the real filesystem.
///
/// `map = current_db_bytes + max(0, available − reserve)`, with the head-room
/// term capped at `WINDOWS_MAP_HEADROOM` on Windows. Existing data
/// (`current_db_bytes`) is always covered so a resize can never truncate the
/// database, even when the head-room cap or a nearly-full disk drives the
/// growth term to zero.
fn map_target_bytes(current_db_bytes: u64, available: u64, reserve: u64) -> u64 {
    // available_space excludes the DB file, so we add it back to get the
    // total space the DB could occupy while still leaving `reserve` free.
    let growth_room = available.saturating_sub(reserve);

    // On Windows, bound the head-room so the mapped size (and its
    // size-proportional commit overhead) stays proportional to stored data
    // rather than disk capacity. Elsewhere, use all reachable space.
    #[cfg(windows)]
    let growth_room = growth_room.min(WINDOWS_MAP_HEADROOM);

    current_db_bytes.saturating_add(growth_room)
}

/// Map the result of a space query onto the *disk half* of the capacity
/// verdict.
///
/// `Full` here means "below the reserve", which since ant-node #210 is only
/// half the refusal predicate: [`LmdbStorage::capacity_verdict`] goes on to
/// consult the store's reusable pages before refusing.
///
/// Split out from [`LmdbStorage::capacity_verdict`] because the three-way
/// mapping is the part worth proving, and proving it through the filesystem is
/// not portable: asking for the free space of a directory that does not exist
/// fails on Unix but succeeds on Windows, which resolves it to the volume.
fn verdict_from_available_space(available: std::io::Result<u64>, reserve: u64) -> CapacityVerdict {
    match available {
        Ok(available) if available < reserve => CapacityVerdict::Full,
        Ok(_) => CapacityVerdict::Writable,
        Err(e) => {
            warn!("Could not query available disk space: {e}");
            CapacityVerdict::Unknown
        }
    }
}

/// What a capacity check concluded, when the caller needs to tell "this node is
/// full" apart from "this node could not find out".
///
/// [`LmdbStorage::check_capacity`] collapses both into `Err`, which is right for
/// a caller that only wants to know whether to attempt a write. A caller
/// deciding *how long to stand down* needs the distinction: a full disk is a
/// standing condition worth waiting minutes on, while a failed `statvfs` may
/// have cleared by the next attempt and must not be treated as one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapacityVerdict {
    /// Available space is at or above the configured reserve. That is what the
    /// query establishes, and possibly from the TTL cache — not a promise the
    /// next write succeeds.
    Writable,
    /// Available space is below the configured reserve.
    Full,
    /// The query itself failed, so nothing is known about available space.
    Unknown,
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::ant_protocol::MAX_CHUNK_SIZE;

    /// Short probe used to prove `wait_idle` is still blocked on a parked op.
    const WAIT_IDLE_BLOCKED_PROBE: std::time::Duration = std::time::Duration::from_millis(200);
    /// Generous ceiling for `wait_idle` to complete once the op is released.
    const WAIT_IDLE_COMPLETE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
    /// Poll interval while waiting for a parked raw read to take the shared lock.
    const RAW_READ_LOCK_POLL: std::time::Duration = std::time::Duration::from_millis(5);
    /// Ceiling for a parked raw read to take the shared lock.
    const RAW_READ_LOCK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
    /// Short probe used to prove `try_resize` is still blocked on the shared lock.
    const RESIZE_BLOCKED_PROBE: std::time::Duration = std::time::Duration::from_millis(200);
    /// Generous ceiling for `try_resize` to complete once the raw read releases.
    const RESIZE_COMPLETE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

    #[test]
    fn map_target_covers_existing_data_and_headroom() {
        // A partition with far more free space than any node needs.
        let huge_free = 100 * 1024 * GIB; // 100 TiB
        let reserve = 500 * MIB;

        // Fresh node, no data yet.
        let fresh = map_target_bytes(0, huge_free, reserve);
        // Node already holding 16 GiB of chunks.
        let with_data = map_target_bytes(16 * GIB, huge_free, reserve);

        #[cfg(windows)]
        {
            // Windows: head-room is capped, so the map (and thus page tables)
            // stay bounded regardless of disk size. Existing data always sits
            // on top of the capped head-room.
            assert_eq!(fresh, WINDOWS_MAP_HEADROOM);
            assert_eq!(with_data, 16 * GIB + WINDOWS_MAP_HEADROOM);
            // Sanity: page-table cost (~map/512) is tens of MiB, not tens of GiB.
            assert!(with_data / 512 < 128 * MIB);
        }

        #[cfg(not(windows))]
        {
            // Other platforms keep the disk-sized map (lazy page tables cost
            // nothing), so head-room is the full free span minus reserve.
            assert_eq!(fresh, huge_free - reserve);
            assert_eq!(with_data, 16 * GIB + (huge_free - reserve));
        }
    }

    #[test]
    fn map_target_never_truncates_data_when_disk_nearly_full() {
        // Free space below the reserve → head-room saturates to 0 on every
        // platform, but the existing 4 GiB of data must still be covered.
        assert_eq!(map_target_bytes(4 * GIB, 100 * MIB, 500 * MIB), 4 * GIB);
    }

    /// Regression (V2-620 review): `all_keys` and `get_raw` must take the shared
    /// `env_lock` so their LMDB read transaction can never run concurrently with
    /// `try_resize()`'s unsafe `Env::resize()` — which this PR turns into a
    /// routine ~per-32-GiB Windows event. We prove it by holding the exclusive
    /// lock (as a resize does) and asserting both calls block until it is freed.
    ///
    /// Holding a `parking_lot` guard across `.await` is deliberate and safe here:
    /// the guard stays on this current-thread test task while `all_keys`/`get_raw`
    /// run their blocking work on the `spawn_blocking` pool (separate threads).
    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn read_paths_block_while_env_is_being_resized() {
        use std::time::Duration;
        let (storage, _temp) = create_test_storage().await;

        // Store one chunk so the read paths have real work to return.
        let content = b"resize-safety";
        let address = LmdbStorage::compute_address(content);
        storage.put(&address, content).await.expect("put");

        // Simulate a resize in progress: hold the exclusive env lock.
        let write_guard = storage.env_lock.write();

        // Neither read path may complete while the exclusive lock is held —
        // before the fix they took no lock and would return immediately.
        assert!(
            tokio::time::timeout(Duration::from_millis(250), storage.all_keys())
                .await
                .is_err(),
            "all_keys completed while env_lock was held exclusively — missing shared guard"
        );
        assert!(
            tokio::time::timeout(Duration::from_millis(250), storage.get_raw(&address))
                .await
                .is_err(),
            "get_raw completed while env_lock was held exclusively — missing shared guard"
        );

        // Once the exclusive lock is released, both proceed and see the data.
        drop(write_guard);
        assert_eq!(storage.all_keys().await.expect("all_keys").len(), 1);
        assert_eq!(
            storage.get_raw(&address).await.expect("get_raw").as_deref(),
            Some(content.as_slice())
        );
    }

    async fn create_test_storage() -> (LmdbStorage, tempfile::TempDir) {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let config = LmdbStorageConfig {
            root_dir: temp_dir.path().to_path_buf(),
            ..LmdbStorageConfig::test_default()
        };
        let storage = LmdbStorage::new(config).await.expect("create storage");
        (storage, temp_dir)
    }

    #[tokio::test]
    async fn test_put_and_get() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"hello world";
        let address = LmdbStorage::compute_address(content);

        // Store chunk
        let is_new = storage.put(&address, content).await.expect("put");
        assert!(is_new);

        // Retrieve chunk
        let retrieved = storage.get(&address).await.expect("get");
        assert_eq!(retrieved, Some(content.to_vec()));
    }

    #[tokio::test]
    async fn test_put_duplicate() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"test data";
        let address = LmdbStorage::compute_address(content);

        // First store
        let is_new1 = storage.put(&address, content).await.expect("put 1");
        assert!(is_new1);

        // Duplicate store
        let is_new2 = storage.put(&address, content).await.expect("put 2");
        assert!(!is_new2);

        // Check stats
        let stats = storage.stats();
        assert_eq!(stats.chunks_stored, 1);
        assert_eq!(stats.duplicates, 1);
    }

    #[tokio::test]
    async fn test_get_not_found() {
        let (storage, _temp) = create_test_storage().await;

        let address = [0xAB; 32];
        let result = storage.get(&address).await.expect("get");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_exists() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"exists test";
        let address = LmdbStorage::compute_address(content);

        assert!(!storage.exists(&address).expect("exists"));

        storage.put(&address, content).await.expect("put");

        assert!(storage.exists(&address).expect("exists"));
    }

    #[tokio::test]
    async fn test_delete() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"delete test";
        let address = LmdbStorage::compute_address(content);

        // Store
        storage.put(&address, content).await.expect("put");
        assert!(storage.exists(&address).expect("exists"));

        // Delete
        let deleted = storage.delete(&address).await.expect("delete");
        assert!(deleted);
        assert!(!storage.exists(&address).expect("exists"));

        // Delete again (already deleted)
        let deleted2 = storage.delete(&address).await.expect("delete 2");
        assert!(!deleted2);
    }

    #[tokio::test]
    async fn test_address_mismatch() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"some content";
        let wrong_address = [0xFF; 32]; // Wrong address

        let result = storage.put(&wrong_address, content).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("mismatch"));
    }

    #[test]
    fn test_compute_address() {
        // Known BLAKE3 hash of "hello world"
        let content = b"hello world";
        let address = LmdbStorage::compute_address(content);

        let expected_hex = "d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
        assert_eq!(hex::encode(address), expected_hex);
    }

    #[tokio::test]
    async fn test_stats() {
        let (storage, _temp) = create_test_storage().await;

        let content1 = b"content 1";
        let content2 = b"content 2";
        let address1 = LmdbStorage::compute_address(content1);
        let address2 = LmdbStorage::compute_address(content2);

        // Store two chunks
        storage.put(&address1, content1).await.expect("put 1");
        storage.put(&address2, content2).await.expect("put 2");

        // Retrieve one
        storage.get(&address1).await.expect("get");

        let stats = storage.stats();
        assert_eq!(stats.chunks_stored, 2);
        assert_eq!(stats.chunks_retrieved, 1);
        assert_eq!(
            stats.bytes_stored,
            content1.len() as u64 + content2.len() as u64
        );
        assert_eq!(stats.bytes_retrieved, content1.len() as u64);
        assert_eq!(stats.current_chunks, 2);
    }

    #[tokio::test]
    async fn test_persistence_across_reopen() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let content = b"persistent data";
        let address = LmdbStorage::compute_address(content);

        // Store a chunk
        {
            let config = LmdbStorageConfig {
                root_dir: temp_dir.path().to_path_buf(),
                ..LmdbStorageConfig::test_default()
            };
            let storage = LmdbStorage::new(config).await.expect("create storage");
            storage.put(&address, content).await.expect("put");
        }

        // Re-open and verify it persisted
        {
            let config = LmdbStorageConfig {
                root_dir: temp_dir.path().to_path_buf(),
                ..LmdbStorageConfig::test_default()
            };
            let storage = LmdbStorage::new(config).await.expect("reopen storage");
            assert_eq!(storage.current_chunks().expect("current_chunks"), 1);
            let retrieved = storage.get(&address).await.expect("get");
            assert_eq!(retrieved, Some(content.to_vec()));
        }
    }

    #[tokio::test]
    async fn test_all_keys() {
        let (storage, _temp) = create_test_storage().await;

        // Empty storage
        let keys = storage.all_keys().await.expect("all_keys empty");
        assert!(keys.is_empty());

        // Store some chunks
        let content1 = b"chunk one for keys";
        let content2 = b"chunk two for keys";
        let addr1 = LmdbStorage::compute_address(content1);
        let addr2 = LmdbStorage::compute_address(content2);
        storage.put(&addr1, content1).await.expect("put 1");
        storage.put(&addr2, content2).await.expect("put 2");

        let mut keys = storage.all_keys().await.expect("all_keys");
        keys.sort_unstable();
        let mut expected = vec![addr1, addr2];
        expected.sort_unstable();
        assert_eq!(keys, expected);
    }

    #[tokio::test]
    async fn test_get_raw() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"raw test data";
        let address = LmdbStorage::compute_address(content);
        storage.put(&address, content).await.expect("put");

        // get_raw returns bytes without verification
        let raw = storage.get_raw(&address).await.expect("get_raw");
        assert_eq!(raw, Some(content.to_vec()));

        // Non-existent key
        let missing = storage.get_raw(&[0xFF; 32]).await.expect("get_raw missing");
        assert!(missing.is_none());
    }

    /// Dropping a put's awaiter does not cancel its `spawn_blocking` LMDB
    /// transaction; `wait_idle` must wait for that detached write, and the
    /// storage must remain usable afterwards.
    // Holding the gate's write guard across awaits is the point of the test:
    // it parks the blocking closure while we probe wait_idle.
    #[allow(clippy::await_holding_lock)]
    #[tokio::test]
    async fn wait_idle_waits_for_detached_put_blocking_op() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"detached put survives its dropped awaiter";
        let address = LmdbStorage::compute_address(content);

        // Park the put's blocking closure on the test gate.
        let gate = storage.test_put_gate();
        let parked = gate.write();

        // Drop the awaiting future mid-flight: the biased select! polls the
        // put once — far enough to spawn the blocking task, which parks on
        // the gate — then completes on the ready branch, dropping the put.
        tokio::select! {
            biased;
            res = storage.put(&address, content) => {
                panic!("put must be parked on the test gate, got {res:?}")
            }
            () = std::future::ready(()) => {}
        }

        // The blocking op is still running: wait_idle must not complete.
        let blocked = tokio::time::timeout(WAIT_IDLE_BLOCKED_PROBE, storage.wait_idle()).await;
        assert!(
            blocked.is_err(),
            "wait_idle returned while the blocking op was parked"
        );

        // Release the gate: the detached closure commits and exits.
        drop(parked);
        tokio::time::timeout(WAIT_IDLE_COMPLETE_TIMEOUT, storage.wait_idle())
            .await
            .expect("wait_idle after release");

        // The dropped awaiter did not lose the write: it committed.
        assert!(storage.exists(&address).expect("exists after release"));

        // The storage remains usable after wait_idle (tracker reopened).
        let more = b"storage still usable after wait_idle";
        let more_addr = LmdbStorage::compute_address(more);
        assert!(storage
            .put(&more_addr, more)
            .await
            .expect("put after wait_idle"));
    }

    /// A map resize takes the environment's *exclusive* lock, so it must wait
    /// for in-flight raw reads (which hold the *shared* lock) to finish before
    /// calling `env.resize()`. This proves `get_raw` holds that shared lock for
    /// the whole duration of its blocking closure; `all_keys` uses the same
    /// guard.
    // Holding the gate's write guard across awaits is the point of the test:
    // it parks the raw read's blocking closure while we probe try_resize.
    #[allow(clippy::await_holding_lock)]
    #[tokio::test]
    async fn resize_waits_for_in_flight_raw_read() {
        let (storage, _temp) = create_test_storage().await;

        let content = b"raw read holds the shared env lock";
        let address = LmdbStorage::compute_address(content);
        storage.put(&address, content).await.expect("put");

        // Park the raw read's blocking closure on the test gate. It acquires
        // the shared env_lock first, then parks here still holding it.
        let gate = storage.test_read_gate();
        let parked = gate.write();

        // Drop the awaiting future mid-flight: the biased select! polls get_raw
        // once — far enough to spawn the blocking task, which takes the shared
        // lock and parks on the gate — then completes on the ready branch,
        // dropping the awaiter. The detached closure keeps holding the lock.
        tokio::select! {
            biased;
            res = storage.get_raw(&address) => {
                panic!("get_raw must be parked on the test gate, got {res:?}")
            }
            () = std::future::ready(()) => {}
        }

        // Wait until the detached read has actually taken the shared lock,
        // signalled by the exclusive half no longer being immediately available.
        tokio::time::timeout(RAW_READ_LOCK_TIMEOUT, async {
            loop {
                let free = storage.env_lock.try_write().is_some();
                if !free {
                    break;
                }
                tokio::time::sleep(RAW_READ_LOCK_POLL).await;
            }
        })
        .await
        .expect("raw read did not take the shared env lock");

        // A resize needs the exclusive lock, so it must block while the raw
        // read holds the shared lock.
        let resize = storage.try_resize();
        tokio::pin!(resize);
        let blocked = tokio::time::timeout(RESIZE_BLOCKED_PROBE, &mut resize).await;
        assert!(
            blocked.is_err(),
            "try_resize completed while a raw read held the shared env lock"
        );

        // Release the read: it drops the shared lock, letting the resize take
        // the exclusive lock and finish.
        drop(parked);
        tokio::time::timeout(RESIZE_COMPLETE_TIMEOUT, &mut resize)
            .await
            .expect("try_resize did not complete after the raw read released")
            .expect("try_resize");
    }

    /// The gate fires on `Full` and only on `Full`, so the verdict has to tell a
    /// disk below its reserve from one above it, and has to notice when that
    /// stops being true.
    ///
    /// The third call is the one that matters for recovery: the below-reserve
    /// condition clears, and the verdict has to follow it rather than stay stuck
    /// on its earlier answer. A node that remembered a refusal would stop
    /// fetching for good.
    ///
    /// What this does not show: that a changed free-space reading is re-read from
    /// the filesystem. The condition is cleared by dropping the reserve, which is
    /// the same comparison approached from the other side.
    #[tokio::test]
    async fn capacity_verdict_follows_the_reserve_and_notices_recovery() {
        let (writable, _temp) = create_test_storage().await;
        assert_eq!(writable.capacity_verdict(), CapacityVerdict::Writable);

        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let config = LmdbStorageConfig {
            root_dir: temp_dir.path().to_path_buf(),
            // Far above any real free space, the same way the e2e builds a
            // write-blocked node.
            disk_reserve: u64::MAX / 2,
            ..LmdbStorageConfig::test_default()
        };
        let mut full = LmdbStorage::new(config).await.expect("create storage");
        assert_eq!(full.capacity_verdict(), CapacityVerdict::Full);

        // Still short of space, so still `Full`. A `Full` result that populated
        // the passing-result cache would answer `Writable` here.
        assert_eq!(full.capacity_verdict(), CapacityVerdict::Full);

        // Space is no longer short. Nothing cached a refusal, so the very next
        // read has to see it.
        full.config.disk_reserve = 0;
        assert_eq!(
            full.capacity_verdict(),
            CapacityVerdict::Writable,
            "a refusal must not be negatively cached: the next read has to see the \
             below-reserve condition clear"
        );
    }

    /// A space query that fails says nothing about available space, so it must
    /// not read as a full disk. The gate stands a key down for five minutes on
    /// `Full` alone, and a failed `statvfs` is not a condition worth standing
    /// down for: it may be gone by the next cycle.
    ///
    /// The two `Ok` cases pin the boundary the reserve names: equal to the
    /// reserve is writable, one byte under it is not.
    ///
    /// What this does not show: that the gate leaves `Unknown` alone. The gate
    /// tests `== Full` on a separate line inside the verification cycle, which
    /// needs a network to reach, so this covers the classification only.
    #[test]
    fn a_failed_space_query_reads_as_unknown_not_full() {
        const RESERVE: u64 = 1024;

        assert_eq!(
            verdict_from_available_space(Err(std::io::Error::other("space query failed")), RESERVE),
            CapacityVerdict::Unknown,
            "a failed query must not be reported as a full disk"
        );

        assert_eq!(
            verdict_from_available_space(Ok(RESERVE - 1), RESERVE),
            CapacityVerdict::Full
        );
        assert_eq!(
            verdict_from_available_space(Ok(RESERVE), RESERVE),
            CapacityVerdict::Writable,
            "at the reserve is not below it"
        );
    }

    /// The verdict and the pre-check have to refuse on the same condition.
    ///
    /// They are two readings of one question — can this node write? — and two
    /// callers depend on them separately: the verification cycle gates the
    /// close-group probe on the verdict, while `execute_single_fetch` gates the
    /// dial on the pre-check. A verdict stricter than the pre-check is the
    /// harmful direction. A node the pre-check would let write stops
    /// discovering holders for keys it could have stored, which is
    /// under-replication rather than a saved probe, and nothing else in the
    /// change would notice.
    ///
    /// This is a tripwire, deliberately built on the state where the two are
    /// about to part company rather than on a bare full disk. ant-node
    /// \#210 makes the pre-check a two-part predicate — below the reserve *and*
    /// out of reusable pages inside the store — and a store that has deleted
    /// more than one chunk's worth of pages fails only the first half, because
    /// LMDB returns those pages to its own free list and never to the
    /// filesystem. On a bare full disk the two predicates still agree, so a
    /// test built on one would pass straight through the divergence. Whichever
    /// of the two changes merges second has to carry the second half into the
    /// verdict, and this is what makes that a red test rather than a textual
    /// conflict resolved without it.
    ///
    /// What this does not show: which of `Full` and `Unknown` a refusal is.
    /// `a_failed_space_query_reads_as_unknown_not_full` pins that, and only
    /// `Full` reaches the gate.
    #[tokio::test]
    async fn capacity_verdict_refuses_exactly_when_check_capacity_does() {
        let (mut storage, _temp) = create_test_storage().await;
        assert_eq!(storage.capacity_verdict(), CapacityVerdict::Writable);
        assert!(
            storage.check_capacity().is_ok(),
            "a writable verdict has to mean the pre-check admits the write"
        );

        // Two chunks written and deleted, so the store sits on more than one
        // chunk of space LMDB can reuse and the filesystem will never take
        // back. This is the pruned node the two predicates disagree about.
        for fill in [1u8, 2u8] {
            let content = vec![fill; MAX_CHUNK_SIZE];
            let address = LmdbStorage::compute_address(&content);
            assert!(storage.put(&address, &content).await.expect("put"));
            assert!(storage.delete(&address).await.expect("delete"));
        }

        // Established without either function under test, so the scenario does
        // not rest on the thing being measured.
        storage.config.disk_reserve = u64::MAX / 2;
        *storage.last_disk_ok.lock() = None;
        let available = fs2::available_space(&storage.env_dir).expect("query available space");
        assert!(
            available < storage.config.disk_reserve,
            "the volume has to read as below the reserve, or neither predicate is \
             being asked the interesting question"
        );
        // Reusable bytes read the way the two-part predicate reads them: the
        // file's size less the pages still holding data. `Env::stat` rather
        // than `non_free_pages_size`, which calls `String::from_utf8(key)` and
        // unwraps, so it panics on our 32 random bytes of key.
        let stat = storage.env.stat();
        let live_pages = (stat.branch_pages as u64)
            .saturating_add(stat.leaf_pages as u64)
            .saturating_add(stat.overflow_pages as u64);
        let live_bytes = live_pages.saturating_mul(u64::from(stat.page_size));
        let file_bytes = storage.env.real_disk_size().expect("query store file size");
        let reusable = file_bytes.saturating_sub(live_bytes);
        assert!(
            reusable > MAX_CHUNK_SIZE as u64,
            "the store has to sit on more than one chunk of reusable space, or the \
             two predicates are not yet being asked to differ \
             (file_bytes={file_bytes}, live_bytes={live_bytes})"
        );

        // Neither call caches a refusal, so the order of the two does not
        // decide either answer.
        let pre_check_refuses = storage.check_capacity().is_err();
        let verdict = storage.capacity_verdict();
        assert_eq!(
            pre_check_refuses,
            verdict != CapacityVerdict::Writable,
            "the dial pre-check and the verification gate disagree about whether \
             this node can write: check_capacity refuses={pre_check_refuses}, \
             verdict={verdict:?}. A verdict of Full under a pre-check that admits \
             the write leaves a node that can store chunks refusing to look for \
             them"
        );
    }

    // ── Capacity below the disk reserve (LMDB reuse) ────────────────────

    /// Value size for the reuse tests. A whole number of chunks' worth, so the
    /// space freed by a few deletes is unambiguously enough for one more.
    const REUSE_VALUE_LEN: usize = 1024 * 1024;

    /// Distinct filler of `REUSE_VALUE_LEN` bytes.
    fn reuse_filler(seed: u32) -> Vec<u8> {
        let mut content = seed.to_le_bytes().to_vec();
        content.resize(REUSE_VALUE_LEN, 0u8);
        content
    }

    /// A config for `dir` whose reserve exceeds any real disk, so the store
    /// always sees itself as below the reserve.
    fn below_reserve_config(dir: &Path) -> LmdbStorageConfig {
        LmdbStorageConfig {
            root_dir: dir.to_path_buf(),
            disk_reserve: u64::MAX,
            ..LmdbStorageConfig::test_default()
        }
    }

    /// Write `count` chunks with an unconstrained reserve, returning their
    /// addresses in insertion order.
    async fn seed_chunks(dir: &Path, count: u32) -> Vec<XorName> {
        let config = LmdbStorageConfig {
            root_dir: dir.to_path_buf(),
            ..LmdbStorageConfig::test_default()
        };
        let storage = LmdbStorage::new(config).await.expect("create storage");

        let mut addresses = Vec::new();
        for seed in 0..count {
            let content = reuse_filler(seed);
            let address = LmdbStorage::compute_address(&content);
            storage.put(&address, &content).await.expect("seed put");
            addresses.push(address);
        }

        storage.wait_idle().await;
        addresses
    }

    fn file_len(storage: &LmdbStorage) -> u64 {
        storage.env.real_disk_size().expect("real_disk_size")
    }

    /// The regression this change is about: a node whose volume is below the
    /// reserve must still write into pages an earlier delete freed. Before the
    /// fix the pre-check refused on the disk half alone, so a node that had
    /// pruned sat on reusable space it could not use.
    #[tokio::test]
    async fn below_reserve_put_reuses_freed_pages() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let seeded = seed_chunks(temp_dir.path(), 12).await;

        let storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        let content = reuse_filler(u32::MAX);
        let address = LmdbStorage::compute_address(&content);

        // Nothing freed yet, so this write would have to grow the file. Below
        // the reserve that is exactly what must be refused.
        assert!(
            storage.put(&address, &content).await.is_err(),
            "a write that must grow the file was allowed below the reserve"
        );

        // Free several chunks. Their pages go on LMDB's free list, not back to
        // the filesystem, so `statvfs` still reports the volume as full.
        for seeded_address in seeded.iter().take(6) {
            assert!(storage.delete(seeded_address).await.expect("delete"));
        }

        let before = file_len(&storage);
        let stored = storage
            .put(&address, &content)
            .await
            .expect("put into freed pages was refused below the reserve");
        assert!(stored);
        assert_eq!(
            storage.get(&address).await.expect("get"),
            Some(content),
            "chunk written into reused pages did not read back"
        );

        // The whole point: it was served from inside the existing file.
        assert_eq!(
            file_len(&storage),
            before,
            "reusing freed pages grew data.mdb, consuming the reserve"
        );
    }

    /// A refused write must not have grown the file on its way to failing,
    /// which is what protects the reserve while the map is pinned.
    #[tokio::test]
    async fn below_reserve_refused_put_does_not_grow_the_file() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let _ = seed_chunks(temp_dir.path(), 6).await;

        let storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        let content = reuse_filler(u32::MAX);
        let address = LmdbStorage::compute_address(&content);

        // Take the baseline after the first attempt, so it includes the pin.
        let refusal = storage
            .put(&address, &content)
            .await
            .expect_err("a write that must grow the file was allowed");
        assert!(
            refusal.to_string().contains("Insufficient disk space"),
            "refused for the wrong reason: {refusal}"
        );
        let before = file_len(&storage);
        let pinned_map = storage.env.info().map_size;

        for seed in 0..4u32 {
            let content = reuse_filler(u32::MAX - 1 - seed);
            let address = LmdbStorage::compute_address(&content);
            let refusal = storage
                .put(&address, &content)
                .await
                .expect_err("a write that must grow the file was allowed");
            assert!(
                refusal.to_string().contains("Insufficient disk space"),
                "refused for the wrong reason: {refusal}"
            );
        }

        assert_eq!(
            storage.env.info().map_size,
            pinned_map,
            "the map ceiling drifted while writes were being refused"
        );

        assert_eq!(
            file_len(&storage),
            before,
            "refused writes still extended data.mdb into the reserve"
        );
    }

    /// One refused maximum-sized value must not lock out smaller ones. LMDB's
    /// `MapFull` is specific to the allocation it was asked for, so remembering
    /// it store-wide would let a single large chunk deny every later write.
    #[tokio::test]
    async fn large_refusal_does_not_block_a_smaller_put() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let seeded = seed_chunks(temp_dir.path(), 10).await;

        let storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        // Free room for a small value, but not for a large one.
        let Some(first) = seeded.first() else {
            panic!("seed_chunks returned no addresses");
        };
        assert!(storage.delete(first).await.expect("delete"));

        // A value far larger than what was freed cannot fit.
        let oversized = vec![3u8; 8 * REUSE_VALUE_LEN];
        let oversized_address = LmdbStorage::compute_address(&oversized);
        assert!(storage.put(&oversized_address, &oversized).await.is_err());

        // A small value still must, using the pages the delete released.
        let small = b"small record that fits in a freed page".to_vec();
        let small_address = LmdbStorage::compute_address(&small);
        let stored = storage
            .put(&small_address, &small)
            .await
            .expect("a large refusal blocked a small put that had room");
        assert!(stored);
    }

    /// A store with no reusable page must still be able to delete, or it can
    /// never prune its way back to health.
    #[tokio::test]
    async fn full_store_below_reserve_can_still_delete() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let seeded = seed_chunks(temp_dir.path(), 8).await;

        let storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        // Pin the map by attempting a write that cannot fit.
        let content = reuse_filler(u32::MAX);
        let address = LmdbStorage::compute_address(&content);
        assert!(storage.put(&address, &content).await.is_err());
        assert!(storage.no_growth.load(Ordering::Acquire));

        let pinned_map = storage.env.info().map_size;

        for seeded_address in &seeded {
            assert!(
                storage.delete(seeded_address).await.expect("delete"),
                "a pinned store could not prune"
            );
        }
        assert_eq!(storage.current_chunks().expect("current_chunks"), 0);

        // The allowance bounds permanent file growth, not the number of
        // assisted deletes. Pruning a pinned store must stay possible however
        // many deletes it takes, so whatever was charged has to be growth the
        // file really took, and has to stay inside the budget.
        let charged = storage.delete_growth_charged.load(Ordering::Acquire);
        assert!(
            charged < DELETE_COW_GROWTH_BUDGET,
            "deletes exhausted the growth allowance ({charged} B) while pruning a pinned store"
        );

        // Whether or not any delete needed the maintenance allowance, none of
        // it may be left in the ceiling afterwards: a raised ceiling is
        // ordinary put capacity, so leaking it hands away the reserve.
        let file_bytes = file_len(&storage);
        assert!(
            storage.env.info().map_size as u64 <= file_bytes.max(pinned_map as u64),
            "delete left maintenance slack in the map ceiling"
        );

        // And the store must still refuse a write it cannot fit, i.e. the pin
        // is still doing its job after the prune.
        let oversized = vec![9u8; 64 * REUSE_VALUE_LEN];
        let oversized_address = LmdbStorage::compute_address(&oversized);
        assert!(
            storage.put(&oversized_address, &oversized).await.is_err(),
            "pinning stopped working after a delete used the allowance"
        );
    }

    /// The pre-check must admit while reuse is plausible and refuse once it is
    /// not. Refusing on `statvfs` alone is what blinded a node to its own free
    /// pages, so being below the reserve cannot by itself be an error.
    #[tokio::test]
    async fn check_capacity_tracks_reusable_space_not_just_disk() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let seeded = seed_chunks(temp_dir.path(), 16).await;

        let storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        // A freshly written store has almost no free page, so below the reserve
        // the pre-check refuses and the caller skips its expensive setup.
        assert!(
            storage.check_capacity().is_err(),
            "pre-check stayed open on a store with no reusable space"
        );

        // Pruning puts pages back on the free list. Nothing is returned to the
        // filesystem, so `statvfs` is unchanged and only the reusable half of
        // the predicate can reopen the node.
        for seeded_address in seeded.iter().take(10) {
            assert!(storage.delete(seeded_address).await.expect("delete"));
        }

        storage
            .check_capacity()
            .expect("pre-check stayed closed after pruning freed pages");
    }

    /// Freeing disk must lift the pin, or a node would stay clamped to its
    /// high-water mark after an operator grew the partition.
    #[tokio::test]
    async fn leaving_no_growth_restores_head_room() {
        let temp_dir = tempfile::TempDir::new().expect("create temp dir");
        let _ = seed_chunks(temp_dir.path(), 6).await;

        let mut storage = LmdbStorage::new(below_reserve_config(temp_dir.path()))
            .await
            .expect("reopen storage");

        let content = reuse_filler(u32::MAX);
        let address = LmdbStorage::compute_address(&content);
        assert!(storage.put(&address, &content).await.is_err());
        assert!(storage.no_growth.load(Ordering::Acquire));
        let pinned_map = storage.env.info().map_size;

        // Simulate the operator freeing space: the reserve is now satisfiable.
        storage.config.disk_reserve = 0;
        *storage.last_disk_ok.lock() = None;

        let stored = storage
            .put(&address, &content)
            .await
            .expect("store stayed pinned after disk was freed");
        assert!(stored);
        assert!(!storage.no_growth.load(Ordering::Acquire));
        assert!(
            storage.env.info().map_size > pinned_map,
            "map was not re-grown after leaving no-growth mode"
        );
    }

    /// Above the reserve nothing changes: no pinning, and writes grow the file
    /// on demand exactly as before.
    #[tokio::test]
    async fn above_reserve_behaviour_is_unchanged() {
        let (storage, _temp) = create_test_storage().await;

        storage
            .check_capacity()
            .expect("pre-check on a healthy node");

        let content = reuse_filler(1);
        let address = LmdbStorage::compute_address(&content);
        assert!(storage.put(&address, &content).await.expect("put"));
        assert!(!storage.no_growth.load(Ordering::Acquire));
        storage
            .check_capacity()
            .expect("pre-check after a healthy put");
    }
}