ethrex-vm 28.0.0

EVM execution backend and database abstraction 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
pub mod db;
mod tracing;

use super::{BlockExecutionResult, FrameValidationOutcome, TxGasBreakdown, compute_burned_fees};
use crate::system_contracts::{
    AMSTERDAM_REQUEST_PREDEPLOYS, BEACON_ROOTS_ADDRESS, BUILDER_DEPOSIT_CONTRACT_ADDRESS,
    BUILDER_EXIT_CONTRACT_ADDRESS, CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS,
    EXPIRY_VERIFIER_PREDEPLOY, EXPIRY_VERIFIER_RUNTIME_BYTECODE, HISTORY_STORAGE_ADDRESS,
    PRAGUE_SYSTEM_CONTRACTS, SYSTEM_ADDRESS, WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS,
};
use crate::{EvmError, ExecutionResult};
use bytes::Bytes;
use ethrex_common::H256;
#[cfg(feature = "rayon")]
use ethrex_common::constants::EMPTY_KECCAK_HASH;
use ethrex_common::types::Code;
#[cfg(feature = "rayon")]
use ethrex_common::types::TxType;
use ethrex_common::types::block_access_list::BlockAccessList;
#[cfg(feature = "rayon")]
use ethrex_common::types::block_access_list::{
    AccountChanges, BalAddressIndex, find_exact_change_balance, find_exact_change_code,
    find_exact_change_nonce, find_exact_change_storage, has_exact_change_balance,
    has_exact_change_code, has_exact_change_nonce, has_exact_change_storage,
};
use ethrex_common::types::fee_config::FeeConfig;
use ethrex_common::types::{AuthorizationTuple, EIP7702Transaction};
#[cfg(feature = "rayon")]
use ethrex_common::utils::u256_from_big_endian_const;
use ethrex_common::{
    Address, U256,
    types::{
        AccessList, AccountUpdate, Block, BlockHeader, EIP1559Transaction, Fork, FrameReceipt,
        GWEI_TO_WEI, GenericTransaction, INITIAL_BASE_FEE, Log, Receipt, Transaction, TxKind,
        Withdrawal, requests::Requests,
    },
};
#[cfg(feature = "rayon")]
use ethrex_common::{
    BigEndianHash, constants::AMSTERDAM_MAX_CODE_SIZE, validate_bal_code_sizes,
    validate_block_access_list_size, validate_header_bal_indices,
};
use ethrex_crypto::Crypto;
use ethrex_levm::EVMConfig;
use ethrex_levm::StatelessValidator;
#[cfg(feature = "rayon")]
use ethrex_levm::account::{AccountStatus, LevmAccount};
use ethrex_levm::call_frame::Stack;
use ethrex_levm::constants::{
    POST_OSAKA_GAS_LIMIT_CAP, STACK_LIMIT, SYS_CALL_GAS_LIMIT, TX_MAX_GAS_LIMIT_AMSTERDAM,
};
use ethrex_levm::db::gen_db::GeneralizedDatabase;
#[cfg(feature = "rayon")]
use ethrex_levm::db::gen_db::{
    LazyBalCursor, code_from_bal, post_value_at_or_before, seed_one_address_info_from_bal,
};
#[cfg(feature = "rayon")]
use ethrex_levm::db::{Database, gen_db::CacheDB};
use ethrex_levm::errors::{InternalError, TxValidationError};
use ethrex_levm::memory::Memory;
#[cfg(feature = "perf_opcode_timings")]
use ethrex_levm::timings::{OPCODE_TIMINGS, PRECOMPILES_TIMINGS};
use ethrex_levm::tracing::LevmCallTracer;
use ethrex_levm::utils::get_base_fee_per_blob_gas;
use ethrex_levm::utils::intrinsic_gas_floor;
use ethrex_levm::validation_observer::FrameSimViolation;
use ethrex_levm::vm::VMType;
use ethrex_levm::{
    Environment,
    errors::{ExecutionReport, TxResult, VMError},
    vm::VM,
};
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
#[cfg(feature = "rayon")]
use rustc_hash::{FxHashMap, FxHashSet};
use std::cmp::min;
use std::sync::Arc;
#[cfg(feature = "rayon")]
use std::sync::atomic::AtomicBool;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::mpsc::Sender;

/// EIP-7928 `block_access_index` of the pre-block system calls.
/// Only the parallel BAL path reads it, and that path is rayon-gated.
#[cfg(feature = "rayon")]
const PRE_BLOCK_BAL_INDEX: u32 = 0;

/// EIP-8079 `burned_fees` for an LStar block: `base_fee · post_refund_gas +
/// blob_base_fee · blob_gas_used`. `post_refund_gas` is the block's post-refund
/// Σ gas_spent (the running `cumulative_gas_used`, equivalently the last
/// receipt's `cumulative_gas_used`). Callers gate this on `is_lstar` and wrap
/// the result in `Some`. Extracted so the production and verification paths
/// (`execute_block` and both `execute_block_pipeline` arms) derive the burn
/// identically.
fn lstar_burned_fees(
    chain_config: &ethrex_common::types::ChainConfig,
    header: &ethrex_common::types::BlockHeader,
    post_refund_gas: u64,
) -> u64 {
    let evm_cfg = EVMConfig::new_from_chain_config(chain_config, header);
    let blob_base_fee = get_base_fee_per_blob_gas(header.excess_blob_gas, &evm_cfg)
        .map(|v| u64::try_from(v).unwrap_or(u64::MAX))
        .unwrap_or(0);
    let blob_gas_used = header.blob_gas_used.unwrap_or(0);
    compute_burned_fees(
        header.base_fee_per_gas.unwrap_or(0),
        // EIP-8079: burn basis is post-refund Σ gas_spent.
        post_refund_gas,
        blob_base_fee,
        blob_gas_used,
    )
}

/// The struct implements the following functions:
/// [LEVM::execute_block]
/// [LEVM::execute_tx]
/// [LEVM::get_state_transitions]
/// [LEVM::process_withdrawals]
#[derive(Debug)]
pub struct LEVM;

/// Build the per-frame receipts (EIP-8141) for a frame transaction from an
/// execution report's `frame_results`. Returns `None` when the report carries
/// no frame results.
fn frame_receipts_from(
    frame_results: Option<Vec<(u8, u64, Vec<Log>)>>,
) -> Option<Vec<FrameReceipt>> {
    frame_results.map(|results| {
        results
            .into_iter()
            .map(|(status, gas_used, logs)| FrameReceipt {
                status,
                gas_used,
                logs,
            })
            .collect()
    })
}

/// Checks that adding `tx_gas_limit` to `block_gas_used` doesn't exceed `block_gas_limit`.
fn check_gas_limit(
    block_gas_used: u64,
    tx_gas_limit: u64,
    block_gas_limit: u64,
) -> Result<(), EvmError> {
    if tx_gas_limit > block_gas_limit.saturating_sub(block_gas_used) {
        return Err(EvmError::Transaction(format!(
            "Gas allowance exceeded: \
             used {block_gas_used} + tx limit {tx_gas_limit} > block limit {block_gas_limit}"
        )));
    }
    Ok(())
}

/// Upper bound on the gas a block can legitimately spend across all its transactions.
///
/// Per transaction `gas_used = regular + state`, so
/// `sum(gas_used) = sum(regular) + sum(state) <= 2 * max(sum(regular), sum(state))`.
/// That max is exactly the block's reported `gas_used`, which a valid block keeps
/// within its gas limit, so no valid block spends more than twice its limit in total.
pub fn block_work_budget(block_gas_limit: u64) -> u64 {
    block_gas_limit.saturating_mul(2)
}

/// Reject a block whose transactions cannot all be admitted, before executing any of
/// them.
///
/// Ordered gas admission ([`check_2d_gas_allowance`]) needs each transaction's real
/// gas, so on the parallel path it can only run once every transaction has executed
/// and its report is held in memory. This bound needs none of that: every transaction
/// spends at least its EIP-7623/7976 floor, so a block whose floors already exceed
/// [`block_work_budget`] cannot pass admission whatever it executes to.
///
/// Sound in the accepting direction: for any block that could be valid,
/// `sum(floor) <= sum(gas_used) <= block_work_budget`, so this never rejects a block
/// ordered admission would have accepted.
pub fn check_minimum_block_work<'a>(
    txs_with_sender: impl IntoIterator<Item = (&'a Transaction, Address)>,
    fork: Fork,
    block_gas_limit: u64,
) -> Result<(), EvmError> {
    let budget = block_work_budget(block_gas_limit);
    let mut floor_total = 0_u64;
    for (tx, sender) in txs_with_sender {
        // The floor is computed from the transaction's own fields, so failing to
        // compute it (an overflow) makes the block invalid rather than a transient
        // error to retry.
        let floor = intrinsic_gas_floor(tx, sender, fork)
            .map_err(|e| EvmError::Transaction(format!("intrinsic gas floor: {e}")))?;
        floor_total = floor_total.saturating_add(floor);
        if floor_total > budget {
            return Err(EvmError::Transaction(format!(
                "Gas allowance exceeded: minimum gas of the block's transactions \
                 {floor_total} exceeds the block work budget {budget} \
                 (block_gas_limit={block_gas_limit})"
            )));
        }
    }
    Ok(())
}

/// Gas spent so far by the transactions parallel execution has finished, per EIP-8037
/// dimension, so it can stop once the block is over its gas limit.
///
/// Ordered admission ([`check_2d_gas_allowance`]) only runs after every transaction
/// has executed, so without this a block that is over its gas limit still costs its
/// full execution, which can be many times the work of any valid block.
///
/// Sound in the accepting direction: a valid block keeps the sum of each dimension
/// within its gas limit, and the transactions finished so far are a subset of the
/// block, so their sums cannot exceed the limit either.
#[derive(Default)]
pub struct CompletedGas {
    regular: AtomicU64,
    state: AtomicU64,
}

impl CompletedGas {
    /// Rejects once the finished transactions exceed the gas limit in either dimension.
    pub fn check(&self, block_gas_limit: u64) -> Result<(), EvmError> {
        let regular = self.regular.load(Ordering::Relaxed);
        let state = self.state.load(Ordering::Relaxed);
        if regular > block_gas_limit || state > block_gas_limit {
            return Err(EvmError::Transaction(format!(
                "Gas allowance exceeded: transactions completed during parallel execution \
                 used regular={regular} state={state}, over block_gas_limit={block_gas_limit}"
            )));
        }
        Ok(())
    }

    /// Adds one finished transaction's gas, then applies [`Self::check`].
    pub fn record(
        &self,
        regular_gas: u64,
        state_gas: u64,
        block_gas_limit: u64,
    ) -> Result<(), EvmError> {
        self.regular.fetch_add(regular_gas, Ordering::Relaxed);
        self.state.fetch_add(state_gas, Ordering::Relaxed);
        self.check(block_gas_limit)
    }
}

/// EIP-8037 (Amsterdam+, execution-specs PR #2703) per-tx 2D inclusion check.
///
/// A tx is rejected (block invalid) if its worst-case contribution to either
/// dimension exceeds the remaining budget at tx inclusion time:
///
/// - regular dim: `min(TX_MAX_GAS_LIMIT, tx.gas) > block_gas_limit - block_regular_gas_used`
/// - state dim:   `tx.gas > block_gas_limit - block_state_gas_used`
///
/// The full `tx.gas` is used in both dimensions (only the regular dimension is
/// capped at `TX_MAX_GAS_LIMIT`); intrinsic underfunding is rejected separately
/// in transaction validation, not here. Mirrors
/// `src/ethereum/forks/amsterdam/fork.py` `check_transaction` at the
/// `tests-glamsterdam-devnet@v7.1.0` spec.
///
/// Note: `block_gas_used_regular` here equals EELS's `block_output.block_gas_used`
/// because our `report.gas_used` already reflects `max(raw_regular, calldata_floor)`
/// per-tx — i.e. the floor is applied before aggregation, not after. Keep this in
/// sync with the aggregation loop in [`execute_block_parallel`].
pub fn check_2d_gas_allowance(
    tx: &Transaction,
    block_gas_used_regular: u64,
    block_gas_used_state: u64,
    block_gas_limit: u64,
) -> Result<(), EvmError> {
    let tx_gas = tx.gas_limit();
    let regular_available = block_gas_limit.saturating_sub(block_gas_used_regular);
    let state_available = block_gas_limit.saturating_sub(block_gas_used_state);

    // Regular dim: worst-case regular contribution = full tx.gas, capped at
    // TX_MAX_GAS_LIMIT. The spec uses the full tx gas with no intrinsic
    // subtraction; intrinsic underfunding is rejected separately in transaction
    // validation, not by this inclusion check.
    let regular_contrib = tx_gas.min(TX_MAX_GAS_LIMIT_AMSTERDAM);
    if regular_contrib > regular_available {
        return Err(EvmError::Transaction(format!(
            "Gas allowance exceeded: regular dim worst-case {regular_contrib} > \
             available {regular_available} (block_gas_used_regular={block_gas_used_regular}, \
             block_gas_limit={block_gas_limit})"
        )));
    }

    // State dim: worst-case state contribution = full tx.gas.
    let state_contrib = tx_gas;
    if state_contrib > state_available {
        return Err(EvmError::Transaction(format!(
            "Gas allowance exceeded: state dim worst-case {state_contrib} > \
             available {state_available} (block_gas_used_state={block_gas_used_state}, \
             block_gas_limit={block_gas_limit})"
        )));
    }

    Ok(())
}

/// Error type for BAL validation failures, distinguishing state mismatches
/// from database errors.
///
/// Public so [`LEVM::validate_tx_execution`] is directly callable (and its
/// error variants inspectable) from unit tests outside this crate.
#[cfg(feature = "rayon")]
#[derive(Debug, thiserror::Error)]
pub enum BalValidationError {
    #[error("{0}")]
    Mismatch(String),
    #[error("{0}")]
    Database(String),
}

