ethrex-storage 30.0.0

Persistent block and state storage layer for the ethrex Ethereum execution client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
#[cfg(feature = "rocksdb")]
use crate::backend::rocksdb::RocksDBBackend;
use crate::{
    STORE_METADATA_FILENAME, STORE_SCHEMA_VERSION,
    api::{
        StorageBackend, StorageReadView, StorageWriteBatch,
        tables::{
            ACCOUNT_CODE_METADATA, ACCOUNT_CODES, ACCOUNT_FLATKEYVALUE, ACCOUNT_TRIE_NODES,
            BAD_BLOCKS, BLOCK_ACCESS_LISTS, BLOCK_NUMBERS, BODIES, CANONICAL_BLOCK_HASHES,
            CHAIN_DATA, EXECUTION_WITNESSES, FULLSYNC_HEADERS, HEADERS, INVALID_CHAINS,
            MISC_VALUES, PENDING_BLOCKS, RECEIPTS_V2, SNAP_STATE, STATE_HISTORY,
            STORAGE_FLATKEYVALUE, STORAGE_TRIE_NODES, TRANSACTION_LOCATIONS,
        },
    },
    apply_prefix,
    backend::in_memory::InMemoryBackend,
    block_data_buffer::BlockDataBuffer,
    error::StoreError,
    journal::{FlatDiff, JournalEntry},
    layering::{Overlay, OverlayCf, TrieLayerCache, TrieWrapper},
    rlp::{BlockBodyRLP, BlockHeaderRLP, BlockRLP},
    trie::{BackendTrieDB, BackendTrieDBLocked, classify_trie_key},
    utils::{ChainDataIndex, SnapStateIndex},
};

use ethrex_common::{
    Address, H256, U256,
    seeded_hash::{SeededHashSet, SeededState},
    types::{
        AccountInfo, AccountState, AccountUpdate, Block, BlockBody, BlockHash, BlockHeader,
        BlockNumber, ChainConfig, Code, CodeMetadata, ForkId, Genesis, GenesisAccount, Index,
        Receipt, Transaction,
        block_access_list::BlockAccessList,
        block_execution_witness::{ExecutionWitness, RpcExecutionWitness},
    },
    utils::keccak,
};
use ethrex_crypto::{NativeCrypto, keccak::keccak_hash};
use ethrex_rlp::{
    decode::{RLPDecode, decode_bytes, decode_rlp_item},
    encode::RLPEncode,
};
use ethrex_trie::{EMPTY_TRIE_HASH, Nibbles, Trie, TrieLogger, TrieNode, TrieWitness};
use ethrex_trie::{Node, NodeRLP};
use indexmap::IndexMap;
use lru::LruCache;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, HashMap, HashSet, hash_map::Entry},
    fmt::Debug,
    hash::BuildHasher,
    io::Write,
    num::NonZeroUsize,
    path::{Path, PathBuf},
    sync::{
        Arc, Condvar, Mutex, RwLock,
        atomic::{AtomicUsize, Ordering},
        mpsc::{SyncSender, TryRecvError, sync_channel},
    },
    thread::JoinHandle,
};
use tracing::{debug, error, info, warn};

/// Maximum number of execution witnesses to keep in the database
pub const MAX_WITNESSES: u64 = 128;

// We use one constant for in-memory and another for on-disk backends.
// This is due to tests requiring state older than 128 blocks.
// TODO: unify these
pub const DB_COMMIT_THRESHOLD: usize = 128;
const IN_MEMORY_COMMIT_THRESHOLD: usize = 10000;

/// Depth-only commit threshold for batch execution (full sync / block import). Each batch
/// layer holds ~1024 blocks of trie diffs (~1 GB), so we flush after a few layers to bound
/// memory. The canonical `head - DB_COMMIT_THRESHOLD` safe-commit root never lands on a batch
/// layer boundary, so batch mode commits by depth instead; this is sound because full sync and
/// import only ever extend a single canonical chain (no competing forks to mis-commit).
pub const BATCH_COMMIT_THRESHOLD: usize = 4;

/// Sorted, sharded `multi_get` tuning, shared by the account, storage, and
/// trie-node batch read paths. A single `multi_get` runs at queue depth 1
/// (async_io is off), so cold batches are split into contiguous sorted shards
/// read on separate blocking threads. `KEYS_PER_SHARD` targets shard size,
/// `MAX_SHARDS` caps concurrency; scattered keys make 64 the sweet spot (128
/// over-fragments; coldbench).
const KEYS_PER_SHARD: usize = 256;
const MAX_SHARDS: usize = 64;

/// Shard count for a sorted, sharded `multi_get` over `n` keys.
fn shard_count(n: usize) -> usize {
    n.div_ceil(KEYS_PER_SHARD).clamp(1, MAX_SHARDS)
}

/// Ceiling on the RocksDB shared block cache: 12 GiB.
///
/// This cache holds both data blocks AND the index/bloom-filter blocks for every
/// open SST file (because we enable `cache_index_and_filter_blocks`), so its size
/// is the effective upper bound on RocksDB's resident memory footprint. 12 GiB
/// keeps the filter/index working set resident plus hot EVM state; a sweep on a
/// synced mainnet node (32 GiB cap) found 8-16 GiB all keep up with head-following,
/// with larger giving no gain (the OS page cache backstops the uncompressed state
/// CFs) and ~8 GiB the floor where the filter set starts to thrash.
pub const MAX_ROCKSDB_BLOCK_CACHE_SIZE_BYTES: usize = 12 * 1024 * 1024 * 1024;

/// Floor on the RocksDB shared block cache: 512 MiB. Below this the per-SST
/// index and filter blocks no longer stay resident and every trie read pays an
/// extra disk seek, which is worse than the memory it saves. Applied even when
/// it exceeds [`ROCKSDB_BLOCK_CACHE_MEMORY_PERCENT`] of a very small host, and
/// even when it exceeds the detected limit outright: a node given less than this
/// much memory cannot follow the chain anyway, so the floor is the more useful
/// failure mode than a cache too small to hold the filters.
pub const MIN_ROCKSDB_BLOCK_CACHE_SIZE_BYTES: usize = 512 * 1024 * 1024;

/// Share of the host's (or cgroup's) memory the block cache may claim by default.
///
/// The rest has to cover the in-memory trie-layer backlog, block execution, the
/// mempool, peer buffers and allocator slack, so the cache cannot have most of the
/// machine. At 40% a 32 GiB host lands on the 12 GiB ceiling and a 16 GiB host gets
/// ~6.4 GiB — under the ~8 GiB thrash floor, but that is the memory-constrained
/// tradeoff the ceiling's docs describe, and it leaves the node room not to be
/// OOM-killed.
pub const ROCKSDB_BLOCK_CACHE_MEMORY_PERCENT: usize = 40;

/// Default RocksDB shared block cache size, derived from the memory this process
/// is actually allowed to use.
///
/// A fixed default cannot serve both a 64 GiB validator and a 16 GiB CI runner: the
/// ceiling alone is 71% of a 16 GiB host, which leaves no headroom for the rest of
/// the node. Sizes to [`ROCKSDB_BLOCK_CACHE_MEMORY_PERCENT`] of the detected limit —
/// the smaller of physical memory and the cgroup limit, so a container is sized
/// against its own allowance rather than the machine it lands on — clamped to
/// [`MIN_ROCKSDB_BLOCK_CACHE_SIZE_BYTES`] ..=
/// [`MAX_ROCKSDB_BLOCK_CACHE_SIZE_BYTES`]. Falls back to the ceiling when the limit
/// cannot be detected, preserving the previous behavior.
pub fn default_rocksdb_block_cache_size() -> usize {
    let physical = physical_memory_bytes();
    let cgroup = cgroup_memory_limit_bytes();
    let limit = [physical, cgroup].into_iter().flatten().min();
    let size = rocksdb_block_cache_size_for(limit);
    // The cache size used to be a knowable constant; now it depends on the
    // environment, on a code path that exists because a mis-sized cache was
    // only ever discovered via an OOM kill. Leave the whole derivation in the
    // log: which detector won, what it read, and what that resolved to.
    let source = match (physical, cgroup) {
        (None, None) => "undetected, falling back to the ceiling",
        (Some(_), None) => "physical memory",
        (None, Some(_)) => "cgroup limit",
        (Some(p), Some(c)) => {
            if c <= p {
                "cgroup limit"
            } else {
                "physical memory"
            }
        }
    };
    info!(
        detected_limit_bytes = ?limit,
        source,
        cache_bytes = size,
        "sized RocksDB block cache"
    );
    size
}

/// Pure part of [`default_rocksdb_block_cache_size`]: the clamp, with the detected
/// memory limit supplied by the caller. `None` means detection failed.
pub fn rocksdb_block_cache_size_for(memory_limit: Option<usize>) -> usize {
    let Some(limit) = memory_limit else {
        return MAX_ROCKSDB_BLOCK_CACHE_SIZE_BYTES;
    };
    (limit.saturating_mul(ROCKSDB_BLOCK_CACHE_MEMORY_PERCENT) / 100).clamp(
        MIN_ROCKSDB_BLOCK_CACHE_SIZE_BYTES,
        MAX_ROCKSDB_BLOCK_CACHE_SIZE_BYTES,
    )
}

/// `MemTotal` from `/proc/meminfo`, in bytes.
fn physical_memory_bytes() -> Option<usize> {
    let meminfo = std::fs::read_to_string("/proc/meminfo").ok()?;
    let kib: usize = meminfo
        .lines()
        .find_map(|line| line.strip_prefix("MemTotal:"))?
        .split_whitespace()
        .next()?
        .parse()
        .ok()?;
    kib.checked_mul(1024)
}

/// The cgroup memory limit that applies to this process, in bytes: `memory.max`
/// under cgroup v2, falling back to `memory.limit_in_bytes` under v1. Both report
/// "no limit" out of band — v2 with the literal `max`, v1 with a sentinel near
/// `u64::MAX` — which yields `None` here (v1's sentinel simply exceeds any real
/// `MemTotal`, so the `min` above discards it).
///
/// The limit may sit on any cgroup between the one the process belongs to and the
/// mount root — a systemd slice, a Kubernetes pod's parent, an outer container — and
/// the mount root itself is usually unlimited, so reading the root alone would miss
/// it. The process's own cgroup comes from `/proc/self/cgroup` and every level up to
/// the root is read, with the smallest limit winning.
fn cgroup_memory_limit_bytes() -> Option<usize> {
    let cgroups = std::fs::read_to_string("/proc/self/cgroup").ok()?;
    let v2 = cgroups
        .lines()
        .find_map(|line| line.strip_prefix("0::"))
        .and_then(|relative| {
            cgroup_limit_up_to_root(Path::new("/sys/fs/cgroup"), relative, "memory.max")
        });
    v2.or_else(|| {
        let relative = cgroups.lines().find_map(|line| {
            // `hierarchy-ID:controller-list:cgroup-path`
            let mut fields = line.splitn(3, ':');
            let controllers = fields.nth(1)?;
            let path = fields.next()?;
            controllers
                .split(',')
                .any(|controller| controller == "memory")
                .then_some(path)
        })?;
        cgroup_limit_up_to_root(
            Path::new("/sys/fs/cgroup/memory"),
            relative,
            "memory.limit_in_bytes",
        )
    })
}

/// Smallest value of `file` across `<mount>/<relative>` and each of its ancestors up
/// to `mount`, skipping levels whose file is absent or reports no limit.
fn cgroup_limit_up_to_root(mount: &Path, relative: &str, file: &str) -> Option<usize> {
    let mut dir = mount.join(relative.trim_start_matches('/'));
    let mut limit: Option<usize> = None;
    loop {
        if let Some(value) = std::fs::read_to_string(dir.join(file))
            .ok()
            .and_then(|contents| contents.trim().parse::<usize>().ok())
        {
            limit = Some(limit.map_or(value, |current| current.min(value)));
        }
        if dir == mount || !dir.pop() {
            return limit;
        }
    }
}

/// Tunable configuration for [`Store::new_with_config`] and related constructors.
///
/// Use [`StoreConfig::default()`] for production-tuned defaults; callers that
/// don't need to override anything should keep calling [`Store::new`] directly.
#[derive(Debug, Clone, Copy)]
pub struct StoreConfig {
    /// Size in bytes of the RocksDB shared block cache. With
    /// `cache_index_and_filter_blocks` enabled (the ethrex default), this is
    /// the effective ceiling on RocksDB's resident memory. Ignored for
    /// in-memory backends.
    pub rocksdb_block_cache_size: usize,
    /// Byte budget of the bytecode cache. `None` keeps [`CODE_CACHE_MAX_SIZE`].
    pub code_cache_size: Option<u64>,
    /// Bound on the persist worker's channel: number of staged (acked) live
    /// messages whose flush may still be in flight. Once full, the next send
    /// blocks — that is the backpressure that throttles `newPayload`.
    /// Clamped to `max(1)` at construction (0 would make a rendezvous channel).
    pub persist_channel_capacity: usize,
}

impl StoreConfig {
    /// Every tuned default except the block cache, which the caller supplies.
    /// An explicit cache size must come through here rather than overwriting a
    /// [`StoreConfig::default()`], which would run (and log) the memory
    /// detection only to discard its result.
    pub fn with_rocksdb_block_cache_size(rocksdb_block_cache_size: usize) -> Self {
        Self {
            rocksdb_block_cache_size,
            code_cache_size: None,
            persist_channel_capacity: DEFAULT_PERSIST_CHANNEL_CAPACITY,
        }
    }
}

impl Default for StoreConfig {
    fn default() -> Self {
        Self::with_rocksdb_block_cache_size(default_rocksdb_block_cache_size())
    }
}

/// Control messages for the FlatKeyValue generator
#[derive(Debug, PartialEq)]
enum FKVGeneratorControlMessage {
    Stop,
    Continue,
}

/// Byte budget for the in-memory bytecode cache.
///
/// Bytecode is a small fraction of the working set next to trie nodes and flat
/// key-values (which the RocksDB block cache serves), but a cold code read costs a
/// blob-file fetch plus decompression, so caching contracts pays for itself.
///
/// 2 GiB holds ~29k max-size contracts: a 64 KiB contract (EIP-7954) is accounted
/// at ~72 KiB, its padded bytecode plus the 8 KiB jumpdest bitmap. Entries never
/// expire and every read that misses inserts (block execution, snap `GetByteCodes`
/// serving, `eth_getCode`, `eth_call`), so a long-running node fills the budget.
/// Each entry is also charged [`CODE_CACHE_ENTRY_OVERHEAD`] for its map slot and
/// buffer headers, so small contracts cannot hold the cache far over its budget.
const CODE_CACHE_MAX_SIZE: u64 = 2 * 1024 * 1024 * 1024;

/// Entry bound for [`Store::code_metadata_cache`], derived from a 16 MiB ceiling at the
/// ~64 B an `LruCache` entry costs (32 B key + 8 B value + list/table overhead). The
/// ~262k lengths it keeps cover every code the 2 GiB code cache can hold while cached
/// contracts average at least 8 KiB (~10.5 KiB on mainnet). A cache of smaller ones,
/// such as EIP-7702 delegations, holds more codes than that, and `EXTCODESIZE` on
/// those falls through to `ACCOUNT_CODE_METADATA`. The bound caps what an
/// `EXTCODESIZE` sweep over unique contracts can pin.
const CODE_METADATA_CACHE_MAX_ENTRIES: usize = (16 * 1024 * 1024) / 64;

/// Key used to persist the `flushed_upto` block number in `MISC_VALUES`.
const FLUSHED_UPTO_KEY: &[u8] = b"bodies_flushed_upto";

/// Single key under which the bounded list of bad blocks is stored in `BAD_BLOCKS`.
const BAD_BLOCKS_KEY: &[u8] = b"bad_blocks";

/// Maximum number of bad blocks retained for `debug_getBadBlocks`.
const MAX_BAD_BLOCKS: usize = 16;

/// Heap bytes a [`CodeCache`] entry costs beyond [`Code::size`]: its slot in the map's
/// entry vector and index, and the headers of the two shared buffers. Measured at 126 to
/// 133 bytes requested per entry, before the allocator rounds them up to its size classes.
/// Without it, a cache full of small contracts held nearly twice its budget (1.87x at 32
/// bytes).
const CODE_CACHE_ENTRY_OVERHEAD: u64 = 160;

/// Bytecode cache that evicts a uniformly random entry when over budget.
///
/// Not LRU on purpose: blocks can walk the same set of contracts over and over, and
/// once that set outgrows the budget an LRU evicts every entry just before its next
/// use, so the hit rate collapses to zero. A random victim has no such pathology: with
/// a set 1.25x the budget about 62% of the reads still hit, and the hit rate shrinks
/// gradually as the set grows instead of falling off a cliff.
///
/// Seeded: entries outlive the block that loaded them, and a deployer can grind its code
/// hash into a chosen bucket (see `ethrex_common::seeded_hash`).
struct CodeCache {
    entries: IndexMap<H256, Code, SeededState>,
    cache_size: u64,
    max_size: u64,
    /// xorshift64* state; seeded per process so the victims cannot be predicted.
    rng: u64,
}

// Not derived: `Store` derives `Debug`, and a derived impl here would format every cached
// bytecode (up to the whole budget) and the RNG state.
impl std::fmt::Debug for CodeCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CodeCache")
            .field("len", &self.entries.len())
            .field("cache_size", &self.cache_size)
            .field("max_size", &self.max_size)
            .finish_non_exhaustive()
    }
}

impl Default for CodeCache {
    fn default() -> Self {
        Self {
            entries: IndexMap::default(),
            cache_size: 0,
            max_size: CODE_CACHE_MAX_SIZE,
            // xorshift must not start at zero.
            rng: std::collections::hash_map::RandomState::new().hash_one(0u64) | 1,
        }
    }
}

impl CodeCache {
    /// What one cached `code` costs against `max_size`.
    fn entry_size(code: &Code) -> u64 {
        (code.size() as u64).saturating_add(CODE_CACHE_ENTRY_OVERHEAD)
    }

    fn get(&self, code_hash: &H256) -> Result<Option<Code>, StoreError> {
        Ok(self.entries.get(code_hash).cloned())
    }

    fn contains(&self, code_hash: &H256) -> bool {
        self.entries.contains_key(code_hash)
    }

    fn insert(&mut self, code: &Code) -> Result<(), StoreError> {
        // A hash already cached must not be added to `cache_size` again, or the counter
        // drifts up permanently and evicts entries that fit.
        if self.entries.contains_key(&code.hash) {
            return Ok(());
        }
        // An entry larger than the whole budget would hold the cache over it, so it is
        // not cached; the caller already has its copy.
        let size = Self::entry_size(code);
        if size > self.max_size {
            return Ok(());
        }

        // Evicting before the insert keeps the new entry out of the draw, so it stays
        // cached for the caller about to read it and every victim is equally likely.
        while self.cache_size + size > self.max_size && !self.entries.is_empty() {
            let victim = (self.next_random() % self.entries.len() as u64) as usize;
            if let Some((_, evicted)) = self.entries.swap_remove_index(victim) {
                self.cache_size -= Self::entry_size(&evicted);
            }
        }
        self.cache_size += size;
        self.entries.insert(code.hash, code.clone());

        let cache_len = self.entries.len();
        let current_size = self.cache_size;
        debug!(
            "[ACCOUNT CODE CACHE] cache elements (): {cache_len}, total size: {current_size} bytes"
        );
        Ok(())
    }

    fn next_random(&mut self) -> u64 {
        self.rng ^= self.rng >> 12;
        self.rng ^= self.rng << 25;
        self.rng ^= self.rng >> 27;
        self.rng.wrapping_mul(0x2545_F491_4F6C_DD1D)
    }
}

/// Main storage interface for the ethrex client.
///
/// `Store` is `Clone` and thread-safe; all clones share the same backend and
/// caches via `Arc`. Reads consult an in-memory block-data buffer before disk
/// so not-yet-flushed blocks are always visible.
#[derive(Debug, Clone)]
pub struct Store {
    /// Path to the database directory.
    db_path: PathBuf,
    /// Storage backend (InMemory or RocksDB).
    backend: Arc<dyn StorageBackend>,
    /// Chain configuration (fork schedule, chain ID, etc.).
    chain_config: ChainConfig,
    /// Cache for trie nodes from recent blocks.
    trie_cache: Arc<RwLock<Arc<TrieLayerCache>>>,
    /// Channel for controlling the FlatKeyValue generator background task.
    flatkeyvalue_control_tx: std::sync::mpsc::SyncSender<FKVGeneratorControlMessage>,
    /// In-memory overlay of block data not yet flushed to disk.
    block_data_buffer: Arc<RwLock<Arc<BlockDataBuffer>>>,
    /// Channel to the single persist worker (`apply_updates` → `PersistMessage::Block`,
    /// `wait_for_persistence_idle` → `PersistMessage::Ping`). The worker is the
    /// sole mutator of `block_data_buffer` in production.
    persist_tx: std::sync::mpsc::SyncSender<PersistMessage>,
    /// Roots whose trie diff-layer is being built but not yet installed in
    /// `trie_cache`. Trie opens block on these so a just-added block's state is
    /// never read as stale before its layer lands.
    pending_trie_roots: Arc<PendingTrieRoots>,
    /// Cached latest canonical block header. May be slightly stale, which is
    /// acceptable for RPC "latest" queries and sync operations.
    latest_block_header: LatestBlockHeaderCache,
    /// Last computed FlatKeyValue for incremental updates.
    last_computed_flatkeyvalue: Arc<RwLock<Vec<u8>>>,

    /// Cache for account bytecodes, keyed by the bytecode hash.
    /// Note that we don't remove entries on account code changes, since
    /// those changes already affect the code hash stored in the account, and only
    /// may result in this cache having useless data.
    account_code_cache: Arc<Mutex<CodeCache>>,

    /// Cache for code metadata (code length), keyed by the bytecode hash.
    ///
    /// Bounded: `EXTCODESIZE` reads this on the execution path, so an unbounded map
    /// would grow by one entry per distinct contract ever asked for a length and never
    /// give the memory back. See [`CODE_METADATA_CACHE_MAX_ENTRIES`]. Seeded, like
    /// [`CodeCache`].
    code_metadata_cache: Arc<Mutex<LruCache<H256, CodeMetadata, SeededState>>>,

    /// Serializes concurrent `forkchoice_update` callers so that the cache
    /// update and the DB write transaction remain mutually ordered.
    fcu_lock: Arc<tokio::sync::Mutex<()>>,

    /// Canonical safe-commit state root, computed after each forkchoice update.
    ///
    /// Shared with the [`TrieLayerCache`] so that the Store can update the cell without
    /// replacing the cache Arc. `H256::zero()` means "no safe commit point yet".
    /// Cloning `Store` shares this cell across all clones, which is required and correct.
    safe_commit_root: Arc<RwLock<H256>>,

    /// While set, `forkchoice_update_inner` skips STATE_HISTORY pruning (the
    /// finalized-number update still lands). Set by `Blockchain::enter_reorg` for
    /// the duration of a deep-reorg apply pass: `Overlay::from_journal` reads
    /// journal entries one by one with no snapshot isolation, and syncer-driven
    /// forkchoice updates are not gated by the reorg mutex, so a concurrent
    /// finality advance could otherwise prune entries out from under overlay
    /// construction (spurious `MissingEntry`) or between a case-1 attempt and its
    /// retry. Pruning catches up on the first finality advance after the pass
    /// ends (the pruned range starts at the lowest entry still present).
    journal_pruning_paused: Arc<std::sync::atomic::AtomicBool>,

    background_threads: Arc<ThreadList>,
}

#[derive(Debug, Default)]
struct ThreadList {
    list: Vec<JoinHandle<()>>,
}

impl Drop for ThreadList {
    fn drop(&mut self) {
        for handle in self.list.drain(..) {
            let _ = handle.join();
        }
    }
}

/// Storage trie nodes grouped by account address hash.
///
/// Each entry contains the hashed account address and the trie nodes
/// for that account's storage trie.
pub type StorageTrieNodes = Vec<(H256, Vec<(Nibbles, Vec<u8>)>)>;
type StorageTries = HashMap<Address, (TrieWitness, Trie)>;

/// Storage backend type selection.
///
/// Used when creating a new [`Store`] to specify which backend to use.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EngineType {
    /// In-memory storage, non-persistent. Suitable for testing.
    InMemory,
    /// RocksDB storage, persistent. Suitable for production.
    #[cfg(feature = "rocksdb")]
    RocksDB,
}

/// Batch of updates to apply to the store atomically.
///
/// Used during block execution to collect all state changes before
/// committing them to the database in a single transaction.
pub struct UpdateBatch {
    /// New nodes to add to the state trie.
    pub account_updates: Vec<TrieNode>,
    /// Storage trie updates per account (keyed by hashed address).
    pub storage_updates: Vec<(H256, Vec<TrieNode>)>,
    /// Blocks to store.
    pub blocks: Vec<Block>,
    /// Receipts to store, grouped by block hash.
    pub receipts: Vec<(H256, Vec<Receipt>)>,
    /// Contract code updates (code hash -> bytecode).
    pub code_updates: Vec<(H256, Code)>,
    /// Commit gate for this batch's trie layers (independent of `wait_for_flush`).
    ///
    /// - `None`: live path (`newPayload`). The persist worker commits by the canonical
    ///   `head - DB_COMMIT_THRESHOLD` safe-commit root.
    /// - `Some(depth)`: single-canonical-chain execution (batch import, full sync, startup
    ///   state regeneration). The persist worker commits every layer deeper than `depth`
    ///   (see [`Trie::get_commitable_by_depth`]), which bounds resident trie layers to ~`depth`.
    pub commit_depth: Option<usize>,
    /// When the persist worker acks (independent of `commit_depth`).
    ///
    /// - `false`: ack after staging, so the caller's next-block execution overlaps this
    ///   block's disk flush. Used by the live path and the per-block re-execution paths
    ///   (regen / full-sync fallback / import tail) — their memory is already bounded by
    ///   `commit_depth`'s depth gate and the persist channel capacity, so waiting per block
    ///   would only serialize CPU and I/O for no benefit.
    /// - `true`: ack after flush, bounding in-flight work to ~1 message. Used by the bespoke
    ///   batch path, where a single message carries ~1024 blocks (~1 GB of trie diff) and two
    ///   in flight would be a real memory cost.
    pub wait_for_flush: bool,
}

/// A block's historical chain data to backfill below the snap-sync pivot: the
/// (already-canonical) header, its body, and its receipts.
///
/// `index_transactions` controls whether the transaction-lookup index is written
/// for this block; the backfill task drives it from the `--history.transactions`
/// horizon so old blocks can be excluded from the index while still storing their
/// bodies and receipts.
pub struct BackfilledBlock {
    pub header: BlockHeader,
    pub body: BlockBody,
    pub receipts: Vec<Receipt>,
    pub index_transactions: bool,
}

/// Storage trie updates grouped by account address hash.
pub type StorageUpdates = Vec<(H256, Vec<(Nibbles, Vec<u8>)>)>;

/// Collection of account state changes from block execution.
///
/// Contains all the data needed to update the state trie after
/// executing a block: account updates, storage updates, and code deployments.
pub struct AccountUpdatesList {
    /// Root hash of the state trie after applying these updates.
    pub state_trie_hash: H256,
    /// State trie node updates (path -> RLP-encoded node).
    pub state_updates: Vec<(Nibbles, Vec<u8>)>,
    /// Storage trie updates per account.
    pub storage_updates: StorageUpdates,
    /// New contract bytecode deployments.
    pub code_updates: Vec<(H256, Code)>,
}

/// Encodes a tx-location entry as the operand passed to `merge_cf`.
///
/// The operand uses the **same encoding as the stored value** — a
/// `Vec<(BlockNumber, BlockHash, Index)>` with a single element. This is
/// required for an *associative* merge operator: RocksDB folds operands
/// together with PartialMerge (during compaction, without a base value), and
/// the result becomes an operand for a later merge. If the operand format
/// differed from the merge output (e.g. operand = bare tuple, output = Vec),
/// the re-fed result would fail to decode and entries would be silently
/// dropped. Keeping both as `Vec` makes the merge truly associative.
pub(crate) fn encode_tx_location_operand(
    block_number: BlockNumber,
    block_hash: BlockHash,
    index: Index,
) -> Vec<u8> {
    vec![(block_number, block_hash, index)].encode_to_vec()
}

/// Merge function for the `TRANSACTION_LOCATIONS` column family.
///
/// The CF stores `Vec<(BlockNumber, BlockHash, Index)>` keyed by tx hash.
/// Both stored values and operands use this same `Vec` encoding — this
/// associativity requirement is mandatory: RocksDB folds operands together
/// during compaction without a base value (PartialMerge), then feeds that
/// result back into a later merge. A differing format would silently drop
/// entries. See `encode_tx_location_operand`.
///
/// Within the fold, a later entry with the same `block_hash` replaces an
/// earlier one (reorg dedupe). On decode failure the merge returns `None`
/// so RocksDB surfaces a corruption error rather than silently dropping
/// locations.
///
/// Merge instead of read-modify-write avoids the ~5–20 ms/block per-tx point
/// lookup on the write path; consolidation is deferred to compaction or the
/// next read.
pub fn tx_locations_merge(
    existing: Option<&[u8]>,
    operands: impl IntoIterator<Item = impl AsRef<[u8]>>,
) -> Option<Vec<u8>> {
    // Fold one RLP-encoded `Vec` chunk into `list`, deduping by block_hash
    // (later entry wins). Returns false on decode failure so the caller can
    // abort the whole merge.
    fn fold_chunk(
        list: &mut Vec<(BlockNumber, BlockHash, Index)>,
        bytes: &[u8],
        what: &str,
    ) -> bool {
        match <Vec<(BlockNumber, BlockHash, Index)>>::decode(bytes) {
            Ok(entries) => {
                for (bn, bh, idx) in entries {
                    list.retain(|(_, existing_bh, _)| *existing_bh != bh);
                    list.push((bn, bh, idx));
                }
                true
            }
            Err(e) => {
                error!(
                    "tx_locations_merge: failed to decode {what} ({} bytes): {e}; \
                     aborting merge to avoid silent data loss",
                    bytes.len()
                );
                false
            }
        }
    }

    let mut list: Vec<(BlockNumber, BlockHash, Index)> = Vec::new();

    // Order matters: RocksDB delivers operands oldest-first.
    if let Some(bytes) = existing
        && !fold_chunk(&mut list, bytes, "existing value")
    {
        return None;
    }
    for op in operands {
        if !fold_chunk(&mut list, op.as_ref(), "operand") {
            return None;
        }
    }
    Some(list.encode_to_vec())
}

impl Store {
    /// Block until the persist worker has fully processed all previously-sent
    /// `Block` messages (staged, trie-layer built, flushed, evicted).
    ///
    /// Uses an ack-based `Ping` rather than a bare send because the channel is
    /// buffered — a bare send proves nothing about prior message completion. The
    /// worker is FIFO, so it handles the `Ping` only after every earlier `Block`
    /// is done.
    ///
    /// Concurrent-producer caveat: if another thread sends a `Block` after the
    /// `Ping` is enqueued, that block may not be flushed by the time this returns.
    pub async fn wait_for_persistence_idle(&self) -> Result<(), StoreError> {
        let tx = self.persist_tx.clone();
        tokio::task::spawn_blocking(move || {
            let (ack_tx, ack_rx) = sync_channel::<Result<(), StoreError>>(1);
            tx.send(PersistMessage::Ping(ack_tx))
                .map_err(|e| StoreError::Custom(format!("wait_for_persistence_idle send: {e}")))?;
            ack_rx
                .recv()
                .map_err(|e| StoreError::Custom(format!("wait_for_persistence_idle ack: {e}")))?
        })
        .await
        .map_err(|e| StoreError::Custom(format!("wait_for_persistence_idle join: {e}")))?
    }

