mega-evm 1.6.0

The evm tailored for the MegaETH
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
//! Tests for the `MegaAccessControl` system contract.
//!
//! When a contract calls `ACCESS_CONTROL_ADDRESS.disableVolatileDataAccess()`, the caller's
//! frame and all inner calls that access volatile data will revert with
//! `VolatileDataAccessDisabled()` error.

use std::convert::Infallible;

use alloy_primitives::{address, Address, Bytes, U256};
use alloy_sol_types::{SolCall, SolError};
use mega_evm::{
    test_utils::{BytecodeBuilder, MemoryDatabase},
    IMegaAccessControl, MegaContext, MegaEvm, MegaHaltReason, MegaSpecId, MegaTransaction,
    MegaTransactionError, TestExternalEnvs, VolatileDataAccessType, ACCESS_CONTROL_ADDRESS,
    ORACLE_CONTRACT_ADDRESS,
};
use revm::{
    bytecode::opcode::*,
    context::{
        result::{EVMError, ResultAndState},
        tx::TxEnvBuilder,
        ContextTr, TxEnv,
    },
    interpreter::{CallInputs, CallOutcome, InterpreterTypes},
    Inspector,
};

// Test addresses
const CALLER: Address = address!("0000000000000000000000000000000000200000");
const PARENT: Address = address!("0000000000000000000000000000000000200001");
const CHILD: Address = address!("0000000000000000000000000000000000200002");
const GRANDCHILD: Address = address!("0000000000000000000000000000000000200003");
const SIBLING: Address = address!("0000000000000000000000000000000000200004");

/// The 4-byte selector for `disableVolatileDataAccess()`.
const DISABLE_VOLATILE_DATA_ACCESS_SELECTOR: [u8; 4] =
    IMegaAccessControl::disableVolatileDataAccessCall::SELECTOR;

/// The 4-byte selector for `enableVolatileDataAccess()`.
const ENABLE_VOLATILE_DATA_ACCESS_SELECTOR: [u8; 4] =
    IMegaAccessControl::enableVolatileDataAccessCall::SELECTOR;

/// The 4-byte selector for `isVolatileDataAccessDisabled()`.
const IS_VOLATILE_DATA_ACCESS_DISABLED_SELECTOR: [u8; 4] =
    IMegaAccessControl::isVolatileDataAccessDisabledCall::SELECTOR;

/// The 4-byte selector for `VolatileDataAccessDisabled()` error.
const VOLATILE_DATA_ACCESS_DISABLED_SELECTOR: [u8; 4] =
    IMegaAccessControl::VolatileDataAccessDisabled::SELECTOR;

/// The 4-byte selector for `DisabledByParent()` error.
const DISABLED_BY_PARENT_SELECTOR: [u8; 4] = IMegaAccessControl::DisabledByParent::SELECTOR;

/// The 4-byte selector for `NotIntercepted()` error.
const NOT_INTERCEPTED_SELECTOR: [u8; 4] = IMegaAccessControl::NotIntercepted::SELECTOR;
/// The 4-byte selector for `NonZeroTransfer()` error.
const NON_ZERO_TRANSFER_SELECTOR: [u8; 4] = IMegaAccessControl::NonZeroTransfer::SELECTOR;

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

/// Decodes `VolatileDataAccessDisabled(uint8 accessType)` from revert data.
fn decode_volatile_data_access_disabled(
    data: &[u8],
) -> IMegaAccessControl::VolatileDataAccessDisabled {
    <IMegaAccessControl::VolatileDataAccessDisabled as SolError>::abi_decode(data)
        .expect("valid VolatileDataAccessDisabled revert data")
}

/// Executes a transaction on Rex4 spec.
fn transact(
    db: &mut MemoryDatabase,
    tx: TxEnv,
) -> Result<ResultAndState<MegaHaltReason>, EVMError<Infallible, MegaTransactionError>> {
    let mut context = MegaContext::new(db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(tx);
    tx.enveloped_tx = Some(Bytes::new());
    alloy_evm::Evm::transact_raw(&mut evm, tx)
}

/// Builds a default transaction calling a contract.
fn default_tx(to: Address) -> TxEnv {
    TxEnvBuilder::default().caller(CALLER).call(to).gas_limit(100_000_000).build_fill()
}

/// Builds bytecode that calls `disableVolatileDataAccess()` on the access control contract.
fn call_disable_volatile_data_access(builder: BytecodeBuilder) -> BytecodeBuilder {
    // Store selector in memory
    let builder = builder.mstore(0x0, DISABLE_VOLATILE_DATA_ACCESS_SELECTOR);
    // CALL(gas, addr, value, argsOffset, argsSize, retOffset, retSize)
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(4_u64) // argsSize (4-byte selector)
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64) // gas
        .append(CALL)
        .append(POP) // discard success flag
}

/// Builds bytecode that calls `enableVolatileDataAccess()` on the access control contract.
fn call_enable_volatile_data_access(builder: BytecodeBuilder) -> BytecodeBuilder {
    let builder = builder.mstore(0x0, ENABLE_VOLATILE_DATA_ACCESS_SELECTOR);
    builder
        .push_number(0_u64)
        .push_number(0_u64)
        .push_number(4_u64)
        .push_number(0_u64)
        .push_number(0_u64)
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64)
        .append(CALL)
        .append(POP)
}