impl LEVM {
    /// Execute a block and return the execution result.
    ///
    /// Also records and returns the Block Access List (EIP-7928) for Amsterdam+ forks.
    /// The BAL will be `None` for pre-Amsterdam forks.
    pub fn execute_block(
        block: &Block,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<(BlockExecutionResult, Option<BlockAccessList>), EvmError> {
        let chain_config = db.store.get_chain_config()?;
        let is_amsterdam = chain_config.is_amsterdam_activated(block.header.timestamp);
        // EIP-8079 (LStar+): compute burned fees
        let is_lstar = chain_config.is_lstar_activated(block.header.timestamp);

        // EIP-7928 BlockAccessIndex is uint32. Block validity forbids >= 2^32 txs
        // long before we'd reach this point, but guard the invariant explicitly
        // so any upstream bug that inflates tx counts panics in debug instead of
        // silently producing a `u32::MAX` index.
        debug_assert!(
            block.body.transactions.len() < u32::MAX as usize,
            "tx count overflows u32 BlockAccessIndex"
        );

        // Enable BAL recording for Amsterdam+ forks
        if is_amsterdam {
            db.enable_bal_recording();
            // Set index 0 for pre-execution phase (system contracts)
            db.set_bal_index(0);
        }

        Self::prepare_block(block, db, vm_type, crypto)?;

        // Block-invariant EVM config + chain id + base blob fee, computed once and
        // reused by every tx (mirrors `execute_block_pipeline`): avoids a per-tx
        // chain-config copy, fork/blob-schedule recompute, and `fake_exponential` call.
        let evm_config = EVMConfig::new_from_chain_config(&chain_config, &block.header);
        let chain_id = chain_config.chain_id;
        let base_blob_fee_per_gas =
            get_base_fee_per_blob_gas(block.header.excess_blob_gas, &evm_config)?;
        // Stack/memory buffer pools reused across txs (each tx draws one and reclaims it).
        let mut shared_stack_pool = Vec::with_capacity(STACK_LIMIT);
        let mut shared_memory_pool = Vec::with_capacity(1);

        let n_txs = block.body.transactions.len();
        let mut receipts = Vec::with_capacity(n_txs);
        let mut tx_gas_breakdowns: Vec<TxGasBreakdown> = Vec::with_capacity(n_txs);
        // Cumulative gas for receipts (POST-REFUND per EIP-7778)
        let mut cumulative_gas_used = 0_u64;
        // Block gas accounting (PRE-REFUND for Amsterdam+ per EIP-7778)
        let mut block_gas_used = 0_u64;
        // EIP-8037 (Amsterdam+): track regular and state gas separately for block-level max()
        let mut block_regular_gas_used = 0_u64;
        let mut block_state_gas_used = 0_u64;
        let transactions_with_sender =
            block
                .body
                .get_transactions_with_sender(crypto)
                .map_err(|error| {
                    EvmError::Transaction(format!("Couldn't recover addresses with error: {error}"))
                })?;

        for (tx_idx, (tx, tx_sender)) in transactions_with_sender.into_iter().enumerate() {
            // Pre-tx gas limit guard:
            // Pre-Amsterdam: reject tx if cumulative post-refund gas + tx.gas > block limit.
            // Amsterdam+: skip — EIP-8037's 2D gas model means cumulative gas (regular +
            // state) can legally exceed the block gas limit as long as
            // max(sum_regular, sum_state) stays within it. Block-level overflow is
            // detected post-execution.
            if !is_amsterdam {
                check_gas_limit(cumulative_gas_used, tx.gas_limit(), block.header.gas_limit)?;
            }

            // EIP-8037 (Amsterdam+, PR #2703): per-tx 2D inclusion check.
            if is_amsterdam {
                check_2d_gas_allowance(
                    tx,
                    block_regular_gas_used,
                    block_state_gas_used,
                    block.header.gas_limit,
                )?;
            }

            // Set BAL index for this transaction (1-indexed per EIP-7928)
            if is_amsterdam {
                let bal_index = u32::try_from(tx_idx + 1).unwrap_or(u32::MAX);
                db.set_bal_index(bal_index);

                // Record tx sender for BAL. The recipient is recorded when the prepare
                // region loads it (default_hook), per the EIP-7928 v7.1.0 update: an
                // EIP-7702 auth halt before the recipient load must exclude it.
                if let Some(recorder) = db.bal_recorder_mut() {
                    recorder.record_touched_address(tx_sender);
                }
            }

            let report = Self::execute_tx_in_block(
                tx,
                tx_sender,
                &block.header,
                db,
                vm_type,
                base_blob_fee_per_gas,
                &mut shared_stack_pool,
                &mut shared_memory_pool,
                false,
                crypto,
                evm_config,
                chain_id,
                stateless_validator,
            )?;

            tx_gas_breakdowns.push(TxGasBreakdown::from_report(
                tx_idx,
                tx.hash(crypto),
                &report,
            ));

            // EIP-7778: gas_spent (POST-REFUND) for receipt cumulative_gas_used
            cumulative_gas_used += report.gas_spent;

            // EIP-8037 (Amsterdam+): block_gas_used = max(sum_regular, sum_state)
            // For pre-Amsterdam, state_gas_used is always 0 so gas_used == regular_gas.
            let tx_state_gas = report.state_gas_used;
            let tx_regular_gas = report.gas_used.saturating_sub(tx_state_gas);
            block_regular_gas_used = block_regular_gas_used.saturating_add(tx_regular_gas);
            block_state_gas_used = block_state_gas_used.saturating_add(tx_state_gas);

            if is_amsterdam {
                // Amsterdam+: block gas = max(regular_sum, state_sum)
                block_gas_used = block_regular_gas_used.max(block_state_gas_used);
                ::tracing::debug!(
                    "EIP-8037 validate tx[{tx_idx}]: regular={tx_regular_gas} state={tx_state_gas} gas_used={} gas_spent={} block_regular={block_regular_gas_used} block_state={block_state_gas_used} block_max={block_gas_used}",
                    report.gas_used,
                    report.gas_spent,
                );

                // DoS protection: early exit if either regular or state gas exceeds the limit.
                // Since block_gas_used = max(regular, state), if either component exceeds
                // the limit, we know the block is invalid and can safely reject without
                // violating EIP-8037 semantics.
                if block_regular_gas_used > block.header.gas_limit
                    || block_state_gas_used > block.header.gas_limit
                {
                    return Err(EvmError::Transaction(format!(
                        "Gas allowance exceeded: Block gas used overflow: \
                         block_gas_used {block_gas_used} > block_gas_limit {}",
                        block.header.gas_limit
                    )));
                }
            } else {
                block_gas_used = block_gas_used.saturating_add(report.gas_used);
            }

            let mut receipt = Receipt::new(
                tx.tx_type(),
                matches!(report.result, TxResult::Success),
                cumulative_gas_used,
                report.logs,
            );

            // For frame transactions, propagate payer and per-frame receipts
            if matches!(tx, Transaction::FrameTransaction(_)) {
                receipt.payer = report.payer_address;
                receipt.frame_receipts = frame_receipts_from(report.frame_results);
            }

            receipts.push(receipt);
        }

        // EIP-7778 (Amsterdam+): block-level gas overflow check.
        // Per-tx checks are skipped for Amsterdam because block gas is computed
        // from pre-refund values; overflow can only be detected after execution.
        if is_amsterdam && block_gas_used > block.header.gas_limit {
            return Err(EvmError::Transaction(format!(
                "Gas allowance exceeded: Block gas used overflow: \
                 block_gas_used {block_gas_used} > block_gas_limit {}",
                block.header.gas_limit
            )));
        }

        // Set BAL index for post-execution phase (withdrawals + requests)
        //
        // Withdrawals are applied BEFORE the request system calls. EIP-7002 places its
        // system call "at the end of processing any execution block ... after processing
        // all transactions and after performing the block body withdrawal requests
        // validations", and EELS `apply_body` calls `process_withdrawals` then
        // `process_general_purpose_requests`. The order is observable only when a
        // withdrawal credits an address whose own system call then spends that balance in
        // the same block, which no real predeploy does; the EEST case
        // `bal_withdrawals_and_dequeues_net_balance_at_last_index` constructs exactly that.
        // geth runs its request system calls first (`PostExecution` precedes `Finalize`),
        // so this follows the spec rather than geth.
        if is_amsterdam {
            let post_tx_index =
                u32::try_from(block.body.transactions.len() + 1).unwrap_or(u32::MAX);
            db.set_bal_index(post_tx_index);

            // Record ALL withdrawal recipients for BAL per EIP-7928:
            // "Withdrawal recipients regardless of amount"
            // The amount filter only applies to balance_changes, not touched_addresses
            if let Some(withdrawals) = &block.body.withdrawals
                && let Some(recorder) = db.bal_recorder_mut()
            {
                recorder.extend_touched_addresses(withdrawals.iter().map(|w| w.address));
            }
        }

        if let Some(withdrawals) = &block.body.withdrawals {
            Self::process_withdrawals(db, withdrawals)?;
        }

        // TODO: I don't like deciding the behavior based on the VMType here.
        // TODO2: Revise this, apparently extract_all_requests_levm is not called
        // in L2 execution, but its implementation behaves differently based on this.
        let requests = match vm_type {
            VMType::L1 => extract_all_requests_levm(&receipts, db, &block.header, vm_type, crypto)?,
            VMType::L2(_) => Default::default(),
        };

        // Extract BAL if recording was enabled
        let bal = db.take_bal();

        Ok((
            BlockExecutionResult {
                receipts,
                requests,
                block_gas_used,
                // cumulative_gas_used is accumulated as += report.gas_spent (post-refund).
                burned_fees: is_lstar
                    .then(|| lstar_burned_fees(&chain_config, &block.header, cumulative_gas_used)),
                tx_gas_breakdowns,
            },
            bal,
        ))
    }

    /// `merkleizer` is `Some` on the streaming (non-BAL) path; the BAL validation path
    /// passes `None` because the caller merkleizes optimistically from the input BAL and
    /// the EVM-side `bal_to_account_updates` send is then redundant work.
    #[allow(clippy::too_many_arguments)]
    pub fn execute_block_pipeline(
        block: &Block,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        merkleizer: Option<Sender<Vec<AccountUpdate>>>,
        queue_length: &AtomicUsize,
        crypto: &dyn Crypto,
        header_bal: Option<Arc<BlockAccessList>>,
        bal_parallel_exec_enabled: bool,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<(BlockExecutionResult, Option<BlockAccessList>), EvmError> {
        let chain_config = db.store.get_chain_config()?;
        let is_amsterdam = chain_config.is_amsterdam_activated(block.header.timestamp);
        let is_lstar = chain_config.is_lstar_activated(block.header.timestamp);
        // Block-invariant EVM config + chain id, computed once and reused by every tx
        // (avoids a per-tx chain-config dyn-dispatch copy + fork/blob-schedule recompute).
        let evm_config = EVMConfig::new_from_chain_config(&chain_config, &block.header);
        let chain_id = chain_config.chain_id;

        // EIP-7928 BlockAccessIndex invariant — see `execute_block` for rationale.
        debug_assert!(
            block.body.transactions.len() < u32::MAX as usize,
            "tx count overflows u32 BlockAccessIndex"
        );

        let transactions_with_sender =
            block
                .body
                .get_transactions_with_sender(crypto)
                .map_err(|error| {
                    EvmError::Transaction(format!("Couldn't recover addresses with error: {error}"))
                })?;

        #[cfg(not(feature = "rayon"))]
        // Without rayon there is no parallel BAL path, so these are unused.
        // Adding dummy let to avoid unused warnings.
        let _ = (header_bal, bal_parallel_exec_enabled);
        #[cfg(feature = "rayon")]
        // When BAL is provided (Amsterdam+ validation path): use parallel execution.
        // The `is_amsterdam` gate is required: `execute_block_parallel` (and the
        // optimistic merkleization it feeds) is only correct on Amsterdam+; a
        // pre-Amsterdam call here in release would skip the inner debug_assert.
        // `--no-bal-parallel-exec` opts out and falls through to the sequential pipeline below.
        if let Some(bal) = header_bal
            && is_amsterdam
            && bal_parallel_exec_enabled
        {
            // Validate header BAL structural properties before execution.
            // This catches index-out-of-bounds early, before wasting execution time.
            // Note: the EIP-7928 item-count cap is deferred until after transaction
            // processing so that transaction-level errors (e.g. gas allowance exceeded)
            // take priority, matching the reference implementation's validation order.
            validate_header_bal_indices(&bal, block.body.transactions.len())
                .map_err(|e| EvmError::Custom(e.to_string()))?;
            // Each transaction that loads an account builds its code from the BAL, so an
            // oversized code change must be rejected before any of them run.
            validate_bal_code_sizes(&bal, AMSTERDAM_MAX_CODE_SIZE)
                .map_err(|e| EvmError::Custom(format!("BAL validation failed: {e}")))?;

            // Shadow-record the system phases: their account cache also holds internal
            // loads the recorder never sees, so it can't say what they touched.
            db.enable_bal_recording();
            db.set_bal_index(PRE_BLOCK_BAL_INDEX);
            let prepare_result = Self::prepare_block(block, db, vm_type, crypto);
            let pre_block_bal = db.take_bal().unwrap_or_default();
            prepare_result?;

            // Build validation index once — shared across parallel execution and post-exec seeding.
            let validation_index = Arc::new(bal.build_validation_index());

            // Validate supplied-BAL entries at the pre-execution index (0). The
            // per-tx (`validate_tx_execution`) and withdrawal
            // (`validate_bal_withdrawal_index`) validators never reach index 0,
            // so a spurious no-op entry there would otherwise pass the parallel
            // path (it leaves the state root unchanged). Must run before
            // `get_state_transitions_tx` drains `current_accounts_state`.
            Self::validate_bal_pre_exec_index(db, &bal, &validation_index)?;

            // Drain system call state and snapshot for per-tx db seeding
            LEVM::get_state_transitions_tx(db)?;
            let system_seed = Arc::new(std::mem::take(&mut db.initial_accounts_state));

            let parallel_result = Self::execute_block_parallel(
                block,
                &transactions_with_sender,
                db,
                vm_type,
                Arc::clone(&bal),
                merkleizer.as_ref(),
                queue_length,
                system_seed,
                &pre_block_bal,
                crypto,
                Arc::clone(&validation_index),
                stateless_validator,
            );

            // If parallel execution failed (e.g. BAL validation), still check system
            // contracts — SystemContractCallFailed takes priority over BAL errors.
            // The BAL may be inconsistent for blocks that are fundamentally invalid
            // due to a failing system contract.
            let (
                receipts,
                block_gas_used,
                mut unread_storage_reads,
                mut unaccessed_pure_accounts,
                tx_gas_breakdowns,
            ) = match parallel_result {
                Ok(result) => result,
                Err(parallel_err) => {
                    let last_tx_idx =
                        u32::try_from(block.body.transactions.len()).unwrap_or(u32::MAX);
                    if Self::seed_db_from_bal(
                        db,
                        &bal,
                        last_tx_idx,
                        &validation_index.accounts_by_min_index,
                    )
                    .is_ok()
                        && let VMType::L1 = vm_type
                        && let Err(e @ EvmError::SystemContractCallFailed(_)) =
                            extract_all_requests_levm(&[], db, &block.header, vm_type, crypto)
                    {
                        return Err(e);
                    }
                    return Err(parallel_err);
                }
            };

            // Seed main db with post-tx state (excluding withdrawal effects) so
            // request extraction system calls see user-queued requests on predeploys.
            // Withdrawal index is n_txs+1 in BAL; we use n_txs to avoid double-applying
            // withdrawal balances (process_withdrawals handles those below).
            let last_tx_idx = u32::try_from(block.body.transactions.len()).unwrap_or(u32::MAX);
            // Eager seed retained: lazy_bal cursor is per-tx only; outer DB has no cursor.
            Self::seed_db_from_bal(
                db,
                &bal,
                last_tx_idx,
                &validation_index.accounts_by_min_index,
            )?;

            let withdrawal_bal_idx = u32::try_from(block.body.transactions.len())
                .map(|n| n.saturating_add(1))
                .unwrap_or(u32::MAX);
            db.enable_bal_recording();
            db.set_bal_index(withdrawal_bal_idx);

            // Withdrawals precede the request system calls; see the ordering note on
            // the sequential path above. The db was seeded at `last_tx_idx` (post-tx,
            // pre-withdrawal), so applying withdrawals here and then extracting
            // requests lets the system calls observe the withdrawal credits.
            let post_block_result = (|| -> Result<Vec<Requests>, EvmError> {
                if let Some(withdrawals) = &block.body.withdrawals {
                    Self::process_withdrawals(db, withdrawals)?;
                }

                let requests = match vm_type {
                    VMType::L1 => {
                        extract_all_requests_levm(&receipts, db, &block.header, vm_type, crypto)?
                    }
                    VMType::L2(_) => Default::default(),
                };
                Ok(requests)
            })();
            let post_block_bal = db.take_bal().unwrap_or_default();
            let requests = post_block_result?;
            // State transitions for merkleizer come from bal_to_account_updates,
            // not from db — no need to call send_state_transitions_tx here.

            // Validate BAL entries at the withdrawal index against actual
            // post-withdrawal/request state. `saturating_add(1)` prevents a
            // release-build wrap if `n == u32::MAX` (debug_assert on tx count
            // catches this upstream, but belt-and-braces).
            let withdrawal_idx = u32::try_from(block.body.transactions.len())
                .map(|n| n.saturating_add(1))
                .unwrap_or(u32::MAX);
            Self::validate_bal_withdrawal_index(db, &bal, withdrawal_idx, &validation_index)?;

            // Mark storage_reads that occurred during the withdrawal/request phase.
            if !unread_storage_reads.is_empty() {
                for (addr, acct) in &db.current_accounts_state {
                    for key in acct.storage.keys() {
                        unread_storage_reads.remove(&(*addr, *key));
                    }
                }
            }

            // Mark pure-access accounts touched during the withdrawal/request phase.
            // All withdrawal recipients (including 0-amount) are marked because the
            // BAL recorder calls extend_touched_addresses for them, even though
            // process_withdrawals only calls get_account_mut for amount > 0.
            if !unaccessed_pure_accounts.is_empty() {
                if let Some(withdrawals) = &block.body.withdrawals {
                    for w in withdrawals {
                        unaccessed_pure_accounts.remove(&w.address);
                    }
                }
                for canon in post_block_bal.accounts() {
                    // EIP-7928: only user-tx touches legitimize a bare SYSTEM_ADDRESS entry.
                    if canon.address == SYSTEM_ADDRESS {
                        continue;
                    }
                    unaccessed_pure_accounts.remove(&canon.address);
                }
            }

            Self::validate_bal_covers(
                &db.current_accounts_state,
                &post_block_bal,
                &bal,
                &validation_index,
                "the withdrawal/request phase",
                withdrawal_bal_idx,
            )?;

            // EIP-7928 records withdrawal recipients regardless of amount, but
            // `process_withdrawals` only loads the account for amount > 0.
            if let Some(withdrawals) = &block.body.withdrawals {
                for w in withdrawals {
                    if !validation_index.addr_to_idx.contains_key(&w.address) {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed: withdrawal recipient {:?} is missing \
                             from BAL",
                            w.address
                        )));
                    }
                }
            }

            // Any remaining unread storage_reads are extraneous BAL entries.
            if let Some((addr, key)) = unread_storage_reads.iter().next() {
                let slot = ethrex_common::BigEndianHash::into_uint(key);
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: storage_read for account {addr:?} slot \
                     {slot} was never actually read during block execution"
                )));
            }

            // Any remaining pure-access accounts were never accessed during execution.
            if let Some(addr) = unaccessed_pure_accounts.iter().next() {
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: account {addr:?} has no mutations \
                     and no storage reads but was never accessed during block execution"
                )));
            }

            // Only the Engine API checked ordering. Two changes at one index pass the
            // `find_exact_change_*` lookups (first entry) while synthesis uses the last.
            bal.validate_ordering()
                .map_err(|e| EvmError::Custom(format!("BAL validation failed: {e}")))?;

            // EIP-7928 size cap: validated after execution so that transaction-level
            // errors (e.g. gas allowance exceeded) take priority.
            validate_block_access_list_size(&block.header, &chain_config, &bal)
                .map_err(|e| EvmError::Custom(e.to_string()))?;

            // EIP-8079: burn basis is post-refund Σ gas_spent.
            // Each receipt's cumulative_gas_used += report.gas_spent (post-refund);
            // the last receipt holds the block total. Compute before `receipts` is moved.
            let post_refund_gas = receipts.last().map(|r| r.cumulative_gas_used).unwrap_or(0);
            let burned_fees_par =
                is_lstar.then(|| lstar_burned_fees(&chain_config, &block.header, post_refund_gas));

            return Ok((
                BlockExecutionResult {
                    receipts,
                    requests,
                    block_gas_used,
                    burned_fees: burned_fees_par,
                    tx_gas_breakdowns,
                },
                None,
            ));
        }

        // Sequential path (existing code, for block production and non-Amsterdam).
        // The non-BAL caller always provides a Sender; the BAL path returned above.
        // Surface a missing Sender as a normal error instead of panicking, so a
        // future refactor that reshapes the BAL branch can't silently break the
        // contract and bring down the executor thread.
        let Some(merkleizer) = merkleizer else {
            return Err(EvmError::Custom(
                "sequential execution path called without a merkleizer Sender".to_string(),
            ));
        };
        if is_amsterdam {
            db.enable_bal_recording();
            // Set index 0 for pre-execution phase (system contracts)
            db.set_bal_index(0);
        }

        Self::prepare_block(block, db, vm_type, crypto)?;

        // Compute base blob fee once for the entire block (block-invariant).
        let base_blob_fee_per_gas =
            get_base_fee_per_blob_gas(block.header.excess_blob_gas, &evm_config)?;

        let mut shared_stack_pool = Vec::with_capacity(STACK_LIMIT);
        // Holds at most one root memory buffer at a time (each tx pops one and reclaims one).
        let mut shared_memory_pool = Vec::with_capacity(1);

        let n_txs = block.body.transactions.len();
        let mut receipts = Vec::with_capacity(n_txs);
        let mut tx_gas_breakdowns: Vec<TxGasBreakdown> = Vec::with_capacity(n_txs);
        // Cumulative gas for receipts (POST-REFUND per EIP-7778)
        let mut cumulative_gas_used = 0_u64;
        // Block gas accounting (PRE-REFUND for Amsterdam+ per EIP-7778)
        let mut block_gas_used = 0_u64;
        // EIP-8037 (Amsterdam+): track regular and state gas separately for block-level max()
        let mut block_regular_gas_used = 0_u64;
        let mut block_state_gas_used = 0_u64;
        // Starts at 2 to account for the two precompile calls done in `Self::prepare_block`.
        // The value itself can be safely changed.
        let mut tx_since_last_flush = 2;

        for (tx_idx, (tx, tx_sender)) in transactions_with_sender.into_iter().enumerate() {
            // Pre-tx gas limit guard:
            // Pre-Amsterdam: reject tx if cumulative post-refund gas + tx.gas > block limit.
            // Amsterdam+: skip — EIP-8037's 2D gas model means cumulative gas (regular +
            // state) can legally exceed the block gas limit as long as
            // max(sum_regular, sum_state) stays within it. Block-level overflow is
            // detected post-execution.
            if !is_amsterdam {
                check_gas_limit(cumulative_gas_used, tx.gas_limit(), block.header.gas_limit)?;
            }

            // EIP-8037 (Amsterdam+, PR #2703): per-tx 2D inclusion check.
            if is_amsterdam {
                check_2d_gas_allowance(
                    tx,
                    block_regular_gas_used,
                    block_state_gas_used,
                    block.header.gas_limit,
                )?;
            }

            // Set BAL index for this transaction (1-indexed per EIP-7928)
            if is_amsterdam {
                let bal_index = u32::try_from(tx_idx + 1).unwrap_or(u32::MAX);
                db.set_bal_index(bal_index);

                // Record tx sender for BAL. The recipient is recorded when the prepare
                // region loads it (default_hook), per the EIP-7928 v7.1.0 update: an
                // EIP-7702 auth halt before the recipient load must exclude it.
                if let Some(recorder) = db.bal_recorder_mut() {
                    recorder.record_touched_address(tx_sender);
                }
            }

            let report = Self::execute_tx_in_block(
                tx,
                tx_sender,
                &block.header,
                db,
                vm_type,
                base_blob_fee_per_gas,
                &mut shared_stack_pool,
                &mut shared_memory_pool,
                false,
                crypto,
                evm_config,
                chain_id,
                stateless_validator,
            )?;

            tx_gas_breakdowns.push(TxGasBreakdown::from_report(
                tx_idx,
                tx.hash(crypto),
                &report,
            ));

            if queue_length.load(Ordering::Relaxed) == 0 && tx_since_last_flush > 5 {
                LEVM::send_state_transitions_tx(&merkleizer, db, queue_length)?;
                tx_since_last_flush = 0;
            } else {
                tx_since_last_flush += 1;
            }

            // EIP-7778: gas_spent (POST-REFUND) for receipt cumulative_gas_used
            cumulative_gas_used += report.gas_spent;

            // EIP-8037 (Amsterdam+): block_gas_used = max(sum_regular, sum_state)
            // For pre-Amsterdam, state_gas_used is always 0 so gas_used == regular_gas.
            let tx_state_gas = report.state_gas_used;
            let tx_regular_gas = report.gas_used.saturating_sub(tx_state_gas);
            block_regular_gas_used = block_regular_gas_used.saturating_add(tx_regular_gas);
            block_state_gas_used = block_state_gas_used.saturating_add(tx_state_gas);

            if is_amsterdam {
                // Amsterdam+: block gas = max(regular_sum, state_sum)
                block_gas_used = block_regular_gas_used.max(block_state_gas_used);

                // DoS protection: early exit if either regular or state gas exceeds the limit.
                // Since block_gas_used = max(regular, state), if either component exceeds
                // the limit, we know the block is invalid and can safely reject without
                // violating EIP-8037 semantics.
                if block_regular_gas_used > block.header.gas_limit
                    || block_state_gas_used > block.header.gas_limit
                {
                    return Err(EvmError::Transaction(format!(
                        "Gas allowance exceeded: Block gas used overflow: \
                         block_gas_used {block_gas_used} > block_gas_limit {}",
                        block.header.gas_limit
                    )));
                }
            } else {
                block_gas_used = block_gas_used.saturating_add(report.gas_used);
            }

            let mut receipt = Receipt::new(
                tx.tx_type(),
                matches!(report.result, TxResult::Success),
                cumulative_gas_used,
                report.logs,
            );

            // For frame transactions, propagate payer and per-frame receipts
            if matches!(tx, Transaction::FrameTransaction(_)) {
                receipt.payer = report.payer_address;
                receipt.frame_receipts = frame_receipts_from(report.frame_results);
            }

            receipts.push(receipt);
        }

        // EIP-7778 (Amsterdam+): block-level gas overflow check.
        // Per-tx checks are skipped for Amsterdam because block gas is computed
        // from pre-refund values; overflow can only be detected after execution.
        if is_amsterdam && block_gas_used > block.header.gas_limit {
            return Err(EvmError::Transaction(format!(
                "Gas allowance exceeded: Block gas used overflow: \
                 block_gas_used {block_gas_used} > block_gas_limit {}",
                block.header.gas_limit
            )));
        }

        #[cfg(feature = "perf_opcode_timings")]
        {
            let mut timings = OPCODE_TIMINGS.lock().expect("poison");
            timings.inc_tx_count(receipts.len());
            timings.inc_block_count();
            ::tracing::info!("{}", timings.info_pretty());
            let precompiles_timings = PRECOMPILES_TIMINGS.lock().expect("poison");
            ::tracing::info!("{}", precompiles_timings.info_pretty());
        }

        if queue_length.load(Ordering::Relaxed) == 0 {
            LEVM::send_state_transitions_tx(&merkleizer, db, queue_length)?;
        }

        // Set BAL index for post-execution phase (withdrawals + requests); see the
        // ordering note on the sequential path above.
        if is_amsterdam {
            let post_tx_index =
                u32::try_from(block.body.transactions.len() + 1).unwrap_or(u32::MAX);
            db.set_bal_index(post_tx_index);

            // Record ALL withdrawal recipients for BAL per EIP-7928
            if let Some(withdrawals) = &block.body.withdrawals
                && let Some(recorder) = db.bal_recorder_mut()
            {
                recorder.extend_touched_addresses(withdrawals.iter().map(|w| w.address));
            }
        }

        // Withdrawals precede the request system calls; see the ordering note on the
        // sequential path above.
        if let Some(withdrawals) = &block.body.withdrawals {
            Self::process_withdrawals(db, withdrawals)?;
        }

        // TODO: I don't like deciding the behavior based on the VMType here.
        // TODO2: Revise this, apparently extract_all_requests_levm is not called
        // in L2 execution, but its implementation behaves differently based on this.
        let requests = match vm_type {
            VMType::L1 => extract_all_requests_levm(&receipts, db, &block.header, vm_type, crypto)?,
            VMType::L2(_) => Default::default(),
        };
        LEVM::send_state_transitions_tx(&merkleizer, db, queue_length)?;

        // Extract BAL if recording was enabled
        let bal = db.take_bal();

        Ok((
            BlockExecutionResult {
                receipts,
                requests,
                block_gas_used,
                // cumulative_gas_used is accumulated as += report.gas_spent (post-refund).
                burned_fees: is_lstar
                    .then(|| lstar_burned_fees(&chain_config, &block.header, cumulative_gas_used)),
                tx_gas_breakdowns,
            },
            bal,
        ))
    }

    ///
    /// For each account in the BAL, extracts the **final** post-block state
    /// (highest `block_access_index` entry per field) and builds an AccountUpdate.
    /// State comes entirely from the BAL — no execution needed.
    #[cfg(feature = "rayon")]
    fn bal_to_account_updates(
        bal: &BlockAccessList,
        store: &dyn Database,
    ) -> Result<Vec<AccountUpdate>, EvmError> {
        use ethrex_common::types::AccountInfo;

        let mut updates = Vec::new();

        // Batch prefetch all accounts with writes so per-account lookups are cache hits
        let write_addrs: Vec<Address> = bal
            .accounts()
            .iter()
            .filter(|ac| {
                !ac.balance_changes.is_empty()
                    || !ac.nonce_changes.is_empty()
                    || !ac.code_changes.is_empty()
                    || !ac.storage_changes.is_empty()
            })
            .map(|ac| ac.address)
            .collect();
        // Best-effort: the loop below reads every account it needs through
        // `get_account_state`, so a failed warm costs cache hits and nothing else.
        // Propagating it here would let a prefetch turn into a block-processing error.
        let _ = store.prefetch_accounts(&write_addrs);

        for acct_changes in bal.accounts() {
            let addr = acct_changes.address;

            // Skip accounts with only reads and no writes
            let has_writes = !acct_changes.balance_changes.is_empty()
                || !acct_changes.nonce_changes.is_empty()
                || !acct_changes.code_changes.is_empty()
                || !acct_changes.storage_changes.is_empty();
            if !has_writes {
                continue;
            }

            // Load pre-state for unchanged fields (cache hit after prefetch)
            let prestate = store
                .get_account_state(addr)
                .map_err(|e| EvmError::Custom(format!("bal_to_account_updates: {e}")))?;

            // Final balance: last entry (highest index) or prestate
            let balance = acct_changes
                .balance_changes
                .last()
                .map(|c| c.post_balance)
                .unwrap_or(prestate.balance);

            // Final nonce: last entry or prestate
            let nonce = acct_changes
                .nonce_changes
                .last()
                .map(|c| c.post_nonce)
                .unwrap_or(prestate.nonce);

            // Final code: last entry or prestate
            let (code_hash, code) = if let Some(c) = acct_changes.code_changes.last() {
                code_from_bal(&c.new_code)
            } else {
                (prestate.code_hash, None)
            };

            // Storage: per slot, last entry (highest index)
            let mut added_storage = FxHashMap::with_capacity_and_hasher(
                acct_changes.storage_changes.len(),
                Default::default(),
            );
            for slot_change in &acct_changes.storage_changes {
                if let Some(last) = slot_change.slot_changes.last() {
                    let key = ethrex_common::utils::u256_to_h256(slot_change.slot);
                    added_storage.insert(key, last.post_value);
                }
            }

            // Detect account removal (EIP-161): post-state empty but pre-state existed
            let post_empty = balance.is_zero() && nonce == 0 && code_hash == *EMPTY_KECCAK_HASH;
            let pre_empty = prestate.balance.is_zero()
                && prestate.nonce == 0
                && prestate.code_hash == *EMPTY_KECCAK_HASH;
            let removed = post_empty && !pre_empty;

            let balance_changed = acct_changes
                .balance_changes
                .last()
                .is_some_and(|c| c.post_balance != prestate.balance);
            let nonce_changed = acct_changes
                .nonce_changes
                .last()
                .is_some_and(|c| c.post_nonce != prestate.nonce);
            let code_changed = acct_changes.code_changes.last().is_some();
            let acc_info_updated = balance_changed || nonce_changed || code_changed;

            if !removed && !acc_info_updated && added_storage.is_empty() {
                continue;
            }

            let info = if acc_info_updated {
                Some(AccountInfo {
                    code_hash,
                    balance,
                    nonce,
                })
            } else {
                None
            };

            let update = AccountUpdate {
                address: addr,
                removed,
                info,
                code,
                added_storage,
                // EIP-6780 restricts SELFDESTRUCT to the creation tx, so
                // cross-tx storage wipes can't happen. For the rare same-tx
                // destroy+recreate case at a reused address, EIP-7928 records
                // individual slot zeroing in storage_changes (each old slot → 0),
                // so `added_storage` already contains those zeroed entries and
                // the trie update is correct without setting removed_storage.
                removed_storage: false,
            };
            updates.push(update);
        }

        Ok(updates)
    }

    /// Validates a system phase the per-tx shadow recorder doesn't cover, against
    /// `phase_bal`, the BAL that phase's own recorder built. Every canonical entry must
    /// be in the supplied BAL with matching values, and nothing may be declared at
    /// `phase_idx` that the phase didn't do. The state root can't catch a mismatch:
    /// the parallel merkleizer derives it from the supplied BAL.
    #[cfg(feature = "rayon")]
    fn validate_bal_covers(
        cache: &CacheDB,
        phase_bal: &BlockAccessList,
        bal: &BlockAccessList,
        index: &BalAddressIndex,
        phase: &str,
        phase_idx: u32,
    ) -> Result<(), EvmError> {
        // No SYSTEM_ADDRESS exemption: the recorder already drops it inside system calls,
        // so it only reaches `phase_bal` when something else changed it.
        for canon in phase_bal.accounts() {
            let addr = canon.address;
            let Some(supplied) = index
                .addr_to_idx
                .get(&addr)
                .and_then(|i| bal.accounts().get(*i))
            else {
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: account {addr:?} was accessed during {phase} \
                     but is missing from BAL"
                )));
            };

            // By value, not membership, so a write demoted to a read is caught.
            for canon_slot in &canon.storage_changes {
                let slot = canon_slot.slot;
                let supplied_changes = supplied
                    .storage_changes
                    .binary_search_by(|sc| sc.slot.cmp(&slot))
                    .ok()
                    .and_then(|i| supplied.storage_changes.get(i))
                    .map(|sc| sc.slot_changes.as_slice())
                    .unwrap_or_default();
                for canon_change in &canon_slot.slot_changes {
                    let idx = canon_change.block_access_index;
                    match find_exact_change_storage(supplied_changes, idx) {
                        Some(v) if v == canon_change.post_value => {}
                        Some(v) => {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed: account {addr:?} storage slot {slot} \
                                 was written during {phase} to {} but the BAL declares {v} \
                                 at index {idx}",
                                canon_change.post_value
                            )));
                        }
                        None => {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed: account {addr:?} storage slot {slot} \
                                 was written during {phase} to {} but the BAL declares no \
                                 change at index {idx}",
                                canon_change.post_value
                            )));
                        }
                    }
                }
            }

            for canon_change in &canon.balance_changes {
                let idx = canon_change.block_access_index;
                let expected = canon_change.post_balance;
                if find_exact_change_balance(&supplied.balance_changes, idx) != Some(expected) {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: account {addr:?} balance changed to {expected} \
                         during {phase} but the BAL declares no matching change at index {idx}"
                    )));
                }
            }
            for canon_change in &canon.nonce_changes {
                let idx = canon_change.block_access_index;
                let expected = canon_change.post_nonce;
                if find_exact_change_nonce(&supplied.nonce_changes, idx) != Some(expected) {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: account {addr:?} nonce changed to {expected} \
                         during {phase} but the BAL declares no matching change at index {idx}"
                    )));
                }
            }
            for canon_change in &canon.code_changes {
                let idx = canon_change.block_access_index;
                if find_exact_change_code(&supplied.code_changes, idx)
                    != Some(&canon_change.new_code)
                {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: account {addr:?} code changed during {phase} \
                         but the BAL declares no matching change at index {idx}"
                    )));
                }
            }

            // A read shows up as a change if a later transaction writes the slot.
            for &slot in &canon.storage_reads {
                if supplied.storage_reads.binary_search(&slot).is_err()
                    && supplied
                        .storage_changes
                        .binary_search_by(|sc| sc.slot.cmp(&slot))
                        .is_err()
                {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: storage slot {slot} of account {addr:?} was \
                         read during {phase} but is missing from BAL"
                    )));
                }
            }
        }

        // Nothing extra at the phase's own index: a no-op change there matches the
        // cache, so `validate_bal_withdrawal_index` alone accepts it.
        let canonical: FxHashMap<Address, &AccountChanges> = phase_bal
            .accounts()
            .iter()
            .map(|a| (a.address, a))
            .collect();
        for supplied in bal.accounts() {
            let addr = supplied.address;
            let canon = canonical.get(&addr).copied();
            for sc in &supplied.storage_changes {
                if find_exact_change_storage(&sc.slot_changes, phase_idx).is_none() {
                    continue;
                }
                let declared_canonically = canon.is_some_and(|c| {
                    c.storage_changes
                        .binary_search_by(|cs| cs.slot.cmp(&sc.slot))
                        .ok()
                        .and_then(|i| c.storage_changes.get(i))
                        .is_some_and(|cs| {
                            find_exact_change_storage(&cs.slot_changes, phase_idx).is_some()
                        })
                });
                if !declared_canonically {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: account {addr:?} declares a storage change \
                         for slot {} at index {phase_idx} but {phase} did not write it",
                        sc.slot
                    )));
                }
            }
            if find_exact_change_balance(&supplied.balance_changes, phase_idx).is_some()
                && canon.is_none_or(|c| {
                    find_exact_change_balance(&c.balance_changes, phase_idx).is_none()
                })
            {
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: account {addr:?} declares a balance change at \
                     index {phase_idx} but {phase} did not change its balance"
                )));
            }
            if find_exact_change_nonce(&supplied.nonce_changes, phase_idx).is_some()
                && canon
                    .is_none_or(|c| find_exact_change_nonce(&c.nonce_changes, phase_idx).is_none())
            {
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: account {addr:?} declares a nonce change at \
                     index {phase_idx} but {phase} did not change its nonce"
                )));
            }
            if find_exact_change_code(&supplied.code_changes, phase_idx).is_some()
                && canon
                    .is_none_or(|c| find_exact_change_code(&c.code_changes, phase_idx).is_none())
            {
                return Err(EvmError::Custom(format!(
                    "BAL validation failed: account {addr:?} declares a code change at \
                     index {phase_idx} but {phase} did not change its code"
                )));
            }
        }

        // Backstop from the cache, independent of the recorder.
        for (addr, acct) in cache {
            if *addr == SYSTEM_ADDRESS {
                continue;
            }
            let supplied = match index
                .addr_to_idx
                .get(addr)
                .and_then(|i| bal.accounts().get(*i))
            {
                Some(a) => a,
                // Cached storage means a real access; info-only probes load no slots.
                None if !acct.storage.is_empty() => {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: account {addr:?} was accessed during {phase} \
                         but is missing from BAL"
                    )));
                }
                None => continue,
            };
            // storage_reads is strictly ascending, as at the per-tx check; binary
            // search fails closed on an unsorted list, so release stays sound.
            debug_assert!(
                supplied.storage_reads.windows(2).all(|w| w[0] < w[1]),
                "storage_reads must be strictly ascending for binary_search"
            );
            for key in acct.storage.keys() {
                let slot = ethrex_common::BigEndianHash::into_uint(key);
                if supplied.storage_reads.binary_search(&slot).is_err()
                    && supplied
                        .storage_changes
                        .binary_search_by(|sc| sc.slot.cmp(&slot))
                        .is_err()
                {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed: storage slot {slot} of account {addr:?} was \
                         read during {phase} but is missing from BAL"
                    )));
                }
            }
        }
        Ok(())
    }

    /// Eager BAL prefix seed — used only by the outer DB path (parallel-execution
    /// fallback recovery and post-tx outer seed before request extraction).
    /// Per-tx parallel execution uses `LazyBalCursor` in `execute_block_parallel`;
    /// see also `seed_one_address_info_from_bal` and `seed_one_storage_slot_from_bal`
    /// in `ethrex_levm::db::gen_db`.
    ///
    /// Pre-seed a GeneralizedDatabase with BAL-derived state for a specific tx.
    ///
    /// For each BAL-modified account, applies accumulated diffs with
    /// `block_access_index <= max_idx` on top of the loaded pre-block state.
    /// This matches geth's approach: each parallel tx sees the state as if
    /// all previous txs had already executed (via BAL intermediate values).
    ///
    /// `max_idx` is the BAL block_access_index of the last tx whose effects
    /// should be visible. BAL indexing: 0 = system calls, 1 = tx 0, 2 = tx 1, ...
    /// For tx at index `i`, pass `max_idx = i` (diffs with index <= i = system + txs 0..i-1).
    #[cfg(feature = "rayon")]
    fn seed_db_from_bal(
        db: &mut GeneralizedDatabase,
        bal: &BlockAccessList,
        max_idx: u32,
        accounts_by_min_index: &[(u32, usize)],
    ) -> Result<(), EvmError> {
        let end = accounts_by_min_index.partition_point(|(min_idx, _)| *min_idx <= max_idx);
        let bal_accounts = bal.accounts();
        for &(_, acct_idx) in &accounts_by_min_index[..end] {
            seed_one_address_info_from_bal(db, bal, acct_idx, max_idx)
                .map_err(|e| EvmError::Custom(format!("seed_db_from_bal: {e}")))?;

            let acct_changes = &bal_accounts[acct_idx];
            if acct_changes.storage_changes.is_empty() {
                continue;
            }
            let any_storage = acct_changes.storage_changes.iter().any(|sc| {
                sc.slot_changes
                    .first()
                    .is_some_and(|c| c.block_access_index <= max_idx)
            });
            if !any_storage {
                continue;
            }
            let addr = acct_changes.address;
            if !db.current_accounts_state.contains_key(&addr) {
                db.get_account(addr)
                    .map_err(|e| EvmError::Custom(format!("seed storage: {e}")))?;
            }
            let acc = db
                .get_account_mut(addr)
                .map_err(|e| EvmError::Custom(format!("seed storage mut: {e}")))?;
            for sc in &acct_changes.storage_changes {
                if let Some(value) = post_value_at_or_before(sc, max_idx) {
                    acc.storage
                        .insert(ethrex_common::utils::u256_to_h256(sc.slot), value);
                }
            }
        }
        Ok(())
    }

    /// Execute block transactions in parallel using BAL-derived state.
    /// Only called for Amsterdam+ blocks when the header BAL is available.
    ///
    /// Each tx runs independently on its own database pre-seeded with BAL
    /// intermediate state (geth-style). State for the merkleizer comes from
    /// `bal_to_account_updates`, not from tx execution.
    #[cfg(feature = "rayon")]
    #[allow(clippy::too_many_arguments, clippy::type_complexity)]
    fn execute_block_parallel(
        block: &Block,
        txs_with_sender: &[(&Transaction, Address)],
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        bal: Arc<BlockAccessList>,
        merkleizer: Option<&Sender<Vec<AccountUpdate>>>,
        queue_length: &AtomicUsize,
        system_seed: Arc<CacheDB>,
        pre_block_bal: &BlockAccessList,
        crypto: &dyn Crypto,
        validation_index: Arc<BalAddressIndex>,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<
        (
            Vec<Receipt>,
            u64,
            FxHashSet<(Address, H256)>,
            FxHashSet<Address>,
            Vec<TxGasBreakdown>,
        ),
        EvmError,
    > {
        let store = db.store.clone();
        let header = &block.header;
        let n_txs = txs_with_sender.len();
        // BAL-seeded parallel execution is only reachable on Amsterdam+ (callers
        // gate on is_amsterdam before providing a header BAL). We recompute the
        // flag here to gate the 2D inclusion check explicitly, keeping the
        // invariant checkable rather than implicit.
        let chain_config = store.get_chain_config()?;
        let is_amsterdam = chain_config.is_amsterdam_activated(header.timestamp);
        // Block-invariant EVM config + chain id, computed once and shared across the
        // parallel workers (both are `Copy` + `Send`/`Sync`).
        let evm_config = EVMConfig::new_from_chain_config(&chain_config, header);
        let chain_id = chain_config.chain_id;
        // Block-invariant base blob fee, computed once and shared across workers.
        let base_blob_fee_per_gas = get_base_fee_per_blob_gas(header.excess_blob_gas, &evm_config)?;
        debug_assert!(
            is_amsterdam,
            "execute_block_parallel invoked on non-Amsterdam block"
        );

        // 1. Convert BAL → AccountUpdates and send to merkleizer (single batch).
        // Skipped when the caller merkleizes optimistically from the input BAL; the
        // conversion is then redundant work (and does pre-state reads we don't need).
        if let Some(merkleizer) = merkleizer {
            let account_updates = Self::bal_to_account_updates(&bal, store.as_ref())?;
            merkleizer
                .send(account_updates)
                .map_err(|e| EvmError::Custom(format!("merkleizer send failed: {e}")))?;
            queue_length.fetch_add(1, Ordering::Relaxed);
        }

        // Build a checklist of all BAL storage_reads. Entries are removed as they
        // are actually read during execution phases. Anything left over is extraneous.
        let mut unread_storage_reads: FxHashSet<(Address, H256)> = FxHashSet::default();
        // Build a checklist of BAL "pure-access" accounts: entries with no mutations
        // and no storage reads. These must be accessed via load_account during execution.
        let mut unaccessed_pure_accounts: FxHashSet<Address> = FxHashSet::default();
        for acct in bal.accounts() {
            for &slot in &acct.storage_reads {
                let key = ethrex_common::utils::u256_to_h256(slot);
                unread_storage_reads.insert((acct.address, key));
            }
            let is_pure = acct.storage_changes.is_empty()
                && acct.storage_reads.is_empty()
                && acct.balance_changes.is_empty()
                && acct.nonce_changes.is_empty()
                && acct.code_changes.is_empty();
            if is_pure {
                unaccessed_pure_accounts.insert(acct.address);
            }
        }

        // Mark pure-access accounts that were touched during system calls, per the
        // recorder: `system_seed` also holds unrecorded internal loads.
        // EIP-7928: SYSTEM_ADDRESS is excluded from BAL entries created by system calls
        // (only user-tx touches legitimize it). Keep it in `unaccessed_pure_accounts` so a
        // BAL that carries a bare SYSTEM_ADDRESS entry without a corresponding user-tx
        // touch is rejected as extraneous.
        for canon in pre_block_bal.accounts() {
            if canon.address == SYSTEM_ADDRESS {
                continue;
            }
            unaccessed_pure_accounts.remove(&canon.address);
        }

        // Mark storage reads that occurred during system calls (prepare_block).
        unread_storage_reads.retain(|(addr, key)| {
            !system_seed
                .get(addr)
                .is_some_and(|a| a.storage.contains_key(key))
        });

        // Already-owned Arcs from the caller; per-thread/per-tx uses below are pointer clones.
        let arc_bal = bal;
        let arc_idx = validation_index;

        // 2. Execute all txs in parallel (embarrassingly parallel, BAL-seeded).
        //    BAL validation runs INSIDE the par_iter closure (parallel) but its
        //    errors are deferred via Option<EvmError> so the post-par_iter
        //    gas-limit check still takes priority (GAS_USED_OVERFLOW must beat
        //    BAL mismatch on blocks exceeding the gas limit; the BAL is built
        //    assuming rejected txs, so miner balance in the BAL won't match
        //    execution that ran all txs).
        //
        //    The closure also precomputes the small (Vec<(Address, H256)>,
        //    Vec<Address>) inputs needed to update the shared
        //    `unread_storage_reads` / `unaccessed_pure_accounts` sets, so the
        //    serial pass after par_iter is just hash-set ops; current_state
        //    and codes never cross the rayon boundary.
        type TxExecResult = (
            usize,
            TxType,
            ExecutionReport,
            FxHashSet<Address>,   // accessed_accounts tracker (coarse)
            Vec<(Address, H256)>, // reads_satisfied: (addr, slot) loaded during this tx
            Vec<Address>,         // destroyed: accounts selfdestructed during this tx
            Option<EvmError>,     // deferred BAL validation error
        );

        // Ordered gas admission (step 3 below) can only reject a block once every
        // transaction has been executed and its report retained. Two checks make an
        // over-limit block cheap to reject, and both hold for any block that could be
        // valid.
        //
        // Before any execution: the transactions' minimum gas alone may already rule
        // the block out.
        check_minimum_block_work(
            txs_with_sender.iter().map(|(tx, sender)| (*tx, *sender)),
            chain_config.fork(header.timestamp),
            header.gas_limit,
        )?;

        // During execution: stop starting transactions once the finished ones are over
        // the gas limit, which bounds both the work done and the reports retained.
        let completed_gas = CompletedGas::default();

        let exec_results: Result<Vec<TxExecResult>, EvmError> = (0..n_txs)
            .into_par_iter()
            .map(|tx_idx| -> Result<_, EvmError> {
                completed_gas.check(header.gas_limit)?;
                let (tx, sender) = &txs_with_sender[tx_idx];
                // Small capacity hint — per-tx DBs materialize only touched accounts via lazy_bal cursor.
                let mut tx_db = GeneralizedDatabase::new_with_shared_base_and_capacity(
                    store.clone(),
                    system_seed.clone(),
                    32,
                );
                tx_db.lazy_bal = Some(LazyBalCursor {
                    bal: arc_bal.clone(),
                    bal_index: u32::try_from(tx_idx + 1).unwrap_or(u32::MAX),
                    index: arc_idx.clone(),
                });
                // Small capacity: parallel txs rarely nest >8 call frames, and
                // over-allocating per-tx wastes memory across many rayon tasks.
                let mut stack_pool = Vec::with_capacity(8);
                // Holds at most one root memory buffer (popped + reclaimed per tx).
                let mut memory_pool = Vec::with_capacity(1);

                // Enable accessed_accounts tracker (coarse) for `unaccessed_pure_accounts`
                // diagnostics. Safe to over-report: used only to REMOVE entries from a
                // extraneous-entry checklist.
                tx_db.accessed_accounts =
                    Some(FxHashSet::with_capacity_and_hasher(16, Default::default()));

                // Enable a shadow BAL recorder on this per-tx db. The recorder is gated
                // at the same gas-check points as the builder path, giving us an exact
                // EIP-7928 access signal (missing-account and missing-storage-read
                // detection). Per-tx recorder — no cross-task contention.
                tx_db.enable_bal_recording();
                let bal_index = u32::try_from(tx_idx + 1).unwrap_or(u32::MAX);
                tx_db.set_bal_index(bal_index);
                // Record tx sender for BAL. The recipient is recorded when the prepare
                // region loads it (default_hook), per the EIP-7928 v7.1.0 update: an
                // EIP-7702 auth halt before the recipient load must exclude it.
                if let Some(recorder) = tx_db.bal_recorder_mut() {
                    recorder.record_touched_address(*sender);
                }

                let report = LEVM::execute_tx_in_block(
                    tx,
                    *sender,
                    header,
                    &mut tx_db,
                    vm_type,
                    base_blob_fee_per_gas,
                    &mut stack_pool,
                    &mut memory_pool,
                    false,
                    crypto,
                    evm_config,
                    chain_id,
                    stateless_validator,
                )?;
                let mut report = report;

                // Block validation builds receipts from `logs` and never reads the
                // top-level return data, so holding it until the collect below would
                // retain a per-transaction buffer nothing downstream consumes.
                report.output = Bytes::new();

                // Same regular/state split as the ordered admission loop below.
                completed_gas.record(
                    report.gas_used.saturating_sub(report.state_gas_used),
                    report.state_gas_used,
                    header.gas_limit,
                )?;

                let current_state = std::mem::take(&mut tx_db.current_accounts_state);
                let codes = std::mem::take(&mut tx_db.codes);
                let mut tracked = tx_db.accessed_accounts.take().unwrap_or_default();
                // `tx_initial` maps each genuinely-written slot to its start-of-tx
                // value (see `take_tx_initial_storage`). It serves the storage checks two
                // ways: its keys are the written slots, and its values are the seeded
                // pre-state the validator would otherwise re-read from the store. The
                // recorder already stores it as an `FxHashMap`, so it is moved out here
                // (no per-tx rebuild).
                let (shadow_touched, shadow_reads, tx_initial) = tx_db
                    .bal_recorder
                    .take()
                    .map(|mut r| {
                        (
                            r.take_touched_addresses(),
                            r.take_storage_reads(),
                            r.take_tx_initial_storage(),
                        )
                    })
                    .unwrap_or_default();
                // Genuine pure reads = observed reads minus written slots. A slot read
                // then written stays in `shadow_reads` (the recorder promotes it only in
                // `build()`, which the shadow path skips) and SSTORE always records an
                // implicit read — so this subtraction is required for correctness, not
                // just efficiency: without it a written slot would be treated as a pure
                // read and its BAL entry skipped. The execution->BAL check skips the
                // DB-backed seeded lookup for genuine reads: a read never diverges from
                // its pre-tx value, so there is nothing to validate.
                let pure_reads: FxHashSet<(Address, U256)> = shadow_reads
                    .iter()
                    .copied()
                    .filter(|k| !tx_initial.contains_key(k))
                    .collect();

                // Precompute the per-tx inputs the serial pass uses to update
                // the shared unread_storage_reads set. Selfdestruct clears
                // storage from the final state, so a destroyed account's
                // slots are no longer visible in `current_state`. That does
                // NOT mean every BAL storage_reads entry for the account is
                // legitimate: a phantom (never-read) declared read must
                // still be rejected. Only slots the shadow recorder actually
                // observed (`shadow_reads`, the EIP-7928 access authority)
                // are folded into `reads_satisfied` below, once `destroyed`
                // is populated.
                // Rough avg storage slots per touched account; over-allocation
                // is cheap compared to 2-3 reallocations on the hot path.
                let mut reads_satisfied: Vec<(Address, H256)> =
                    Vec::with_capacity(current_state.len() * 4);
                // `destroyed` stays empty on the typical block (selfdestruct
                // is rare post-EIP-6780), so `Vec::new()` (no allocation) is
                // optimal here.
                let mut destroyed: Vec<Address> = Vec::new();
                for (addr, acct) in &current_state {
                    if matches!(
                        acct.status,
                        AccountStatus::Destroyed | AccountStatus::DestroyedModified
                    ) {
                        destroyed.push(*addr);
                    } else {
                        for key in acct.storage.keys() {
                            reads_satisfied.push((*addr, *key));
                        }
                    }
                }
                // A destroyed account's genuinely-read slots (per the shadow
                // recorder) still satisfy their BAL storage_reads entry even
                // though the storage itself is gone from `current_state`. A
                // slot the BAL merely *declares* as read but this tx never
                // actually touched is deliberately left unsatisfied here: it
                // stays in `unread_storage_reads` and is rejected by the
                // leftover-reads check as a phantom read.
                for (addr, slot) in &shadow_reads {
                    if destroyed.contains(addr) {
                        reads_satisfied.push((*addr, ethrex_common::utils::u256_to_h256(*slot)));
                    }
                }

                // Run BAL validation inline. Errors are DEFERRED: stored in
                // Option<EvmError> so the serial gas-limit check below still
                // takes priority. Borrow current_state / codes during the
                // validation closure, then drop them before returning so
                // they don't cross the rayon boundary.
                let deferred_bal_err: Option<EvmError> = (|| -> Result<(), EvmError> {
                    let bal_idx = u32::try_from(tx_idx + 1).unwrap_or(u32::MAX);
                    let seed_idx = u32::try_from(tx_idx).unwrap_or(u32::MAX);
                    Self::validate_tx_execution(
                        bal_idx,
                        seed_idx,
                        &current_state,
                        &codes,
                        &arc_bal,
                        &arc_idx,
                        &system_seed,
                        &store,
                        &pure_reads,
                        &tx_initial,
                    )
                    .map_err(|e| match e {
                        // A store failure during validation is infrastructure, not a
                        // consensus mismatch — surface it as EvmError::DB so it isn't
                        // mislabelled "BAL validation failed".
                        BalValidationError::Database(msg) => {
                            EvmError::DB(format!("BAL validation for tx {tx_idx}: {msg}"))
                        }
                        BalValidationError::Mismatch(msg) => EvmError::Custom(format!(
                            "BAL validation failed for tx {tx_idx}: {msg}"
                        )),
                    })?;

                    // EIP-7928 (Group B): missing-access detection via shadow recorder.
                    for addr in &shadow_touched {
                        if !arc_idx.addr_to_idx.contains_key(addr) {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for tx {tx_idx}: account {addr:?} was \
                                 accessed during execution but is missing from BAL"
                            )));
                        }
                    }
                    for (addr, slot) in &shadow_reads {
                        let Some(&bal_acct_idx) = arc_idx.addr_to_idx.get(addr) else {
                            // Already caught by the touched-address check above.
                            continue;
                        };
                        let acct = &arc_bal.accounts()[bal_acct_idx];
                        let in_changes = acct
                            .storage_changes
                            .binary_search_by(|sc| sc.slot.cmp(slot))
                            .is_ok();
                        // storage_reads is validated strictly-ascending, so binary search
                        // (matches storage_changes above) instead of a linear scan. The
                        // import path doesn't call `validate_ordering`, so assert the
                        // precondition in debug builds; `binary_search` fails closed on
                        // an unsorted list (it can only miss a present slot -> over-reject,
                        // never accept a missing one), so release stays sound regardless.
                        debug_assert!(
                            acct.storage_reads.windows(2).all(|w| w[0] < w[1]),
                            "storage_reads must be strictly ascending for binary_search"
                        );
                        let in_reads = acct.storage_reads.binary_search(slot).is_ok();
                        if !in_changes && !in_reads {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for tx {tx_idx}: storage slot {slot} of \
                                 account {addr:?} was read during execution but is missing from \
                                 BAL (no storage_changes or storage_reads entry)"
                            )));
                        }
                    }
                    Ok(())
                })()
                .err();

                drop(current_state);
                drop(codes);

                // The shadow BAL recorder's touched addresses are ethrex's exact
                // EELS `account_reads` analog: an address is recorded here iff EELS
                // would add it to `account_reads` (e.g. a CREATE target read by
                // `is_account_alive` before an OOG state-gas charge). The coarse
                // `accessed_accounts` tracker only sees `load_account` calls, which
                // can miss such a target when the frame OOGs before materializing it.
                // Fold the recorder touches into `tracked` so the pure-access
                // checklist is reduced by everything ethrex legitimately accessed.
                tracked.extend(shadow_touched.iter().copied());

                Ok((
                    tx_idx,
                    tx.tx_type(),
                    report,
                    tracked,
                    reads_satisfied,
                    destroyed,
                    deferred_bal_err,
                ))
            })
            .collect();

        let mut exec_results = exec_results?;

        // `IndexedParallelIterator` (via `(0..n_txs).into_par_iter()`) preserves
        // source-index order through `.map().collect()`, so `exec_results` is
        // already sorted. The sort is kept as a defensive guard against a future
        // refactor swapping in an unordered iterator; `sort_unstable_by_key` on
        // an already-sorted slice is near-linear via pdqsort, so the cost is
        // negligible.
        exec_results.sort_unstable_by_key(|(idx, _, _, _, _, _, _)| *idx);

        // 3. Gas limit check — must happen BEFORE BAL validation errors so that
        //    blocks exceeding the gas limit produce GAS_USED_OVERFLOW instead of
        //    a BAL mismatch error. EIP-8037 PR #2703: also enforce the per-tx
        //    2D inclusion check against running block totals.
        let mut block_regular_gas_used = 0_u64;
        let mut block_state_gas_used = 0_u64;
        let mut tx_gas_breakdowns: Vec<TxGasBreakdown> = Vec::with_capacity(exec_results.len());
        for (tx_idx, _, report, _, _, _, _) in &exec_results {
            let (tx, _tx_sender) = txs_with_sender
                .get(*tx_idx)
                .ok_or_else(|| EvmError::Custom(format!("tx index {tx_idx} out of bounds")))?;
            if is_amsterdam {
                check_2d_gas_allowance(
                    tx,
                    block_regular_gas_used,
                    block_state_gas_used,
                    header.gas_limit,
                )?;
            }

            tx_gas_breakdowns.push(TxGasBreakdown::from_report(
                *tx_idx,
                tx.hash(crypto),
                report,
            ));

            let tx_state_gas = report.state_gas_used;
            let tx_regular_gas = report.gas_used.saturating_sub(tx_state_gas);
            block_regular_gas_used = block_regular_gas_used.saturating_add(tx_regular_gas);
            block_state_gas_used = block_state_gas_used.saturating_add(tx_state_gas);
        }
        let block_gas_used = block_regular_gas_used.max(block_state_gas_used);
        // EIP-7778: block-level overflow check using pre-refund gas.
        if block_gas_used > header.gas_limit {
            return Err(EvmError::Transaction(format!(
                "Gas allowance exceeded: Block gas used overflow: \
                 block_gas_used {block_gas_used} > block_gas_limit {}",
                header.gas_limit
            )));
        }

        // 4. Surface the BAL errors held back until the gas-limit check has passed:
        //    system-call coverage, then the first deferred per-tx error in tx order.
        Self::validate_bal_covers(
            &system_seed,
            pre_block_bal,
            &arc_bal,
            &arc_idx,
            "system calls",
            PRE_BLOCK_BAL_INDEX,
        )?;

        for (_, _, _, _, _, _, deferred) in &mut exec_results {
            if let Some(err) = deferred.take() {
                return Err(err);
            }
        }

        // 5. Apply per-tx reads_satisfied / tracked to the shared sets (cheap
        //    hash-set ops; preserves prior semantics). Note: this does NOT
        //    blanket-clear a destroyed account's entries — only slots it
        //    genuinely read (already folded into reads_satisfied above,
        //    including destroyed-account reads via shadow_reads) are
        //    removed. A phantom storage_reads entry declared on a destroyed
        //    account (never actually read) stays in unread_storage_reads and
        //    is rejected by the leftover-reads check below.
        for (_, _, _, tracked_accounts, reads_satisfied, _, _) in &exec_results {
            if !unread_storage_reads.is_empty() {
                for pair in reads_satisfied {
                    unread_storage_reads.remove(pair);
                }
            }
            // The coinbase is always accessed during fee finalization (geth's
            // readerTracker records it), even when the miner fee is zero and
            // ethrex skips the load_account call.
            if !unaccessed_pure_accounts.is_empty() {
                unaccessed_pure_accounts.remove(&header.coinbase);
                for addr in tracked_accounts {
                    unaccessed_pure_accounts.remove(addr);
                }
            }
        }

        // 6. Build receipts in tx order.
        let mut receipts = Vec::with_capacity(n_txs);
        let mut cumulative_gas_used = 0_u64;
        for (_, tx_type, report, _, _, _, _) in exec_results {
            cumulative_gas_used += report.gas_spent;
            let mut receipt = Receipt::new(
                tx_type,
                matches!(report.result, TxResult::Success),
                cumulative_gas_used,
                report.logs,
            );
            if tx_type == TxType::Frame {
                receipt.payer = report.payer_address;
                receipt.frame_receipts = frame_receipts_from(report.frame_results);
            }
            receipts.push(receipt);
        }

        Ok((
            receipts,
            block_gas_used,
            unread_storage_reads,
            unaccessed_pure_accounts,
            tx_gas_breakdowns,
        ))
    }

    /// Gets the seeded balance for an account at `seed_idx` from BAL, falling
    /// back to system_seed/store if no BAL entry exists before that index.
    #[cfg(feature = "rayon")]
    fn seeded_balance(
        seed_idx: u32,
        acct: &ethrex_common::types::block_access_list::AccountChanges,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
    ) -> Result<U256, BalValidationError> {
        let pos = acct
            .balance_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        if pos > 0 {
            Ok(acct.balance_changes[pos - 1].post_balance)
        } else if let Some(a) = system_seed.get(&acct.address) {
            Ok(a.info.balance)
        } else {
            store
                .get_account_state(acct.address)
                .map(|a| a.balance)
                .map_err(|e| {
                    BalValidationError::Database(format!(
                        "DB error reading balance for {:?}: {e}",
                        acct.address
                    ))
                })
        }
    }

    /// Gets the seeded code hash for an account at `seed_idx` from BAL, falling
    /// back to system_seed/store if no BAL entry exists before that index.
    #[cfg(feature = "rayon")]
    fn seeded_code_hash(
        seed_idx: u32,
        acct: &ethrex_common::types::block_access_list::AccountChanges,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
    ) -> Result<H256, BalValidationError> {
        let pos = acct
            .code_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        if pos > 0 {
            let seeded_code = &acct.code_changes[pos - 1].new_code;
            Ok(if seeded_code.is_empty() {
                *EMPTY_KECCAK_HASH
            } else {
                ethrex_common::utils::keccak(seeded_code)
            })
        } else if let Some(a) = system_seed.get(&acct.address) {
            Ok(a.info.code_hash)
        } else {
            store
                .get_account_state(acct.address)
                .map(|a| a.code_hash)
                .map_err(|e| {
                    BalValidationError::Database(format!(
                        "DB error reading account state for {:?}: {e}",
                        acct.address
                    ))
                })
        }
    }

    /// Gets the seeded nonce for an account at `seed_idx` from BAL, falling
    /// back to system_seed/store if no BAL entry exists before that index.
    #[cfg(feature = "rayon")]
    fn seeded_nonce(
        seed_idx: u32,
        acct: &ethrex_common::types::block_access_list::AccountChanges,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
    ) -> Result<u64, BalValidationError> {
        let pos = acct
            .nonce_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        if pos > 0 {
            Ok(acct.nonce_changes[pos - 1].post_nonce)
        } else if let Some(a) = system_seed.get(&acct.address) {
            Ok(a.info.nonce)
        } else {
            store
                .get_account_state(acct.address)
                .map(|a| a.nonce)
                .map_err(|e| {
                    BalValidationError::Database(format!(
                        "DB error reading nonce for {:?}: {e}",
                        acct.address
                    ))
                })
        }
    }

    /// Start-of-tx `(balance, nonce, code_hash)` for `acct`: each field resolves
    /// from its own BAL change history at or before `seed_idx`, then the
    /// pre-system-call snapshot, then a single shared store read for whatever
    /// remains. Semantically identical to calling `seeded_balance` +
    /// `seeded_nonce` + `seeded_code_hash`, but reads the store at most once — the
    /// PART A no-op checks need all three for the same account, and an account
    /// with no BAL history before this tx would otherwise read it three times.
    #[cfg(feature = "rayon")]
    fn seeded_account_triple(
        seed_idx: u32,
        acct: &ethrex_common::types::block_access_list::AccountChanges,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
    ) -> Result<(U256, u64, H256), BalValidationError> {
        let bpos = acct
            .balance_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        let npos = acct
            .nonce_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        let cpos = acct
            .code_changes
            .partition_point(|c| c.block_access_index <= seed_idx);

        let mut balance = (bpos > 0).then(|| acct.balance_changes[bpos - 1].post_balance);
        let mut nonce = (npos > 0).then(|| acct.nonce_changes[npos - 1].post_nonce);
        let mut code_hash = (cpos > 0).then(|| {
            let c = &acct.code_changes[cpos - 1].new_code;
            if c.is_empty() {
                *EMPTY_KECCAK_HASH
            } else {
                ethrex_common::utils::keccak(c)
            }
        });

        // Fields with no BAL history fall back to the pre-system-call snapshot.
        if let Some(a) = system_seed.get(&acct.address) {
            balance.get_or_insert(a.info.balance);
            nonce.get_or_insert(a.info.nonce);
            code_hash.get_or_insert(a.info.code_hash);
        }

        // Anything still unresolved comes from the store (read once).
        if balance.is_none() || nonce.is_none() || code_hash.is_none() {
            let s = store.get_account_state(acct.address).map_err(|e| {
                BalValidationError::Database(format!(
                    "DB error reading account state for {:?}: {e}",
                    acct.address
                ))
            })?;
            balance.get_or_insert(s.balance);
            nonce.get_or_insert(s.nonce);
            code_hash.get_or_insert(s.code_hash);
        }

        Ok((
            balance.unwrap_or_default(),
            nonce.unwrap_or_default(),
            code_hash.unwrap_or(*EMPTY_KECCAK_HASH),
        ))
    }

    /// Start-of-tx value for a slot with no BAL `slot_changes` history to consult:
    /// the recorder's `tx_initial` fast-path (a slot the EVM genuinely wrote this
    /// tx), else the pre-tx state (system_seed, then store). Used by the
    /// execution->BAL check for a slot absent from `storage_changes`.
    #[cfg(feature = "rayon")]
    fn seeded_storage_pre_value(
        addr: Address,
        key: H256,
        slot: U256,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
        tx_initial: &FxHashMap<(Address, U256), U256>,
    ) -> Result<U256, BalValidationError> {
        if let Some(v) = tx_initial.get(&(addr, slot)) {
            return Ok(*v);
        }
        Self::storage_from_seed_or_store(addr, key, slot, system_seed, store)
    }

    /// Pre-tx value for a slot from the in-memory snapshot, falling back to the
    /// store. Shared tail of the two `seeded_storage*` helpers.
    #[cfg(feature = "rayon")]
    fn storage_from_seed_or_store(
        addr: Address,
        key: H256,
        slot: U256,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
    ) -> Result<U256, BalValidationError> {
        if let Some(v) = system_seed
            .get(&addr)
            .and_then(|a| a.storage.get(&key))
            .copied()
        {
            Ok(v)
        } else {
            store.get_storage_value(addr, key).map_err(|e| {
                BalValidationError::Database(format!(
                    "DB error reading storage for {addr:?} slot {slot}: {e}"
                ))
            })
        }
    }

    /// Gets the seeded value for a storage slot at `seed_idx` from BAL, falling
    /// back to system_seed/store if no BAL entry exists before that index.
    ///
    /// Fast path: for a slot the EVM genuinely wrote this tx, `tx_initial` already
    /// holds the start-of-tx value it captured during execution — identical to what
    /// this function would otherwise recompute — so return it and skip the lookup.
    #[cfg(feature = "rayon")]
    fn seeded_storage(
        seed_idx: u32,
        sc: &ethrex_common::types::block_access_list::SlotChange,
        addr: Address,
        key: H256,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
        tx_initial: &FxHashMap<(Address, U256), U256>,
    ) -> Result<U256, BalValidationError> {
        if let Some(v) = tx_initial.get(&(addr, sc.slot)) {
            return Ok(*v);
        }
        let pos = sc
            .slot_changes
            .partition_point(|c| c.block_access_index <= seed_idx);
        if pos > 0 {
            Ok(sc.slot_changes[pos - 1].post_value)
        } else {
            Self::storage_from_seed_or_store(addr, key, sc.slot, system_seed, store)
        }
    }

    /// Validates that a tx's post-execution state matches BAL claims.
    ///
    /// Replaces the previous snapshot->diff->validate approach:
    /// - No HashMap clone needed (reconstructs seeded values from BAL)
    /// - Uses pre-built index for O(1) account lookups
    /// - Uses binary search on sorted change lists
    ///
    /// `bal_idx`: block_access_index for this tx (tx_idx + 1)
    /// `seed_idx`: max BAL index used for seeding (= tx_idx = bal_idx - 1)
    /// `current_state`: post-execution account state from per-tx DB
    /// `codes`: code cache from per-tx DB (for code change validation)
    /// `bal`: the block access list
    /// `index`: pre-built validation index
    /// `system_seed`: pre-system-call state snapshot (for extraneous entry detection)
    /// `store`: database (fallback for pre-state lookups)
    /// `pure_reads`: slots the shadow recorder observed as pure reads (never
    ///   written); the execution->BAL check skips the seeded pre-state lookup for these since a
    ///   genuine read cannot diverge from its pre-tx value. Pass an empty set to
    ///   validate every slot unconditionally (e.g. from unit tests).
    /// `tx_initial`: start-of-tx storage values the EVM already captured for
    ///   written slots (`(addr, slot) -> pre_value`); used as the storage seed
    ///   fast-path so `seeded_storage` avoids a store read. Pass an empty map to
    ///   force the store fallback (e.g. from unit tests).
    ///
    /// Exposed as `pub` (rather than crate-private) solely so the direct
    /// `validate_tx_execution` unit tests in the `ethrex-test` crate
    /// (`test/tests/blockchain/bal_validate_tx_execution_tests.rs`) can call it.
    #[cfg(feature = "rayon")]
    #[allow(clippy::too_many_arguments)]
    pub fn validate_tx_execution(
        bal_idx: u32,
        seed_idx: u32,
        current_state: &FxHashMap<Address, LevmAccount>,
        codes: &FxHashMap<H256, Code>,
        bal: &BlockAccessList,
        index: &BalAddressIndex,
        system_seed: &CacheDB,
        store: &Arc<dyn Database>,
        pure_reads: &FxHashSet<(Address, U256)>,
        tx_initial: &FxHashMap<(Address, U256), U256>,
    ) -> Result<(), BalValidationError> {
        // BAL -> execution: for each BAL account with changes at bal_idx,
        // verify execution produced the matching post-state (and that no
        // claimed change is a no-op equal to the start-of-tx value).
        if let Some(active_accounts) = index.tx_to_accounts.get(&bal_idx) {
            for &acct_inner_idx in active_accounts {
                let acct = &bal.accounts()[acct_inner_idx];
                let addr = acct.address;
                let actual = current_state.get(&addr);
                // Start-of-tx (balance, nonce, code_hash), computed once per account
                // and shared by the balance/nonce/code no-op checks below so an
                // account with no BAL history isn't read from the store three times.
                let mut seeded_info: Option<(U256, u64, H256)> = None;

                // Balance
                if let Some(expected) = find_exact_change_balance(&acct.balance_changes, bal_idx) {
                    match actual {
                        Some(a) if a.info.balance == expected => {
                            // No-op check: a BAL balance change is only canonical if
                            // it actually differs from the start-of-tx seeded balance
                            // (the builder MUST NOT record round-tripped balances).
                            if seeded_info.is_none() {
                                seeded_info = Some(Self::seeded_account_triple(
                                    seed_idx,
                                    acct,
                                    system_seed,
                                    store,
                                )?);
                            }
                            let seeded = seeded_info.unwrap_or_default().0;
                            if expected == seeded {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL balance change \
                                     at index {bal_idx}: post==pre=={expected}"
                                )));
                            }
                        }
                        Some(a) => {
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} balance mismatch at index {bal_idx}: BAL={expected}, exec={} (diff={})",
                                a.info.balance,
                                describe_balance_diff(expected, a.info.balance),
                            )));
                        }
                        None => {
                            // Account absent from execution state, yet the BAL declares a
                            // balance change for it at this tx. Never canonical: either it
                            // differs from the start-of-tx seeded balance (a real mismatch)
                            // or equals it (a spurious no-op the builder must not record).
                            // Reject both to match the sequential path; the state root does
                            // NOT catch the no-op case (post==pre leaves the root unchanged).
                            let seeded = Self::seeded_balance(seed_idx, acct, system_seed, store)?;
                            if expected == seeded {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL balance change \
                                     at index {bal_idx} for an account absent from execution \
                                     state: post==pre=={expected}"
                                )));
                            }
                            // Dump full BAL entry for diagnosis
                            let all_bal_indices: Vec<u32> = acct
                                .balance_changes
                                .iter()
                                .map(|c| c.block_access_index)
                                .collect();
                            let all_nonce_indices: Vec<u32> = acct
                                .nonce_changes
                                .iter()
                                .map(|c| c.block_access_index)
                                .collect();
                            let all_storage_indices: Vec<(u32, u64)> = acct
                                .storage_changes
                                .iter()
                                .flat_map(|sc| {
                                    sc.slot_changes
                                        .iter()
                                        .map(|c| (c.block_access_index, sc.slot.low_u64()))
                                })
                                .collect();
                            let code_indices: Vec<u32> = acct
                                .code_changes
                                .iter()
                                .map(|c| c.block_access_index)
                                .collect();
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} has BAL balance change at {bal_idx} \
                                 but not in execution state (expected={expected}, pre={seeded}, \
                                 all_bal_idx={all_bal_indices:?}, nonce_idx={all_nonce_indices:?}, \
                                 storage_idx={all_storage_indices:?}, code_idx={code_indices:?})"
                            )));
                        }
                    }
                }

                // Nonce
                if let Some(expected) = find_exact_change_nonce(&acct.nonce_changes, bal_idx) {
                    match actual {
                        Some(a) if a.info.nonce == expected => {
                            // No-op check: nonces are only ever recorded via a strict
                            // increment (or a one-time fixed-value predeploy install
                            // that also strictly increases), so a canonical BAL nonce
                            // change can never equal the start-of-tx seeded nonce.
                            if seeded_info.is_none() {
                                seeded_info = Some(Self::seeded_account_triple(
                                    seed_idx,
                                    acct,
                                    system_seed,
                                    store,
                                )?);
                            }
                            let seeded = seeded_info.unwrap_or_default().1;
                            if expected == seeded {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL nonce change \
                                     at index {bal_idx}: post==pre=={expected}"
                                )));
                            }
                        }
                        Some(a) => {
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} nonce mismatch at index {bal_idx}: BAL={expected}, exec={}",
                                a.info.nonce
                            )));
                        }
                        None => {
                            // Account absent from execution state, yet the BAL declares a
                            // nonce change: reject the no-op (post==pre) and the mismatch
                            // alike (see the balance arm rationale).
                            let seeded = Self::seeded_nonce(seed_idx, acct, system_seed, store)?;
                            if expected == seeded {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL nonce change \
                                     at index {bal_idx} for an account absent from execution \
                                     state: post==pre=={expected}"
                                )));
                            }
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} has BAL nonce change at {bal_idx} \
                                 but not in execution state (expected={expected}, pre={seeded})"
                            )));
                        }
                    }
                }

                // Code
                if let Some(expected_code) = find_exact_change_code(&acct.code_changes, bal_idx) {
                    match actual {
                        Some(a) => {
                            let actual_code = if let Some(c) = codes.get(&a.info.code_hash) {
                                c.code_bytes()
                            } else {
                                let c = store.get_account_code(a.info.code_hash).map_err(|e| {
                                    BalValidationError::Database(format!(
                                        "DB error reading account code for {addr:?}: {e}"
                                    ))
                                })?;
                                c.code_bytes()
                            };
                            if actual_code != *expected_code {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} code mismatch at index {bal_idx}"
                                )));
                            }
                            // No-op check: a BAL code change is only canonical if
                            // it actually differs from the start-of-tx seeded code.
                            if seeded_info.is_none() {
                                seeded_info = Some(Self::seeded_account_triple(
                                    seed_idx,
                                    acct,
                                    system_seed,
                                    store,
                                )?);
                            }
                            let seeded_hash = seeded_info.unwrap_or_default().2;
                            if a.info.code_hash == seeded_hash {
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL code change \
                                     at index {bal_idx}"
                                )));
                            }
                        }
                        None => {
                            // No-op check: compare against pre-state code.
                            // Try system_seed + codes cache first, then fall
                            // back to store (consistent with balance/nonce).
                            let code_hash = if let Some(a) = system_seed.get(&addr) {
                                a.info.code_hash
                            } else {
                                store
                                    .get_account_state(addr)
                                    .map(|a| a.code_hash)
                                    .map_err(|e| {
                                        BalValidationError::Database(format!(
                                            "DB error reading account state for {addr:?}: {e}"
                                        ))
                                    })?
                            };
                            let pre_code = if let Some(c) = codes.get(&code_hash) {
                                c.code_bytes()
                            } else {
                                let c = store.get_account_code(code_hash).map_err(|e| {
                                    BalValidationError::Database(format!(
                                        "DB error reading account code for hash \
                                             {code_hash:?}: {e}"
                                    ))
                                })?;
                                c.code_bytes()
                            };
                            if *expected_code == pre_code {
                                // Spurious no-op: BAL code change equals pre-state code
                                // for an account absent from execution state. Reject to
                                // match the sequential path (the state root won't catch it).
                                return Err(BalValidationError::Mismatch(format!(
                                    "account {addr:?} has spurious no-op BAL code change \
                                     at index {bal_idx} for an account absent from execution state"
                                )));
                            }
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} has BAL code change at {bal_idx} \
                                 but not in execution state"
                            )));
                        }
                    }
                }

                // Storage
                for sc in &acct.storage_changes {
                    if let Some(expected_value) =
                        find_exact_change_storage(&sc.slot_changes, bal_idx)
                    {
                        let key = ethrex_common::utils::u256_to_h256(sc.slot);
                        let actual_value = actual.and_then(|a| a.storage.get(&key)).copied();
                        if actual_value != Some(expected_value) {
                            // Slot absent from execution state. Compare against the
                            // start-of-tx seeded value: an entry equal to it is a
                            // spurious no-op (post==pre) that the builder must not
                            // record. Reject it to match the sequential path — the
                            // state root won't catch it since the value is unchanged.
                            if actual.is_none() || actual_value.is_none() {
                                let seeded = Self::seeded_storage(
                                    seed_idx,
                                    sc,
                                    addr,
                                    key,
                                    system_seed,
                                    store,
                                    tx_initial,
                                )?;
                                if expected_value == seeded {
                                    return Err(BalValidationError::Mismatch(format!(
                                        "account {addr:?} has spurious no-op BAL storage \
                                         change for slot {} at index {bal_idx} for a slot \
                                         absent from execution state: post==pre=={expected_value}",
                                        sc.slot
                                    )));
                                }
                            }
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} storage slot {} mismatch at index {bal_idx}: \
                                 BAL={expected_value}, exec={actual_value:?}",
                                sc.slot
                            )));
                        }
                        // Matching arm: slot present in execution state and equal
                        // to expected. Reject spurious no-op entries (post == the
                        // start-of-tx seeded value).
                        let seeded = Self::seeded_storage(
                            seed_idx,
                            sc,
                            addr,
                            key,
                            system_seed,
                            store,
                            tx_initial,
                        )?;
                        if expected_value == seeded {
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} has spurious no-op BAL storage change for \
                                 slot {} at index {bal_idx}",
                                sc.slot
                            )));
                        }
                    }
                }
            }
        }

        // execution -> BAL: for each account modified by execution, verify every
        // mutation it produced is claimed by the BAL (no unrecorded change).
        for (addr, account) in current_state {
            if account.is_unmodified() {
                continue;
            }

            let Some(&bal_acct_idx) = index.addr_to_idx.get(addr) else {
                // Account is Modified but absent from BAL. Could be a warm-access
                // artifact (get_account_mut without value changes) or a genuine
                // missing entry. Compare with pre-execution state to distinguish.
                let pre = system_seed
                    .get(addr)
                    .map(|a| (a.info.balance, a.info.nonce, a.info.code_hash))
                    .or_else(|| {
                        store
                            .get_account_state(*addr)
                            .ok()
                            .map(|a| (a.balance, a.nonce, a.code_hash))
                    })
                    .unwrap_or_default();
                let post = (
                    account.info.balance,
                    account.info.nonce,
                    account.info.code_hash,
                );
                if pre != post {
                    return Err(BalValidationError::Mismatch(format!(
                        "account {addr:?} was modified by execution but is absent from BAL"
                    )));
                }
                continue;
            };

            let acct = &bal.accounts()[bal_acct_idx];

            // Balance: if BAL has no change at bal_idx, execution must not have changed it
            if !has_exact_change_balance(&acct.balance_changes, bal_idx) {
                let seeded = Self::seeded_balance(seed_idx, acct, system_seed, store)?;
                if account.info.balance != seeded {
                    return Err(BalValidationError::Mismatch(format!(
                        "account {addr:?} balance changed by execution ({}) but BAL has no \
                         balance change at index {bal_idx} (seeded={seeded})",
                        account.info.balance
                    )));
                }
            }

            // Nonce: same pattern
            if !has_exact_change_nonce(&acct.nonce_changes, bal_idx) {
                let seeded = Self::seeded_nonce(seed_idx, acct, system_seed, store)?;
                if account.info.nonce != seeded {
                    return Err(BalValidationError::Mismatch(format!(
                        "account {addr:?} nonce changed by execution ({}) but BAL has no \
                         nonce change at index {bal_idx} (seeded={seeded})",
                        account.info.nonce
                    )));
                }
            }

            // Code: same pattern — use keccak256 of the raw bytes directly to
            // avoid reconstructing a full Code object (seed_db_from_bal already
            // did that work; here we only need the hash for comparison).
            if !has_exact_change_code(&acct.code_changes, bal_idx) {
                let seeded_hash = Self::seeded_code_hash(seed_idx, acct, system_seed, store)?;
                if account.info.code_hash != seeded_hash {
                    return Err(BalValidationError::Mismatch(format!(
                        "account {addr:?} code changed by execution but BAL has no \
                         code change at index {bal_idx} (seeded_hash={seeded_hash:?})"
                    )));
                }
            }

            // Storage: for each slot in execution state, check it's expected
            for (key_h256, &value) in &account.storage {
                let slot_u256 = u256_from_big_endian_const(key_h256.0);
                // EIP-7928 requires storage_changes sorted by slot, so use binary search.
                let pos = acct
                    .storage_changes
                    .partition_point(|sc| sc.slot < slot_u256);
                if pos < acct.storage_changes.len() && acct.storage_changes[pos].slot == slot_u256 {
                    let sc = &acct.storage_changes[pos];
                    if !has_exact_change_storage(&sc.slot_changes, bal_idx) {
                        // Compare against the start-of-tx value. `seeded_storage` falls
                        // back to system_seed -> store when the slot has no BAL change
                        // at or before this tx (seeded_pos == 0), so a write at this tx
                        // whose BAL entry was omitted is still caught rather than skipped.
                        let seeded = Self::seeded_storage(
                            seed_idx,
                            sc,
                            *addr,
                            *key_h256,
                            system_seed,
                            store,
                            tx_initial,
                        )?;
                        if value != seeded {
                            return Err(BalValidationError::Mismatch(format!(
                                "account {addr:?} storage slot {slot_u256} changed by \
                                 execution ({value}) but BAL has no change at index \
                                 {bal_idx} (seeded={seeded})"
                            )));
                        }
                    }
                }
                // Slot not in BAL storage_changes (a slot is in at most one of
                // storage_changes/storage_reads per EIP-7928 ordering rules). A value
                // that diverges from the start-of-tx baseline means execution WROTE the
                // slot but the write was not recorded in storage_changes — non-canonical.
                // A genuine read leaves value == pre_value and passes. This catches both
                // an entirely-absent slot AND a real write mis-declared under
                // storage_reads: the shadow-reads check in execute_block_parallel only
                // verifies the slot is present in *some* list, never that a changed value
                // was recorded in the correct one.
                //
                // Fast path: the recorder proved this slot a pure read (writes are
                // excluded from `pure_reads`), so its value equals the pre-tx value by
                // construction — skip the DB-backed seeded lookup entirely.
                else if !pure_reads.contains(&(*addr, slot_u256)) {
                    // This slot has no `storage_changes` entry, so there is no BAL
                    // slot history to seed from; take the start-of-tx value straight
                    // from the recorder fast-path / pre-tx state. A written slot hits
                    // `tx_initial` and skips the store read.
                    let pre_value = Self::seeded_storage_pre_value(
                        *addr,
                        *key_h256,
                        slot_u256,
                        system_seed,
                        store,
                        tx_initial,
                    )?;
                    if value != pre_value {
                        let declared_as_read = acct.storage_reads.contains(&slot_u256);
                        return Err(BalValidationError::Mismatch(format!(
                            "account {addr:?} storage slot {slot_u256} was written by \
                             execution ({value}) but has no storage_changes entry at index \
                             {bal_idx} (pre_value={pre_value}, declared_as_read={declared_as_read})"
                        )));
                    }
                }
            }
        }

        Ok(())
    }

    /// Validates BAL entries at the withdrawal index against actual post-withdrawal state.
    ///
    /// After `process_withdrawals` + `extract_all_requests_levm` run on the BAL-seeded
    /// DB, `current_accounts_state` reflects the actual state. Validation is bidirectional:
    ///
    /// BAL -> DB: every BAL claim at the withdrawal index must match the DB.
    /// DB -> BAL: every account modified during the withdrawal/request phase
    ///         must have a corresponding BAL entry. Without this reverse check, a
    ///         malicious builder could omit a withdrawal recipient from the BAL,
    ///         causing the BAL-derived state root to exclude the withdrawal balance
    ///         change.
    #[cfg(feature = "rayon")]
    fn validate_bal_withdrawal_index(
        db: &GeneralizedDatabase,
        bal: &BlockAccessList,
        withdrawal_idx: u32,
        index: &BalAddressIndex,
    ) -> Result<(), EvmError> {
        // BAL -> DB: For each BAL account with changes at the withdrawal index,
        //         verify the DB matches.
        for acct in bal.accounts() {
            let addr = acct.address;
            let actual = db.current_accounts_state.get(&addr);

            // Balance
            if let Some(expected) = find_exact_change_balance(&acct.balance_changes, withdrawal_idx)
            {
                match actual {
                    Some(a) if a.info.balance == expected => {}
                    Some(a) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} balance \
                             mismatch at index {withdrawal_idx}: BAL={expected}, actual={}",
                            a.info.balance
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} has \
                             balance change at index {withdrawal_idx} but was not touched \
                             by withdrawal/request phase"
                        )));
                    }
                }
            }

            // Nonce
            if let Some(expected) = find_exact_change_nonce(&acct.nonce_changes, withdrawal_idx) {
                match actual {
                    Some(a) if a.info.nonce == expected => {}
                    Some(a) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} nonce \
                             mismatch at index {withdrawal_idx}: BAL={expected}, actual={}",
                            a.info.nonce
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} has \
                             nonce change at index {withdrawal_idx} but was not touched \
                             by withdrawal/request phase"
                        )));
                    }
                }
            }

            // Code
            if let Some(expected_code) = find_exact_change_code(&acct.code_changes, withdrawal_idx)
            {
                let code_hash = if expected_code.is_empty() {
                    *EMPTY_KECCAK_HASH
                } else {
                    ethrex_common::utils::keccak(expected_code)
                };
                match actual {
                    Some(a) if a.info.code_hash == code_hash => {}
                    Some(_) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} code \
                             mismatch at index {withdrawal_idx}"
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} has \
                             code change at index {withdrawal_idx} but was not touched \
                             by withdrawal/request phase"
                        )));
                    }
                }
            }

            // Storage writes
            for sc in &acct.storage_changes {
                if let Some(expected_value) =
                    find_exact_change_storage(&sc.slot_changes, withdrawal_idx)
                {
                    let key = ethrex_common::utils::u256_to_h256(sc.slot);
                    let actual_value = actual.and_then(|a| a.storage.get(&key)).copied();
                    if actual_value != Some(expected_value) {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} storage \
                             slot {} mismatch at index {withdrawal_idx}: BAL={expected_value}, \
                             actual={actual_value:?}",
                            sc.slot
                        )));
                    }
                }
            }
        }

        // DB -> BAL: For each account modified during the withdrawal/request phase,
        //         verify it has a corresponding BAL entry claiming the change.
        for (addr, account) in &db.current_accounts_state {
            if account.is_unmodified() {
                continue;
            }

            let Some(&bal_acct_idx) = index.addr_to_idx.get(addr) else {
                // Account modified during withdrawal/request phase but absent
                // from BAL entirely. Compare with pre-state (store) to
                // distinguish genuine mutations from warm-access artifacts.
                let pre_state = db.store.get_account_state(*addr).map_err(|e| {
                    EvmError::Custom(format!(
                        "BAL validation failed for withdrawal: db error reading \
                         account {addr:?}: {e}"
                    ))
                })?;
                let pre = (pre_state.balance, pre_state.nonce, pre_state.code_hash);
                let post = (
                    account.info.balance,
                    account.info.nonce,
                    account.info.code_hash,
                );
                if pre != post {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for withdrawal: account {addr:?} was modified \
                         during withdrawal/request phase but is absent from BAL"
                    )));
                }
                // Also check storage: if any slot differs from pre-state,
                // the account should have been in the BAL.
                for (key_h256, &value) in &account.storage {
                    let pre_value = db.store.get_storage_value(*addr, *key_h256).map_err(|e| {
                        EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: db error reading \
                                 storage {addr:?}[{}]: {e}",
                            u256_from_big_endian_const(key_h256.0)
                        ))
                    })?;
                    if value != pre_value {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} storage \
                             slot {} changed during withdrawal/request phase but is absent \
                             from BAL",
                            u256_from_big_endian_const(key_h256.0)
                        )));
                    }
                }
                continue;
            };

            let acct = &bal.accounts()[bal_acct_idx];

            // Balance: if BAL has no change at withdrawal_idx, the withdrawal
            // phase must not have changed it relative to the last BAL entry.
            if !has_exact_change_balance(&acct.balance_changes, withdrawal_idx) {
                let seeded = match acct.balance_changes.last() {
                    Some(c) => c.post_balance,
                    None => {
                        db.store
                            .get_account_state(*addr)
                            .map_err(|e| {
                                EvmError::Custom(format!(
                                    "BAL validation failed for withdrawal: db error reading \
                                 account {addr:?}: {e}"
                                ))
                            })?
                            .balance
                    }
                };
                if account.info.balance != seeded {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for withdrawal: account {addr:?} balance \
                         changed during withdrawal/request phase ({}) but BAL has no \
                         balance change at index {withdrawal_idx} (last_bal={seeded})",
                        account.info.balance
                    )));
                }
            }

            // Nonce
            if !has_exact_change_nonce(&acct.nonce_changes, withdrawal_idx) {
                let seeded = match acct.nonce_changes.last() {
                    Some(c) => c.post_nonce,
                    None => {
                        db.store
                            .get_account_state(*addr)
                            .map_err(|e| {
                                EvmError::Custom(format!(
                                    "BAL validation failed for withdrawal: db error reading \
                                 account {addr:?}: {e}"
                                ))
                            })?
                            .nonce
                    }
                };
                if account.info.nonce != seeded {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for withdrawal: account {addr:?} nonce \
                         changed during withdrawal/request phase ({}) but BAL has no \
                         nonce change at index {withdrawal_idx} (last_bal={seeded})",
                        account.info.nonce
                    )));
                }
            }

            // Code
            if !has_exact_change_code(&acct.code_changes, withdrawal_idx) {
                let seeded_hash = match acct.code_changes.last() {
                    Some(c) if c.new_code.is_empty() => *EMPTY_KECCAK_HASH,
                    Some(c) => ethrex_common::utils::keccak(&c.new_code),
                    None => {
                        db.store
                            .get_account_state(*addr)
                            .map_err(|e| {
                                EvmError::Custom(format!(
                                    "BAL validation failed for withdrawal: db error reading \
                                 account {addr:?}: {e}"
                                ))
                            })?
                            .code_hash
                    }
                };
                if account.info.code_hash != seeded_hash {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for withdrawal: account {addr:?} code \
                         changed during withdrawal/request phase but BAL has no \
                         code change at index {withdrawal_idx} \
                         (actual={:?}, last_bal={seeded_hash:?})",
                        account.info.code_hash
                    )));
                }
            }

            // Storage: for each slot in the withdrawal/request-phase state,
            // verify the BAL has a corresponding entry or the value is unchanged.
            for (key_h256, &value) in &account.storage {
                let slot_u256 = u256_from_big_endian_const(key_h256.0);
                let pos = acct
                    .storage_changes
                    .partition_point(|sc| sc.slot < slot_u256);
                if pos < acct.storage_changes.len() && acct.storage_changes[pos].slot == slot_u256 {
                    let sc = &acct.storage_changes[pos];
                    if !has_exact_change_storage(&sc.slot_changes, withdrawal_idx) {
                        // No BAL entry at withdrawal_idx; compare against
                        // last BAL entry (the seeded value).
                        let seeded = match sc.slot_changes.last() {
                            Some(c) => c.post_value,
                            None => db.store.get_storage_value(*addr, *key_h256).map_err(|e| {
                                EvmError::Custom(format!(
                                    "BAL validation failed for withdrawal: db error reading \
                                     storage {addr:?}[{slot_u256}]: {e}"
                                ))
                            })?,
                        };
                        if value != seeded {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for withdrawal: account {addr:?} \
                                 storage slot {slot_u256} changed during withdrawal/request \
                                 phase ({value}) but BAL has no change at index \
                                 {withdrawal_idx} (last_bal={seeded})"
                            )));
                        }
                    }
                } else {
                    // Slot not in BAL storage_changes at all: verify it
                    // wasn't actually mutated during the withdrawal/request phase.
                    let pre_value = db.store.get_storage_value(*addr, *key_h256).map_err(|e| {
                        EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: db error reading \
                             storage {addr:?}[{slot_u256}]: {e}"
                        ))
                    })?;
                    if value != pre_value {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for withdrawal: account {addr:?} \
                             storage slot {slot_u256} changed during withdrawal/request \
                             phase ({value}) but slot is absent from BAL storage_changes \
                             (pre={pre_value})"
                        )));
                    }
                }
            }
        }

        Ok(())
    }

    /// Validates supplied-BAL entries at the pre-execution index (0) on the
    /// parallel import path. `validate_tx_execution` covers indices `1..=n` and
    /// `validate_bal_withdrawal_index` covers `n+1`, but index 0 — the pre-block
    /// system calls (EIP-2935 block-hash history, EIP-4788 beacon root) — had no
    /// validator. A spurious no-op entry at index 0 (`post == pre-block value`,
    /// e.g. a `balance: 0` change for an untouched empty account) leaves the
    /// BAL-derived state root unchanged, so it slipped past both the downstream
    /// `validate_state_root` check and content validation: the parallel path
    /// accepted a block the sequential/full-rebuild path rejects.
    ///
    /// Must run right after `prepare_block`, while `current_accounts_state` still
    /// holds the pre-exec changes (`get_state_transitions_tx` drains it). The
    /// check is BAL -> DB only: an index-0 entry for an account the pre-exec
    /// phase never touched, whose value diverges from the post-`prepare_block`
    /// state, or whose value equals the pre-block value (a no-op), is rejected.
    /// Omissions and genuine divergences change the state root and are already
    /// caught by `validate_state_root`; the no-op case is the one this closes.
    #[cfg(feature = "rayon")]
    fn validate_bal_pre_exec_index(
        db: &GeneralizedDatabase,
        bal: &BlockAccessList,
        index: &BalAddressIndex,
    ) -> Result<(), EvmError> {
        let Some(active_accounts) = index.tx_to_accounts.get(&PRE_BLOCK_BAL_INDEX) else {
            return Ok(());
        };

        // Pre-block seed: index-0 changes have no BAL history before them, so the
        // start-of-block value comes straight from the store, which on the import
        // path is still the unmodified parent state. Returns (balance, nonce,
        // code_hash); absent accounts default to (0, 0, EMPTY_KECCAK_HASH).
        let pre_account = |addr: Address| -> Result<(U256, u64, H256), EvmError> {
            let s = db.store.get_account_state(addr).map_err(|e| {
                EvmError::Custom(format!(
                    // "system_tx" names the pre-execution system-call phase, block
                    // access index 0. The label is part of the wire contract, not just
                    // prose: the EEST exception mappers resolve
                    // BlockException.INVALID_BLOCK_ACCESS_LIST by matching
                    // "BAL validation failed for (tx N|system_tx|withdrawal)", so a
                    // phase name outside that set leaves a correct rejection classified
                    // as some other exception and the conformance test fails.
                    "BAL validation failed for system_tx: db error reading account {addr:?}: {e}"
                ))
            })?;
            Ok((s.balance, s.nonce, s.code_hash))
        };

        for &acct_inner_idx in active_accounts {
            let acct = &bal.accounts()[acct_inner_idx];
            let addr = acct.address;
            let actual = db.current_accounts_state.get(&addr);

            // Balance
            if let Some(expected) =
                find_exact_change_balance(&acct.balance_changes, PRE_BLOCK_BAL_INDEX)
            {
                match actual {
                    Some(a) if a.info.balance == expected => {
                        if expected == pre_account(addr)?.0 {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for system_tx: account {addr:?} has spurious \
                                 no-op balance change at index 0: post==pre=={expected}"
                            )));
                        }
                    }
                    Some(a) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} balance mismatch \
                             at index 0: BAL={expected}, actual={}",
                            a.info.balance
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} has balance \
                             change at index 0 but was not touched by the pre-exec phase"
                        )));
                    }
                }
            }

            // Nonce
            if let Some(expected) =
                find_exact_change_nonce(&acct.nonce_changes, PRE_BLOCK_BAL_INDEX)
            {
                match actual {
                    Some(a) if a.info.nonce == expected => {
                        if expected == pre_account(addr)?.1 {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for system_tx: account {addr:?} has spurious \
                                 no-op nonce change at index 0: post==pre=={expected}"
                            )));
                        }
                    }
                    Some(a) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} nonce mismatch \
                             at index 0: BAL={expected}, actual={}",
                            a.info.nonce
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} has nonce \
                             change at index 0 but was not touched by the pre-exec phase"
                        )));
                    }
                }
            }

            // Code
            if let Some(expected_code) =
                find_exact_change_code(&acct.code_changes, PRE_BLOCK_BAL_INDEX)
            {
                let expected_hash = if expected_code.is_empty() {
                    *EMPTY_KECCAK_HASH
                } else {
                    ethrex_common::utils::keccak(expected_code)
                };
                match actual {
                    Some(a) if a.info.code_hash == expected_hash => {
                        if expected_hash == pre_account(addr)?.2 {
                            return Err(EvmError::Custom(format!(
                                "BAL validation failed for system_tx: account {addr:?} has spurious \
                                 no-op code change at index 0"
                            )));
                        }
                    }
                    Some(_) => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} code mismatch \
                             at index 0"
                        )));
                    }
                    None => {
                        return Err(EvmError::Custom(format!(
                            "BAL validation failed for system_tx: account {addr:?} has code \
                             change at index 0 but was not touched by the pre-exec phase"
                        )));
                    }
                }
            }

            // Storage
            for sc in &acct.storage_changes {
                let Some(expected_value) =
                    find_exact_change_storage(&sc.slot_changes, PRE_BLOCK_BAL_INDEX)
                else {
                    continue;
                };
                let key = ethrex_common::utils::u256_to_h256(sc.slot);
                let actual_value = actual.and_then(|a| a.storage.get(&key)).copied();
                if actual_value != Some(expected_value) {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for system_tx: account {addr:?} storage slot {} \
                         mismatch at index 0: BAL={expected_value}, actual={actual_value:?}",
                        sc.slot
                    )));
                }
                let pre_value = db.store.get_storage_value(addr, key).map_err(|e| {
                    EvmError::Custom(format!(
                        "BAL validation failed for system_tx: db error reading storage {addr:?} \
                         slot {}: {e}",
                        sc.slot
                    ))
                })?;
                if expected_value == pre_value {
                    return Err(EvmError::Custom(format!(
                        "BAL validation failed for system_tx: account {addr:?} has spurious no-op \
                         storage change for slot {} at index 0: post==pre=={expected_value}",
                        sc.slot
                    )));
                }
            }
        }

        Ok(())
    }

    /// Pre-warms state by executing all transactions in parallel, grouped by sender.
    ///
    /// The `store` parameter should be a `CachingDatabase`-wrapped store so that
    /// parallel workers can benefit from shared caching. The same cache should
    /// be used by the sequential execution phase.
    #[cfg(feature = "rayon")]
    pub fn warm_block(
        block: &Block,
        store: Arc<dyn Database>,
        vm_type: VMType,
        crypto: &dyn Crypto,
        cancelled: &AtomicBool,
    ) -> Result<(), EvmError> {
        let txs_with_sender = block
            .body
            .get_transactions_with_sender(crypto)
            .map_err(|error| {
                EvmError::Transaction(format!("Couldn't recover addresses with error: {error}"))
            })?;

        Self::warm_txs(
            &txs_with_sender,
            &block.header,
            store.clone(),
            vm_type,
            crypto,
            &|| cancelled.load(Ordering::Relaxed),
        )?;

        let mut db = GeneralizedDatabase::new(store.clone());

        if cancelled.load(Ordering::Relaxed) {
            return Ok(());
        }

        for withdrawal in block
            .body
            .withdrawals
            .iter()
            .flatten()
            .filter(|withdrawal| withdrawal.amount > 0)
        {
            db.get_account_mut(withdrawal.address).map_err(|_| {
                EvmError::DB(format!(
                    "Withdrawal account {} not found",
                    withdrawal.address
                ))
            })?;
        }
        Ok(())
    }

    /// Speculatively executes an arbitrary transaction set against `header`'s
    /// parent state to warm shared caches (used by the mempool prewarmer and
    /// `warm_block`). Sequential within a sender group, parallel across groups.
    /// `should_stop` is polled before each sender group and again before each
    /// transaction, so cancellation latency is bounded by one transaction's
    /// execution. Execution results are discarded — only cache population
    /// matters.
    #[cfg(feature = "rayon")]
    pub fn warm_txs(
        txs_with_sender: &[(&Transaction, Address)],
        header: &BlockHeader,
        store: Arc<dyn Database>,
        vm_type: VMType,
        crypto: &dyn Crypto,
        should_stop: &(dyn Fn() -> bool + Sync),
    ) -> Result<(), EvmError> {
        // Group by sender so nonce/balance changes propagate within a group
        // (inspired by Nethermind's per-sender prewarmer).
        let mut sender_groups: FxHashMap<Address, Vec<&Transaction>> = FxHashMap::default();
        for (tx, sender) in txs_with_sender {
            sender_groups.entry(*sender).or_default().push(tx);
        }

        // Block-invariant EVM config + chain id, computed once and shared (by copy)
        // across the parallel warming workers.
        let chain_config = store.get_chain_config()?;
        let evm_config = EVMConfig::new_from_chain_config(&chain_config, header);
        let chain_id = chain_config.chain_id;
        // Block-invariant base blob fee, computed once and shared across workers.
        let base_blob_fee_per_gas = get_base_fee_per_blob_gas(header.excess_blob_gas, &evm_config)?;

        // Parallel across sender groups, sequential within each group. The stack pool is reused
        // across all groups a worker handles (it is `Send`).
        sender_groups.into_par_iter().for_each_with(
            Vec::with_capacity(STACK_LIMIT),
            |stack_pool, (sender, txs)| {
                if should_stop() {
                    return;
                }
                // Memory holds an `Rc` (not `Send`), so its pool can't ride the `for_each_with`
                // init; keep it local to this group's run, where it still amortizes the buffer
                // alloc across the group's txs.
                let mut memory_pool = Vec::with_capacity(1);
                // Each sender group gets its own db instance for state propagation
                let mut group_db = GeneralizedDatabase::new(store.clone());
                for tx in txs {
                    if should_stop() {
                        return;
                    }
                    let _ = Self::execute_tx_in_block(
                        tx,
                        sender,
                        header,
                        &mut group_db,
                        vm_type,
                        base_blob_fee_per_gas,
                        stack_pool,
                        &mut memory_pool,
                        true,
                        crypto,
                        evm_config,
                        chain_id,
                        // Warming discards results; the EXECUTE precompile is not run
                        // here (the real execution path carries the validator).
                        None,
                    );
                }
            },
        );
        Ok(())
    }

    /// Flattened (address, slot) storage worklist for a BAL, in natural account
    /// order (slots grouped per account for storage-trie locality).
    #[cfg(feature = "rayon")]
    pub fn bal_storage_slots(bal: &BlockAccessList) -> Vec<(Address, H256)> {
        bal.accounts()
            .iter()
            .flat_map(|ac| {
                ac.all_storage_slots()
                    .map(move |slot| (ac.address, H256::from_uint(&slot)))
            })
            .collect()
    }

    /// Concurrent block warmer for the BAL path: prefetches account states and
    /// contract code while execution runs.
    ///
    /// Storage slots are deliberately NOT warmed here. They are prefetched
    /// synchronously before the executor starts (see `bal_storage_slots` and the
    /// call site in `blockchain.rs`); warming them concurrently here let the
    /// executor race the warmer to the trie for SSTORE original values and cost
    /// ~22% of CPU. Keep storage warming synchronous and up front.
    #[cfg(feature = "rayon")]
    pub fn warm_block_from_bal(
        bal: &BlockAccessList,
        store: Arc<dyn Database>,
        cancelled: &AtomicBool,
    ) -> Result<(), EvmError> {
        let accounts = bal.accounts();
        if accounts.is_empty() {
            return Ok(());
        }

        // Phase 1: Prefetch all account states — parallel inner fetch + single write-lock.
        // This warms the CachingDatabase account cache and the TrieLayerCache with
        // state trie nodes. Storage slots are prefetched synchronously before the
        // executor starts (see `bal_storage_slots` at the call site), so this warmer
        // only needs to cover account states and contract code, which overlap exec.
        let account_addresses: Vec<Address> = accounts.iter().map(|ac| ac.address).collect();
        store
            .prefetch_accounts(&account_addresses)
            .map_err(|e| EvmError::Custom(format!("prefetch_accounts: {e}")))?;

        if cancelled.load(Ordering::Relaxed) {
            return Ok(());
        }

        // Phase 2: Code prefetch — collect code hashes from Phase 1 account states
        // (already cached after Phase 1 prefetch), then batch-fetch codes in parallel.
        // Uses par_iter for collection since blocks can have thousands of accounts.
        let code_hashes: Vec<ethrex_common::H256> = accounts
            .par_iter()
            .filter_map(|ac| {
                store
                    .get_account_state(ac.address)
                    .ok()
                    .filter(|s| s.code_hash != *EMPTY_KECCAK_HASH)
                    .map(|s| s.code_hash)
            })
            .collect();
        code_hashes.par_iter().for_each(|&h| {
            let _ = store.get_account_code(h);
        });

        Ok(())
    }

    fn send_state_transitions_tx(
        merkleizer: &Sender<Vec<AccountUpdate>>,
        db: &mut GeneralizedDatabase,
        queue_length: &AtomicUsize,
    ) -> Result<(), EvmError> {
        let transitions = LEVM::get_state_transitions_tx(db)?;
        merkleizer
            .send(transitions)
            .map_err(|e| EvmError::Custom(format!("send failed: {e}")))?;
        queue_length.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }

    fn setup_env(
        tx: &Transaction,
        tx_sender: Address,
        block_header: &BlockHeader,
        db: &GeneralizedDatabase,
        vm_type: VMType,
    ) -> Result<Environment, EvmError> {
        // `chain_config` (a dyn-dispatch copy) and `EVMConfig`/fork/blob-schedule are
        // block-invariant; in a block loop, compute them once and use
        // `setup_env_with_config` instead. This single-tx entry point computes them here.
        let chain_config = db.store.get_chain_config()?;
        let config = EVMConfig::new_from_chain_config(&chain_config, block_header);
        let base_blob_fee_per_gas =
            get_base_fee_per_blob_gas(block_header.excess_blob_gas, &config)?;
        Self::setup_env_with_config(
            tx,
            tx_sender,
            block_header,
            config,
            chain_config.chain_id,
            vm_type,
            base_blob_fee_per_gas,
        )
    }

    /// Per-tx `Environment` builder that takes the block-invariant `EVMConfig` and
    /// `chain_id` precomputed once per block, avoiding a per-tx `get_chain_config()`
    /// dyn-dispatch `ChainConfig` copy + `fork`/blob-schedule recompute.
    fn setup_env_with_config(
        tx: &Transaction,
        tx_sender: Address,
        block_header: &BlockHeader,
        config: EVMConfig,
        chain_id: u64,
        vm_type: VMType,
        base_blob_fee_per_gas: U256,
    ) -> Result<Environment, EvmError> {
        let gas_price: U256 = calculate_gas_price_for_tx(
            tx,
            block_header.base_fee_per_gas.unwrap_or_default(),
            &vm_type,
        )?;

        let block_excess_blob_gas = block_header.excess_blob_gas;
        let env = Environment {
            origin: tx_sender,
            gas_limit: tx.gas_limit(),
            config,
            block_number: block_header.number,
            coinbase: block_header.coinbase,
            timestamp: block_header.timestamp,
            prev_randao: Some(block_header.prev_randao),
            slot_number: block_header
                .slot_number
                .map(U256::from)
                .unwrap_or(U256::zero()),
            chain_id: chain_id.into(),
            base_fee_per_gas: block_header.base_fee_per_gas.unwrap_or_default().into(),
            base_blob_fee_per_gas,
            gas_price,
            block_excess_blob_gas,
            block_blob_gas_used: block_header.blob_gas_used,
            tx_blob_hashes: tx.blob_versioned_hashes(),
            tx_max_priority_fee_per_gas: tx.max_priority_fee().map(U256::from),
            tx_max_fee_per_gas: tx.max_fee_per_gas().map(U256::from),
            tx_max_fee_per_blob_gas: tx.max_fee_per_blob_gas(),
            tx_nonce: tx.nonce(),
            block_gas_limit: block_header.gas_limit,
            difficulty: block_header.difficulty,
            is_privileged: matches!(tx, Transaction::PrivilegedL2Transaction(_)),
            fee_token: tx.fee_token(),
            disable_balance_check: false,
            disable_nonce_check: false,
            disable_gas_allowance_check: false,
            disable_sender_eoa_check: false,
            is_system_call: false,
        };

        Ok(env)
    }

    pub fn execute_tx(
        // The transaction to execute.
        tx: &Transaction,
        // The transaction's recovered address
        tx_sender: Address,
        // The block header for the current block.
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<ExecutionReport, EvmError> {
        let env = Self::setup_env(tx, tx_sender, block_header, db, vm_type)?;
        let mut vm = VM::new(
            env,
            db,
            tx,
            LevmCallTracer::disabled(),
            vm_type,
            crypto,
            stateless_validator,
        )?;

        vm.execute().map_err(VMError::into)
    }

    // Like execute_tx but allows reusing the stack pool. Takes the block-invariant
    // `config`/`chain_id` precomputed once per block (see `setup_env_with_config`).
    #[allow(clippy::too_many_arguments)]
    fn execute_tx_in_block(
        // The transaction to execute.
        tx: &Transaction,
        // The transaction's recovered address
        tx_sender: Address,
        // The block header for the current block.
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        base_blob_fee_per_gas: U256,
        stack_pool: &mut Vec<Stack>,
        memory_pool: &mut Vec<Memory>,
        disable_balance_check: bool,
        crypto: &dyn Crypto,
        config: EVMConfig,
        chain_id: u64,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<ExecutionReport, EvmError> {
        let mut env = Self::setup_env_with_config(
            tx,
            tx_sender,
            block_header,
            config,
            chain_id,
            vm_type,
            base_blob_fee_per_gas,
        )?;
        env.disable_balance_check = disable_balance_check;
        // Draw the root frame's stack and memory buffer from the shared pools (and adopt the
        // stacks for sub-frames), then return them afterwards so the next tx reuses them instead
        // of allocating + zeroing a fresh 32 KB stack and a fresh memory buffer per transaction.
        let mut vm = VM::new_pooled(
            env,
            db,
            tx,
            LevmCallTracer::disabled(),
            vm_type,
            crypto,
            stateless_validator,
            stack_pool,
            memory_pool,
        )?;
        let result = vm.execute().map_err(VMError::into);
        // Runs on both success and error paths (execute borrowed `vm` mutably but left it intact).
        vm.reclaim_into(stack_pool, memory_pool);
        result
    }

    pub fn undo_last_tx(db: &mut GeneralizedDatabase) -> Result<(), EvmError> {
        db.undo_last_transaction()?;
        Ok(())
    }

    pub fn simulate_tx_from_generic(
        // The transaction to execute.
        tx: &GenericTransaction,
        // The block header for the current block.
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
        stateless_validator: Option<&dyn StatelessValidator>,
    ) -> Result<ExecutionResult, EvmError> {
        // `env_from_generic` already relaxes the admission-only validations for every
        // simulation RPC, the gas allowance included. `block_gas_limit` keeps the block's
        // real value there: the GASLIMIT opcode reads it, so overwriting it would make
        // 0x45 return a number that appears nowhere on chain.
        let mut env = env_from_generic(tx, block_header, db, vm_type)?;

        adjust_disabled_base_fee(&mut env);

        let converted_tx = generic_tx_to_transaction(tx)?;
        let mut vm = vm_from_generic(&converted_tx, env, db, vm_type, crypto, stateless_validator)?;

        vm.execute()
            .map(|value| value.into())
            .map_err(VMError::into)
    }

    /// EIP-8141 mempool validation-prefix simulation (local peer policy, never
    /// consensus). Builds a VM like [`LEVM::execute_tx`] over a fresh
    /// simulation database, activates the [`ValidationObserver`] for the
    /// transaction's sender, runs ONLY the validation-prefix frames, and applies
    /// the admission assertions:
    ///   - no prefix frame reverted;
    ///   - each verify/pay frame that must APPROVE did (the prefix established a
    ///     payer);
    ///   - the deploy frame (if any) left non-empty code at the sender;
    ///   - total simulated prefix gas <= MAX_VERIFY_GAS;
    ///   - no validation-trace rule was violated.
    ///
    /// `canonical_paymaster_code_hash` is the pinned canonical paymaster code
    /// hash, when known (always `None` today, OQ1).
    #[allow(clippy::too_many_arguments)]
    pub fn simulate_frame_validation_prefix(
        tx: &Transaction,
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
        prefix: &ethrex_common::types::ValidationPrefix,
        _canonical_paymaster_code_hash: Option<H256>,
    ) -> Result<FrameValidationOutcome, EvmError> {
        use ethrex_common::types::FRAME_TX_MAX_VERIFY_GAS;

        let frame_tx = match tx {
            Transaction::FrameTransaction(ft) => ft,
            _ => {
                return Err(EvmError::Custom(
                    "simulate_frame_validation_prefix requires a frame transaction".to_string(),
                ));
            }
        };
        let sender = frame_tx.sender;

        let env = Self::setup_env(tx, sender, block_header, db, vm_type)?;
        let mut vm = VM::new(
            env,
            db,
            tx,
            LevmCallTracer::disabled(),
            vm_type,
            crypto,
            None,
        )?;

        // OQ1: no canonical paymaster is resolvable, so the canonical pay-frame
        // exemption never fires (always `None`).
        let canonical_pay_frame: Option<usize> = None;

        let sim = match vm.run_frame_validation_prefix(
            &prefix.frame_indices,
            prefix.deploy_index,
            canonical_pay_frame,
        ) {
            Ok(sim) => sim,
            Err(err) => {
                // A preamble / VM error means the prefix cannot be validated;
                // treat it as a (conservative) rejection rather than failing the
                // whole admission pipeline.
                return Ok(FrameValidationOutcome {
                    passed: false,
                    violation: Some(EvmError::from(err).to_string()),
                    reservation_ceiling: Self::frame_tx_reservation_ceiling(frame_tx),
                    accessed_paymaster: None,
                    touched_sender_slots: Vec::new(),
                });
            }
        };

        let reservation_ceiling = Self::frame_tx_reservation_ceiling(frame_tx);
        let touched_sender_slots = vm.validation_observer.touched_sender_slots.clone();
        // The payer established by the prefix is the paymaster (OQ2: the
        // APPROVE-payment address is treated uniformly as "paymaster", including
        // the self-funded sender). Its canonical flag is always false (OQ1: no
        // canonical paymaster bytecode is resolvable), which is also why
        // `_canonical_paymaster_code_hash` is unused. The observer carries no
        // distinct paymaster, so derive it from the established payer.
        let accessed_paymaster = sim.payer_address.map(|payer| (payer, false));

        // Assertion: a recorded trace violation fails validation.
        if let Some(violation) = &vm.validation_observer.violation {
            return Ok(FrameValidationOutcome {
                passed: false,
                violation: Some(format!("{violation:?}")),
                reservation_ceiling,
                accessed_paymaster,
                touched_sender_slots,
            });
        }

        // Assertion: no prefix frame reverted.
        if sim.any_revert {
            return Ok(FrameValidationOutcome {
                passed: false,
                violation: Some("validation prefix frame reverted".to_string()),
                reservation_ceiling,
                accessed_paymaster,
                touched_sender_slots,
            });
        }

        // Assertion: the prefix established a payer (verify/pay frames must
        // APPROVE-payment; otherwise the transaction has no payer).
        if sim.payer_address.is_none() {
            return Ok(FrameValidationOutcome {
                passed: false,
                violation: Some("validation prefix did not establish a payer".to_string()),
                reservation_ceiling,
                accessed_paymaster,
                touched_sender_slots,
            });
        }

        // Assertion: a deploy frame must leave non-empty code at the sender.
        if prefix.deploy_index.is_some() {
            let code = vm.db.get_account_code(sender).map_err(VMError::from)?;
            if code.is_empty() {
                return Ok(FrameValidationOutcome {
                    passed: false,
                    violation: Some(format!("{:?}", FrameSimViolation::DeployInstalledNoCode)),
                    reservation_ceiling,
                    accessed_paymaster,
                    touched_sender_slots,
                });
            }
        }

        // Assertion: total simulated prefix gas within the verify-gas budget.
        if sim.total_gas_used > FRAME_TX_MAX_VERIFY_GAS {
            return Ok(FrameValidationOutcome {
                passed: false,
                violation: Some(format!(
                    "validation prefix gas {} exceeds MAX_VERIFY_GAS {}",
                    sim.total_gas_used, FRAME_TX_MAX_VERIFY_GAS
                )),
                reservation_ceiling,
                accessed_paymaster,
                touched_sender_slots,
            });
        }

        Ok(FrameValidationOutcome {
            passed: true,
            violation: None,
            reservation_ceiling,
            accessed_paymaster,
            touched_sender_slots,
        })
    }

    /// Mempool reservation ceiling for a frame transaction:
    /// `max_gas * max_fee_per_gas + len(blob_hashes) * 131072 * max_fee_per_blob_gas`,
    /// saturating.
    ///
    /// The consensus `max_cost` that APPROVE collects prices blobs at the
    /// including block's `blob_base_fee` (EIP-8141 §Gas Accounting, TXPARAM 0x06;
    /// `load_tx_param` 0x06 in `opcode_handlers/frame_tx.rs`). That rate is not
    /// known at admission — the simulation runs against the current head, while
    /// execution charges the base fee of whichever later block includes the
    /// transaction — and the blob base fee moves per block, so pricing the
    /// reservation at the head's rate could reserve less than the eventual charge.
    /// `max_fee_per_blob_gas >= blob_base_fee` is an inclusion condition
    /// (EIP-8141 §Blob handling), so the declared max rate bounds every block that
    /// can include the transaction and keeps this a true ceiling.
    ///
    /// Intentionally saturating (not checked): the TXPARAM 0x06 consensus handler
    /// uses checked_mul/checked_add and halts on overflow. Saturating to
    /// `U256::MAX` here only makes the reservation larger, never smaller.
    fn frame_tx_reservation_ceiling(frame_tx: &ethrex_common::types::FrameTransaction) -> U256 {
        let gas_cost =
            U256::from(frame_tx.max_fee_per_gas).saturating_mul(U256::from(frame_tx.max_gas()));
        let blob_cost = U256::from(frame_tx.blob_versioned_hashes.len())
            .saturating_mul(U256::from(131072u64))
            .saturating_mul(frame_tx.max_fee_per_blob_gas);
        gas_cost.saturating_add(blob_cost)
    }

    pub fn get_state_transitions(
        db: &mut GeneralizedDatabase,
    ) -> Result<Vec<AccountUpdate>, EvmError> {
        Ok(db.get_state_transitions()?)
    }

    pub fn get_state_transitions_tx(
        db: &mut GeneralizedDatabase,
    ) -> Result<Vec<AccountUpdate>, EvmError> {
        Ok(db.get_state_transitions_tx()?)
    }

    pub fn process_withdrawals(
        db: &mut GeneralizedDatabase,
        withdrawals: &[Withdrawal],
    ) -> Result<(), EvmError> {
        // For every withdrawal we increment the target account's balance
        for (address, increment) in withdrawals
            .iter()
            .filter(|withdrawal| withdrawal.amount > 0)
            .map(|w| (w.address, u128::from(w.amount) * u128::from(GWEI_TO_WEI)))
        {
            let account = db
                .get_account_mut(address)
                .map_err(|_| EvmError::DB(format!("Withdrawal account {address} not found")))?;

            let initial_balance = account.info.balance;
            account.info.balance += increment.into();
            let new_balance = account.info.balance;

            // Record balance change for BAL (EIP-7928)
            if let Some(recorder) = db.bal_recorder_mut() {
                recorder.set_initial_balance(address, initial_balance);
                recorder.record_balance_change(address, new_balance);
            }
        }
        Ok(())
    }

    // SYSTEM CONTRACTS
    pub fn beacon_root_contract_call(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<(), EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "beacon_root_contract_call should not be called for L2 VM".to_string(),
            ));
        }

        let beacon_root = block_header.parent_beacon_block_root.ok_or_else(|| {
            EvmError::Header("parent_beacon_block_root field is missing".to_string())
        })?;

        generic_system_contract_levm(
            block_header,
            Bytes::copy_from_slice(beacon_root.as_bytes()),
            db,
            BEACON_ROOTS_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;
        Ok(())
    }

    pub fn process_block_hash_history(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<(), EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "process_block_hash_history should not be called for L2 VM".to_string(),
            ));
        }

        generic_system_contract_levm(
            block_header,
            Bytes::copy_from_slice(block_header.parent_hash.as_bytes()),
            db,
            HISTORY_STORAGE_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;
        Ok(())
    }

    /// Install the canonical EIP-8141 expiry verifier runtime code at
    /// EXPIRY_VERIFIER on Hegota activation (EIP-8141: "At
    /// activation, clients must install..."). Idempotent: writes only when
    /// the existing code differs, so exactly one account update is produced
    /// (at the first Hegota block) and none afterwards.
    pub fn install_expiry_verifier_code(
        db: &mut GeneralizedDatabase,
        crypto: &dyn Crypto,
    ) -> Result<(), EvmError> {
        // Predeploy convention (matches the genesis predeploys 4788/2935/7002/7251).
        const PREDEPLOY_NONCE: u64 = 1;

        let current = db.get_account_code(EXPIRY_VERIFIER_PREDEPLOY.address)?;
        if current.code() == EXPIRY_VERIFIER_RUNTIME_BYTECODE.as_slice() {
            return Ok(());
        }
        let code = Code::from_bytecode(
            Bytes::from_static(&EXPIRY_VERIFIER_RUNTIME_BYTECODE),
            crypto,
        );
        let code_hash = code.hash;
        // Record BAL code/nonce changes if recording is active, so a BAL
        // reconstructor reproduces the same post-state (it takes nonce from
        // prestate otherwise).
        if let Some(recorder) = db.bal_recorder_mut() {
            recorder.record_code_change(EXPIRY_VERIFIER_PREDEPLOY.address, code.code_bytes());
            recorder.record_nonce_change(EXPIRY_VERIFIER_PREDEPLOY.address, PREDEPLOY_NONCE);
        }
        let acc = db
            .get_account_mut(EXPIRY_VERIFIER_PREDEPLOY.address)
            .map_err(EvmError::from)?;
        acc.info.code_hash = code_hash;
        acc.info.nonce = PREDEPLOY_NONCE;
        db.codes.entry(code_hash).or_insert(code);
        Ok(())
    }

    pub(crate) fn read_withdrawal_requests(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<ExecutionReport, EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "read_withdrawal_requests should not be called for L2 VM".to_string(),
            ));
        }

        let report = generic_system_contract_levm(
            block_header,
            Bytes::new(),
            db,
            WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;

        match report.result {
            TxResult::Success => Ok(report),
            // EIP-7002 specifies that a failed system call invalidates the entire block.
            TxResult::Revert(vm_error) => Err(EvmError::SystemContractCallFailed(format!(
                "REVERT when reading withdrawal requests with error: {vm_error:?}. According to EIP-7002, the revert of this system call invalidates the block.",
            ))),
        }
    }

    pub(crate) fn dequeue_consolidation_requests(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<ExecutionReport, EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "dequeue_consolidation_requests should not be called for L2 VM".to_string(),
            ));
        }

        let report = generic_system_contract_levm(
            block_header,
            Bytes::new(),
            db,
            CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;

        match report.result {
            TxResult::Success => Ok(report),
            // EIP-7251 specifies that a failed system call invalidates the entire block.
            TxResult::Revert(vm_error) => Err(EvmError::SystemContractCallFailed(format!(
                "REVERT when dequeuing consolidation requests with error: {vm_error:?}. According to EIP-7251, the revert of this system call invalidates the block.",
            ))),
        }
    }

    pub(crate) fn read_builder_deposit_requests(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<ExecutionReport, EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "read_builder_deposit_requests should not be called for L2 VM".to_string(),
            ));
        }

        let report = generic_system_contract_levm(
            block_header,
            Bytes::new(),
            db,
            BUILDER_DEPOSIT_CONTRACT_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;

        match report.result {
            TxResult::Success => Ok(report),
            // EIP-8282 specifies that a failed system call invalidates the entire block.
            TxResult::Revert(vm_error) => Err(EvmError::SystemContractCallFailed(format!(
                "REVERT when reading builder deposit requests with error: {vm_error:?}. According to EIP-8282, the revert of this system call invalidates the block.",
            ))),
        }
    }

    pub(crate) fn dequeue_builder_exit_requests(
        block_header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<ExecutionReport, EvmError> {
        if let VMType::L2(_) = vm_type {
            return Err(EvmError::InvalidEVM(
                "dequeue_builder_exit_requests should not be called for L2 VM".to_string(),
            ));
        }

        let report = generic_system_contract_levm(
            block_header,
            Bytes::new(),
            db,
            BUILDER_EXIT_CONTRACT_ADDRESS.address,
            SYSTEM_ADDRESS,
            vm_type,
            crypto,
        )?;

        match report.result {
            TxResult::Success => Ok(report),
            // EIP-8282 specifies that a failed system call invalidates the entire block.
            TxResult::Revert(vm_error) => Err(EvmError::SystemContractCallFailed(format!(
                "REVERT when dequeuing builder exit requests with error: {vm_error:?}. According to EIP-8282, the revert of this system call invalidates the block.",
            ))),
        }
    }

    pub fn create_access_list(
        mut tx: GenericTransaction,
        header: &BlockHeader,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<(ExecutionResult, AccessList), VMError> {
        let mut env = env_from_generic(&tx, header, db, vm_type)?;

        adjust_disabled_base_fee(&mut env);

        let converted_tx = generic_tx_to_transaction(&tx)?;
        let mut vm = vm_from_generic(&converted_tx, env.clone(), db, vm_type, crypto, None)?;

        vm.stateless_execute()?;

        // Execute the tx again, now with the created access list.
        tx.access_list = vm.substate.make_access_list();
        let converted_tx = generic_tx_to_transaction(&tx)?;
        let mut vm = vm_from_generic(&converted_tx, env, db, vm_type, crypto, None)?;

        let report = vm.stateless_execute()?;

        Ok((
            report.into(),
            tx.access_list
                .into_iter()
                .map(|x| (x.address, x.storage_keys))
                .collect(),
        ))
    }

    pub fn prepare_block(
        block: &Block,
        db: &mut GeneralizedDatabase,
        vm_type: VMType,
        crypto: &dyn Crypto,
    ) -> Result<(), EvmError> {
        let chain_config = db.store.get_chain_config()?;
        let block_header = &block.header;
        let fork = chain_config.fork(block_header.timestamp);

        // TODO: I don't like deciding the behavior based on the VMType here.
        if let VMType::L2(_) = vm_type {
            return Ok(());
        }

        // EIP-8141: the expiry verifier predeploy must exist from Hegota
        // activation onward. Idempotent install; also
        // hooked in apply_system_calls for the payload-build path.
        if fork >= Fork::Hegota {
            Self::install_expiry_verifier_code(db, crypto)?;
        }

        if block_header.parent_beacon_block_root.is_some() && fork >= Fork::Cancun {
            Self::beacon_root_contract_call(block_header, db, vm_type, crypto)?;
        }

        if fork >= Fork::Prague {
            //eip 2935: stores parent block hash in system contract
            Self::process_block_hash_history(block_header, db, vm_type, crypto)?;
        }
        Ok(())
    }
}

pub fn generic_system_contract_levm(
    block_header: &BlockHeader,
    calldata: Bytes,
    db: &mut GeneralizedDatabase,
    contract_address: Address,
    system_address: Address,
    vm_type: VMType,
    crypto: &dyn Crypto,
) -> Result<ExecutionReport, EvmError> {
    let chain_config = db.store.get_chain_config()?;
    let config = EVMConfig::new_from_chain_config(&chain_config, block_header);
    let env = Environment {
        origin: system_address,
        // EIPs 2935, 4788, 7002 and 7251 dictate that the system calls have a gas
        // limit of 30 million and they do not use intrinsic gas. EELS
        // (process_unchecked_system_transaction) builds the message with
        // intrinsic_regular_gas=0 and gas=SYSTEM_TRANSACTION_GAS, so the contract
        // gets the full SYS_CALL_GAS_LIMIT. We match that by NOT padding the limit
        // here and by zeroing the intrinsic for system calls in the default hook.
        // (A previous `+ TX_BASE_COST` buffer assumed the flat 21000 Prague
        // intrinsic; EIP-2780 lowered Amsterdam's intrinsic to 15000, so the buffer
        // over-funded the frame by 6000 and let an OOG-by-1 system contract avoid
        // running out of gas.)
        gas_limit: SYS_CALL_GAS_LIMIT,
        block_number: block_header.number,
        coinbase: block_header.coinbase,
        timestamp: block_header.timestamp,
        prev_randao: Some(block_header.prev_randao),
        base_fee_per_gas: U256::zero(),
        gas_price: U256::zero(),
        block_excess_blob_gas: block_header.excess_blob_gas,
        block_blob_gas_used: block_header.blob_gas_used,
        // Use the actual block's gas_limit so EIP-8037 cost_per_state_byte is correct.
        // The gas-allowance check is bypassed via `is_system_call` below; feeding
        // i64::MAX here would make cpsb astronomically large and OOG any SSTORE
        // that charges state gas (e.g. EIP-2935, EIP-4788 new-slot writes).
        block_gas_limit: block_header.gas_limit,
        is_system_call: true,
        config,
        ..Default::default()
    };

    // Invariant: with a zero gas price (and `is_system_call` making the hook
    // skip the sender path entirely) a system call leaves no SYSTEM_ADDRESS
    // state behind — no nonce bump, no balance change, not even a read. If
    // this ever becomes non-zero, the hook's system-call branches must be
    // revisited.
    debug_assert!(
        env.gas_price.is_zero() && env.base_fee_per_gas.is_zero(),
        "system calls must run with a zero gas price"
    );

    // This check is not necessary in practice, since contract deployment has succesfully happened in all relevant testnets and mainnet
    // However, it's necessary to pass some of the Hive tests related to system contract deployment, which is why we have it
    // The error that should be returned for the relevant contracts is indicated in the following:
    // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7002.md#empty-code-failure
    // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7251.md#empty-code-failure
    // EIP-8282 applies the same empty-code-failure rule to the builder deposit/exit predeploys.
    // No extra fork guard is needed here: builder predeploy addresses only reach this function via
    // extract_all_requests_levm, which gates them on fork >= Amsterdam, so a Prague/Osaka block
    // never passes a builder address to this check.
    if PRAGUE_SYSTEM_CONTRACTS
        .iter()
        .chain(AMSTERDAM_REQUEST_PREDEPLOYS.iter())
        .any(|contract| contract.address == contract_address)
        && db.get_account_code(contract_address)?.is_empty()
    {
        return Err(EvmError::SystemContractCallFailed(format!(
            "System contract: {contract_address} has no code after deployment"
        )));
    };

    let tx = &Transaction::EIP1559Transaction(EIP1559Transaction {
        to: TxKind::Call(contract_address),
        value: U256::zero(),
        data: calldata,
        ..Default::default()
    });
    // EIP-7928: Mark BAL recorder as in system call mode to filter SYSTEM_ADDRESS changes
    if let Some(recorder) = db.bal_recorder.as_mut() {
        recorder.enter_system_call();
    }

    let result = VM::new(
        env,
        db,
        tx,
        LevmCallTracer::disabled(),
        vm_type,
        crypto,
        None,
    )
    .and_then(|mut vm| vm.execute())
    .map_err(EvmError::from);

    // EIP-7928: Exit system call mode before restoring accounts (must run even on error)
    if let Some(recorder) = db.bal_recorder.as_mut() {
        recorder.exit_system_call();
    }

    result
}

#[allow(unreachable_code)]
#[allow(unused_variables)]
pub fn extract_all_requests_levm(
    receipts: &[Receipt],
    db: &mut GeneralizedDatabase,
    header: &BlockHeader,
    vm_type: VMType,
    crypto: &dyn Crypto,
) -> Result<Vec<Requests>, EvmError> {
    if let VMType::L2(_) = vm_type {
        return Err(EvmError::InvalidEVM(
            "extract_all_requests_levm should not be called for L2 VM".to_string(),
        ));
    }

    let chain_config = db.store.get_chain_config()?;
    let fork = chain_config.fork(header.timestamp);

    if fork < Fork::Prague {
        return Ok(Default::default());
    }

    let withdrawals_data: Vec<u8> = LEVM::read_withdrawal_requests(header, db, vm_type, crypto)?
        .output
        .into();
    let consolidation_data: Vec<u8> =
        LEVM::dequeue_consolidation_requests(header, db, vm_type, crypto)?
            .output
            .into();

    let deposits = Requests::from_deposit_receipts(chain_config.deposit_contract_address, receipts)
        .ok_or(EvmError::InvalidDepositRequest)?;
    let withdrawals = Requests::from_withdrawals_data(withdrawals_data);
    let consolidation = Requests::from_consolidation_data(consolidation_data);

    let mut requests = vec![deposits, withdrawals, consolidation];

    // EIP-8282 (Amsterdam): builder deposit (0x03) and builder exit (0x04) requests.
    // Prague (18) < Amsterdam (25), so this needs a separate explicit gate; appending
    // unconditionally after the Prague early-return above would emit these on Prague/Osaka blocks.
    if fork >= Fork::Amsterdam {
        // Grow to exactly 5 once, avoiding a realloc on each of the two pushes below.
        requests.reserve_exact(2);
        let builder_deposit_data: Vec<u8> =
            LEVM::read_builder_deposit_requests(header, db, vm_type, crypto)?
                .output
                .into();
        let builder_exit_data: Vec<u8> =
            LEVM::dequeue_builder_exit_requests(header, db, vm_type, crypto)?
                .output
                .into();
        requests.push(Requests::from_builder_deposit_data(builder_deposit_data));
        requests.push(Requests::from_builder_exit_data(builder_exit_data));
    }

    Ok(requests)
}

/// Calculating gas_price according to EIP-1559 rules
/// See https://github.com/ethereum/go-ethereum/blob/7ee9a6e89f59cee21b5852f5f6ffa2bcfc05a25f/internal/ethapi/transaction_args.go#L430
pub fn calculate_gas_price_for_generic(tx: &GenericTransaction, basefee: u64) -> U256 {
    if !tx.gas_price.is_zero() {
        // Legacy gas field was specified, use it
        tx.gas_price
    } else {
        // Backfill the legacy gas price for EVM execution, (zero if max_fee_per_gas is zero)
        min(
            tx.max_priority_fee_per_gas.unwrap_or(0) + basefee,
            tx.max_fee_per_gas.unwrap_or(0),
        )
        .into()
    }
}

pub fn calculate_gas_price_for_tx(
    tx: &Transaction,
    mut fee_per_gas: u64,
    vm_type: &VMType,
) -> Result<U256, VMError> {
    let Some(max_priority_fee) = tx.max_priority_fee() else {
        // Legacy transaction
        return Ok(tx.gas_price());
    };

    let max_fee_per_gas = tx.max_fee_per_gas().ok_or(VMError::TxValidation(
        TxValidationError::InsufficientMaxFeePerGas,
    ))?;

    if let VMType::L2(fee_config) = vm_type
        && let Some(operator_fee_config) = &fee_config.operator_fee_config
    {
        fee_per_gas += operator_fee_config.operator_fee_per_gas;
    }

    if fee_per_gas > max_fee_per_gas {
        return Err(VMError::TxValidation(
            TxValidationError::InsufficientMaxFeePerGas,
        ));
    }

    Ok(min(max_priority_fee + fee_per_gas, max_fee_per_gas).into())
}

/// When basefee tracking is disabled  (ie. env.disable_base_fee = true; env.disable_block_gas_limit = true;)
/// and no gas prices were specified, lower the basefee to 0 to avoid breaking EVM invariants (basefee < feecap)
/// See https://github.com/ethereum/go-ethereum/blob/00294e9d28151122e955c7db4344f06724295ec5/core/vm/evm.go#L137
pub(crate) fn adjust_disabled_base_fee(env: &mut Environment) {
    if env.gas_price == U256::zero() {
        env.base_fee_per_gas = U256::zero();
    }
    // Same for the blob fee: a call object that opts out by passing `maxFeePerBlobGas: 0`
    // must not be rejected for undercutting the block's blob base fee.
    // `base_blob_fee_per_gas` is the field the validation and the up-front cost both read
    // (`validate_max_fee_per_blob_gas`, `deduct_caller`), and it is computed in
    // `env_from_generic` before this runs, so it is what has to be lowered. Clearing
    // `block_excess_blob_gas` instead had no effect: nothing reads that field, and the
    // blob base fee derived from a zero excess still has a floor of 1, which a zero fee
    // cap undercuts just the same.
    if env
        .tx_max_fee_per_blob_gas
        .is_some_and(|v| v == U256::zero())
    {
        env.base_blob_fee_per_gas = U256::zero();
    }
}

/// When l2 fees are disabled (ie. env.gas_price = 0), set fee configs to None to avoid breaking failing fee deductions
fn adjust_disabled_l2_fees(env: &Environment, vm_type: VMType) -> VMType {
    if env.gas_price == U256::zero()
        && let VMType::L2(fee_config) = vm_type
    {
        // Don't deduct fees if no gas price is set
        return VMType::L2(FeeConfig {
            operator_fee_config: None,
            l1_fee_config: None,
            ..fee_config
        });
    }
    vm_type
}

pub(crate) fn env_from_generic(
    tx: &GenericTransaction,
    header: &BlockHeader,
    db: &GeneralizedDatabase,
    vm_type: VMType,
) -> Result<Environment, VMError> {
    let chain_config = db.store.get_chain_config()?;
    let gas_price =
        calculate_gas_price_for_generic(tx, header.base_fee_per_gas.unwrap_or(INITIAL_BASE_FEE));
    let block_excess_blob_gas = header.excess_blob_gas;
    let config = EVMConfig::new_from_chain_config(&chain_config, header);

    // slot_number: default a missing value to zero exactly like
    // `setup_env_with_config` (the block-execution env builder) does, rather
    // than erroring on Amsterdam+. A canonical Amsterdam header always carries
    // a slot — `validate_prague_header_fields` rejects one that doesn't, the
    // genesis builder fills in Some(0), and engine_newPayloadV5 requires the
    // field — so this branch is not reachable on a well-formed chain. It is
    // defense in depth, and it belongs on the execution side (where a missing
    // slot would be a consensus fault) rather than in simulation, whose job is
    // to predict what execution does: a header the executor would happily run
    // with SLOTNUM reading 0 must not make eth_call fail. That divergence is
    // reachable if a devnet's fork timestamps are moved so that Amsterdam
    // retroactively covers already-stored pre-Amsterdam headers.
    // For L2 chains, slot_number is always 0.
    let slot_number = if let VMType::L2(_) = vm_type {
        U256::zero()
    } else {
        header.slot_number.map(U256::from).unwrap_or(U256::zero())
    };

    Ok(Environment {
        origin: tx.from.0.into(),
        gas_limit: tx
            .gas
            .unwrap_or(get_max_allowed_gas_limit(header.gas_limit, config.fork)), // Ensure tx doesn't fail due to gas limit
        config,
        block_number: header.number,
        coinbase: header.coinbase,
        timestamp: header.timestamp,
        prev_randao: Some(header.prev_randao),
        slot_number,
        chain_id: chain_config.chain_id.into(),
        base_fee_per_gas: header.base_fee_per_gas.unwrap_or_default().into(),
        base_blob_fee_per_gas: get_base_fee_per_blob_gas(block_excess_blob_gas, &config)?,
        gas_price,
        block_excess_blob_gas,
        block_blob_gas_used: header.blob_gas_used,
        tx_blob_hashes: tx.blob_versioned_hashes.clone(),
        tx_max_priority_fee_per_gas: tx.max_priority_fee_per_gas.map(U256::from),
        tx_max_fee_per_gas: tx.max_fee_per_gas.map(U256::from),
        tx_max_fee_per_blob_gas: tx.max_fee_per_blob_gas,
        tx_nonce: tx.nonce.unwrap_or_default(),
        block_gas_limit: header.gas_limit,
        difficulty: header.difficulty,
        is_privileged: false,
        fee_token: tx.fee_token,
        disable_balance_check: false,
        // Every `env_from_generic` caller is a simulation RPC (eth_call,
        // eth_estimateGas, eth_createAccessList, debug_traceCall). Those run
        // relaxed messages with no nonce enforcement: a call object without
        // `nonce` defaults `tx_nonce` to 0 above, which the hook would otherwise
        // reject for any sender whose nonce is nonzero.
        disable_nonce_check: true,
        // Same reasoning: a caller passing a `gas` above the block allowance or above the
        // EIP-7825 per-transaction cap still expects an answer, because nothing is being
        // submitted. `eth_createAccessList` enforced both until now, which rejected call
        // objects that the other simulation RPCs answered, and which tools that pass the
        // block gas limit as `gas` hit routinely.
        disable_gas_allowance_check: true,
        // Same reasoning: a simulated call carries no signature, so EIP-3607 has nothing
        // to protect, and a contract as `from` is a normal thing to simulate.
        disable_sender_eoa_check: true,
        is_system_call: false,
    })
}

/// Build a synthetic `Transaction` from a `GenericTransaction` for simulation paths.
///
/// Split out from `vm_from_generic` so the caller owns the resulting `Transaction` for at least
/// the VM's lifetime — `VM` now borrows its tx (`&'a Transaction`) instead of cloning it.
///
/// The envelope fields are carried over even though execution reads almost none of them,
/// because the L2 prices its L1 data fee from `Transaction::length()` — the RLP-encoded byte
/// count — and reserves that as gas before execution. Leaving them at their defaults made a
/// simulated transaction encode up to ~104 bytes shorter than the signed one it stands for
/// (a zero `U256` is one RLP byte, a real signature component is 33), so `eth_estimateGas`
/// under-reserved the L1 fee and the transaction it was estimating ran out of gas. The
/// signature is stamped at full width for the same reason, and deliberately at the maximum:
/// a real component can be shorter after leading-zero trimming, and over-reserving the L1
/// fee is safe where under-reserving is not.
fn generic_tx_to_transaction(tx: &GenericTransaction) -> Result<Transaction, VMError> {
    // Not read during execution — the sender comes from `env.origin` — so this only has to
    // encode to the same width as a real signature.
    let (signature_r, signature_s) = (U256::MAX, U256::MAX);
    let nonce = tx.nonce.unwrap_or_default();
    let gas_limit = tx.gas.unwrap_or_default();
    let max_fee_per_gas = tx.max_fee_per_gas.unwrap_or_default();
    let max_priority_fee_per_gas = tx.max_priority_fee_per_gas.unwrap_or_default();
    let chain_id = tx.chain_id.unwrap_or_default();

    Ok(match &tx.authorization_list {
        Some(authorization_list) => Transaction::EIP7702Transaction(EIP7702Transaction {
            to: match tx.to {
                TxKind::Call(to) => to,
                TxKind::Create => {
                    return Err(InternalError::msg("Generic Tx cannot be create type").into());
                }
            },
            value: tx.value,
            data: tx.input.clone(),
            access_list: tx
                .access_list
                .iter()
                .map(|list| (list.address, list.storage_keys.clone()))
                .collect(),
            authorization_list: authorization_list
                .iter()
                .map(|auth| Into::<AuthorizationTuple>::into(auth.clone()))
                .collect(),
            nonce,
            gas_limit,
            max_fee_per_gas,
            max_priority_fee_per_gas,
            chain_id,
            signature_r,
            signature_s,
            ..Default::default()
        }),
        None => Transaction::EIP1559Transaction(EIP1559Transaction {
            to: tx.to.clone(),
            value: tx.value,
            data: tx.input.clone(),
            access_list: tx
                .access_list
                .iter()
                .map(|list| (list.address, list.storage_keys.clone()))
                .collect(),
            nonce,
            gas_limit,
            max_fee_per_gas,
            max_priority_fee_per_gas,
            chain_id,
            signature_r,
            signature_s,
            ..Default::default()
        }),
    })
}

fn vm_from_generic<'a>(
    tx: &'a Transaction,
    env: Environment,
    db: &'a mut GeneralizedDatabase,
    vm_type: VMType,
    crypto: &'a dyn Crypto,
    stateless_validator: Option<&'a dyn StatelessValidator>,
) -> Result<VM<'a>, VMError> {
    let vm_type = adjust_disabled_l2_fees(&env, vm_type);
    VM::new(
        env,
        db,
        tx,
        LevmCallTracer::disabled(),
        vm_type,
        crypto,
        stateless_validator,
    )
}