    /// Flushes all in-memory state to disk for a clean shutdown.
    ///
    /// Sends a `Shutdown` handshake to the persist worker, which (being FIFO)
    /// first drains every queued `Block`, then force-flushes the block-data
    /// buffer to disk. Once the worker acks, this syncs the backend (memtables +
    /// WAL) so the next process start needs no WAL recovery.
    ///
    /// The in-memory trie diff-layers are intentionally *not* force-committed.
    /// The on-disk trie is a single-version, path-based store, so folding the
    /// non-finalized tail into it would leave a post-restart reorg unable to
    /// reconstruct the overwritten ancestor state (the node would wedge). The
    /// recent (< `DB_COMMIT_THRESHOLD`) layers are dropped and re-executed on the
    /// next start from the deep, reorg-safe on-disk base — exactly as after any
    /// restart today.
    ///
    /// After this returns the persist worker has exited; the store must not be
    /// used for further writes. Idempotent only in the sense that a second call
    /// errors on the closed channel — call it exactly once, on shutdown.
    pub async fn shutdown(&self) -> Result<(), StoreError> {
        let tx = self.persist_tx.clone();
        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let (ack_tx, ack_rx) = sync_channel::<Result<(), StoreError>>(1);
            tx.send(PersistMessage::Shutdown { ack: ack_tx })
                .map_err(|e| StoreError::Custom(format!("shutdown send: {e}")))?;
            ack_rx
                .recv()
                .map_err(|e| StoreError::Custom(format!("shutdown ack: {e}")))??;
            // Worker has flushed block data to the WAL/memtables; make it durable
            // and recovery-free.
            backend.flush()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("shutdown join: {e}")))?
    }

    /// Add a block in a single transaction.
    /// This will store -> BlockHeader, BlockBody, BlockTransactions, BlockNumber.
    pub async fn add_block(&self, block: Block) -> Result<(), StoreError> {
        self.add_blocks(vec![block]).await
    }

    /// Add a batch of blocks in a single transaction.
    /// This will store -> BlockHeader, BlockBody, BlockTransactions, BlockNumber.
    pub async fn add_blocks(&self, blocks: Vec<Block>) -> Result<(), StoreError> {
        let db = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let mut tx = db.begin_write()?;

            for block in blocks {
                write_block_data(
                    tx.as_mut(),
                    block.header.number,
                    block.hash(),
                    &block.header,
                    &block.body,
                )?;
            }

            tx.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Add block header
    pub async fn add_block_header(
        &self,
        block_hash: BlockHash,
        block_header: BlockHeader,
    ) -> Result<(), StoreError> {
        let hash_key = block_hash.encode_to_vec();
        let header_value = BlockHeaderRLP::from(block_header).into_vec();
        self.write_async(HEADERS, hash_key, header_value).await
    }

    /// Add a batch of block headers
    pub async fn add_block_headers(
        &self,
        block_headers: Vec<BlockHeader>,
    ) -> Result<(), StoreError> {
        let mut txn = self.backend.begin_write()?;

        for header in block_headers {
            let block_hash = header.hash();
            let block_number = header.number;
            let hash_key = block_hash.encode_to_vec();
            let header_value = BlockHeaderRLP::from(header).into_vec();

            txn.put(HEADERS, &hash_key, &header_value)?;

            let number_key = block_number.to_le_bytes().to_vec();
            txn.put(BLOCK_NUMBERS, &hash_key, &number_key)?;
        }
        txn.commit()?;
        Ok(())
    }

    /// Obtain canonical block header
    pub fn get_block_header(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHeader>, StoreError> {
        let latest = self.latest_block_header.get();
        if block_number == latest.number {
            return Ok(Some((*latest).clone()));
        }
        // Resolve the canonical hash, then read through the buffer-aware by-hash
        // path so a canonical-but-still-buffered block is visible (mirrors
        // `get_block_body`). `load_block_header` is disk-only and would return
        // `None` for a block whose header has not been flushed yet.
        let Some(block_hash) = self.get_canonical_block_hash_sync(block_number)? else {
            return Ok(None);
        };
        self.get_block_header_by_hash(block_hash)
    }

    /// Add block body
    pub async fn add_block_body(
        &self,
        block_hash: BlockHash,
        block_body: BlockBody,
    ) -> Result<(), StoreError> {
        let hash_key = block_hash.encode_to_vec();
        let body_value = BlockBodyRLP::from(block_body).into_vec();
        self.write_async(BODIES, hash_key, body_value).await
    }

    /// Backfill historical chain data for a contiguous range of already-canonical
    /// blocks (bodies, receipts, and optionally the transaction-lookup index),
    /// then lower `earliest_block_number` to `new_earliest` — all in one write
    /// transaction.
    ///
    /// This is the storage half of `--history.chain` backfill. It does NOT touch
    /// state, the trie layer cache, or fork choice: the headers and canonical
    /// mapping already exist from snap-sync header backfill, so only the
    /// body / receipt / tx-index tables are filled. Writing the data and advancing
    /// the frontier atomically preserves the invariant that every block in
    /// `[earliest_block_number, head]` has a stored body — a crash mid-batch
    /// leaves both the data and the frontier unchanged, so resume never skips a
    /// hole.
    ///
    /// `new_earliest` must be the lowest block number covered by `blocks` (the
    /// bottom of the contiguous filled range); the caller fills downward without
    /// gaps.
    pub async fn add_backfilled_blocks(
        &self,
        blocks: Vec<BackfilledBlock>,
        new_earliest: BlockNumber,
    ) -> Result<(), StoreError> {
        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let mut txn = backend.begin_write()?;
            for block in &blocks {
                let hash = block.header.hash();
                let hash_key = hash.encode_to_vec();
                txn.put(
                    BODIES,
                    &hash_key,
                    &BlockBodyRLP::from(block.body.clone()).into_vec(),
                )?;
                for (index, receipt) in block.receipts.iter().enumerate() {
                    txn.put(
                        RECEIPTS_V2,
                        &receipt_key(&hash, index as u64),
                        &receipt.encode_storage(),
                    )?;
                }
                if block.index_transactions {
                    for (index, transaction) in block.body.transactions.iter().enumerate() {
                        txn.merge(
                            TRANSACTION_LOCATIONS,
                            transaction.hash(&NativeCrypto).as_bytes(),
                            &encode_tx_location_operand(block.header.number, hash, index as u64),
                        )?;
                    }
                }
            }
            txn.put(
                CHAIN_DATA,
                &chain_data_key(ChainDataIndex::EarliestBlockNumber),
                &new_earliest.to_le_bytes(),
            )?;
            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Backfill write task panicked: {e}")))?
    }

    /// RocksDB observability snapshot (per-column-family sizes/keys/files, block
    /// cache, compaction). `None` for the in-memory backend. Read by the metrics
    /// collector; cheap (RocksDB property reads).
    pub fn rocksdb_stats(&self) -> Option<crate::api::RocksDbStats> {
        self.backend.db_stats()
    }

    /// Obtain canonical block body
    pub async fn get_block_body(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockBody>, StoreError> {
        let Some(block_hash) = self.get_canonical_block_hash_sync(block_number)? else {
            return Ok(None);
        };

        self.get_block_body_by_hash(block_hash).await
    }

    /// Remove canonical block
    pub async fn remove_block(&self, block_number: BlockNumber) -> Result<(), StoreError> {
        let Some(hash) = self.get_canonical_block_hash_sync(block_number)? else {
            return Ok(());
        };

        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let hash_key = hash.encode_to_vec();

            let mut txn = backend.begin_write()?;
            txn.delete(
                CANONICAL_BLOCK_HASHES,
                block_number.to_le_bytes().as_slice(),
            )?;
            txn.delete(BODIES, &hash_key)?;
            txn.delete(HEADERS, &hash_key)?;
            txn.delete(BLOCK_NUMBERS, &hash_key)?;
            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Obtain canonical block bodies in from..=to
    pub async fn get_block_bodies(
        &self,
        from: BlockNumber,
        to: BlockNumber,
    ) -> Result<Vec<Option<BlockBody>>, StoreError> {
        // TODO: Implement read bulk
        let buffer = self.buffer()?;
        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let numbers: Vec<BlockNumber> = (from..=to).collect();
            let mut block_bodies = Vec::new();

            let txn = backend.begin_read()?;
            for number in numbers {
                let Some(hash) = txn
                    .get(CANONICAL_BLOCK_HASHES, number.to_le_bytes().as_slice())?
                    .map(|bytes| H256::decode(bytes.as_slice()))
                    .transpose()?
                else {
                    block_bodies.push(None);
                    continue;
                };
                // Consult the in-memory buffer first so a not-yet-flushed body
                // is not reported as missing (mirrors get_block_bodies_by_hash).
                if let Some(body) = buffer.get_body(&hash) {
                    block_bodies.push(Some(body));
                    continue;
                }
                let hash_key = hash.encode_to_vec();
                let block_body_opt = txn
                    .get(BODIES, &hash_key)?
                    .map(|bytes| BlockBodyRLP::from_bytes(bytes).to())
                    .transpose()
                    .map_err(StoreError::from)?;

                block_bodies.push(block_body_opt);
            }

            Ok(block_bodies)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Obtain block bodies from a list of hashes
    pub async fn get_block_bodies_by_hash(
        &self,
        hashes: Vec<BlockHash>,
    ) -> Result<Vec<BlockBody>, StoreError> {
        let buffer = self.buffer()?;
        let backend = self.backend.clone();
        // TODO: Implement read bulk
        tokio::task::spawn_blocking(move || {
            let txn = backend.begin_read()?;
            let mut block_bodies = Vec::new();
            for hash in hashes {
                // Consult the in-memory buffer first, like the single-hash reader,
                // so a not-yet-flushed body is not reported as missing.
                if let Some(body) = buffer.get_body(&hash) {
                    block_bodies.push(body);
                    continue;
                }
                let hash_key = hash.encode_to_vec();

                let Some(block_body) = txn
                    .get(BODIES, &hash_key)?
                    .map(|bytes| BlockBodyRLP::from_bytes(bytes).to())
                    .transpose()
                    .map_err(StoreError::from)?
                else {
                    return Err(StoreError::Custom(format!(
                        "Block body not found for hash: {hash}"
                    )));
                };
                block_bodies.push(block_body);
            }
            Ok(block_bodies)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Obtain any block body using the hash
    pub async fn get_block_body_by_hash(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockBody>, StoreError> {
        if let Some(b) = self.buffer()?.get_body(&block_hash) {
            return Ok(Some(b));
        }
        self.read_async(BODIES, block_hash.encode_to_vec())
            .await?
            .map(|bytes| BlockBodyRLP::from_bytes(bytes).to())
            .transpose()
            .map_err(StoreError::from)
    }

    pub fn get_block_header_by_hash(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockHeader>, StoreError> {
        let latest = self.latest_block_header.get();
        if block_hash == latest.hash() {
            return Ok(Some((*latest).clone()));
        }
        if let Some(h) = self.buffer()?.get_header(&block_hash) {
            return Ok(Some(h));
        }
        self.load_block_header_by_hash(block_hash)
    }

    pub fn add_pending_block(&self, block: Block) -> Result<(), StoreError> {
        let block_hash = block.hash();
        let block_value = BlockRLP::from(block).into_vec();
        self.write(PENDING_BLOCKS, block_hash.as_bytes().to_vec(), block_value)
    }

    pub async fn get_pending_block(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<Block>, StoreError> {
        self.read_async(PENDING_BLOCKS, block_hash.as_bytes().to_vec())
            .await?
            .map(|bytes| BlockRLP::from_bytes(bytes).to())
            .transpose()
            .map_err(StoreError::from)
    }

    /// Add block number for a given hash
    pub async fn add_block_number(
        &self,
        block_hash: BlockHash,
        block_number: BlockNumber,
    ) -> Result<(), StoreError> {
        let number_value = block_number.to_le_bytes().to_vec();
        self.write_async(BLOCK_NUMBERS, block_hash.encode_to_vec(), number_value)
            .await
    }

    /// Obtain block number for a given hash
    pub async fn get_block_number(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockNumber>, StoreError> {
        if let Some(n) = self.buffer()?.get_number(&block_hash) {
            return Ok(Some(n));
        }
        self.read_async(BLOCK_NUMBERS, block_hash.encode_to_vec())
            .await?
            .map(decode_block_number)
            .transpose()
    }

    /// Store transaction location (block number and index of the transaction within the block)
    pub async fn add_transaction_location(
        &self,
        transaction_hash: H256,
        block_number: BlockNumber,
        block_hash: BlockHash,
        index: Index,
    ) -> Result<(), StoreError> {
        self.add_transaction_locations(vec![(transaction_hash, block_number, block_hash, index)])
            .await
    }

    /// Store transaction locations in batch (one db transaction for all)
    pub async fn add_transaction_locations(
        &self,
        locations: Vec<(H256, BlockNumber, BlockHash, Index)>,
    ) -> Result<(), StoreError> {
        let db = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let mut tx = db.begin_write()?;
            for (tx_hash, block_number, block_hash, index) in locations {
                tx.merge(
                    TRANSACTION_LOCATIONS,
                    tx_hash.as_bytes(),
                    &encode_tx_location_operand(block_number, block_hash, index),
                )?;
            }
            tx.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Obtain transaction location (block hash and index)
    pub async fn get_transaction_location(
        &self,
        transaction_hash: H256,
    ) -> Result<Option<(BlockNumber, BlockHash, Index)>, StoreError> {
        let buffered = self.buffer()?.get_tx_locations(&transaction_hash);
        let db = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let tx = db.begin_read()?;
            let mut locations = buffered;
            if let Some(bytes) = tx.get(TRANSACTION_LOCATIONS, transaction_hash.as_bytes())? {
                locations.extend(<Vec<(BlockNumber, BlockHash, Index)>>::decode(&bytes)?);
            }
            for (block_number, block_hash, index) in locations {
                let canonical_hash = tx
                    .get(
                        CANONICAL_BLOCK_HASHES,
                        block_number.to_le_bytes().as_slice(),
                    )?
                    .map(|bytes| H256::decode(bytes.as_slice()))
                    .transpose()?;
                if canonical_hash == Some(block_hash) {
                    return Ok(Some((block_number, block_hash, index)));
                }
            }
            Ok(None)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Add receipt
    pub async fn add_receipt(
        &self,
        block_hash: BlockHash,
        index: Index,
        receipt: Receipt,
    ) -> Result<(), StoreError> {
        let key = receipt_key(&block_hash, index);
        // Storage codec (NOT wire/consensus): preserves frame-receipt
        // `succeeded` + aggregated logs; identical to encode_to_vec for
        // non-frame receipts.
        let value = receipt.encode_storage();
        self.write_async(RECEIPTS_V2, key, value).await
    }

    /// Add receipts
    pub async fn add_receipts(
        &self,
        block_hash: BlockHash,
        receipts: Vec<Receipt>,
    ) -> Result<(), StoreError> {
        let batch_items: Vec<_> = receipts
            .into_iter()
            .enumerate()
            .map(|(index, receipt)| {
                let key = receipt_key(&block_hash, index as u64);
                let value = receipt.encode_storage();
                (key, value)
            })
            .collect();
        self.write_batch_async(RECEIPTS_V2, batch_items).await
    }

    /// Obtain receipt for a canonical block represented by the block number.
    pub async fn get_receipt(
        &self,
        block_number: BlockNumber,
        index: Index,
    ) -> Result<Option<Receipt>, StoreError> {
        // FIXME (#4353)
        let Some(block_hash) = self.get_canonical_block_hash(block_number).await? else {
            return Ok(None);
        };
        self.get_receipt_by_block_hash(block_hash, index).await
    }

    /// Obtain receipt by block hash and index
    async fn get_receipt_by_block_hash(
        &self,
        block_hash: BlockHash,
        index: Index,
    ) -> Result<Option<Receipt>, StoreError> {
        if let Some(r) = self.buffer()?.get_receipt(&block_hash, index) {
            return Ok(Some(r));
        }
        let key = receipt_key(&block_hash, index);
        self.read_async(RECEIPTS_V2, key)
            .await?
            .map(|bytes| Receipt::decode_storage(bytes.as_slice()))
            .transpose()
            .map_err(StoreError::from)
    }

    /// Capacity of the bytecode cache, in [`Code::size`] bytes, which is how much
    /// bytecode this node is configured to keep resident and so bounds what a block warm
    /// reads speculatively.
    ///
    /// A poisoned cache reports zero, which stops warming rather than warming into a
    /// cache no reader can reach.
    pub fn code_cache_budget_bytes(&self) -> u64 {
        self.account_code_cache
            .lock()
            .map(|cache| cache.max_size)
            .unwrap_or(0)
    }

    /// Get account code by its hash.
    ///
    /// Checks the in-memory block-data buffer first, then the bytecode cache
    /// (`account_code_cache`), and finally the database.  Code that has been
    /// inserted via `engine_newPayload` but not yet flushed to disk is therefore
    /// visible to callers without an explicit flush.
    pub fn get_account_code(&self, code_hash: H256) -> Result<Option<Code>, StoreError> {
        if let Some(code) = self.buffer()?.get_code(&code_hash) {
            return Ok(Some(code));
        }
        // check cache first
        if let Some(code) = self
            .account_code_cache
            .lock()
            .map_err(|_| StoreError::LockError)?
            .get(&code_hash)?
        {
            return Ok(Some(code));
        }

        let Some(bytes) = self
            .backend
            .begin_read()?
            .get(ACCOUNT_CODES, code_hash.as_bytes())?
        else {
            return Ok(None);
        };
        let (bytecode_slice, jumpdests) = decode_bytes(&bytes)?;
        let (jumpdests, was_legacy) = decode_jumpdests(bytecode_slice, jumpdests)?;
        let code = Code::from_parts_unchecked(code_hash, bytecode_slice, jumpdests);

        // Self-heal a legacy (pre-bitmap) row so the jumpdest recompute above is
        // paid at most once. Fire-and-forget, and only when a Tokio runtime is
        // reachable: this read runs on rayon execution-path workers, where
        // `spawn` would panic. Skipping just leaves the next read to recompute
        // again; there is no correctness dependency (matches the code-metadata
        // backfill in `get_code_metadata`).
        if was_legacy && let Ok(handle) = tokio::runtime::Handle::try_current() {
            let buf = encode_code(&code);
            let hash_key = code_hash.0.to_vec();
            let backend = self.backend.clone();
            handle.spawn(async move {
                if let Err(e) = async {
                    let mut tx = backend.begin_write()?;
                    tx.put(ACCOUNT_CODES, &hash_key, &buf)?;
                    tx.commit()
                }
                .await
                {
                    tracing::warn!("Failed to rewrite legacy jumpdests during backfill: {e}");
                }
            });
        }

        // insert into cache and evict if needed
        self.account_code_cache
            .lock()
            .map_err(|_| StoreError::LockError)?
            .insert(&code)?;

        Ok(Some(code))
    }

    /// Batched [`Self::get_account_code`].
    ///
    /// Resolves the buffer and the bytecode cache first, then reads whatever is left by whichever
    /// of two strategies gets more of those reads in flight for a batch this size: a
    /// parallel fan-out of point gets, or sorted keys split into contiguous shards read
    /// concurrently. See the comment on the read below for how the choice is made. The
    /// cache is locked once for the whole batch rather than twice per code.
    ///
    /// Results are returned in the order of `code_hashes`. Duplicate hashes are read
    /// once. `None` means the hash is absent from the database.
    ///
    /// Every requested code is held at once until the result is dropped, and nothing here
    /// bounds that by the code cache budget: a caller reading many contracts it does not
    /// control must chunk `code_hashes` and charge each chunk against its own budget.
    pub fn get_account_codes_batch(
        &self,
        code_hashes: &[H256],
    ) -> Result<Vec<Option<Code>>, StoreError> {
        let mut out: Vec<Option<Code>> = vec![None; code_hashes.len()];
        // Positions to fill per distinct hash, so a repeated hash costs one read.
        let mut pending: HashMap<H256, Vec<usize>> = HashMap::new();

        {
            let buffer = self.buffer()?;
            let cache = self
                .account_code_cache
                .lock()
                .map_err(|_| StoreError::LockError)?;
            for (i, hash) in code_hashes.iter().enumerate() {
                if let Some(code) = buffer.get_code(hash) {
                    out[i] = Some(code);
                } else if let Some(code) = cache.get(hash)? {
                    out[i] = Some(code);
                } else {
                    pending.entry(*hash).or_default().push(i);
                }
            }
        }

        if pending.is_empty() {
            return Ok(out);
        }

        let mut missing: Vec<H256> = pending.keys().copied().collect();
        missing.sort_unstable();

        // Cold blob reads here are latency-bound, so what matters is how many are in
        // flight. Two ways to get there, and which one wins depends on the batch size:
        //
        // * A parallel fan-out of point gets reaches queue depth ~= core count. Cheap
        //   for any batch: rayon's pool is already warm.
        // * Contiguous shards of the SORTED keys, one blocking thread each, reach queue
        //   depth ~= shard count and share RocksDB blocks within a shard. This is the
        //   only way past core count (async_io is OFF in this build, so a single
        //   `multi_get` runs the whole batch at queue depth 1), but it pays a thread
        //   spawn per shard.
        //
        // So shard only once sharding can actually beat the fan-out, i.e. once the batch
        // is wide enough to form more shards than there are cores. Below that the
        // fan-out is both deeper and cheaper, and a single serial `multi_get` would be
        // far worse than either.
        //
        // The shard cap has to sit above the core count, or on a host with at least that
        // many cores the fan-out would always win and this path would be unreachable.
        // Twice the cores guarantees that, with a floor so a small host keeps the depth
        // it can already reach: these threads block on I/O rather than compute, so more
        // of them than cores is the point.
        const KEYS_PER_SHARD: usize = 256;
        const MIN_SHARD_CAP: usize = 64;
        let parallelism = std::thread::available_parallelism().map_or(8, |p| p.get());
        let max_shards = parallelism.saturating_mul(2).max(MIN_SHARD_CAP);
        let shards = missing.len().div_ceil(KEYS_PER_SHARD).min(max_shards);
        let read_view = self.backend.begin_read()?;
        // Both paths decode in whatever thread did the read, so rebuilding the jumpdest
        // bitmap for a legacy entry stays off the caller's thread and stays parallel.
        let decode = |hash: &H256, value: Option<Vec<u8>>| -> Result<Option<Code>, StoreError> {
            let Some(bytes) = value else { return Ok(None) };
            let (bytecode_slice, jumpdests) = decode_bytes(&bytes)?;
            // This is the bulk prefetch warm path; a legacy row is recomputed here
            // and self-healed later when `get_account_code` reads it on the hot path.
            let (jumpdests, _was_legacy) = decode_jumpdests(bytecode_slice, jumpdests)?;
            Ok(Some(Code::from_parts_unchecked(
                *hash,
                bytecode_slice,
                jumpdests,
            )))
        };
        let decoded: Vec<Result<Option<Code>, StoreError>> = if shards > parallelism {
            let chunk = missing.len().div_ceil(shards);
            let rv = read_view.as_ref();
            let read_shard = |hashes: &[H256]| -> Vec<Result<Option<Code>, StoreError>> {
                let keys: Vec<&[u8]> = hashes.iter().map(|h| h.as_bytes()).collect();
                rv.multi_get(ACCOUNT_CODES, &keys)
                    .into_iter()
                    .zip(hashes.iter())
                    .map(|(value, hash)| decode(hash, value?))
                    .collect()
            };
            std::thread::scope(|scope| {
                let handles: Vec<_> = missing
                    .chunks(chunk)
                    .map(|ck| (ck, scope.spawn(move || read_shard(ck))))
                    .collect();
                handles
                    .into_iter()
                    .flat_map(|(shard, handle)| {
                        // A panicked shard becomes an `Err` per key it covered, keeping
                        // the results aligned with `missing` and leaving the caller's
                        // best-effort handling to decide. Re-panicking here would
                        // escalate a warm into taking down whatever runs it.
                        handle.join().unwrap_or_else(|_| {
                            shard
                                .iter()
                                .map(|_| {
                                    Err(StoreError::Custom(
                                        "account code shard panicked".to_string(),
                                    ))
                                })
                                .collect()
                        })
                    })
                    .collect()
            })
        } else {
            // One key per read, so `get` rather than a single-key `multi_get`: the
            // batched call sets up its own result buffers, which is pure overhead here.
            let rv = read_view.as_ref();
            missing
                .par_iter()
                .map(|hash| decode(hash, rv.get(ACCOUNT_CODES, hash.as_bytes())?))
                .collect()
        };

        let mut fetched: Vec<Code> = Vec::new();
        for (hash, code) in missing.iter().zip(decoded.into_iter()) {
            let Some(code) = code? else { continue };
            for &i in pending.get(hash).into_iter().flatten() {
                out[i] = Some(code.clone());
            }
            fetched.push(code);
        }

        if !fetched.is_empty() {
            let mut cache = self
                .account_code_cache
                .lock()
                .map_err(|_| StoreError::LockError)?;
            for code in &fetched {
                cache.insert(code)?;
            }
        }

        Ok(out)
    }

    /// Check if account code exists by its hash, without constructing the full `Code` struct.
    /// More efficient than `get_account_code` for existence checks since it skips
    /// RLP decoding and `Code` struct construction (no jumpdest-bitmap decoding).
    /// Note: The underlying `get()` still reads the value from RocksDB (including blob files).
    pub fn code_exists(&self, code_hash: H256) -> Result<bool, StoreError> {
        // Code introduced by a not-yet-flushed block lives only in the buffer; check
        // it first so a contract created in the current block is visible (matches
        // get_account_code / get_code_metadata).
        if self.buffer()?.get_code(&code_hash).is_some() {
            return Ok(true);
        }
        // Check cache first
        if self
            .account_code_cache
            .lock()
            .map_err(|_| StoreError::LockError)?
            .contains(&code_hash)
        {
            return Ok(true);
        }
        // Check DB without reading the full value
        Ok(self
            .backend
            .begin_read()?
            .get(ACCOUNT_CODES, code_hash.as_bytes())?
            .is_some())
    }

    /// Get code metadata (length) by its hash.
    ///
    /// Checks cache first, falls back to database. If metadata is missing,
    /// falls back to loading full code and extracts length (auto-migration).
    pub fn get_code_metadata(&self, code_hash: H256) -> Result<Option<CodeMetadata>, StoreError> {
        use ethrex_common::constants::EMPTY_KECCAK_HASH;

        // Empty code special case
        if code_hash == *EMPTY_KECCAK_HASH {
            return Ok(Some(CodeMetadata { length: 0 }));
        }

        // Check cache first
        if let Some(metadata) = self
            .code_metadata_cache
            .lock()
            .map_err(|_| StoreError::LockError)?
            .get(&code_hash)
            .copied()
        {
            return Ok(Some(metadata));
        }

        // Try reading from metadata table
        let metadata = if let Some(bytes) = self
            .backend
            .begin_read()?
            .get(ACCOUNT_CODE_METADATA, code_hash.as_bytes())?
        {
            let length =
                u64::from_be_bytes(bytes.try_into().map_err(|_| {
                    StoreError::Custom("Invalid metadata length encoding".to_string())
                })?);
            CodeMetadata { length }
        } else {
            // Fallback: load full code and extract length (auto-migration)
            let Some(code) = self.get_account_code(code_hash)? else {
                return Ok(None);
            };
            let metadata = CodeMetadata {
                length: code.len() as u64,
            };

            // Backfill the row for future reads, fire and forget.
            //
            // Only when a Tokio runtime is reachable: this read is on the execution
            // path (`EXTCODESIZE`), which runs on rayon workers, and
            // `tokio::task::spawn` panics outside a runtime. Skipping the backfill
            // costs one code read the next time that hash is asked for; panicking
            // would take the node down.
            if let Ok(handle) = tokio::runtime::Handle::try_current() {
                let metadata_buf = metadata.length.to_be_bytes().to_vec();
                let hash_key = code_hash.0.to_vec();
                let backend = self.backend.clone();
                handle.spawn(async move {
                    if let Err(e) = async {
                        let mut tx = backend.begin_write()?;
                        tx.put(ACCOUNT_CODE_METADATA, &hash_key, &metadata_buf)?;
                        tx.commit()
                    }
                    .await
                    {
                        tracing::warn!("Failed to write code metadata during backfill: {}", e);
                    }
                });
            }

            metadata
        };

        // Update cache
        self.code_metadata_cache
            .lock()
            .map_err(|_| StoreError::LockError)?
            .put(code_hash, metadata);

        Ok(Some(metadata))
    }

    /// Add account code
    pub async fn add_account_code(&self, code: Code) -> Result<(), StoreError> {
        let hash_key = code.hash.0.to_vec();
        let buf = encode_code(&code);
        let metadata_buf = (code.len() as u64).to_be_bytes();

        // Write both code and metadata atomically
        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            let mut tx = backend.begin_write()?;
            tx.put(ACCOUNT_CODES, &hash_key, &buf)?;
            tx.put(ACCOUNT_CODE_METADATA, &hash_key, &metadata_buf)?;
            tx.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Clears all checkpoint data created during the last snap sync
    pub async fn clear_snap_state(&self) -> Result<(), StoreError> {
        let db = self.backend.clone();
        tokio::task::spawn_blocking(move || db.clear_table(SNAP_STATE))
            .await
            .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    pub async fn get_transaction_by_hash(
        &self,
        transaction_hash: H256,
    ) -> Result<Option<Transaction>, StoreError> {
        let (_block_number, block_hash, index) =
            match self.get_transaction_location(transaction_hash).await? {
                Some(location) => location,
                None => return Ok(None),
            };
        self.get_transaction_by_location(block_hash, index).await
    }

    pub async fn get_transaction_by_location(
        &self,
        block_hash: H256,
        index: u64,
    ) -> Result<Option<Transaction>, StoreError> {
        let block_body = match self.get_block_body_by_hash(block_hash).await? {
            Some(body) => body,
            None => return Ok(None),
        };
        let index: usize = index.try_into()?;
        Ok(block_body.transactions.get(index).cloned())
    }

    pub async fn get_block_by_hash(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<Block>, StoreError> {
        let header = match self.get_block_header_by_hash(block_hash)? {
            Some(header) => header,
            None => return Ok(None),
        };
        let body = match self.get_block_body_by_hash(block_hash).await? {
            Some(body) => body,
            None => return Ok(None),
        };
        Ok(Some(Block::new(header, body)))
    }

    pub async fn get_block_by_number(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<Block>, StoreError> {
        let Some(block_hash) = self.get_canonical_block_hash(block_number).await? else {
            return Ok(None);
        };
        self.get_block_by_hash(block_hash).await
    }

    // Get the canonical block hash for a given block number.
    pub async fn get_canonical_block_hash(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHash>, StoreError> {
        let last = self.latest_block_header.get();
        if last.number == block_number {
            return Ok(Some(last.hash()));
        }
        let backend = self.backend.clone();
        tokio::task::spawn_blocking(move || {
            backend
                .begin_read()?
                .get(
                    CANONICAL_BLOCK_HASHES,
                    block_number.to_le_bytes().as_slice(),
                )?
                .map(|bytes| H256::decode(bytes.as_slice()))
                .transpose()
                .map_err(StoreError::from)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Stores the chain configuration values, should only be called once after reading the genesis file
    /// Ignores previously stored values if present
    pub async fn set_chain_config(&mut self, chain_config: &ChainConfig) -> Result<(), StoreError> {
        self.chain_config = *chain_config;
        let key = chain_data_key(ChainDataIndex::ChainConfig);
        let value = serde_json::to_string(chain_config)
            .map_err(|_| StoreError::Custom("Failed to serialize chain config".to_string()))?
            .into_bytes();
        self.write_async(CHAIN_DATA, key, value).await
    }

    /// Update earliest block number
    pub async fn update_earliest_block_number(
        &self,
        block_number: BlockNumber,
    ) -> Result<(), StoreError> {
        let key = chain_data_key(ChainDataIndex::EarliestBlockNumber);
        let value = block_number.to_le_bytes().to_vec();
        self.write_async(CHAIN_DATA, key, value).await
    }

    /// Reads a block number stored under the given chain-data index.
    async fn read_chain_data_block_number(
        &self,
        index: ChainDataIndex,
    ) -> Result<Option<BlockNumber>, StoreError> {
        self.read_async(CHAIN_DATA, chain_data_key(index))
            .await?
            .map(decode_block_number)
            .transpose()
    }

    /// Obtain earliest block number
    pub async fn get_earliest_block_number(&self) -> Result<BlockNumber, StoreError> {
        self.read_chain_data_block_number(ChainDataIndex::EarliestBlockNumber)
            .await?
            .ok_or(StoreError::MissingEarliestBlockNumber)
    }

    /// Obtain finalized block number
    pub async fn get_finalized_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        self.read_chain_data_block_number(ChainDataIndex::FinalizedBlockNumber)
            .await
    }

    /// Obtain safe block number
    pub async fn get_safe_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        self.read_chain_data_block_number(ChainDataIndex::SafeBlockNumber)
            .await
    }

    /// Obtain latest block number
    pub fn get_latest_block_number(&self) -> Result<BlockNumber, StoreError> {
        Ok(self.latest_block_header.get().number)
    }

    /// Update pending block number
    pub async fn update_pending_block_number(
        &self,
        block_number: BlockNumber,
    ) -> Result<(), StoreError> {
        let key = chain_data_key(ChainDataIndex::PendingBlockNumber);
        let value = block_number.to_le_bytes().to_vec();
        self.write_async(CHAIN_DATA, key, value).await
    }

    /// Obtain pending block number
    pub async fn get_pending_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        self.read_chain_data_block_number(ChainDataIndex::PendingBlockNumber)
            .await
    }

    /// DB mutation step of `forkchoice_update`.
    ///
    /// Callers MUST hold `fcu_lock` (only `forkchoice_update` should invoke this).
    /// The read of `LatestBlockNumber` below happens outside the write
    /// transaction and would be a TOCTOU window without that serialization.
    /// Applies a fork-choice update atomically: writes canonical hashes, latest/safe/
    /// finalized numbers, and prunes the state-history journal up to the new finalized.
    ///
    /// **Concurrency:** This function must be called with exclusive access. In
    /// production it is gated by `fcu_lock` from [`forkchoice_update`]. The
    /// pre-finalized read used to decide journal pruning happens outside the write
    /// transaction; without serialization, two concurrent callers could race on
    /// `prev_finalized` and double-prune. Tests that call this directly must not
    /// run concurrently with another FCU.
    async fn forkchoice_update_inner(
        &self,
        new_canonical_blocks: Vec<(BlockNumber, BlockHash)>,
        head_number: BlockNumber,
        head_hash: BlockHash,
        safe: Option<BlockNumber>,
        finalized: Option<BlockNumber>,
    ) -> Result<(), StoreError> {
        let latest = self.load_latest_block_number().await?.unwrap_or(0);
        let db = self.backend.clone();
        let journal_pruning_paused = self.journal_pruning_paused.clone();
        tokio::task::spawn_blocking(move || {
            let mut txn = db.begin_write()?;

            for (block_number, block_hash) in new_canonical_blocks {
                let head_key = block_number.to_le_bytes();
                let head_value = block_hash.encode_to_vec();
                txn.put(CANONICAL_BLOCK_HASHES, &head_key, &head_value)?;
            }

            // Delete canonical entries above the new head by enumerating each key.
            // `delete_range` is not safe here: keys are `u64::to_le_bytes()`, and
            // RocksDB's lexicographic comparator does not match LE numeric order
            // (e.g. block 256 = [0x00, 0x01, ..] sorts before block 11 = [0x0B, ..]),
            // so a range-delete would silently miss blocks whose LE first byte is
            // smaller than `head+1`'s first byte.
            for number in (head_number + 1)..=(latest) {
                txn.delete(CANONICAL_BLOCK_HASHES, number.to_le_bytes().as_slice())?;
            }

            // Make head canonical
            let head_key = head_number.to_le_bytes();
            let head_value = head_hash.encode_to_vec();
            txn.put(CANONICAL_BLOCK_HASHES, &head_key, &head_value)?;

            // Update chain data
            let latest_key = chain_data_key(ChainDataIndex::LatestBlockNumber);
            txn.put(CHAIN_DATA, &latest_key, &head_number.to_le_bytes())?;

            if let Some(safe) = safe {
                let safe_key = chain_data_key(ChainDataIndex::SafeBlockNumber);
                txn.put(CHAIN_DATA, &safe_key, &safe.to_le_bytes())?;
            }

            if let Some(finalized) = finalized {
                let finalized_key = chain_data_key(ChainDataIndex::FinalizedBlockNumber);

                // Read the previous finalized number from the same backend before we
                // overwrite it. The journal can only be pruned when finality actually
                // advances; a no-op or backwards FCU must not touch STATE_HISTORY.
                // Pre-merge or fresh chains have no entry; treat as 0.
                //
                // A length mismatch is treated as a hard error rather than a silent
                // fallback to 0: if a future schema change stores this field with a
                // different width, the silent default would make `finalized > 0` true
                // for every FCU and prune the entire journal. Bail out instead.
                let read = db.begin_read()?;
                let prev_finalized = match read.get(CHAIN_DATA, &finalized_key)? {
                    Some(bytes) => {
                        let arr: [u8; 8] = bytes.as_slice().try_into().map_err(|_| {
                            StoreError::Custom(format!(
                                "FinalizedBlockNumber has unexpected length {} (want 8)",
                                bytes.len()
                            ))
                        })?;
                        BlockNumber::from_le_bytes(arr)
                    }
                    None => 0,
                };

                txn.put(CHAIN_DATA, &finalized_key, &finalized.to_le_bytes())?;

                // Prune every STATE_HISTORY entry at or below the new finalized number
                // in the same atomic txn. STATE_HISTORY uses big-endian keys, so
                // lexicographic byte order matches numeric order, and `delete_range`
                // is half-open `[start, end)`.
                //
                // The range covers only entries that exist: it starts at the lowest
                // entry present and ends at the highest one at or below finality, and
                // is skipped when the lowest entry is above finality. Entries are
                // written ~DB_COMMIT_THRESHOLD blocks behind head, in increasing block
                // order, while finality can be ahead of them (an L2 finalizes head on
                // every block), so a range ending at `finalized` would cover block
                // numbers whose entries land later. A range delete only hides keys
                // written before it, so those entries would survive and the next
                // prune would overlap this one. Ending at the highest existing entry
                // keeps every range disjoint from the previous ones, and none is
                // written when there is nothing to prune. That matters because
                // RocksDB keeps unflushed range deletes in the memtable and fragments
                // them against each other on reads and flushes: N ranges that all
                // start at 0 overlap pairwise, so the fragmented form grows as N², and
                // a memtable holding a few thousand of them takes seconds and GBs to
                // flush or replay from the WAL. Disjoint ranges fragment in linear
                // time and memory.
                //
                // Skipped while a deep-reorg apply pass is in flight
                // (`journal_pruning_paused`): `Overlay::from_journal` reads entries
                // with no snapshot isolation, so pruning mid-construction fails it
                // with a spurious `MissingEntry`. The finalized-number update above
                // still lands; pruning catches up on the next advance after the
                // pass ends, because the range starts at whatever entry is lowest.
                if finalized > prev_finalized
                    && !journal_pruning_paused.load(std::sync::atomic::Ordering::Acquire)
                    && let Some(lowest) = read.first_key(STATE_HISTORY)?
                {
                    let lowest = state_history_key_number(&lowest)?;
                    if lowest <= finalized {
                        // `first_key` returned an entry, so `last_key` returns one too.
                        let highest = match read.last_key(STATE_HISTORY)? {
                            Some(key) => state_history_key_number(&key)?,
                            None => finalized,
                        };
                        let end = finalized.min(highest).saturating_add(1);
                        txn.delete_range(STATE_HISTORY, &lowest.to_be_bytes(), &end.to_be_bytes())?;
                    }
                }
            }

            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    pub async fn get_receipts_for_block(
        &self,
        block_hash: &BlockHash,
    ) -> Result<Vec<Receipt>, StoreError> {
        self.get_receipts_for_block_from_index(block_hash, 0, None)
            .await
    }

    /// Retrieves receipts for a block starting from the given index,
    /// optionally limited to `max_count` receipts.
    ///
    /// Uses cursor-based prefix iteration over the 32-byte block hash prefix
    /// for efficient batch retrieval. Used by:
    /// - eth/70 partial receipt requests (EIP-7975) via p2p
    /// - `eth_getTransactionReceipt` RPC with a count limit to avoid
    ///   fetching the entire block's receipts
    pub async fn get_receipts_for_block_from_index(
        &self,
        block_hash: &BlockHash,
        start_index: u64,
        max_count: Option<usize>,
    ) -> Result<Vec<Receipt>, StoreError> {
        if let Some(all) = self.buffer()?.get_receipts(block_hash) {
            let start = start_index as usize;
            let slice = all.into_iter().skip(start);
            return Ok(match max_count {
                Some(max) => slice.take(max).collect(),
                None => slice.collect(),
            });
        }
        let backend = self.backend.clone();
        let block_hash = *block_hash;

        tokio::task::spawn_blocking(move || {
            let txn = backend.begin_read()?;
            let prefix = block_hash.as_bytes().to_vec();
            // Seek directly to block_hash || start_index to avoid O(start_index) scan.
            // Keys are big-endian u64, so lexicographic order matches numeric order.
            let mut seek_key = prefix.clone();
            seek_key.extend_from_slice(&start_index.to_be_bytes());
            let iter = txn.prefix_iterator(RECEIPTS_V2, &seek_key)?;
            let mut receipts = Vec::new();
            for result in iter {
                let (k, v) = result?;
                if !k.starts_with(&prefix) {
                    break;
                }
                if k.len() != 40 {
                    continue;
                }
                receipts.push(Receipt::decode_storage(v.as_ref())?);
                if let Some(max) = max_count
                    && receipts.len() >= max
                {
                    break;
                }
            }
            Ok(receipts)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {e}")))?
    }

    // Snap State methods

    /// Sets the hash of the last header downloaded during a snap sync
    pub async fn set_header_download_checkpoint(
        &self,
        block_hash: BlockHash,
    ) -> Result<(), StoreError> {
        let key = snap_state_key(SnapStateIndex::HeaderDownloadCheckpoint);
        let value = block_hash.encode_to_vec();
        self.write_async(SNAP_STATE, key, value).await
    }

    /// Gets the hash of the last header downloaded during a snap sync
    pub async fn get_header_download_checkpoint(&self) -> Result<Option<BlockHash>, StoreError> {
        let key = snap_state_key(SnapStateIndex::HeaderDownloadCheckpoint);
        self.backend
            .begin_read()?
            .get(SNAP_STATE, &key)?
            .map(|bytes| H256::decode(bytes.as_slice()))
            .transpose()
            .map_err(StoreError::from)
    }

    /// The `forkchoice_update` and `new_payload` methods require the `latest_valid_hash`
    /// when processing an invalid payload. To provide this, we must track invalid chains.
    ///
    /// We only store the last known valid head upon encountering a bad block,
    /// rather than tracking every subsequent invalid block.
    pub async fn set_latest_valid_ancestor(
        &self,
        bad_block: BlockHash,
        latest_valid: BlockHash,
    ) -> Result<(), StoreError> {
        let value = latest_valid.encode_to_vec();
        self.write_async(INVALID_CHAINS, bad_block.as_bytes().to_vec(), value)
            .await
    }

    /// Returns the latest valid ancestor hash for a given invalid block hash.
    /// Used to provide `latest_valid_hash` in the Engine API when processing invalid payloads.
    pub async fn get_latest_valid_ancestor(
        &self,
        block: BlockHash,
    ) -> Result<Option<BlockHash>, StoreError> {
        self.read_async(INVALID_CHAINS, block.as_bytes().to_vec())
            .await?
            .map(|bytes| H256::decode(bytes.as_slice()))
            .transpose()
            .map_err(StoreError::from)
    }

    /// Records a block that failed validation so it can be served by
    /// `debug_getBadBlocks`. The list is bounded to [`MAX_BAD_BLOCKS`] entries,
    /// kept sorted by descending block number, with the oldest dropped once the
    /// bound is exceeded. Duplicate `(number, hash)` entries are ignored.
    pub async fn add_bad_block(&self, block: Block) -> Result<(), StoreError> {
        let mut bad_blocks = self.get_bad_blocks().await?;
        let block_number = block.header.number;
        let block_hash = block.hash();
        if bad_blocks
            .iter()
            .any(|b| b.header.number == block_number && b.hash() == block_hash)
        {
            return Ok(());
        }
        bad_blocks.push(block);
        bad_blocks.sort_by(|a, b| b.header.number.cmp(&a.header.number));
        bad_blocks.truncate(MAX_BAD_BLOCKS);
        self.write_async(
            BAD_BLOCKS,
            BAD_BLOCKS_KEY.to_vec(),
            bad_blocks.encode_to_vec(),
        )
        .await
    }

    /// Returns the recent bad blocks seen by the client, sorted by descending
    /// block number. Used by `debug_getBadBlocks`.
    pub async fn get_bad_blocks(&self) -> Result<Vec<Block>, StoreError> {
        match self.read_async(BAD_BLOCKS, BAD_BLOCKS_KEY.to_vec()).await? {
            Some(bytes) => Vec::<Block>::decode(&bytes).map_err(StoreError::from),
            None => Ok(Vec::new()),
        }
    }

    /// Obtain block number for a given hash
    pub fn get_block_number_sync(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockNumber>, StoreError> {
        if let Some(n) = self.buffer()?.get_number(&block_hash) {
            return Ok(Some(n));
        }
        let txn = self.backend.begin_read()?;
        txn.get(BLOCK_NUMBERS, &block_hash.encode_to_vec())?
            .map(decode_block_number)
            .transpose()
    }

    /// Get the canonical block hash for a given block number.
    pub fn get_canonical_block_hash_sync(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHash>, StoreError> {
        let last = self.latest_block_header.get();
        if last.number == block_number {
            return Ok(Some(last.hash()));
        }
        let txn = self.backend.begin_read()?;
        txn.get(
            CANONICAL_BLOCK_HASHES,
            block_number.to_le_bytes().as_slice(),
        )?
        .map(|bytes| H256::decode(bytes.as_slice()))
        .transpose()
        .map_err(StoreError::from)
    }

    /// CAUTION: This method writes directly to the underlying database, bypassing any caching layer.
    /// For updating the state after block execution, use [`Self::store_block_updates`].
    pub async fn write_storage_trie_nodes_batch(
        &self,
        storage_trie_nodes: StorageUpdates,
    ) -> Result<(), StoreError> {
        let mut txn = self.backend.begin_write()?;
        tokio::task::spawn_blocking(move || {
            for (address_hash, nodes) in storage_trie_nodes {
                for (node_path, node_data) in nodes {
                    let key = apply_prefix(Some(address_hash), node_path);
                    if node_data.is_empty() {
                        txn.delete(STORAGE_TRIE_NODES, key.as_ref())?;
                    } else {
                        txn.put(STORAGE_TRIE_NODES, key.as_ref(), &node_data)?;
                    }
                }
            }
            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// CAUTION: This method writes directly to the underlying database, bypassing any caching layer.
    /// For updating the state after block execution, use [`Self::store_block_updates`].
    pub async fn write_account_code_batch(
        &self,
        account_codes: Vec<(H256, Code)>,
    ) -> Result<(), StoreError> {
        let mut code_batch_items = Vec::new();
        let mut metadata_batch_items = Vec::new();

        for (code_hash, code) in account_codes {
            let buf = encode_code(&code);
            let metadata_buf = (code.len() as u64).to_be_bytes().to_vec();
            code_batch_items.push((code_hash.as_bytes().to_vec(), buf));
            metadata_batch_items.push((code_hash.as_bytes().to_vec(), metadata_buf));
        }

        // Write both batches
        self.write_batch_async(ACCOUNT_CODES, code_batch_items)
            .await?;
        self.write_batch_async(ACCOUNT_CODE_METADATA, metadata_batch_items)
            .await
    }

    /// Returns a snapshot of the current block-data buffer.
    fn buffer(&self) -> Result<Arc<BlockDataBuffer>, StoreError> {
        Ok(self
            .block_data_buffer
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone())
    }

    // Helper methods for async operations with spawn_blocking
    // These methods ensure RocksDB I/O doesn't block the tokio runtime

    /// Helper method for async writes
    /// Spawns blocking task to avoid blocking tokio runtime
    pub fn write(
        &self,
        table: &'static str,
        key: Vec<u8>,
        value: Vec<u8>,
    ) -> Result<(), StoreError> {
        let backend = self.backend.clone();
        let mut txn = backend.begin_write()?;
        txn.put(table, &key, &value)?;
        txn.commit()
    }

    /// Helper method for async writes
    /// Spawns blocking task to avoid blocking tokio runtime
    async fn write_async(
        &self,
        table: &'static str,
        key: Vec<u8>,
        value: Vec<u8>,
    ) -> Result<(), StoreError> {
        let backend = self.backend.clone();

        tokio::task::spawn_blocking(move || {
            let mut txn = backend.begin_write()?;
            txn.put(table, &key, &value)?;
            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Helper method for async reads
    /// Spawns blocking task to avoid blocking tokio runtime
    pub async fn read_async(
        &self,
        table: &'static str,
        key: Vec<u8>,
    ) -> Result<Option<Vec<u8>>, StoreError> {
        let backend = self.backend.clone();

        tokio::task::spawn_blocking(move || {
            let txn = backend.begin_read()?;
            txn.get(table, &key)
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Helper method for sync reads
    /// Spawns blocking task to avoid blocking tokio runtime
    pub fn read(&self, table: &'static str, key: Vec<u8>) -> Result<Option<Vec<u8>>, StoreError> {
        let backend = self.backend.clone();
        let txn = backend.begin_read()?;
        txn.get(table, &key)
    }

    /// Helper method for batch writes
    /// Spawns blocking task to avoid blocking tokio runtime
    /// This is the most important optimization for healing performance
    pub async fn write_batch_async(
        &self,
        table: &'static str,
        batch_ops: Vec<(Vec<u8>, Vec<u8>)>,
    ) -> Result<(), StoreError> {
        let backend = self.backend.clone();

        tokio::task::spawn_blocking(move || {
            let mut txn = backend.begin_write()?;
            txn.put_batch(table, batch_ops)?;
            txn.commit()
        })
        .await
        .map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))?
    }

    /// Helper method for batch writes
    pub fn write_batch(
        &self,
        table: &'static str,
        batch_ops: Vec<(Vec<u8>, Vec<u8>)>,
    ) -> Result<(), StoreError> {
        let backend = self.backend.clone();
        let mut txn = backend.begin_write()?;
        txn.put_batch(table, batch_ops)?;
        txn.commit()
    }

    pub async fn add_fullsync_batch(&self, headers: Vec<BlockHeader>) -> Result<(), StoreError> {
        self.write_batch_async(
            FULLSYNC_HEADERS,
            headers
                .into_iter()
                .map(|header| (header.number.to_le_bytes().to_vec(), header.encode_to_vec()))
                .collect(),
        )
        .await
    }

    pub async fn read_fullsync_batch(
        &self,
        start: BlockNumber,
        limit: u64,
    ) -> Result<Vec<Option<BlockHeader>>, StoreError> {
        let mut res = vec![];
        let read_tx = self.backend.begin_read()?;
        // TODO: use read_bulk here
        for key in start..start + limit {
            let header_opt = read_tx
                .get(FULLSYNC_HEADERS, &key.to_le_bytes())?
                .map(|header| BlockHeader::decode(&header))
                .transpose()?;
            res.push(header_opt);
        }
        Ok(res)
    }

    pub async fn clear_fullsync_headers(&self) -> Result<(), StoreError> {
        self.backend.clear_table(FULLSYNC_HEADERS)
    }

    /// Delete a key from a table
    pub fn delete(&self, table: &'static str, key: Vec<u8>) -> Result<(), StoreError> {
        let mut txn = self.backend.begin_write()?;
        txn.delete(table, &key)?;
        txn.commit()
    }

    pub fn store_block_updates(&self, update_batch: UpdateBatch) -> Result<(), StoreError> {
        self.apply_updates(update_batch)
    }

    /// Compute `(parent_state_root, last_state_root, last_block_number,
    /// last_block_hash)` for a batch's trie update: the state root of the first
    /// block's parent, the last block's own state root, and the last block's
    /// number and hash (the identity the journal records for the committed
    /// layer). Used by `apply_updates` for both the live and full-sync paths
    /// (which share the single persist worker).
    fn batch_state_roots(
        &self,
        update_batch: &UpdateBatch,
    ) -> Result<(H256, H256, BlockNumber, H256), StoreError> {
        let parent_state_root = self
            .get_block_header_by_hash(
                update_batch
                    .blocks
                    .first()
                    .ok_or(StoreError::UpdateBatchNoBlocks)?
                    .header
                    .parent_hash,
            )?
            .map(|header| header.state_root)
            .unwrap_or_default();
        let last_block = update_batch
            .blocks
            .last()
            .ok_or(StoreError::UpdateBatchNoBlocks)?;
        let last_state_root = last_block.header.state_root;
        let last_block_number = last_block.header.number;
        let last_block_hash = last_block.hash();
        Ok((
            parent_state_root,
            last_state_root,
            last_block_number,
            last_block_hash,
        ))
    }

    /// Single path for all updates: hand the whole unit (block data + one aggregate trie
    /// diff) to the SINGLE persist worker and wait for its ack. `commit_depth` selects the
    /// commit gate; `wait_for_flush` selects when the worker acks (see [`UpdateBatch`]).
    fn apply_updates(&self, update_batch: UpdateBatch) -> Result<(), StoreError> {
        let (parent_state_root, last_state_root, last_block_number, last_block_hash) =
            self.batch_state_roots(&update_batch)?;

        let UpdateBatch {
            account_updates,
            storage_updates,
            blocks,
            receipts,
            code_updates,
            commit_depth,
            wait_for_flush,
        } = update_batch;

        // Register before handing off to the worker and before this returns, so
        // any reader opening this root blocks in `gated_snapshot` until the
        // layer is installed rather than snapshotting a stale cache.
        self.pending_trie_roots.register(last_state_root)?;

        // Pair blocks with receipts. Single-block fast path avoids a HashMap
        // allocation; full-sync batch joins by hash.
        let blocks_with_receipts: Vec<(Block, Vec<Receipt>)> = if blocks.len() == 1 {
            let block = blocks.into_iter().next().expect("len == 1");
            let hash = block.hash();
            let r = receipts
                .into_iter()
                .find(|(h, _)| *h == hash)
                .map(|(_, r)| r)
                .unwrap_or_default();
            vec![(block, r)]
        } else {
            let mut receipts_by_hash: std::collections::HashMap<BlockHash, Vec<Receipt>> =
                receipts.into_iter().collect();
            blocks
                .into_iter()
                .map(|b| {
                    let r = receipts_by_hash.remove(&b.hash()).unwrap_or_default();
                    (b, r)
                })
                .collect()
        };

        // Send to the persist worker and wait for its ack.
        // wait_for_flush=false: worker acks after staging; the ack carries the PRIOR flush
        //   result so a disk error surfaces on the next call.
        // wait_for_flush=true: worker acks after flush, bounding in-flight work to ~1.
        let (ack_tx, ack_rx) = sync_channel(1);
        self.persist_tx
            .send(PersistMessage::Block(Box::new(BlockPersist {
                blocks: blocks_with_receipts,
                codes: code_updates,
                parent_state_root,
                child_state_root: last_state_root,
                account_updates,
                storage_updates,
                commit_depth,
                wait_for_flush,
                block_number: last_block_number,
                block_hash: last_block_hash,
                ack: ack_tx,
            })))
            .map_err(|e| StoreError::Custom(format!("failed to send block persist: {e}")))?;
        ack_rx
            .recv()
            .map_err(|e| StoreError::Custom(format!("block persist ack failed: {e}")))??;

        Ok(())
    }

    /// Opens (or creates) a store at `path` with the default [`StoreConfig`].
    ///
    /// Production callers that need to override storage tunables (e.g. the RocksDB
    /// block cache size from a CLI option) should use [`Store::new_with_config`].
    pub fn new(path: impl AsRef<Path>, engine_type: EngineType) -> Result<Self, StoreError> {
        Self::new_with_config(path, engine_type, StoreConfig::default())
    }

    /// Opens (or creates) a store at `path`, applying the supplied [`StoreConfig`].
    pub fn new_with_config(
        path: impl AsRef<Path>,
        engine_type: EngineType,
        config: StoreConfig,
    ) -> Result<Self, StoreError> {
        let code_cache_size = config.code_cache_size.unwrap_or(CODE_CACHE_MAX_SIZE);
        let store = Self::open_with_config(path, engine_type, config)?;
        if let Ok(mut cache) = store.account_code_cache.lock() {
            cache.max_size = code_cache_size;
        }
        info!(cache_bytes = code_cache_size, "sized bytecode cache");
        Ok(store)
    }

    fn open_with_config(
        path: impl AsRef<Path>,
        engine_type: EngineType,
        // `config` only feeds the RocksDB backend; without that feature it is unused.
        #[cfg_attr(not(feature = "rocksdb"), allow(unused_variables))] config: StoreConfig,
    ) -> Result<Self, StoreError> {
        let db_path = path.as_ref().to_path_buf();

        if engine_type != EngineType::InMemory {
            let version = read_store_schema_version(&db_path)?;

            match version {
                None if db_path.exists() && dir_contains_legacy_db(&db_path)? => {
                    // Pre-metadata DB — cannot migrate safely
                    return Err(StoreError::NotFoundDBVersion);
                }
                None => {
                    // No metadata and no recognizable database files. The directory
                    // may still hold unrelated files (e.g. a JWT secret placed in the
                    // datadir by tooling such as EthDocker, see issue #5680), so treat
                    // this as a fresh datadir and write the initial metadata instead
                    // of erroring out.
                    init_metadata_file(&db_path)?;
                }
                // Neither arm below runs a migration, so neither is a
                // `MigrationFailed`: that variant tells the operator the data may be
                // half-converted, which is false here — the database is untouched.
                Some(v) if v < 1 => {
                    // No ethrex ever wrote a v0 marker; the file is corrupt or hand-edited.
                    return Err(StoreError::IncompatibleDBVersion {
                        found: v,
                        expected: STORE_SCHEMA_VERSION,
                    });
                }
                Some(v) if v > STORE_SCHEMA_VERSION => {
                    // Written by a newer ethrex. Downgrading is unsupported, but the
                    // database is intact: a binary that speaks v{v} opens it as is.
                    return Err(StoreError::IncompatibleDBVersion {
                        found: v,
                        expected: STORE_SCHEMA_VERSION,
                    });
                }
                #[cfg(feature = "rocksdb")]
                Some(v) if v < STORE_SCHEMA_VERSION => {
                    // Open backend, run migrations, then drop obsolete CFs.
                    // Cleanup must happen AFTER migrations so legacy CFs (e.g.
                    // `receipts`) are still readable during the migration.
                    let rocksdb = Arc::new(RocksDBBackend::open(
                        &path,
                        config.rocksdb_block_cache_size,
                    )?);
                    crate::migrations::run_pending_migrations(rocksdb.as_ref(), &db_path, v)?;
                    rocksdb.drop_obsolete_cfs(&path);
                    let backend: Arc<dyn crate::api::StorageBackend> = rocksdb;
                    return Self::from_backend(
                        backend,
                        db_path,
                        DB_COMMIT_THRESHOLD,
                        config.persist_channel_capacity,
                    );
                }
                Some(_) => {
                    // version == STORE_SCHEMA_VERSION, proceed normally.
                    // Without the `rocksdb` feature this also covers v < target,
                    // but that path is unreachable since InMemory is the only
                    // engine type and the outer guard excludes it.
                }
            }
        }

        match engine_type {
            #[cfg(feature = "rocksdb")]
            EngineType::RocksDB => {
                let rocksdb = RocksDBBackend::open(&path, config.rocksdb_block_cache_size)?;
                rocksdb.drop_obsolete_cfs(&path);
                let backend: Arc<dyn StorageBackend> = Arc::new(rocksdb);
                Self::from_backend(
                    backend,
                    db_path,
                    DB_COMMIT_THRESHOLD,
                    config.persist_channel_capacity,
                )
            }
            EngineType::InMemory => {
                let backend = Arc::new(InMemoryBackend::open()?);
                Self::from_backend(
                    backend,
                    db_path,
                    IN_MEMORY_COMMIT_THRESHOLD,
                    config.persist_channel_capacity,
                )
            }
        }
    }

    fn from_backend(
        backend: Arc<dyn StorageBackend>,
        db_path: PathBuf,
        commit_threshold: usize,
        persist_channel_capacity: usize,
    ) -> Result<Self, StoreError> {
        debug!("Initializing Store with {commit_threshold} in-memory diff-layers");
        let (fkv_tx, fkv_rx) = std::sync::mpsc::sync_channel(0);
        let persist_cap = persist_channel_capacity.max(1); // clamp: 0 would be a rendezvous channel
        let (persist_tx, persist_rx) = std::sync::mpsc::sync_channel(persist_cap);

        let (last_written, initial_flushed_upto) = {
            let tx = backend.begin_read()?;
            let last_written = tx
                .get(MISC_VALUES, "last_written".as_bytes())?
                .unwrap_or_else(|| vec![0u8; 64]);
            let last_written = if last_written == [0xff] {
                vec![0xff; 64]
            } else {
                last_written
            };
            let initial_flushed_upto = match tx.get(MISC_VALUES, FLUSHED_UPTO_KEY)? {
                Some(bytes) => decode_flushed_upto(&bytes)?,
                None => 0,
            };
            (last_written, initial_flushed_upto)
        };
        let mut initial_buffer = BlockDataBuffer::new();
        initial_buffer.set_flushed_upto(initial_flushed_upto);

        let mut background_threads = Vec::new();
        let safe_commit_root = Arc::new(RwLock::new(H256::zero()));
        let mut store = Self {
            db_path,
            backend,
            chain_config: Default::default(),
            latest_block_header: Default::default(),
            trie_cache: Arc::new(RwLock::new(Arc::new(TrieLayerCache::new_with_safe_commit(
                commit_threshold,
                safe_commit_root.clone(),
            )))),
            flatkeyvalue_control_tx: fkv_tx,
            block_data_buffer: Arc::new(RwLock::new(Arc::new(initial_buffer))),
            persist_tx,
            pending_trie_roots: Arc::new(PendingTrieRoots::default()),
            last_computed_flatkeyvalue: Arc::new(RwLock::new(last_written)),
            account_code_cache: Arc::new(Mutex::new(CodeCache::default())),
            code_metadata_cache: Arc::new(Mutex::new(LruCache::with_hasher(
                NonZeroUsize::new(CODE_METADATA_CACHE_MAX_ENTRIES).unwrap_or(NonZeroUsize::MIN),
                SeededState::default(),
            ))),
            fcu_lock: Arc::new(tokio::sync::Mutex::new(())),
            safe_commit_root,
            journal_pruning_paused: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            background_threads: Default::default(),
        };
        let backend_clone = store.backend.clone();
        let last_computed_fkv = store.last_computed_flatkeyvalue.clone();
        background_threads.push(std::thread::spawn(move || {
            let rx = fkv_rx;
            // Wait for the first Continue to start generation
            loop {
                match rx.recv() {
                    Ok(FKVGeneratorControlMessage::Continue) => break,
                    Ok(FKVGeneratorControlMessage::Stop) => {}
                    Err(std::sync::mpsc::RecvError) => {
                        debug!("Closing FlatKeyValue generator.");
                        return;
                    }
                }
            }

            let _ = flatkeyvalue_generator(&backend_clone, &last_computed_fkv, &rx)
                .inspect_err(|err| error!("Error while generating FlatKeyValue: {err}"));
        }));
        // The single persist worker: sole swapper of `block_data_buffer`, sole
        // builder of trie diff-layers. One DB transaction per `Block` message.
        let persist_backend = store.backend.clone();
        let persist_buffer = store.block_data_buffer.clone();
        let persist_trie_cache = store.trie_cache.clone();
        let persist_pending_roots = store.pending_trie_roots.clone();
        let persist_fkv_ctl = store.flatkeyvalue_control_tx.clone();
        background_threads.push(std::thread::spawn(move || {
            let rx = persist_rx;
            // Carries the prior flush result: the live path acks after staging,
            // so a disk failure surfaces on the next message's ack.
            let mut last_flush_result: Result<(), StoreError> = Ok(());
            loop {
                match rx.recv() {
                    Ok(PersistMessage::Block(bp)) => {
                        let bp = *bp;
                        // Stage block data (sole swapper of the buffer; codes
                        // are batch-level and attributed to the first block).
                        let staged = mutate_block_buffer(&persist_buffer, move |b| {
                            let mut codes = Some(bp.codes);
                            for (block, receipts) in bp.blocks {
                                b.insert(block, receipts, codes.take().unwrap_or_default());
                            }
                        });
                        if let Err(e) = staged {
                            // Stage failure is terminal for this message.
                            // Clear the pending root so gated readers are not
                            // blocked forever (apply_trie_phase1, which normally
                            // does this, is skipped when we continue here).
                            persist_pending_roots.clear(bp.child_state_root);
                            let _ = bp.ack.send(Err(e));
                            continue;
                        }
                        // ACK-AFTER-STAGING: ack now, carrying the prior flush result.
                        // NOTE: this acks block validity BEFORE apply_trie_phase1
                        // installs the trie layer below. A phase-1 failure (only
                        // reachable via lock poisoning, which is already fatal) is
                        // therefore deferred to the next block's ack via
                        // last_flush_result rather than attributed to this block;
                        // the pending root is still cleared unconditionally, so
                        // gated readers error rather than hang.
                        if !bp.wait_for_flush {
                            let _ = bp
                                .ack
                                .send(std::mem::replace(&mut last_flush_result, Ok(())));
                        }
                        // Build + install the trie layer; clear the read gate.
                        if let Err(err) = apply_trie_phase1(
                            &persist_trie_cache,
                            &persist_pending_roots,
                            bp.parent_state_root,
                            bp.child_state_root,
                            bp.block_number,
                            bp.block_hash,
                            bp.account_updates,
                            bp.storage_updates,
                        ) {
                            error!("persist worker trie phase-1 failed: {err}");
                            if bp.wait_for_flush {
                                let _ = bp.ack.send(Err(err));
                            } else {
                                last_flush_result = Err(err);
                            }
                            continue;
                        }
                        // Flush block data + commit bottom trie layer when due.
                        let flushed = flush_block_data(persist_backend.as_ref(), &persist_buffer)
                            .inspect_err(|err| error!("flush_block_data failed: {err}"))
                            .and_then(|_| {
                                commit_trie_if_due(
                                    persist_backend.as_ref(),
                                    &persist_trie_cache,
                                    &persist_fkv_ctl,
                                    bp.parent_state_root,
                                    bp.commit_depth,
                                    bp.wait_for_flush,
                                )
                            });
                        // ACK-AFTER-FLUSH: ack now (bounds in-flight work to ~1), folding in
                        // any prior deferred error. ACK-AFTER-STAGING: stash for the next ack.
                        if bp.wait_for_flush {
                            let prior = std::mem::replace(&mut last_flush_result, Ok(()));
                            let _ = bp.ack.send(prior.and(flushed));
                        } else {
                            last_flush_result = flushed;
                        }
                    }
                    Ok(PersistMessage::Commit(root)) => match persist_trie_cache.read() {
                        Ok(guard) => {
                            let trie = guard.clone();
                            drop(guard);
                            // Forkchoice-driven flush is the live (non-batch) path, so
                            // journaling is enabled: pass `is_batch = false`.
                            let _ = commit_to_disk(
                                persist_backend.as_ref(),
                                &persist_fkv_ctl,
                                &persist_trie_cache,
                                &trie,
                                root,
                                false,
                            )
                            .inspect_err(|err| error!("commit_to_disk failed: {err}"));
                        }
                        Err(_) => error!("trie cache lock poisoned during commit"),
                    },
                    Ok(PersistMessage::Ping(ack)) => {
                        // Idle handshake: reached only after all earlier Block
                        // messages are fully processed. Carry the pending flush
                        // result so a live-path failure is not silently dropped.
                        let _ = ack.send(std::mem::replace(&mut last_flush_result, Ok(())));
                    }
                    Ok(PersistMessage::Shutdown { ack }) => {
                        // Graceful shutdown: drain (already guaranteed by FIFO) and
                        // force-flush the not-yet-flushed block-data tail. The trie
                        // diff-layers stay in memory and are dropped on exit: the
                        // on-disk trie is a single-version path store, so committing
                        // the non-finalized tail would make a post-restart reorg
                        // unrecoverable. Those layers re-execute on the next start.
                        let result = flush_block_data(persist_backend.as_ref(), &persist_buffer);
                        let prior = std::mem::replace(&mut last_flush_result, Ok(()));
                        let _ = ack.send(prior.and(result));
                        // No more work will follow a shutdown request.
                        return;
                    }
                    Err(_) => return,
                }
            }
        }));
        store.background_threads = Arc::new(ThreadList {
            list: background_threads,
        });
        Ok(store)
    }

    /// Opens (or creates) a store at `store_path` and seeds it from the
    /// given genesis file, using the default [`StoreConfig`].
    pub async fn new_from_genesis(
        store_path: &Path,
        engine_type: EngineType,
        genesis_path: &str,
    ) -> Result<Self, StoreError> {
        Self::new_from_genesis_with_config(
            store_path,
            engine_type,
            genesis_path,
            StoreConfig::default(),
        )
        .await
    }

    /// Opens (or creates) a store at `store_path` from genesis, applying the
    /// supplied [`StoreConfig`].
    pub async fn new_from_genesis_with_config(
        store_path: &Path,
        engine_type: EngineType,
        genesis_path: &str,
        config: StoreConfig,
    ) -> Result<Self, StoreError> {
        let file = std::fs::File::open(genesis_path)
            .map_err(|error| StoreError::Custom(format!("Failed to open genesis file: {error}")))?;
        let reader = std::io::BufReader::new(file);
        let genesis: Genesis = serde_json::from_reader(reader)
            .map_err(|e| StoreError::Custom(format!("Failed to deserialize genesis file: {e}")))?;
        let mut store = Self::new_with_config(store_path, engine_type, config)?;
        store.add_initial_state(genesis).await?;
        Ok(store)
    }

    pub async fn get_account_info(
        &self,
        block_number: BlockNumber,
        address: Address,
    ) -> Result<Option<AccountInfo>, StoreError> {
        match self.get_canonical_block_hash(block_number).await? {
            Some(block_hash) => self.get_account_info_by_hash(block_hash, address),
            None => Ok(None),
        }
    }

    pub fn get_account_info_by_hash(
        &self,
        block_hash: BlockHash,
        address: Address,
    ) -> Result<Option<AccountInfo>, StoreError> {
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        let hashed_address = hash_address_fixed(&address);

        let Some(encoded_state) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };

        let account_state = AccountState::decode(&encoded_state)?;
        Ok(Some(AccountInfo {
            code_hash: account_state.code_hash,
            balance: account_state.balance,
            nonce: account_state.nonce,
        }))
    }

    pub fn get_account_state_by_acc_hash(
        &self,
        block_hash: BlockHash,
        account_hash: H256,
    ) -> Result<Option<AccountState>, StoreError> {
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        let Some(encoded_state) = state_trie.get(account_hash.as_bytes())? else {
            return Ok(None);
        };
        let account_state = AccountState::decode(&encoded_state)?;
        Ok(Some(account_state))
    }

    pub async fn get_fork_id(&self) -> Result<ForkId, StoreError> {
        let chain_config = self.get_chain_config();
        let genesis_header = self
            .load_block_header(0)?
            .ok_or(StoreError::MissingEarliestBlockNumber)?;
        let block_header = self.latest_block_header.get();

        Ok(ForkId::new(
            chain_config,
            genesis_header,
            block_header.timestamp,
            block_header.number,
        ))
    }

    pub async fn get_code_by_account_address(
        &self,
        block_number: BlockNumber,
        address: Address,
    ) -> Result<Option<Code>, StoreError> {
        let Some(block_hash) = self.get_canonical_block_hash(block_number).await? else {
            return Ok(None);
        };
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        let hashed_address = hash_address_fixed(&address);
        let Some(encoded_state) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        let account_state = AccountState::decode(&encoded_state)?;
        self.get_account_code(account_state.code_hash)
    }

    pub async fn get_nonce_by_account_address(
        &self,
        block_number: BlockNumber,
        address: Address,
    ) -> Result<Option<u64>, StoreError> {
        let Some(block_hash) = self.get_canonical_block_hash(block_number).await? else {
            return Ok(None);
        };
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        let hashed_address = hash_address_fixed(&address);
        let Some(encoded_state) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        let account_state = AccountState::decode(&encoded_state)?;
        Ok(Some(account_state.nonce))
    }

    /// Applies account updates based on the block's latest storage state
    /// and returns the new state root after the updates have been applied.
    pub fn apply_account_updates_batch(
        &self,
        block_hash: BlockHash,
        account_updates: &[AccountUpdate],
    ) -> Result<Option<AccountUpdatesList>, StoreError> {
        let Some(mut state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };

        Ok(Some(self.apply_account_updates_from_trie_batch(
            &mut state_trie,
            account_updates,
        )?))
    }

    pub fn apply_account_updates_from_trie_batch<'a>(
        &self,
        state_trie: &mut Trie,
        account_updates: impl IntoIterator<Item = &'a AccountUpdate>,
    ) -> Result<AccountUpdatesList, StoreError> {
        let mut ret_storage_updates = Vec::new();
        let mut code_updates = Vec::new();
        let state_root = state_trie.hash_no_commit(&NativeCrypto);
        for update in account_updates {
            let hashed_address = hash_address_fixed(&update.address);
            if update.removed {
                // Remove account from trie
                state_trie.remove(hashed_address.as_bytes())?;
                continue;
            }
            // Add or update AccountState in the trie
            // Fetch current state or create a new state to be inserted
            let mut account_state = match state_trie.get(hashed_address.as_bytes())? {
                Some(encoded_state) => AccountState::decode(&encoded_state)?,
                None => AccountState::default(),
            };
            if update.removed_storage {
                account_state.storage_root = EMPTY_TRIE_HASH;
            }
            if let Some(info) = &update.info {
                account_state.nonce = info.nonce;
                account_state.balance = info.balance;
                account_state.code_hash = info.code_hash;
                // Store updated code in DB
                if let Some(code) = &update.code {
                    code_updates.push((info.code_hash, code.clone()));
                }
            }
            // Store the added storage in the account's storage trie and compute its new root
            if !update.added_storage.is_empty() {
                let mut storage_trie =
                    self.open_storage_trie(hashed_address, state_root, account_state.storage_root)?;
                for (storage_key, storage_value) in &update.added_storage {
                    let hashed_key = hash_key(storage_key);
                    if storage_value.is_zero() {
                        storage_trie.remove(&hashed_key)?;
                    } else {
                        storage_trie.insert(hashed_key, storage_value.encode_to_vec())?;
                    }
                }
                let (storage_hash, storage_updates) =
                    storage_trie.collect_changes_since_last_hash(&NativeCrypto);
                account_state.storage_root = storage_hash;
                ret_storage_updates.push((hashed_address, storage_updates));
            }
            state_trie.insert(
                hashed_address.as_bytes().to_vec(),
                account_state.encode_to_vec(),
            )?;
        }
        let (state_trie_hash, state_updates) =
            state_trie.collect_changes_since_last_hash(&NativeCrypto);

        Ok(AccountUpdatesList {
            state_trie_hash,
            state_updates,
            storage_updates: ret_storage_updates,
            code_updates,
        })
    }

    /// Performs the same actions as apply_account_updates_from_trie
    ///  but also returns the used storage tries with witness recorded
    pub fn apply_account_updates_from_trie_with_witness(
        &self,
        mut state_trie: Trie,
        account_updates: &[AccountUpdate],
        mut storage_tries: StorageTries,
    ) -> Result<(StorageTries, AccountUpdatesList), StoreError> {
        let mut ret_storage_updates = Vec::new();

        let mut code_updates = Vec::new();

        let state_root = state_trie.hash_no_commit(&NativeCrypto);

        // Apply in address order. The guest replays these same updates on the nodes recorded
        // here, and the nodes a removal reads depend on what was inserted before it: a branch
        // left with one child loads that child. Both lists are built from seeded maps, so
        // only a sort keeps the two sides touching the same nodes.
        let mut ordered: Vec<&AccountUpdate> = account_updates.iter().collect();
        ordered.sort_unstable_by_key(|update| update.address);

        for update in ordered {
            let hashed_address = hash_address(&update.address);

            if update.removed {
                // Remove account from trie
                state_trie.remove(&hashed_address)?;

                continue;
            }

            // Add or update AccountState in the trie
            // Fetch current state or create a new state to be inserted
            let mut account_state = match state_trie.get(&hashed_address)? {
                Some(encoded_state) => AccountState::decode(&encoded_state)?,
                None => AccountState::default(),
            };

            if update.removed_storage {
                account_state.storage_root = EMPTY_TRIE_HASH;
            }

            if let Some(info) = &update.info {
                account_state.nonce = info.nonce;

                account_state.balance = info.balance;

                account_state.code_hash = info.code_hash;

                // Store updated code in DB
                if let Some(code) = &update.code {
                    code_updates.push((info.code_hash, code.clone()));
                }
            }

            // Store the added storage in the account's storage trie and compute its new root
            if !update.added_storage.is_empty() {
                let (_witness, storage_trie) = match storage_tries.entry(update.address) {
                    Entry::Occupied(value) => value.into_mut(),
                    Entry::Vacant(vacant) => {
                        let trie = self.open_storage_trie(
                            H256::from_slice(&hashed_address),
                            state_root,
                            account_state.storage_root,
                        )?;
                        vacant.insert(TrieLogger::open_trie(trie))
                    }
                };

                for (storage_key, storage_value) in &update.added_storage {
                    let hashed_key = hash_key(storage_key);

                    if storage_value.is_zero() {
                        storage_trie.remove(&hashed_key)?;
                    } else {
                        storage_trie.insert(hashed_key, storage_value.encode_to_vec())?;
                    }
                }

                let (storage_hash, storage_updates) =
                    storage_trie.collect_changes_since_last_hash(&NativeCrypto);

                account_state.storage_root = storage_hash;

                ret_storage_updates.push((H256::from_slice(&hashed_address), storage_updates));
            }

            state_trie.insert(hashed_address, account_state.encode_to_vec())?;
        }

        let (state_trie_hash, state_updates) =
            state_trie.collect_changes_since_last_hash(&NativeCrypto);

        let account_updates_list = AccountUpdatesList {
            state_trie_hash,
            state_updates,
            storage_updates: ret_storage_updates,
            code_updates,
        };

        Ok((storage_tries, account_updates_list))
    }

    /// Adds all genesis accounts and returns the genesis block's state_root
    pub async fn setup_genesis_state_trie(
        &self,
        genesis_accounts: BTreeMap<Address, GenesisAccount>,
    ) -> Result<H256, StoreError> {
        let mut storage_trie_nodes = vec![];
        let mut genesis_state_trie = self.open_direct_state_trie(EMPTY_TRIE_HASH)?;
        for (address, account) in genesis_accounts {
            let hashed_address = hash_address(&address);
            let h256_hashed_address = H256::from_slice(&hashed_address);

            // Store account code (as this won't be stored in the trie)
            let code = Code::from_bytecode(account.code, &NativeCrypto);
            let code_hash = code.hash;
            self.add_account_code(code).await?;

            // Store the account's storage in a clean storage trie and compute its root
            let mut storage_trie =
                self.open_direct_storage_trie(h256_hashed_address, EMPTY_TRIE_HASH)?;
            for (storage_key, storage_value) in account.storage {
                if !storage_value.is_zero() {
                    let hashed_key = hash_key(&H256(storage_key.to_big_endian()));
                    storage_trie.insert(hashed_key, storage_value.encode_to_vec())?;
                }
            }

            let (storage_root, storage_nodes) =
                storage_trie.collect_changes_since_last_hash(&NativeCrypto);

            storage_trie_nodes.extend(
                storage_nodes
                    .into_iter()
                    .map(|(path, n)| (apply_prefix(Some(h256_hashed_address), path).into_vec(), n)),
            );

            // Add account to trie
            let account_state = AccountState {
                nonce: account.nonce,
                balance: account.balance,
                storage_root,
                code_hash,
            };
            genesis_state_trie.insert(hashed_address, account_state.encode_to_vec())?;
        }

        let (state_root, account_trie_nodes) =
            genesis_state_trie.collect_changes_since_last_hash(&NativeCrypto);
        let account_trie_nodes = account_trie_nodes
            .into_iter()
            .map(|(path, n)| (apply_prefix(None, path).into_vec(), n))
            .collect::<Vec<_>>();

        let mut tx = self.backend.begin_write()?;
        tx.put_batch(ACCOUNT_TRIE_NODES, account_trie_nodes)?;
        tx.put_batch(STORAGE_TRIE_NODES, storage_trie_nodes)?;
        tx.commit()?;

        Ok(state_root)
    }

    // Key format: block_number (8 bytes, big-endian) + block_hash (32 bytes)
    fn make_witness_key(block_number: u64, block_hash: &BlockHash) -> Vec<u8> {
        let mut composite_key = Vec::with_capacity(8 + 32);
        composite_key.extend_from_slice(&block_number.to_be_bytes());
        composite_key.extend_from_slice(block_hash.as_bytes());
        composite_key
    }

    /// Stores a pre-serialized execution witness for a block.
    ///
    /// The witness is converted to RPC format (RpcExecutionWitness) before storage
    /// to avoid expensive `encode_subtrie` traversal on every read. This pre-computes
    /// the serialization at write time instead of read time.
    pub fn store_witness(
        &self,
        block_hash: BlockHash,
        block_number: u64,
        witness: ExecutionWitness,
    ) -> Result<(), StoreError> {
        // Convert to RPC format once at storage time
        let rpc_witness = RpcExecutionWitness::try_from(witness)?;
        let key = Self::make_witness_key(block_number, &block_hash);
        let value = serde_json::to_vec(&rpc_witness)?;
        self.write(EXECUTION_WITNESSES, key, value)?;
        // Clean up old witnesses (keep only last 128)
        self.cleanup_old_witnesses(block_number)
    }

    fn cleanup_old_witnesses(&self, latest_block_number: u64) -> Result<(), StoreError> {
        // If we have less than 128 blocks, no cleanup needed
        if latest_block_number <= MAX_WITNESSES {
            return Ok(());
        }

        let threshold = latest_block_number - MAX_WITNESSES;

        if let Some(oldest_block_number) = self.get_oldest_witness_number()? {
            let prefix = oldest_block_number.to_be_bytes();
            let mut to_delete = Vec::new();

            {
                let read_txn = self.backend.begin_read()?;
                let iter = read_txn.prefix_iterator(EXECUTION_WITNESSES, &prefix)?;

                // We may have multiple witnesses for the same block number (forks)
                for item in iter {
                    let (key, _value) = item?;
                    let mut block_number_bytes = [0u8; 8];
                    block_number_bytes.copy_from_slice(&key[0..8]);
                    let block_number = u64::from_be_bytes(block_number_bytes);
                    if block_number > threshold {
                        break;
                    }
                    to_delete.push(key.to_vec());
                }
            }

            for key in to_delete {
                self.delete(EXECUTION_WITNESSES, key)?;
            }
        };

        self.update_oldest_witness_number(threshold + 1)?;

        Ok(())
    }

    fn update_oldest_witness_number(&self, oldest_block_number: u64) -> Result<(), StoreError> {
        self.write(
            MISC_VALUES,
            b"oldest_witness_block_number".to_vec(),
            oldest_block_number.to_le_bytes().to_vec(),
        )?;
        Ok(())
    }

    fn get_oldest_witness_number(&self) -> Result<Option<u64>, StoreError> {
        let Some(value) = self.read(MISC_VALUES, b"oldest_witness_block_number".to_vec())? else {
            return Ok(None);
        };

        let array: [u8; 8] = value.as_slice().try_into().map_err(|_| {
            StoreError::Custom("Invalid oldest witness block number bytes".to_string())
        })?;
        Ok(Some(u64::from_le_bytes(array)))
    }

    /// Returns the raw JSON bytes of a cached witness for a block.
    ///
    /// This is the most efficient method for the RPC handler since it avoids
    /// deserialization and re-serialization. The bytes can be parsed directly
    /// as a JSON Value for the RPC response.
    pub fn get_witness_json_bytes(
        &self,
        block_number: u64,
        block_hash: BlockHash,
    ) -> Result<Option<Vec<u8>>, StoreError> {
        let key = Self::make_witness_key(block_number, &block_hash);
        self.read(EXECUTION_WITNESSES, key)
    }

    /// Returns the deserialized RpcExecutionWitness for a block.
    ///
    /// Prefer `get_witness_json_bytes` when you need to return the witness
    /// as JSON (e.g., for RPC responses) to avoid re-serialization.
    pub fn get_witness_by_number_and_hash(
        &self,
        block_number: u64,
        block_hash: BlockHash,
    ) -> Result<Option<RpcExecutionWitness>, StoreError> {
        let key = Self::make_witness_key(block_number, &block_hash);
        match self.read(EXECUTION_WITNESSES, key)? {
            Some(value) => {
                let witness: RpcExecutionWitness = serde_json::from_slice(&value)?;
                Ok(Some(witness))
            }
            None => Ok(None),
        }
    }

    /// Stores a block access list for a given block hash.
    pub fn store_block_access_list(
        &self,
        block_hash: BlockHash,
        bal: &BlockAccessList,
    ) -> Result<(), StoreError> {
        let mut value = vec![];
        bal.encode(&mut value);
        self.store_block_access_list_encoded(block_hash, value)
    }

    /// Store a block access list the caller has already RLP-encoded, so a caller
    /// that also needs the encoded size does not encode it twice.
    pub fn store_block_access_list_encoded(
        &self,
        block_hash: BlockHash,
        encoded: Vec<u8>,
    ) -> Result<(), StoreError> {
        self.write(BLOCK_ACCESS_LISTS, block_hash.as_bytes().to_vec(), encoded)
    }

    /// Returns the block access list for a given block hash, if stored.
    pub fn get_block_access_list(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockAccessList>, StoreError> {
        let key = block_hash.as_bytes().to_vec();
        match self.read(BLOCK_ACCESS_LISTS, key)? {
            Some(value) => {
                let bal = BlockAccessList::decode(&value)
                    .map_err(|e| StoreError::Custom(format!("Failed to decode BAL: {e}")))?;
                Ok(Some(bal))
            }
            None => Ok(None),
        }
    }

    /// Fetches block access lists for a slice of block hashes, preserving order.
    ///
    /// Returns `None` at any position where the BAL is unavailable (unknown block,
    /// pre-Amsterdam block, or pruned data). Never errors for individual missing entries.
    pub fn iter_block_access_lists_by_hashes(
        &self,
        hashes: &[BlockHash],
    ) -> Result<Vec<Option<BlockAccessList>>, StoreError> {
        let mut out = Vec::with_capacity(hashes.len());
        for hash in hashes {
            out.push(self.get_block_access_list(*hash)?);
        }
        Ok(out)
    }

    pub async fn add_initial_state(&mut self, genesis: Genesis) -> Result<(), StoreError> {
        self.add_initial_state_inner(genesis, false).await
    }

    /// Like [`Store::add_initial_state`], but trusts a pre-existing datadir's
    /// state instead of validating it against the provided genesis. If a genesis
    /// header is already stored, it is kept as-is rather than recomputing the
    /// genesis state root from `genesis.alloc` and rejecting on mismatch. The
    /// chain config from the genesis file is still applied either way.
    ///
    /// Intended for booting a datadir produced out-of-band (e.g. by a state
    /// generator that writes the state trie directly and emits a genesis file
    /// with an empty `alloc`), where the operator vouches for the stored state
    /// root. Has no effect on a fresh datadir: the genesis is built normally.
    pub async fn add_initial_state_skip_validation(
        &mut self,
        genesis: Genesis,
    ) -> Result<(), StoreError> {
        self.add_initial_state_inner(genesis, true).await
    }

    async fn add_initial_state_inner(
        &mut self,
        genesis: Genesis,
        skip_genesis_validation: bool,
    ) -> Result<(), StoreError> {
        debug!("Storing initial state from genesis");

        // Obtain genesis block
        let genesis_block = genesis.get_block();
        let genesis_block_number = genesis_block.header.number;

        let genesis_hash = genesis_block.hash();

        let stored_genesis_header = self.load_block_header(genesis_block_number)?;

        // Always set the chain config from the genesis file. The in-memory
        // `chain_config` starts at `Default::default()` on every boot and is
        // not reloaded from the datadir, so skipping this would leave the store
        // with the wrong chainId and an empty fork schedule. Skip-validation
        // only waives the genesis state-root/header check; the `config` section
        // of the genesis file is still authoritative and must be applied.
        self.set_chain_config(&genesis.config).await?;

        // The cache can't be empty. Clamp the head to the durable block: after a
        // crash, `LatestBlockNumber` can be ahead of `flushed_upto` (FCU writes the
        // head synchronously while block bodies are buffered), so loading the raw
        // latest header would brick boot when its body was never flushed.
        if let Some(latest) = self.load_latest_block_number().await? {
            self.anchor_to_durable_head(latest).await?;
        }

        match stored_genesis_header {
            Some(header) if skip_genesis_validation => {
                info!(
                    stored_genesis = %header.hash(),
                    "Skipping genesis state validation; trusting the genesis header and state already stored in the datadir"
                );
                return Ok(());
            }
            Some(header) if header.hash() == genesis_hash => {
                info!("Received genesis file matching a previously stored one, nothing to do");
                return Ok(());
            }
            Some(_) => {
                error!(
                    "The chain configuration stored in the database is incompatible with the provided configuration. If you intended to switch networks, choose another datadir or clear the database (e.g., run `ethrex removedb`) and try again."
                );
                return Err(StoreError::IncompatibleChainConfig);
            }
            None => {
                self.add_block_header(genesis_hash, genesis_block.header.clone())
                    .await?
            }
        }
        // Store genesis accounts
        // TODO: Should we use this root instead of computing it before the block hash check?
        let genesis_state_root = self.setup_genesis_state_trie(genesis.alloc).await?;
        debug_assert_eq!(genesis_state_root, genesis_block.header.state_root);

        // Store genesis block
        info!(hash = %genesis_hash, "Storing genesis block");

        self.add_block(genesis_block).await?;
        self.update_earliest_block_number(genesis_block_number)
            .await?;
        self.forkchoice_update(vec![], genesis_block_number, genesis_hash, None, None)
            .await?;
        Ok(())
    }

    pub async fn load_initial_state(&self) -> Result<(), StoreError> {
        info!("Loading initial state from DB");
        let Some(latest) = self.load_latest_block_number().await? else {
            return Err(StoreError::MissingLatestBlockNumber);
        };
        // Use the same durable-head clamp as the node boot path so export and the
        // running node agree on the head. The persisted head is only rewritten when
        // it actually moved, so a plain export run does not mutate `CHAIN_DATA`.
        self.anchor_to_durable_head(latest).await?;
        Ok(())
    }

    pub fn get_storage_at(
        &self,
        block_number: BlockNumber,
        address: Address,
        storage_key: H256,
    ) -> Result<Option<U256>, StoreError> {
        match self.get_block_header(block_number)? {
            Some(header) => self.get_storage_at_root(header.state_root, address, storage_key),
            None => Ok(None),
        }
    }

    pub fn get_storage_at_root(
        &self,
        state_root: H256,
        address: Address,
        storage_key: H256,
    ) -> Result<Option<U256>, StoreError> {
        let account_hash = hash_address_fixed(&address);

        // Pre-acquire shared resources once for both trie opens
        let read_view = self.backend.begin_read()?;
        let cache = self.gated_snapshot(state_root)?;
        let last_written = self.last_written()?;
        // While a deep-reorg overlay serves this root, flat-KV reads must
        // go through the trie: journal entries written while the FKV generator was
        // running lack pre-images for keys past the generator frontier, so disk
        // flat-KV may hold the generator's stale values. `TrieWrapper` already
        // forces the trie-node read path in this window; mirror the gate here so
        // the EMPTY_TRIE_HASH shortcut doesn't bypass it.
        let use_fkv = Self::flatkeyvalue_computed_with_last_written(account_hash, &last_written)
            && !cache.overlay_serves(state_root);

        let storage_root = if use_fkv {
            // We will use FKVs, we don't need the root
            EMPTY_TRIE_HASH
        } else {
            let state_trie = self.open_state_trie_shared(
                state_root,
                read_view.clone(),
                cache.clone(),
                last_written.clone(),
            )?;
            let Some(encoded_account) = state_trie.get(account_hash.as_bytes())? else {
                return Ok(None);
            };
            let account = AccountState::decode(&encoded_account)?;
            account.storage_root
        };
        let storage_trie = self.open_storage_trie_shared(
            account_hash,
            state_root,
            storage_root,
            read_view,
            cache,
            last_written,
        )?;

        let hashed_key = hash_key_fixed(&storage_key);
        storage_trie
            .get(&hashed_key)?
            .map(|rlp| U256::decode(&rlp).map_err(StoreError::RLPDecode))
            .transpose()
    }

    /// Gets storage value when the account hash and storage root are already known.
    ///
    /// This skips the state-trie account lookup and account RLP decode done by
    /// [`Self::get_storage_at_root`], and directly opens the account storage trie.
    pub fn get_storage_at_root_with_known_storage_root(
        &self,
        state_root: H256,
        account_hash: H256,
        storage_root: H256,
        storage_key: H256,
    ) -> Result<Option<U256>, StoreError> {
        let read_view = self.backend.begin_read()?;
        let cache = self.gated_snapshot(state_root)?;
        let last_written = self.last_written()?;
        // When FKV is active the real storage root is in the flatkeyvalue store,
        // not in the account's RLP-encoded storage_root field. Use EMPTY_TRIE_HASH
        // so open_storage_trie_shared falls through to the FKV path. While a
        // deep-reorg overlay serves this root, keep the real root instead:
        // disk flat-KV may hold stale generator values, so reads must go through
        // the trie (see `TrieWrapper::flatkeyvalue_computed`).
        let storage_root =
            if Self::flatkeyvalue_computed_with_last_written(account_hash, &last_written)
                && !cache.overlay_serves(state_root)
            {
                EMPTY_TRIE_HASH
            } else {
                storage_root
            };
        let storage_trie = self.open_storage_trie_shared(
            account_hash,
            state_root,
            storage_root,
            read_view,
            cache,
            last_written,
        )?;

        let hashed_key = hash_key_fixed(&storage_key);
        storage_trie
            .get(&hashed_key)?
            .map(|rlp| U256::decode(&rlp).map_err(StoreError::RLPDecode))
            .transpose()
    }

    pub fn get_chain_config(&self) -> ChainConfig {
        self.chain_config
    }

    pub async fn get_latest_canonical_block_hash(&self) -> Result<Option<BlockHash>, StoreError> {
        Ok(Some(self.latest_block_header.get().hash()))
    }

    /// Updates the canonical chain.
    /// Inserts new canonical blocks, removes blocks beyond the new head,
    /// and updates the head, safe, and finalized block pointers.
    /// All operations are performed in a single database transaction.
    pub async fn forkchoice_update(
        &self,
        new_canonical_blocks: Vec<(BlockNumber, BlockHash)>,
        head_number: BlockNumber,
        head_hash: BlockHash,
        safe: Option<BlockNumber>,
        finalized: Option<BlockNumber>,
    ) -> Result<(), StoreError> {
        // Serialize concurrent forkchoice updates. Without this, two callers
        // could interleave their `latest_block_header` cache updates with each
        // other's DB writes, leaving the cache inconsistent with the DB or
        // letting a later caller's write reorder relative to the cache update
        // order (see the TOCTOU discussion around canonical/latest drift).
        let _guard = self.fcu_lock.lock().await;

        // Updates first the latest_block_header to avoid nonce inconsistencies #3927.
        // Snapshot the previous header so we can roll the cache back if the DB
        // write fails — otherwise the cache would point at a block the DB does
        // not consider canonical.
        let previous_head = self.latest_block_header.get();
        let new_head = self
            .get_block_header_by_hash(head_hash)?
            .ok_or_else(|| StoreError::MissingLatestBlockNumber)?;
        self.latest_block_header.update(new_head);
        if let Err(err) = self
            .forkchoice_update_inner(
                new_canonical_blocks,
                head_number,
                head_hash,
                safe,
                finalized,
            )
            .await
        {
            self.latest_block_header.update((*previous_head).clone());
            return Err(err);
        }

        // Refresh the canonical safe-commit root now that the canonical tables reflect the new
        // head. `None` (chain shorter than the threshold, e.g. genesis init at head 0) leaves the
        // cell unchanged so genesis-on-disk is never gated away.
        // No `latest_block_header` rollback on error here (unlike the `inner` failure above): the
        // only error is a poisoned safe-commit `RwLock`, which is an unrecoverable process state.
        if let Some(root) = self.compute_safe_commit_root(head_number)? {
            // Advancing the cell alone does not flush: the commit step (Phase 2) only runs
            // while blocks execute, so an execute-all-then-one-forkchoice flow (e.g. block
            // import) would accumulate every layer and never persist. When the safe-commit
            // root advances, poke the worker to flush the now-committable backlog up to it.
            if self.set_safe_commit_root(root)? {
                let tx = self.persist_tx.clone();
                let _ = tokio::task::spawn_blocking(move || tx.send(PersistMessage::Commit(root)))
                    .await;
            }
        }

        Ok(())
    }

    /// Updates the dedicated safe-commit-root cell with the given state root,
    /// returning `true` if the cell changed (so callers can skip a redundant flush).
    ///
    /// This is a plain synchronous function; it touches only the dedicated cell
    /// and is disjoint from the trie-cache Arc (no cache clone or replacement).
    /// Crate-private: only `forkchoice_update` (post-canonicalization) may set it,
    /// preserving the invariant that the cell only ever holds a canonical state root.
    pub(crate) fn set_safe_commit_root(&self, root: H256) -> Result<bool, StoreError> {
        let mut guard = self
            .safe_commit_root
            .write()
            .map_err(|_| StoreError::LockError)?;
        let changed = *guard != root;
        *guard = root;
        Ok(changed)
    }

    /// Computes the canonical safe-commit state root: the state root of the canonical block
    /// `commit_threshold` layers below `head`.
    ///
    /// Returns `Ok(None)` when the chain is shorter than the threshold (underflow), or when the
    /// target block is not yet canonical / its header is absent. Synchronous getters only; no
    /// await and no lock guard held across one. The threshold is read from the trie cache to
    /// avoid duplicating the IN_MEMORY/DB selection that `from_backend` already made.
    /// Crate-private: only `forkchoice_update` consumes it.
    pub(crate) fn compute_safe_commit_root(
        &self,
        head: BlockNumber,
    ) -> Result<Option<H256>, StoreError> {
        let commit_threshold = self
            .trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .commit_threshold;
        let Some(target) = head.checked_sub(commit_threshold as u64) else {
            return Ok(None);
        };
        let Some(hash) = self.get_canonical_block_hash_sync(target)? else {
            return Ok(None);
        };
        let Some(header) = self.get_block_header_by_hash(hash)? else {
            return Ok(None);
        };
        Ok(Some(header.state_root))
    }

    /// Obtain the storage trie for the given block
    pub fn state_trie(&self, block_hash: BlockHash) -> Result<Option<Trie>, StoreError> {
        let Some(header) = self.get_block_header_by_hash(block_hash)? else {
            return Ok(None);
        };
        Ok(Some(self.open_state_trie(header.state_root)?))
    }

    /// Obtain the storage trie for the given account on the given block
    pub fn storage_trie(
        &self,
        block_hash: BlockHash,
        address: Address,
    ) -> Result<Option<Trie>, StoreError> {
        let Some(header) = self.get_block_header_by_hash(block_hash)? else {
            return Ok(None);
        };
        // Fetch Account from state_trie
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        let hashed_address = hash_address_fixed(&address);
        let Some(encoded_account) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        let account = AccountState::decode(&encoded_account)?;
        // Open storage_trie
        let storage_root = account.storage_root;
        Ok(Some(self.open_storage_trie(
            hashed_address,
            header.state_root,
            storage_root,
        )?))
    }

    pub async fn get_account_state(
        &self,
        block_number: BlockNumber,
        address: Address,
    ) -> Result<Option<AccountState>, StoreError> {
        let Some(block_hash) = self.get_canonical_block_hash(block_number).await? else {
            return Ok(None);
        };
        let Some(state_trie) = self.state_trie(block_hash)? else {
            return Ok(None);
        };
        self.get_account_state_from_trie(&state_trie, address)
    }

    pub fn get_account_state_by_root(
        &self,
        state_root: H256,
        address: Address,
    ) -> Result<Option<AccountState>, StoreError> {
        let state_trie = self.open_state_trie(state_root)?;
        self.get_account_state_from_trie(&state_trie, address)
    }

    pub fn get_account_state_from_trie(
        &self,
        state_trie: &Trie,
        address: Address,
    ) -> Result<Option<AccountState>, StoreError> {
        let hashed_address = hash_address_fixed(&address);
        let Some(encoded_state) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        Ok(Some(AccountState::decode(&encoded_state)?))
    }

    /// Batch lookup of account states by address against a given state root.
    ///
    /// Fast path: for addresses whose hashed path falls within the FKV cursor
    /// (and which are not present in the in-memory diff-layer cache), values
    /// are fetched in a single `multi_get` on `ACCOUNT_FLATKEYVALUE`. Other
    /// addresses fall back to per-address trie walks.
    ///
    /// Results are returned in the same order as the input addresses.
    pub fn get_account_states_batch_by_root(
        &self,
        state_root: H256,
        addresses: &[Address],
    ) -> Result<Vec<Option<AccountState>>, StoreError> {
        if addresses.is_empty() {
            return Ok(Vec::new());
        }

        let last_written = self.last_written()?;
        let trie_cache = self
            .trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone();

        let mut results: Vec<Option<AccountState>> = vec![None; addresses.len()];
        // Per-address leaf paths (nibbles + leaf flag). Length 65.
        let leaf_paths: Vec<Vec<u8>> = addresses
            .iter()
            .map(|addr| {
                let hashed = hash_address_fixed(addr);
                Nibbles::from_bytes(hashed.as_bytes()).into_vec()
            })
            .collect();

        let mut fkv_indices: Vec<usize> = Vec::new();
        let mut trie_indices: Vec<usize> = Vec::new();

        // Match `BackendTrieDB::flatkeyvalue_computed` semantics: a path is
        // covered by FKV iff `last_written >= path` as raw nibble bytes. This
        // is the same check `Trie::get` uses; the related helper
        // `Store::flatkeyvalue_computed_with_last_written` slices `[0..64]`
        // and is intentionally more conservative — using that here would
        // unnecessarily fall back to the trie when the cursor sits inside an
        // account's storage sweep (the account leaf is already in FKV at that
        // point; see `flatkeyvalue_generator`).
        //
        // While a deep-reorg overlay serves this root, skip the FKV fast
        // path entirely: this function never consults the overlay, and disk
        // flat-KV may hold values the generator computed against the chain being
        // reorged away (journal entries written during generation lack
        // past-frontier flat-KV pre-images). The per-address trie fallback goes
        // through `TrieWrapper`, which routes reads through the overlay.
        let overlay_active = trie_cache.overlay_serves(state_root);
        let fkv_cursor: &[u8] = last_written.as_slice();
        for (i, path) in leaf_paths.iter().enumerate() {
            if let Some(value) = trie_cache.get(state_root, path.as_slice()) {
                if !value.is_empty() {
                    results[i] = Some(AccountState::decode(&value)?);
                }
                continue;
            }
            if !overlay_active && fkv_cursor >= path.as_slice() {
                fkv_indices.push(i);
            } else {
                trie_indices.push(i);
            }
        }

        if !fkv_indices.is_empty() {
            // Sort by the prefixed (hashed-order) key so adjacent accounts land
            // in the same RocksDB data blocks, then read the sorted keys in
            // contiguous shards concurrently. A single `multi_get` runs the whole
            // batch at queue depth 1 (async_io is off), which on a cold account-
            // heavy block serializes thousands of random reads (coldbench: ~13x
            // slower than this sharded path). Mirrors the storage FKV batch.
            fkv_indices.sort_unstable_by(|&a, &b| leaf_paths[a].cmp(&leaf_paths[b]));
            let read_view = self.backend.begin_read()?;
            let keys: Vec<&[u8]> = fkv_indices
                .iter()
                .map(|&i| leaf_paths[i].as_slice())
                .collect();
            let shards = shard_count(keys.len());
            let raw: Vec<Result<Option<Vec<u8>>, StoreError>> = if shards <= 1 {
                read_view.multi_get(ACCOUNT_FLATKEYVALUE, &keys)
            } else {
                let chunk = keys.len().div_ceil(shards);
                let rv = read_view.as_ref();
                std::thread::scope(|scope| {
                    let handles: Vec<_> = keys
                        .chunks(chunk)
                        .map(|ck| scope.spawn(move || rv.multi_get(ACCOUNT_FLATKEYVALUE, ck)))
                        .collect();
                    handles
                        .into_iter()
                        // A panicked shard yields one Err element rather than
                        // re-panicking the whole node; the consumer's `?` below
                        // surfaces it to the caller. Every caller of this batch
                        // treats a failed warm as best-effort, so the keys are
                        // simply left uncached and read normally later.
                        .flat_map(|h| {
                            h.join().unwrap_or_else(|_| {
                                vec![Err(StoreError::Custom(
                                    "account prefetch shard panicked".into(),
                                ))]
                            })
                        })
                        .collect()
                })
            };
            for (slot, res) in fkv_indices.iter().zip(raw.into_iter()) {
                let Some(encoded) = res? else { continue };
                if encoded.is_empty() {
                    continue;
                }
                results[*slot] = Some(AccountState::decode(&encoded)?);
            }
        }

        if !trie_indices.is_empty() {
            // Fall back to the regular trie path for any addresses whose path
            // hasn't been swept by the FKV generator yet. Parallelized to
            // recover the per-address fan-out the pre-batch `par_iter` path
            // had, which matters during initial sync when most addresses
            // miss FKV.
            let state_trie = self.open_state_trie(state_root)?;
            let fetched: Result<Vec<(usize, Option<AccountState>)>, StoreError> = trie_indices
                .par_iter()
                .map(|&i| {
                    self.get_account_state_from_trie(&state_trie, addresses[i])
                        .map(|s| (i, s))
                })
                .collect();
            for (i, s) in fetched? {
                results[i] = s;
            }
        }

        Ok(results)
    }

    /// Batch lookup of storage slot values against a given state root.
    ///
    /// Each input tuple is `(account_hash, storage_root, slot)` where
    /// `account_hash` is the keccak hash of the account address and `slot` is
    /// the raw (un-hashed) storage key, matching the inputs available to
    /// [`Self::get_storage_at_root_with_known_storage_root`].
    ///
    /// Fast path: for slots whose prefixed FKV leaf path falls within the FKV
    /// cursor (and which are not present in the in-memory diff-layer cache),
    /// values are fetched in a single `multi_get` on `STORAGE_FLATKEYVALUE`.
    /// The batch is sorted by the prefixed FKV key (hashed order) before the
    /// `multi_get` so that adjacent slots share RocksDB data blocks, removing
    /// the random-order read amplification of per-slot lookups. Slots not yet
    /// swept by the FKV generator fall back to per-slot trie walks via
    /// [`Self::get_storage_at_root_with_known_storage_root`], which preserves the
    /// exact FKV / trie / diff-cache ordering of the single-slot path.
    ///
    /// Results are returned in the same order as the input slots. A missing slot
    /// (deleted or never written) yields `None`, identical to the single-get path.
    pub fn get_storage_values_batch_by_root(
        &self,
        state_root: H256,
        slots: &[(H256, H256, H256)],
    ) -> Result<Vec<Option<U256>>, StoreError> {
        if slots.is_empty() {
            return Ok(Vec::new());
        }

        let last_written = self.last_written()?;
        let trie_cache = self
            .trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone();

        let mut results: Vec<Option<U256>> = vec![None; slots.len()];
        // Per-slot prefixed FKV leaf paths, length 131: 64 account-hash nibbles
        // + 1 (account leaf flag) + 1 (separator) + 64 hashed-slot nibbles + 1
        // (storage leaf flag). This is the exact key
        // `BackendTrieDB::flatkeyvalue_computed` / `Trie::get` use for a storage
        // leaf (see `apply_prefix`).
        let leaf_paths: Vec<Vec<u8>> = slots
            .iter()
            .map(|(account_hash, _storage_root, slot)| {
                let hashed_slot = hash_key_fixed(slot);
                apply_prefix(Some(*account_hash), Nibbles::from_bytes(&hashed_slot)).into_vec()
            })
            .collect();

        let mut fkv_indices: Vec<usize> = Vec::new();
        let mut trie_indices: Vec<usize> = Vec::new();

        // Same watermark check as the account batch and `BackendTrieDB`: a path
        // is covered by FKV iff `last_written >= prefixed_path` compared as raw
        // nibble bytes. `Nibbles` orders by its inner `data` vec, so the raw
        // slice comparison is byte-identical to the `Nibbles >= Nibbles` check
        // in `BackendTrieDB::flatkeyvalue_computed`.
        //
        // While a deep-reorg overlay serves this root, skip the FKV fast path
        // entirely: this function never consults the overlay, and disk flat-KV may
        // hold values the generator computed against the chain being reorged away
        // (journal entries written during generation lack past-frontier flat-KV
        // pre-images). The per-slot trie fallback reads through `TrieWrapper`, which
        // routes through the overlay. Mirrors the gate in
        // `get_storage_at_root` and `get_account_states_batch_by_root`.
        let overlay_active = trie_cache.overlay_serves(state_root);
        let fkv_cursor: &[u8] = last_written.as_slice();
        for (i, path) in leaf_paths.iter().enumerate() {
            // Diff-layer cache holds the authoritative latest value for this
            // key at this state root, overriding both FKV and trie. Mirror the
            // cache-first semantics of `TrieWrapper::get` / the account batch:
            // an empty cached value means the slot was deleted -> None.
            if let Some(value) = trie_cache.get(state_root, path.as_slice()) {
                if !value.is_empty() {
                    results[i] = Some(U256::decode(&value).map_err(StoreError::RLPDecode)?);
                }
                continue;
            }
            if !overlay_active && fkv_cursor >= path.as_slice() {
                fkv_indices.push(i);
            } else {
                trie_indices.push(i);
            }
        }

        if !fkv_indices.is_empty() {
            // Sort the FKV batch by the prefixed (hashed-order) key so adjacent
            // slots land in the same 4KB RocksDB data blocks. This is the whole
            // point of the batch: it removes the random-order amplification of
            // issuing one point lookup per slot in execution order.
            fkv_indices.sort_unstable_by(|&a, &b| leaf_paths[a].cmp(&leaf_paths[b]));
            let read_view = self.backend.begin_read()?;
            let keys: Vec<&[u8]> = fkv_indices
                .iter()
                .map(|&i| leaf_paths[i].as_slice())
                .collect();
            // Sharded parallel multi_get. A single `multi_get` runs the whole
            // batch serially inside RocksDB (async_io is OFF in our build, so
            // no io_uring => queue depth ~= 1), which on a cold NVMe is purely
            // read-latency-bound: it touches few blocks (sorted) but serializes
            // them. Splitting the SORTED keys into contiguous shards read by
            // separate blocking threads keeps the per-shard block sharing while
            // issuing the shards concurrently, so effective queue depth ~= shard
            // count. On a bloated-account cold read this is ~4.7x a single
            // multi_get and ~4.6x the per-slot `par_iter` baseline (coldbench).
            // Shard count scales with batch size and is capped; small batches
            // run a single in-line multi_get (no thread spawn). All shards share
            // one read view (one snapshot) so the reads stay consistent.
            let shards = shard_count(keys.len());
            let raw: Vec<Result<Option<Vec<u8>>, StoreError>> = if shards <= 1 {
                read_view.multi_get(STORAGE_FLATKEYVALUE, &keys)
            } else {
                let chunk = keys.len().div_ceil(shards);
                let rv = read_view.as_ref();
                std::thread::scope(|scope| {
                    let handles: Vec<_> = keys
                        .chunks(chunk)
                        .map(|ck| scope.spawn(move || rv.multi_get(STORAGE_FLATKEYVALUE, ck)))
                        .collect();
                    handles
                        .into_iter()
                        // A panicked shard yields one Err element rather than
                        // re-panicking the whole node; the consumer's `?` below
                        // surfaces it to the caller. Every caller of this batch
                        // treats a failed warm as best-effort, so the keys are
                        // simply left uncached and read normally later.
                        .flat_map(|h| {
                            h.join().unwrap_or_else(|_| {
                                vec![Err(StoreError::Custom(
                                    "storage prefetch shard panicked".into(),
                                ))]
                            })
                        })
                        .collect()
                })
            };
            for (slot_idx, res) in fkv_indices.iter().zip(raw.into_iter()) {
                let Some(encoded) = res? else { continue };
                if encoded.is_empty() {
                    continue;
                }
                results[*slot_idx] = Some(U256::decode(&encoded).map_err(StoreError::RLPDecode)?);
            }
        }

        if !trie_indices.is_empty() {
            // Fall back to the regular single-slot path for any slot whose path
            // hasn't been swept by the FKV generator yet. Reusing
            // `get_storage_at_root_with_known_storage_root` guarantees the
            // trie-walk fallback is byte-identical to the unbatched read,
            // including its own per-slot FKV / EMPTY_TRIE_HASH handling.
            // Parallelized to recover the per-slot fan-out the pre-batch
            // `par_iter` prefetch path had.
            let fetched: Result<Vec<(usize, Option<U256>)>, StoreError> = trie_indices
                .par_iter()
                .map(|&i| {
                    let (account_hash, storage_root, slot) = slots[i];
                    self.get_storage_at_root_with_known_storage_root(
                        state_root,
                        account_hash,
                        storage_root,
                        slot,
                    )
                    .map(|v| (i, v))
                })
                .collect();
            for (i, v) in fetched? {
                results[i] = v;
            }
        }

        Ok(results)
    }

    /// Best-effort warming of the MPT *internal* nodes the merkleizer will walk,
    /// driven entirely by the BAL working set. The merkle phase reads trie nodes
    /// from `STORAGE_TRIE_NODES` / `ACCOUNT_TRIE_NODES` (see `BackendTrieDB::get`)
    /// by nibble path; the existing flat-KV prefetch warms only the leaf-value
    /// CFs that *execution* reads, so every merkle-walk node read is otherwise
    /// cold. This pulls those node blocks into the page/block cache up front so
    /// the subsequent walk runs warm.
    ///
    /// The keys are not discovered by walking (a walk is QD1 and serial): the
    /// internal nodes on the path to a leaf are all at *prefixes* of that leaf's
    /// nibble path, so for every BAL leaf we speculatively probe every prefix up
    /// to `MAX_PREFETCH_DEPTH`. Prefixes that are not real nodes are rejected by
    /// the CF bloom filter in RAM (no disk read; measured ~free), and the shallow
    /// prefixes collapse on dedup. This turns the cold merkle node reads into one
    /// sorted, sharded, high-queue-depth `multi_get` per CF — the same lever that
    /// fixed the flat-KV `sload` path, applied to the trie-node CFs.
    ///
    /// Results are discarded. Missing a node (e.g. one deeper than the probe
    /// band) is harmless: the merkleizer just cold-reads it. Run this
    /// synchronously up front (mirroring the flat-KV prefetch) so it never races
    /// the merkleizer; it touches different CFs than execution, so it does not
    /// contend with the executor either.
    /// Returns the number of probed keys that hit a real stored node (warmed).
    /// On the warm/no-op path this still reports coverage; a near-zero hit count
    /// against a non-empty working set signals a key-encoding mismatch (the
    /// effectiveness test asserts on this).
    pub fn prefetch_trie_nodes(
        &self,
        storage_slots: &[(Address, H256)],
        accounts: &[Address],
    ) -> Result<usize, StoreError> {
        // Probe band: nodes live at depth <= ~ceil(log16(trie_size)). 8 nibbles
        // covers tries up to ~16^8 entries near-fully; any deeper node is simply
        // cold-read by the walk, so under-covering only loses a little warming,
        // never correctness. Tunable.
        const MAX_PREFETCH_DEPTH: usize = 8;

        // STORAGE_TRIE_NODES: prefixes of apply_prefix(hashed_addr, hashed_slot).
        // Each input pushes MAX_PREFETCH_DEPTH+1 prefixes (depths 0..=max_d).
        let mut storage_keys: Vec<Vec<u8>> =
            Vec::with_capacity(storage_slots.len() * (MAX_PREFETCH_DEPTH + 1));
        for (address, slot) in storage_slots {
            let hashed_address = hash_address_fixed(address);
            let hashed_slot = hash_key_fixed(slot);
            let slot_nibbles = Nibbles::from_bytes(&hashed_slot);
            let max_d = MAX_PREFETCH_DEPTH.min(slot_nibbles.len());
            for d in 0..=max_d {
                let prefix = apply_prefix(Some(hashed_address), slot_nibbles.slice(0, d));
                storage_keys.push(prefix.into_vec());
            }
        }

        // ACCOUNT_TRIE_NODES: prefixes of the hashed address (no account prefix).
        let mut account_keys: Vec<Vec<u8>> =
            Vec::with_capacity(accounts.len() * (MAX_PREFETCH_DEPTH + 1));
        for address in accounts {
            let hashed_address = hash_address_fixed(address);
            let addr_nibbles = Nibbles::from_bytes(hashed_address.as_bytes());
            let max_d = MAX_PREFETCH_DEPTH.min(addr_nibbles.len());
            for d in 0..=max_d {
                account_keys.push(addr_nibbles.slice(0, d).into_vec());
            }
        }

        let hits = self.warm_trie_node_keys(STORAGE_TRIE_NODES, storage_keys)?
            + self.warm_trie_node_keys(ACCOUNT_TRIE_NODES, account_keys)?;
        Ok(hits)
    }

    /// Sorted, sharded, high-queue-depth `multi_get` over `keys` in `table`,
    /// discarding results. Sorting makes adjacent keys share RocksDB data blocks;
    /// sharding across blocking threads issues the reads at queue depth ~= shard
    /// count (async_io is off, so a single multi_get runs at QD1). Mirrors the
    /// storage flat-KV batch path. Dedups first so shared shallow prefixes are
    /// read once.
    fn warm_trie_node_keys(
        &self,
        table: &'static str,
        mut keys: Vec<Vec<u8>>,
    ) -> Result<usize, StoreError> {
        if keys.is_empty() {
            return Ok(0);
        }
        keys.sort_unstable();
        keys.dedup();

        let shards = shard_count(keys.len());
        let read_view = self.backend.begin_read()?;
        let count_hits = |res: Vec<Result<Option<Vec<u8>>, StoreError>>| {
            res.into_iter().filter(|r| matches!(r, Ok(Some(_)))).count()
        };
        if shards <= 1 {
            let refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
            return Ok(count_hits(read_view.multi_get(table, &refs)));
        }
        let chunk = keys.len().div_ceil(shards);
        let rv = read_view.as_ref();
        let hits = std::thread::scope(|scope| {
            let handles: Vec<_> = keys
                .chunks(chunk)
                .map(|ck| {
                    scope.spawn(move || {
                        let refs: Vec<&[u8]> = ck.iter().map(|k| k.as_slice()).collect();
                        // Warm-only: keep cache population, just tally the hits.
                        count_hits(rv.multi_get(table, &refs))
                    })
                })
                .collect();
            handles
                .into_iter()
                // Best-effort warming: a panicked shard contributes 0 hits rather
                // than re-panicking. The caller treats this prefetch as best-effort
                // (missing nodes are just cold-read by the walk), so a shard hiccup
                // must not unwind block processing.
                .map(|h| h.join().unwrap_or(0))
                .sum::<usize>()
        });
        Ok(hits)
    }

    /// Constructs a merkle proof for the given account address against a given state.
    /// If storage_keys are provided, also constructs the storage proofs for those keys.
    ///
    /// Returns `None` if the state trie is missing, otherwise returns the proof.
    pub async fn get_account_proof(
        &self,
        state_root: H256,
        address: Address,
        storage_keys: &[H256],
    ) -> Result<Option<AccountProof>, StoreError> {
        // TODO: check state root
        // let Some(state_trie) = self.open_state_trie(state_trie)? else {
        //     return Ok(None);
        // };
        let state_trie = self.open_state_trie(state_root)?;
        let address_path = hash_address_fixed(&address);
        let proof = state_trie.get_proof(address_path.as_bytes())?;
        let account_opt = state_trie
            .get(address_path.as_bytes())?
            .map(|encoded_state| AccountState::decode(&encoded_state))
            .transpose()?;

        let mut storage_proof = Vec::with_capacity(storage_keys.len());

        if let Some(account) = &account_opt {
            let storage_trie =
                self.open_storage_trie(address_path, state_root, account.storage_root)?;

            for key in storage_keys {
                let hashed_key = hash_key(key);
                let proof = storage_trie.get_proof(&hashed_key)?;
                let value = storage_trie
                    .get(&hashed_key)?
                    .map(|rlp| U256::decode(&rlp).map_err(StoreError::RLPDecode))
                    .transpose()?
                    .unwrap_or_default();

                let slot_proof = StorageSlotProof {
                    proof,
                    key: *key,
                    value,
                };
                storage_proof.push(slot_proof);
            }
        } else {
            storage_proof.extend(storage_keys.iter().map(|key| StorageSlotProof {
                proof: Vec::new(),
                key: *key,
                value: U256::zero(),
            }));
        }
        let account = account_opt.unwrap_or_default();
        let account_proof = AccountProof {
            proof,
            account,
            storage_proof,
        };
        Ok(Some(account_proof))
    }

    // Returns an iterator across all accounts in the state trie given by the state_root
    // Does not check that the state_root is valid
    pub fn iter_accounts_from(
        &self,
        state_root: H256,
        starting_address: H256,
    ) -> Result<impl Iterator<Item = (H256, AccountState)>, StoreError> {
        let mut iter = self.open_locked_state_trie(state_root)?.into_iter();
        iter.advance(starting_address.0.to_vec())?;
        Ok(iter.content().map_while(|(path, value)| {
            Some((H256::from_slice(&path), AccountState::decode(&value).ok()?))
        }))
    }

    // Returns an iterator across all accounts in the state trie given by the state_root
    // Does not check that the state_root is valid
    pub fn iter_accounts(
        &self,
        state_root: H256,
    ) -> Result<impl Iterator<Item = (H256, AccountState)>, StoreError> {
        self.iter_accounts_from(state_root, H256::zero())
    }

    // Returns an iterator across all accounts in the state trie given by the state_root
    // Does not check that the state_root is valid
    pub fn iter_storage_from(
        &self,
        state_root: H256,
        hashed_address: H256,
        starting_slot: H256,
    ) -> Result<Option<impl Iterator<Item = (H256, U256)>>, StoreError> {
        let state_trie = self.open_locked_state_trie(state_root)?;
        let Some(account_rlp) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        let storage_root = AccountState::decode(&account_rlp)?.storage_root;
        let mut iter = self
            .open_locked_storage_trie(hashed_address, state_root, storage_root)?
            .into_iter();
        iter.advance(starting_slot.0.to_vec())?;
        Ok(Some(iter.content().map_while(|(path, value)| {
            Some((H256::from_slice(&path), U256::decode(&value).ok()?))
        })))
    }

    // Returns an iterator across all accounts in the state trie given by the state_root
    // Does not check that the state_root is valid
    pub fn iter_storage(
        &self,
        state_root: H256,
        hashed_address: H256,
    ) -> Result<Option<impl Iterator<Item = (H256, U256)>>, StoreError> {
        self.iter_storage_from(state_root, hashed_address, H256::zero())
    }

    pub fn get_account_range_proof(
        &self,
        state_root: H256,
        starting_hash: H256,
        last_hash: Option<H256>,
    ) -> Result<Vec<Vec<u8>>, StoreError> {
        let state_trie = self.open_state_trie(state_root)?;
        let mut proof = state_trie.get_proof(starting_hash.as_bytes())?;
        if let Some(last_hash) = last_hash {
            proof.extend_from_slice(&state_trie.get_proof(last_hash.as_bytes())?);
        }
        Ok(proof)
    }

    pub fn get_storage_range_proof(
        &self,
        state_root: H256,
        hashed_address: H256,
        starting_hash: H256,
        last_hash: Option<H256>,
    ) -> Result<Option<Vec<Vec<u8>>>, StoreError> {
        let state_trie = self.open_state_trie(state_root)?;
        let Some(account_rlp) = state_trie.get(hashed_address.as_bytes())? else {
            return Ok(None);
        };
        let storage_root = AccountState::decode(&account_rlp)?.storage_root;
        let storage_trie = self.open_storage_trie(hashed_address, state_root, storage_root)?;
        let mut proof = storage_trie.get_proof(starting_hash.as_bytes())?;
        if let Some(last_hash) = last_hash {
            proof.extend_from_slice(&storage_trie.get_proof(last_hash.as_bytes())?);
        }
        Ok(Some(proof))
    }

    /// Receives the root of the state trie and a list of paths where the first path will correspond to a path in the state trie
    /// (aka a hashed account address) and the following paths will be paths in the account's storage trie (aka hashed storage keys)
    /// If only one hash (account) is received, then the state trie node containing the account will be returned.
    /// If more than one hash is received, then the storage trie nodes where each storage key is stored will be returned
    /// For more information check out snap capability message [`GetTrieNodes`](https://github.com/ethereum/devp2p/blob/master/caps/snap.md#gettrienodes-0x06)
    /// The paths can be either full paths (hash) or partial paths (compact-encoded nibbles), if a partial path is given for the account this method will not return storage nodes for it
    pub fn get_trie_nodes(
        &self,
        state_root: H256,
        paths: Vec<Vec<u8>>,
        byte_limit: u64,
    ) -> Result<Vec<Vec<u8>>, StoreError> {
        let Some(account_path) = paths.first() else {
            return Ok(vec![]);
        };
        let state_trie = self.open_state_trie(state_root)?;
        // State Trie Nodes Request
        if paths.len() == 1 {
            // Fetch state trie node
            let node = state_trie.get_node(account_path)?;
            return Ok(vec![node]);
        }
        // Storage Trie Nodes Request
        let Some(account_state) = state_trie
            .get(account_path)?
            .map(|ref rlp| AccountState::decode(rlp))
            .transpose()?
        else {
            return Ok(vec![]);
        };
        // We can't access the storage trie without the account's address hash
        let Ok(hashed_address) = account_path.clone().try_into().map(H256) else {
            return Ok(vec![]);
        };
        let storage_trie =
            self.open_storage_trie(hashed_address, state_root, account_state.storage_root)?;
        // Fetch storage trie nodes
        let mut nodes = vec![];
        let mut bytes_used = 0;
        // The number of sub-paths is bounded upstream by the snap server's per-request lookup
        // cap (`MAX_SERVE_LOOKUPS`), which truncates the pathset before this call; here the
        // byte budget only bounds response size.
        for path in paths.iter().skip(1) {
            if bytes_used >= byte_limit {
                break;
            }
            let node = storage_trie.get_node(path)?;
            bytes_used += node.len() as u64;
            nodes.push(node);
        }
        Ok(nodes)
    }

    // Methods exclusive for trie management during snap-syncing

    /// Snapshot the trie layer cache for reading at `state_root`, blocking until
    /// that root's diff layer has been installed if it is still in-flight (see
    /// [`PendingTrieRoots`]). This is the read barrier for deferred layer builds:
    /// taking it at trie-open time guarantees the snapshot contains the layer, so
    /// a just-added block's state is never read as stale. Roots that are not
    /// pending (already installed, historical/committed, genesis) never block.
    fn gated_snapshot(&self, state_root: H256) -> Result<Arc<TrieLayerCache>, StoreError> {
        self.pending_trie_roots.wait_until_ready(state_root)?;
        Ok(self
            .trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone())
    }

    /// Obtain a state trie from the given state root
    /// Doesn't check if the state root is valid
    /// Used for internal store operations
    pub fn open_state_trie(&self, state_root: H256) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            self.gated_snapshot(state_root)?,
            Box::new(BackendTrieDB::new_for_accounts(
                self.backend.clone(),
                self.last_written()?,
            )?),
            None,
        );
        Ok(Trie::open(Box::new(trie_db), state_root))
    }

    /// Obtain a state trie from the given state root
    /// Doesn't check if the state root is valid
    /// Used for internal store operations
    pub fn open_direct_state_trie(&self, state_root: H256) -> Result<Trie, StoreError> {
        Ok(Trie::open(
            Box::new(BackendTrieDB::new_for_accounts(
                self.backend.clone(),
                self.last_written()?,
            )?),
            state_root,
        ))
    }

    /// Obtain a state trie locked for reads from the given state root
    /// Doesn't check if the state root is valid
    /// Used for internal store operations
    pub fn open_locked_state_trie(&self, state_root: H256) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            self.gated_snapshot(state_root)?,
            Box::new(state_trie_locked_backend(
                self.backend.as_ref(),
                self.last_written()?,
            )?),
            None,
        );
        Ok(Trie::open(Box::new(trie_db), state_root))
    }

    /// Obtain a storage trie from the given address and storage_root.
    /// Doesn't check if the account is stored
    pub fn open_storage_trie(
        &self,
        account_hash: H256,
        state_root: H256,
        storage_root: H256,
    ) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            self.gated_snapshot(state_root)?,
            Box::new(BackendTrieDB::new_for_storages(
                self.backend.clone(),
                self.last_written()?,
            )?),
            Some(account_hash),
        );
        Ok(Trie::open(Box::new(trie_db), storage_root))
    }

    /// Open a state trie using pre-acquired shared resources.
    /// Avoids redundant RwLock acquisitions when multiple tries are opened
    /// in the same operation (e.g., state trie + storage trie in get_storage_at_root).
    fn open_state_trie_shared(
        &self,
        state_root: H256,
        read_view: Arc<dyn StorageReadView>,
        cache: Arc<TrieLayerCache>,
        last_written: Vec<u8>,
    ) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            cache,
            Box::new(BackendTrieDB::new_for_accounts_with_view(
                self.backend.clone(),
                read_view,
                last_written,
            )?),
            None,
        );
        Ok(Trie::open(Box::new(trie_db), state_root))
    }

    /// Open a storage trie using pre-acquired shared resources.
    fn open_storage_trie_shared(
        &self,
        account_hash: H256,
        state_root: H256,
        storage_root: H256,
        read_view: Arc<dyn StorageReadView>,
        cache: Arc<TrieLayerCache>,
        last_written: Vec<u8>,
    ) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            cache,
            Box::new(BackendTrieDB::new_for_storages_with_view(
                self.backend.clone(),
                read_view,
                last_written,
            )?),
            Some(account_hash),
        );
        Ok(Trie::open(Box::new(trie_db), storage_root))
    }

    /// Obtain a storage trie from the given address and storage_root.
    /// Doesn't check if the account is stored
    pub fn open_direct_storage_trie(
        &self,
        account_hash: H256,
        storage_root: H256,
    ) -> Result<Trie, StoreError> {
        Ok(Trie::open(
            Box::new(BackendTrieDB::new_for_account_storage(
                self.backend.clone(),
                account_hash,
                self.last_written()?,
            )?),
            storage_root,
        ))
    }

    /// Obtain a read-locked storage trie from the given address and storage_root.
    /// Doesn't check if the account is stored
    pub fn open_locked_storage_trie(
        &self,
        account_hash: H256,
        state_root: H256,
        storage_root: H256,
    ) -> Result<Trie, StoreError> {
        let trie_db = TrieWrapper::new(
            state_root,
            self.gated_snapshot(state_root)?,
            Box::new(state_trie_locked_backend(
                self.backend.as_ref(),
                self.last_written()?,
            )?),
            Some(account_hash),
        );
        Ok(Trie::open(Box::new(trie_db), storage_root))
    }

    pub fn has_state_root(&self, state_root: H256) -> Result<bool, StoreError> {
        // Empty state trie is always available
        if state_root == EMPTY_TRIE_HASH {
            return Ok(true);
        }
        let trie = self.open_state_trie(state_root)?;
        // NOTE: here we hash the root because the trie doesn't check the state root is correct
        let Some(root) = trie.db().get(Nibbles::default())? else {
            return Ok(false);
        };
        let root_hash = ethrex_trie::Node::decode(&root)?
            .compute_hash(&NativeCrypto)
            .finalize(&NativeCrypto);
        Ok(state_root == root_hash)
    }

    // ===========================================================================
    // Deep-reorg primitives (storage side).
    // ===========================================================================

    /// Returns `true` if the in-memory layer cache currently has a layer with
    /// the given `state_root`. Used by the deep-reorg orchestrator to decide
    /// whether the head's state can be reached through forward execution (cache
    /// hit) or whether a deep-reorg path with overlay construction is required.
    pub fn is_state_in_layer_cache(&self, state_root: H256) -> Result<bool, StoreError> {
        let trie = self
            .trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone();
        Ok(trie.contains(state_root))
    }

    /// Returns the highest block number with a `STATE_HISTORY` entry; the cache
    /// edge `D` (the deepest block whose post-state is on disk). Returns `None`
    /// if the journal is empty (no commits since boot, fully pruned by finality, or
    /// every layer committed since was already final; see `journal_skip_ceiling`).
    ///
    /// O(1) via reverse seek on the column family's last key.
    pub fn highest_state_history_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        let read = self.backend.begin_read()?;
        let Some(key) = read.last_key(STATE_HISTORY)? else {
            return Ok(None);
        };
        let arr = <[u8; 8]>::try_from(key.as_slice()).map_err(|_| {
            StoreError::Custom(format!(
                "STATE_HISTORY key has unexpected length: {}",
                key.len()
            ))
        })?;
        Ok(Some(BlockNumber::from_be_bytes(arr)))
    }

    /// Returns the hash of the block whose trie-layer commit the `STATE_HISTORY`
    /// entry at `block_number` journals, or `None` when there is no entry.
    ///
    /// While the journal is non-empty the persist worker stages one entry per
    /// committed layer (see `journal_skip_ceiling`), so the entry at
    /// [`Self::highest_state_history_block_number`] identifies the block whose
    /// post-state is the one on disk.
    pub fn get_state_history_block_hash(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHash>, StoreError> {
        let read = self.backend.begin_read()?;
        let Some(bytes) = read.get(STATE_HISTORY, &block_number.to_be_bytes())? else {
            return Ok(None);
        };
        JournalEntry::decode_block_hash(&bytes)
            .map(Some)
            .map_err(|e| StoreError::Custom(format!("STATE_HISTORY entry {block_number}: {e}")))
    }

    /// Returns the lowest block number with a `STATE_HISTORY` entry. Returns `None`
    /// if the journal is empty (no commits since boot, or fully pruned by finality).
    ///
    /// O(1) via forward seek on the column family's first key.
    pub fn lowest_state_history_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        let read = self.backend.begin_read()?;
        let Some(key) = read.first_key(STATE_HISTORY)? else {
            return Ok(None);
        };
        let arr = <[u8; 8]>::try_from(key.as_slice()).map_err(|_| {
            StoreError::Custom(format!(
                "STATE_HISTORY key has unexpected length: {}",
                key.len()
            ))
        })?;
        Ok(Some(BlockNumber::from_be_bytes(arr)))
    }

    /// The in-memory diff-layer retention (the layer cache's commit threshold):
    /// the deepest reorg the node can serve straight from the layer cache, with no
    /// journal/overlay reconstruction. RocksDB default 128, in-memory 10000. Used by
    /// `compute_reorg_ceiling` as the physical floor when there is no finality signal.
    pub fn reorg_retention(&self) -> Result<u64, StoreError> {
        let cache = self.trie_cache.read().map_err(|_| StoreError::LockError)?;
        Ok(cache.commit_threshold() as u64)
    }

    /// Test-only: inserts a pre-encoded `STATE_HISTORY` entry at the given block
    /// number. Lets integration tests seed the journal without running enough
    /// commits to trip the in-memory cache's flush threshold.
    #[doc(hidden)]
    pub fn put_state_history_entry_for_test(
        &self,
        block_number: BlockNumber,
        encoded: &[u8],
    ) -> Result<(), StoreError> {
        let mut tx = self.backend.begin_write()?;
        tx.put(STATE_HISTORY, &block_number.to_be_bytes(), encoded)?;
        tx.commit()?;
        Ok(())
    }

    /// Atomically prepares the store for a deep-reorg apply pass.
    ///
    /// Builds an [`Overlay`] from journal entries in `[to_block, from_block]`
    /// (inclusive both ends), verifies each entry's `block_hash` against
    /// `expected_hash`, then swaps the in-memory layer cache for a fresh one
    /// with the overlay installed. After this call:
    ///
    /// - The layer cache contains zero forward layers.
    /// - The overlay is in place; subsequent `TrieWrapper::get` calls cascade
    ///   layer cache -> overlay -> disk.
    /// - The on-disk trie/flat-KV state is unchanged (still at the OLD chain's
    ///   edge `D`).
    ///
    /// Side-chain blocks `[pivot+1 .. new_head]` should now be executed in
    /// chain order through the normal `Blockchain::add_block` path; each
    /// block's reads cascade through the overlay, and each block's commit
    /// produces a new forward layer.
    ///
    /// Errors abort the swap: if overlay construction fails (missing entry,
    /// hash mismatch, decode error), the existing layer cache is left intact.
    pub fn install_overlay_for_reorg(
        &self,
        from_block: BlockNumber,
        to_block: BlockNumber,
        expected_hash: impl Fn(BlockNumber) -> Option<H256>,
    ) -> Result<(), StoreError> {
        // Build the overlay first so any failure aborts before mutating the cache.
        let overlay =
            Overlay::from_journal(self.backend.as_ref(), from_block, to_block, expected_hash)
                .map_err(|e| StoreError::Custom(format!("overlay construction failed: {e}")))?;

        let threshold = {
            let current = self.trie_cache.read().map_err(|_| StoreError::LockError)?;
            current.commit_threshold()
        };
        // Share the Store's safe-commit-root cell so the canonical commit gate keeps
        // working after the cache swap (the cell only advances via forkchoice).
        let mut fresh =
            TrieLayerCache::new_with_safe_commit(threshold, self.safe_commit_root.clone());
        fresh.set_overlay(Arc::new(overlay));

        // Wait for the persist worker to be idle before swapping the cache (see
        // [`Self::rendezvous_persist_worker`]).
        self.rendezvous_persist_worker("install_overlay_for_reorg")?;

        let mut guard = self.trie_cache.write().map_err(|_| StoreError::LockError)?;
        *guard = Arc::new(fresh);
        Ok(())
    }

    /// Waits for the persist worker to be idle before a layer-cache swap. That
    /// worker owns the trie-layer install (`apply_trie_phase1`, run from the
    /// `PersistMessage::Block` handler): it reads `trie_cache`, mutates a local
    /// clone, and RCU-writes it back; if it is mid-flight when we swap, its
    /// write-back can clobber the freshly swapped cache (e.g. drop a just-installed
    /// overlay, or install a side-chain layer over a base that no longer has the
    /// overlay underneath). `PersistMessage::Ping` carries an ack channel and the
    /// worker is FIFO, so its ack proves every earlier `Block` (and thus every
    /// earlier trie install) is fully processed and the worker is back at
    /// `rx.recv()`. This is the synchronous core of [`wait_for_persistence_idle`];
    /// we inline it here because callers are not async. The caller's subsequent
    /// `trie_cache.write()` serialising any future RCU makes the swap safe.
    fn rendezvous_persist_worker(&self, caller: &str) -> Result<(), StoreError> {
        let (ack_tx, ack_rx) = sync_channel::<Result<(), StoreError>>(1);
        self.persist_tx
            .send(PersistMessage::Ping(ack_tx))
            .map_err(|e| {
                StoreError::Custom(format!("{caller}: failed to ping persist worker: {e}"))
            })?;
        ack_rx.recv().map_err(|e| {
            StoreError::Custom(format!("{caller}: persist worker ping ack failed: {e}"))
        })??;
        Ok(())
    }

    /// Returns `(entry_count, byte_size)` of the currently installed overlay, or
    /// `(0, 0)` when no overlay is installed. Used by the observability layer in
    /// `fork_choice.rs` immediately after `install_overlay_for_reorg` to emit
    /// `ethrex_reorg_overlay_entries` / `ethrex_reorg_overlay_bytes`.
    pub fn reorg_overlay_size_hint(&self) -> Result<(usize, usize), StoreError> {
        let guard = self.trie_cache.read().map_err(|_| StoreError::LockError)?;
        match guard.overlay() {
            Some(ov) => Ok((ov.len(), ov.byte_size())),
            None => Ok((0, 0)),
        }
    }

    /// Pauses or resumes STATE_HISTORY pruning at finality advance (see the
    /// `journal_pruning_paused` field). Called by `Blockchain::enter_reorg` /
    /// `ReorgGuard::drop` to bracket a deep-reorg apply pass.
    pub fn set_journal_pruning_paused(&self, paused: bool) {
        self.journal_pruning_paused
            .store(paused, std::sync::atomic::Ordering::Release);
    }

    /// Removes any installed overlay from the layer cache. Called by the
    /// reconciliation path after the first new-chain commit folds
    /// the overlay into disk. Idempotent.
    pub fn clear_reorg_overlay(&self) -> Result<(), StoreError> {
        let mut guard = self.trie_cache.write().map_err(|_| StoreError::LockError)?;
        let mut updated = (**guard).clone();
        updated.clear_overlay();
        *guard = Arc::new(updated);
        Ok(())
    }

    /// Aborts an in-progress deep reorg and resets the layer cache to a fresh
    /// empty state with the same commit threshold. Both the overlay and any
    /// partially-built new-chain layers are discarded. On-disk state is
    /// untouched (still at the OLD chain's `D`), so subsequent FCU evaluations
    /// start from a clean foundation.
    pub fn abort_reorg(&self) -> Result<(), StoreError> {
        // Rendezvous with the persist worker before swapping, exactly like
        // `install_overlay_for_reorg`: the live-path ack fires BEFORE
        // `apply_trie_phase1` installs the layer, so the worker can still be
        // mid-flight with a pre-abort RCU snapshot. Without the rendezvous its
        // write-back could install a side-chain layer into the fresh cache with
        // no overlay underneath — reads at that root would then cascade into
        // old-chain disk state.
        self.rendezvous_persist_worker("abort_reorg")?;
        let mut guard = self.trie_cache.write().map_err(|_| StoreError::LockError)?;
        let threshold = guard.commit_threshold();
        *guard = Arc::new(TrieLayerCache::new_with_safe_commit(
            threshold,
            self.safe_commit_root.clone(),
        ));
        Ok(())
    }

    /// Takes a block hash and returns an iterator to its ancestors. Block headers are returned
    /// in reverse order, starting from the given block and going up to the genesis block.
    pub fn ancestors(&self, block_hash: BlockHash) -> AncestorIterator {
        AncestorIterator {
            store: self.clone(),
            next_hash: block_hash,
        }
    }

    /// Checks if a given block belongs to the current canonical chain. Returns false if the block is not known
    pub fn is_canonical_sync(&self, block_hash: BlockHash) -> Result<bool, StoreError> {
        let Some(block_number) = self.get_block_number_sync(block_hash)? else {
            return Ok(false);
        };
        Ok(self
            .get_canonical_block_hash_sync(block_number)?
            .is_some_and(|h| h == block_hash))
    }

    pub fn generate_flatkeyvalue(&self) -> Result<(), StoreError> {
        self.flatkeyvalue_control_tx
            .send(FKVGeneratorControlMessage::Continue)
            .map_err(|_| StoreError::Custom("FlatKeyValue thread disconnected.".to_string()))
    }

    pub fn create_checkpoint(&self, path: impl AsRef<Path>) -> Result<(), StoreError> {
        self.backend.create_checkpoint(path.as_ref())?;
        init_metadata_file(path.as_ref())?;
        Ok(())
    }

    pub fn get_store_directory(&self) -> Result<PathBuf, StoreError> {
        Ok(self.db_path.clone())
    }

    /// Loads the latest block number stored in the database, bypassing the latest block number cache
    async fn load_latest_block_number(&self) -> Result<Option<BlockNumber>, StoreError> {
        self.read_chain_data_block_number(ChainDataIndex::LatestBlockNumber)
            .await
    }

    fn load_canonical_block_hash(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHash>, StoreError> {
        let txn = self.backend.begin_read()?;
        txn.get(
            CANONICAL_BLOCK_HASHES,
            block_number.to_le_bytes().as_slice(),
        )?
        .map(|bytes| H256::decode(bytes.as_slice()))
        .transpose()
        .map_err(StoreError::from)
    }

    fn load_block_header(
        &self,
        block_number: BlockNumber,
    ) -> Result<Option<BlockHeader>, StoreError> {
        let Some(block_hash) = self.load_canonical_block_hash(block_number)? else {
            return Ok(None);
        };
        self.load_block_header_by_hash(block_hash)
    }

    /// Load a block header, bypassing the latest header cache
    fn load_block_header_by_hash(
        &self,
        block_hash: BlockHash,
    ) -> Result<Option<BlockHeader>, StoreError> {
        let txn = self.backend.begin_read()?;
        let hash_key = block_hash.encode_to_vec();
        let header_value = txn.get(HEADERS, hash_key.as_slice())?;
        let mut header = header_value
            .map(|bytes| BlockHeaderRLP::from_bytes(bytes).to())
            .transpose()
            .map_err(StoreError::from)?;
        header.as_mut().inspect(|h| {
            // Set the hash so we avoid recomputing it later
            let _ = h.hash.set(block_hash);
        });
        Ok(header)
    }

    pub fn last_written(&self) -> Result<Vec<u8>, StoreError> {
        let last_computed_flatkeyvalue = self
            .last_computed_flatkeyvalue
            .read()
            .map_err(|_| StoreError::LockError)?;
        Ok(last_computed_flatkeyvalue.clone())
    }

    /// Returns `true` once the flat-key-value generator has finished its full pass.
    ///
    /// Completion is recorded by the 1-byte `[0xff]` sentinel the generator writes to
    /// `MISC_VALUES["last_written"]` on the final iteration (see `flatkeyvalue_generator`);
    /// every non-final frontier value is a real nibble path (bytes `0x00..=0x0f`), so the
    /// sentinel is unambiguous. Reads the durable marker rather than the in-memory frontier,
    /// which is expanded to `[0xff; 64]`/`[0xff; 131]` and would need length-aware handling.
    ///
    /// Used to gate journal-backed deep reorgs: entries journaled while generation is still
    /// in progress omit past-frontier flat-KV pre-images.
    pub fn flatkeyvalue_fully_generated(&self) -> Result<bool, StoreError> {
        let tx = self.backend.begin_read()?;
        let marker = tx.get(MISC_VALUES, "last_written".as_bytes())?;
        Ok(Self::flatkeyvalue_generation_complete(marker.as_deref()))
    }

    /// Pure completeness test for the durable `last_written` marker: complete iff it is the
    /// exact 1-byte `[0xff]` sentinel. Any in-progress frontier is a nibble path (bytes
    /// `0x00..=0x0f`) and an unset marker is absent, so neither matches.
    fn flatkeyvalue_generation_complete(marker: Option<&[u8]>) -> bool {
        marker == Some([0xff].as_slice())
    }

    fn flatkeyvalue_computed_with_last_written(account: H256, last_written: &[u8]) -> bool {
        let account_nibbles = Nibbles::from_bytes(account.as_bytes());
        &last_written[0..64] > account_nibbles.as_ref()
    }

    /// Raises the `flushed_upto` marker to `block_number`, on disk and in the
    /// buffer's mirror, so the next start anchors at that height. The marker is a
    /// durability floor and only moves forward: a value at or below the current
    /// one is a no-op.
    ///
    /// Used when startup adopts a head above the recorded one (interrupted
    /// full-sync batch): the blocks were flushed by the persist worker before the
    /// interruption, but the failed starts in between walked the marker down.
    ///
    /// Must run while the persist worker is idle (at startup, before p2p and RPC
    /// exist): the buffer update is a read-clone-swap with no compare-and-swap,
    /// so a concurrent persist message could lose either mutation.
    pub fn advance_flushed_upto(&self, block_number: BlockNumber) -> Result<(), StoreError> {
        if self
            .read_flushed_upto_opt()?
            .is_some_and(|current| current >= block_number)
        {
            return Ok(());
        }
        let mut tx = self.backend.begin_write()?;
        write_flushed_upto(tx.as_mut(), block_number)?;
        tx.commit()?;
        mutate_block_buffer(&self.block_data_buffer, |b| {
            b.set_flushed_upto(block_number)
        })
    }

    /// Returns the highest block number durably flushed to disk, or `0` when
    /// the marker is absent. Use [`Self::read_flushed_upto_opt`] when you need
    /// to distinguish "absent marker" (legacy DB, everything is durable) from
    /// "marker present and equal to 0".
    pub fn read_flushed_upto(&self) -> Result<BlockNumber, StoreError> {
        Ok(self.read_flushed_upto_opt()?.unwrap_or(0))
    }

    /// Returns `None` when the marker has never been written — a legacy or fresh
    /// DB where everything is durable and the head must not be clamped to 0.
    fn read_flushed_upto_opt(&self) -> Result<Option<BlockNumber>, StoreError> {
        let tx = self.backend.begin_read()?;
        match tx.get(MISC_VALUES, FLUSHED_UPTO_KEY)? {
            Some(bytes) => Ok(Some(decode_flushed_upto(&bytes)?)),
            None => Ok(None),
        }
    }

    /// Insert a block into the in-memory buffer without writing to disk.
    /// For testing only — gates production code off.
    #[cfg(any(test, feature = "testing"))]
    pub fn buffer_block_for_test(&self, block: &Block) {
        mutate_block_buffer(&self.block_data_buffer, |b| {
            b.insert(block.clone(), vec![], vec![])
        })
        .expect("block_data_buffer lock poisoned");
    }

    /// Synchronously flush the block data buffer to disk.
    /// For testing only — gates production code off.
    #[cfg(any(test, feature = "testing"))]
    pub fn flush_block_data_for_test(&self) -> Result<(), StoreError> {
        flush_block_data(self.backend.as_ref(), &self.block_data_buffer)
    }

    /// Read a raw trie node straight from the on-disk account/storage trie-node
    /// table by its committed key. For testing only — lets a reopen assert which
    /// trie diff-layers a shutdown flush did (or did not) commit to disk.
    #[cfg(any(test, feature = "testing"))]
    pub fn get_trie_node_for_test(
        &self,
        is_account: bool,
        key: &[u8],
    ) -> Result<Option<Vec<u8>>, StoreError> {
        let table = if is_account {
            ACCOUNT_TRIE_NODES
        } else {
            STORAGE_TRIE_NODES
        };
        self.backend.begin_read()?.get(table, key)
    }

    /// Insert a block plus associated codes into the in-memory buffer without
    /// writing to disk.  For testing only — proves the buffer overlay resolves
    /// code that has not been persisted yet.
    #[cfg(any(test, feature = "testing"))]
    pub fn buffer_block_with_codes_for_test(&self, block: &Block, codes: Vec<(H256, Code)>) {
        mutate_block_buffer(&self.block_data_buffer, |b| {
            b.insert(block.clone(), vec![], codes)
        })
        .expect("block_data_buffer lock poisoned");
    }

    /// Mark a state root as in-flight (build pending) without doing a build.
    /// For testing only — simulates the window where the persist worker has not
    /// yet installed the layer, so reads at this root must block in
    /// `gated_snapshot`.
    #[cfg(any(test, feature = "testing"))]
    pub fn register_pending_root_for_test(&self, root: H256) -> Result<(), StoreError> {
        self.pending_trie_roots.register(root)
    }

    /// Clear an in-flight state root (simulates the worker having installed the
    /// layer), unblocking readers waiting in `gated_snapshot`. For testing only.
    #[cfg(any(test, feature = "testing"))]
    pub fn clear_pending_root_for_test(&self, root: H256) {
        self.pending_trie_roots.clear(root)
    }

    /// Boot-time recovery: clamp `latest_block_header` to the durable head.
    ///
    /// Durable head = `min(flushed_upto, latest)` when the marker is present
    /// (buffered blocks past `flushed_upto` may be lost after a crash; the CL
    /// re-sends them via `newPayload`). When the marker is absent the DB
    /// predates deferred persistence and everything is on disk — use `latest`
    /// as-is, never rewind to 0. On first boot the marker is seeded so a later
    /// crash clamps against it rather than an absent (→ 0) marker.
    ///
    /// The marker tracks the max flushed *block number*, not which hash is
    /// canonical at that height. A tip reorg inside the flush window — `Na` at
    /// height N is flushed (marker = N), then `newPayload(Nb)` buffers a sibling
    /// and FCU durably repoints `canonical[N]` to the still-unflushed `Nb` — can
    /// leave `canonical[head]` resolving to a header that never reached disk if
    /// we crash before `Nb` flushes. So we walk `head` down to the highest height
    /// whose canonical hash actually resolves on disk rather than bricking with
    /// `MissingLatestBlockNumber`. A legacy DB (no marker) is exempt: everything
    /// there is durable, so a missing header is real corruption and must surface.
    async fn anchor_to_durable_head(&self, latest: BlockNumber) -> Result<(), StoreError> {
        let marker = self.read_flushed_upto_opt()?;
        let start = match marker {
            Some(flushed) => flushed.min(latest),
            None => latest,
        };

        let mut head = start;
        let latest_block_header = loop {
            match self.load_block_header(head)? {
                Some(header) => break header,
                // Legacy/fresh DB: everything is supposed to be durable, so a
                // missing header is real corruption — surface it, don't rewind.
                None if marker.is_none() => return Err(StoreError::MissingLatestBlockNumber),
                None if head == 0 => return Err(StoreError::MissingLatestBlockNumber),
                None => {
                    warn!(
                        "durable head {head}: canonical hash has no on-disk header \
                         (reorg inside flush window); rewinding"
                    );
                    head -= 1;
                }
            }
        };
        self.latest_block_header.update(latest_block_header);

        // Re-anchor the persisted head when we moved below `latest`, and (re)write
        // the marker to the resolved head: an absent marker is seeded to the
        // durable baseline, and a walked-down head lowers the marker so a later
        // crash clamps against a hash known to resolve.
        let reanchor = head != latest;
        let rewrite_marker = marker != Some(head);
        if reanchor || rewrite_marker {
            let mut tx = self.backend.begin_write()?;
            if reanchor {
                // Re-anchor the persisted head so `get_latest_block_number` and
                // every downstream consumer agree with the clamped head.
                let latest_key = chain_data_key(ChainDataIndex::LatestBlockNumber);
                tx.put(CHAIN_DATA, &latest_key, &head.to_le_bytes())?;
            }
            if rewrite_marker {
                write_flushed_upto(tx.as_mut(), head)?;
            }
            tx.commit()?;
        }
        Ok(())
    }
}

/// Writes the `flushed_upto` block number into an open write batch.
///
/// The caller is responsible for committing `tx` afterward.
pub fn write_flushed_upto(
    tx: &mut dyn StorageWriteBatch,
    n: BlockNumber,
) -> Result<(), StoreError> {
    tx.put(MISC_VALUES, FLUSHED_UPTO_KEY, &n.to_le_bytes())
}

/// Decode an 8-byte little-endian `flushed_upto` marker value.
///
/// Returns an error for a present-but-malformed value so on-disk corruption is
/// surfaced loudly rather than silently resetting the durable marker. Single
/// source of truth for both `from_backend` and [`Store::read_flushed_upto`].
/// Decodes a `STATE_HISTORY` key (`block_number.to_be_bytes()`). A length mismatch is
/// an error rather than a silent default, which could prune the wrong range.
fn state_history_key_number(key: &[u8]) -> Result<BlockNumber, StoreError> {
    let arr = <[u8; 8]>::try_from(key).map_err(|_| {
        StoreError::Custom(format!(
            "STATE_HISTORY key has unexpected length: {}",
            key.len()
        ))
    })?;
    Ok(BlockNumber::from_be_bytes(arr))
}

fn decode_flushed_upto(bytes: &[u8]) -> Result<BlockNumber, StoreError> {
    let arr: [u8; 8] = bytes
        .try_into()
        .map_err(|_| StoreError::Custom("Invalid flushed_upto bytes".to_string()))?;
    Ok(BlockNumber::from_le_bytes(arr))
}

/// RCU-swap the block-data buffer: read-clone-mutate-swap with no compare-and-swap,
/// so two concurrent callers lose one mutation. In production the persist worker
/// is the only caller while the node runs; [`Store::advance_flushed_upto`] also
/// calls it, but only at startup before the worker has any message in flight.
/// Test helpers call it on one thread.
fn mutate_block_buffer(
    buffer: &Arc<RwLock<Arc<BlockDataBuffer>>>,
    f: impl FnOnce(&mut BlockDataBuffer),
) -> Result<(), StoreError> {
    let mut new_buf = (*buffer.read().map_err(|_| StoreError::LockError)?.clone()).clone();
    f(&mut new_buf);
    *buffer.write().map_err(|_| StoreError::LockError)? = Arc::new(new_buf);
    Ok(())
}

/// Default for [`StoreConfig::persist_channel_capacity`].
const DEFAULT_PERSIST_CHANNEL_CAPACITY: usize = 2;

/// One unit of work for the persist worker: stage block(s), build the trie diff-layer,
/// flush to disk. `commit_depth` selects the commit gate (`None` = canonical safe-commit
/// root, `Some(depth)` = commit layers deeper than `depth`). `wait_for_flush` selects the
/// ack point independently: `false` acks after staging (carrying the prior flush result),
/// `true` acks after flush.
struct BlockPersist {
    blocks: Vec<(Block, Vec<Receipt>)>,
    codes: Vec<(H256, Code)>,
    parent_state_root: H256,
    child_state_root: H256,
    account_updates: TrieNodesUpdate,
    storage_updates: Vec<(H256, TrieNodesUpdate)>,
    commit_depth: Option<usize>,
    wait_for_flush: bool,
    /// Number of the block whose layer this update represents (the last block in
    /// the batch, matching `child_state_root`). Threaded into the trie layer so
    /// the committed-layer identity is available for the journal write path.
    ///
    /// On a multi-block batch this names only the LAST block, which is NOT a usable
    /// journal identity for the batch's layer. `wait_for_flush` does not make that
    /// safe: it decides when THIS message acks, whereas the layer it creates is
    /// journaled or not by whichever LATER commit sweeps it — `TrieLayerCache::commit`
    /// takes the target layer and every ancestor, so a per-block commit can reach a
    /// batch layer. Any journal entry must therefore be gated on the committed
    /// layer covering exactly one block, not on this message's ack mode.
    block_number: BlockNumber,
    /// Hash of the block whose layer this update represents (see `block_number`).
    block_hash: H256,
    ack: std::sync::mpsc::SyncSender<Result<(), StoreError>>,
}

/// Messages for the persist worker. `Ping(ack)` is the idle handshake for
/// [`Store::wait_for_persistence_idle`]: the FIFO worker handles it only after
/// all earlier `Block` messages are fully processed.
enum PersistMessage {
    Block(Box<BlockPersist>),
    /// Flush the committable layer backlog up to and including this state root, then
    /// prune the flushed layers. Sent by `forkchoice_update` when the safe-commit root
    /// advances: the commit step otherwise only runs while blocks execute, so an
    /// execute-all-then-one-forkchoice flow (e.g. block import) would accumulate every
    /// layer and never persist anything to disk.
    Commit(H256),
    Ping(std::sync::mpsc::SyncSender<Result<(), StoreError>>),
    /// Graceful-shutdown handshake. Handled only after every earlier `Block`
    /// (FIFO), so it both drains in-flight work and force-flushes the block-data
    /// buffer to disk. The trie diff-layers are deliberately left in memory (see
    /// [`Store::shutdown`]). The worker acks and exits.
    Shutdown {
        ack: std::sync::mpsc::SyncSender<Result<(), StoreError>>,
    },
}

/// Write one block's header, body, number, and tx locations into an open batch.
/// Shared by [`Store::add_blocks`] (sync import) and [`flush_block_data`]
/// (deferred flush) so the on-disk encoding stays in lockstep. Receipts and codes
/// are written by callers that need them (only `flush_block_data` does).
fn write_block_data(
    tx: &mut dyn StorageWriteBatch,
    number: BlockNumber,
    hash: BlockHash,
    header: &BlockHeader,
    body: &BlockBody,
) -> Result<(), StoreError> {
    let hash_key = hash.encode_to_vec();
    tx.put(
        HEADERS,
        &hash_key,
        BlockHeaderRLP::from(header.clone()).bytes(),
    )?;
    tx.put(
        BODIES,
        &hash_key,
        BlockBodyRLP::from_bytes(body.encode_to_vec()).bytes(),
    )?;
    tx.put(BLOCK_NUMBERS, &hash_key, &number.to_le_bytes())?;
    for (index, transaction) in body.transactions.iter().enumerate() {
        tx.merge(
            TRANSACTION_LOCATIONS,
            transaction.hash(&NativeCrypto).as_bytes(),
            &encode_tx_location_operand(number, hash, index as u64),
        )?;
    }
    Ok(())
}

/// Write all unflushed blocks to disk in one tx, advance `flushed_upto`, then
/// evict. Eviction is gap-safe: blocks stay buffered until the commit succeeds.
fn flush_block_data(
    backend: &dyn StorageBackend,
    buffer: &Arc<RwLock<Arc<BlockDataBuffer>>>,
) -> Result<(), StoreError> {
    let snapshot = buffer.read().map_err(|_| StoreError::LockError)?.clone();
    let to_flush = snapshot.flushable();
    if to_flush.is_empty() {
        return Ok(());
    }
    let hashes: Vec<_> = to_flush.iter().map(|b| b.header.hash()).collect();
    let codes = snapshot.codes_for(&hashes);
    let mut max_number = snapshot.flushed_upto();

    let mut tx = backend.begin_write()?;
    for b in &to_flush {
        let hash = b.header.hash();
        write_block_data(tx.as_mut(), b.number, hash, &b.header, &b.body)?;
        for (index, receipt) in b.receipts.iter().enumerate() {
            tx.put(
                RECEIPTS_V2,
                &receipt_key(&hash, index as u64),
                &receipt.encode_to_vec(),
            )?;
        }
        max_number = max_number.max(b.number);
    }
    for (code_hash, code) in codes {
        let buf = encode_code(&code);
        tx.put(ACCOUNT_CODES, code_hash.as_ref(), &buf)?;
        tx.put(
            ACCOUNT_CODE_METADATA,
            code_hash.as_ref(),
            &(code.len() as u64).to_be_bytes(),
        )?;
    }
    write_flushed_upto(tx.as_mut(), max_number)?;
    tx.commit()?;

    // Phase 3: evict only after the commit succeeded (gap safety).
    mutate_block_buffer(buffer, |b| b.evict_flushed(max_number))
}

type TrieNodesUpdate = Vec<(Nibbles, Vec<u8>)>;

/// Tracks state roots whose trie diff-layer is in-flight (building but not yet
/// installed in `trie_cache`). `apply_updates` registers a root *before*
/// returning; the worker clears it *after* swapping the layer in. This ordering
/// is mandatory: a reader opening a trie at a pending root blocks until the
/// layer is installed, preventing stale on-disk reads.
#[derive(Debug, Default)]
struct PendingTrieRoots {
    /// Fast-path: when zero, nothing is in flight and readers skip the lock.
    count: AtomicUsize,
    roots: Mutex<HashSet<H256>>,
    ready: Condvar,
}

impl PendingTrieRoots {
    /// Mark `root` as in-flight. MUST be called before the build is handed to
    /// the worker (so the worker's `clear` always finds it) and before the head
    /// can advance to `root` (so any reader that can reference it sees it pending).
    fn register(&self, root: H256) -> Result<(), StoreError> {
        let mut roots = self.roots.lock().map_err(|_| StoreError::LockError)?;
        if roots.insert(root) {
            self.count.fetch_add(1, Ordering::Release);
        }
        Ok(())
    }

    /// Mark `root` as installed and wake any waiting readers. MUST be called only
    /// after the layer is swapped into `trie_cache`, so a woken reader sees it.
    /// Best-effort: a poisoned lock means a reader's `wait_until_ready` also errors,
    /// so no reader deadlocks.
    fn clear(&self, root: H256) {
        let Ok(mut roots) = self.roots.lock() else {
            return;
        };
        if roots.remove(&root) {
            self.count.fetch_sub(1, Ordering::Release);
            self.ready.notify_all();
        }
    }

    /// Block until `root` is no longer in-flight (its layer is installed). Returns
    /// immediately on the fast path when nothing is pending.
    fn wait_until_ready(&self, root: H256) -> Result<(), StoreError> {
        if self.count.load(Ordering::Acquire) == 0 {
            return Ok(());
        }
        let mut roots = self.roots.lock().map_err(|_| StoreError::LockError)?;
        while roots.contains(&root) {
            roots = self.ready.wait(roots).map_err(|_| StoreError::LockError)?;
        }
        Ok(())
    }
}

/// Build the trie diff-layer, RCU-swap it into `trie_cache`, then clear the
/// pending root. Swap MUST precede the clear so a woken reader sees the layer.
/// On swap failure the root is still cleared so gated readers error, not deadlock.
#[allow(clippy::too_many_arguments)]
fn apply_trie_phase1(
    trie_cache: &Arc<RwLock<Arc<TrieLayerCache>>>,
    pending_roots: &PendingTrieRoots,
    parent_state_root: H256,
    child_state_root: H256,
    block_number: BlockNumber,
    block_hash: H256,
    account_updates: TrieNodesUpdate,
    storage_updates: Vec<(H256, TrieNodesUpdate)>,
) -> Result<(), StoreError> {
    let build: Result<(), StoreError> = (|| {
        let new_layer = storage_updates
            .into_iter()
            .flat_map(|(account_hash, nodes)| {
                nodes
                    .into_iter()
                    .map(move |(path, node)| (apply_prefix(Some(account_hash), path), node))
            })
            .chain(account_updates)
            .collect();
        let trie = trie_cache
            .read()
            .map_err(|_| StoreError::LockError)?
            .clone();
        let mut trie_mut = (*trie).clone();
        trie_mut.put_batch(
            parent_state_root,
            child_state_root,
            block_number,
            block_hash,
            new_layer,
        );
        *trie_cache.write().map_err(|_| StoreError::LockError)? = Arc::new(trie_mut);
        Ok(())
    })();
    // Always clear the pending root, whether or not the swap succeeded: on success
    // readers see the installed layer; on failure (poisoning) the lock is poisoned
    // so gated readers error rather than read stale, and we must not leave them
    // blocked forever.
    pending_roots.clear(child_state_root);
    build
}

/// Flush and prune the committable trie-layer backlog. No-ops when nothing is committable.
///
/// `commit_depth` selects the gate. `Some(depth)`: single-canonical-chain execution (batch
/// import, full sync, startup regeneration) commits by depth, because the canonical `head - 128`
/// safe-commit root never lands on a batch layer boundary; sound because these paths only ever
/// extend a single canonical chain (no competing forks to mis-commit). `None`: live block-by-block
/// execution uses the canonical safe-commit gate (`TrieLayerCache::get_commitable`) so non-canonical
/// `newPayload` state is never persisted.
///
/// `is_batch` is independent of the gate and only selects journaling (see
/// [`commit_to_disk`]). It tracks `wait_for_flush`, not `commit_depth.is_some()`, so every
/// per-block path journals (layers already final aside, see [`journal_skip_ceiling`]) and
/// only the bespoke batch path skips it. That keeps the full-sync tail and the import
/// tail journaling exactly as they did when `commit_depth` and `wait_for_flush` were a
/// single flag.
///
/// Startup state regeneration is the one path where this is new behavior rather than
/// preserved behavior: it runs before any forkchoice update, so the safe-commit cell is
/// still zero and the canonical gate committed nothing there, which is the unbounded-layer
/// problem the depth gate fixes. Now that it does commit, it also journals. Deriving
/// `is_batch` from `commit_depth` instead would suppress that, but it would also suppress
/// the full-sync and import tails, and it would leave the journal discontiguous with the
/// on-disk root: surviving entries below a non-journaled commit describe pre-images
/// relative to a root the disk has already moved past.
fn commit_trie_if_due(
    backend: &dyn StorageBackend,
    trie_cache: &Arc<RwLock<Arc<TrieLayerCache>>>,
    fkv_ctl: &SyncSender<FKVGeneratorControlMessage>,
    parent_state_root: H256,
    commit_depth: Option<usize>,
    is_batch: bool,
) -> Result<(), StoreError> {
    let trie = trie_cache
        .read()
        .map_err(|_| StoreError::LockError)?
        .clone();
    // Phase 2 + 3: flush and prune the committable backlog.
    let commitable = match commit_depth {
        Some(depth) => trie.get_commitable_by_depth(parent_state_root, depth),
        None => trie.get_commitable(parent_state_root),
    };
    let Some(root) = commitable else {
        // Nothing to commit to disk, move on.
        return Ok(());
    };
    commit_to_disk(backend, fkv_ctl, trie_cache, &trie, root, is_batch)
}

/// Flush the layer at `root` and all older ancestors to disk, then prune them from the
/// in-memory cache (Phases 2 and 3 of the persistence pipeline).
///
/// `trie` must be the current cache snapshot and `root` a committable layer (as returned
/// by [`TrieLayerCache::get_commitable`]). A `root` that is not a layer commits nothing.
///
/// Reused by both the per-block path ([`commit_trie_if_due`]) and the forkchoice-driven
/// flush ([`PersistMessage::Commit`]): without the latter, an execute-all-then-one-forkchoice
/// flow (block import) would never persist, because the commit step only runs while blocks execute.
fn commit_to_disk(
    backend: &dyn StorageBackend,
    fkv_ctl: &SyncSender<FKVGeneratorControlMessage>,
    trie_cache: &Arc<RwLock<Arc<TrieLayerCache>>>,
    trie: &Arc<TrieLayerCache>,
    root: H256,
    is_batch: bool,
) -> Result<(), StoreError> {
    // `root` need not have a layer: the forkchoice `PersistMessage::Commit(root)` path
    // forwards the safe-commit root without consulting the cache, and `put_batch` skips
    // blocks whose state root equals their parent's. Bail before the side effects below.
    if !trie.has_layer(root) {
        debug!(
            root = ?root,
            layers = trie.layer_count(),
            is_batch,
            "Skipping trie commit: state root has no in-memory layer. Expected when the block \
             did not change the state root (empty L2 blocks) or the root was already flushed."
        );
        return Ok(());
    }

    // Stop the flat-key-value generator thread, as the underlying trie is about to change.
    // Ignore the error, if the channel is closed it means there is no worker to notify.
    let _ = fkv_ctl.send(FKVGeneratorControlMessage::Stop);

    // RCU to remove the bottom layer: update step needs to happen after disk layer is updated.
    let mut trie_mut = (**trie).clone();

    // Open the read view BEFORE the write batch so each `.get()` sees disk as it was
    // before our writes. The journal records `(key, prev_value)` so a future rollback
    // can apply diffs directly without reading state.
    //
    // NOTE: `StorageReadView` does not currently promise true snapshot isolation
    // (see the trait docs in `api/mod.rs`). What makes the pre-image read safe here
    // is that `commit_to_disk` only ever runs on the single persist worker thread,
    // `write_tx` is a buffered batch that does not become visible until
    // `write_tx.commit()` at the end of the function, and the other writers to
    // these CFs touch disjoint key space: the flat-KV generator only writes keys
    // strictly past the committed `last_written` frontier, and snap-sync healing
    // completes before block execution commits layers. So every `.get()` below
    // sees on-disk state as of the begin_read call.
    let read_view = backend.begin_read()?;
    let last_written = read_view
        .get(MISC_VALUES, "last_written".as_bytes())?
        .unwrap_or_default();

    let mut write_tx = backend.begin_write()?;

    // Before encoding, accounts have only the account address as their path, while storage keys have
    // the account address (32 bytes) + storage path (up to 32 bytes).

    // Snapshot the overlay (if any) BEFORE commit so reconciliation can fold its entries
    // into this write batch. After a deep reorg, the first
    // new-chain commit advances disk from the OLD chain's edge `D` directly to the new
    // chain's tip `T` in a single atomic write; the overlay supplies the bridge for keys
    // layer_T does not touch. Only meaningful when `!is_batch`: the legacy batch-store path
    // (`wait_for_flush == true`) is the one path that does not journal.
    let overlay_for_reconciliation = if !is_batch {
        trie.overlay().cloned()
    } else {
        None
    };

    // While an overlay is installed, commit only the bottom layer per pass. The
    // The reconciliation below is defined for a single layer at the pivot
    // tip `T`: bridge entries are folded into that layer's writes, [T, D] is
    // delete_ranged, and T's journal entry records pre-images against the
    // old-chain disk state. A multi-layer sweep would journal upper layers'
    // pre-images against old-chain disk instead of the new-chain/bridge state
    // (the intra-batch pre-image map is not seeded with bridge values), silently
    // corrupting a future unwind. The backlog above `T` drains in later passes,
    // after this commit clears the overlay (see `trie_mut.clear_overlay` below).
    let root = if overlay_for_reconciliation.is_some() {
        trie.bottom_layer_root(root)
    } else {
        root
    };

    // `commit` removes the committed layer(s) and returns one `CommittedLayer` per block
    // in oldest-first order. In normal block-by-block operation this is a single layer,
    // one commit-cadence behind the just-added block. A forkchoice-driven flush of an
    // accumulated backlog (e.g. block import) can return several layers at once, so we
    // write one journal entry per block below rather than merging diffs across blocks.
    //
    // `has_layer` above established `root` is a layer and `trie_mut` is a `Clone` that
    // preserves the map, so this must be `Some`. Reaching the error means the clone lost the
    // map, which is corruption, not the ordinary no-op handled above.
    let committed_layers = trie_mut.commit(root).ok_or_else(|| {
        StoreError::Custom(format!(
            "trie layer for state root {root:?} disappeared between the has_layer check and \
             commit (layers={}, is_batch={is_batch}): the TrieLayerCache clone lost the layer \
             map, so this block's state was not written",
            trie.layer_count(),
        ))
    })?;

    // Deep-reorg reconciliation is a single new-chain commit advancing disk to the pivot
    // tip `T`; it must map to exactly one committed layer. A multi-layer commit with an
    // overlay installed would mean a backlog accumulated between overlay install and
    // flush, corrupting the [T, D] delete_range below.
    debug_assert!(
        overlay_for_reconciliation.is_none() || committed_layers.len() == 1,
        "overlay-backed reconciliation must commit exactly one layer (T), got {}",
        committed_layers.len()
    );

    // Reconciliation: overlay entries the new chain has NOT rewritten must be
    // bridged onto disk so disk fully reflects the pivot->T transition. Keys any committed
    // layer touches are skipped (the layer wins, its value is the post-T state). Overlay-only
    // entries with `None` become an empty-value write -> deleted on disk, matching the
    // "absent at pivot" semantics.
    let extra_writes: Vec<(Vec<u8>, Vec<u8>)> = match &overlay_for_reconciliation {
        Some(overlay) => {
            let layer_keys: SeededHashSet<&Vec<u8>> = committed_layers
                .iter()
                .flat_map(|l| l.nodes.iter().map(|(k, _)| k))
                .collect();
            overlay
                .iter_all_entries()
                .filter(|(_, key, _)| !layer_keys.contains(key))
                .map(|(_, key, value)| (key.clone(), value.clone().unwrap_or_default()))
                .collect()
        }
        None => Vec::new(),
    };
    // Pivot heights (T, D) for the reconciliation commit; `None` in steady state.
    let reorg_heights = overlay_for_reconciliation
        .as_ref()
        .map(|ov| (ov.to_block(), ov.from_block()));

    // Layers at or below finality are committed without a journal entry. An entry only
    // serves a reorg that unwinds its block, and finality pruning deletes every entry at
    // or below finality on the next advance, so this one could only serve a reorg across
    // finality that arrives before that advance. Finality is not a reorg limit (see
    // `compute_reorg_ceiling` in the blockchain crate), but the limit is computed from
    // the entries present, so such a reorg is rejected as too deep in that window instead
    // of served. Layers commit ~DB_COMMIT_THRESHOLD blocks behind the head, which on a
    // finalizing chain is already final (an L2 finalizes its head every block, L1
    // finality trails the head by two or three epochs), so otherwise every entry is
    // written only to be range-deleted. The reconciliation layer of a deep reorg always
    // journals. `None` journals every layer.
    let skip_journal_upto = if is_batch || overlay_for_reconciliation.is_some() {
        None
    } else {
        journal_skip_ceiling(read_view.as_ref())
    };
    let journals_layer = |block_number: BlockNumber| {
        !is_batch && skip_journal_upto.is_none_or(|upto| block_number > upto)
    };
    // Skipped layers come first (layers are oldest-first), and a journaled layer after them
    // must see their staged writes as its pre-images, so they still feed the overlay below.
    let track_overlay = committed_layers
        .iter()
        .any(|layer| journals_layer(layer.block_number));

    // Intra-batch overlay of values already staged in THIS write batch, so each block's
    // reverse diff records the value as of the *previous* committed block's write, not
    // just the pre-batch on-disk value. `None` means an earlier block deleted the key.
    // For the common single-layer commit this stays empty and every pre-image comes
    // straight from `read_view`. Only maintained when some layer of this commit journals
    // (`track_overlay`).
    //
    // PERF: the first touch of each key does one synchronous `read_view.get(table, &key)`.
    // For large state diffs this is O(N) extra reads on the per-block critical path.
    // A follow-up could batch these via `multi_get_cf` if profiling shows it's significant.
    let mut overlay: HashMap<Vec<u8>, Option<Vec<u8>>> = HashMap::new();

    let mut result = Ok(());
    'layers: for layer in &committed_layers {
        // Reverse-diff accumulators for this block's journal entry, one per CF. Each entry
        // stores the on-disk key as-is (storage CFs carry their nibble-encoded account-hash
        // prefix), so a future rollback applies diffs directly without interpretation. For
        // the legacy batch-store path (`is_batch == true`) and for layers at or below
        // `skip_journal_upto` no journal entry is written. Every other per-block commit
        // journals — including full sync through the unified pipeline, whose
        // interrupted-batch recovery at startup relies on the newest entry naming the
        // block whose state is on disk.
        let journal_layer = journals_layer(layer.block_number);
        let mut journal_account_trie: FlatDiff = Vec::new();
        let mut journal_storage_trie: FlatDiff = Vec::new();
        let mut journal_account_flat: FlatDiff = Vec::new();
        let mut journal_storage_flat: FlatDiff = Vec::new();

        // The reconciliation commit is single-layer at `T`; fold the overlay bridge entries
        // into that layer's writes so they land in T's journal entry. `extra` is empty in
        // steady state (no overlay) and for any non-T layer.
        // With the bottom-layer-only rule above, a commit made while the overlay
        // is installed always contains exactly this one (bottom) layer, so it is
        // the reconciliation layer by construction. Matching by block number
        // (`layer.block_number == to_block`) would be fragile: root-preserving
        // blocks create no layer (L2 empty blocks), so the bottom layer can sit
        // above `to_block` and the bridge entries would be neither written nor
        // journaled before the overlay is cleared.
        let is_reconciliation_layer = overlay_for_reconciliation.is_some();
        let extra: &[(Vec<u8>, Vec<u8>)] = if is_reconciliation_layer {
            &extra_writes
        } else {
            &[]
        };

        // Pre-images for the reconciliation layer must be taken at the pivot, not at the
        // old chain's edge `D`. `T`'s journal entry has to reverse disk back to the pivot
        // state, but this batch is built while disk still holds `D`, so `read_view` yields
        // `D`'s values for every key the old chain rewrote in `[T, D]`. The overlay *is*
        // the pivot-vs-`D` difference, so it wins over `read_view` for any key it carries.
        // `None` in the overlay means "absent at the pivot", which is exactly the journal's
        // "delete on rollback" entry.
        let pivot_overlay = overlay_for_reconciliation.as_ref();

        for (key, value) in layer.nodes.iter().chain(extra.iter()) {
            let (is_leaf, is_account) = classify_trie_key(key.len());

            // Keys past the flat-KV generator's frontier aren't written to disk yet, so
            // they must not be journaled either (a `Some(None)` entry recorded here
            // would cause a rollback to delete a key that was never put). The `continue`
            // jumps over both the write and the journal push below.
            if is_leaf && key.as_slice() > last_written.as_slice() {
                continue;
            }
            let table = if is_leaf {
                if is_account {
                    &ACCOUNT_FLATKEYVALUE
                } else {
                    &STORAGE_FLATKEYVALUE
                }
            } else if is_account {
                &ACCOUNT_TRIE_NODES
            } else {
                &STORAGE_TRIE_NODES
            };

            // Pre-image: the intra-batch overlay wins over the pivot overlay, which wins
            // over disk, so multi-layer commits record each block's true pre-state and the
            // reconciliation layer records the pivot's. Skipped for layers that don't journal.
            let prev_value = if journal_layer {
                match overlay.get(key) {
                    Some(v) => Some(v.clone()),
                    None => match pivot_overlay
                        .and_then(|ov| ov.lookup(OverlayCf::classify_by_key_length(key.len()), key))
                    {
                        Some(v) => Some(v),
                        None => match read_view.get(table, key) {
                            Ok(v) => Some(v),
                            Err(e) => {
                                result = Err(e);
                                break 'layers;
                            }
                        },
                    },
                }
            } else {
                None
            };

            let new_value = if value.is_empty() {
                result = write_tx.delete(table, key);
                None
            } else {
                result = write_tx.put(table, key, value);
                Some(value.clone())
            };
            if result.is_err() {
                break 'layers;
            }

            // Record the reverse-diff entry after the put/delete is staged, so a write
            // error doesn't accumulate state we won't persist.
            if let Some(prev) = prev_value {
                let bucket = match (is_leaf, is_account) {
                    (false, true) => &mut journal_account_trie,
                    (false, false) => &mut journal_storage_trie,
                    (true, true) => &mut journal_account_flat,
                    (true, false) => &mut journal_storage_flat,
                };
                bucket.push((key.clone(), prev));
            }
            // Advance the overlay so a later block in this same commit sees this
            // block's write as its pre-image.
            if track_overlay {
                overlay.insert(key.clone(), new_value);
            }
        }

        // Reconciliation: BEFORE writing this block's journal entry, wipe the
        // obsolete OLD-chain entries in `[T, D]` so the new T entry (below) isn't clobbered
        // by the range delete. Fires only for the single reconciliation layer at height T.
        // T = `overlay.to_block()`, D = `overlay.from_block()`.
        if !is_batch && let Some((t, d)) = reorg_heights {
            debug_assert_eq!(
                layer.block_number, t,
                "first new-chain commit must be at the pivot's T height (overlay.to_block)"
            );
            // Only reconcile when this committed layer is exactly the pivot height `t`.
            // The `delete_range` wipes the OLD-chain journal entries `[t, d]`; if the layer
            // is not at `t` the range delete would drop new-chain entries that belong in
            // `[t, d]` and leave gaps that break later reorg recovery. The debug_assert
            // catches this loudly in tests; in release we skip (and log) rather than corrupt
            // STATE_HISTORY.
            if layer.block_number == t {
                let start = t.to_be_bytes();
                let end = d.saturating_add(1).to_be_bytes();
                result = write_tx.delete_range(STATE_HISTORY, &start, &end);
                if result.is_err() {
                    break 'layers;
                }
            } else {
                error!(
                    block_number = layer.block_number,
                    t,
                    d,
                    "deep-reorg reconciliation skipped: committed layer not at pivot height; skipping STATE_HISTORY delete_range to avoid history gaps"
                );
            }
        }

        // Stage this block's journal entry into the same write batch as the trie/flat-KV
        // overwrites. `put` is buffered until `commit`, so all CFs and every block's entry
        // land atomically (or none do on commit failure). Each entry is keyed and identified
        // by its own COMMITTED block, not the in-flight block whose insertion triggered this
        // commit (that block commits later, one cadence behind).
        if journal_layer {
            let entry = JournalEntry {
                block_hash: layer.block_hash,
                parent_state_root: layer.parent_state_root,
                account_trie_diff: journal_account_trie,
                storage_trie_diff: journal_storage_trie,
                account_flat_diff: journal_account_flat,
                storage_flat_diff: journal_storage_flat,
            };
            result = write_tx.put(
                STATE_HISTORY,
                &layer.block_number.to_be_bytes(),
                &entry.encode(),
            );
            if result.is_err() {
                break 'layers;
            }
        }
    }

    if result.is_ok() {
        result = write_tx.commit();
    }
    // We want to send this message even if there was an error during the batch write
    let _ = fkv_ctl.send(FKVGeneratorControlMessage::Continue);
    result?;

    // Reconciliation succeeded: drop the overlay from the cache. Subsequent
    // commits revert to the normal one-block path.
    if overlay_for_reconciliation.is_some() {
        trie_mut.clear_overlay();
    }

    // Phase 3: update diff layers with the removal of bottom layer.
    //
    // SAFETY: `install_overlay_for_reorg` rendezvous-pings this worker before
    // swapping `trie_cache`, so no concurrent RCU can race the write-back here.
    // See the Ping comment in `install_overlay_for_reorg`.
    *trie_cache.write().map_err(|_| StoreError::LockError)? = Arc::new(trie_mut);
    Ok(())
}

/// Highest block number whose layer [`commit_to_disk`] may commit without a journal
/// entry, or `None` to journal every layer.
///
/// A layer is skipped only when its block is at or below finality, at or below the head,
/// and the journal is empty. Together these leave `STATE_HISTORY` in a state it already
/// reaches without skipping (layers committed, then pruned by the next finality advance),
/// so no reader of the journal sees anything new:
/// - Finality: pruning deletes every entry at or below finality on the next advance, so
///   the entry could only serve a reorg across finality that arrives before that advance.
///   Finality is not a reorg limit (`compute_reorg_ceiling` trusts the CL's fork choice,
///   and the engine API tests reorg across it), but the limit is computed from the entries
///   present, so such a reorg is rejected as too deep in that window instead of served:
///   up to an epoch on L1, one block on L2.
/// - Head: interrupted-batch recovery adopts the newest entry only when it is above the
///   head, so it never needs a skipped one. Finality is normally at or below the head,
///   but a head rewound without a finality update (an L2 revert, a restart clamping the
///   head to the flushed blocks) can leave it above.
/// - Empty journal: a surviving entry below a skipped layer would hold pre-images against
///   a state the disk has moved past, and would no longer be the newest entry naming the
///   block whose state is on disk. Skipping resumes once finality pruning has cleared it.
///
/// A failed or malformed read journals, the conservative choice.
fn journal_skip_ceiling(read_view: &dyn StorageReadView) -> Option<BlockNumber> {
    let read_number = |index| {
        read_view
            .get(CHAIN_DATA, &chain_data_key(index))
            .ok()
            .flatten()
            .and_then(|bytes| <[u8; 8]>::try_from(bytes.as_slice()).ok())
            .map(BlockNumber::from_le_bytes)
    };
    let finalized = read_number(ChainDataIndex::FinalizedBlockNumber)?;
    let head = read_number(ChainDataIndex::LatestBlockNumber)?;
    match read_view.first_key(STATE_HISTORY) {
        Ok(None) => Some(finalized.min(head)),
        _ => None,
    }
}

// NOTE: we don't receive `Store` here to avoid cyclic dependencies
// with the other end of `control_rx`
fn flatkeyvalue_generator(
    backend: &Arc<dyn StorageBackend>,
    last_computed_fkv: &RwLock<Vec<u8>>,
    control_rx: &std::sync::mpsc::Receiver<FKVGeneratorControlMessage>,
) -> Result<(), StoreError> {
    info!("Generation of FlatKeyValue started.");
    let initial_last_written = backend
        .begin_read()?
        .get(MISC_VALUES, "last_written".as_bytes())?
        .unwrap_or_default();

    if initial_last_written.is_empty() {
        // First time generating the FKV. Remove all FKV entries just in case
        backend.clear_table(ACCOUNT_FLATKEYVALUE)?;
        backend.clear_table(STORAGE_FLATKEYVALUE)?;
    } else if initial_last_written == [0xff] {
        // FKV was already generated
        info!("FlatKeyValue already generated. Skipping.");
        return Ok(());
    }

    loop {
        // Acquire a fresh read view per iteration so updates performed while the
        // generator is paused are visible after a Continue signal.
        let read_tx = backend.begin_read()?;
        let root = read_tx
            .get(ACCOUNT_TRIE_NODES, &[])?
            .ok_or(StoreError::MissingLatestBlockNumber)?;
        let root: Node = ethrex_trie::Node::decode(&root)?;
        let state_root = root.compute_hash(&NativeCrypto).finalize(&NativeCrypto);

        let last_written = read_tx
            .get(MISC_VALUES, "last_written".as_bytes())?
            .unwrap_or_default();
        let last_written_account = last_written
            .get(0..64)
            .map(|v| Nibbles::from_hex(v.to_vec()))
            .unwrap_or_default();
        let mut last_written_storage = last_written
            .get(66..130)
            .map(|v| Nibbles::from_hex(v.to_vec()))
            .unwrap_or_default();

        debug!("Starting FlatKeyValue loop pivot={last_written:?} SR={state_root:x}");

        let mut ctr = 0;
        let mut write_txn = backend.begin_write()?;
        let mut iter = Trie::open(
            Box::new(BackendTrieDB::new_for_accounts_with_view(
                backend.clone(),
                read_tx.clone(),
                last_written.clone(),
            )?),
            state_root,
        )
        .into_iter();
        if last_written_account > Nibbles::default() {
            iter.advance(last_written_account.to_bytes())?;
        }
        let res = iter.try_for_each(|(path, node)| -> Result<(), StoreError> {
            let Node::Leaf(node) = node else {
                return Ok(());
            };
            let account_state = AccountState::decode(&node.value)?;
            let account_hash = H256::from_slice(&path.to_bytes());
            write_txn.put(MISC_VALUES, "last_written".as_bytes(), path.as_ref())?;
            write_txn.put(ACCOUNT_FLATKEYVALUE, path.as_ref(), &node.value)?;
            ctr += 1;
            if ctr > 10_000 {
                write_txn.commit()?;
                write_txn = backend.begin_write()?;
                *last_computed_fkv
                    .write()
                    .map_err(|_| StoreError::LockError)? = path.as_ref().to_vec();
                ctr = 0;
            }

            let mut iter_inner = Trie::open(
                Box::new(BackendTrieDB::new_for_account_storage_with_view(
                    backend.clone(),
                    read_tx.clone(),
                    account_hash,
                    path.as_ref().to_vec(),
                )?),
                account_state.storage_root,
            )
            .into_iter();
            if last_written_storage > Nibbles::default() {
                iter_inner.advance(last_written_storage.to_bytes())?;
                last_written_storage = Nibbles::default();
            }
            iter_inner.try_for_each(|(path, node)| -> Result<(), StoreError> {
                let Node::Leaf(node) = node else {
                    return Ok(());
                };
                let key = apply_prefix(Some(account_hash), path);
                write_txn.put(MISC_VALUES, "last_written".as_bytes(), key.as_ref())?;
                write_txn.put(STORAGE_FLATKEYVALUE, key.as_ref(), &node.value)?;
                ctr += 1;
                if ctr > 10_000 {
                    write_txn.commit()?;
                    write_txn = backend.begin_write()?;
                    *last_computed_fkv
                        .write()
                        .map_err(|_| StoreError::LockError)? = key.into_vec();
                    ctr = 0;
                }
                fkv_check_for_stop_msg(control_rx)?;
                Ok(())
            })?;
            fkv_check_for_stop_msg(control_rx)?;
            Ok(())
        });
        match res {
            Err(StoreError::PivotChanged) => {
                match control_rx.recv() {
                    Ok(FKVGeneratorControlMessage::Continue) => {}
                    Ok(FKVGeneratorControlMessage::Stop) => {
                        return Err(StoreError::Custom("Unexpected Stop message".to_string()));
                    }
                    // If the channel was closed, we stop generation prematurely
                    Err(std::sync::mpsc::RecvError) => {
                        info!("Store closed, stopping FlatKeyValue generation.");
                        return Ok(());
                    }
                }
            }
            Err(err) => return Err(err),
            Ok(()) => {
                write_txn.put(MISC_VALUES, "last_written".as_bytes(), &[0xff])?;
                write_txn.commit()?;
                *last_computed_fkv
                    .write()
                    .map_err(|_| StoreError::LockError)? = vec![0xff; 131];
                info!("FlatKeyValue generation finished.");
                return Ok(());
            }
        };
    }
}

fn fkv_check_for_stop_msg(
    control_rx: &std::sync::mpsc::Receiver<FKVGeneratorControlMessage>,
) -> Result<(), StoreError> {
    match control_rx.try_recv() {
        Ok(FKVGeneratorControlMessage::Stop) | Err(TryRecvError::Disconnected) => {
            return Err(StoreError::PivotChanged);
        }
        Ok(FKVGeneratorControlMessage::Continue) => {
            return Err(StoreError::Custom(
                "Unexpected Continue message".to_string(),
            ));
        }
        Err(TryRecvError::Empty) => {}
    }
    Ok(())
}

fn state_trie_locked_backend(
    backend: &dyn StorageBackend,
    last_written: Vec<u8>,
) -> Result<BackendTrieDBLocked, StoreError> {
    // No address prefix for state trie
    BackendTrieDBLocked::new(backend, last_written)
}

pub struct AccountProof {
    pub proof: Vec<NodeRLP>,
    pub account: AccountState,
    pub storage_proof: Vec<StorageSlotProof>,
}

pub struct StorageSlotProof {
    pub proof: Vec<NodeRLP>,
    pub key: H256,
    pub value: U256,
}

pub struct AncestorIterator {
    store: Store,
    next_hash: BlockHash,
}

impl Iterator for AncestorIterator {
    type Item = Result<(BlockHash, BlockHeader), StoreError>;

    fn next(&mut self) -> Option<Self::Item> {
        let next_hash = self.next_hash;
        // Buffer-aware: a not-yet-flushed ancestor (e.g. on a side branch during
        // a reorg) must be visible here, or a BLOCKHASH opcode resolving through
        // this walk would wrongly reject a valid block.
        match self.store.get_block_header_by_hash(next_hash) {
            Ok(Some(header)) => {
                let ret_hash = self.next_hash;
                self.next_hash = header.parent_hash;
                Some(Ok((ret_hash, header)))
            }
            Ok(None) => None,
            Err(e) => Some(Err(e)),
        }
    }
}

pub fn hash_address(address: &Address) -> Vec<u8> {
    keccak_hash(address.to_fixed_bytes()).to_vec()
}

fn hash_address_fixed(address: &Address) -> H256 {
    keccak(address.to_fixed_bytes())
}

pub fn hash_key(key: &H256) -> Vec<u8> {
    keccak_hash(key.to_fixed_bytes()).to_vec()
}

pub fn hash_key_fixed(key: &H256) -> [u8; 32] {
    keccak_hash(key.to_fixed_bytes())
}

fn chain_data_key(index: ChainDataIndex) -> Vec<u8> {
    (index as u8).encode_to_vec()
}

/// Decodes a block number stored as little-endian bytes.
fn decode_block_number(bytes: Vec<u8>) -> Result<BlockNumber, StoreError> {
    let array: [u8; 8] = bytes
        .try_into()
        .map_err(|_| StoreError::Custom("Invalid BlockNumber bytes".to_string()))?;
    Ok(BlockNumber::from_le_bytes(array))
}

fn snap_state_key(index: SnapStateIndex) -> Vec<u8> {
    (index as u8).encode_to_vec()
}

/// Builds a fixed-width RECEIPTS key: block_hash (32B) || index (8B BE).
pub fn receipt_key(block_hash: &BlockHash, index: u64) -> Vec<u8> {
    let mut key = Vec::with_capacity(40);
    key.extend_from_slice(block_hash.as_bytes());
    key.extend_from_slice(&index.to_be_bytes());
    key
}

pub fn encode_code(code: &Code) -> Vec<u8> {
    let jumpdests = code.jumpdests();
    let mut buf = Vec::with_capacity(6 + code.len() + jumpdests.len());
    code.code().encode(&mut buf);
    jumpdests.encode(&mut buf);
    buf
}

/// Decodes the JUMPDEST bitmap stored after the bytecode in an [`ACCOUNT_CODES`] value.
///
/// Values written before the bitmap representation hold an RLP *list* of `u32` offsets
/// there instead. The RLP item header distinguishes a list from a byte string, so both
/// forms are readable: for the older one the bitmap is rebuilt from the bytecode, which
/// is cheaper than decoding the list.
/// Decodes the stored jump-destination bitmap. The bool is `true` when the value
/// was in the legacy RLP-list-of-`u32` format and had to be recomputed from
/// `code` — the caller uses it to self-heal the row to the bitmap format so the
/// recompute is paid at most once per entry rather than on every cold read.
fn decode_jumpdests(code: &[u8], encoded: &[u8]) -> Result<(Arc<[u8]>, bool), StoreError> {
    let (is_list, payload, _) = decode_rlp_item(encoded)?;
    if is_list {
        return Ok((Code::compute_jumpdests(code), true));
    }
    Ok((Arc::from(payload), false))
}

#[derive(Debug, Default, Clone)]
struct LatestBlockHeaderCache {
    current: Arc<Mutex<Arc<BlockHeader>>>,
}

impl LatestBlockHeaderCache {
    pub fn get(&self) -> Arc<BlockHeader> {
        self.current.lock().expect("poisoned mutex").clone()
    }

    pub fn update(&self, header: BlockHeader) {
        let new = Arc::new(header);
        *self.current.lock().expect("poisoned mutex") = new;
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct StoreMetadata {
    pub schema_version: u64,
}

impl StoreMetadata {
    pub fn new(schema_version: u64) -> Self {
        Self { schema_version }
    }
}

/// Reads the schema version from the metadata file, if it exists.
///
/// Returns `Some(version)` when metadata.json is present and valid,
/// or `None` when the file does not exist.
fn read_store_schema_version(path: &Path) -> Result<Option<u64>, StoreError> {
    let metadata_path = path.join(STORE_METADATA_FILENAME);
    if !metadata_path.exists() {
        return Ok(None);
    }
    if !metadata_path.is_file() {
        return Err(StoreError::Custom(
            "store schema path exists but is not a file".to_string(),
        ));
    }
    let file_contents = std::fs::read_to_string(metadata_path)?;
    let metadata: StoreMetadata = serde_json::from_str(&file_contents)?;
    Ok(Some(metadata.schema_version))
}

fn init_metadata_file(parent_path: &Path) -> Result<(), StoreError> {
    std::fs::create_dir_all(parent_path)?;

    let metadata_path = parent_path.join(STORE_METADATA_FILENAME);
    let metadata = StoreMetadata::new(STORE_SCHEMA_VERSION);
    let serialized_metadata = serde_json::to_string_pretty(&metadata)?;
    let mut new_file = std::fs::File::create_new(metadata_path)?;
    new_file.write_all(serialized_metadata.as_bytes())?;
    Ok(())
}

/// Returns `true` if `path` contains a *legacy* database — one written before
/// the metadata file existed, so it has no `metadata.json` to identify it.
/// Detected by RocksDB's own marker files, as opposed to unrelated files that
/// merely share the datadir. Only meaningful once metadata has been confirmed
/// absent; otherwise prefer `has_valid_db`, which keys off the metadata file.
///
/// Previously the caller treated *any* non-empty directory as such a legacy
/// database, which made startup fail when unrelated files lived alongside the DB
/// — e.g. EthDocker writes the JWT secret into the datadir (issue #5680). We
/// instead look for RocksDB's marker files, so a datadir that only contains such
/// unrelated files is correctly treated as fresh.
fn dir_contains_legacy_db(path: &Path) -> Result<bool, StoreError> {
    // `CURRENT` has a fixed name and is written by every RocksDB instance, so
    // check for it directly instead of scanning a datadir that may hold many
    // unrelated files.
    if path.join("CURRENT").is_file() {
        return Ok(true);
    }
    // The manifest has a numeric suffix (`MANIFEST-<n>`), so it can only be
    // found by scanning. Restrict to plain files: a directory that happens to
    // share the name is not a database marker.
    for entry in std::fs::read_dir(path)? {
        let entry = entry?;
        if !entry.file_type()?.is_file() {
            continue;
        }
        if entry.file_name().to_string_lossy().starts_with("MANIFEST-") {
            return Ok(true);
        }
    }
    Ok(false)
}

/// Checks whether a valid (or migratable) database exists at the given path
/// by looking for a metadata.json file with a schema version between 1 and
/// `STORE_SCHEMA_VERSION` (inclusive).
pub fn has_valid_db(path: &Path) -> bool {
    let metadata_path = path.join(STORE_METADATA_FILENAME);
    if !metadata_path.is_file() {
        return false;
    }
    let Ok(contents) = std::fs::read_to_string(&metadata_path) else {
        return false;
    };
    let Ok(metadata) = serde_json::from_str::<StoreMetadata>(&contents) else {
        return false;
    };
    metadata.schema_version >= 1 && metadata.schema_version <= STORE_SCHEMA_VERSION
}

/// Reads the chain ID from an existing database without performing a full
/// store initialization. Returns `None` if the database doesn't exist or
/// the chain config can't be read. Always returns `None` when compiled
/// without the `rocksdb` feature.
///
/// Each failure mode logs a warning so callers (and operators) can diagnose
/// why an existing database was not usable — previously every error was
/// silently swallowed by `.ok()?`.
pub fn read_chain_id_from_db(path: &Path) -> Option<u64> {
    if !has_valid_db(path) {
        return None;
    }
    #[cfg(feature = "rocksdb")]
    {
        // The cache size is irrelevant for this one-shot chain-id read (the LRU
        // is sized as a ceiling, not pre-allocated), so we use the ceiling.
        let backend = match RocksDBBackend::open(path, MAX_ROCKSDB_BLOCK_CACHE_SIZE_BYTES) {
            Ok(backend) => backend,
            Err(e) => {
                warn!("Failed to open RocksDB at {path:?} to read chain ID: {e}");
                return None;
            }
        };
        let read = match backend.begin_read() {
            Ok(read) => read,
            Err(e) => {
                warn!("Failed to begin read transaction at {path:?}: {e}");
                return None;
            }
        };
        let key = chain_data_key(ChainDataIndex::ChainConfig);
        let bytes = match read.get(CHAIN_DATA, &key) {
            Ok(Some(bytes)) => bytes,
            Ok(None) => {
                warn!("Chain config entry not found in database at {path:?}");
                return None;
            }
            Err(e) => {
                warn!("Failed to read chain config from database at {path:?}: {e}");
                return None;
            }
        };
        // Only extract chain_id here: the stored `ChainConfig` JSON may include
        // fields whose serialization changed across releases (e.g. pre-v10 wrote
        // `terminal_total_difficulty` as a plain number, v10 expects hex string).
        // Deserializing the full struct would reject otherwise-migratable v9 data.
        #[derive(serde::Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct ChainIdOnly {
            chain_id: u64,
        }
        match serde_json::from_slice::<ChainIdOnly>(&bytes) {
            Ok(partial) => Some(partial.chain_id),
            Err(e) => {
                warn!("Failed to deserialize chain ID from database at {path:?}: {e}");
                None
            }
        }
    }
    #[cfg(not(feature = "rocksdb"))]
    {
        let _ = path;
        None
    }
}

#[cfg(test)]
mod account_code_tests {
    use super::*;

    const JUMPDEST: u8 = 0x5b;
    const PUSH1: u8 = 0x60;

    /// `EXTCODESIZE` resolves a length through [`Store::get_code_metadata`], and
    /// execution runs on rayon workers, which are not inside a Tokio runtime. A hash
    /// with no metadata row SHALL still answer from the bytecode there rather than
    /// panicking on the backfill spawn. This test is deliberately NOT `#[tokio::test]`:
    /// the absence of a runtime is the condition under test.
    #[test]
    fn metadata_falls_back_to_the_bytecode_without_a_tokio_runtime() {
        use crate::backend::in_memory::InMemoryBackend;

        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // Code present, metadata row absent: the shape of any database written before
        // ACCOUNT_CODE_METADATA existed, which no migration backfills.
        let code = jumpdest_dense_code();
        let mut tx = backend.begin_write().unwrap();
        tx.put(ACCOUNT_CODES, code.hash.as_bytes(), &encode_code(&code))
            .unwrap();
        tx.commit().unwrap();

        let metadata = store
            .get_code_metadata(code.hash)
            .expect("metadata read must not fail off-runtime");
        assert_eq!(metadata.map(|m| m.length), Some(code.len() as u64));
    }

    /// A max-size contract that is almost entirely JUMPDESTs, the shape that makes the
    /// jump-destination representation matter: as a list of offsets it is ~4x the size
    /// of the bytecode it describes.
    fn jumpdest_dense_code() -> Code {
        let mut bytecode = vec![JUMPDEST; 24576];
        bytecode[0] = 0x00;
        Code::from_bytecode_unchecked(bytecode.into(), H256::zero())
    }

    /// Encodes an `ACCOUNT_CODES` value the way it was written before the bitmap: the
    /// bytecode followed by an RLP list of `u32` JUMPDEST offsets.
    fn encode_code_legacy(code: &Code) -> Vec<u8> {
        let offsets: Vec<u32> = (0..code.len())
            .filter(|offset| code.is_valid_jumpdest(*offset))
            .map(|offset| offset as u32)
            .collect();
        let mut buf = Vec::new();
        code.code().encode(&mut buf);
        offsets.encode(&mut buf);
        buf
    }

    #[test]
    fn encoded_value_carries_one_bitmap_bit_per_code_byte() {
        let code = jumpdest_dense_code();
        let encoded = encode_code(&code);

        assert_eq!(encoded.len(), 6 + code.len() + code.len().div_ceil(8));
    }

    #[test]
    fn round_trip_preserves_the_bitmap() {
        let code = jumpdest_dense_code();
        let encoded = encode_code(&code);

        let (bytecode, jumpdests) = decode_bytes(&encoded).unwrap();
        assert_eq!(bytecode, code.code());
        let (rebuilt, was_legacy) = decode_jumpdests(bytecode, jumpdests).unwrap();
        assert_eq!(rebuilt.as_ref(), code.jumpdests());
        assert!(!was_legacy, "bitmap format must not be flagged legacy");
    }

    /// Values written before the bitmap SHALL still decode, with the bitmap rebuilt from
    /// the bytecode, so an existing database needs no rewriting.
    ///
    /// The expectation is built from the offsets the legacy value itself names, not from
    /// `Code::compute_jumpdests`, so this pins the rebuilt bitmap against the old format
    /// rather than against the implementation that produces it.
    #[test]
    fn legacy_offset_list_values_decode_to_the_same_bitmap() {
        for (bytecode, offsets) in [
            (vec![0x00, JUMPDEST, 0x00, JUMPDEST], vec![1u32, 3]),
            // The byte after PUSH1 is its immediate, so only offset 2 is a destination.
            (vec![PUSH1, JUMPDEST, JUMPDEST], vec![2u32]),
            (vec![0x00; 32], vec![]),
        ] {
            let code = Code::from_bytecode_unchecked(bytecode.clone().into(), H256::zero());
            let legacy = encode_code_legacy(&code);

            let mut expected = vec![0u8; bytecode.len().div_ceil(8)];
            for offset in &offsets {
                let index = usize::try_from(*offset).expect("offset fits usize");
                expected[index / 8] |= 1 << (index % 8);
            }
            // A jumpless contract stores no bitmap at all rather than an all-zero one.
            if offsets.is_empty() {
                expected.clear();
            }

            let (decoded_bytecode, jumpdests) = decode_bytes(&legacy).unwrap();
            assert_eq!(decoded_bytecode, code.code());
            let (rebuilt, was_legacy) = decode_jumpdests(decoded_bytecode, jumpdests).unwrap();
            assert_eq!(
                rebuilt.as_ref(),
                expected.as_slice(),
                "rebuilt bitmap disagrees with the legacy offsets for {bytecode:?}"
            );
            assert!(
                was_legacy,
                "legacy RLP-list format must be flagged for self-heal"
            );
        }
    }

    /// Re-inserting a cached hash SHALL NOT grow the accounted size, or the counter
    /// drifts up until the cache evicts entries that fit.
    #[test]
    fn repeated_inserts_do_not_inflate_the_accounted_size() {
        let code = jumpdest_dense_code();
        let mut cache = CodeCache::default();

        cache.insert(&code).unwrap();
        let after_first = cache.cache_size;
        for _ in 0..8 {
            cache.insert(&code).unwrap();
        }

        assert_eq!(cache.cache_size, after_first);
        assert_eq!(cache.entries.len(), 1);
    }

    /// The accounted size SHALL include the bytecode itself, so the cache honors its
    /// memory budget instead of holding orders of magnitude more than it accounts for.
    #[test]
    fn accounted_size_covers_the_bytecode_and_bitmap() {
        let code = jumpdest_dense_code();
        let mut cache = CodeCache::default();
        cache.insert(&code).unwrap();

        assert!(cache.cache_size >= (code.len() + code.jumpdests().len()) as u64);
    }

    /// Fixed xorshift seed, so a failure of an eviction test reproduces.
    const TEST_RNG_SEED: u64 = 0x9E37_79B9_7F4A_7C15;

    #[test]
    fn cache_evicts_down_to_its_budget() {
        let mut cache = CodeCache {
            max_size: 128 * 1024,
            rng: TEST_RNG_SEED,
            ..Default::default()
        };

        for i in 0..16u8 {
            let mut bytecode = vec![JUMPDEST; 24576];
            bytecode[0] = i;
            cache
                .insert(&Code::from_bytecode_unchecked(
                    bytecode.into(),
                    H256::from_low_u64_be(i.into()),
                ))
                .unwrap();
        }

        assert!(cache.cache_size <= cache.max_size);
        assert!(cache.entries.len() < 16);
    }

    fn numbered_code(i: u64, len: usize) -> Code {
        let mut bytecode = vec![JUMPDEST; len];
        bytecode[..8].copy_from_slice(&i.to_be_bytes());
        Code::from_bytecode_unchecked(bytecode.into(), H256::from_low_u64_be(i))
    }

    /// Blocks can read the same set of contracts in the same order again and again.
    /// Once that set outgrows the budget, the cache SHALL still serve most reads: an
    /// LRU serves none of them, because it evicts every entry just before its reuse.
    #[test]
    fn a_cyclic_scan_larger_than_the_budget_still_hits() {
        let entry_size = CodeCache::entry_size(&numbered_code(0, 4096));
        let mut cache = CodeCache {
            max_size: 400 * entry_size,
            rng: TEST_RNG_SEED,
            ..Default::default()
        };
        // 500 contracts against room for 400: the set is 1.25x the budget.
        let codes: Vec<Code> = (0..500).map(|i| numbered_code(i, 4096)).collect();

        let (mut hits, mut reads) = (0u64, 0u64);
        for round in 0..20 {
            for code in &codes {
                let cached = cache.get(&code.hash).unwrap();
                if round >= 5 {
                    reads += 1;
                    hits += u64::from(cached.is_some());
                }
                if cached.is_none() {
                    cache.insert(code).unwrap();
                }
            }
        }

        assert!(cache.cache_size <= cache.max_size);
        assert!(
            hits * 2 > reads,
            "only {hits} of {reads} reads hit once the scan outgrew the budget"
        );
    }

    /// The entry being inserted SHALL stay cached, since its caller reads it next.
    #[test]
    fn eviction_keeps_the_entry_being_inserted() {
        let entry_size = CodeCache::entry_size(&numbered_code(0, 4096));
        let mut cache = CodeCache {
            max_size: 2 * entry_size,
            rng: TEST_RNG_SEED,
            ..Default::default()
        };
        for i in 0..64 {
            let code = numbered_code(i, 4096);
            cache.insert(&code).unwrap();
            assert!(cache.get(&code.hash).unwrap().is_some());
            assert!(cache.cache_size <= cache.max_size);
        }
    }

    /// An entry larger than the whole budget SHALL NOT be cached, so the cache never
    /// holds more than its budget, and the entries already cached SHALL stay.
    #[test]
    fn an_entry_larger_than_the_budget_is_not_cached() {
        let small = numbered_code(0, 1024);
        let mut cache = CodeCache {
            max_size: 4 * CodeCache::entry_size(&small),
            rng: TEST_RNG_SEED,
            ..Default::default()
        };
        cache.insert(&small).unwrap();

        let large = numbered_code(1, 8192);
        assert!(CodeCache::entry_size(&large) > cache.max_size);
        cache.insert(&large).unwrap();

        assert!(!cache.contains(&large.hash));
        assert!(cache.contains(&small.hash));
        assert_eq!(cache.cache_size, CodeCache::entry_size(&small));
    }

    /// Formatting the cache SHALL NOT print the cached code: `Store` derives `Debug`, and
    /// the cache can hold its whole budget of bytecode.
    #[test]
    fn debug_output_leaves_out_the_cached_code() {
        let mut cache = CodeCache::default();
        cache.insert(&numbered_code(7, 4096)).unwrap();
        let printed = format!("{cache:?}");
        assert!(printed.len() < 200, "{printed}");
    }

    /// Small contracts SHALL be charged what an entry costs besides their bytes, so a cache
    /// full of them stays within its budget instead of holding nearly twice it.
    #[test]
    fn small_contracts_are_charged_their_entry_overhead() {
        let mut cache = CodeCache {
            max_size: 160 * 1024,
            rng: TEST_RNG_SEED,
            ..Default::default()
        };
        let mut charged = 0;
        for i in 0..4096u64 {
            let mut bytecode = vec![JUMPDEST; 32];
            bytecode[..8].copy_from_slice(&i.to_be_bytes());
            let code = Code::from_bytecode_unchecked(bytecode.into(), H256::from_low_u64_be(i));
            charged = code.size() as u64 + CODE_CACHE_ENTRY_OVERHEAD;
            cache.insert(&code).unwrap();
        }

        assert!(cache.cache_size <= cache.max_size);
        assert_eq!(cache.entries.len() as u64, cache.max_size / charged);
    }

    /// The default budget SHALL be the one the cache actually enforces, so a change to
    /// the constant cannot silently leave the cache unbounded.
    #[test]
    fn default_cache_uses_the_configured_budget() {
        assert_eq!(CodeCache::default().max_size, CODE_CACHE_MAX_SIZE);
    }

    /// A store opened with an explicit code cache size SHALL enforce it, and one opened
    /// without SHALL keep the default budget.
    #[test]
    fn store_uses_the_configured_code_cache_budget() {
        let config = StoreConfig {
            code_cache_size: Some(300 * 1024 * 1024),
            ..StoreConfig::with_rocksdb_block_cache_size(MIN_ROCKSDB_BLOCK_CACHE_SIZE_BYTES)
        };
        let store = Store::new_with_config("", EngineType::InMemory, config).unwrap();
        assert_eq!(store.code_cache_budget_bytes(), 300 * 1024 * 1024);

        let config = StoreConfig::with_rocksdb_block_cache_size(MIN_ROCKSDB_BLOCK_CACHE_SIZE_BYTES);
        let store = Store::new_with_config("", EngineType::InMemory, config).unwrap();
        assert_eq!(store.code_cache_budget_bytes(), CODE_CACHE_MAX_SIZE);
    }
}

#[cfg(test)]
mod state_history_tests {
    use super::*;
    use crate::api::tables::STATE_HISTORY;
    use crate::backend::in_memory::InMemoryBackend;
    use crate::journal::JournalEntry;
    use ethrex_common::types::{BlockBody, BlockHeader};
    use ethrex_trie::Nibbles;
    use std::time::{Duration, Instant};

    fn make_block(number: BlockNumber, parent_hash: H256, state_root: H256) -> Block {
        let header = BlockHeader {
            number,
            parent_hash,
            state_root,
            ..Default::default()
        };
        Block::new(header, BlockBody::default())
    }

    /// Polls `STATE_HISTORY` for an entry at the given block number, up to `timeout`.
    /// The trie worker commits to disk asynchronously after `store_block_updates`
    /// returns, so a small wait window is required.
    fn await_journal_entry(
        backend: &Arc<dyn StorageBackend>,
        block_number: BlockNumber,
        timeout: Duration,
    ) -> Option<Vec<u8>> {
        let key = block_number.to_be_bytes();
        let deadline = Instant::now() + timeout;
        loop {
            let read = backend.begin_read().ok()?;
            if let Ok(Some(v)) = read.get(STATE_HISTORY, &key) {
                return Some(v);
            }
            if Instant::now() >= deadline {
                return None;
            }
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    /// Asserts no STATE_HISTORY entry materializes for `block_number` within the
    /// given window. Polls repeatedly; if an entry appears at any poll, fails
    /// loudly. Absence at every poll over the full window counts as verified.
    /// More robust than a single fixed sleep under CI load.
    fn assert_no_journal_entry(backend: &Arc<dyn StorageBackend>, block_number: BlockNumber) {
        let window = Duration::from_millis(500);
        let key = block_number.to_be_bytes();
        let deadline = Instant::now() + window;
        loop {
            let read = backend.begin_read().expect("read view");
            let v = read.get(STATE_HISTORY, &key).expect("get");
            assert!(
                v.is_none(),
                "expected no STATE_HISTORY entry for block {block_number}, got {v:?}"
            );
            if Instant::now() >= deadline {
                return;
            }
            std::thread::sleep(Duration::from_millis(20));
        }
    }

    /// Live commits gate on the canonical safe-commit root cell (see `get_commitable`),
    /// which only advances via forkchoice. We simulate that by calling
    /// `set_safe_commit_root` to the parent block's state root before storing the next
    /// block: storing block N+1 then commits block N's layer to disk, producing one
    /// journal entry per committed block. We verify entries for block 1 and block 2.
    #[test]
    fn journal_entry_written_per_block_in_regular_mode() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        let state_root_1 = H256::repeat_byte(0x11);
        let block1 = make_block(1, H256::zero(), state_root_1);
        let block1_hash = block1.hash();
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![(Nibbles::from_raw(&[0x00, 0x01], false), vec![0xab, 0xcd])],
                storage_updates: vec![],
                blocks: vec![block1],
                receipts: vec![],
                code_updates: vec![],
                commit_depth: None,
                wait_for_flush: false,
            })
            .unwrap();

        // Advance the safe-commit root to block 1's state root, then store block 2:
        // the canonical gate now finds block 1's layer committable and flushes it.
        store.set_safe_commit_root(state_root_1).unwrap();
        let state_root_2 = H256::repeat_byte(0x22);
        let block2 = make_block(2, block1_hash, state_root_2);
        let block2_hash = block2.hash();
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![(Nibbles::from_raw(&[0x00, 0x02], false), vec![0xef, 0x11])],
                storage_updates: vec![],
                blocks: vec![block2],
                receipts: vec![],
                code_updates: vec![],
                commit_depth: None,
                wait_for_flush: false,
            })
            .unwrap();

        let bytes = await_journal_entry(&backend, 1, Duration::from_secs(2))
            .expect("STATE_HISTORY entry for block 1 should appear after block 2 commits it");
        let entry = JournalEntry::decode(&bytes).unwrap();
        assert_eq!(entry.block_hash, block1_hash);
        assert_eq!(entry.parent_state_root, H256::zero());
        assert!(!entry.account_trie_diff.is_empty());
        let (path, prev) = &entry.account_trie_diff[0];
        assert_eq!(prev, &None, "first-time write means previous value is None");
        assert!(path.len() < 65);

        // Advance the safe-commit root to block 2's state root, then store block 3:
        // block 2's layer is now committable.
        store.set_safe_commit_root(state_root_2).unwrap();
        let state_root_3 = H256::repeat_byte(0x33);
        let block3 = make_block(3, block2_hash, state_root_3);
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![(Nibbles::from_raw(&[0x00, 0x03], false), vec![0x77])],
                storage_updates: vec![],
                blocks: vec![block3],
                receipts: vec![],
                code_updates: vec![],
                commit_depth: None,
                wait_for_flush: false,
            })
            .unwrap();

        let bytes = await_journal_entry(&backend, 2, Duration::from_secs(2))
            .expect("STATE_HISTORY entry for block 2 should appear after block 3 commits it");
        let entry = JournalEntry::decode(&bytes).unwrap();
        assert_eq!(entry.block_hash, block2_hash);
        assert_eq!(entry.parent_state_root, state_root_1);
        assert!(!entry.account_trie_diff.is_empty());
    }

    /// A depth-gated commit that is NOT in batch mode SHALL journal, and its entries
    /// SHALL carry the committed layer's own identity and pre-image.
    ///
    /// `commit_depth: Some(d)` together with `wait_for_flush: false` — the startup
    /// regeneration and sync/import tail paths — is a combination that did not exist while
    /// the commit gate and the ack timing were a single flag, so no other test covers it.
    /// Journaling follows `wait_for_flush`, so deriving it from `commit_depth.is_some()`
    /// instead would silently stop journaling on exactly these paths; this test fails in
    /// that case.
    #[test]
    fn depth_gated_per_block_commit_journals_with_correct_pre_image() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // One shared key rewritten every block, so pre-images are checkable.
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);
        let mut prev_hash = H256::zero();
        let mut hashes = vec![H256::zero()];
        let mut roots = vec![H256::zero()];
        for n in 1..=6u64 {
            let state_root = H256::repeat_byte(0xc0 | (n as u8));
            let block = make_block(n, prev_hash, state_root);
            prev_hash = block.hash();
            hashes.push(prev_hash);
            roots.push(state_root);
            store
                .store_block_updates(UpdateBatch {
                    account_updates: vec![(shared.clone(), vec![0xb0 | (n as u8)])],
                    storage_updates: vec![],
                    blocks: vec![block],
                    receipts: vec![],
                    code_updates: vec![],
                    commit_depth: Some(3),
                    wait_for_flush: false,
                })
                .unwrap();
        }

        let bytes = await_journal_entry(&backend, 1, Duration::from_secs(2))
            .expect("a depth-gated per-block commit must journal");
        let entry = JournalEntry::decode(&bytes).unwrap();
        assert_eq!(
            entry.block_hash, hashes[1],
            "entry 1 carries block 1's identity"
        );
        assert_eq!(entry.parent_state_root, H256::zero());
        assert_eq!(
            entry.account_trie_diff[0].1, None,
            "block 1 first-writes the key, so its pre-image is absence"
        );

        let bytes =
            await_journal_entry(&backend, 2, Duration::from_secs(2)).expect("entry for block 2");
        let entry = JournalEntry::decode(&bytes).unwrap();
        assert_eq!(
            entry.block_hash, hashes[2],
            "entry 2 carries block 2's identity"
        );
        assert_eq!(
            entry.parent_state_root, roots[1],
            "entry 2's parent_state_root is block 1's state root"
        );
        assert_eq!(
            entry.account_trie_diff[0].1,
            Some(vec![0xb0 | 1u8]),
            "block 2's pre-image is the value block 1 wrote"
        );
    }

    /// The bespoke batch path SHALL skip the journal entirely. To actually exercise the
    /// gating we push enough batches to trigger a commit under
    /// `BATCH_COMMIT_THRESHOLD = 4`, then verify no STATE_HISTORY entry materializes
    /// despite the commit happening.
    ///
    /// That path is `wait_for_flush: true` with `commit_depth: Some(BATCH_COMMIT_THRESHOLD)`
    /// — the combination the single `batch_mode` flag used to stand for. Journaling follows
    /// `wait_for_flush`, so the depth-gated per-block paths are not covered here.
    #[test]
    fn journal_skipped_in_batch_mode() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        let mut prev_hash = H256::zero();
        for n in 1..=5u64 {
            let state_root = H256::repeat_byte(0xa0 | (n as u8));
            let block = make_block(n, prev_hash, state_root);
            prev_hash = block.hash();
            store
                .store_block_updates(UpdateBatch {
                    account_updates: vec![(
                        Nibbles::from_raw(&[n as u8], false),
                        vec![0xde, 0xad, n as u8],
                    )],
                    storage_updates: vec![],
                    blocks: vec![block],
                    receipts: vec![],
                    code_updates: vec![],
                    commit_depth: Some(BATCH_COMMIT_THRESHOLD),
                    wait_for_flush: true,
                })
                .unwrap();
        }

        for n in 1..=5u64 {
            assert_no_journal_entry(&backend, n);
        }
    }

    /// Storage trie updates SHALL appear in `storage_trie_diff` (not
    /// `account_trie_diff`), with their on-disk keys as written.
    #[test]
    fn journal_storage_updates_appear_in_storage_diff() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        let account_hash_a = H256::repeat_byte(0xa0);
        let account_hash_b = H256::repeat_byte(0xb0);

        let state_root_1 = H256::repeat_byte(0x33);
        let block1 = make_block(1, H256::zero(), state_root_1);
        let block1_hash = block1.hash();
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![],
                storage_updates: vec![
                    (
                        account_hash_a,
                        vec![(Nibbles::from_raw(&[0x05], false), vec![0x01])],
                    ),
                    (
                        account_hash_b,
                        vec![(Nibbles::from_raw(&[0x06], false), vec![0x02])],
                    ),
                ],
                blocks: vec![block1],
                receipts: vec![],
                code_updates: vec![],
                commit_depth: None,
                wait_for_flush: false,
            })
            .unwrap();

        // Advance the safe-commit root to block 1's state root, then store block 2:
        // block 1's layer is now committable and flushes to disk.
        store.set_safe_commit_root(state_root_1).unwrap();
        let state_root_2 = H256::repeat_byte(0x44);
        let block2 = make_block(2, block1_hash, state_root_2);
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![(Nibbles::from_raw(&[0xee], false), vec![0xff])],
                storage_updates: vec![],
                blocks: vec![block2],
                receipts: vec![],
                code_updates: vec![],
                commit_depth: None,
                wait_for_flush: false,
            })
            .unwrap();

        let bytes = await_journal_entry(&backend, 1, Duration::from_secs(2))
            .expect("STATE_HISTORY entry for block 1");
        let entry = JournalEntry::decode(&bytes).unwrap();
        assert_eq!(entry.block_hash, block1_hash);
        assert_eq!(
            entry.storage_trie_diff.len(),
            2,
            "two distinct account hashes must produce two storage_trie entries"
        );
        for (_key, prev) in &entry.storage_trie_diff {
            assert_eq!(prev, &None, "first-time storage write has None pre-image");
        }
        assert!(entry.account_trie_diff.is_empty());
    }

    fn seed_journal_entries(backend: &Arc<dyn StorageBackend>, block_numbers: &[BlockNumber]) {
        let mut tx = backend.begin_write().unwrap();
        for n in block_numbers {
            let entry = JournalEntry {
                block_hash: H256::repeat_byte(*n as u8),
                parent_state_root: H256::zero(),
                account_trie_diff: vec![(vec![*n as u8], None)],
                storage_trie_diff: vec![],
                account_flat_diff: vec![],
                storage_flat_diff: vec![],
            };
            tx.put(STATE_HISTORY, &n.to_be_bytes(), &entry.encode())
                .unwrap();
        }
        tx.commit().unwrap();
    }

    fn journal_entry_exists(backend: &Arc<dyn StorageBackend>, block_number: BlockNumber) -> bool {
        backend
            .begin_read()
            .unwrap()
            .get(STATE_HISTORY, &block_number.to_be_bytes())
            .unwrap()
            .is_some()
    }

    /// Finality advance SHALL prune every STATE_HISTORY entry at or below the
    /// new finalized number, in the same atomic txn as the finalized-number update.
    #[tokio::test]
    async fn finality_advance_prunes_journal_below_boundary() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        seed_journal_entries(&backend, &(1..=10).collect::<Vec<_>>());
        for n in 1..=10 {
            assert!(journal_entry_exists(&backend, n), "seed entry {n} present");
        }

        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(5))
            .await
            .unwrap();

        for n in 1..=5 {
            assert!(
                !journal_entry_exists(&backend, n),
                "entry {n} should have been pruned (<= finalized)"
            );
        }
        for n in 6..=10 {
            assert!(
                journal_entry_exists(&backend, n),
                "entry {n} should remain (> finalized)"
            );
        }
    }

    /// Forkchoice updates that don't advance finalized SHALL NOT prune the journal.
    #[tokio::test]
    async fn finality_no_op_does_not_prune() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(5))
            .await
            .unwrap();

        seed_journal_entries(&backend, &(6..=10).collect::<Vec<_>>());

        // FCU re-asserting finalized = 5: must not prune anything.
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(5))
            .await
            .unwrap();

        for n in 6..=10 {
            assert!(
                journal_entry_exists(&backend, n),
                "entry {n} should still exist after no-op finality update"
            );
        }

        // FCU with finalized = None: also a no-op for pruning.
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, None)
            .await
            .unwrap();

        for n in 6..=10 {
            assert!(
                journal_entry_exists(&backend, n),
                "entry {n} should still exist when finalized is None"
            );
        }
    }

    /// A malformed (wrong-length) `FinalizedBlockNumber` value SHALL surface as a
    /// hard error rather than a silent fallback to 0, which would over-prune the
    /// journal on the next FCU.
    #[tokio::test]
    async fn malformed_finalized_returns_error_not_silent_zero() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // Plant a 4-byte (instead of 8-byte) FinalizedBlockNumber value.
        let mut tx = backend.begin_write().unwrap();
        let finalized_key = chain_data_key(ChainDataIndex::FinalizedBlockNumber);
        tx.put(CHAIN_DATA, &finalized_key, &[0u8, 0, 0, 0]).unwrap();
        tx.commit().unwrap();

        seed_journal_entries(&backend, &(1..=5).collect::<Vec<_>>());

        let err = store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(3))
            .await
            .expect_err("malformed finalized must surface as an error");
        let msg = format!("{err}");
        assert!(
            msg.contains("FinalizedBlockNumber has unexpected length"),
            "expected length-mismatch error, got: {msg}"
        );

        // Journal must not have been pruned.
        for n in 1..=5 {
            assert!(
                journal_entry_exists(&backend, n),
                "entry {n} must NOT be pruned when finalized read failed"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Deep-reorg primitive tests.
    // -----------------------------------------------------------------------

    /// `highest_state_history_block_number` SHALL return the max key present in
    /// `STATE_HISTORY`, or `None` when the table is empty.
    #[tokio::test]
    async fn highest_state_history_block_number_finds_max() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        assert_eq!(store.highest_state_history_block_number().unwrap(), None);

        seed_journal_entries(&backend, &[3, 7, 5, 11, 8]);
        assert_eq!(
            store.highest_state_history_block_number().unwrap(),
            Some(11),
            "max over present entries"
        );
    }

    /// The interrupted-batch recovery at startup identifies the block whose state is
    /// on disk from the newest `STATE_HISTORY` entry, so the entry's block hash must
    /// read back exactly as journaled and an absent entry must read as `None`.
    #[tokio::test]
    async fn journaled_block_hash_reads_back() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        seed_journal_entries(&backend, &[3, 11]);
        assert_eq!(
            store.get_state_history_block_hash(11).unwrap(),
            Some(H256::repeat_byte(11))
        );
        assert_eq!(store.get_state_history_block_hash(4).unwrap(), None);
    }

    /// `flushed_upto` is a durability floor: `advance_flushed_upto` raises it and
    /// ignores a value at or below the current marker.
    #[tokio::test]
    async fn advance_flushed_upto_only_moves_forward() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend,
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        store.advance_flushed_upto(10).unwrap();
        assert_eq!(store.read_flushed_upto().unwrap(), 10);
        store.advance_flushed_upto(5).unwrap();
        assert_eq!(store.read_flushed_upto().unwrap(), 10);
        store.advance_flushed_upto(12).unwrap();
        assert_eq!(store.read_flushed_upto().unwrap(), 12);
    }

    /// `lowest_state_history_block_number` SHALL return the min key present in
    /// `STATE_HISTORY`, or `None` when the table is empty. Phase 2's cap fallback
    /// depends on this when no finalized hash is known.
    #[tokio::test]
    async fn lowest_state_history_block_number_finds_min() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        assert_eq!(store.lowest_state_history_block_number().unwrap(), None);

        seed_journal_entries(&backend, &[7, 3, 11, 5, 8]);
        assert_eq!(
            store.lowest_state_history_block_number().unwrap(),
            Some(3),
            "min over present entries"
        );
    }

    /// `install_overlay_for_reorg` SHALL atomically swap the layer cache for a
    /// fresh empty one with the overlay installed; layer-cache hits in the OLD
    /// cache MUST NOT survive the swap.
    #[tokio::test]
    async fn install_overlay_replaces_cache_with_fresh_one() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            4,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        seed_journal_entries(&backend, &[3, 4]);

        // Pre-populate the cache with a sentinel layer to verify the swap discards it.
        {
            let mut guard = store.trie_cache.write().unwrap();
            let mut updated = (**guard).clone();
            updated.put_batch(
                H256::zero(),
                H256::repeat_byte(0xff),
                42,
                H256::repeat_byte(0x42),
                vec![],
            );
            *guard = Arc::new(updated);
        }
        assert!(
            store
                .is_state_in_layer_cache(H256::repeat_byte(0xff))
                .unwrap()
        );

        store
            .install_overlay_for_reorg(4, 3, |_| None)
            .expect("install must succeed");

        assert!(
            !store
                .is_state_in_layer_cache(H256::repeat_byte(0xff))
                .unwrap(),
            "old cache layer must be gone after swap"
        );
        let cache = store.trie_cache.read().unwrap().clone();
        assert!(cache.overlay().is_some(), "overlay must be installed");
        assert_eq!(
            cache.commit_threshold(),
            4,
            "fresh cache must inherit threshold"
        );
    }

    /// `install_overlay_for_reorg` SHALL leave the existing cache intact when
    /// overlay construction fails (missing journal entry).
    #[tokio::test]
    async fn install_overlay_failure_preserves_cache() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            4,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // Only seed block 5; constructor will walk down through 4 and 3 and fail.
        seed_journal_entries(&backend, &[5]);
        {
            let mut guard = store.trie_cache.write().unwrap();
            let mut updated = (**guard).clone();
            updated.put_batch(
                H256::zero(),
                H256::repeat_byte(0xaa),
                7,
                H256::repeat_byte(0x77),
                vec![],
            );
            *guard = Arc::new(updated);
        }
        assert!(
            store
                .is_state_in_layer_cache(H256::repeat_byte(0xaa))
                .unwrap()
        );

        let err = store
            .install_overlay_for_reorg(5, 3, |_| None)
            .expect_err("missing entries 3 and 4 must abort");
        let msg = format!("{err}");
        assert!(
            msg.contains("overlay construction failed"),
            "error should explain construction failure: {msg}"
        );

        // Cache must be intact: the sentinel layer must still be there.
        assert!(
            store
                .is_state_in_layer_cache(H256::repeat_byte(0xaa))
                .unwrap(),
            "cache must survive a failed overlay install"
        );
        let cache = store.trie_cache.read().unwrap().clone();
        assert!(cache.overlay().is_none(), "no overlay should be installed");
    }

    /// `abort_reorg` SHALL discard both the overlay and any partial new-chain
    /// layers, restoring the cache to a fresh empty state with the same
    /// commit threshold.
    #[tokio::test]
    async fn abort_reorg_resets_cache_to_fresh() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            4,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        seed_journal_entries(&backend, &[3, 4]);
        store.install_overlay_for_reorg(4, 3, |_| None).unwrap();
        // Simulate partial new-chain layer.
        {
            let mut guard = store.trie_cache.write().unwrap();
            let mut updated = (**guard).clone();
            updated.put_batch(
                H256::zero(),
                H256::repeat_byte(0xbb),
                4,
                H256::repeat_byte(0xb4),
                vec![],
            );
            *guard = Arc::new(updated);
        }
        assert!(
            store
                .is_state_in_layer_cache(H256::repeat_byte(0xbb))
                .unwrap()
        );

        store.abort_reorg().unwrap();

        assert!(
            !store
                .is_state_in_layer_cache(H256::repeat_byte(0xbb))
                .unwrap(),
            "partial new-chain layer must be discarded"
        );
        let cache = store.trie_cache.read().unwrap().clone();
        assert!(cache.overlay().is_none(), "overlay must be cleared");
        assert_eq!(
            cache.commit_threshold(),
            4,
            "fresh cache must inherit threshold"
        );
    }

    /// `clear_reorg_overlay` SHALL remove an installed overlay and SHALL be
    /// idempotent (safe to call when no overlay is installed).
    #[tokio::test]
    async fn clear_reorg_overlay_removes_overlay_and_is_idempotent() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            4,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // Idempotent when no overlay is installed.
        store.clear_reorg_overlay().unwrap();
        assert!(store.trie_cache.read().unwrap().overlay().is_none());

        seed_journal_entries(&backend, &[7]);
        store.install_overlay_for_reorg(7, 7, |_| None).unwrap();
        assert!(store.trie_cache.read().unwrap().overlay().is_some());

        store.clear_reorg_overlay().unwrap();
        assert!(store.trie_cache.read().unwrap().overlay().is_none());

        // Calling again is still a no-op.
        store.clear_reorg_overlay().unwrap();
        assert!(store.trie_cache.read().unwrap().overlay().is_none());
    }

    /// Regression test: journal entries written while the FKV generator
    /// was running lack flat-KV pre-images for keys past the generator frontier,
    /// and the generator's own flat-KV writes are never journaled. While an
    /// overlay serves the read's state root, every flat-KV fast path must be
    /// disabled so reads fall back to the (always journaled) trie nodes;
    /// otherwise they serve the generator's stale values for the chain being
    /// reorged away. Roots the overlay does not serve must keep the fast path.
    #[tokio::test]
    async fn deep_reorg_overlay_disables_flat_kv_fast_paths() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());

        // Simulate a finished FKV generation BEFORE the Store boots, so its
        // in-memory frontier starts at [0xff; 64] and every path counts as
        // "computed" (the Store expands the durable [0xff] sentinel at open).
        {
            let mut tx = backend.begin_write().unwrap();
            tx.put(MISC_VALUES, "last_written".as_bytes(), &[0xff])
                .unwrap();
            tx.commit().unwrap();
        }

        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // The pivot state is empty, so the correct value for every read at the
        // pivot root is None. The old chain created the account/slot after the
        // pivot; the generator then wrote their flat-KV leaves against the old
        // chain — writes that are never journaled.
        let address = Address::from_low_u64_be(0xbeef);
        let account_hash = hash_address_fixed(&address);
        let slot = H256::from_low_u64_be(1);
        let slot_hash = hash_key_fixed(&slot);
        let generator_account = AccountState {
            nonce: 7,
            balance: U256::from(999u64),
            storage_root: H256::zero(),
            code_hash: H256::zero(),
        };
        let generator_slot_value = U256::from(42u64);
        {
            let mut tx = backend.begin_write().unwrap();
            tx.put(
                ACCOUNT_FLATKEYVALUE,
                &Nibbles::from_bytes(account_hash.as_bytes()).into_vec(),
                &generator_account.encode_to_vec(),
            )
            .unwrap();
            tx.put(
                STORAGE_FLATKEYVALUE,
                &apply_prefix(Some(account_hash), Nibbles::from_bytes(&slot_hash)).into_vec(),
                &generator_slot_value.encode_to_vec(),
            )
            .unwrap();
            tx.commit().unwrap();
        }

        // Journal entries for blocks 10 and 11 above the pivot (block 9), as
        // written DURING generation: flat diffs are empty because the keys were
        // past the frontier at commit time (the incomplete-journal hole). The pivot's state
        // root is the empty trie.
        let pivot_root = EMPTY_TRIE_HASH;
        {
            let mut tx = backend.begin_write().unwrap();
            for (n, parent_root) in [(10u64, pivot_root), (11, H256::repeat_byte(0x10))] {
                let entry = JournalEntry {
                    block_hash: H256::repeat_byte(n as u8),
                    parent_state_root: parent_root,
                    account_trie_diff: vec![],
                    storage_trie_diff: vec![],
                    account_flat_diff: vec![],
                    storage_flat_diff: vec![],
                };
                tx.put(STATE_HISTORY, &n.to_be_bytes(), &entry.encode())
                    .unwrap();
            }
            tx.commit().unwrap();
        }

        store
            .install_overlay_for_reorg(11, 10, |_| None)
            .expect("overlay install must succeed");

        // Reads at the pivot root must return the pivot state (account and slot
        // absent), NOT the generator's stale flat-KV values.
        let accounts = store
            .get_account_states_batch_by_root(pivot_root, &[address])
            .unwrap();
        assert_eq!(
            accounts,
            vec![None],
            "batch account read must not serve the generator's stale flat-KV value"
        );

        let slot_value = store
            .get_storage_at_root(pivot_root, address, slot)
            .unwrap();
        assert_eq!(
            slot_value, None,
            "storage read must not serve the generator's stale flat-KV value"
        );

        let batched_slots = store
            .get_storage_values_batch_by_root(
                pivot_root,
                &[(hash_address_fixed(&address), EMPTY_TRIE_HASH, slot)],
            )
            .unwrap();
        assert_eq!(
            batched_slots,
            vec![None],
            "batch storage read must not serve the generator's stale flat-KV value"
        );

        let state_trie = store.open_state_trie(pivot_root).unwrap();
        assert_eq!(
            state_trie.get(account_hash.as_bytes()).unwrap(),
            None,
            "trie read must not serve the generator's stale flat-KV value"
        );

        // Outside the overlay window the fast paths are untouched: a root the
        // overlay does not serve still reads flat-KV straight from disk.
        let unserved_root = H256::repeat_byte(0x99);
        let accounts = store
            .get_account_states_batch_by_root(unserved_root, &[address])
            .unwrap();
        assert_eq!(
            accounts,
            vec![Some(generator_account)],
            "unserved roots must keep the flat-KV fast path"
        );
        let slot_value = store
            .get_storage_at_root(unserved_root, address, slot)
            .unwrap();
        assert_eq!(
            slot_value,
            Some(generator_slot_value),
            "unserved roots must keep the flat-KV fast path"
        );

        let batched_slots = store
            .get_storage_values_batch_by_root(
                unserved_root,
                &[(hash_address_fixed(&address), EMPTY_TRIE_HASH, slot)],
            )
            .unwrap();
        assert_eq!(
            batched_slots,
            vec![Some(generator_slot_value)],
            "unserved roots must keep the flat-KV fast path"
        );
    }

    /// Adversarial regression for the multi-layer reconciliation finding: with an
    /// overlay installed, a commit targeting a root SEVERAL layers above the pivot
    /// must commit ONLY the bottom layer (the pivot tip `T`) per pass. Before the
    /// fix this swept every layer at once: the `debug_assert!(len == 1)` panicked in
    /// debug builds, and in release the upper layers' journal entries recorded
    /// pre-images against the OLD-chain disk state instead of the new-chain/bridge
    /// state — silent corruption for a future unwind.
    #[tokio::test]
    async fn overlay_backed_commit_only_commits_bottom_layer_per_pass() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            4,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        // Pivot at block 9's root; journal entries for blocks 10 and 11 (the old
        // chain above the pivot). The overlay serves the pivot root.
        let pivot_root = H256::repeat_byte(0x09);
        {
            let mut tx = backend.begin_write().unwrap();
            for (n, parent_root) in [(10u64, pivot_root), (11, H256::repeat_byte(0x0a))] {
                let entry = JournalEntry {
                    block_hash: H256::repeat_byte(n as u8),
                    parent_state_root: parent_root,
                    account_trie_diff: vec![(vec![0x00, n as u8], None)],
                    storage_trie_diff: vec![],
                    account_flat_diff: vec![],
                    storage_flat_diff: vec![],
                };
                tx.put(STATE_HISTORY, &n.to_be_bytes(), &entry.encode())
                    .unwrap();
            }
            tx.commit().unwrap();
        }
        store.install_overlay_for_reorg(11, 10, |_| None).unwrap();

        // Three new-chain layers above the pivot: blocks 10, 11, 12.
        let roots: Vec<H256> = (10..=12u8).map(H256::repeat_byte).collect();
        {
            let mut guard = store.trie_cache.write().unwrap();
            let mut updated = (**guard).clone();
            let mut parent = pivot_root;
            for (i, root) in roots.iter().enumerate() {
                let n = 10 + i as u64;
                updated.put_batch(
                    parent,
                    *root,
                    n,
                    H256::repeat_byte(n as u8),
                    vec![(Nibbles::from_raw(&[0x01, n as u8], false), vec![n as u8])],
                );
                parent = *root;
            }
            *guard = Arc::new(updated);
        }

        // Commit targeting the TOP layer (3 deep). With the overlay installed this
        // MUST reduce to the bottom layer only.
        let trie = store.trie_cache.read().unwrap().clone();
        commit_to_disk(
            store.backend.as_ref(),
            &store.flatkeyvalue_control_tx,
            &store.trie_cache,
            &trie,
            roots[2],
            false,
        )
        .expect("bottom-layer commit must succeed (no debug_assert panic)");

        // Only the bottom layer (block 10) is committed and pruned; 11 and 12 stay resident.
        assert!(
            !store.is_state_in_layer_cache(roots[0]).unwrap(),
            "bottom layer must be committed and pruned"
        );
        assert!(
            store.is_state_in_layer_cache(roots[1]).unwrap()
                && store.is_state_in_layer_cache(roots[2]).unwrap(),
            "upper layers must remain resident after the bottom-layer-only commit"
        );
        // The overlay was consumed by the reconciliation commit.
        assert!(
            store.trie_cache.read().unwrap().overlay().is_none(),
            "reconciliation must clear the overlay"
        );
        // Journal: an entry exists for block 10, none yet for 11/12.
        assert!(
            journal_entry_exists(&backend, 10),
            "bottom layer T must be journaled"
        );
        assert!(!journal_entry_exists(&backend, 11));
        assert!(!journal_entry_exists(&backend, 12));

        // The reconciliation fold MUST land in the bottom layer's journal entry:
        // the overlay's bridge keys (untouched by the layer) appear with their
        // old-chain pre-images. This is what block-number-keyed reconciliation
        // matching loses when the bottom layer sits above `to_block`.
        let entry_bytes = backend
            .begin_read()
            .unwrap()
            .get(STATE_HISTORY, &10u64.to_be_bytes())
            .unwrap()
            .expect("journal entry for block 10");
        let entry = JournalEntry::decode(&entry_bytes).unwrap();
        let diff_keys: Vec<&Vec<u8>> = entry.account_trie_diff.iter().map(|(k, _)| k).collect();
        assert!(
            diff_keys.contains(&&vec![0x00, 10]) && diff_keys.contains(&&vec![0x00, 11]),
            "bridge keys from the overlay must be folded into T's journal entry, got {diff_keys:?}"
        );

        // Second pass: with the overlay gone the remaining backlog commits normally.
        let trie = store.trie_cache.read().unwrap().clone();
        commit_to_disk(
            store.backend.as_ref(),
            &store.flatkeyvalue_control_tx,
            &store.trie_cache,
            &trie,
            roots[2],
            false,
        )
        .unwrap();
        assert!(!store.is_state_in_layer_cache(roots[1]).unwrap());
        assert!(!store.is_state_in_layer_cache(roots[2]).unwrap());
        assert!(journal_entry_exists(&backend, 11));
        assert!(journal_entry_exists(&backend, 12));
    }

    /// Adversarial regression for the journal-pruning race: while a deep-reorg
    /// apply pass holds the pause flag, a finality advance must NOT prune
    /// STATE_HISTORY (a concurrent `Overlay::from_journal` reads entries with no
    /// snapshot isolation). Pruning must catch up once the pause is released.
    #[tokio::test]
    async fn journal_pruning_pauses_during_reorg_and_catches_up_after() {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();

        seed_journal_entries(&backend, &(1..=5).collect::<Vec<_>>());

        // Paused: finality advance to 3 must not prune anything.
        store.set_journal_pruning_paused(true);
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(3))
            .await
            .unwrap();
        for n in 1..=5 {
            assert!(
                journal_entry_exists(&backend, n),
                "entry {n} must survive pruning while the reorg pause is held"
            );
        }

        // Released: the next advance prunes from the lowest surviving entry.
        store.set_journal_pruning_paused(false);
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(4))
            .await
            .unwrap();
        for n in 1..=4 {
            assert!(
                !journal_entry_exists(&backend, n),
                "entry {n} must be pruned once the pause is released"
            );
        }
        assert!(journal_entry_exists(&backend, 5));
    }

    /// `(start, end)` block numbers of each STATE_HISTORY `delete_range`, in order.
    type RecordedRanges = Arc<Mutex<Vec<(BlockNumber, BlockNumber)>>>;

    /// Wraps a backend and records every `delete_range` issued on STATE_HISTORY, so
    /// tests can check which ranges finality pruning writes, not just its effect.
    #[derive(Debug)]
    struct RangeDeleteRecorder {
        inner: Arc<dyn StorageBackend>,
        ranges: RecordedRanges,
    }

    struct RecordingWriteBatch {
        inner: Box<dyn StorageWriteBatch + 'static>,
        ranges: RecordedRanges,
    }

    fn block_number_key(key: &[u8]) -> BlockNumber {
        BlockNumber::from_be_bytes(key.try_into().expect("8-byte STATE_HISTORY key"))
    }

    impl StorageBackend for RangeDeleteRecorder {
        fn clear_table(&self, table: &'static str) -> Result<(), StoreError> {
            self.inner.clear_table(table)
        }
        fn begin_read(&self) -> Result<Arc<dyn StorageReadView>, StoreError> {
            self.inner.begin_read()
        }
        fn begin_write(&self) -> Result<Box<dyn StorageWriteBatch + 'static>, StoreError> {
            Ok(Box::new(RecordingWriteBatch {
                inner: self.inner.begin_write()?,
                ranges: self.ranges.clone(),
            }))
        }
        fn begin_locked(
            &self,
            table_name: &'static str,
        ) -> Result<Box<dyn crate::api::StorageLockedView + 'static>, StoreError> {
            self.inner.begin_locked(table_name)
        }
        fn create_checkpoint(&self, path: &std::path::Path) -> Result<(), StoreError> {
            self.inner.create_checkpoint(path)
        }
    }

    impl StorageWriteBatch for RecordingWriteBatch {
        fn put_batch(
            &mut self,
            table: &'static str,
            batch: Vec<(Vec<u8>, Vec<u8>)>,
        ) -> Result<(), StoreError> {
            self.inner.put_batch(table, batch)
        }
        fn delete(&mut self, table: &'static str, key: &[u8]) -> Result<(), StoreError> {
            self.inner.delete(table, key)
        }
        fn delete_range(
            &mut self,
            table: &'static str,
            start: &[u8],
            end: &[u8],
        ) -> Result<(), StoreError> {
            if table == STATE_HISTORY {
                self.ranges
                    .lock()
                    .unwrap()
                    .push((block_number_key(start), block_number_key(end)));
            }
            self.inner.delete_range(table, start, end)
        }
        fn merge(
            &mut self,
            table: &'static str,
            key: &[u8],
            operand: &[u8],
        ) -> Result<(), StoreError> {
            self.inner.merge(table, key, operand)
        }
        fn commit(&mut self) -> Result<(), StoreError> {
            self.inner.commit()
        }
    }

    fn recording_store() -> (
        Store,
        Arc<dyn StorageBackend>,
        RecordedRanges,
        tempfile::TempDir,
    ) {
        let ranges = Arc::new(Mutex::new(Vec::new()));
        let backend: Arc<dyn StorageBackend> = Arc::new(RangeDeleteRecorder {
            inner: Arc::new(InMemoryBackend::open().unwrap()),
            ranges: ranges.clone(),
        });
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();
        (store, backend, ranges, dir)
    }

    /// Finality pruning SHALL start each range at the lowest surviving entry, so the
    /// ranges it writes never overlap. Ranges anchored at 0 overlap pairwise, and
    /// RocksDB fragments unflushed range tombstones in time and memory quadratic in
    /// how many overlap.
    #[tokio::test]
    async fn finality_pruning_writes_disjoint_ranges() {
        let (store, backend, ranges, _dir) = recording_store();
        seed_journal_entries(&backend, &[3, 7, 9, 12]);

        for finalized in [5, 8, 10] {
            store
                .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(finalized))
                .await
                .unwrap();
        }

        assert_eq!(*ranges.lock().unwrap(), vec![(3, 6), (7, 9), (9, 11)]);
        assert!(journal_entry_exists(&backend, 12));
    }

    /// A finality advance that expires no entry SHALL write no range delete at all,
    /// so a chain whose entries are sparse does not accumulate one tombstone per block.
    #[tokio::test]
    async fn finality_advance_without_expired_entries_writes_no_range() {
        let (store, backend, ranges, _dir) = recording_store();

        // Empty STATE_HISTORY: nothing to prune.
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(5))
            .await
            .unwrap();

        // Every surviving entry is above the new finality boundary.
        seed_journal_entries(&backend, &[20]);
        store
            .forkchoice_update_inner(vec![], 100, H256::zero(), None, Some(10))
            .await
            .unwrap();

        assert!(ranges.lock().unwrap().is_empty());
        assert!(journal_entry_exists(&backend, 20));
    }

    /// Finality pruning SHALL write disjoint ranges when entries keep landing below
    /// finality, as on an L2: the commit worker writes the entry for
    /// `head - DB_COMMIT_THRESHOLD` while every FCU finalizes `head`.
    #[tokio::test]
    async fn finality_pruning_stays_disjoint_when_entries_land_below_finality() {
        let (store, backend, ranges, _dir) = recording_store();
        let lag = DB_COMMIT_THRESHOLD as u64;
        for head in 1..=(lag + 50) {
            if head > lag {
                seed_journal_entries(&backend, &[head - lag]);
            }
            store
                .forkchoice_update_inner(vec![], head, H256::zero(), None, Some(head))
                .await
                .unwrap();
        }

        let ranges = ranges.lock().unwrap();
        assert_eq!(ranges.len(), 50);
        for pair in ranges.windows(2) {
            assert!(pair[0].1 <= pair[1].0, "overlapping ranges {pair:?}");
        }
    }

    fn journal_store() -> (Store, Arc<dyn StorageBackend>, tempfile::TempDir) {
        let backend: Arc<dyn StorageBackend> = Arc::new(InMemoryBackend::open().unwrap());
        let dir = tempfile::tempdir().unwrap();
        let store = Store::from_backend(
            backend.clone(),
            dir.path().to_path_buf(),
            1,
            DEFAULT_PERSIST_CHANNEL_CAPACITY,
        )
        .unwrap();
        (store, backend, dir)
    }

    /// Writes the head and finalized numbers the persist worker reads to decide which
    /// committed layers journal, as a forkchoice update would.
    fn set_head_and_finalized(
        backend: &Arc<dyn StorageBackend>,
        head: BlockNumber,
        finalized: BlockNumber,
    ) {
        let mut tx = backend.begin_write().unwrap();
        tx.put(
            CHAIN_DATA,
            &chain_data_key(ChainDataIndex::LatestBlockNumber),
            &head.to_le_bytes(),
        )
        .unwrap();
        tx.put(
            CHAIN_DATA,
            &chain_data_key(ChainDataIndex::FinalizedBlockNumber),
            &finalized.to_le_bytes(),
        )
        .unwrap();
        tx.commit().unwrap();
    }

    /// Stores block `number` on top of `parent_hash`, rewriting `shared` to `[number]`,
    /// and returns the block's hash and state root.
    fn store_block_rewriting(
        store: &Store,
        number: BlockNumber,
        parent_hash: H256,
        shared: &Nibbles,
        commit_depth: Option<usize>,
    ) -> (H256, H256) {
        let state_root = H256::repeat_byte(0xc0 | number as u8);
        let block = make_block(number, parent_hash, state_root);
        let hash = block.hash();
        store
            .store_block_updates(UpdateBatch {
                account_updates: vec![(shared.clone(), vec![number as u8])],
                storage_updates: vec![],
                blocks: vec![block],
                receipts: vec![],
                code_updates: vec![],
                commit_depth,
                wait_for_flush: false,
            })
            .unwrap();
        (hash, state_root)
    }

    /// Stores blocks `1..=count` rewriting `shared`. With `commit_depth: Some(1)`,
    /// storing block n commits block n-1, one layer per commit.
    fn store_chain(
        store: &Store,
        count: BlockNumber,
        shared: &Nibbles,
        commit_depth: Option<usize>,
    ) {
        let mut parent_hash = H256::zero();
        for number in 1..=count {
            (parent_hash, _) =
                store_block_rewriting(store, number, parent_hash, shared, commit_depth);
        }
    }

    fn journal_entry(backend: &Arc<dyn StorageBackend>, block_number: BlockNumber) -> JournalEntry {
        let bytes = backend
            .begin_read()
            .unwrap()
            .get(STATE_HISTORY, &block_number.to_be_bytes())
            .unwrap()
            .unwrap_or_else(|| panic!("journal entry for block {block_number}"));
        JournalEntry::decode(&bytes).unwrap()
    }

    /// A layer committed at or below finality (and the head) SHALL get no journal entry:
    /// no reorg can unwind it. The first layer above finality SHALL journal, with the
    /// pre-image the skipped layer left on disk.
    #[tokio::test]
    async fn final_layers_are_committed_without_journal_entries() {
        let (store, backend, _dir) = journal_store();
        set_head_and_finalized(&backend, 100, 3);
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);

        store_chain(&store, 6, &shared, Some(1));
        store.wait_for_persistence_idle().await.unwrap();

        for n in 1..=3 {
            assert!(!journal_entry_exists(&backend, n), "block {n} is final");
        }
        for n in 4..=5 {
            assert!(journal_entry_exists(&backend, n), "block {n} is not final");
        }
        let entry = journal_entry(&backend, 4);
        assert_eq!(entry.account_trie_diff.len(), 1);
        assert_eq!(
            entry.account_trie_diff[0].1,
            Some(vec![3]),
            "block 4's pre-image is what skipped block 3 wrote"
        );
    }

    /// A single commit spanning finality SHALL skip its final layers yet journal the
    /// layers above them with pre-images that include the skipped layers' writes. Those
    /// are staged in the same write batch, so disk does not have them yet.
    #[tokio::test]
    async fn layer_journaled_after_skipped_ones_in_one_commit_sees_their_writes() {
        let (store, backend, _dir) = journal_store();
        set_head_and_finalized(&backend, 100, 2);
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);

        // No safe-commit root yet, so blocks 1..=4 stay in memory.
        let mut parent_hash = H256::zero();
        let mut roots = vec![H256::zero()];
        for number in 1..=4 {
            let (hash, root) = store_block_rewriting(&store, number, parent_hash, &shared, None);
            parent_hash = hash;
            roots.push(root);
        }
        // With block 3 as the safe-commit root, storing block 5 commits layers 1, 2 and 3
        // in one write batch.
        store.set_safe_commit_root(roots[3]).unwrap();
        store_block_rewriting(&store, 5, parent_hash, &shared, None);
        store.wait_for_persistence_idle().await.unwrap();

        assert!(!journal_entry_exists(&backend, 1));
        assert!(!journal_entry_exists(&backend, 2));
        let entry = journal_entry(&backend, 3);
        assert_eq!(entry.account_trie_diff.len(), 1);
        assert_eq!(
            entry.account_trie_diff[0].1,
            Some(vec![2]),
            "block 3's pre-image is what skipped block 2 staged in the same batch"
        );
    }

    /// A head rewound below finality without a finality update SHALL NOT leave layers
    /// above the head unjournaled, final or not: interrupted-batch recovery needs the
    /// newest entry above the head to name the block whose state is on disk.
    #[tokio::test]
    async fn layers_above_the_head_are_journaled_even_when_final() {
        let (store, backend, _dir) = journal_store();
        set_head_and_finalized(&backend, 2, 100);
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);

        store_chain(&store, 6, &shared, Some(1));
        store.wait_for_persistence_idle().await.unwrap();

        for n in 1..=2 {
            assert!(
                !journal_entry_exists(&backend, n),
                "block {n} is final and not above the head"
            );
        }
        for n in 3..=5 {
            assert!(
                journal_entry_exists(&backend, n),
                "block {n} is above the head"
            );
        }
    }

    /// While older entries survive, every committed layer SHALL journal, final or not, so
    /// the newest entry keeps naming the block whose state is on disk and no surviving
    /// entry holds pre-images against a state the disk has moved past.
    #[tokio::test]
    async fn final_layers_are_journaled_while_older_entries_survive() {
        let (store, backend, _dir) = journal_store();
        seed_journal_entries(&backend, &[0]);
        set_head_and_finalized(&backend, 100, 100);
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);

        store_chain(&store, 4, &shared, Some(1));
        store.wait_for_persistence_idle().await.unwrap();

        for n in 1..=3 {
            assert!(
                journal_entry_exists(&backend, n),
                "block {n} follows a surviving entry"
            );
        }
    }

    /// On an L2, which finalizes its head every block, every layer is final by the time
    /// it commits. The journal SHALL stay empty and finality pruning SHALL write no range
    /// delete.
    #[tokio::test]
    async fn chain_finalizing_its_head_writes_no_journal_and_no_range_deletes() {
        let (store, backend, ranges, _dir) = recording_store();
        let shared = Nibbles::from_raw(&[0x0a, 0x0b], false);

        let mut parent_hash = H256::zero();
        for number in 1..=20 {
            let (hash, _) = store_block_rewriting(&store, number, parent_hash, &shared, Some(4));
            store
                .forkchoice_update_inner(vec![], number, hash, None, Some(number))
                .await
                .unwrap();
            parent_hash = hash;
        }
        store.wait_for_persistence_idle().await.unwrap();

        for n in 1..=20 {
            assert!(
                !journal_entry_exists(&backend, n),
                "block {n} was final when committed"
            );
        }
        assert!(ranges.lock().unwrap().is_empty());
    }
}