/// Builds bytecode that calls `isVolatileDataAccessDisabled()` and returns the bool result.
/// The CALL return data (32-byte ABI-encoded bool) is returned as the frame output.
fn call_is_volatile_data_access_disabled(builder: BytecodeBuilder) -> BytecodeBuilder {
    let builder = builder.mstore(0x0, IS_VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    builder
        .push_number(32_u64) // retSize (32-byte bool)
        .push_number(0x20_u64) // retOffset (put return data at 0x20 to not overwrite calldata)
        .push_number(4_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64)
        .append(CALL)
        .append(POP) // discard success flag
        // Return the 32-byte bool output
        .push_number(32_u64) // size
        .push_number(0x20_u64) // offset
        .append(RETURN)
}

/// Builds bytecode that CALLs a target address with the given gas.
fn append_call(builder: BytecodeBuilder, target: Address, gas: u64) -> BytecodeBuilder {
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(0_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(target)
        .push_number(gas)
        .append(CALL)
}

/// Builds bytecode that STATICCALLs a target address with the given gas.
fn append_staticcall(builder: BytecodeBuilder, target: Address, gas: u64) -> BytecodeBuilder {
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(0_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_address(target)
        .push_number(gas)
        .append(STATICCALL)
}

/// Builds bytecode that STATICCALLs the access control contract with the given selector.
fn append_access_control_staticcall(
    builder: BytecodeBuilder,
    selector: [u8; 4],
    gas: u64,
) -> BytecodeBuilder {
    let builder = builder.mstore(0x0, selector);
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(4_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(gas)
        .append(STATICCALL)
}

/// Builds bytecode that DELEGATECALLs a target address with the given gas.
fn append_delegatecall(builder: BytecodeBuilder, target: Address, gas: u64) -> BytecodeBuilder {
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(0_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_address(target)
        .push_number(gas)
        .append(DELEGATECALL)
}

/// Appends bytecode that emits a LOG0 with the top-of-stack value (expected: CALL success flag).
/// Consumes the top stack value.
fn append_log_call_status(builder: BytecodeBuilder) -> BytecodeBuilder {
    builder
        .push_number(0_u64) // memory offset
        .append(MSTORE) // MSTORE(0, success_flag)
        .push_number(32_u64) // size (32 bytes, the full word)
        .push_number(0_u64) // offset
        .append(LOG0)
}

/// Asserts that the log at the given index contains the expected CALL success flag.
fn assert_log_call_status(
    result: &ResultAndState<MegaHaltReason>,
    log_index: usize,
    expected_success: bool,
) {
    let logs = result.result.logs();
    assert!(
        logs.len() > log_index,
        "Expected at least {} log(s), got {}",
        log_index + 1,
        logs.len()
    );
    let value = U256::from_be_slice(logs[log_index].data.data.as_ref());
    let expected = if expected_success { U256::from(1) } else { U256::ZERO };
    assert_eq!(value, expected, "Log[{log_index}] call status: expected {expected}, got {value}");
}

/// Appends CALL that captures return data, then returns it.
fn append_call_and_return_data(
    builder: BytecodeBuilder,
    target: Address,
    gas: u64,
) -> BytecodeBuilder {
    append_call(builder, target, gas)
        .append(POP) // discard CALL success flag
        // Copy return data to memory
        .append(RETURNDATASIZE) // size
        .push_number(0_u64) // dataOffset
        .push_number(0_u64) // destOffset
        .append(RETURNDATACOPY)
        // Return the return data
        .append(RETURNDATASIZE)
        .push_number(0_u64) // offset
        .append(RETURN)
}

// ============================================================================
// 1. BASIC disableVolatileDataAccess() BEHAVIOR
// ============================================================================

/// Inner call that accesses TIMESTAMP should revert when volatile access is disabled.
#[test]
fn test_inner_call_timestamp_reverts() {
    // Child bytecode: just reads TIMESTAMP
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent bytecode: call disableVolatileDataAccess(), then call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);
}

/// Caller's own frame IS restricted after calling `disableVolatileDataAccess()`.
#[test]
fn test_caller_frame_is_restricted() {
    // Parent bytecode: call disableVolatileDataAccess(), then read TIMESTAMP itself
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = parent_code.append(TIMESTAMP).append(POP).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    // The parent should revert because its own frame is restricted after disabling.
    assert!(
        !result.result.is_success(),
        "Caller should revert when accessing TIMESTAMP after disabling, got: {:?}",
        result.result
    );
}

/// Inner call that does NOT access volatile data should succeed.
#[test]
fn test_inner_call_without_volatile_access_succeeds() {
    // Child bytecode: just does some arithmetic and stops
    let child_code = BytecodeBuilder::default()
        .push_number(1_u64)
        .push_number(2_u64)
        .append(ADD)
        .append(POP)
        .stop()
        .build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Inner call without volatile access should succeed");
    assert_log_call_status(&result, 0, true);
}

// ============================================================================
// 2. VOLATILE OPCODE COVERAGE
// ============================================================================

/// Test that multiple volatile opcodes each trigger revert in inner calls.
#[test]
fn test_volatile_opcodes_all_revert_in_inner_call() {
    let volatile_opcodes: &[(u8, &str)] = &[
        (TIMESTAMP, "TIMESTAMP"),
        (NUMBER, "NUMBER"),
        (COINBASE, "COINBASE"),
        (DIFFICULTY, "DIFFICULTY"),
        (GASLIMIT, "GASLIMIT"),
        (BASEFEE, "BASEFEE"),
        (BLOCKHASH, "BLOCKHASH"),
        (BLOBBASEFEE, "BLOBBASEFEE"),
        (BLOBHASH, "BLOBHASH"),
    ];

    for &(opcode, name) in volatile_opcodes {
        // Child code: execute the volatile opcode, then POP and STOP
        let child_code = if opcode == BLOCKHASH || opcode == BLOBHASH {
            // BLOCKHASH and BLOBHASH need an argument on the stack
            BytecodeBuilder::default().push_number(0_u64).append(opcode).append(POP).stop().build()
        } else {
            BytecodeBuilder::default().append(opcode).append(POP).stop().build()
        };

        // Parent: disable volatile access, call child, log call status
        let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
        let parent_code = append_call(parent_code, CHILD, 50_000_000);
        let parent_code = append_log_call_status(parent_code).stop().build();

        let mut db = MemoryDatabase::default()
            .account_balance(CALLER, U256::from(1_000_000))
            .account_code(PARENT, parent_code)
            .account_code(CHILD, child_code);

        let result = transact(&mut db, default_tx(PARENT)).unwrap();
        assert!(result.result.is_success(), "Parent tx should succeed for opcode {name}");
        assert_log_call_status(&result, 0, false);
    }
}

// ============================================================================
// 3. REVERT DATA CONTAINS ABI-ENCODED ERROR
// ============================================================================

/// The revert data from an inner call that accesses volatile data should contain
/// the `VolatileDataAccessDisabled(uint8)` error with the correct access type.
#[test]
fn test_revert_data_contains_error_with_access_type() {
    // Child: reads TIMESTAMP (will revert)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: disable volatile access, call child, capture revert data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR, "Selector mismatch");
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Timestamp,
        "Access type should be Timestamp (1)"
    );
}

// ============================================================================
// 4. NESTED CALLS
// ============================================================================

/// Volatile access restriction propagates through nested calls.
/// A → B (disableVolatileDataAccess) → C → D (reads TIMESTAMP) → reverts.
#[test]
fn test_nested_call_volatile_access_reverts() {
    // Grandchild: reads TIMESTAMP
    let grandchild_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Child: calls grandchild and logs grandchild's call status
    let child_code = append_call(BytecodeBuilder::default(), GRANDCHILD, 40_000_000);
    let child_code = append_log_call_status(child_code).stop().build();

    // Parent: disable volatile access, call child, log child's call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code)
        .account_code(GRANDCHILD, grandchild_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed even though grandchild reverted");
    // Child succeeded (returned to parent), but grandchild reverted.
    // Log[0] is from child logging grandchild's call status (0 = reverted).
    // Log[1] is from parent logging child's call status (1 = succeeded).
    assert_log_call_status(&result, 0, false);
    assert_log_call_status(&result, 1, true);
}

// ============================================================================
// 5. GAS ACCOUNTING
// ============================================================================

/// Reverted inner call returns gas to parent.
#[test]
fn test_reverted_inner_call_returns_gas() {
    // Child: reads TIMESTAMP (will revert immediately)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: disable volatile access, call child with limited gas, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 10_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The gas used should be relatively small since child reverted immediately
    // and gas was returned to parent
    let gas_used = result.result.gas_used();
    assert!(gas_used < 5_000_000, "Gas used ({gas_used}) should be relatively small");
}

// ============================================================================
// 6. CALL VARIANTS
// ============================================================================

/// STATICCALL inner call that accesses volatile data also reverts.
#[test]
fn test_staticcall_also_restricted() {
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_staticcall(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed with STATICCALL");
    assert_log_call_status(&result, 0, false);
}

/// DELEGATECALL inner call that accesses volatile data also reverts.
#[test]
fn test_delegatecall_also_restricted() {
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_delegatecall(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed with DELEGATECALL");
    assert_log_call_status(&result, 0, false);
}

// ============================================================================
// 7. BACKWARD COMPATIBILITY
// ============================================================================

/// On Rex3, calling the access control address has no special effect (contract not deployed).
#[test]
fn test_pre_rex4_no_interception() {
    // On Rex3, the access control contract is not deployed, so calling it
    // should not activate any volatile access restrictions.
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: attempt to call disableVolatileDataAccess, then call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    // Use Rex3 spec
    let mut context = MegaContext::new(&mut db, MegaSpecId::REX3);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();

    // The child should succeed (TIMESTAMP not blocked on Rex3)
    assert!(result.result.is_success(), "On Rex3, volatile access should NOT be disabled");
    assert_log_call_status(&result, 0, true);
}

// ============================================================================
// 8. CONDITIONAL VOLATILE OPCODES
// ============================================================================

/// BALANCE on a non-beneficiary address should NOT revert when volatile access is disabled,
/// because it does not actually access volatile data.
#[test]
fn test_balance_non_beneficiary_not_restricted() {
    // Child: BALANCE of a non-beneficiary address (CHILD itself)
    let child_code = BytecodeBuilder::default()
        .push_address(CHILD) // non-beneficiary address
        .append(BALANCE)
        .append(POP)
        .stop()
        .build();

    // Parent: disable volatile access, then call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "BALANCE on non-beneficiary should not be restricted");
    assert_log_call_status(&result, 0, true);
}

/// BALANCE on the beneficiary address SHOULD revert when volatile access is disabled.
#[test]
fn test_balance_beneficiary_restricted() {
    // The default beneficiary is Address::ZERO in tests.
    let beneficiary = Address::ZERO;

    // Child: BALANCE of the beneficiary address
    let child_code = BytecodeBuilder::default()
        .push_address(beneficiary)
        .append(BALANCE)
        .append(POP)
        .stop()
        .build();

    // Parent: disable volatile access, call child, capture revert data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // The inner call should have reverted with VolatileDataAccessDisabled(Beneficiary)
    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "Access type should be Beneficiary (10)"
    );
}

/// EXTCODESIZE on a non-beneficiary address should NOT revert when volatile access is disabled.
#[test]
fn test_extcodesize_non_beneficiary_not_restricted() {
    // Child: EXTCODESIZE of a non-beneficiary address
    let child_code = BytecodeBuilder::default()
        .push_address(CHILD)
        .append(EXTCODESIZE)
        .append(POP)
        .stop()
        .build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "EXTCODESIZE on non-beneficiary should not be restricted");
    assert_log_call_status(&result, 0, true);
}

/// When parent accesses volatile data (TIMESTAMP) before calling `disableVolatileDataAccess()`,
/// the child should still be restricted when accessing the same volatile data type.
/// The pre-execution check in the instruction handler detects the disabled state regardless of
/// whether the bitmap bit is already set.
#[test]
fn test_parent_accesses_volatile_then_child_restricted() {
    // Child: reads TIMESTAMP (should revert because volatile access is disabled)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: read TIMESTAMP first, then disable volatile access, then call child
    let parent_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP);
    let parent_code = call_disable_volatile_data_access(parent_code);
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // The inner call should have reverted with VolatileDataAccessDisabled(Timestamp)
    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Timestamp,
        "Child should revert with access type Timestamp even though parent already accessed it"
    );
}

// ============================================================================
// 9. SIBLING CALL SCOPING
// ============================================================================

/// When C1 calls `disableVolatileDataAccess()` and returns, sibling C2's children should NOT be
/// restricted. The disable flag should be deactivated when C1's frame returns.
///
/// ```text
/// PARENT (depth 2) → C1 (depth 3) → disableVolatileDataAccess() → return
///                   → C2 (depth 3) → GRANDCHILD (depth 4) reads TIMESTAMP → should succeed
/// ```
#[test]
fn test_sibling_call_not_restricted() {
    // C1: calls disableVolatileDataAccess() and returns
    let c1_code = call_disable_volatile_data_access(BytecodeBuilder::default()).stop().build();

    // GRANDCHILD: reads TIMESTAMP (should succeed because C2 never disabled volatile access)
    let grandchild_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // C2 (SIBLING): calls GRANDCHILD and propagates the call result
    // CALL returns 1 on success, 0 on failure. Store to memory and return it.
    let c2_code = append_call(BytecodeBuilder::default(), GRANDCHILD, 40_000_000)
        // Stack: [success_flag]
        .push_number(0_u64) // memory offset
        .append(MSTORE) // MSTORE(offset=0, value=success_flag)
        .push_number(32_u64) // size
        .push_number(0_u64) // offset
        .append(RETURN)
        .build();

    // PARENT: call C1, then call C2 and return C2's output
    let parent_code = append_call(BytecodeBuilder::default(), CHILD, 50_000_000)
        .append(POP) // discard C1's success flag
        // Now call C2 and capture its return data
        .push_number(32_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(0_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(SIBLING)
        .push_number(50_000_000_u64) // gas
        .append(CALL)
        .append(POP) // discard C2's success flag
        // Return C2's return data (the GRANDCHILD call result stored at memory[0])
        .push_number(32_u64) // size
        .push_number(0_u64) // offset
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, c1_code) // C1
        .account_code(SIBLING, c2_code) // C2
        .account_code(GRANDCHILD, grandchild_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // The return data should contain the GRANDCHILD call success flag (1 = success).
    let output = result.result.output().expect("Should have output");
    let grandchild_success = U256::from_be_slice(output.as_ref());
    assert_eq!(
        grandchild_success,
        U256::from(1),
        "GRANDCHILD should succeed: C2's child should NOT be restricted by C1's disableVolatileDataAccess()"
    );
}

// ============================================================================
// 10. enableVolatileDataAccess()
// ============================================================================

/// Same frame disables then enables, child can access volatile data.
#[test]
fn test_enable_after_disable_succeeds() {
    // Child: reads TIMESTAMP (should succeed because parent re-enabled)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: disable, then enable, then call child
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = call_enable_volatile_data_access(parent_code);
    let parent_code = append_call(parent_code, CHILD, 50_000_000)
        // CALL returns 1 on success, 0 on failure. Store and return.
        .push_number(0_u64)
        .append(MSTORE)
        .push_number(32_u64)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    let child_success = U256::from_be_slice(output.as_ref());
    assert_eq!(
        child_success,
        U256::from(1),
        "Child should succeed after parent re-enabled volatile data access"
    );
}

/// Parent disables, child tries to enable → reverts with `DisabledByParent()`.
#[test]
fn test_enable_by_child_reverts_when_parent_disabled() {
    // Child: tries to call enableVolatileDataAccess(), captures return data
    let child_code = call_enable_volatile_data_access(BytecodeBuilder::default());
    // enableVolatileDataAccess() call will have reverted. Capture the return data from it.
    // The enable CALL returns 0 on failure. Let's just return the enable call's return data.
    let child_code = child_code
        .append(RETURNDATASIZE)
        .push_number(0_u64)
        .push_number(0_u64)
        .append(RETURNDATACOPY)
        .append(RETURNDATASIZE)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    // Parent: disable volatile access, call child, capture child's return data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(
        output.as_ref(),
        &DISABLED_BY_PARENT_SELECTOR,
        "Child's enable call should revert with DisabledByParent()"
    );
}

/// Calling `enableVolatileDataAccess()` when not disabled is a no-op (succeeds).
#[test]
fn test_enable_when_not_disabled_is_noop() {
    // Parent: just call enableVolatileDataAccess() without prior disable
    let parent_code = call_enable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = parent_code.stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(
        result.result.is_success(),
        "enableVolatileDataAccess() when not disabled should succeed"
    );
}

/// STATICCALL to `disableVolatileDataAccess()` should apply the same restriction as CALL.
#[test]
fn test_staticcall_disable_volatile_data_access_is_intercepted() {
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    let parent_code = append_access_control_staticcall(
        BytecodeBuilder::default(),
        DISABLE_VOLATILE_DATA_ACCESS_SELECTOR,
        100_000,
    );
    let parent_code = append_log_call_status(parent_code);
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, true);
    assert_log_call_status(&result, 1, false);
}

/// STATICCALL to `enableVolatileDataAccess()` should re-enable access when the caller disabled it.
#[test]
fn test_staticcall_enable_volatile_data_access_is_intercepted() {
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_access_control_staticcall(
        parent_code,
        ENABLE_VOLATILE_DATA_ACCESS_SELECTOR,
        100_000,
    );
    let parent_code = append_log_call_status(parent_code);
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, true);
    assert_log_call_status(&result, 1, true);
}

// ============================================================================
// 11. isVolatileDataAccessDisabled()
// ============================================================================

/// Query returns false when no disable is active.
#[test]
fn test_query_returns_false_when_not_disabled() {
    // Parent: call isVolatileDataAccessDisabled() and return the result
    let parent_code = call_is_volatile_data_access_disabled(BytecodeBuilder::default()).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    let disabled = U256::from_be_slice(output.as_ref());
    assert_eq!(disabled, U256::ZERO, "Should return false when not disabled");
}

/// Query returns true when a parent disabled volatile data access.
#[test]
fn test_query_returns_true_when_parent_disabled() {
    // Child: call isVolatileDataAccessDisabled() and return the result
    let child_code = call_is_volatile_data_access_disabled(BytecodeBuilder::default()).build();

    // Parent: disable, then call child that queries
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    let disabled = U256::from_be_slice(output.as_ref());
    assert_eq!(disabled, U256::from(1), "Should return true when parent disabled");
}

/// Query returns true for the frame that called disable (caller IS now restricted).
#[test]
fn test_query_returns_true_for_disabling_frame() {
    // Parent: disable, then query isVolatileDataAccessDisabled() in same frame
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = call_is_volatile_data_access_disabled(parent_code).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    let disabled = U256::from_be_slice(output.as_ref());
    assert_eq!(
        disabled,
        U256::from(1),
        "Should return true when queried in the frame that called disable"
    );
}

// ============================================================================
// 12. DIRECT TX CALLS TO ACCESS CONTROL CONTRACT
// ============================================================================

/// Helper to build a TX that calls the access control contract directly with the given selector.
fn direct_access_control_tx(selector: &[u8; 4]) -> TxEnv {
    direct_access_control_tx_with_value(selector, U256::ZERO)
}

/// Helper to build a TX that calls the access control contract directly with selector and value.
fn direct_access_control_tx_with_value(selector: &[u8; 4], value: U256) -> TxEnv {
    TxEnvBuilder::default()
        .caller(CALLER)
        .call(ACCESS_CONTROL_ADDRESS)
        .value(value)
        .gas_limit(100_000_000)
        .data(Bytes::copy_from_slice(selector))
        .build_fill()
}

/// Direct TX calling `disableVolatileDataAccess()` should succeed without frame stack underflow.
#[test]
fn test_direct_tx_disable_volatile_data_access() {
    let mut db = MemoryDatabase::default().account_balance(CALLER, U256::from(1_000_000));

    let result =
        transact(&mut db, direct_access_control_tx(&DISABLE_VOLATILE_DATA_ACCESS_SELECTOR))
            .unwrap();
    assert!(
        result.result.is_success(),
        "Direct TX to disableVolatileDataAccess should succeed, got: {:?}",
        result.result
    );
}

/// Direct TX calling `disableVolatileDataAccess()` with non-zero value should revert.
#[test]
fn test_direct_tx_disable_volatile_data_access_with_value_reverts() {
    let mut db = MemoryDatabase::default().account_balance(CALLER, U256::from(1_000_000));

    let result = transact(
        &mut db,
        direct_access_control_tx_with_value(
            &DISABLE_VOLATILE_DATA_ACCESS_SELECTOR,
            U256::from(1_u64),
        ),
    )
    .unwrap();
    assert!(
        !result.result.is_success(),
        "Direct TX with non-zero value should revert, got: {:?}",
        result.result
    );
    let output = result.result.output().expect("Should have output");
    assert_eq!(output.len(), 4, "non-zero transfer revert should return selector only");
    assert_eq!(
        &output[..4],
        &NON_ZERO_TRANSFER_SELECTOR,
        "non-zero transfer should revert with NonZeroTransfer()"
    );
}

/// Direct TX calling `enableVolatileDataAccess()` should succeed without frame stack underflow.
#[test]
fn test_direct_tx_enable_volatile_data_access() {
    let mut db = MemoryDatabase::default().account_balance(CALLER, U256::from(1_000_000));

    let result =
        transact(&mut db, direct_access_control_tx(&ENABLE_VOLATILE_DATA_ACCESS_SELECTOR)).unwrap();
    assert!(
        result.result.is_success(),
        "Direct TX to enableVolatileDataAccess should succeed, got: {:?}",
        result.result
    );
}

/// Direct TX calling `isVolatileDataAccessDisabled()` should succeed and return false.
#[test]
fn test_direct_tx_is_volatile_data_access_disabled() {
    let mut db = MemoryDatabase::default().account_balance(CALLER, U256::from(1_000_000));

    let result =
        transact(&mut db, direct_access_control_tx(&IS_VOLATILE_DATA_ACCESS_DISABLED_SELECTOR))
            .unwrap();
    assert!(
        result.result.is_success(),
        "Direct TX to isVolatileDataAccessDisabled should succeed, got: {:?}",
        result.result
    );

    let output = result.result.output().expect("Should have output");
    let disabled = U256::from_be_slice(output.as_ref());
    assert_eq!(disabled, U256::ZERO, "Should return false when called directly by TX");
}

/// Direct TX with unknown selector should fall through and revert with `NotIntercepted()`.
#[test]
fn test_direct_tx_unknown_selector_falls_through_and_reverts_not_intercepted() {
    let unknown_selector = [0xde, 0xad, 0xbe, 0xef];
    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(ACCESS_CONTROL_ADDRESS, mega_evm::ACCESS_CONTROL_CODE);

    let result = transact(&mut db, direct_access_control_tx(&unknown_selector)).unwrap();
    assert!(
        !result.result.is_success(),
        "Unknown selector should fall through and revert, got: {:?}",
        result.result
    );

    let output = result.result.output().expect("Should have output");
    assert_eq!(
        output.len(),
        4,
        "Fallback should return only NotIntercepted selector, got {} bytes",
        output.len()
    );
    assert_eq!(
        &output[..4],
        &NOT_INTERCEPTED_SELECTOR,
        "Unknown selector should revert with NotIntercepted()"
    );
}

// ============================================================================
// 13. INSPECTOR VISIBILITY FOR SYSTEM CONTRACT CALLS
// ============================================================================

/// An inspector that records all `call` and `call_end` invocations.
#[derive(Default)]
struct CallTrackingInspector {
    /// Target addresses seen in `call` hooks, in order.
    calls: Vec<Address>,
    /// Target addresses seen in `call_end` hooks, in order.
    call_ends: Vec<Address>,
}

impl<CTX: ContextTr, INTR: InterpreterTypes> Inspector<CTX, INTR> for CallTrackingInspector {
    fn call(&mut self, _context: &mut CTX, inputs: &mut CallInputs) -> Option<CallOutcome> {
        self.calls.push(inputs.target_address);
        None
    }

    fn call_end(&mut self, _context: &mut CTX, inputs: &CallInputs, _outcome: &mut CallOutcome) {
        self.call_ends.push(inputs.target_address);
    }
}

/// When an inspector is attached, it should see `call` and `call_end` for system contract
/// calls that are intercepted in `frame_init`, even though no interpreter frame is created.
#[test]
fn test_inspector_sees_system_contract_call() {
    // Child: simple arithmetic, no volatile access
    let child_code = BytecodeBuilder::default()
        .push_number(1_u64)
        .push_number(2_u64)
        .append(ADD)
        .append(POP)
        .stop()
        .build();

    // Parent: call disableVolatileDataAccess(), then call CHILD, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let mut inspector = CallTrackingInspector::default();
    let mut evm = MegaEvm::new(context).with_inspector(&mut inspector);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Transaction should succeed");

    // Inspector should have seen 3 calls:
    // 1. Top-level call to PARENT
    // 2. PARENT -> ACCESS_CONTROL_ADDRESS (intercepted in frame_init)
    // 3. PARENT -> CHILD (normal call)
    assert_eq!(
        inspector.calls.len(),
        3,
        "Inspector should see 3 call hooks: top-level + access control + child, got: {:?}",
        inspector.calls
    );
    assert_eq!(inspector.calls[0], PARENT, "First call should be to PARENT");
    assert_eq!(
        inspector.calls[1], ACCESS_CONTROL_ADDRESS,
        "Second call should be to ACCESS_CONTROL_ADDRESS"
    );
    assert_eq!(inspector.calls[2], CHILD, "Third call should be to CHILD");

    // call_end should be invoked for all 3 calls, in reverse order
    assert_eq!(
        inspector.call_ends.len(),
        3,
        "Inspector should see 3 call_end hooks, got: {:?}",
        inspector.call_ends
    );
    assert_eq!(
        inspector.call_ends[0], ACCESS_CONTROL_ADDRESS,
        "First call_end should be ACCESS_CONTROL_ADDRESS (innermost, returned first)"
    );
    assert_eq!(inspector.call_ends[1], CHILD, "Second call_end should be CHILD");
    assert_eq!(inspector.call_ends[2], PARENT, "Third call_end should be PARENT (outermost)");

    assert_log_call_status(&result, 0, true);
}

// ============================================================================
// 14. GAS COST OF SYSTEM CONTRACT CALL
// ============================================================================

/// The system contract call intercepted in `frame_init` should only consume the CALL opcode
/// overhead (warm account access = 100 gas), not the `gas_limit` forwarded to the child frame.
/// The child frame's gas is fully refunded since the interception returns `Gas::new(gas_limit)`.
#[test]
fn test_system_contract_call_gas_cost() {
    // Parent bytecode:
    // 1. GAS                         — push gas_before
    // 2. CALL(100_000, ACCESS_CONTROL, 0, disableVolatileDataAccess selector)
    // 3. POP                         — discard CALL success flag
    // 4. GAS                         — push gas_after
    // 5. SWAP1                       — [gas_after, gas_before] -> [gas_before, gas_after]
    // 6. SUB                         — gas_before - gas_after = gas_consumed
    // 7. MSTORE at 0x20              — store gas_consumed
    // 8. RETURN 32 bytes from 0x20
    //
    // Note: the GAS opcode itself costs 2 gas. The measured delta includes the cost of
    // setting up the CALL arguments (pushes), the CALL opcode overhead, POP, and one GAS.
    // The CALL forwards 100,000 gas to the child frame, which should all come back.

    // First, store the selector in memory (needed for CALL input)
    let parent_code = BytecodeBuilder::default().mstore(0x0, DISABLE_VOLATILE_DATA_ACCESS_SELECTOR);

    let parent_code = parent_code
        .append(GAS) // gas_before (costs 2 gas, measured AFTER this)
        // Set up CALL arguments
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(4_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64) // gas to forward
        .append(CALL)
        .append(POP) // discard success flag
        .append(GAS) // gas_after
        // gas_before is below gas_after on the stack: [gas_before, gas_after]
        // SWAP1 to get [gas_after, gas_before], then SUB = gas_before - gas_after
        .append(SWAP1)
        .append(SUB) // gas_consumed = gas_before - gas_after
        // Store and return
        .push_number(0x20_u64)
        .append(MSTORE)
        .push_number(32_u64) // size
        .push_number(0x20_u64) // offset
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Transaction should succeed");

    let output = result.result.output().expect("Should have output");
    let gas_consumed = U256::from_be_slice(output.as_ref()).to::<u64>();

    // The gas consumed by the measured region includes:
    // - 7 PUSH instructions (3 gas each = 21 gas) for CALL arguments
    // - 1 PUSH20 for address (3 gas)
    // - CALL cold account access (2600 gas, since ACCESS_CONTROL_ADDRESS is first accessed here)
    // - POP (2 gas)
    // - GAS (2 gas)
    // - SWAP1 (3 gas)
    // Total overhead ≈ 2631 gas
    //
    // Critically, it should NOT include the 100,000 gas forwarded to the child frame,
    // because the intercepted call returns all gas.
    assert!(
        gas_consumed < 3000,
        "System contract call gas delta should be small (CALL overhead only), got: {gas_consumed}. \
         If this is close to 100,000, the child frame gas was not refunded."
    );

    // Sanity check: it should at least include the cold CALL cost (2600)
    assert!(
        gas_consumed >= 2600,
        "Gas consumed ({gas_consumed}) should be at least 2600 (cold CALL cost)"
    );
}

// ============================================================================
// 15. BLOCKED VOLATILE ACCESS DOES NOT POLLUTE TRACKER
// ============================================================================

/// When volatile data access is disabled and a child frame attempts to read volatile data
/// (causing it to revert), the `volatile_data_accessed` bitmap and `compute_gas_limit`
/// should NOT be modified.
/// The instruction handler reverts before the opcode executes, so no tracker state is changed.
#[test]
fn test_blocked_volatile_access_does_not_set_bitmap() {
    // Child: reads TIMESTAMP (will revert because volatile access is disabled)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: disable volatile access, call child (which reverts), log call status, then stop.
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The blocked TIMESTAMP access should NOT have set the volatile_data_accessed bitmap.
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "volatile_data_accessed should be empty after blocked access, got: {:?}",
        tracker.get_volatile_data_accessed()
    );
    assert!(
        tracker.get_compute_gas_limit().is_none(),
        "compute_gas_limit should not be set after blocked access, got: {:?}",
        tracker.get_compute_gas_limit()
    );
}