pub fn get_max_allowed_gas_limit(block_gas_limit: u64, fork: Fork) -> u64 {
    // EIP-7825 imposes a flat per-tx gas cap (POST_OSAKA_GAS_LIMIT_CAP) from
    // Osaka through the BPO forks. Amsterdam supersedes it with the EIP-8037 2D
    // gas model: tx.gas may exceed the flat cap (the excess funds the state-gas
    // reservoir) and is instead bounded by block_gas_limit on the state
    // dimension, so capping the estimateGas ceiling at the flat value would
    // prevent convergence for state-heavy creations. Mirror the Osaka-only
    // gating used at the other cap sites (block validation, default_hook).
    if fork >= Fork::Osaka && fork < Fork::Amsterdam {
        POST_OSAKA_GAS_LIMIT_CAP
    } else {
        block_gas_limit
    }
}

/// Format a balance diff (signed wei) and try to identify it as a multiple of
/// well-known EIP-8037 state-gas constants (NEW_ACCOUNT, STORAGE_SET, AUTH_*),
/// scaled by a plausible gas_price. Best-effort hint to triage gas-accounting
/// drifts at a glance.
///
/// `dead_code` allowed: only reached via the L1 BAL-validation path, which is
/// not exercised in the L2 build profile so the per-crate analysis flags it.
#[allow(dead_code)]
fn describe_balance_diff(expected: U256, actual: U256) -> String {
    let (sign, mag) = if expected >= actual {
        ("+", expected - actual)
    } else {
        ("-", actual - expected)
    };
    let Ok(mag_u128) = u128::try_from(mag) else {
        return format!("{sign}{mag}");
    };
    if mag_u128 == 0 {
        return "0".to_string();
    }
    let cpsb: u128 = 1530;
    // EIP-8037 state-byte constants
    let consts = [
        ("NEW_ACCOUNT", 120u128),
        ("STORAGE_SET", 64),
        ("AUTH_BASE", 23),
        ("AUTH_TOTAL", 143),
    ];
    // Try common test gas_prices first, then 1 wei/gas as fallback.
    for &gp in &[10u128, 1, 7, 100, 1000, 1_000_000_000] {
        if !mag_u128.is_multiple_of(gp) {
            continue;
        }
        let gas = mag_u128 / gp;
        if !gas.is_multiple_of(cpsb) {
            continue;
        }
        let bytes = gas / cpsb;
        for (name, c) in consts {
            if bytes.is_multiple_of(c) {
                let n = bytes / c;
                return format!(
                    "{sign}{mag_u128} wei (= {gas} gas at {gp} wei/gas = {n}× {name}_state_gas)"
                );
            }
        }
    }
    format!("{sign}{mag_u128} wei")
}