#[cfg(test)]
mod merge_tests {
    use super::*;

    fn h256(b: u8) -> H256 {
        H256::from_low_u64_be(b as u64)
    }

    fn op(bn: BlockNumber, bh: H256, idx: Index) -> Vec<u8> {
        encode_tx_location_operand(bn, bh, idx)
    }

    fn decode(v: &[u8]) -> Vec<(BlockNumber, BlockHash, Index)> {
        <Vec<(BlockNumber, BlockHash, Index)>>::decode(v).unwrap()
    }

    #[test]
    fn single_operand_on_empty_base() {
        let out = tx_locations_merge(None, vec![op(100, h256(0x10), 0)]).unwrap();
        assert_eq!(decode(&out), vec![(100, h256(0x10), 0)]);
    }

    #[test]
    fn operand_appended_to_existing_base() {
        let base = vec![(100u64, h256(0x10), 0u64)].encode_to_vec();
        let out = tx_locations_merge(Some(&base), vec![op(101, h256(0x11), 5)]).unwrap();
        let mut got = decode(&out);
        got.sort();
        let mut want = vec![(100, h256(0x10), 0), (101, h256(0x11), 5)];
        want.sort();
        assert_eq!(got, want);
    }

    #[test]
    fn multiple_operands_combined() {
        let out = tx_locations_merge(
            None,
            vec![
                op(100, h256(0x10), 0),
                op(100, h256(0x11), 1),
                op(101, h256(0x12), 2),
            ],
        )
        .unwrap();
        assert_eq!(decode(&out).len(), 3);
    }