/// When volatile data access is disabled, BALANCE on the beneficiary should revert
/// and NOT pollute the `volatile_data_accessed` bitmap or `compute_gas_limit`.
#[test]
fn test_blocked_beneficiary_balance_does_not_set_bitmap() {
    let beneficiary = Address::ZERO;

    // Child: BALANCE of the beneficiary (will revert because volatile access is disabled)
    let child_code = BytecodeBuilder::default()
        .push_address(beneficiary)
        .append(BALANCE)
        .append(POP)
        .stop()
        .build();

    // Parent: disable volatile access, call child (which reverts), log call status, then stop.
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The blocked BALANCE(beneficiary) access should NOT have set the bitmap.
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "volatile_data_accessed should be empty after blocked beneficiary BALANCE, got: {:?}",
        tracker.get_volatile_data_accessed()
    );
    assert!(
        tracker.get_compute_gas_limit().is_none(),
        "compute_gas_limit should not be set after blocked beneficiary BALANCE, got: {:?}",
        tracker.get_compute_gas_limit()
    );
}

/// When volatile data access is disabled, SLOAD on the oracle contract should revert
/// and NOT pollute the `volatile_data_accessed` bitmap or `compute_gas_limit`.
#[test]
fn test_blocked_oracle_sload_does_not_set_bitmap() {
    // Oracle contract code: SLOAD(0) and return.
    // When called by an inner frame with volatile access disabled, this triggers the
    // pre-execution check in the SLOAD instruction handler and reverts.
    let oracle_code = BytecodeBuilder::default()
        .push_number(0_u64) // key = 0
        .append(SLOAD)
        .append(POP)
        .stop()
        .build();

    // Parent: disable volatile access, then call the oracle contract (which will revert),
    // log call status, then stop.
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, ORACLE_CONTRACT_ADDRESS, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(ORACLE_CONTRACT_ADDRESS, oracle_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The blocked oracle SLOAD should NOT have set the bitmap.
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "volatile_data_accessed should be empty after blocked oracle SLOAD, got: {:?}",
        tracker.get_volatile_data_accessed()
    );
    assert!(
        tracker.get_compute_gas_limit().is_none(),
        "compute_gas_limit should not be set after blocked oracle SLOAD, got: {:?}",
        tracker.get_compute_gas_limit()
    );
}