// Exercises the rayon-parallel-BAL execution path (and shares its
// `rayon`-gated imports), so it only builds when that feature is on.
#[cfg(all(test, feature = "rayon"))]
mod bal_tests {
    use super::*;
    use ethrex_common::H256;
    use ethrex_common::types::AccountState;
    use ethrex_common::types::block_access_list::{
        AccountChanges, BalanceChange, NonceChange, SlotChange, StorageChange,
    };
    use ethrex_levm::errors::DatabaseError;

    fn addr(byte: u8) -> Address {
        let mut a = Address::zero();
        a.0[19] = byte;
        a
    }

    /// Minimal in-memory store for testing bal_to_account_updates.
    struct MockStore {
        accounts: FxHashMap<Address, AccountState>,
    }

    impl MockStore {
        fn new() -> Self {
            Self {
                accounts: FxHashMap::default(),
            }
        }

        fn with_account(mut self, address: Address, state: AccountState) -> Self {
            self.accounts.insert(address, state);
            self
        }
    }

    impl Database for MockStore {
        fn get_account_state(&self, address: Address) -> Result<AccountState, DatabaseError> {
            Ok(self.accounts.get(&address).copied().unwrap_or_default())
        }
        fn get_storage_value(&self, _: Address, _: H256) -> Result<U256, DatabaseError> {
            Ok(U256::zero())
        }
        fn get_block_hash(&self, _: u64) -> Result<H256, DatabaseError> {
            Ok(H256::zero())
        }
        fn get_chain_config(&self) -> Result<ethrex_common::types::ChainConfig, DatabaseError> {
            Err(DatabaseError::Custom("not implemented".into()))
        }
        fn get_account_code(&self, _: H256) -> Result<ethrex_common::types::Code, DatabaseError> {
            Ok(ethrex_common::types::Code::from_bytecode(
                Bytes::new(),
                &ethrex_crypto::NativeCrypto,
            ))
        }
        fn get_code_metadata(
            &self,
            _: H256,
        ) -> Result<ethrex_common::types::CodeMetadata, DatabaseError> {
            Ok(ethrex_common::types::CodeMetadata { length: 0 })
        }
    }