    #[test]
    fn same_block_hash_is_deduped() {
        // Two operands with the same block_hash: the later one replaces the earlier.
        let out =
            tx_locations_merge(None, vec![op(100, h256(0x10), 0), op(100, h256(0x10), 7)]).unwrap();
        assert_eq!(decode(&out), vec![(100, h256(0x10), 7)]);
    }

    #[test]
    fn malformed_operand_aborts_merge() {
        // Fail loud: a malformed operand must abort the merge (return None), not
        // silently drop it and commit a partial result.
        let out = tx_locations_merge(None, vec![vec![0xff, 0xff], op(100, h256(0x10), 0)]);
        assert!(out.is_none(), "merge must abort on a malformed operand");
    }

    #[test]
    fn malformed_base_value_aborts_merge() {
        let out = tx_locations_merge(Some(&[0xff, 0xff]), vec![op(100, h256(0x10), 0)]);
        assert!(out.is_none(), "merge must abort on a corrupt base value");
    }

    /// Regression for the associative-merge format bug: a PartialMerge result
    /// must be re-mergeable as an operand. RocksDB folds operands together
    /// without a base value during compaction, then feeds that result back into
    /// a later merge. If the operand format differed from the output format,
    /// the re-fed result would fail to decode and entries would be dropped
    /// (observed as 1664 silent drops during a compaction pass on mainnet).
    #[test]
    fn partial_merge_result_is_a_valid_operand() {
        // Step 1: PartialMerge — combine operands with NO base value.
        let partial =
            tx_locations_merge(None, vec![op(100, h256(0x10), 0), op(101, h256(0x11), 1)]).unwrap();

        // Step 2: the partial result is now itself an operand in a later merge,
        // on top of an existing base value. This is the path that used to drop
        // entries.
        let base = vec![(99u64, h256(0x09), 9u64)].encode_to_vec();
        let out = tx_locations_merge(Some(&base), vec![partial]).unwrap();

        let mut got = decode(&out);
        got.sort();
        let mut want = vec![
            (99, h256(0x09), 9),
            (100, h256(0x10), 0),
            (101, h256(0x11), 1),
        ];
        want.sort();
        assert_eq!(
            got, want,
            "no entries may be lost when re-merging a partial result"
        );
    }