// ============================================================================
// 16. DELEGATECALL / CALLCODE TO ACCESS CONTROL CONTRACT
// ============================================================================

/// Builds bytecode that CALLCODEs a target address with the given gas and no calldata.
fn append_callcode(builder: BytecodeBuilder, target: Address, gas: u64) -> BytecodeBuilder {
    builder
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(0_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(target)
        .push_number(gas)
        .append(CALLCODE)
}

/// Executes a transaction on Rex4 spec with oracle external environment.
fn transact_with_oracle(
    db: &mut MemoryDatabase,
    tx: TxEnv,
) -> Result<ResultAndState<MegaHaltReason>, EVMError<Infallible, MegaTransactionError>> {
    let external_envs = TestExternalEnvs::<Infallible>::new()
        .with_oracle_storage(U256::from(0), U256::from(0x1234));
    let mut context =
        MegaContext::new(db, MegaSpecId::REX4).with_external_envs((&external_envs).into());
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(tx);
    tx.enveloped_tx = Some(Bytes::new());
    alloy_evm::Evm::transact_raw(&mut evm, tx)
}

/// DELEGATECALL to the access control contract should NOT be intercepted.
/// The underlying contract code executes and reverts with `NotIntercepted()`.
/// Subsequent inner calls that access volatile data should succeed.
#[test]
fn test_delegatecall_to_access_control_not_intercepted() {
    // Child: reads TIMESTAMP (should succeed — disable was never activated)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: DELEGATECALL access control with disableVolatileDataAccess selector,
    // then CALL child which reads TIMESTAMP.
    let parent_code = BytecodeBuilder::default().mstore(0x0, DISABLE_VOLATILE_DATA_ACCESS_SELECTOR);
    // DELEGATECALL(gas, addr, argsOffset, argsSize, retOffset, retSize)
    let parent_code = parent_code
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(4_u64) // argsSize
        .push_number(0_u64) // argsOffset
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64) // gas
        .append(DELEGATECALL)
        .append(POP); // discard success flag (should be 0 = failure due to NotIntercepted revert)
    let parent_code = append_call(parent_code, CHILD, 50_000_000)
        // Stack: [call_success]. Store and return.
        .push_number(0_u64)
        .append(MSTORE)
        .push_number(32_u64)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code)
        .account_code(ACCESS_CONTROL_ADDRESS, mega_evm::ACCESS_CONTROL_CODE);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // Child CALL should have succeeded (returned 1)
    let output = result.result.output().expect("Should have output");
    let child_success = U256::from_be_slice(output.as_ref());
    assert_eq!(
        child_success,
        U256::from(1),
        "Child should succeed because DELEGATECALL to access control was not intercepted"
    );
}