    #[test]
    fn test_bal_to_account_updates_basic() {
        // Account with balance + nonce + storage changes → correct AccountUpdate
        let address = addr(1);
        let store = MockStore::new().with_account(
            address,
            AccountState {
                balance: U256::from(100),
                nonce: 5,
                code_hash: *EMPTY_KECCAK_HASH,
                storage_root: H256::zero(),
            },
        );

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address)
                .with_balance_changes(vec![
                    BalanceChange::new(1, U256::from(90)),
                    BalanceChange::new(2, U256::from(80)),
                ])
                .with_nonce_changes(vec![NonceChange::new(1, 6)])
                .with_storage_changes(vec![SlotChange::with_changes(
                    U256::from(42),
                    vec![StorageChange::new(1, U256::from(999))],
                )]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert_eq!(updates.len(), 1);
        let u = &updates[0];
        assert_eq!(u.address, address);
        assert!(!u.removed);
        let info = u.info.as_ref().unwrap();
        // Last balance entry wins
        assert_eq!(info.balance, U256::from(80));
        assert_eq!(info.nonce, 6);
        assert_eq!(info.code_hash, *EMPTY_KECCAK_HASH);
        // Storage
        let key = ethrex_common::utils::u256_to_h256(U256::from(42));
        assert_eq!(*u.added_storage.get(&key).unwrap(), U256::from(999));
    }