    /// Operand and stored-value encodings must be byte-identical types, so a
    /// freshly-encoded operand round-trips through the value decoder.
    #[test]
    fn operand_encoding_matches_value_encoding() {
        let operand = op(100, h256(0x10), 3);
        // Decoding the operand as the stored Vec type must succeed.
        assert_eq!(decode(&operand), vec![(100, h256(0x10), 3)]);
    }

    /// Chained PartialMerges (operand-only folds applied repeatedly) stay valid.
    #[test]
    fn chained_partial_merges() {
        let p1 = tx_locations_merge(None, vec![op(1, h256(0x01), 0)]).unwrap();
        let p2 = tx_locations_merge(None, vec![p1, op(2, h256(0x02), 0)]).unwrap();
        let p3 = tx_locations_merge(None, vec![p2, op(3, h256(0x03), 0)]).unwrap();
        let out = tx_locations_merge(None, vec![p3]).unwrap();
        assert_eq!(decode(&out).len(), 3);
    }
}

#[cfg(test)]
mod backfill_write_tests {
    use super::*;
    use ethrex_common::types::{EIP1559Transaction, TxType};

    fn test_header(number: BlockNumber) -> BlockHeader {
        BlockHeader {
            number,
            ..Default::default()
        }
    }

    fn test_tx(nonce: u64) -> Transaction {
        Transaction::EIP1559Transaction(EIP1559Transaction {
            nonce,
            ..Default::default()
        })
    }