/// CALLCODE to the access control contract should NOT be intercepted.
/// The underlying contract code executes and reverts with `NotIntercepted()`.
/// Subsequent inner calls that access volatile data should succeed.
#[test]
fn test_callcode_to_access_control_not_intercepted() {
    // Child: reads TIMESTAMP (should succeed — disable was never activated)
    let child_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // Parent: CALLCODE access control with disableVolatileDataAccess selector,
    // then CALL child which reads TIMESTAMP.
    let parent_code = BytecodeBuilder::default().mstore(0x0, DISABLE_VOLATILE_DATA_ACCESS_SELECTOR);
    // CALLCODE(gas, addr, value, argsOffset, argsSize, retOffset, retSize)
    let parent_code = parent_code
        .push_number(0_u64) // retSize
        .push_number(0_u64) // retOffset
        .push_number(4_u64) // argsSize (4-byte selector)
        .push_number(0_u64) // argsOffset
        .push_number(0_u64) // value
        .push_address(ACCESS_CONTROL_ADDRESS)
        .push_number(100_000_u64) // gas
        .append(CALLCODE)
        .append(POP); // discard success flag (should be 0 = failure due to NotIntercepted revert)
    let parent_code = append_call(parent_code, CHILD, 50_000_000)
        .push_number(0_u64)
        .append(MSTORE)
        .push_number(32_u64)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code)
        .account_code(ACCESS_CONTROL_ADDRESS, mega_evm::ACCESS_CONTROL_CODE);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // Child CALL should have succeeded (returned 1)
    let output = result.result.output().expect("Should have output");
    let child_success = U256::from_be_slice(output.as_ref());
    assert_eq!(
        child_success,
        U256::from(1),
        "Child should succeed because CALLCODE to access control was not intercepted"
    );
}