    #[test]
    fn test_bal_to_account_updates_highest_index_wins() {
        // Multiple changes per field: the last entry (highest index) wins.
        let address = addr(2);
        let store = MockStore::new().with_account(
            address,
            AccountState {
                balance: U256::from(1000),
                nonce: 0,
                code_hash: *EMPTY_KECCAK_HASH,
                storage_root: H256::zero(),
            },
        );

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address).with_balance_changes(vec![
                BalanceChange::new(1, U256::from(900)),
                BalanceChange::new(2, U256::from(800)),
                BalanceChange::new(3, U256::from(700)),
            ]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert_eq!(updates.len(), 1);
        assert_eq!(updates[0].info.as_ref().unwrap().balance, U256::from(700));
    }

    #[test]
    fn test_bal_to_account_updates_reads_only_skipped() {
        // Account with only storage_reads and no writes → no AccountUpdate.
        let address = addr(3);
        let store = MockStore::new();

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address).with_storage_reads(vec![U256::from(1)]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert!(updates.is_empty());
    }

    #[test]
    fn test_bal_to_account_updates_removal() {
        // Account removal (EIP-161): post-state empty but pre-state existed.
        let address = addr(4);
        let store = MockStore::new().with_account(
            address,
            AccountState {
                balance: U256::from(50),
                nonce: 1,
                code_hash: *EMPTY_KECCAK_HASH,
                storage_root: H256::zero(),
            },
        );

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address)
                .with_balance_changes(vec![BalanceChange::new(1, U256::zero())])
                .with_nonce_changes(vec![NonceChange::new(1, 0)]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert_eq!(updates.len(), 1);
        assert!(updates[0].removed);
    }