    fn test_receipt() -> Receipt {
        Receipt {
            tx_type: TxType::EIP1559,
            succeeded: true,
            cumulative_gas_used: 21_000,
            logs: vec![],
            payer: None,
            frame_receipts: None,
        }
    }

    /// `add_backfilled_blocks` writes the body + receipts + transaction index and
    /// lowers the frontier (`earliest_block_number`) in one shot; the indexed tx
    /// is then locatable for its (canonical) block.
    #[tokio::test]
    async fn writes_data_indexes_txs_and_lowers_frontier() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        store.update_earliest_block_number(100).await.unwrap();

        let header = test_header(99);
        let hash = header.hash();
        let tx = test_tx(7);
        let body = BlockBody {
            transactions: vec![tx.clone()],
            ommers: vec![],
            withdrawals: Some(vec![]),
        };
        // In production the header is already stored and canonical from snap
        // header backfill; mirror the canonical mapping so the tx-index lookup
        // (which is canonical-filtered) can resolve.
        store.add_block_headers(vec![header.clone()]).await.unwrap();
        store
            .forkchoice_update(vec![(99, hash)], 99, hash, None, None)
            .await
            .unwrap();

        store
            .add_backfilled_blocks(
                vec![BackfilledBlock {
                    header,
                    body,
                    receipts: vec![test_receipt()],
                    index_transactions: true,
                }],
                99,
            )
            .await
            .unwrap();