// ============================================================================
// 17. DISABLE IN REVERTED CHILD DOES NOT AFFECT SIBLING
// ============================================================================

/// When a child frame calls `disableVolatileDataAccess()` and then reverts, the disable
/// should be cleared when the child's frame returns (via `enable_access_if_returning`).
/// A sibling call should NOT be restricted.
///
/// ```text
/// PARENT → CHILD (calls disable, then REVERTs)
///        → SIBLING (reads TIMESTAMP) → should succeed
/// ```
#[test]
fn test_disable_in_reverted_child_does_not_affect_sibling() {
    // CHILD: calls disableVolatileDataAccess(), then immediately reverts
    let child_code = call_disable_volatile_data_access(BytecodeBuilder::default())
        .push_number(0_u64) // size
        .push_number(0_u64) // offset
        .append(REVERT)
        .build();

    // SIBLING: reads TIMESTAMP (should succeed)
    let sibling_code = BytecodeBuilder::default().append(TIMESTAMP).append(POP).stop().build();

    // PARENT: call CHILD (which reverts), log child call status, then call SIBLING,
    // return SIBLING's success flag
    let parent_code = append_call(BytecodeBuilder::default(), CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code);
    let parent_code = append_call(parent_code, SIBLING, 50_000_000)
        // Stack: [sibling_success]. Store and return.
        .push_number(0_u64)
        .append(MSTORE)
        .push_number(32_u64)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code)
        .account_code(SIBLING, sibling_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    assert_log_call_status(&result, 0, false);

    let output = result.result.output().expect("Should have output");
    let sibling_success = U256::from_be_slice(output.as_ref());
    assert_eq!(
        sibling_success,
        U256::from(1),
        "SIBLING should succeed: CHILD's disable was cleared when CHILD's frame reverted"
    );
}