    #[test]
    fn test_bal_to_account_updates_storage_zero() {
        // Storage slot set to 0 → included in added_storage (valid trie deletion).
        let address = addr(5);
        let store = MockStore::new();

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address).with_storage_changes(vec![SlotChange::with_changes(
                U256::from(7),
                vec![StorageChange::new(1, U256::zero())],
            )]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert_eq!(updates.len(), 1);
        let key = ethrex_common::utils::u256_to_h256(U256::from(7));
        assert_eq!(*updates[0].added_storage.get(&key).unwrap(), U256::zero());
    }

    #[test]
    fn test_bal_to_account_updates_code_deployment() {
        // Code deployment → correct code_hash computed.
        let address = addr(6);
        let store = MockStore::new();
        let code = Bytes::from(vec![0x60, 0x00, 0x60, 0x00, 0xf3]); // PUSH0 PUSH0 RETURN
        let expected_hash =
            ethrex_common::types::Code::from_bytecode(code.clone(), &ethrex_crypto::NativeCrypto)
                .hash;

        let bal = BlockAccessList::from_accounts(vec![
            AccountChanges::new(address)
                .with_code_changes(vec![
                    ethrex_common::types::block_access_list::CodeChange::new(1, code.clone()),
                ])
                .with_nonce_changes(vec![NonceChange::new(1, 1)]),
        ]);

        let updates = LEVM::bal_to_account_updates(&bal, &store).unwrap();
        assert_eq!(updates.len(), 1);
        let u = &updates[0];
        assert_eq!(u.info.as_ref().unwrap().code_hash, expected_hash);
        assert_eq!(u.code.as_ref().unwrap().code(), &code[..]);
    }
}

#[cfg(test)]
mod system_call_coinbase_tests {
    //! Regression tests for the system-call coinbase collision. When a block's
    //! fee recipient (coinbase) equals the system contract being called,
    //! `generic_system_contract_levm`'s post-call coinbase restore must NOT clobber
    //! the storage write the system call just made; otherwise the write is dropped
    //! from the emitted state updates and the state root diverges from other clients.
    use super::*;
    use ethrex_common::types::{AccountState, AccountUpdate, ChainConfig, Code, CodeMetadata};
    use ethrex_crypto::NativeCrypto;
    use ethrex_levm::db::Database;
    use ethrex_levm::errors::DatabaseError;
    use std::sync::Arc;

    // EIP-2935 history-contract runtime bytecode.
    const HISTORY_RUNTIME_CODE: &str = concat!(
        "3373fffffffffffffffffffffffffffffffffffffffe1460465760203603604257",
        "5f35600143038111604257611fff81430311604257611fff900654",
        "5f5260205ff35b5f5ffd5b5f35611fff60014303065500",
    );

    struct Store {
        chain_config: ChainConfig,
        history_code: Code,
    }

    impl Database for Store {
        fn get_account_state(&self, address: Address) -> Result<AccountState, DatabaseError> {
            if address == HISTORY_STORAGE_ADDRESS.address {
                return Ok(AccountState {
                    nonce: 1,
                    code_hash: self.history_code.hash,
                    ..Default::default()
                });
            }
            Ok(AccountState::default())
        }
        fn get_storage_value(&self, _: Address, _: H256) -> Result<U256, DatabaseError> {
            Ok(U256::zero())
        }
        fn get_block_hash(&self, _: u64) -> Result<H256, DatabaseError> {
            Ok(H256::zero())
        }
        fn get_chain_config(&self) -> Result<ChainConfig, DatabaseError> {
            Ok(self.chain_config)
        }
        fn get_account_code(&self, code_hash: H256) -> Result<Code, DatabaseError> {
            if code_hash == self.history_code.hash {
                return Ok(self.history_code.clone());
            }
            Ok(Code::default())
        }
        fn get_code_metadata(&self, code_hash: H256) -> Result<CodeMetadata, DatabaseError> {
            let length = if code_hash == self.history_code.hash {
                self.history_code.len() as u64
            } else {
                0
            };
            Ok(CodeMetadata { length })
        }
    }

    fn history_code() -> Code {
        let bytes = hex::decode(HISTORY_RUNTIME_CODE).expect("history runtime code is valid hex");
        Code::from_bytecode(Bytes::from(bytes), &NativeCrypto)
    }

    fn prague_db() -> GeneralizedDatabase {
        GeneralizedDatabase::new(Arc::new(Store {
            chain_config: ChainConfig {
                prague_time: Some(0),
                ..Default::default()
            },
            history_code: history_code(),
        }))
    }

    fn parent_hash_value(parent_hash: H256) -> U256 {
        U256::from_big_endian(parent_hash.as_bytes())
    }

    fn history_slot(block_number: u64) -> H256 {
        H256::from_low_u64_be((block_number - 1) % 8191)
    }

    /// Run the EIP-2935 system call for block 42 with the given fee recipient and
    /// return (slot value cached on the history contract, emitted state updates,
    /// parent hash).
    fn run_history_update(coinbase: Address) -> (Option<U256>, Vec<AccountUpdate>, H256) {
        let mut db = prague_db();
        let parent_hash = H256::from_low_u64_be(0x2935);
        let block_number = 42;
        let header = BlockHeader {
            parent_hash,
            coinbase,
            number: block_number,
            timestamp: 1,
            ..Default::default()
        };

        LEVM::process_block_hash_history(&header, &mut db, VMType::L1, &NativeCrypto)
            .expect("history system call executes");

        let slot = history_slot(block_number);
        let stored_value = db
            .current_accounts_state
            .get(&HISTORY_STORAGE_ADDRESS.address)
            .and_then(|account| account.storage.get(&slot).copied());
        let updates =
            LEVM::get_state_transitions(&mut db).expect("state transitions are generated");

        (stored_value, updates, parent_hash)
    }