        assert_eq!(
            store.get_earliest_block_number().await.unwrap(),
            99,
            "frontier must be lowered atomically with the data write"
        );
        assert!(
            store.get_block_body_by_hash(hash).await.unwrap().is_some(),
            "body must be stored"
        );
        assert_eq!(
            store.get_receipts_for_block(&hash).await.unwrap().len(),
            1,
            "receipts must be stored"
        );
        assert_eq!(
            store
                .get_transaction_location(tx.hash(&NativeCrypto))
                .await
                .unwrap(),
            Some((99, hash, 0)),
            "indexed transaction must be locatable"
        );
    }

    /// With `index_transactions = false` (a block outside the
    /// `--history.transactions` horizon) the body and receipts are still written,
    /// but the transaction index is skipped.
    #[tokio::test]
    async fn skips_tx_index_when_disabled() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        store.update_earliest_block_number(100).await.unwrap();

        let header = test_header(99);
        let hash = header.hash();
        let tx = test_tx(7);
        let body = BlockBody {
            transactions: vec![tx.clone()],
            ommers: vec![],
            withdrawals: Some(vec![]),
        };
        store.add_block_headers(vec![header.clone()]).await.unwrap();
        store
            .forkchoice_update(vec![(99, hash)], 99, hash, None, None)
            .await
            .unwrap();

        store
            .add_backfilled_blocks(
                vec![BackfilledBlock {
                    header,
                    body,
                    receipts: vec![test_receipt()],
                    index_transactions: false,
                }],
                99,
            )
            .await
            .unwrap();

        assert!(
            store.get_block_body_by_hash(hash).await.unwrap().is_some(),
            "body must still be stored"
        );
        assert_eq!(
            store.get_receipts_for_block(&hash).await.unwrap().len(),
            1,
            "receipts must still be stored"
        );
        assert_eq!(
            store
                .get_transaction_location(tx.hash(&NativeCrypto))
                .await
                .unwrap(),
            None,
            "transaction index must be skipped when index_transactions is false"
        );
    }

    /// End-to-end at the storage/RPC boundary: the getters the RPC handlers use
    /// (`get_block_body`, `get_transaction_by_hash`, `get_receipt`) return nothing
    /// for a headers-only (post-snap) block and real data after backfill.
    #[tokio::test]
    async fn historical_getters_transition_from_null_to_present() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        store.update_earliest_block_number(100).await.unwrap();

        let header = test_header(99);
        let hash = header.hash();
        let tx = test_tx(7);
        let tx_hash = tx.hash(&NativeCrypto);
        let body = BlockBody {
            transactions: vec![tx],
            ommers: vec![],
            withdrawals: Some(vec![]),
        };
        // Post-snap state: header stored and canonical, but no body/receipts.
        store.add_block_headers(vec![header.clone()]).await.unwrap();
        store
            .forkchoice_update(vec![(99, hash)], 99, hash, None, None)
            .await
            .unwrap();

        // Previously-null historical queries.
        assert!(
            store.get_block_body(99).await.unwrap().is_none(),
            "body absent before backfill"
        );
        assert!(
            store
                .get_transaction_by_hash(tx_hash)
                .await
                .unwrap()
                .is_none(),
            "tx unlocatable before backfill"
        );
        assert!(
            store.get_receipt(99, 0).await.unwrap().is_none(),
            "receipt absent before backfill"
        );

        store
            .add_backfilled_blocks(
                vec![BackfilledBlock {
                    header,
                    body,
                    receipts: vec![test_receipt()],
                    index_transactions: true,
                }],
                99,
            )
            .await
            .unwrap();

        // The same queries now return real data.
        assert!(
            store.get_block_body(99).await.unwrap().is_some(),
            "body present after backfill"
        );
        assert!(
            store
                .get_transaction_by_hash(tx_hash)
                .await
                .unwrap()
                .is_some(),
            "tx locatable after backfill"
        );
        assert!(
            store.get_receipt(99, 0).await.unwrap().is_some(),
            "receipt present after backfill"
        );
    }

    /// Backfill fills downward in batches; the frontier lowers after each batch,
    /// persists (doubling as the resume cursor), and leaves no gaps.
    #[tokio::test]
    async fn resume_fills_contiguously_without_gaps() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        store.update_earliest_block_number(100).await.unwrap();

        let make_batch = |range: std::ops::Range<u64>| -> Vec<BackfilledBlock> {
            range
                .rev()
                .map(|n| BackfilledBlock {
                    header: test_header(n),
                    body: BlockBody {
                        transactions: vec![],
                        ommers: vec![],
                        withdrawals: Some(vec![]),
                    },
                    receipts: vec![],
                    index_transactions: false,
                })
                .collect()
        };

        // Batch 1: fill [90, 99], lowering the frontier to 90.
        store
            .add_backfilled_blocks(make_batch(90..100), 90)
            .await
            .unwrap();
        assert_eq!(store.get_earliest_block_number().await.unwrap(), 90);

        // Simulate a restart: the task resumes from the persisted frontier.
        let resume_from = store.get_earliest_block_number().await.unwrap();
        assert_eq!(resume_from, 90, "resume reads the persisted frontier");

        // Batch 2: fill [80, 89], lowering the frontier to 80.
        store
            .add_backfilled_blocks(make_batch(80..90), 80)
            .await
            .unwrap();
        assert_eq!(store.get_earliest_block_number().await.unwrap(), 80);

        // No gaps: every block in [80, 99] has a stored body.
        for n in 80..100 {
            assert!(
                store
                    .get_block_body_by_hash(test_header(n).hash())
                    .await
                    .unwrap()
                    .is_some(),
                "block {n} must have a body (no gap left by resume)"
            );
        }
    }
}