// ============================================================================
// 18. ORACLE SLOAD WITH DISABLED VOLATILE ACCESS
// ============================================================================

/// Oracle SLOAD should revert with VolatileDataAccessDisabled(Oracle) when volatile
/// data access is disabled.
#[test]
fn test_oracle_sload_reverts_when_volatile_access_disabled() {
    // Oracle contract code: SLOAD(0)
    let oracle_code = BytecodeBuilder::default()
        .push_number(0_u64) // key = 0
        .append(SLOAD)
        .append(POP)
        .stop()
        .build();

    // Parent: disable volatile access, call oracle, capture return data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code =
        append_call_and_return_data(parent_code, ORACLE_CONTRACT_ADDRESS, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(ORACLE_CONTRACT_ADDRESS, oracle_code);

    let result = transact_with_oracle(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // The oracle SLOAD should have reverted with VolatileDataAccessDisabled(Oracle)
    let output = result.result.output().expect("Should have output");
    assert_eq!(
        &output[..4],
        &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR,
        "Oracle SLOAD should revert with VolatileDataAccessDisabled error"
    );
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(decoded.accessType, VolatileDataAccessType::Oracle, "Access type should be Oracle");
}

// ============================================================================
// 19. CREATE WITH DISABLED VOLATILE ACCESS
// ============================================================================

/// CREATE whose init code accesses TIMESTAMP should fail when volatile access is disabled.
/// The CREATE returns address 0 (failure), but the parent transaction itself succeeds.
#[test]
fn test_create_reverts_when_volatile_access_disabled() {
    // Init code that accesses TIMESTAMP, then returns empty deployed code.
    // TIMESTAMP -> POP -> PUSH1(0) -> PUSH1(0) -> RETURN (deploy zero-length code)
    let init_code = BytecodeBuilder::default()
        .append(TIMESTAMP)
        .append(POP)
        .push_number(0_u64) // size
        .push_number(0_u64) // offset
        .append(RETURN)
        .build();

    // Parent: disable volatile access, then CREATE with the init code, return created address.
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    // Store init code in memory at offset 0x40 (after selector storage area)
    let parent_code = parent_code
        .mstore(0x40, init_code.clone())
        .push_number(init_code.len() as u64) // size
        .push_number(0x40_u64) // offset
        .push_number(0_u64) // value
        .append(CREATE)
        // Stack: [created_address]. Store and return.
        .push_number(0_u64)
        .append(MSTORE)
        .push_number(32_u64)
        .push_number(0_u64)
        .append(RETURN)
        .build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    // CREATE should have failed (returned address 0) because init code's TIMESTAMP reverted
    let output = result.result.output().expect("Should have output");
    let created_address = U256::from_be_slice(output.as_ref());
    assert_eq!(
        created_address,
        U256::ZERO,
        "CREATE should fail (return 0) because init code accessed volatile data"
    );
}

// ============================================================================
// 20. CALL-LIKE OPCODES TARGETING BENEFICIARY
// ============================================================================

/// CALL to the beneficiary address should revert when volatile access is disabled.
/// The child contract CALLs the beneficiary; the parent captures the child's revert data.
#[test]
fn test_call_beneficiary_restricted() {
    let beneficiary = Address::ZERO;

    // Child: CALL the beneficiary address
    let child_code = append_call(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    // Parent: disable volatile access, call child, capture revert data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "CALL to beneficiary should revert with Beneficiary access type"
    );
}

/// STATICCALL to the beneficiary address should revert when volatile access is disabled.
#[test]
fn test_staticcall_beneficiary_restricted() {
    let beneficiary = Address::ZERO;

    // Child: STATICCALL the beneficiary address
    let child_code =
        append_staticcall(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "STATICCALL to beneficiary should revert with Beneficiary access type"
    );
}

/// DELEGATECALL to the beneficiary address should revert when volatile access is disabled.
#[test]
fn test_delegatecall_beneficiary_restricted() {
    let beneficiary = Address::ZERO;

    // Child: DELEGATECALL the beneficiary address
    let child_code =
        append_delegatecall(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "DELEGATECALL to beneficiary should revert with Beneficiary access type"
    );
}

/// CALLCODE to the beneficiary address should revert when volatile access is disabled.
#[test]
fn test_callcode_beneficiary_restricted() {
    let beneficiary = Address::ZERO;

    // Child: CALLCODE the beneficiary address
    let child_code =
        append_callcode(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");

    let output = result.result.output().expect("Should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "CALLCODE to beneficiary should revert with Beneficiary access type"
    );
}

/// CALL to a non-beneficiary address should NOT revert when volatile access is disabled.
#[test]
fn test_call_non_beneficiary_not_restricted() {
    // Child: CALL a non-beneficiary address (GRANDCHILD)
    let grandchild_code = BytecodeBuilder::default().stop().build();
    let child_code = append_call(BytecodeBuilder::default(), GRANDCHILD, 100_000).stop().build();

    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code)
        .account_code(GRANDCHILD, grandchild_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "CALL to non-beneficiary should not be restricted");
    assert_log_call_status(&result, 0, true);
}

/// CALL to beneficiary should work normally (with gas detention) when volatile access is NOT
/// disabled.
#[test]
fn test_call_beneficiary_not_restricted_without_disable() {
    let beneficiary = Address::ZERO;

    // Child: CALL beneficiary without disabling volatile access.
    let child_code = append_call(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    let parent_code = append_call(BytecodeBuilder::default(), CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(
        result.result.is_success(),
        "CALL to beneficiary should succeed when volatile access is not disabled"
    );
    assert_log_call_status(&result, 0, true);
}

/// Blocked CALL to beneficiary should NOT pollute the volatile data tracker.
#[test]
fn test_blocked_call_beneficiary_does_not_pollute_tracker() {
    let beneficiary = Address::ZERO;

    // Child: CALL beneficiary (will be blocked by volatile access disable)
    let child_code = append_call(BytecodeBuilder::default(), beneficiary, 100_000).stop().build();

    // Parent: disable volatile access, call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The blocked CALL to beneficiary should NOT have set the volatile_data_accessed bitmap.
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "volatile_data_accessed should be empty after blocked CALL to beneficiary"
    );
    assert!(
        tracker.get_compute_gas_limit().is_none(),
        "compute_gas_limit should not be set after blocked CALL to beneficiary"
    );
}

// ============================================================================
// 21. SELFDESTRUCT TARGETING BENEFICIARY
// ============================================================================

/// SELFDESTRUCT targeting the beneficiary should revert when volatile access is disabled.
#[test]
fn test_selfdestruct_beneficiary_restricted() {
    let beneficiary = Address::ZERO;

    // Child: SELFDESTRUCT targeting the beneficiary address
    let child_code =
        BytecodeBuilder::default().push_address(beneficiary).append(SELFDESTRUCT).build();

    // Parent: disable volatile access, call child, capture revert data
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call_and_return_data(parent_code, CHILD, 50_000_000).build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    let output = result.result.output().expect("should have output");
    assert_eq!(&output[..4], &VOLATILE_DATA_ACCESS_DISABLED_SELECTOR);
    let decoded = decode_volatile_data_access_disabled(output);
    assert_eq!(
        decoded.accessType,
        VolatileDataAccessType::Beneficiary,
        "SELFDESTRUCT to beneficiary should revert with Beneficiary access type"
    );
}

/// SELFDESTRUCT targeting a non-beneficiary address should NOT revert when volatile access
/// is disabled.
#[test]
fn test_selfdestruct_non_beneficiary_not_restricted() {
    // Child: SELFDESTRUCT targeting a non-beneficiary address (GRANDCHILD)
    let child_code =
        BytecodeBuilder::default().push_address(GRANDCHILD).append(SELFDESTRUCT).build();

    // Parent: disable volatile access, call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(result.result.is_success(), "SELFDESTRUCT to non-beneficiary should not be restricted");
    assert_log_call_status(&result, 0, true);
}

/// SELFDESTRUCT targeting the beneficiary should work (with gas detention) when volatile
/// access is NOT disabled.
#[test]
fn test_selfdestruct_beneficiary_not_restricted_without_disable() {
    let beneficiary = Address::ZERO;

    // Child: SELFDESTRUCT targeting the beneficiary
    let child_code =
        BytecodeBuilder::default().push_address(beneficiary).append(SELFDESTRUCT).build();

    // Parent: call child without disabling volatile access, log call status
    let parent_code = append_call(BytecodeBuilder::default(), CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let result = transact(&mut db, default_tx(PARENT)).unwrap();
    assert!(
        result.result.is_success(),
        "SELFDESTRUCT to beneficiary should succeed when volatile access is not disabled"
    );
    assert_log_call_status(&result, 0, true);
}

/// Blocked SELFDESTRUCT to beneficiary should NOT pollute the volatile data tracker.
#[test]
fn test_blocked_selfdestruct_beneficiary_does_not_pollute_tracker() {
    let beneficiary = Address::ZERO;

    // Child: SELFDESTRUCT targeting the beneficiary (will be blocked)
    let child_code =
        BytecodeBuilder::default().push_address(beneficiary).append(SELFDESTRUCT).build();

    // Parent: disable volatile access, call child, log call status
    let parent_code = call_disable_volatile_data_access(BytecodeBuilder::default());
    let parent_code = append_call(parent_code, CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    let mut context = MegaContext::new(&mut db, MegaSpecId::REX4);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();
    assert!(result.result.is_success(), "Parent tx should succeed");
    assert_log_call_status(&result, 0, false);

    // The blocked SELFDESTRUCT to beneficiary should NOT have set the bitmap.
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "volatile_data_accessed should be empty after blocked SELFDESTRUCT to beneficiary"
    );
    assert!(
        tracker.get_compute_gas_limit().is_none(),
        "compute_gas_limit should not be set after blocked SELFDESTRUCT to beneficiary"
    );
}

/// On Rex3 (pre-Rex4), SELFDESTRUCT targeting the beneficiary should NOT be checked or
/// restricted for volatile data access. The volatile data check is Rex4-only.
#[test]
fn test_selfdestruct_beneficiary_not_restricted_pre_rex4() {
    let beneficiary = Address::ZERO;

    // Child: SELFDESTRUCT targeting the beneficiary
    let child_code =
        BytecodeBuilder::default().push_address(beneficiary).append(SELFDESTRUCT).build();

    // Parent: call child, log call status
    let parent_code = append_call(BytecodeBuilder::default(), CHILD, 50_000_000);
    let parent_code = append_log_call_status(parent_code).stop().build();

    let mut db = MemoryDatabase::default()
        .account_balance(CALLER, U256::from(1_000_000))
        .account_code(PARENT, parent_code)
        .account_code(CHILD, child_code);

    // Use Rex3 spec (pre-Rex4)
    let mut context = MegaContext::new(&mut db, MegaSpecId::REX3);
    context.modify_chain(|chain| {
        chain.operator_fee_scalar = Some(U256::from(0));
        chain.operator_fee_constant = Some(U256::from(0));
    });
    let volatile_data_tracker = context.volatile_data_tracker.clone();

    let mut evm = MegaEvm::new(context);
    let mut tx = MegaTransaction::new(default_tx(PARENT));
    tx.enveloped_tx = Some(Bytes::new());
    let result = alloy_evm::Evm::transact_raw(&mut evm, tx).unwrap();

    // SELFDESTRUCT targeting beneficiary should succeed on Rex3
    assert!(
        result.result.is_success(),
        "On Rex3, SELFDESTRUCT to beneficiary should not be restricted"
    );
    assert_log_call_status(&result, 0, true);

    // Volatile data tracker should NOT have marked beneficiary balance access
    let tracker = volatile_data_tracker.borrow();
    assert!(
        !tracker.accessed(),
        "On Rex3, SELFDESTRUCT to beneficiary should not mark volatile data access"
    );
}