    fn assert_history_write_emitted(coinbase: Address) {
        let (stored_value, updates, parent_hash) = run_history_update(coinbase);
        let slot = history_slot(42);
        assert_eq!(
            stored_value,
            Some(parent_hash_value(parent_hash)),
            "history storage must hold the parent hash after the system call"
        );
        assert!(
            updates.iter().any(|update| {
                update.address == HISTORY_STORAGE_ADDRESS.address
                    && update.added_storage.get(&slot) == Some(&parent_hash_value(parent_hash))
            }),
            "the history-contract storage write must be emitted as a state update"
        );
    }

    #[test]
    fn ordinary_coinbase_preserves_history_storage_write() {
        assert_history_write_emitted(Address::from_low_u64_be(0xbeef));
    }

    /// Regression: a fee recipient equal to the EIP-2935 history contract must not
    /// cause the system call's storage write to be dropped by the coinbase restore.
    #[test]
    fn history_address_coinbase_preserves_history_storage_write() {
        assert_history_write_emitted(HISTORY_STORAGE_ADDRESS.address);
    }
}

/// Tests for EIP-8079 burned_fees computation in execute_block (LStar-gated).
///
/// Shares the non-guest execution path's `rayon`-gated imports
/// (`EMPTY_KECCAK_HASH`, `FxHashMap`, `Database`), so it only builds with rayon.
#[cfg(all(test, feature = "rayon"))]
mod burned_fees_tests {
    use super::*;
    use ethrex_common::{
        constants::{DEFAULT_OMMERS_HASH, DEFAULT_REQUESTS_HASH},
        types::{AccountState, Block, BlockBody, BlockHeader, ChainConfig, Code, CodeMetadata},
        utils::keccak,
    };
    use ethrex_crypto::NativeCrypto;
    use ethrex_levm::errors::DatabaseError;
    use ethrex_rlp::encode::PayloadRLPEncode;

    // ── helpers ─────────────────────────────────────────────────────────────

    fn lstar_chain_config() -> ChainConfig {
        ChainConfig {
            chain_id: 1,
            homestead_block: Some(0),
            eip150_block: Some(0),
            eip155_block: Some(0),
            eip158_block: Some(0),
            byzantium_block: Some(0),
            constantinople_block: Some(0),
            petersburg_block: Some(0),
            istanbul_block: Some(0),
            berlin_block: Some(0),
            london_block: Some(0),
            terminal_total_difficulty: Some(0),
            terminal_total_difficulty_passed: true,
            shanghai_time: Some(0),
            cancun_time: Some(0),
            prague_time: Some(0),
            amsterdam_time: Some(0),
            lstar_time: Some(0),
            ..Default::default()
        }
    }

    fn amsterdam_chain_config() -> ChainConfig {
        ChainConfig {
            chain_id: 1,
            homestead_block: Some(0),
            eip150_block: Some(0),
            eip155_block: Some(0),
            eip158_block: Some(0),
            byzantium_block: Some(0),
            constantinople_block: Some(0),
            petersburg_block: Some(0),
            istanbul_block: Some(0),
            berlin_block: Some(0),
            london_block: Some(0),
            terminal_total_difficulty: Some(0),
            terminal_total_difficulty_passed: true,
            shanghai_time: Some(0),
            cancun_time: Some(0),
            prague_time: Some(0),
            amsterdam_time: Some(0),
            lstar_time: None, // no LStar → burned_fees must be None
            ..Default::default()
        }
    }

    // ── mock database ────────────────────────────────────────────────────────

    struct MockDB {
        chain_config: ChainConfig,
        accounts: FxHashMap<Address, (AccountState, Bytes)>,
        /// Pre-set storage values (address, slot) → value.
        storage: FxHashMap<(Address, H256), U256>,
    }

    impl MockDB {
        fn new(chain_config: ChainConfig) -> Self {
            Self {
                chain_config,
                accounts: FxHashMap::default(),
                storage: FxHashMap::default(),
            }
        }

        fn with_account(mut self, address: Address, balance: U256, code: Option<Bytes>) -> Self {
            let code_bytes = code.unwrap_or_default();
            let code_hash = if code_bytes.is_empty() {
                *EMPTY_KECCAK_HASH
            } else {
                keccak(&code_bytes)
            };
            let state = AccountState {
                nonce: 0,
                balance,
                storage_root: H256::zero(),
                code_hash,
            };
            self.accounts.insert(address, (state, code_bytes));
            self
        }

        fn with_storage(mut self, address: Address, slot: H256, value: U256) -> Self {
            self.storage.insert((address, slot), value);
            self
        }
    }

    impl Database for MockDB {
        fn get_account_state(&self, address: Address) -> Result<AccountState, DatabaseError> {
            Ok(self
                .accounts
                .get(&address)
                .map(|(s, _)| *s)
                .unwrap_or_default())
        }

        fn get_storage_value(&self, address: Address, slot: H256) -> Result<U256, DatabaseError> {
            Ok(self
                .storage
                .get(&(address, slot))
                .copied()
                .unwrap_or_default())
        }

        fn get_block_hash(&self, _: u64) -> Result<H256, DatabaseError> {
            Ok(H256::zero())
        }

        fn get_chain_config(&self) -> Result<ChainConfig, DatabaseError> {
            Ok(self.chain_config)
        }

        fn get_account_code(&self, code_hash: H256) -> Result<Code, DatabaseError> {
            let crypto = NativeCrypto;
            for (state, bytecode) in self.accounts.values() {
                if state.code_hash == code_hash && !bytecode.is_empty() {
                    return Ok(Code::from_bytecode(bytecode.clone(), &crypto));
                }
            }
            Ok(Code::from_bytecode(Bytes::new(), &crypto))
        }

        fn get_code_metadata(&self, code_hash: H256) -> Result<CodeMetadata, DatabaseError> {
            for (state, bytecode) in self.accounts.values() {
                if state.code_hash == code_hash {
                    return Ok(CodeMetadata {
                        length: bytecode.len() as u64,
                    });
                }
            }
            Ok(CodeMetadata { length: 0 })
        }
    }

    // ── signing ──────────────────────────────────────────────────────────────

    const PRIVATE_KEY: [u8; 32] = [
        0x4c, 0x08, 0x83, 0xa6, 0x91, 0x02, 0x93, 0x7d, 0x62, 0x31, 0x47, 0x1b, 0x5d, 0xbb, 0x62,
        0x04, 0xfe, 0x51, 0x29, 0x61, 0x70, 0x82, 0x79, 0x2a, 0xe4, 0x68, 0xd0, 0x1a, 0x3f, 0x36,
        0x23, 0x18,
    ];

    fn sender_address() -> Address {
        use k256::ecdsa::SigningKey;
        let signing_key = SigningKey::from_bytes(&PRIVATE_KEY.into()).expect("valid key");
        let uncompressed_pub = signing_key.verifying_key().to_encoded_point(false);
        let pub_hash = keccak(&uncompressed_pub.as_bytes()[1..]);
        Address::from_slice(&pub_hash.0[12..])
    }

    /// Build a signed EIP-1559 ETH transfer.
    /// base_fee = BASE_FEE, gas_limit = 21_000 → gas_spent = 21_000 (no refunds).
    fn signed_eth_transfer(to: Address, nonce: u64, base_fee: u64) -> Transaction {
        use k256::ecdsa::SigningKey;

        let tx = EIP1559Transaction {
            chain_id: 1,
            nonce,
            max_priority_fee_per_gas: 1,
            max_fee_per_gas: base_fee + 10,
            gas_limit: 21_000,
            to: TxKind::Call(to),
            value: U256::zero(),
            ..Default::default()
        };

        // Signing hash: [0x02] || RLP(chain_id, nonce, max_priority, max_fee, gas, to, value, data, access_list)
        let mut buf = vec![0x02u8];
        tx.encode_payload(&mut buf);
        let hash = keccak(&buf);

        let signing_key = SigningKey::from_bytes(&PRIVATE_KEY.into()).expect("valid key");
        let (sig, rid) = signing_key
            .sign_prehash_recoverable(&hash.0)
            .expect("sign ok");
        let sig_bytes = sig.to_bytes();

        Transaction::EIP1559Transaction(EIP1559Transaction {
            signature_r: U256::from_big_endian(&sig_bytes[..32]),
            signature_s: U256::from_big_endian(&sig_bytes[32..]),
            signature_y_parity: rid.to_byte() != 0,
            ..tx
        })
    }

    // STOP opcode: system contracts need non-empty code (EIP-7002/7251 check).
    const STOP_BYTECODE: &[u8] = &[0x00];

    fn make_block_with_tx(tx: Transaction, base_fee: u64) -> Block {
        let header = BlockHeader {
            parent_hash: H256::zero(),
            ommers_hash: *DEFAULT_OMMERS_HASH,
            number: 1,
            gas_limit: 1_000_000,
            timestamp: 1,
            base_fee_per_gas: Some(base_fee),
            withdrawals_root: Some(H256::zero()),
            requests_hash: Some(*DEFAULT_REQUESTS_HASH),
            // No parent_beacon_block_root → skip beacon-root system call.
            parent_beacon_block_root: None,
            ..Default::default()
        };
        let body = BlockBody {
            transactions: vec![tx],
            ommers: vec![],
            withdrawals: Some(vec![]),
        };
        Block::new(header, body)
    }

    fn make_gen_db(chain_config: ChainConfig) -> GeneralizedDatabase {
        let sender = sender_address();
        let stop = Bytes::from_static(STOP_BYTECODE);
        let mock = MockDB::new(chain_config)
            // Fund the sender with enough ETH to pay for gas + priority fee.
            .with_account(sender, U256::from(1_000_000_000u64), None)
            // Prague system contracts require non-empty code (EIP-7002/7251).
            .with_account(
                WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            .with_account(
                CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            // EIP-8282 (Amsterdam) builder predeploys require non-empty code.
            .with_account(
                BUILDER_DEPOSIT_CONTRACT_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            .with_account(
                BUILDER_EXIT_CONTRACT_ADDRESS.address,
                U256::zero(),
                Some(stop),
            );
        GeneralizedDatabase::new(Arc::new(mock))
    }

    // Contract bytecode: PUSH1 0x00, PUSH1 0x00, SSTORE, STOP
    // Clears storage slot 0.  When the slot's initial value is non-zero, the
    // EIP-3529 "SSTORE_CLEARS_SCHEDULE" refund of 4800 gas is applied, making
    // gas_used > gas_spent so block_gas_used ≠ cumulative_gas_used.
    const SSTORE_CLEAR_BYTECODE: &[u8] = &[0x60, 0x00, 0x60, 0x00, 0x55, 0x00];

    /// Build a signed EIP-1559 call to `to` with an explicit gas limit.
    fn signed_call_tx(to: Address, nonce: u64, base_fee: u64, gas_limit: u64) -> Transaction {
        use k256::ecdsa::SigningKey;

        let tx = EIP1559Transaction {
            chain_id: 1,
            nonce,
            max_priority_fee_per_gas: 1,
            max_fee_per_gas: base_fee + 10,
            gas_limit,
            to: TxKind::Call(to),
            value: U256::zero(),
            ..Default::default()
        };

        let mut buf = vec![0x02u8];
        tx.encode_payload(&mut buf);
        let hash = keccak(&buf);

        let signing_key = SigningKey::from_bytes(&PRIVATE_KEY.into()).expect("valid key");
        let (sig, rid) = signing_key
            .sign_prehash_recoverable(&hash.0)
            .expect("sign ok");
        let sig_bytes = sig.to_bytes();

        Transaction::EIP1559Transaction(EIP1559Transaction {
            signature_r: U256::from_big_endian(&sig_bytes[..32]),
            signature_s: U256::from_big_endian(&sig_bytes[32..]),
            signature_y_parity: rid.to_byte() != 0,
            ..tx
        })
    }

    /// DB with SSTORE_CLEAR_BYTECODE deployed at `contract_addr` and slot 0 = 1.
    /// The sender has extra ETH to cover the higher gas_limit in the call tx.
    fn make_gen_db_sstore_clear(
        chain_config: ChainConfig,
        contract_addr: Address,
    ) -> GeneralizedDatabase {
        let sender = sender_address();
        let stop = Bytes::from_static(STOP_BYTECODE);
        let mock = MockDB::new(chain_config)
            .with_account(sender, U256::from(100_000_000_000u64), None)
            .with_account(
                WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            .with_account(
                CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            // EIP-8282 (Amsterdam) builder predeploys require non-empty code.
            .with_account(
                BUILDER_DEPOSIT_CONTRACT_ADDRESS.address,
                U256::zero(),
                Some(stop.clone()),
            )
            .with_account(
                BUILDER_EXIT_CONTRACT_ADDRESS.address,
                U256::zero(),
                Some(stop),
            )
            .with_account(
                contract_addr,
                U256::zero(),
                Some(Bytes::from_static(SSTORE_CLEAR_BYTECODE)),
            )
            // Slot 0 = 1 so SSTORE(0, 0) transitions 1→0, triggering EIP-3529 refund.
            .with_storage(contract_addr, H256::zero(), U256::one());
        GeneralizedDatabase::new(Arc::new(mock))
    }

    // ── tests ────────────────────────────────────────────────────────────────

    #[test]
    fn test_burned_fees_block_aggregate() {
        use std::sync::atomic::AtomicUsize;
        use std::sync::mpsc;

        const BASE_FEE: u64 = 10;
        // Simple ETH transfer, no blobs (blob_gas_used = 0, blob_base_fee = 0).
        // The exact gas charged depends on the active fork's gas model, so this test
        // verifies the burned_fees FORMULA against the block's actual post-refund gas
        // (rather than a hardcoded amount) plus the execute_block ↔ execute_block_pipeline
        // cross-path consistency and the LStar gate. The refund-sensitive case lives in
        // test_burned_fees_post_refund_discriminating.

        let recipient = Address::from_low_u64_be(0xBEEF);
        let tx = signed_eth_transfer(recipient, 0, BASE_FEE);
        let block = make_block_with_tx(tx, BASE_FEE);
        let crypto = NativeCrypto;

        // (a) execute_block path
        let mut db1 = make_gen_db(lstar_chain_config());
        let (result, _bal) = LEVM::execute_block(&block, &mut db1, VMType::L1, &crypto, None)
            .expect("execute_block succeeded");
        let cumulative = result
            .receipts
            .last()
            .map(|r| r.cumulative_gas_used)
            .unwrap_or(0);
        let expected = compute_burned_fees(BASE_FEE, cumulative, 0, 0);
        assert_eq!(
            result.burned_fees,
            Some(expected),
            "execute_block: burned_fees must equal base_fee * post-refund gas ({cumulative})"
        );

        // (b) execute_block_pipeline path (sequential: no BAL provided)
        let mut db2 = make_gen_db(lstar_chain_config());
        let (tx_sender, _rx) = mpsc::channel();
        let queue_len = AtomicUsize::new(0);
        let tx2 = signed_eth_transfer(recipient, 0, BASE_FEE);
        let block2 = make_block_with_tx(tx2, BASE_FEE);
        let (result_pipeline, _bal) = LEVM::execute_block_pipeline(
            &block2,
            &mut db2,
            VMType::L1,
            Some(tx_sender),
            &queue_len,
            &crypto,
            None, // no BAL → sequential path
            false,
            None,
        )
        .expect("execute_block_pipeline succeeded");
        assert_eq!(
            result_pipeline.burned_fees, result.burned_fees,
            "execute_block_pipeline: burned_fees must match execute_block (production/verification consistency)"
        );

        // (c) pre-LStar → None
        let tx3 = signed_eth_transfer(recipient, 0, BASE_FEE);
        let block3 = make_block_with_tx(tx3, BASE_FEE);
        let mut db3 = make_gen_db(amsterdam_chain_config());
        let (result_pre_lstar, _) =
            LEVM::execute_block(&block3, &mut db3, VMType::L1, &crypto, None)
                .expect("pre-LStar execute_block succeeded");
        assert_eq!(
            result_pre_lstar.burned_fees, None,
            "pre-LStar: burned_fees must be None"
        );
    }

    /// Discriminating test: a transaction with an EIP-3529 SSTORE refund makes
    /// `block_gas_used` (pre-refund) differ from `cumulative_gas_used` (post-refund).
    /// The correct `burned_fees` uses post-refund gas; using `block_gas_used` would
    /// over-count by `base_fee * refund_amount`.
    ///
    /// Gas analysis (Prague + Amsterdam era):
    ///   intrinsic = 21 000
    ///   PUSH1 + PUSH1 = 6
    ///   SSTORE cold (original=1, new=0): cold-access 2100 + warm-reset 2900 = 5 000
    ///     refund = SSTORE_CLEARS_SCHEDULE = 4 800 (≤ 26 006 / 5 = 5 201, so full refund)
    ///   gas_used = 26 006, gas_spent = 21 206
    ///
    /// With the wrong code (block_gas_used = 26 006):
    ///   burned_fees = 10 * 26 006 = 260 060  ← test FAILS
    /// After the fix (cumulative_gas_used = 21 206):
    ///   burned_fees = 10 * 21 206 = 212 060  ← test PASSES
    #[test]
    fn test_burned_fees_post_refund_discriminating() {
        use std::sync::atomic::AtomicUsize;
        use std::sync::mpsc;

        const BASE_FEE: u64 = 10;
        // Gas limit must exceed gas_used (26 006); 100 000 is ample.
        const TX_GAS_LIMIT: u64 = 100_000;

        let contract_addr = Address::from_low_u64_be(0xC0DEBEEF);
        let crypto = NativeCrypto;

        // ── (a) execute_block path (site 1) ───────────────────────────────────
        let tx_a = signed_call_tx(contract_addr, 0, BASE_FEE, TX_GAS_LIMIT);
        let block_a = make_block_with_tx(tx_a, BASE_FEE);
        let mut db_a = make_gen_db_sstore_clear(lstar_chain_config(), contract_addr);
        let (result_a, _) = LEVM::execute_block(&block_a, &mut db_a, VMType::L1, &crypto, None)
            .expect("execute_block with SSTORE clear succeeded");

        let cumulative_a = result_a.receipts[0].cumulative_gas_used;
        // Discriminating guard: the refund must have happened.
        assert!(
            result_a.block_gas_used > cumulative_a,
            "SSTORE refund must cause block_gas_used ({}) > cumulative_gas_used ({})",
            result_a.block_gas_used,
            cumulative_a,
        );
        // Correct burned_fees uses post-refund cumulative_gas_used, not block_gas_used.
        assert_eq!(
            result_a.burned_fees,
            Some(BASE_FEE * cumulative_a),
            "execute_block: burned_fees must use post-refund gas (cumulative={}, block_gas_used={})",
            cumulative_a,
            result_a.block_gas_used,
        );

        // ── (b) execute_block_pipeline sequential path (site 3, no BAL) ──────
        let tx_b = signed_call_tx(contract_addr, 0, BASE_FEE, TX_GAS_LIMIT);
        let block_b = make_block_with_tx(tx_b, BASE_FEE);
        let mut db_b = make_gen_db_sstore_clear(lstar_chain_config(), contract_addr);
        let (tx_sender_b, _rx_b) = mpsc::channel();
        let queue_len_b = AtomicUsize::new(0);
        let (result_b, _) = LEVM::execute_block_pipeline(
            &block_b,
            &mut db_b,
            VMType::L1,
            Some(tx_sender_b),
            &queue_len_b,
            &crypto,
            None, // no BAL → sequential path (site 3)
            false,
            None,
        )
        .expect("execute_block_pipeline sequential with SSTORE clear succeeded");

        let cumulative_b = result_b.receipts[0].cumulative_gas_used;
        assert!(
            result_b.block_gas_used > cumulative_b,
            "sequential pipeline: SSTORE refund must be present",
        );
        assert_eq!(
            result_b.burned_fees,
            Some(BASE_FEE * cumulative_b),
            "execute_block_pipeline (sequential): burned_fees must use post-refund gas",
        );

        // ── (c) execute_block_pipeline parallel path (site 2, with BAL) ──────
        // Generate BAL via execute_block, then re-execute using that BAL.
        let tx_c = signed_call_tx(contract_addr, 0, BASE_FEE, TX_GAS_LIMIT);
        let block_c = make_block_with_tx(tx_c, BASE_FEE);
        let mut db_c1 = make_gen_db_sstore_clear(lstar_chain_config(), contract_addr);
        let (_, bal_c) = LEVM::execute_block(&block_c, &mut db_c1, VMType::L1, &crypto, None)
            .expect("BAL generation succeeded");
        let bal_c = bal_c.expect("LStar must produce a BAL");

        let mut db_c2 = make_gen_db_sstore_clear(lstar_chain_config(), contract_addr);
        let (tx_sender_c, _rx_c) = mpsc::channel();
        let queue_len_c = AtomicUsize::new(0);
        let (result_c, _) = LEVM::execute_block_pipeline(
            &block_c,
            &mut db_c2,
            VMType::L1,
            Some(tx_sender_c),
            &queue_len_c,
            &crypto,
            Some(std::sync::Arc::new(bal_c)), // ← parallel path (site 2)
            true,
            None,
        )
        .expect("execute_block_pipeline parallel with SSTORE clear succeeded");

        let cumulative_c = result_c.receipts[0].cumulative_gas_used;
        assert!(
            result_c.block_gas_used > cumulative_c,
            "parallel pipeline: SSTORE refund must be present",
        );
        assert_eq!(
            result_c.burned_fees,
            Some(BASE_FEE * cumulative_c),
            "execute_block_pipeline (parallel): burned_fees must use post-refund gas",
        );

        // ── cross-path consistency ─────────────────────────────────────────────
        assert_eq!(
            result_a.burned_fees, result_b.burned_fees,
            "execute_block vs sequential pipeline: burned_fees must match",
        );
        assert_eq!(
            result_a.burned_fees, result_c.burned_fees,
            "execute_block vs parallel pipeline: burned_fees must match",
        );

        // ── (d) pre-LStar → None ──────────────────────────────────────────────
        let tx_d = signed_call_tx(contract_addr, 0, BASE_FEE, TX_GAS_LIMIT);
        let block_d = make_block_with_tx(tx_d, BASE_FEE);
        let mut db_d = make_gen_db_sstore_clear(amsterdam_chain_config(), contract_addr);
        let (result_d, _) = LEVM::execute_block(&block_d, &mut db_d, VMType::L1, &crypto, None)
            .expect("pre-LStar with SSTORE clear succeeded");
        assert_eq!(
            result_d.burned_fees, None,
            "pre-LStar: burned_fees must be None"
        );
    }
}

#[cfg(test)]
mod simulated_tx_encoding_tests {
    //! The L2 prices its L1 data fee from `Transaction::length()` and reserves that
    //! as gas before execution, so a simulated transaction must never encode shorter
    //! than the signed one it stands for — otherwise `eth_estimateGas` reserves less
    //! L1 fee gas than the transaction it is estimating will need, and that
    //! transaction runs out of gas at exactly the estimate.
    //!
    //! This was reached through the L2 fee-token integration test, which submits the
    //! estimate verbatim. It only surfaced once the estimate became exact; the search's
    //! former 1.5% tolerance had been absorbing the shortfall.
    use super::*;
    use ethrex_common::types::GenericTransaction;
    use ethrex_rlp::encode::RLPEncode;

    /// The transaction the SDK signs and sends after estimating, with signature
    /// components at the width secp256k1 actually produces.
    fn signed_equivalent(gas_limit: u64, data: Bytes) -> Transaction {
        Transaction::EIP1559Transaction(EIP1559Transaction {
            chain_id: 9_999,
            nonce: 42,
            max_priority_fee_per_gas: 1_000_000_000,
            max_fee_per_gas: 2_000_000_000,
            gas_limit,
            to: TxKind::Create,
            value: U256::from(1u64),
            data,
            access_list: vec![],
            signature_y_parity: true,
            signature_r: U256::MAX,
            signature_s: U256::MAX,
            ..Default::default()
        })
    }

    fn generic(gas_limit: u64, data: Bytes) -> GenericTransaction {
        GenericTransaction {
            to: TxKind::Create,
            value: U256::from(1u64),
            input: data,
            nonce: Some(42),
            gas: Some(gas_limit),
            max_fee_per_gas: Some(2_000_000_000),
            max_priority_fee_per_gas: Some(1_000_000_000),
            chain_id: Some(9_999),
            ..Default::default()
        }
    }

    #[test]
    fn simulated_tx_never_encodes_shorter_than_the_signed_one() {
        for (gas_limit, data) in [
            (21_000u64, Bytes::new()),
            (150_000, Bytes::from_static(&[0x60, 0x00, 0x60, 0x00])),
            (5_000_000, Bytes::from(vec![0xab; 4_096])),
        ] {
            let simulated = generic_tx_to_transaction(&generic(gas_limit, data.clone()))
                .expect("conversion should succeed");
            let signed = signed_equivalent(gas_limit, data.clone());
            assert!(
                simulated.length() >= signed.length(),
                "simulated tx encodes {} bytes, signed one {} — the L2 would under-reserve \
                 the L1 fee by the difference (gas_limit {gas_limit}, {} data bytes)",
                simulated.length(),
                signed.length(),
                data.len(),
            );
        }
    }
}