/// The schema-version guard runs before any backend is opened, so these tests need
/// a persistent `EngineType` but never touch RocksDB itself.
#[cfg(test)]
#[cfg(feature = "rocksdb")]
mod schema_version_guard_tests {
    use super::*;

    fn write_marker(dir: &Path, schema_version: u64) {
        let metadata = StoreMetadata::new(schema_version);
        std::fs::write(
            dir.join(STORE_METADATA_FILENAME),
            serde_json::to_string_pretty(&metadata).unwrap(),
        )
        .unwrap();
    }

    fn read_marker(dir: &Path) -> u64 {
        read_store_schema_version(dir).unwrap().unwrap()
    }

    #[test]
    fn a_database_ahead_of_the_binary_is_refused_and_left_untouched() {
        // The scenario behind this test: a datadir opened once by a newer build
        // (which stamps its own schema version) and then handed back to an older
        // one. The older build must refuse with a version error — not a
        // "migration failed" — and must not rewrite the marker, so the newer
        // build can still open the database.
        let dir = tempfile::tempdir().unwrap();
        let newer = STORE_SCHEMA_VERSION + 1;
        write_marker(dir.path(), newer);

        let result =
            Store::new_with_config(dir.path(), EngineType::RocksDB, StoreConfig::default());

        assert!(
            matches!(
                result,
                Err(StoreError::IncompatibleDBVersion { found, expected })
                    if found == newer && expected == STORE_SCHEMA_VERSION
            ),
            "expected IncompatibleDBVersion, got {:?}",
            result.err()
        );
        assert_eq!(read_marker(dir.path()), newer);
    }

    #[test]
    fn a_zero_schema_version_is_incompatible_not_a_failed_migration() {
        // No ethrex ever writes v0; the marker is corrupt. Nothing was migrated,
        // so the error must not claim a migration failed.
        let dir = tempfile::tempdir().unwrap();
        write_marker(dir.path(), 0);

        let result =
            Store::new_with_config(dir.path(), EngineType::RocksDB, StoreConfig::default());

        assert!(
            matches!(
                result,
                Err(StoreError::IncompatibleDBVersion { found: 0, expected })
                    if expected == STORE_SCHEMA_VERSION
            ),
            "expected IncompatibleDBVersion, got {:?}",
            result.err()
        );
    }
}

#[cfg(test)]
mod datadir_tests {
    use super::*;
    use std::fs;

    #[test]
    fn empty_dir_has_no_existing_db() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!dir_contains_legacy_db(dir.path()).unwrap());
    }

    #[test]
    fn dir_with_only_unrelated_files_has_no_existing_db() {
        // Regression for #5680: a JWT secret (or any unrelated file) in the
        // datadir must not be mistaken for an existing database.
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("jwt.hex"), "0xdeadbeef").unwrap();
        fs::write(dir.path().join("LOG"), "noise").unwrap();
        assert!(!dir_contains_legacy_db(dir.path()).unwrap());
    }

    #[test]
    fn dir_with_rocksdb_markers_has_existing_db() {
        // A `CURRENT` file (and, separately, a `MANIFEST-*` file) marks a real DB.
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("CURRENT"), "MANIFEST-000001\n").unwrap();
        assert!(dir_contains_legacy_db(dir.path()).unwrap());

        let dir2 = tempfile::tempdir().unwrap();
        fs::write(dir2.path().join("MANIFEST-000007"), "x").unwrap();
        assert!(dir_contains_legacy_db(dir2.path()).unwrap());
    }

    #[test]
    fn dir_with_marker_named_subdirectories_has_no_existing_db() {
        // A *directory* named like a marker file must not be mistaken for a DB;
        // RocksDB only ever visits these as plain files.
        let dir = tempfile::tempdir().unwrap();
        fs::create_dir(dir.path().join("CURRENT")).unwrap();
        fs::create_dir(dir.path().join("MANIFEST-000001")).unwrap();
        assert!(!dir_contains_legacy_db(dir.path()).unwrap());
    }
}

#[cfg(test)]
mod flatkeyvalue_completeness_tests {
    use super::*;

    /// The `last_written` marker signals a finished FKV pass ONLY as the exact 1-byte
    /// `[0xff]` sentinel. This gates journal-backed deep reorgs, so the
    /// boundary must be exact: an unset marker, the initial all-zero frontier, and any
    /// mid-generation nibble path must all read as "not complete".
    #[test]
    fn only_the_one_byte_ff_sentinel_counts_as_complete() {
        // Complete: the durable completion sentinel the generator writes.
        assert!(Store::flatkeyvalue_generation_complete(Some(&[0xff])));

        // Not complete:
        assert!(!Store::flatkeyvalue_generation_complete(None)); // marker never written
        assert!(!Store::flatkeyvalue_generation_complete(Some(&[]))); // empty
        assert!(!Store::flatkeyvalue_generation_complete(Some(&[0u8; 64]))); // initial frontier
        assert!(!Store::flatkeyvalue_generation_complete(Some(&[0x0a; 64]))); // mid-gen nibble path
        // The in-memory frontier is expanded to all-0xff (64/131 bytes), but that is never
        // the durable marker; only the 1-byte form means complete, so these read false.
        assert!(!Store::flatkeyvalue_generation_complete(Some(&[0xff; 64])));
        assert!(!Store::flatkeyvalue_generation_complete(Some(&[
            0xff, 0xff
        ])));
    }
}

#[cfg(test)]
mod cgroup_memory_limit_tests {
    use super::*;
    use std::fs;

    /// Builds `<root>/<relative>` and writes `contents` into the `file` of every
    /// directory the map names, keyed by path relative to `root` ("" is the root).
    fn cgroup_tree(root: &Path, relative: &str, file: &str, limits: &[(&str, &str)]) {
        fs::create_dir_all(root.join(relative)).expect("create cgroup tree");
        for (dir, contents) in limits {
            fs::write(root.join(dir).join(file), contents).expect("write limit");
        }
    }

    /// A limit set on an ancestor cgroup (a systemd slice, a pod's parent) applies to
    /// the process, so it must be found even though the process's own cgroup is
    /// unlimited and the mount root reports `max`.
    #[test]
    fn ancestor_limit_applies() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        cgroup_tree(
            root,
            "system.slice/ethrex.service",
            "memory.max",
            &[("", "max"), ("system.slice", "4294967296")],
        );

        assert_eq!(
            cgroup_limit_up_to_root(root, "/system.slice/ethrex.service", "memory.max"),
            Some(4 * 1024 * 1024 * 1024)
        );
    }

    /// With limits at several levels the tightest one is what the kernel enforces.
    #[test]
    fn smallest_limit_in_the_chain_wins() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        cgroup_tree(
            root,
            "kubepods/pod123/container",
            "memory.max",
            &[
                ("kubepods", "8589934592"),
                ("kubepods/pod123", "2147483648"),
                ("kubepods/pod123/container", "4294967296"),
            ],
        );

        assert_eq!(
            cgroup_limit_up_to_root(root, "kubepods/pod123/container", "memory.max"),
            Some(2 * 1024 * 1024 * 1024)
        );
    }

    /// An unlimited chain is "unknown", not zero: v2 writes the literal `max` and v1 a
    /// sentinel near `u64::MAX`, and absent files must not count either.
    #[test]
    fn unlimited_chain_yields_no_limit() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        cgroup_tree(root, "docker/abc", "memory.max", &[("", "max")]);

        assert_eq!(
            cgroup_limit_up_to_root(root, "docker/abc", "memory.max"),
            None
        );
        // A cgroup path that does not exist under this mount (a v1 controller that is
        // not mounted, a stale line in /proc/self/cgroup) reads as unknown too.
        assert_eq!(
            cgroup_limit_up_to_root(root, "not/here", "memory.max"),
            None
        );
    }

    /// The root cgroup is itself a level: a limit set there still applies.
    #[test]
    fn root_limit_applies_to_the_root_cgroup() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        cgroup_tree(root, "", "memory.limit_in_bytes", &[("", "536870912")]);

        assert_eq!(
            cgroup_limit_up_to_root(root, "/", "memory.limit_in_bytes"),
            Some(512 * 1024 * 1024)
        );
    }
}

#[cfg(test)]
mod in_memory_receipts_tests {
    use super::*;
    use ethrex_common::types::TxType;

    /// Receipts read back from the in-memory backend, the one `--datadir memory` uses:
    /// every receipt of the block from the requested index on, and none of the next
    /// block's.
    #[tokio::test]
    async fn in_memory_store_returns_every_receipt_of_a_block() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        let receipt = |gas| Receipt::new(TxType::Legacy, true, gas, vec![]);
        let block = BlockHash::repeat_byte(1);
        let next_block = BlockHash::repeat_byte(2);
        store
            .add_receipts(block, vec![receipt(1), receipt(2), receipt(3)])
            .await
            .unwrap();
        store
            .add_receipts(next_block, vec![receipt(4)])
            .await
            .unwrap();

        let gas = |receipts: Vec<Receipt>| {
            receipts
                .iter()
                .map(|receipt| receipt.cumulative_gas_used)
                .collect::<Vec<_>>()
        };
        assert_eq!(
            gas(store.get_receipts_for_block(&block).await.unwrap()),
            [1, 2, 3]
        );
        assert_eq!(
            gas(store
                .get_receipts_for_block_from_index(&block, 1, None)
                .await
                .unwrap()),
            [2, 3]
        );
        assert_eq!(
            gas(store
                .get_receipts_for_block_from_index(&block, 1, Some(1))
                .await
                .unwrap()),
            [2]
        );
        assert_eq!(
            gas(store.get_receipts_for_block(&next_block).await.unwrap()),
            [4]
        );
    }
}

#[cfg(test)]
mod witness_pruning_tests {
    use super::*;

    /// Storing a witness prunes those more than `MAX_WITNESSES` blocks behind it, in one
    /// pass: every block from the oldest kept one up to the threshold, every fork of those
    /// blocks, and nothing above the threshold.
    #[test]
    fn storing_a_witness_prunes_every_block_below_the_window() {
        let store = Store::new("", EngineType::InMemory).expect("in-memory store");
        let fork = BlockHash::repeat_byte;
        let store_at = |number: u64, hash: BlockHash| {
            store
                .store_witness(hash, number, ExecutionWitness::default())
                .unwrap()
        };
        let kept = |number: u64, hash: BlockHash| {
            store
                .get_witness_json_bytes(number, hash)
                .unwrap()
                .is_some()
        };

        for (number, hash) in [(2, fork(1)), (3, fork(1)), (3, fork(2)), (4, fork(1))] {
            store_at(number, hash);
        }
        // The first cleanup past the window only records where the next one starts,
        // block 2, without deleting anything.
        store_at(MAX_WITNESSES + 1, fork(1));
        assert!(kept(2, fork(1)) && kept(3, fork(1)) && kept(3, fork(2)) && kept(4, fork(1)));

        // The window now starts at block 4: blocks 2 and 3 go, both forks of block 3
        // included, and the scan stops at block 4.
        store_at(MAX_WITNESSES + 3, fork(1));
        assert!(!kept(2, fork(1)));
        assert!(!kept(3, fork(1)));
        assert!(!kept(3, fork(2)));
        assert!(kept(4, fork(1)));
        assert!(kept(MAX_WITNESSES + 1, fork(1)));
        assert!(kept(MAX_WITNESSES + 3, fork(1)));
    }
}